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

r/aws_kms_key: Add acceptance test for policy with boolean condition #22093

Merged
merged 4 commits into from
Dec 7, 2021
Merged
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
73 changes: 73 additions & 0 deletions internal/service/kms/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,27 @@ func TestAccKMSKey_Policy_iamServiceLinkedRole(t *testing.T) {
})
}

func TestAccKMSKey_Policy_booleanCondition(t *testing.T) {
var key kms.KeyMetadata
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_kms_key.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, kms.EndpointsID),
Providers: acctest.Providers,
CheckDestroy: testAccCheckKeyDestroy,
Steps: []resource.TestStep{
{
Config: testAccKeyPolicyBooleanConditionConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckKeyExists(resourceName, &key),
),
},
},
})
}

func TestAccKMSKey_isEnabled(t *testing.T) {
var key1, key2, key3 kms.KeyMetadata
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
Expand Down Expand Up @@ -826,6 +847,58 @@ resource "aws_kms_key" "test" {
`, rName)
}

func testAccKeyPolicyBooleanConditionConfig(rName string) string {
return fmt.Sprintf(`
data "aws_caller_identity" "current" {}

data "aws_partition" "current" {}

resource "aws_kms_key" "test" {
description = %[1]q
deletion_window_in_days = 7

policy = jsonencode({
Id = %[1]q
Statement = [
{
Action = "kms:*"
Effect = "Allow"
Principal = {
AWS = "*"
}

Resource = "*"
Sid = "Enable IAM User Permissions"
},
{
Action = [
"kms:Encrypt",
"kms:Decrypt",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:DescribeKey",
]
Effect = "Allow"
Principal = {
AWS = "arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:root"
}

Resource = "*"
Sid = "Enable IAM User Permissions"

Condition = {
Bool = {
"kms:GrantIsForAWSResource" = true
}
}
},
]
Version = "2012-10-17"
})
}
`, rName)
}

func testAccKey_removedPolicy(rName string) string {
return fmt.Sprintf(`
resource "aws_kms_key" "test" {
Expand Down