Skip to content

Commit

Permalink
Fix handling of creating an Access Policy for a non-existent Key Vault (
Browse files Browse the repository at this point in the history
  • Loading branch information
jen20 authored and katbyte committed Feb 21, 2019
1 parent eb422b7 commit 68da8c5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
6 changes: 5 additions & 1 deletion azurerm/resource_arm_key_vault_access_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@ func resourceArmKeyVaultAccessPolicyCreateOrDelete(d *schema.ResourceData, meta

keyVault, err := client.Get(ctx, resourceGroup, vaultName)
if err != nil {
if utils.ResponseWasNotFound(keyVault.Response) {
// If the key vault does not exist but this is not a new resource, the policy
// which previously existed was deleted with the key vault, so reflect that in
// state. If this is a new resource and key vault does not exist, it's likely
// a bad ID was given.
if utils.ResponseWasNotFound(keyVault.Response) && !d.IsNewResource() {
log.Printf("[DEBUG] Parent Key Vault %q was not found in Resource Group %q - removing from state!", vaultName, resourceGroup)
d.SetId("")
return nil
Expand Down
61 changes: 61 additions & 0 deletions azurerm/resource_arm_key_vault_access_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package azurerm

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
Expand Down Expand Up @@ -175,6 +176,24 @@ func TestAccAzureRMKeyVaultAccessPolicy_update(t *testing.T) {
})
}

func TestAccAzureRMKeyVaultAccessPolicy_nonExistentVault(t *testing.T) {
rs := acctest.RandString(6)
config := testAccAzureRMKeyVaultAccessPolicy_nonExistentVault(rs, testLocation())

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMKeyVaultDestroy,
Steps: []resource.TestStep{
{
Config: config,
ExpectNonEmptyPlan: true,
ExpectError: regexp.MustCompile(`Error retrieving Key Vault`),
},
},
})
}

func testCheckAzureRMKeyVaultAccessPolicyExists(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testAccProvider.Meta().(*ArmClient).keyVaultClient
Expand Down Expand Up @@ -386,3 +405,45 @@ resource "azurerm_key_vault" "test" {
}
`, rString, location, rString)
}

func testAccAzureRMKeyVaultAccessPolicy_nonExistentVault(rString string, location string) string {
return fmt.Sprintf(`
data "azurerm_client_config" "current" {}
resource "azurerm_resource_group" "test" {
name = "acctestRG-%s"
location = "%s"
}
resource "azurerm_key_vault" "test" {
name = "acctestkv-%s"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
tenant_id = "${data.azurerm_client_config.current.tenant_id}"
sku {
name = "standard"
}
tags {
environment = "Production"
}
}
resource "azurerm_key_vault_access_policy" "test" {
# Must appear to be URL, but not actually exist - appending a string works
key_vault_id = "${azurerm_key_vault.test.id}NOPE"
tenant_id = "${data.azurerm_client_config.current.tenant_id}"
object_id = "${data.azurerm_client_config.current.service_principal_object_id}"
key_permissions = [
"get",
]
secret_permissions = [
"get",
]
}
`, rString, location, rString)
}

0 comments on commit 68da8c5

Please sign in to comment.