@@ -14,6 +14,7 @@ import (
14
14
"github.com/hashicorp/vault/command/agentproxyshared"
15
15
"github.com/hashicorp/vault/internalshared/configutil"
16
16
"github.com/hashicorp/vault/sdk/helper/pointerutil"
17
+ "github.com/stretchr/testify/require"
17
18
"golang.org/x/exp/slices"
18
19
)
19
20
@@ -230,6 +231,9 @@ func TestLoadConfigDir_AgentCache(t *testing.T) {
230
231
t .Fatal (err )
231
232
}
232
233
config2 , err := LoadConfigFile ("./test-fixtures/config-dir-cache/config-cache2.hcl" )
234
+ if err != nil {
235
+ t .Fatal (err )
236
+ }
233
237
234
238
mergedConfig := config .Merge (config2 )
235
239
@@ -441,77 +445,117 @@ func TestLoadConfigFile_AgentCache_NoListeners(t *testing.T) {
441
445
}
442
446
}
443
447
444
- func TestLoadConfigFile (t * testing.T ) {
445
- if err := os .Setenv ("TEST_AAD_ENV" , "aad" ); err != nil {
446
- t .Fatal (err )
447
- }
448
- defer func () {
449
- if err := os .Unsetenv ("TEST_AAD_ENV" ); err != nil {
450
- t .Fatal (err )
451
- }
452
- }()
453
-
454
- config , err := LoadConfigFile ("./test-fixtures/config.hcl" )
455
- if err != nil {
456
- t .Fatalf ("err: %s" , err )
457
- }
448
+ // Test_LoadConfigFile_AutoAuth_AddrConformance verifies basic config file
449
+ // loading in addition to RFC-5942 §4 normalization of auto-auth methods.
450
+ // See: https://rfc-editor.org/rfc/rfc5952.html
451
+ func Test_LoadConfigFile_AutoAuth_AddrConformance (t * testing.T ) {
452
+ t .Setenv ("TEST_AAD_ENV" , "aad" )
453
+
454
+ for name , method := range map [string ]* Method {
455
+ "aws" : {
456
+ Type : "aws" ,
457
+ MountPath : "auth/aws" ,
458
+ Namespace : "aws-namespace/" ,
459
+ Config : map [string ]any {
460
+ "role" : "foobar" ,
461
+ },
462
+ },
463
+ "azure" : {
464
+ Type : "azure" ,
465
+ MountPath : "auth/azure" ,
466
+ Namespace : "azure-namespace/" ,
467
+ Config : map [string ]any {
468
+ "authenticate_from_environment" : true ,
469
+ "role" : "dev-role" ,
470
+ "resource" : "https://[2001:0:0:1::1]" ,
471
+ },
472
+ },
473
+ "gcp" : {
474
+ Type : "gcp" ,
475
+ MountPath : "auth/gcp" ,
476
+ Namespace : "gcp-namespace/" ,
477
+ Config : map [string ]any {
478
+ "role" : "dev-role" ,
479
+ "service_account" : "https://[2001:db8:ac3:fe4::1]" ,
480
+ },
481
+ },
482
+ } {
483
+ t .Run (name , func (t * testing.T ) {
484
+ config , err := LoadConfigFile ("./test-fixtures/config-auto-auth-" + name + ".hcl" )
485
+ require .NoError (t , err )
458
486
459
- expected := & Config {
460
- SharedConfig : & configutil.SharedConfig {
461
- PidFile : "./pidfile" ,
462
- LogFile : "/var/log/vault/vault-agent.log" ,
463
- },
464
- AutoAuth : & AutoAuth {
465
- Method : & Method {
466
- Type : "aws" ,
467
- MountPath : "auth/aws" ,
468
- Namespace : "my-namespace/" ,
469
- Config : map [string ]interface {}{
470
- "role" : "foobar" ,
487
+ expected := & Config {
488
+ SharedConfig : & configutil.SharedConfig {
489
+ PidFile : "./pidfile" ,
490
+ Listeners : []* configutil.Listener {
491
+ {
492
+ Type : "unix" ,
493
+ Address : "/path/to/socket" ,
494
+ TLSDisable : true ,
495
+ AgentAPI : & configutil.AgentAPI {
496
+ EnableQuit : true ,
497
+ },
498
+ },
499
+ {
500
+ Type : "tcp" ,
501
+ Address : "2001:db8::1:8200" , // Normalized
502
+ TLSDisable : true ,
503
+ },
504
+ {
505
+ Type : "tcp" ,
506
+ Address : "[2001:0:0:1::1]:3000" , // Normalized
507
+ Role : "metrics_only" ,
508
+ TLSDisable : true ,
509
+ },
510
+ {
511
+ Type : "tcp" ,
512
+ Role : "default" ,
513
+ Address : "2001:db8:0:1:1:1:1:1:8400" , // Normalized
514
+ TLSKeyFile : "/path/to/cakey.pem" ,
515
+ TLSCertFile : "/path/to/cacert.pem" ,
516
+ },
517
+ },
518
+ LogFile : "/var/log/vault/vault-agent.log" ,
471
519
},
472
- MaxBackoff : 0 ,
473
- },
474
- Sinks : []* Sink {
475
- {
476
- Type : "file" ,
477
- DHType : "curve25519" ,
478
- DHPath : "/tmp/file-foo-dhpath" ,
479
- AAD : "foobar" ,
480
- Config : map [string ]interface {}{
481
- "path" : "/tmp/file-foo" ,
520
+ Vault : & Vault {
521
+ Address : "https://[2001:db8::1]:8200" , // Address is normalized
522
+ Retry : & Retry {
523
+ NumRetries : 12 , // Default number of retries when a vault stanza is set
482
524
},
483
525
},
484
- {
485
- Type : "file" ,
486
- WrapTTL : 5 * time .Minute ,
487
- DHType : "curve25519" ,
488
- DHPath : "/tmp/file-foo-dhpath2" ,
489
- AAD : "aad" ,
490
- DeriveKey : true ,
491
- Config : map [string ]interface {}{
492
- "path" : "/tmp/file-bar" ,
526
+ AutoAuth : & AutoAuth {
527
+ Method : method , // Method properties are normalized correctly
528
+ Sinks : []* Sink {
529
+ {
530
+ Type : "file" ,
531
+ DHType : "curve25519" ,
532
+ DHPath : "/tmp/file-foo-dhpath" ,
533
+ AAD : "foobar" ,
534
+ Config : map [string ]interface {}{
535
+ "path" : "/tmp/file-foo" ,
536
+ },
537
+ },
538
+ {
539
+ Type : "file" ,
540
+ WrapTTL : 5 * time .Minute ,
541
+ DHType : "curve25519" ,
542
+ DHPath : "/tmp/file-foo-dhpath2" ,
543
+ AAD : "aad" ,
544
+ DeriveKey : true ,
545
+ Config : map [string ]interface {}{
546
+ "path" : "/tmp/file-bar" ,
547
+ },
548
+ },
493
549
},
494
550
},
495
- },
496
- },
497
- TemplateConfig : & TemplateConfig {
498
- MaxConnectionsPerHost : DefaultTemplateConfigMaxConnsPerHost ,
499
- },
500
- }
501
-
502
- config .Prune ()
503
- if diff := deep .Equal (config , expected ); diff != nil {
504
- t .Fatal (diff )
505
- }
506
-
507
- config , err = LoadConfigFile ("./test-fixtures/config-embedded-type.hcl" )
508
- if err != nil {
509
- t .Fatalf ("err: %s" , err )
510
- }
551
+ TemplateConfig : & TemplateConfig {
552
+ MaxConnectionsPerHost : DefaultTemplateConfigMaxConnsPerHost ,
553
+ },
554
+ }
511
555
512
- config .Prune ()
513
- if diff := deep . Equal ( config , expected ); diff != nil {
514
- t . Fatal ( diff )
556
+ config .Prune ()
557
+ require . EqualValues ( t , expected , config )
558
+ } )
515
559
}
516
560
}
517
561
0 commit comments