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_cosmosdb_account: Fix submitted key_vault_key_id values #10420

Merged
merged 12 commits into from
Feb 10, 2021
Merged

azurerm_cosmosdb_account: Fix submitted key_vault_key_id values #10420

merged 12 commits into from
Feb 10, 2021

Conversation

tism
Copy link
Contributor

@tism tism commented Feb 2, 2021

Fixes #10379 by removing the doubled // (left the trailing / on KeyVaultBaseUrl) and using a versionless ID for azurerm_cosmosdb_account.key_vault_key_id. Not sure how well LatestVersionID fits into the NewxxID()/ID() parse package pattern.

Copy link
Contributor

@tombuildsstuff tombuildsstuff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @tism

Thanks for this PR - I've taken a look through and left some comments inline. On the whole this looks pretty good, but in retrospect perhaps we're being slightly misleading with the key_vault_key_id field, if we're requiring a versionless ID for the Key Vault Key, it'd be worth updating validation for this field to require this, rather than throwing away the version (and showing it in the plan) - WDYT?

Thanks!

@@ -37,7 +38,16 @@ func NewNestedItemID(keyVaultBaseUrl, nestedItemType, name, version string) (*Ne

func (n NestedItemId) ID() string {
// example: https://tharvey-keyvault.vault.azure.net/type/bird/fdf067c93bbb4b22bff4d8b7a9a56217
return fmt.Sprintf("%s/%s/%s/%s", n.KeyVaultBaseUrl, n.NestedItemType, n.Name, n.Version)
return formatID([]string{strings.TrimSuffix(n.KeyVaultBaseUrl, "/"), n.NestedItemType, n.Name, n.Version})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be honest we could arguably change this to be:

Suggested change
return formatID([]string{strings.TrimSuffix(n.KeyVaultBaseUrl, "/"), n.NestedItemType, n.Name, n.Version})
segments := []string{
strings.TrimSuffix(n.KeyVaultBaseUrl, "/"),
n.Name,
}
if n.Version != "" {
segments = append(segments, n.Version)
}
return strings.TrimPrefix(strings.Join(segments, "/"), "/")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 That'll also highlight if the struct was somehow populated incorrectly with missing values like name

func (n NestedItemId) LatestVersionID() string {
// example: https://tharvey-keyvault.vault.azure.net/type/bird
return formatID([]string{strings.TrimSuffix(n.KeyVaultBaseUrl, "/"), n.NestedItemType, n.Name})
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whilst there's some use-cases for this, Terraform shouldn't be using this directly (unless a users specifying it) - since this means the apply value for this could drift from the plan value - as such consolidating this into ID above should handle this.

This means we could update the key_vault_key_id field within the CosmosDB Account resource to take a versionless ID, by adding a Versionless validation function in azurerm/internal/services/keyvault/validate/nested_item_id.go - meaning what users pass in (the Versionless ID is what gets sent, ensuring the Plan matches the Apply).

This'd also need the versionless_id added to the azurerm_key_vault_key resource to be fair, but would make this more consistent in general - WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that, it makes the interface a lot clearer and the resource isn't making changes to the value under the covers.

@ghost ghost added size/M and removed size/S labels Feb 4, 2021
@tism
Copy link
Contributor Author

tism commented Feb 4, 2021

While running the key vault acceptance tests I couldn't get it to stop trying to purge the soft deleted keys, so anything that uses key_vault_uri will leave dangling vaults.

I thought purge_soft_delete_on_destroy = false would be enough to let the vault destroy pass and that is fine locally. Is there something different about how the acceptance tests are run?

@tombuildsstuff
Copy link
Contributor

I thought purge_soft_delete_on_destroy = false would be enough to let the vault destroy pass and that is fine locally. Is there something different about how the acceptance tests are run?

This'll be due to a bug in the Plugin SDK where at this time the test framework doesn't honour the provider blocks - it's something we're working to fix - for the moment if this leaves some dangling resources during test cleanup this is fine, as they'll be caught by our Dalek (cleanup script).

@tombuildsstuff tombuildsstuff added this to the v2.47.0 milestone Feb 4, 2021
@tombuildsstuff tombuildsstuff self-requested a review February 4, 2021 18:12
Copy link
Collaborator

@katbyte katbyte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @tism - LGTM 👍

katbyte added a commit that referenced this pull request Feb 10, 2021
@katbyte katbyte merged commit 5b6a0f8 into hashicorp:master Feb 10, 2021
@ghost
Copy link

ghost commented Feb 11, 2021

This has been released in version 2.47.0 of the provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading. As an example:

provider "azurerm" {
    version = "~> 2.47.0"
}
# ... other configuration ...

@tism tism deleted the roundtrip-keyvault-ids branch February 11, 2021 22:09
@ghost
Copy link

ghost commented Mar 13, 2021

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 [email protected]. Thanks!

@ghost ghost locked as resolved and limited conversation to collaborators Mar 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Setting key_vault_key_uri on azurerm_cosmosdb_account produces invalid URI passed to Azure
3 participants