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_key - address issue with contacts data plane access when public network access is disabled #25124

Closed
wants to merge 4 commits into from

Conversation

WodansSon
Copy link
Collaborator

@WodansSon WodansSon commented Mar 3, 2024

(Fixes #9738)

This is just a POC that I am working on, while it fixes the issues I am specifically working on at the moment it may not address all issue related to #9738, but I think it will solve a majority of them.

@WodansSon WodansSon marked this pull request as draft March 3, 2024 10:08
Copy link
Collaborator

@magodo magodo left a comment

Choose a reason for hiding this comment

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

Thanks for this PR!
I've taken a look through and left some comments inline. The main point from me is that we shall reach out to the KV team to ask them to support an empty certificate contact resource (i.e. 0 contact defined), which will then makes things much easier.

@@ -117,8 +116,15 @@ func (r KeyVaultCertificateContactsResource) Create() sdk.ResourceFunc {

existing, err := client.GetCertificateContacts(ctx, *keyVaultBaseUri)
if err != nil {
// If we don't have access to the dataplane due to the public network access
Copy link
Collaborator

Choose a reason for hiding this comment

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

In this case, shall we just error out as the user's intent is to create the resource, but lacks of permission?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I am not exactly sure what the behavior should be here tbh... but returning an error is prolly more consistent with the provider so that is prolly a good call...

@@ -132,8 +138,13 @@ func (r KeyVaultCertificateContactsResource) Create() sdk.ResourceFunc {
ContactList: expandKeyVaultCertificateContactsContact(state.Contact),
}

if _, err := client.SetCertificateContacts(ctx, *keyVaultBaseUri, contacts); err != nil {
return fmt.Errorf("creating Key Vault Certificate Contacts %s: %+v", id, err)
// Don't set the contacts unless there are contacts defined...
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we put more texts here to explain why an empty contact resource is useful here (per my understanding, it is used as a guard to ensure any out of band updates to this resource will be tracked)?

Whilst one caveat is that it then allows users to define multiple such resources for a single key vault, which violates the fact that the contact should be a singleton.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Agreed, added more inline comments to explain why an empty resource will be created if the contact field is empty. As for the singleton, I am not currently aware of a way within the provider code to enforce singleton resource creation. I suspect that would be handled by the RP returning the conflict error, stating that there are multiple concurrent updates to the resource. The first one should succeed while the additional updates should fail with the conflict error?

Copy link
Collaborator

Choose a reason for hiding this comment

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

The timing of the updates of these two resources might happen to be in sequence, in which case even the RP has the concurrent update check, it still won't help.

Comment on lines 192 to 195
if utils.ResponseWasForbidden(existing.Response) {
metadata.Logger.Infof("[READ] unable to enumerate key vault certificate contacts at URL %s in %s - ignore error", id.KeyVaultBaseUrl, subscriptionResourceId)
return nil
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I still think in this case we shall just error out, as it is similar to a mgmt plane resource that exists but you don't have permission to access it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As stated above, that is prolly more consistent with standard provider behavior, updated to return and error instead of swallowing the error.

Comment on lines +200 to +205
state := KeyVaultCertificateContactsResourceModel{
KeyVaultId: keyVaultId.ID(),
Contact: make([]Contact, 0),
}

return metadata.Encode(&state)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This then disables one common Terraform behavior that removes non-exist resource from state. Instead, I think we shall reach to the KV service team to ask them to allow creating an empty contact resource (with 0 contact), then things will be much easier.

Copy link
Collaborator Author

@WodansSon WodansSon Mar 7, 2024

Choose a reason for hiding this comment

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

I understand what you are saying, however the core runtime requires all fields to have a value (even if empty). I added this code as a stop gap measure, between the RP and Terraform, that said if we can get the service team to agree to allow empty key vault contacts that would be ideal, however I am not sure how successful we will be in convincing them to do so, which is why I introduced this wrapper code as it is sort of equivalent.

return fmt.Errorf("checking for presence of existing Certificate Contacts (Key Vault %q): %s", id.KeyVaultBaseUrl, err)
// If we don't have access to the dataplane due to the public network access
// being disabled just return and keep the state unchanged...
if utils.ResponseWasForbidden(existing.Response) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As above, now return error base on data plane permissions...

Copy link

github-actions bot commented Apr 8, 2024

This PR is being labeled as "stale" because it has not been updated for 30 or more days.

If this PR is still valid, please remove the "stale" label. If this PR is blocked, please add it to the "Blocked" milestone.

If you need some help completing this PR, please leave a comment letting us know. Thank you!

@github-actions github-actions bot added the stale label Apr 8, 2024
@exe-r
Copy link

exe-r commented Apr 10, 2024

Glad to see this PR as we are currently affected from the same issue. Is this really stale at the moment?

@github-actions github-actions bot removed the stale label Apr 10, 2024
Copy link

This PR is being labeled as "stale" because it has not been updated for 30 or more days.

If this PR is still valid, please remove the "stale" label. If this PR is blocked, please add it to the "Blocked" milestone.

If you need some help completing this PR, please leave a comment letting us know. Thank you!

@github-actions github-actions bot added the stale label May 13, 2024
@WodansSon WodansSon closed this May 14, 2024
@WodansSon WodansSon deleted the exp_key_vault_contacts branch May 14, 2024 22:41
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 14, 2024
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.

keyvault: support for creating items over a nested endpoint
3 participants