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