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 handling of creating an Access Policy for a non-existent Key Vault #2922

Merged
merged 2 commits into from
Feb 21, 2019
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
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) {
jen20 marked this conversation as resolved.
Show resolved Hide resolved
// 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)
}