Skip to content

Commit

Permalink
Add soft deletion retention to KMS key (#5131)
Browse files Browse the repository at this point in the history
  • Loading branch information
slevenick authored Aug 25, 2021
1 parent c7dc1c2 commit 02f686d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
6 changes: 6 additions & 0 deletions mmv1/products/kms/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ objects:
description: |
The time when KMS will create a new version of this Crypto Key.
output: true
- !ruby/object:Api::Type::String
name: 'destroyScheduledDuration'
input: true
description: |
The period of time that versions of this key spend in the DESTROY_SCHEDULED state before transitioning to DESTROYED.
If not specified at creation time, the default duration is 24 hours.
references: !ruby/object:Api::Resource::ReferenceLinks
guides:
'Creating a key':
Expand Down
2 changes: 2 additions & 0 deletions mmv1/products/kms/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ overrides: !ruby/object:Overrides::ResourceOverrides
default_value: :SOFTWARE
nextRotationTime: !ruby/object:Overrides::Terraform::PropertyOverride
exclude: true
destroyScheduledDuration: !ruby/object:Overrides::Terraform::PropertyOverride
default_from_api: true
custom_code: !ruby/object:Provider::Terraform::CustomCode
custom_delete: templates/terraform/custom_delete/kms_crypto_key.erb
custom_import: templates/terraform/custom_import/kms_crypto_key.go.erb
Expand Down
66 changes: 66 additions & 0 deletions mmv1/third_party/terraform/tests/resource_kms_crypto_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,41 @@ func TestAccKmsCryptoKey_template(t *testing.T) {
})
}

func TestAccKmsCryptoKey_destroyDuration(t *testing.T) {
t.Parallel()

projectId := fmt.Sprintf("tf-test-%d", randInt(t))
projectOrg := getTestOrgFromEnv(t)
location := getTestRegionFromEnv()
projectBillingAccount := getTestBillingAccountFromEnv(t)
keyRingName := fmt.Sprintf("tf-test-%s", randString(t, 10))
cryptoKeyName := fmt.Sprintf("tf-test-%s", randString(t, 10))

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testGoogleKmsCryptoKey_destroyDuration(projectId, projectOrg, projectBillingAccount, keyRingName, cryptoKeyName),
},
{
ResourceName: "google_kms_crypto_key.crypto_key",
ImportState: true,
ImportStateVerify: true,
},
// Use a separate TestStep rather than a CheckDestroy because we need the project to still exist.
{
Config: testGoogleKmsCryptoKey_removed(projectId, projectOrg, projectBillingAccount, keyRingName),
Check: resource.ComposeTestCheckFunc(
testAccCheckGoogleKmsCryptoKeyWasRemovedFromState("google_kms_crypto_key.crypto_key"),
testAccCheckGoogleKmsCryptoKeyVersionsDestroyed(t, projectId, location, keyRingName, cryptoKeyName),
testAccCheckGoogleKmsCryptoKeyRotationDisabled(t, projectId, location, keyRingName, cryptoKeyName),
),
},
},
})
}

// KMS KeyRings cannot be deleted. This ensures that the CryptoKey resource was removed from state,
// even though the server-side resource was not removed.
func testAccCheckGoogleKmsCryptoKeyWasRemovedFromState(resourceName string) resource.TestCheckFunc {
Expand Down Expand Up @@ -502,3 +537,34 @@ resource "google_kms_key_ring" "key_ring" {
}
`, projectId, projectId, projectOrg, projectBillingAccount, keyRingName)
}

func testGoogleKmsCryptoKey_destroyDuration(projectId, projectOrg, projectBillingAccount, keyRingName, cryptoKeyName string) string {
return fmt.Sprintf(`
resource "google_project" "acceptance" {
name = "%s"
project_id = "%s"
org_id = "%s"
billing_account = "%s"
}
resource "google_project_service" "acceptance" {
project = google_project.acceptance.project_id
service = "cloudkms.googleapis.com"
}
resource "google_kms_key_ring" "key_ring" {
project = google_project_service.acceptance.project
name = "%s"
location = "us-central1"
}
resource "google_kms_crypto_key" "crypto_key" {
name = "%s"
key_ring = google_kms_key_ring.key_ring.self_link
labels = {
key = "value"
}
destroy_scheduled_duration = "129600s"
}
`, projectId, projectId, projectOrg, projectBillingAccount, keyRingName, cryptoKeyName)
}

0 comments on commit 02f686d

Please sign in to comment.