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

azurerm_key_vault_managed_hardware_security_module - support for purging when soft deleted #17148

Merged
merged 3 commits into from
Jun 9, 2022
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
1 change: 1 addition & 0 deletions internal/features/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func Default() UserFeatures {
PurgeSoftDeletedKeysOnDestroy: true,
PurgeSoftDeletedCertsOnDestroy: true,
PurgeSoftDeletedSecretsOnDestroy: true,
PurgeSoftDeletedHSMsOnDestroy: true,
RecoverSoftDeletedKeyVaults: true,
RecoverSoftDeletedKeys: true,
RecoverSoftDeletedCerts: true,
Expand Down
1 change: 1 addition & 0 deletions internal/features/user_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type KeyVaultFeatures struct {
PurgeSoftDeletedKeysOnDestroy bool
PurgeSoftDeletedCertsOnDestroy bool
PurgeSoftDeletedSecretsOnDestroy bool
PurgeSoftDeletedHSMsOnDestroy bool
RecoverSoftDeletedKeyVaults bool
RecoverSoftDeletedKeys bool
RecoverSoftDeletedCerts bool
Expand Down
10 changes: 10 additions & 0 deletions internal/provider/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ func schemaFeatures(supportLegacyTestSuite bool) *pluginsdk.Schema {
Default: true,
},

"purge_soft_deleted_hardware_security_modules_on_destroy": {
Description: "When enabled soft-deleted `azurerm_key_vault_managed_hardware_security_module` resources will be permanently deleted (e.g purged), when destroyed",
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},

"recover_soft_deleted_certificates": {
Description: "When enabled soft-deleted `azurerm_key_vault_certificate` resources will be restored, instead of creating new ones",
Type: pluginsdk.TypeBool,
Expand Down Expand Up @@ -320,6 +327,9 @@ func expandFeatures(input []interface{}) features.UserFeatures {
if v, ok := keyVaultRaw["purge_soft_deleted_secrets_on_destroy"]; ok {
featuresMap.KeyVault.PurgeSoftDeletedSecretsOnDestroy = v.(bool)
}
if v, ok := keyVaultRaw["purge_soft_deleted_hardware_security_modules_on_destroy"]; ok {
featuresMap.KeyVault.PurgeSoftDeletedHSMsOnDestroy = v.(bool)
}
if v, ok := keyVaultRaw["recover_soft_deleted_certificates"]; ok {
featuresMap.KeyVault.RecoverSoftDeletedCerts = v.(bool)
}
Expand Down
74 changes: 42 additions & 32 deletions internal/provider/features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestExpandFeatures(t *testing.T) {
PurgeSoftDeletedKeysOnDestroy: true,
PurgeSoftDeletedSecretsOnDestroy: true,
PurgeSoftDeleteOnDestroy: true,
PurgeSoftDeletedHSMsOnDestroy: true,
RecoverSoftDeletedCerts: true,
RecoverSoftDeletedKeys: true,
RecoverSoftDeletedKeyVaults: true,
Expand Down Expand Up @@ -81,14 +82,15 @@ func TestExpandFeatures(t *testing.T) {
},
"key_vault": []interface{}{
map[string]interface{}{
"purge_soft_deleted_certificates_on_destroy": true,
"purge_soft_deleted_keys_on_destroy": true,
"purge_soft_deleted_secrets_on_destroy": true,
"purge_soft_delete_on_destroy": true,
"recover_soft_deleted_certificates": true,
"recover_soft_deleted_keys": true,
"recover_soft_deleted_key_vaults": true,
"recover_soft_deleted_secrets": true,
"purge_soft_deleted_certificates_on_destroy": true,
"purge_soft_deleted_keys_on_destroy": true,
"purge_soft_deleted_secrets_on_destroy": true,
"purge_soft_deleted_hardware_security_modules_on_destroy": true,
"purge_soft_delete_on_destroy": true,
"recover_soft_deleted_certificates": true,
"recover_soft_deleted_keys": true,
"recover_soft_deleted_key_vaults": true,
"recover_soft_deleted_secrets": true,
},
},
"log_analytics_workspace": []interface{}{
Expand Down Expand Up @@ -143,6 +145,7 @@ func TestExpandFeatures(t *testing.T) {
PurgeSoftDeletedKeysOnDestroy: true,
PurgeSoftDeletedSecretsOnDestroy: true,
PurgeSoftDeleteOnDestroy: true,
PurgeSoftDeletedHSMsOnDestroy: true,
RecoverSoftDeletedCerts: true,
RecoverSoftDeletedKeys: true,
RecoverSoftDeletedKeyVaults: true,
Expand Down Expand Up @@ -191,14 +194,15 @@ func TestExpandFeatures(t *testing.T) {
},
"key_vault": []interface{}{
map[string]interface{}{
"purge_soft_deleted_certificates_on_destroy": false,
"purge_soft_deleted_keys_on_destroy": false,
"purge_soft_deleted_secrets_on_destroy": false,
"purge_soft_delete_on_destroy": false,
"recover_soft_deleted_certificates": false,
"recover_soft_deleted_keys": false,
"recover_soft_deleted_key_vaults": false,
"recover_soft_deleted_secrets": false,
"purge_soft_deleted_certificates_on_destroy": false,
"purge_soft_deleted_keys_on_destroy": false,
"purge_soft_deleted_secrets_on_destroy": false,
"purge_soft_deleted_hardware_security_modules_on_destroy": false,
"purge_soft_delete_on_destroy": false,
"recover_soft_deleted_certificates": false,
"recover_soft_deleted_keys": false,
"recover_soft_deleted_key_vaults": false,
"recover_soft_deleted_secrets": false,
},
},
"log_analytics_workspace": []interface{}{
Expand Down Expand Up @@ -252,6 +256,7 @@ func TestExpandFeatures(t *testing.T) {
PurgeSoftDeletedCertsOnDestroy: false,
PurgeSoftDeletedKeysOnDestroy: false,
PurgeSoftDeletedSecretsOnDestroy: false,
PurgeSoftDeletedHSMsOnDestroy: false,
PurgeSoftDeleteOnDestroy: false,
RecoverSoftDeletedCerts: false,
RecoverSoftDeletedKeys: false,
Expand Down Expand Up @@ -510,6 +515,7 @@ func TestExpandFeaturesKeyVault(t *testing.T) {
PurgeSoftDeletedKeysOnDestroy: true,
PurgeSoftDeletedSecretsOnDestroy: true,
PurgeSoftDeleteOnDestroy: true,
PurgeSoftDeletedHSMsOnDestroy: true,
RecoverSoftDeletedCerts: true,
RecoverSoftDeletedKeys: true,
RecoverSoftDeletedKeyVaults: true,
Expand All @@ -523,14 +529,15 @@ func TestExpandFeaturesKeyVault(t *testing.T) {
map[string]interface{}{
"key_vault": []interface{}{
map[string]interface{}{
"purge_soft_deleted_certificates_on_destroy": true,
"purge_soft_deleted_keys_on_destroy": true,
"purge_soft_deleted_secrets_on_destroy": true,
"purge_soft_delete_on_destroy": true,
"recover_soft_deleted_certificates": true,
"recover_soft_deleted_keys": true,
"recover_soft_deleted_key_vaults": true,
"recover_soft_deleted_secrets": true,
"purge_soft_deleted_certificates_on_destroy": true,
"purge_soft_deleted_keys_on_destroy": true,
"purge_soft_deleted_secrets_on_destroy": true,
"purge_soft_deleted_hardware_security_modules_on_destroy": true,
"purge_soft_delete_on_destroy": true,
"recover_soft_deleted_certificates": true,
"recover_soft_deleted_keys": true,
"recover_soft_deleted_key_vaults": true,
"recover_soft_deleted_secrets": true,
},
},
},
Expand All @@ -540,6 +547,7 @@ func TestExpandFeaturesKeyVault(t *testing.T) {
PurgeSoftDeletedCertsOnDestroy: true,
PurgeSoftDeletedKeysOnDestroy: true,
PurgeSoftDeletedSecretsOnDestroy: true,
PurgeSoftDeletedHSMsOnDestroy: true,
PurgeSoftDeleteOnDestroy: true,
RecoverSoftDeletedCerts: true,
RecoverSoftDeletedKeys: true,
Expand All @@ -554,14 +562,15 @@ func TestExpandFeaturesKeyVault(t *testing.T) {
map[string]interface{}{
"key_vault": []interface{}{
map[string]interface{}{
"purge_soft_deleted_certificates_on_destroy": false,
"purge_soft_deleted_keys_on_destroy": false,
"purge_soft_deleted_secrets_on_destroy": false,
"purge_soft_delete_on_destroy": false,
"recover_soft_deleted_certificates": false,
"recover_soft_deleted_keys": false,
"recover_soft_deleted_key_vaults": false,
"recover_soft_deleted_secrets": false,
"purge_soft_deleted_certificates_on_destroy": false,
"purge_soft_deleted_keys_on_destroy": false,
"purge_soft_deleted_secrets_on_destroy": false,
"purge_soft_deleted_hardware_security_modules_on_destroy": false,
"purge_soft_delete_on_destroy": false,
"recover_soft_deleted_certificates": false,
"recover_soft_deleted_keys": false,
"recover_soft_deleted_key_vaults": false,
"recover_soft_deleted_secrets": false,
},
},
},
Expand All @@ -572,6 +581,7 @@ func TestExpandFeaturesKeyVault(t *testing.T) {
PurgeSoftDeletedKeysOnDestroy: false,
PurgeSoftDeletedSecretsOnDestroy: false,
PurgeSoftDeleteOnDestroy: false,
PurgeSoftDeletedHSMsOnDestroy: false,
RecoverSoftDeletedCerts: false,
RecoverSoftDeletedKeyVaults: false,
RecoverSoftDeletedKeys: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,16 @@ func resourceArmKeyVaultManagedHardwareSecurityModuleDelete(d *pluginsdk.Resourc
return err
}

// We need to grab the keyvault hsm to see if purge protection is enabled prior to deletion
resp, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil || resp.Location == nil {
if utils.ResponseWasNotFound(resp.Response) {
return nil
}

return fmt.Errorf("retrieving %s: %+v", id, err)
}

future, err := client.Delete(ctx, id.ResourceGroup, id.Name)
if err != nil {
return fmt.Errorf("deleting %s: %+v", id, err)
Expand All @@ -221,5 +231,23 @@ func resourceArmKeyVaultManagedHardwareSecurityModuleDelete(d *pluginsdk.Resourc
"waiting for deletion of API Management Service %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}
}

shouldPurge := meta.(*clients.Client).Features.KeyVault.PurgeSoftDeletedHSMsOnDestroy
if shouldPurge && resp.Properties != nil && utils.NormaliseNilableBool(resp.Properties.EnablePurgeProtection) {
return fmt.Errorf("cannot purge %s because purge protection is enabled", id)
}

purgeFuture, err := client.PurgeDeleted(ctx, id.Name, *resp.Location)
manicminer marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return fmt.Errorf("deleting %s: %+v", id, err)
}

if err = purgeFuture.WaitForCompletionRef(ctx, client.Client); err != nil {
if !response.WasNotFound(future.Response()) {
return fmt.Errorf(
"waiting for purge of %s: %+v", id, err)
}
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,13 @@ provider "azurerm" {
%s

resource "azurerm_key_vault_managed_hardware_security_module" "test" {
name = "kvHsm%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
sku_name = "Standard_B1"
tenant_id = data.azurerm_client_config.current.tenant_id
admin_object_ids = [data.azurerm_client_config.current.object_id]
name = "kvHsm%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
sku_name = "Standard_B1"
tenant_id = data.azurerm_client_config.current.tenant_id
admin_object_ids = [data.azurerm_client_config.current.object_id]
purge_protection_enabled = false
}
`, template, data.RandomInteger)
}
Expand Down Expand Up @@ -140,7 +141,7 @@ resource "azurerm_key_vault_managed_hardware_security_module" "test" {
location = azurerm_resource_group.test.location
sku_name = "Standard_B1"
soft_delete_retention_days = 7
purge_protection_enabled = true
purge_protection_enabled = false
tenant_id = data.azurerm_client_config.current.tenant_id
admin_object_ids = [data.azurerm_client_config.current.object_id]

Expand Down
2 changes: 2 additions & 0 deletions website/docs/guides/features-block.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ The `key_vault` block supports the following:

* `purge_soft_deleted_secrets_on_destroy` - (Optional) Should the `azurerm_key_vault_secret` resource be permanently deleted (e.g. purged) when destroyed? Defaults to `true`.

* `purge_soft_deleted_hardware_security_modules_on_destroy` - (Optional) Should the `azurerm_key_vault_managed_hardware_security_module` resource be permanently deleted (e.g. purged) when destroyed? Defaults to `true`.

* `recover_soft_deleted_certificates` - (Optional) Should the `azurerm_key_vault_certificate` resource recover a Soft-Deleted Certificate? Defaults to `true`.

* `recover_soft_deleted_key_vaults` - (Optional) Should the `azurerm_key_vault` resource recover a Soft-Deleted Key Vault? Defaults to `true`.
Expand Down