Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(s3): allow aliases for kms key #33993

Merged
merged 2 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions internal/backend/remote-state/s3/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
const (
multiRegionKeyIdPattern = `mrk-[a-f0-9]{32}`
uuidRegexPattern = `[a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[ab89][a-f0-9]{3}-[a-f0-9]{12}`
aliasRegexPattern = `alias/[a-zA-Z0-9/_-]+`
)

func validateKMSKey(path cty.Path, s string) (diags tfdiags.Diagnostics) {
Expand All @@ -31,7 +32,7 @@ func validateKMSKey(path cty.Path, s string) (diags tfdiags.Diagnostics) {
}

func validateKMSKeyID(path cty.Path, s string) (diags tfdiags.Diagnostics) {
keyIdRegex := regexp.MustCompile(`^` + uuidRegexPattern + `|` + multiRegionKeyIdPattern + `$`)
keyIdRegex := regexp.MustCompile(`^` + uuidRegexPattern + `|` + multiRegionKeyIdPattern + `|` + aliasRegexPattern + `$`)
if !keyIdRegex.MatchString(s) {
diags = diags.Append(tfdiags.AttributeValue(
tfdiags.Error,
Expand Down Expand Up @@ -71,7 +72,7 @@ func validateKMSKeyARN(path cty.Path, s string) (diags tfdiags.Diagnostics) {
}

func isKeyARN(arn arn.ARN) bool {
return keyIdFromARNResource(arn.Resource) != ""
return keyIdFromARNResource(arn.Resource) != "" || aliasIdFromARNResource(arn.Resource) != ""
}

func keyIdFromARNResource(s string) string {
Expand All @@ -84,6 +85,16 @@ func keyIdFromARNResource(s string) string {
return matches[1]
}

func aliasIdFromARNResource(s string) string {
aliasIdResourceRegex := regexp.MustCompile(`^(` + aliasRegexPattern + `)$`)
matches := aliasIdResourceRegex.FindStringSubmatch(s)
if matches == nil || len(matches) != 2 {
return ""
}

return matches[1]
}

type stringValidator func(val string, path cty.Path, diags *tfdiags.Diagnostics)

func validateStringNotEmpty(val string, path cty.Path, diags *tfdiags.Diagnostics) {
Expand Down
16 changes: 0 additions & 16 deletions internal/backend/remote-state/s3/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,9 @@ func TestValidateKMSKey(t *testing.T) {
},
"kms key alias": {
in: "alias/arbitrary-key",
expected: tfdiags.Diagnostics{
tfdiags.AttributeValue(
tfdiags.Error,
"Invalid KMS Key ID",
`Value must be a valid KMS Key ID, got "alias/arbitrary-key"`,
path,
),
},
},
"kms key alias arn": {
in: "arn:aws:kms:us-west-2:111122223333:alias/arbitrary-key",
expected: tfdiags.Diagnostics{
tfdiags.AttributeValue(
tfdiags.Error,
"Invalid KMS Key ARN",
`Value must be a valid KMS Key ARN, got "arn:aws:kms:us-west-2:111122223333:alias/arbitrary-key"`,
path,
),
},
},
"invalid key": {
in: "$%wrongkey",
Expand Down
Loading