question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUERY] How can I determine when a secret from Key Vault has been purged?

See original GitHub issue

Library name and version

Azure.Security.KeyVault.Secrets 4.2.0

Query/Question

I have code to restore a key vault secret from a backup. Calling RestoreSecretBackupAsync will return a conflict if the secret exists in the key vault, so I delete the secret first using the following two lines, per the documentation.

var operation = await secretClient.StartDeleteSecretAsync(secretName).ConfigureAwait(false);
await operation.WaitForCompletionAsync().ConfigureAwait(false);

After the second line returns, I now have to purge the secret. After this, I confirm that the secret is no longer in the key vault by calling both GetSecret and GetDeletedSecret. Both return 404 indicating that the secret is neither active in the vault, nor is it in a deleted state.

Immediately following this I perform the restore with RestoreSecretBackupAsync which fails with a status of 409 Conflict because the key vault is saying that the secret still exists. The secret is in some limbo state it seems where it is no longer in the deleted state but it hasn’t been purged. After waiting anywhere from 1 to 5 seconds, I can restore. I have to do this by repeatedly attempting to restore until it succeeds.

My question: Is there a different way for me to determine if a secret has been completely purged? I would rather not rely on repeatedly failing to complete a restore.

Environment

.NET 4.7.1

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
heathscommented, Jan 4, 2022

Thanks for clarifying. Short of polling a little extra or handling the 409 Conflict case with a limited wait + retry loop, I don’t really have a better suggestion than unique naming. Resource management operations like this (not to be confused with ARM operations) are queued in the backend. The long-running operations (LROs) we provided were to try to make this easier and idiomatic for other LROs we have like creating certificates, or in lots of other Azure SDKs.

One reason we may not see this is because a while back I refactored our tests to delete ASAP and purge later; though, before that refactoring we basically did delete + purge in succession and I don’t recall seeing this.

That said, the number of resources in a Key Vault can make some backend queue operations slower, so if your SaaS offering manages a lot of resources that could be a contributing factor. For typical applications we recommend a Key Vault per application that should reduce the number of resources and lighten the load, but I appreciate that may not work in your situation.

1reaction
heathscommented, Jan 4, 2022

Sorry, no. The long-running operations for deleting and restoring resources like secrets actually work by querying for what should exist, but there’s no analog for purged resources like secrets. The only way to do so would be to try to create a secret and check the status code, but then the secret exists when purging is done and you’re back to square one. You’d have to poll as it seems you’re already doing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Azure Key Vault recovery overview
List, recover or purge soft deleted secrets, keys, and certificates · Log in to the Azure portal. · Select your key vault. ·...
Read more >
azure keyvault - Query to locate a secret
How can I query with the name of a secret and find the key vault name? I wonder whether it is possible to...
Read more >
Failed to purge a secret right after it was removed #12722
Describe the bug. Command Name az keyvault secret purge. Errors: Secret is currently being deleted.
Read more >
Azure Key Vault diagnostics to see who accessed your vault
Configure diagnostics logging for your vault; Querying the logs from your Azure Key Vault; Connecting Azure Monitor and Rules to set up ...
Read more >
Enable Key Vault Recoverability
Ensure that production Azure Key Vaults are recoverable in order to prevent permanent deletion/purging of encryption keys, secrets and certificates stored ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found