Skip to content

Commit ab5a015

Browse files
authored
Merge branch 'main' into feat/huaweicloud-kms
2 parents bff2b9a + b3ee47e commit ab5a015

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

cmd/sops/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1943,7 +1943,8 @@ func main() {
19431943
needsCreationRule := isEncryptMode || isRotateMode || isSetMode || isEditMode
19441944
var config *config.Config
19451945
if needsCreationRule {
1946-
config, err = loadConfig(c, fileNameOverride, nil)
1946+
kmsEncryptionContext := kms.ParseKMSContext(c.String("encryption-context"))
1947+
config, err = loadConfig(c, fileNameOverride, kmsEncryptionContext)
19471948
if err != nil {
19481949
return toExitError(err)
19491950
}

config/config_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,3 +891,31 @@ destination_rules:
891891
assert.NotNil(t, conf.Destination)
892892
assert.Contains(t, conf.Destination.Path("secrets.yaml"), "https://vault.example.com/v1/secret/data/secret/sops/secrets.yaml")
893893
}
894+
895+
// TestKeyGroupsForFileWithExternalEncryptionContext tests that when kmsEncryptionContext
896+
// is passed to parseCreationRuleForFile, the resulting KMS keys have the encryption context set.
897+
// This is a regression test for https://github.com/getsops/sops/issues/1972
898+
func TestKeyGroupsForFileWithExternalEncryptionContext(t *testing.T) {
899+
// Config with flat KMS format (not key_groups) - this is where external context applies
900+
var sampleConfigWithFlatKMS = []byte(`
901+
creation_rules:
902+
- path_regex: ""
903+
kms: "arn:aws:kms:us-west-2:123456789012:key/12345678-1234-1234-1234-123456789012"
904+
`)
905+
906+
// External encryption context passed via --encryption-context flag
907+
appName := "myapp"
908+
kmsEncryptionContext := map[string]*string{
909+
"AppName": &appName,
910+
}
911+
912+
conf, err := parseCreationRuleForFile(parseConfigFile(sampleConfigWithFlatKMS, t), "/conf/path", "secrets.yaml", kmsEncryptionContext)
913+
assert.Nil(t, err)
914+
assert.NotNil(t, conf)
915+
assert.Equal(t, 1, len(conf.KeyGroups))
916+
assert.Equal(t, 1, len(conf.KeyGroups[0]))
917+
918+
// The KMS key should have the encryption context applied
919+
// Format: ARN|context where context is "AppName:myapp"
920+
assert.Equal(t, "arn:aws:kms:us-west-2:123456789012:key/12345678-1234-1234-1234-123456789012|AppName:myapp", conf.KeyGroups[0][0].ToString())
921+
}

0 commit comments

Comments
 (0)