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

List secret versions in Key Vault migration guide #39994

Merged
merged 2 commits into from
Nov 14, 2023
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
40 changes: 40 additions & 0 deletions sdk/keyvault/Azure.Security.KeyVault.Secrets/MigrationGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Familiarity with the `Microsoft.Azure.KeyVault` library is assumed. For those ne
- [Setting secrets](#setting-secrets)
- [Getting secrets](#getting-secrets)
- [Listing secrets](#listing-secrets)
- [Listing secret versions](#listing-secret-versions)
- [Deleting secrets](#deleting-secrets)
- [Managing shared access signatures](#managing-shared-access-signatures)
- [Additional samples](#additional-samples)
Expand Down Expand Up @@ -187,6 +188,45 @@ foreach (SecretProperties item in client.GetPropertiesOfSecrets())
}
```

### Listing secret versions

Previously in `Microsoft.Azure.KeyVault`, you could list secret versions' properties using the `KeyVaultClient` and a specific Key Vault endpoint:

```C# Snippet:Microsoft_Azure_KeyVault_Secrets_Snippets_MigrationGuide_ListSecretVersions
IPage<SecretItem> page = await client.GetSecretVersionsAsync("https://myvault.vault.azure.net", "secret-name");
foreach (SecretItem item in page)
{
SecretIdentifier secretId = item.Identifier;
SecretBundle secret = await client.GetSecretAsync(secretId.Vault, secretId.Name, secretId.Version);
}

while (page.NextPageLink != null)
{
page = await client.GetSecretVersionsNextAsync(page.NextPageLink);
foreach (SecretItem item in page)
{
SecretIdentifier secretId = item.Identifier;
SecretBundle secret = await client.GetSecretAsync(secretId.Vault, secretId.Name, secretId.Version);
}
}
```

Now in `Azure.Security.KeyVault.Secrets`, you list secret versions' properties in the Key Vault you specified when constructing the `SecretClient`. This returns an enumerable that enumerates all secret versions across any number of pages. If you want to enumerate pages, call the `AsPages` method on the returned enumerable.

```C# Snippet:Azure_Security_KeyVault_Secrets_Snippets_MigrationGuide_ListSecretVersions
// List all secrets asynchronously.
await foreach (SecretProperties item in client.GetPropertiesOfSecretVersionsAsync("secret-name"))
{
KeyVaultSecret secret = await client.GetSecretAsync(item.Name, item.Version);
}

// List all secrets synchronously.
foreach (SecretProperties item in client.GetPropertiesOfSecretVersions("secret-name"))
{
KeyVaultSecret secret = client.GetSecret(item.Name, item.Version);
}
```

### Deleting secrets

Previously in `Microsoft.Azure.KeyVault`, you could delete a secret using the `KeyVaultClient` and a specific Key Vault endpoint:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,22 @@ private async Task MigrationGuide()
#endregion Snippet:Azure_Security_KeyVault_Secrets_Snippets_MigrationGuide_ListSecrets
}

{
#region Snippet:Azure_Security_KeyVault_Secrets_Snippets_MigrationGuide_ListSecretVersions
// List all secrets asynchronously.
await foreach (SecretProperties item in client.GetPropertiesOfSecretVersionsAsync("secret-name"))
{
KeyVaultSecret secret = await client.GetSecretAsync(item.Name, item.Version);
}

// List all secrets synchronously.
foreach (SecretProperties item in client.GetPropertiesOfSecretVersions("secret-name"))
{
KeyVaultSecret secret = client.GetSecret(item.Name, item.Version);
}
#endregion Snippet:Azure_Security_KeyVault_Secrets_Snippets_MigrationGuide_ListSecretVersions
}

{
#region Snippet:Azure_Security_KeyVault_Secrets_Snippets_MigrationGuide_DeleteSecret
// Delete the secret.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,27 @@ private static async Task Track1MigrationGuide()
#endregion Snippet:Microsoft_Azure_KeyVault_Secrets_Snippets_MigrationGuide_ListSecrets
}

{
#region Snippet:Microsoft_Azure_KeyVault_Secrets_Snippets_MigrationGuide_ListSecretVersions
IPage<SecretItem> page = await client.GetSecretVersionsAsync("https://myvault.vault.azure.net", "secret-name");
foreach (SecretItem item in page)
{
SecretIdentifier secretId = item.Identifier;
SecretBundle secret = await client.GetSecretAsync(secretId.Vault, secretId.Name, secretId.Version);
}

while (page.NextPageLink != null)
{
page = await client.GetSecretVersionsNextAsync(page.NextPageLink);
foreach (SecretItem item in page)
{
SecretIdentifier secretId = item.Identifier;
SecretBundle secret = await client.GetSecretAsync(secretId.Vault, secretId.Name, secretId.Version);
}
}
#endregion Snippet:Microsoft_Azure_KeyVault_Secrets_Snippets_MigrationGuide_ListSecretVersions
}

{
#region Snippet:Microsoft_Azure_KeyVault_Secrets_Snippets_MigrationGuide_DeleteSecret
// Delete the secret.
Expand Down