@@ -21,12 +21,17 @@ package credentials
21
21
import (
22
22
"crypto/tls"
23
23
"crypto/x509"
24
+ "encoding/pem"
25
+ "io/ioutil"
24
26
"net/url"
25
27
"testing"
26
28
27
29
"google.golang.org/grpc/internal/grpctest"
30
+ "google.golang.org/grpc/testdata"
28
31
)
29
32
33
+ const wantURI = "spiffe://foo.bar.com/client/workload/1"
34
+
30
35
type s struct {
31
36
grpctest.Tester
32
37
}
@@ -40,12 +45,12 @@ func (s) TestSPIFFEIDFromState(t *testing.T) {
40
45
name string
41
46
urls []* url.URL
42
47
// If we expect a SPIFFE ID to be returned.
43
- expectID bool
48
+ wantID bool
44
49
}{
45
50
{
46
- name : "empty URIs" ,
47
- urls : []* url.URL {},
48
- expectID : false ,
51
+ name : "empty URIs" ,
52
+ urls : []* url.URL {},
53
+ wantID : false ,
49
54
},
50
55
{
51
56
name : "good SPIFFE ID" ,
@@ -57,7 +62,7 @@ func (s) TestSPIFFEIDFromState(t *testing.T) {
57
62
RawPath : "workload/wl1" ,
58
63
},
59
64
},
60
- expectID : true ,
65
+ wantID : true ,
61
66
},
62
67
{
63
68
name : "invalid host" ,
@@ -69,7 +74,7 @@ func (s) TestSPIFFEIDFromState(t *testing.T) {
69
74
RawPath : "workload/wl1" ,
70
75
},
71
76
},
72
- expectID : false ,
77
+ wantID : false ,
73
78
},
74
79
{
75
80
name : "invalid path" ,
@@ -81,7 +86,7 @@ func (s) TestSPIFFEIDFromState(t *testing.T) {
81
86
RawPath : "" ,
82
87
},
83
88
},
84
- expectID : false ,
89
+ wantID : false ,
85
90
},
86
91
{
87
92
name : "large path" ,
@@ -93,7 +98,7 @@ func (s) TestSPIFFEIDFromState(t *testing.T) {
93
98
RawPath : string (make ([]byte , 2050 )),
94
99
},
95
100
},
96
- expectID : false ,
101
+ wantID : false ,
97
102
},
98
103
{
99
104
name : "large host" ,
@@ -105,7 +110,7 @@ func (s) TestSPIFFEIDFromState(t *testing.T) {
105
110
RawPath : "workload/wl1" ,
106
111
},
107
112
},
108
- expectID : false ,
113
+ wantID : false ,
109
114
},
110
115
{
111
116
name : "multiple URI SANs" ,
@@ -129,7 +134,7 @@ func (s) TestSPIFFEIDFromState(t *testing.T) {
129
134
RawPath : "workload/wl1" ,
130
135
},
131
136
},
132
- expectID : false ,
137
+ wantID : false ,
133
138
},
134
139
{
135
140
name : "multiple URI SANs without SPIFFE ID" ,
@@ -147,7 +152,7 @@ func (s) TestSPIFFEIDFromState(t *testing.T) {
147
152
RawPath : "workload/wl1" ,
148
153
},
149
154
},
150
- expectID : false ,
155
+ wantID : false ,
151
156
},
152
157
{
153
158
name : "multiple URI SANs with one SPIFFE ID" ,
@@ -165,15 +170,63 @@ func (s) TestSPIFFEIDFromState(t *testing.T) {
165
170
RawPath : "workload/wl1" ,
166
171
},
167
172
},
168
- expectID : false ,
173
+ wantID : false ,
169
174
},
170
175
}
171
176
for _ , tt := range tests {
172
177
t .Run (tt .name , func (t * testing.T ) {
173
178
state := tls.ConnectionState {PeerCertificates : []* x509.Certificate {{URIs : tt .urls }}}
174
179
id := SPIFFEIDFromState (state )
175
- if got , want := id != nil , tt .expectID ; got != want {
176
- t .Errorf ("want expectID = %v, but SPIFFE ID is %v" , want , id )
180
+ if got , want := id != nil , tt .wantID ; got != want {
181
+ t .Errorf ("want wantID = %v, but SPIFFE ID is %v" , want , id )
182
+ }
183
+ })
184
+ }
185
+ }
186
+
187
+ func (s ) TestSPIFFEIDFromCert (t * testing.T ) {
188
+ tests := []struct {
189
+ name string
190
+ dataPath string
191
+ // If we expect a SPIFFE ID to be returned.
192
+ wantID bool
193
+ }{
194
+ {
195
+ name : "good certificate with SPIFFE ID" ,
196
+ dataPath : "x509/spiffe_cert.pem" ,
197
+ wantID : true ,
198
+ },
199
+ {
200
+ name : "bad certificate with SPIFFE ID and another URI" ,
201
+ dataPath : "x509/multiple_uri_cert.pem" ,
202
+ wantID : false ,
203
+ },
204
+ {
205
+ name : "certificate without SPIFFE ID" ,
206
+ dataPath : "x509/client1_cert.pem" ,
207
+ wantID : false ,
208
+ },
209
+ }
210
+ for _ , tt := range tests {
211
+ t .Run (tt .name , func (t * testing.T ) {
212
+ data , err := ioutil .ReadFile (testdata .Path (tt .dataPath ))
213
+ if err != nil {
214
+ t .Fatalf ("ioutil.ReadFile(%s) failed: %v" , testdata .Path (tt .dataPath ), err )
215
+ }
216
+ block , _ := pem .Decode (data )
217
+ if block == nil {
218
+ t .Fatalf ("Failed to parse the certificate: byte block is nil" )
219
+ }
220
+ cert , err := x509 .ParseCertificate (block .Bytes )
221
+ if err != nil {
222
+ t .Fatalf ("x509.ParseCertificate(%b) failed: %v" , block .Bytes , err )
223
+ }
224
+ uri := SPIFFEIDFromCert (cert )
225
+ if (uri != nil ) != tt .wantID {
226
+ t .Fatalf ("wantID got and want mismatch, got %t, want %t" , uri != nil , tt .wantID )
227
+ }
228
+ if uri != nil && uri .String () != wantURI {
229
+ t .Fatalf ("SPIFFE ID not expected, got %s, want %s" , uri .String (), wantURI )
177
230
}
178
231
})
179
232
}
0 commit comments