2.4 KiB
Delete and recover deleted secrets
This sample demonstrates how to delete and recover a deleted secret in Azure Key Vault.
Creating a SecretClient
To create a new SecretClient to create, get, update, or delete secrets, you need the endpoint to an Azure Key Vault and credentials.
Key Vault Secrets client for C++ currently supports any TokenCredential for authenticating.
In the sample below, you can create a credential by setting the Tenant ID, Client ID and Client Secret as environment variables.
auto credential = std::make_shared<Azure::Identity::DefaultAzureCredential>();
Then, in the sample below, you can set keyVaultUrl based on an environment variable, configuration setting, or any way that works for your application.
SecretClient secretClient(std::getenv("AZURE_KEYVAULT_URL"), credential);
Creating a Secret
Call SetSecret to create a new secret with name and secret value.
std::string secretName("MySampleSecret");
std::string secretValue("my secret value");
secretClient.SetSecret(secretName, secretValue);
Getting a Secret
Call GetSecret to retrieve a secret from Key Vault.
// get secret
KeyVaultSecret secret = secretClient.GetSecret(secretName).Value;
std::string valueString = secret.Value.HasValue() ? secret.Value.Value() : "NONE RETURNED";
std::cout << "Secret is returned with name " << secret.Name << " and value "
<< valueString << std::endl;
Deleting a secret
Call StartDeleteSecret to delete a secret. This is a long running operation.
// start deleting the secret
DeleteSecretOperation operation = secretClient.StartDeleteSecret(secret.Name);
Recover a Deleted secret
Call StartRecoverDeletedSecret to recover a deleted secret and then poll until the operation is done.
// call restore secret
RecoverDeletedSecretOperation recoverOperation = secretClient.StartRecoverDeletedSecret(secret.Name);
// poll until done
Secret restoredSecret = recoverOperation.PollUntilDone(2s).Value;
Source
To see the full example source, see: Source Code