diff --git a/.changelog/21969.txt b/.changelog/21969.txt new file mode 100644 index 00000000000..595760cf968 --- /dev/null +++ b/.changelog/21969.txt @@ -0,0 +1,3 @@ +```release-note:bug +resource/aws_kms_key: Fix order-related diffs in `policy` +``` \ No newline at end of file diff --git a/internal/service/kms/key.go b/internal/service/kms/key.go index 510f541fe88..232b36966d4 100644 --- a/internal/service/kms/key.go +++ b/internal/service/kms/key.go @@ -199,7 +199,14 @@ func resourceKeyRead(d *schema.ResourceData, meta interface{}) error { d.Set("key_id", key.metadata.KeyId) d.Set("key_usage", key.metadata.KeyUsage) d.Set("multi_region", key.metadata.MultiRegion) - d.Set("policy", key.policy) + + policyToSet, err := verify.SecondJSONUnlessEquivalent(d.Get("policy").(string), key.policy) + + if err != nil { + return fmt.Errorf("while setting policy (%s), encountered: %w", key.policy, err) + } + + d.Set("policy", policyToSet) tags := key.tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig) diff --git a/internal/service/kms/key_test.go b/internal/service/kms/key_test.go index 3522deb2bf4..a6aa15814fb 100644 --- a/internal/service/kms/key_test.go +++ b/internal/service/kms/key_test.go @@ -121,11 +121,11 @@ func TestAccKMSKey_asymmetricKey(t *testing.T) { }) } -func TestAccKMSKey_policy(t *testing.T) { +func TestAccKMSKey_Policy_basic(t *testing.T) { var key kms.KeyMetadata rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resourceName := "aws_kms_key.test" - expectedPolicyText := `{"Version":"2012-10-17","Id":"kms-tf-1","Statement":[{"Sid":"Enable IAM User Permissions","Effect":"Allow","Principal":{"AWS":"*"},"Action":"kms:*","Resource":"*"}]}` + expectedPolicyText := fmt.Sprintf(`{"Version":"2012-10-17","Id":%[1]q,"Statement":[{"Sid":"Enable IAM User Permissions","Effect":"Allow","Principal":{"AWS":"*"},"Action":"kms:*","Resource":"*"}]}`, rName) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(t) }, @@ -156,7 +156,7 @@ func TestAccKMSKey_policy(t *testing.T) { }) } -func TestAccKMSKey_policyBypass(t *testing.T) { +func TestAccKMSKey_Policy_bypass(t *testing.T) { var key kms.KeyMetadata rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resourceName := "aws_kms_key.test" @@ -188,7 +188,7 @@ func TestAccKMSKey_policyBypass(t *testing.T) { }) } -func TestAccKMSKey_policyBypassUpdate(t *testing.T) { +func TestAccKMSKey_Policy_bypassUpdate(t *testing.T) { var before, after kms.KeyMetadata rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resourceName := "aws_kms_key.test" @@ -244,6 +244,49 @@ func TestAccKMSKey_Policy_iamRole(t *testing.T) { }) } +// Reference: https://github.com/hashicorp/terraform-provider-aws/issues/11801 +func TestAccKMSKey_Policy_iamRoleOrder(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: testAccKeyPolicyIAMMultiRoleConfig(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckKeyExists(resourceName, &key), + ), + }, + { + Config: testAccKeyPolicyIAMMultiRoleConfig(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckKeyExists(resourceName, &key), + ), + PlanOnly: true, + }, + { + Config: testAccKeyPolicyIAMMultiRoleConfig(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckKeyExists(resourceName, &key), + ), + PlanOnly: true, + }, + { + Config: testAccKeyPolicyIAMMultiRoleConfig(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckKeyExists(resourceName, &key), + ), + PlanOnly: true, + }, + }, + }) +} + // Reference: https://github.com/hashicorp/terraform-provider-aws/issues/7646 func TestAccKMSKey_Policy_iamServiceLinkedRole(t *testing.T) { var key kms.KeyMetadata @@ -495,23 +538,19 @@ resource "aws_kms_key" "test" { description = %[1]q deletion_window_in_days = 7 - policy = <