1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
|
/*
*
* Copyright 2020 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package xds
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"net"
"os"
"strings"
"testing"
"time"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/tls/certprovider"
xdsinternal "google.golang.org/grpc/internal/credentials/xds"
"google.golang.org/grpc/testdata"
)
func makeClientTLSConfig(t *testing.T, mTLS bool) *tls.Config {
t.Helper()
pemData, err := os.ReadFile(testdata.Path("x509/server_ca_cert.pem"))
if err != nil {
t.Fatal(err)
}
roots := x509.NewCertPool()
roots.AppendCertsFromPEM(pemData)
var certs []tls.Certificate
if mTLS {
cert, err := tls.LoadX509KeyPair(testdata.Path("x509/client1_cert.pem"), testdata.Path("x509/client1_key.pem"))
if err != nil {
t.Fatal(err)
}
certs = append(certs, cert)
}
return &tls.Config{
Certificates: certs,
RootCAs: roots,
ServerName: "*.test.example.com",
// Setting this to true completely turns off the certificate validation
// on the client side. So, the client side handshake always seems to
// succeed. But if we want to turn this ON, we will need to generate
// certificates which work with localhost, or supply a custom
// verification function. So, the server credentials tests will rely
// solely on the success/failure of the server-side handshake.
InsecureSkipVerify: true,
}
}
// Helper function to create a real TLS server credentials which is used as
// fallback credentials from multiple tests.
func makeFallbackServerCreds(t *testing.T) credentials.TransportCredentials {
t.Helper()
creds, err := credentials.NewServerTLSFromFile(testdata.Path("x509/server1_cert.pem"), testdata.Path("x509/server1_key.pem"))
if err != nil {
t.Fatal(err)
}
return creds
}
type errorCreds struct {
credentials.TransportCredentials
}
// TestServerCredsWithoutFallback verifies that the call to
// NewServerCredentials() fails when no fallback is specified.
func (s) TestServerCredsWithoutFallback(t *testing.T) {
if _, err := NewServerCredentials(ServerOptions{}); err == nil {
t.Fatal("NewServerCredentials() succeeded without specifying fallback")
}
}
type wrapperConn struct {
net.Conn
xdsHI *xdsinternal.HandshakeInfo
deadline time.Time
handshakeInfoErr error
}
func (wc *wrapperConn) XDSHandshakeInfo() (*xdsinternal.HandshakeInfo, error) {
return wc.xdsHI, wc.handshakeInfoErr
}
func (wc *wrapperConn) GetDeadline() time.Time {
return wc.deadline
}
func newWrappedConn(conn net.Conn, xdsHI *xdsinternal.HandshakeInfo, deadline time.Time) *wrapperConn {
return &wrapperConn{Conn: conn, xdsHI: xdsHI, deadline: deadline}
}
// TestServerCredsInvalidHandshakeInfo verifies scenarios where the passed in
// HandshakeInfo is invalid because it does not contain the expected certificate
// providers.
func (s) TestServerCredsInvalidHandshakeInfo(t *testing.T) {
opts := ServerOptions{FallbackCreds: &errorCreds{}}
creds, err := NewServerCredentials(opts)
if err != nil {
t.Fatalf("NewServerCredentials(%v) failed: %v", opts, err)
}
info := xdsinternal.NewHandshakeInfo(&fakeProvider{}, nil)
conn := newWrappedConn(nil, info, time.Time{})
if _, _, err := creds.ServerHandshake(conn); err == nil {
t.Fatal("ServerHandshake succeeded without identity certificate provider in HandshakeInfo")
}
}
// TestServerCredsProviderFailure verifies the cases where an expected
// certificate provider is missing in the HandshakeInfo value in the context.
func (s) TestServerCredsProviderFailure(t *testing.T) {
opts := ServerOptions{FallbackCreds: &errorCreds{}}
creds, err := NewServerCredentials(opts)
if err != nil {
t.Fatalf("NewServerCredentials(%v) failed: %v", opts, err)
}
tests := []struct {
desc string
rootProvider certprovider.Provider
identityProvider certprovider.Provider
wantErr string
}{
{
desc: "erroring identity provider",
identityProvider: &fakeProvider{err: errors.New("identity provider error")},
wantErr: "identity provider error",
},
{
desc: "erroring root provider",
identityProvider: &fakeProvider{km: &certprovider.KeyMaterial{}},
rootProvider: &fakeProvider{err: errors.New("root provider error")},
wantErr: "root provider error",
},
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
info := xdsinternal.NewHandshakeInfo(test.rootProvider, test.identityProvider)
conn := newWrappedConn(nil, info, time.Time{})
if _, _, err := creds.ServerHandshake(conn); err == nil || !strings.Contains(err.Error(), test.wantErr) {
t.Fatalf("ServerHandshake() returned error: %q, wantErr: %q", err, test.wantErr)
}
})
}
}
// TestServerCredsHandshake_XDSHandshakeInfoError verifies the case where the
// call to XDSHandshakeInfo() from the ServerHandshake() method returns an
// error, and the test verifies that the ServerHandshake() fails with the
// expected error.
func (s) TestServerCredsHandshake_XDSHandshakeInfoError(t *testing.T) {
opts := ServerOptions{FallbackCreds: &errorCreds{}}
creds, err := NewServerCredentials(opts)
if err != nil {
t.Fatalf("NewServerCredentials(%v) failed: %v", opts, err)
}
// Create a test server which uses the xDS server credentials created above
// to perform TLS handshake on incoming connections.
ts := newTestServerWithHandshakeFunc(func(rawConn net.Conn) handshakeResult {
// Create a wrapped conn which returns a nil HandshakeInfo and a non-nil error.
conn := newWrappedConn(rawConn, nil, time.Now().Add(defaultTestTimeout))
hiErr := errors.New("xdsHandshakeInfo error")
conn.handshakeInfoErr = hiErr
// Invoke the ServerHandshake() method on the xDS credentials and verify
// that the error returned by the XDSHandshakeInfo() method on the
// wrapped conn is returned here.
_, _, err := creds.ServerHandshake(conn)
if !errors.Is(err, hiErr) {
return handshakeResult{err: fmt.Errorf("ServerHandshake() returned err: %v, wantErr: %v", err, hiErr)}
}
return handshakeResult{}
})
defer ts.stop()
// Dial the test server, but don't trigger the TLS handshake. This will
// cause ServerHandshake() to fail.
rawConn, err := net.Dial("tcp", ts.address)
if err != nil {
t.Fatalf("net.Dial(%s) failed: %v", ts.address, err)
}
defer rawConn.Close()
// Read handshake result from the testServer which will return an error if
// the handshake succeeded.
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
val, err := ts.hsResult.Receive(ctx)
if err != nil {
t.Fatalf("testServer failed to return handshake result: %v", err)
}
hsr := val.(handshakeResult)
if hsr.err != nil {
t.Fatalf("testServer handshake failure: %v", hsr.err)
}
}
// TestServerCredsHandshakeTimeout verifies the case where the client does not
// send required handshake data before the deadline set on the net.Conn passed
// to ServerHandshake().
func (s) TestServerCredsHandshakeTimeout(t *testing.T) {
opts := ServerOptions{FallbackCreds: &errorCreds{}}
creds, err := NewServerCredentials(opts)
if err != nil {
t.Fatalf("NewServerCredentials(%v) failed: %v", opts, err)
}
// Create a test server which uses the xDS server credentials created above
// to perform TLS handshake on incoming connections.
ts := newTestServerWithHandshakeFunc(func(rawConn net.Conn) handshakeResult {
hi := xdsinternal.NewHandshakeInfo(makeRootProvider(t, "x509/client_ca_cert.pem"), makeIdentityProvider(t, "x509/server2_cert.pem", "x509/server2_key.pem"))
hi.SetRequireClientCert(true)
// Create a wrapped conn which can return the HandshakeInfo created
// above with a very small deadline.
d := time.Now().Add(defaultTestShortTimeout)
rawConn.SetDeadline(d)
conn := newWrappedConn(rawConn, hi, d)
// ServerHandshake() on the xDS credentials is expected to fail.
if _, _, err := creds.ServerHandshake(conn); err == nil {
return handshakeResult{err: errors.New("ServerHandshake() succeeded when expected to timeout")}
}
return handshakeResult{}
})
defer ts.stop()
// Dial the test server, but don't trigger the TLS handshake. This will
// cause ServerHandshake() to fail.
rawConn, err := net.Dial("tcp", ts.address)
if err != nil {
t.Fatalf("net.Dial(%s) failed: %v", ts.address, err)
}
defer rawConn.Close()
// Read handshake result from the testServer and expect a failure result.
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
val, err := ts.hsResult.Receive(ctx)
if err != nil {
t.Fatalf("testServer failed to return handshake result: %v", err)
}
hsr := val.(handshakeResult)
if hsr.err != nil {
t.Fatalf("testServer handshake failure: %v", hsr.err)
}
}
// TestServerCredsHandshakeFailure verifies the case where the server-side
// credentials uses a root certificate which does not match the certificate
// presented by the client, and hence the handshake must fail.
func (s) TestServerCredsHandshakeFailure(t *testing.T) {
opts := ServerOptions{FallbackCreds: &errorCreds{}}
creds, err := NewServerCredentials(opts)
if err != nil {
t.Fatalf("NewServerCredentials(%v) failed: %v", opts, err)
}
// Create a test server which uses the xDS server credentials created above
// to perform TLS handshake on incoming connections.
ts := newTestServerWithHandshakeFunc(func(rawConn net.Conn) handshakeResult {
// Create a HandshakeInfo which has a root provider which does not match
// the certificate sent by the client.
hi := xdsinternal.NewHandshakeInfo(makeRootProvider(t, "x509/server_ca_cert.pem"), makeIdentityProvider(t, "x509/client2_cert.pem", "x509/client2_key.pem"))
hi.SetRequireClientCert(true)
// Create a wrapped conn which can return the HandshakeInfo and
// configured deadline to the xDS credentials' ServerHandshake()
// method.
conn := newWrappedConn(rawConn, hi, time.Now().Add(defaultTestTimeout))
// ServerHandshake() on the xDS credentials is expected to fail.
if _, _, err := creds.ServerHandshake(conn); err == nil {
return handshakeResult{err: errors.New("ServerHandshake() succeeded when expected to fail")}
}
return handshakeResult{}
})
defer ts.stop()
// Dial the test server, and trigger the TLS handshake.
rawConn, err := net.Dial("tcp", ts.address)
if err != nil {
t.Fatalf("net.Dial(%s) failed: %v", ts.address, err)
}
defer rawConn.Close()
tlsConn := tls.Client(rawConn, makeClientTLSConfig(t, true))
tlsConn.SetDeadline(time.Now().Add(defaultTestTimeout))
if err := tlsConn.Handshake(); err != nil {
t.Fatal(err)
}
// Read handshake result from the testServer which will return an error if
// the handshake succeeded.
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
val, err := ts.hsResult.Receive(ctx)
if err != nil {
t.Fatalf("testServer failed to return handshake result: %v", err)
}
hsr := val.(handshakeResult)
if hsr.err != nil {
t.Fatalf("testServer handshake failure: %v", hsr.err)
}
}
// TestServerCredsHandshakeSuccess verifies success handshake cases.
func (s) TestServerCredsHandshakeSuccess(t *testing.T) {
tests := []struct {
desc string
fallbackCreds credentials.TransportCredentials
rootProvider certprovider.Provider
identityProvider certprovider.Provider
requireClientCert bool
}{
{
desc: "fallback",
fallbackCreds: makeFallbackServerCreds(t),
},
{
desc: "TLS",
fallbackCreds: &errorCreds{},
identityProvider: makeIdentityProvider(t, "x509/server2_cert.pem", "x509/server2_key.pem"),
},
{
desc: "mTLS",
fallbackCreds: &errorCreds{},
identityProvider: makeIdentityProvider(t, "x509/server2_cert.pem", "x509/server2_key.pem"),
rootProvider: makeRootProvider(t, "x509/client_ca_cert.pem"),
requireClientCert: true,
},
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
// Create an xDS server credentials.
opts := ServerOptions{FallbackCreds: test.fallbackCreds}
creds, err := NewServerCredentials(opts)
if err != nil {
t.Fatalf("NewServerCredentials(%v) failed: %v", opts, err)
}
// Create a test server which uses the xDS server credentials
// created above to perform TLS handshake on incoming connections.
ts := newTestServerWithHandshakeFunc(func(rawConn net.Conn) handshakeResult {
// Create a HandshakeInfo with information from the test table.
hi := xdsinternal.NewHandshakeInfo(test.rootProvider, test.identityProvider)
hi.SetRequireClientCert(test.requireClientCert)
// Create a wrapped conn which can return the HandshakeInfo and
// configured deadline to the xDS credentials' ServerHandshake()
// method.
conn := newWrappedConn(rawConn, hi, time.Now().Add(defaultTestTimeout))
// Invoke the ServerHandshake() method on the xDS credentials
// and make some sanity checks before pushing the result for
// inspection by the main test body.
_, ai, err := creds.ServerHandshake(conn)
if err != nil {
return handshakeResult{err: fmt.Errorf("ServerHandshake() failed: %v", err)}
}
if ai.AuthType() != "tls" {
return handshakeResult{err: fmt.Errorf("ServerHandshake returned authType %q, want %q", ai.AuthType(), "tls")}
}
info, ok := ai.(credentials.TLSInfo)
if !ok {
return handshakeResult{err: fmt.Errorf("ServerHandshake returned authInfo of type %T, want %T", ai, credentials.TLSInfo{})}
}
return handshakeResult{connState: info.State}
})
defer ts.stop()
// Dial the test server, and trigger the TLS handshake.
rawConn, err := net.Dial("tcp", ts.address)
if err != nil {
t.Fatalf("net.Dial(%s) failed: %v", ts.address, err)
}
defer rawConn.Close()
tlsConn := tls.Client(rawConn, makeClientTLSConfig(t, test.requireClientCert))
tlsConn.SetDeadline(time.Now().Add(defaultTestTimeout))
if err := tlsConn.Handshake(); err != nil {
t.Fatal(err)
}
// Read the handshake result from the testServer which contains the
// TLS connection state on the server-side and compare it with the
// one received on the client-side.
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
val, err := ts.hsResult.Receive(ctx)
if err != nil {
t.Fatalf("testServer failed to return handshake result: %v", err)
}
hsr := val.(handshakeResult)
if hsr.err != nil {
t.Fatalf("testServer handshake failure: %v", hsr.err)
}
// AuthInfo contains a variety of information. We only verify a
// subset here. This is the same subset which is verified in TLS
// credentials tests.
if err := compareConnState(tlsConn.ConnectionState(), hsr.connState); err != nil {
t.Fatal(err)
}
})
}
}
func (s) TestServerCredsProviderSwitch(t *testing.T) {
opts := ServerOptions{FallbackCreds: &errorCreds{}}
creds, err := NewServerCredentials(opts)
if err != nil {
t.Fatalf("NewServerCredentials(%v) failed: %v", opts, err)
}
// The first time the handshake function is invoked, it returns a
// HandshakeInfo which is expected to fail. Further invocations return a
// HandshakeInfo which is expected to succeed.
cnt := 0
// Create a test server which uses the xDS server credentials created above
// to perform TLS handshake on incoming connections.
ts := newTestServerWithHandshakeFunc(func(rawConn net.Conn) handshakeResult {
cnt++
var hi *xdsinternal.HandshakeInfo
if cnt == 1 {
// Create a HandshakeInfo which has a root provider which does not match
// the certificate sent by the client.
hi = xdsinternal.NewHandshakeInfo(makeRootProvider(t, "x509/server_ca_cert.pem"), makeIdentityProvider(t, "x509/client2_cert.pem", "x509/client2_key.pem"))
hi.SetRequireClientCert(true)
// Create a wrapped conn which can return the HandshakeInfo and
// configured deadline to the xDS credentials' ServerHandshake()
// method.
conn := newWrappedConn(rawConn, hi, time.Now().Add(defaultTestTimeout))
// ServerHandshake() on the xDS credentials is expected to fail.
if _, _, err := creds.ServerHandshake(conn); err == nil {
return handshakeResult{err: errors.New("ServerHandshake() succeeded when expected to fail")}
}
return handshakeResult{}
}
hi = xdsinternal.NewHandshakeInfo(makeRootProvider(t, "x509/client_ca_cert.pem"), makeIdentityProvider(t, "x509/server1_cert.pem", "x509/server1_key.pem"))
hi.SetRequireClientCert(true)
// Create a wrapped conn which can return the HandshakeInfo and
// configured deadline to the xDS credentials' ServerHandshake()
// method.
conn := newWrappedConn(rawConn, hi, time.Now().Add(defaultTestTimeout))
// Invoke the ServerHandshake() method on the xDS credentials
// and make some sanity checks before pushing the result for
// inspection by the main test body.
_, ai, err := creds.ServerHandshake(conn)
if err != nil {
return handshakeResult{err: fmt.Errorf("ServerHandshake() failed: %v", err)}
}
if ai.AuthType() != "tls" {
return handshakeResult{err: fmt.Errorf("ServerHandshake returned authType %q, want %q", ai.AuthType(), "tls")}
}
info, ok := ai.(credentials.TLSInfo)
if !ok {
return handshakeResult{err: fmt.Errorf("ServerHandshake returned authInfo of type %T, want %T", ai, credentials.TLSInfo{})}
}
return handshakeResult{connState: info.State}
})
defer ts.stop()
for i := 0; i < 5; i++ {
// Dial the test server, and trigger the TLS handshake.
rawConn, err := net.Dial("tcp", ts.address)
if err != nil {
t.Fatalf("net.Dial(%s) failed: %v", ts.address, err)
}
defer rawConn.Close()
tlsConn := tls.Client(rawConn, makeClientTLSConfig(t, true))
tlsConn.SetDeadline(time.Now().Add(defaultTestTimeout))
if err := tlsConn.Handshake(); err != nil {
t.Fatal(err)
}
// Read the handshake result from the testServer which contains the
// TLS connection state on the server-side and compare it with the
// one received on the client-side.
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
val, err := ts.hsResult.Receive(ctx)
if err != nil {
t.Fatalf("testServer failed to return handshake result: %v", err)
}
hsr := val.(handshakeResult)
if hsr.err != nil {
t.Fatalf("testServer handshake failure: %v", hsr.err)
}
if i == 0 {
// We expect the first handshake to fail. So, we skip checks which
// compare connection state.
continue
}
// AuthInfo contains a variety of information. We only verify a
// subset here. This is the same subset which is verified in TLS
// credentials tests.
if err := compareConnState(tlsConn.ConnectionState(), hsr.connState); err != nil {
t.Fatal(err)
}
}
}
// TestServerClone verifies the Clone() method on client credentials.
func (s) TestServerClone(t *testing.T) {
opts := ServerOptions{FallbackCreds: makeFallbackServerCreds(t)}
orig, err := NewServerCredentials(opts)
if err != nil {
t.Fatalf("NewServerCredentials(%v) failed: %v", opts, err)
}
// The credsImpl does not have any exported fields, and it does not make
// sense to use any cmp options to look deep into. So, all we make sure here
// is that the cloned object points to a different location in memory.
if clone := orig.Clone(); clone == orig {
t.Fatal("return value from Clone() doesn't point to new credentials instance")
}
}
|