Skip to content

Commit b3ee47e

Browse files
authored
Merge pull request #2021 from shearn89/fix/kms-encryption-context-1972
Fix KMS encryption context not being passed when config is pre-loaded
2 parents fe9ab6e + e50165d commit b3ee47e

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
@@ -1901,7 +1901,8 @@ func main() {
19011901
needsCreationRule := isEncryptMode || isRotateMode || isSetMode || isEditMode
19021902
var config *config.Config
19031903
if needsCreationRule {
1904-
config, err = loadConfig(c, fileNameOverride, nil)
1904+
kmsEncryptionContext := kms.ParseKMSContext(c.String("encryption-context"))
1905+
config, err = loadConfig(c, fileNameOverride, kmsEncryptionContext)
19051906
if err != nil {
19061907
return toExitError(err)
19071908
}

config/config_test.go

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

0 commit comments

Comments
 (0)