@@ -7,11 +7,10 @@ package grpc
7
7
8
8
import (
9
9
"context"
10
+ "crypto/tls"
10
11
"fmt"
11
12
"testing"
12
13
13
- "google.golang.org/grpc/credentials"
14
-
15
14
"github.com/cenkalti/backoff/v4"
16
15
"github.com/nginx/agent/v3/test/helpers"
17
16
"github.com/nginx/agent/v3/test/protos"
@@ -356,28 +355,131 @@ func Test_ValidateGrpcError(t *testing.T) {
356
355
}
357
356
358
357
func Test_getTransportCredentials (t * testing.T ) {
359
- tests := [ ]struct {
360
- want credentials. TransportCredentials
361
- conf * config. Config
362
- wantErr assert. ErrorAssertionFunc
363
- name string
358
+ tests := map [ string ]struct {
359
+ conf * config. Config
360
+ wantErr bool
361
+ wantSecurityProfile string
362
+ wantServerName string
364
363
}{
365
- {
366
- name : "No TLS config returns default credentials" ,
364
+ "Test 1: No TLS config returns default credentials" : {
367
365
conf : & config.Config {
368
366
Command : & config.Command {},
369
367
},
370
- want : defaultCredentials ,
371
- wantErr : assert .NoError ,
368
+ wantErr : false ,
369
+ wantSecurityProfile : "insecure" ,
370
+ },
371
+ "Test 2: With tls config returns secure credentials" : {
372
+ conf : & config.Config {
373
+ Command : & config.Command {
374
+ TLS : & config.TLSConfig {
375
+ ServerName : "foobar" ,
376
+ SkipVerify : true ,
377
+ },
378
+ },
379
+ },
380
+ wantErr : false ,
381
+ wantSecurityProfile : "tls" ,
382
+ },
383
+ "Test 3: With invalid tls config should error" : {
384
+ conf : types .AgentConfig (), // references non-existant certs
385
+ wantErr : true ,
372
386
},
373
387
}
374
- for _ , tt := range tests {
375
- t .Run (tt . name , func (t * testing.T ) {
388
+ for name , tt := range tests {
389
+ t .Run (name , func (t * testing.T ) {
376
390
got , err := getTransportCredentials (tt .conf )
377
- if ! tt .wantErr (t , err , fmt .Sprintf ("getTransportCredentials(%v)" , tt .conf )) {
391
+ if tt .wantErr {
392
+ require .Error (t , err , "getTransportCredentials(%v)" , tt .conf )
393
+ return
394
+ }
395
+ require .NoError (t , err , "getTransportCredentials(%v)" , tt .conf )
396
+ require .Equal (t , tt .wantSecurityProfile , got .Info ().SecurityProtocol , "incorrect SecurityProtocol" )
397
+ })
398
+ }
399
+ }
400
+
401
+ func Test_getTLSConfig (t * testing.T ) {
402
+ tmpDir := t .TempDir ()
403
+ // not mTLS scripts
404
+ key , cert := helpers .GenerateSelfSignedCert (t )
405
+ _ , ca := helpers .GenerateSelfSignedCert (t )
406
+
407
+ keyContents := helpers.Cert {Name : keyFileName , Type : privateKeyType , Contents : key }
408
+ certContents := helpers.Cert {Name : certFileName , Type : certificateType , Contents : cert }
409
+ caContents := helpers.Cert {Name : caFileName , Type : certificateType , Contents : ca }
410
+
411
+ keyPath := helpers .WriteCertFiles (t , tmpDir , keyContents )
412
+ certPath := helpers .WriteCertFiles (t , tmpDir , certContents )
413
+ caPath := helpers .WriteCertFiles (t , tmpDir , caContents )
414
+
415
+ tests := map [string ]struct {
416
+ conf * config.TLSConfig
417
+ wantErr bool
418
+ verify func (* testing.T , * tls.Config )
419
+ }{
420
+ "Test 1: all config should be translated" : {
421
+ conf : & config.TLSConfig {
422
+ Cert : certPath ,
423
+ Key : keyPath ,
424
+ Ca : caPath ,
425
+ ServerName : "foobar" ,
426
+ SkipVerify : true ,
427
+ },
428
+ wantErr : false ,
429
+ verify : func (t * testing.T , c * tls.Config ) {
430
+ require .NotEmpty (t , c .Certificates )
431
+ require .Equal (t , "foobar" , c .ServerName , "wrong servername" )
432
+ require .True (t , c .InsecureSkipVerify , "InsecureSkipVerify not set" )
433
+ },
434
+ },
435
+ "Test 2: CA only config should use CA" : {
436
+ conf : & config.TLSConfig {
437
+ Ca : caPath ,
438
+ },
439
+ wantErr : false ,
440
+ verify : func (t * testing.T , c * tls.Config ) {
441
+ require .NotNil (t , c .RootCAs , "RootCAs should be initialized" )
442
+ require .Len (t , c .RootCAs .Subjects (), 1 , "RootCAs pool should contain at least one subject" )
443
+ require .False (t , c .InsecureSkipVerify , "InsecureSkipVerify should not be set" )
444
+ },
445
+ },
446
+ "Test 3: incorrect CA should not error" : { // REALLY ?!
447
+ conf : & config.TLSConfig {
448
+ Ca : "customca.pem" ,
449
+ },
450
+ wantErr : false ,
451
+ verify : func (t * testing.T , c * tls.Config ) {
452
+ require .Nil (t , c .RootCAs , "RootCAs should be nil to use system" )
453
+ },
454
+ },
455
+ "Test 4: incorrect key path should error" : {
456
+ conf : & config.TLSConfig {
457
+ Ca : caPath ,
458
+ Cert : certPath ,
459
+ Key : "badkey" ,
460
+ },
461
+ wantErr : true ,
462
+ },
463
+ "Test 5: incorrect cert path should error" : {
464
+ conf : & config.TLSConfig {
465
+ Ca : caPath ,
466
+ Cert : "badcert" ,
467
+ Key : keyPath ,
468
+ },
469
+ wantErr : true ,
470
+ },
471
+ }
472
+ for name , tt := range tests {
473
+ t .Run (name , func (t * testing.T ) {
474
+ got , err := getTLSConfigForCredentials (tt .conf )
475
+ if tt .wantErr {
476
+ require .Error (t , err , "getTLSConfigForCredentials(%v)" , tt .conf )
378
477
return
379
478
}
380
- assert .Equalf (t , tt .want , got , "getTransportCredentials(%v)" , tt .conf )
479
+ require .NoError (t , err , "getTLSConfigForCredentials(%v)" , tt .conf )
480
+ if tt .verify != nil {
481
+ tt .verify (t , got )
482
+ }
381
483
})
382
484
}
383
485
}
0 commit comments