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 #12555: Error "grant not found for key id" #12560

Merged
merged 3 commits into from
Apr 1, 2020
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
8 changes: 8 additions & 0 deletions aws/resource_aws_kms_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ func resourceAwsKmsGrantRead(d *schema.ResourceData, meta interface{}) error {
grant, err := findKmsGrantByIdWithRetry(conn, keyId, grantId)

if err != nil {
if isResourceNotFoundError(err) {
log.Printf("[WARN] %s KMS grant id not found for key id %s, removing from state file", grantId, keyId)
d.SetId("")
return nil
}
return err
}

Expand Down Expand Up @@ -306,6 +311,9 @@ func resourceAwsKmsGrantExists(d *schema.ResourceData, meta interface{}) (bool,
grant, err := findKmsGrantByIdWithRetry(conn, keyId, grantId)

if err != nil {
if isResourceNotFoundError(err) {
return false, nil
}
return true, err
}
if grant != nil {
Expand Down
42 changes: 42 additions & 0 deletions aws/resource_aws_kms_grant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
Expand Down Expand Up @@ -168,6 +170,27 @@ func TestAccAWSKmsGrant_ARN(t *testing.T) {
})
}

func TestAccAWSKmsGrant_disappears(t *testing.T) {
resourceName := "aws_kms_grant.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSKmsGrantDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSKmsGrant_Basic(rName, "\"Encrypt\", \"Decrypt\""),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSKmsGrantExists(resourceName),
testAccCheckAWSKmsGrantDisappears(resourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func testAccCheckAWSKmsGrantDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).kmsconn

Expand All @@ -194,6 +217,25 @@ func testAccCheckAWSKmsGrantExists(name string) resource.TestCheckFunc {
}
}

func testAccCheckAWSKmsGrantDisappears(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("not found: %s", name)
}

conn := testAccProvider.Meta().(*AWSClient).kmsconn

revokeInput := kms.RevokeGrantInput{
GrantId: aws.String(rs.Primary.Attributes["grant_id"]),
KeyId: aws.String(rs.Primary.Attributes["key_id"]),
}

_, err := conn.RevokeGrant(&revokeInput)
return err
}
}

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