From 6dc4feb4f8ff3f84ad1c04543fec61e200e0c005 Mon Sep 17 00:00:00 2001 From: Heath Stewart Date: Tue, 14 Nov 2023 14:01:56 -0800 Subject: [PATCH 1/2] List secret versions in Key Vault migration guide --- .../MigrationGuide.md | 39 +++++++++++++++++++ .../tests/samples/SampleSnippets.cs | 16 ++++++++ .../tests/samples/Track1Snippets.cs | 21 ++++++++++ 3 files changed, 76 insertions(+) diff --git a/sdk/keyvault/Azure.Security.KeyVault.Secrets/MigrationGuide.md b/sdk/keyvault/Azure.Security.KeyVault.Secrets/MigrationGuide.md index c04f22fa49a09..a3e867231e6e4 100644 --- a/sdk/keyvault/Azure.Security.KeyVault.Secrets/MigrationGuide.md +++ b/sdk/keyvault/Azure.Security.KeyVault.Secrets/MigrationGuide.md @@ -187,6 +187,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 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: diff --git a/sdk/keyvault/Azure.Security.KeyVault.Secrets/tests/samples/SampleSnippets.cs b/sdk/keyvault/Azure.Security.KeyVault.Secrets/tests/samples/SampleSnippets.cs index 366dec3314b9a..9cca45347f853 100644 --- a/sdk/keyvault/Azure.Security.KeyVault.Secrets/tests/samples/SampleSnippets.cs +++ b/sdk/keyvault/Azure.Security.KeyVault.Secrets/tests/samples/SampleSnippets.cs @@ -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. diff --git a/sdk/keyvault/Azure.Security.KeyVault.Secrets/tests/samples/Track1Snippets.cs b/sdk/keyvault/Azure.Security.KeyVault.Secrets/tests/samples/Track1Snippets.cs index fdc9adff9d8ac..ee28e7840f7bf 100644 --- a/sdk/keyvault/Azure.Security.KeyVault.Secrets/tests/samples/Track1Snippets.cs +++ b/sdk/keyvault/Azure.Security.KeyVault.Secrets/tests/samples/Track1Snippets.cs @@ -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 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. From 18eeb51c3ffa91e849ddfacebaadaa284aa4d1a3 Mon Sep 17 00:00:00 2001 From: Heath Stewart Date: Tue, 14 Nov 2023 14:17:56 -0800 Subject: [PATCH 2/2] Add new section to ToC --- sdk/keyvault/Azure.Security.KeyVault.Secrets/MigrationGuide.md | 1 + 1 file changed, 1 insertion(+) diff --git a/sdk/keyvault/Azure.Security.KeyVault.Secrets/MigrationGuide.md b/sdk/keyvault/Azure.Security.KeyVault.Secrets/MigrationGuide.md index a3e867231e6e4..59c6755109a19 100644 --- a/sdk/keyvault/Azure.Security.KeyVault.Secrets/MigrationGuide.md +++ b/sdk/keyvault/Azure.Security.KeyVault.Secrets/MigrationGuide.md @@ -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)