@@ -459,3 +459,86 @@ func TestMergeKMSEnvConfigAddrConformance(t *testing.T) {
459
459
})
460
460
}
461
461
}
462
+
463
+ func TestMergeKMSEnvConfig_ConfigPrecedence (t * testing.T ) {
464
+ tests := []struct {
465
+ name string
466
+ kmsType string
467
+ configValues map [string ]string
468
+ envVars map [string ]string
469
+ expectedConfig map [string ]string
470
+ }{
471
+ {
472
+ name : "AWS KMS - config file takes precedence over env vars" ,
473
+ kmsType : "awskms" ,
474
+ configValues : map [string ]string {
475
+ "region" : "us-east-1" , // This should NOT be overridden by env var
476
+ },
477
+ envVars : map [string ]string {
478
+ "AWS_REGION" : "us-west-2" , // This should be ignored
479
+ },
480
+ expectedConfig : map [string ]string {
481
+ "region" : "us-east-1" , // Config file value should be preserved
482
+ },
483
+ },
484
+ {
485
+ name : "AWS KMS - env var used when config not set" ,
486
+ kmsType : "awskms" ,
487
+ configValues : map [string ]string {
488
+ "kms_key_id" : "some-key-id" ,
489
+ // region is not set in config
490
+ },
491
+ envVars : map [string ]string {
492
+ "AWS_REGION" : "us-west-2" , // This should be used
493
+ },
494
+ expectedConfig : map [string ]string {
495
+ "kms_key_id" : "some-key-id" ,
496
+ "region" : "us-west-2" , // Env var should be used
497
+ },
498
+ },
499
+ {
500
+ name : "AWS KMS - empty config value allows env var override" ,
501
+ kmsType : "awskms" ,
502
+ configValues : map [string ]string {
503
+ "region" : "" , // Empty value should allow env var
504
+ },
505
+ envVars : map [string ]string {
506
+ "AWS_REGION" : "us-west-2" ,
507
+ },
508
+ expectedConfig : map [string ]string {
509
+ "region" : "us-west-2" , // Env var should be used
510
+ },
511
+ },
512
+ }
513
+
514
+ for _ , tt := range tests {
515
+ t .Run (tt .name , func (t * testing.T ) {
516
+ // Set up environment variables
517
+ for envVar , value := range tt .envVars {
518
+ t .Setenv (envVar , value )
519
+ }
520
+
521
+ // Create KMS config
522
+ kms := & KMS {
523
+ Type : tt .kmsType ,
524
+ Config : make (map [string ]string ),
525
+ }
526
+
527
+ // Set initial config values
528
+ for key , value := range tt .configValues {
529
+ kms .Config [key ] = value
530
+ }
531
+
532
+ // Merge environment config
533
+ err := mergeKMSEnvConfig (kms )
534
+ require .NoError (t , err )
535
+
536
+ // Verify final config matches expected
537
+ for key , expectedValue := range tt .expectedConfig {
538
+ actualValue , exists := kms .Config [key ]
539
+ require .True (t , exists , "Expected config key %s to exist" , key )
540
+ require .Equal (t , expectedValue , actualValue , "Config key %s has wrong value" , key )
541
+ }
542
+ })
543
+ }
544
+ }
0 commit comments