Skip to content

Commit

Permalink
Update Key Vault, Search samples using SNIPPET def (#20439)
Browse files Browse the repository at this point in the history
* Update Key Vault, Search samples using SNIPPET def

Resolves Key Vault's and Search's portions of #20431

* Resolve PR feedback
  • Loading branch information
heaths authored Apr 15, 2021
1 parent 49d1452 commit 133c33c
Show file tree
Hide file tree
Showing 25 changed files with 310 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ and check for status or wait for completion.

```C# Snippet:ResumeBackupAsync
// Construct a new KeyVaultBackupClient or use an existing one.
KeyVaultBackupClient Client = new KeyVaultBackupClient(new Uri(keyVaultUrl), new DefaultAzureCredential());
KeyVaultBackupClient client = new KeyVaultBackupClient(new Uri(keyVaultUrl), new DefaultAzureCredential());

// Construct a BackupOperation using a KeyVaultBackupClient and the Id from a previously started operation.
BackupOperation backupOperation = new BackupOperation(client, backupOperationId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public void CreateClient()
// Create a new access control client using the default credential from Azure.Identity using environment variables previously set,
// including AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, and AZURE_TENANT_ID.
KeyVaultAccessControlClient client = new KeyVaultAccessControlClient(vaultUri: new Uri(keyVaultUrl), credential: new DefaultAzureCredential());
/*@@*/ client = Client;
#if !SNIPPET
client = Client;
#endif

// Retrieve all the role definitions.
List<KeyVaultRoleDefinition> roleDefinitions = client.GetRoleDefinitions(KeyVaultRoleScope.Global).ToList();
Expand Down Expand Up @@ -81,14 +83,17 @@ public void CreateRoleAssignment()
string servicePrincipalObjectId = _objectId;

#region Snippet:ReadmeCreateRoleAssignment
#if SNIPPET
// Replace <roleDefinitionId> with a role definition Id from the definitions returned from the List the role definitions section above
//@@string definitionIdToAssign = "<roleDefinitionId>";
string definitionIdToAssign = "<roleDefinitionId>";

// Replace <objectId> with the service principal object id from the Create/Get credentials section above
//@@string servicePrincipalObjectId = "<objectId>";
string servicePrincipalObjectId = "<objectId>";

//@@RoleAssignment createdAssignment = client.CreateRoleAssignment(RoleAssignmentScope.Global, properties);
/*@@*/ KeyVaultRoleAssignment createdAssignment = client.CreateRoleAssignment(KeyVaultRoleScope.Global, definitionIdToAssign, servicePrincipalObjectId, _roleAssignmentId);
RoleAssignment createdAssignment = client.CreateRoleAssignment(RoleAssignmentScope.Global, properties);
#else
KeyVaultRoleAssignment createdAssignment = client.CreateRoleAssignment(KeyVaultRoleScope.Global, definitionIdToAssign, servicePrincipalObjectId, _roleAssignmentId);
#endif

Console.WriteLine(createdAssignment.Name);
Console.WriteLine(createdAssignment.Properties.PrincipalId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,11 @@ public async Task BackupAndRestoreSampleSync()
while (!backupOperation.HasCompleted)
{
backupOperation.UpdateStatus();
/*@@*/ await DelayAsync(TimeSpan.FromSeconds(3));
//@@Thread.Sleep(3000);
#if SNIPPET
Thread.Sleep(3000);
#else
await DelayAsync(TimeSpan.FromSeconds(3));
#endif
}

// Get the Uri for the location of you backup blob.
Expand All @@ -113,8 +116,11 @@ public async Task BackupAndRestoreSampleSync()
while (!restoreOperation.HasCompleted)
{
restoreOperation.UpdateStatus();
/*@@*/ await DelayAsync(TimeSpan.FromSeconds(3));
//@@Thread.Sleep(3000);
#if SNIPPET
Thread.Sleep(3000);
#else
await DelayAsync(TimeSpan.FromSeconds(3));
#endif
}
Uri restoreResult = backupOperation.Value.FolderUri;
#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,14 @@ public void CreateRoleAssignment()
string servicePrincipalObjectId = _objectId;

#region Snippet:CreateRoleAssignment
//@@string definitionIdToAssign = "<roleDefinitionId>";
//@@string servicePrincipalObjectId = "<objectId>";

//@@KeyVaultRoleAssignment createdAssignment = client.CreateRoleAssignment(RoleAssignmentScope.Global, definitionIdToAssign, servicePrincipalObjectI);
/*@@*/KeyVaultRoleAssignment createdAssignment = client.CreateRoleAssignment(KeyVaultRoleScope.Global, definitionIdToAssign, servicePrincipalObjectId, _roleAssignmentId);
#if SNIPPET
string definitionIdToAssign = "<roleDefinitionId>";
string servicePrincipalObjectId = "<objectId>";

KeyVaultRoleAssignment createdAssignment = client.CreateRoleAssignment(RoleAssignmentScope.Global, definitionIdToAssign, servicePrincipalObjectI);
#else
KeyVaultRoleAssignment createdAssignment = client.CreateRoleAssignment(KeyVaultRoleScope.Global, definitionIdToAssign, servicePrincipalObjectId, _roleAssignmentId);
#endif
#endregion

#region Snippet:GetRoleAssignment
Expand Down Expand Up @@ -136,11 +139,14 @@ public async Task CreateRoleAssignmentAsync()
string servicePrincipalObjectId = _objectId;

#region Snippet:CreateRoleAssignmentAsync
//@@string definitionIdToAssign = "<roleDefinitionId>";
//@@string servicePrincipalObjectId = "<objectId>";

//@@KeyVaultRoleAssignment createdAssignment = await client.CreateRoleAssignmentAsync(RoleAssignmentScope.Global, definitionIdToAssign, servicePrincipalObjectId);
/*@@*/KeyVaultRoleAssignment createdAssignment = await client.CreateRoleAssignmentAsync(KeyVaultRoleScope.Global, definitionIdToAssign, servicePrincipalObjectId, _roleAssignmentId).ConfigureAwait(false);
#if SNIPPET
string definitionIdToAssign = "<roleDefinitionId>";
string servicePrincipalObjectId = "<objectId>";

KeyVaultRoleAssignment createdAssignment = await client.CreateRoleAssignmentAsync(RoleAssignmentScope.Global, definitionIdToAssign, servicePrincipalObjectId);
#else
KeyVaultRoleAssignment createdAssignment = await client.CreateRoleAssignmentAsync(KeyVaultRoleScope.Global, definitionIdToAssign, servicePrincipalObjectId, _roleAssignmentId).ConfigureAwait(false);
#endif
#endregion

#region Snippet:GetRoleAssignmentAsync
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@ public async Task CreateRoleAssignmentAsync()
string servicePrincipalObjectId = _objectId;

#region Snippet:CreateRoleAssignmentKeysScope
//@@string definitionIdToAssign = "<roleDefinitionId>";
//@@string servicePrincipalObjectId = "<objectId>";
#if SNIPPET
string definitionIdToAssign = "<roleDefinitionId>";
string servicePrincipalObjectId = "<objectId>";

//@@RoleAssignment keysScopedAssignment = await client.CreateRoleAssignmentAsync(RoleAssignmentScope.Global, definitionIdToAssign, servicePrincipalObjectId);
/*@@*/KeyVaultRoleAssignment keysScopedAssignment = await client.CreateRoleAssignmentAsync(KeyVaultRoleScope.Keys, definitionIdToAssign, servicePrincipalObjectId , _roleAssignmentId).ConfigureAwait(false);
RoleAssignment keysScopedAssignment = await client.CreateRoleAssignmentAsync(RoleAssignmentScope.Global, definitionIdToAssign, servicePrincipalObjectId);
#else
KeyVaultRoleAssignment keysScopedAssignment = await client.CreateRoleAssignmentAsync(KeyVaultRoleScope.Keys, definitionIdToAssign, servicePrincipalObjectId , _roleAssignmentId).ConfigureAwait(false);
#endif
#endregion

RegisterForCleanup(keysScopedAssignment);
Expand All @@ -60,11 +63,16 @@ public async Task CreateRoleAssignmentAsync()
RegisterKeyForCleanup(keyName);

#region Snippet:CreateRoleAssignmentKeyScope
//@@string keyName = "<your-key-name>";
#if SNIPPET
string keyName = "<your-key-name>";
#endif
KeyVaultKey key = await keyClient.GetKeyAsync(keyName);

//@@RoleAssignment keyScopedAssignment = await client.CreateRoleAssignmentAsync(new RoleAssignmentScope(key.Id), definitionIdToAssign, servicePrincipalObjectId);
/*@@*/KeyVaultRoleAssignment keyScopedAssignment = await client.CreateRoleAssignmentAsync(new KeyVaultRoleScope(key.Id), definitionIdToAssign, servicePrincipalObjectId, _roleAssignmentId).ConfigureAwait(false);
#if SNIPPET
RoleAssignment keyScopedAssignment = await client.CreateRoleAssignmentAsync(new RoleAssignmentScope(key.Id), definitionIdToAssign, servicePrincipalObjectId);
#else
KeyVaultRoleAssignment keyScopedAssignment = await client.CreateRoleAssignmentAsync(new KeyVaultRoleScope(key.Id), definitionIdToAssign, servicePrincipalObjectId, _roleAssignmentId).ConfigureAwait(false);
#endif
#endregion

RegisterForCleanup(keyScopedAssignment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public async Task BackupAndRestoreSampleAsync()
Assert.That(backupOperation.HasValue, Is.True);

#region Snippet:SelectiveRestoreAsync
//@@ string keyName = "<key name to restore>";
#if SNIPPET
string keyName = "<key name to restore>";
#endif

// Start the restore for a specific key that was previously backed up using the backupBlobUri returned from a previous BackupOperation.
SelectiveKeyRestoreOperation restoreOperation = await Client.StartSelectiveRestoreAsync(keyName, folderUri, sasToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@ public async Task ResumeBackupRestore()
var backupOperationId = originalBackupOperation.Id;

#region Snippet:ResumeBackupAsync
#if SNIPPET
// Construct a new KeyVaultBackupClient or use an existing one.
//@@KeyVaultBackupClient Client = new KeyVaultBackupClient(new Uri(keyVaultUrl), new DefaultAzureCredential());
KeyVaultBackupClient client = new KeyVaultBackupClient(new Uri(keyVaultUrl), new DefaultAzureCredential());
#endif

// Construct a BackupOperation using a KeyVaultBackupClient and the Id from a previously started operation.
BackupOperation backupOperation = new BackupOperation(client, backupOperationId);
/*@@*/backupOperation._retryAfterSeconds = (int)PollingInterval.TotalSeconds;
#if !SNIPPET
backupOperation._retryAfterSeconds = (int)PollingInterval.TotalSeconds;
#endif

// Wait for completion of the BackupOperation.
Response<BackupResult> backupResult = await backupOperation.WaitForCompletionAsync();
Expand All @@ -56,12 +60,16 @@ public async Task ResumeBackupRestore()
var restoreOperationId = originalRestoreOperation.Id;

#region Snippet:ResumeRestoreAsync
#if SNIPPET
// Construct a new KeyVaultBackupClient or use an existing one.
//@@KeyVaultBackupClient Client = new KeyVaultBackupClient(new Uri(keyVaultUrl), new DefaultAzureCredential());
KeyVaultBackupClient Client = new KeyVaultBackupClient(new Uri(keyVaultUrl), new DefaultAzureCredential());
#endif

// Construct a RestoreOperation using a KeyVaultBackupClient and the Id from a previously started operation.
RestoreOperation restoreOperation = new RestoreOperation(client, restoreOperationId);
/*@@*/restoreOperation._operationInternal._retryAfterSeconds = (int)PollingInterval.TotalSeconds;
#if !SNIPPET
restoreOperation._operationInternal._retryAfterSeconds = (int)PollingInterval.TotalSeconds;
#endif

// Wait for completion of the RestoreOperation.
RestoreResult restoreResult = await restoreOperation.WaitForCompletionAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ Previously in `Microsoft.Azure.KeyVault`, you could create a self-signed policy

```C# Snippet:Microsoft_Azure_KeyVault_Certificates_Snippets_MigrationGuide_CreateSelfSignedPolicy
CertificatePolicy policy = new CertificatePolicy
policy = new CertificatePolicy
{
IssuerParameters = new IssuerParameters("Self"),
X509CertificateProperties = new X509CertificateProperties("CN=DefaultPolicy")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,9 @@ public async Task VerifyImportCertificatePemWithoutIssuer()
byte[] certificateBytes = Encoding.ASCII.GetBytes(PemCertificateWithV3Extensions);

#region Snippet:CertificateClientLiveTests_VerifyImportCertificatePem
//@@byte[] certificateBytes = File.ReadAllBytes("certificate.pem");
#if SNIPPET
byte[] certificateBytes = File.ReadAllBytes("certificate.pem");
#endif

ImportCertificateOptions options = new ImportCertificateOptions(certificateName, certificateBytes)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ public void ImportPfxCertificateSync()

#region Snippet:CertificatesSample3ImportPfxCertificate
string name = $"cert-{Guid.NewGuid()}";
//@@ byte[] pfx = File.ReadAllBytes("certificate.pfx");
/*@@*/ byte[] pfx = Convert.FromBase64String(s_pfxBase64);
#if SNIPPET
byte[] pfx = File.ReadAllBytes("certificate.pfx");
#else
byte[] pfx = Convert.FromBase64String(s_pfxBase64);
#endif
ImportCertificateOptions importOptions = new ImportCertificateOptions(name, pfx)
{
Policy = new CertificatePolicy(WellKnownIssuerNames.Self, "CN=contoso.com")
Expand Down Expand Up @@ -64,8 +67,11 @@ public void ImportPemCertificateSync()

#region Snippet:CertificatesSample3ImportPemCertificate
string name = $"cert-{Guid.NewGuid()}";
//@@ byte[] pem = File.ReadAllBytes("certificate.cer");
/*@@*/ byte[] pem = Encoding.ASCII.GetBytes(s_pem);
#if SNIPPET
byte[] pem = File.ReadAllBytes("certificate.cer");
#else
byte[] pem = Encoding.ASCII.GetBytes(s_pem);
#endif
ImportCertificateOptions importOptions = new ImportCertificateOptions(name, pem)
{
Policy = new CertificatePolicy(WellKnownIssuerNames.Self, "CN=contoso.com")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ public async Task ImportPfxCertificateAsync()
CertificateClient client = new CertificateClient(new Uri(keyVaultUrl), new DefaultAzureCredential());

string name = $"cert-{Guid.NewGuid()}";
//@@ byte[] pfx = File.ReadAllBytes("certificate.pfx");
/*@@*/ byte[] pfx = Convert.FromBase64String(s_pfxBase64);
#if SNIPPET
byte[] pfx = File.ReadAllBytes("certificate.pfx");
#else
byte[] pfx = Convert.FromBase64String(s_pfxBase64);
#endif
ImportCertificateOptions importOptions = new ImportCertificateOptions(name, pfx)
{
Policy = new CertificatePolicy(WellKnownIssuerNames.Self, "CN=contoso.com")
Expand Down Expand Up @@ -54,8 +57,11 @@ public async Task ImportPemCertificateAsync()
CertificateClient client = new CertificateClient(new Uri(keyVaultUrl), new DefaultAzureCredential());

string name = $"cert-{Guid.NewGuid()}";
//@@ byte[] pem = File.ReadAllBytes("certificate.cer");
/*@@*/ byte[] pem = Encoding.ASCII.GetBytes(s_pem);
#if SNIPPET
byte[] pem = File.ReadAllBytes("certificate.cer");
#else
byte[] pem = Encoding.ASCII.GetBytes(s_pem);
#endif
ImportCertificateOptions importOptions = new ImportCertificateOptions(name, pem)
{
Policy = new CertificatePolicy(WellKnownIssuerNames.Self, "CN=contoso.com")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,11 @@ private async Task MigrationGuide()
Transport = new HttpClientTransport(httpClient)
};

//@@CertificateClient client = new CertificateClient(
/*@@*/ CertificateClient _ = new CertificateClient(
#if SNIPPET
CertificateClient client = new CertificateClient(
#else
CertificateClient _ = new CertificateClient(
#endif
new Uri("https://myvault.vault.azure.net"),
new DefaultAzureCredential(),
options);
Expand Down Expand Up @@ -221,8 +224,11 @@ private async Task MigrationGuide()
#endregion Snippet:Azure_Security_KeyVault_Certificates_Snippets_MigrationGuide_CreateSelfSignedPolicy

#region Snippet:Azure_Security_KeyVault_Certificates_Snippets_MigrationGuide_CreateSelfSignedPolicy
//@@CertificatePolicy policy = CertificatePolicy.Default;
/*@@*/ policy = CertificatePolicy.Default;
#if SNIPPET
CertificatePolicy policy = CertificatePolicy.Default;
#else
policy = CertificatePolicy.Default;
#endif
#endregion Snippet:Azure_Security_KeyVault_Certificates_Snippets_MigrationGuide_CreateSelfSignedPolicy
{
#region Snippet:Azure_Security_KeyVault_Certificates_Snippets_MigrationGuide_CreateCertificate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ public void GetKeysSync()
IEnumerable<KeyProperties> keys = client.GetPropertiesOfKeys();
foreach (KeyProperties key in keys)
{
/*@@*/ if (key.Managed) continue;
#if !SNIPPET
if (key.Managed) continue;
#endif
KeyVaultKey keyWithType = client.GetKey(key.Name);
Debug.WriteLine($"Key is returned with name {keyWithType.Name} and type {keyWithType.KeyType}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public async Task GetKeysAsync()
// So, for each returned key we call GetKey to get the actual key.
await foreach (KeyProperties key in client.GetPropertiesOfKeysAsync())
{
/*@@*/ if (key.Managed) continue;
#if !SNIPPET
if (key.Managed) continue;
#endif
KeyVaultKey keyWithType = await client.GetKeyAsync(key.Name);
Debug.WriteLine($"Key is returned with name {keyWithType.Name} and type {keyWithType.KeyType}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,11 @@ private async Task MigrationGuide()
Transport = new HttpClientTransport(httpClient)
};

//@@KeyClient client = new KeyClient(
/*@@*/ client = new KeyClient(
#if SNIPPET
KeyClient client = new KeyClient(
#else
client = new KeyClient(
#endif
new Uri("https://myvault.vault.azure.net"),
new DefaultAzureCredential(),
options);
Expand All @@ -295,8 +298,11 @@ private async Task MigrationGuide()
Transport = new HttpClientTransport(httpClient)
};

//@@CryptographyClient cryptoClient = new CryptographyClient(
/*@@*/ cryptoClient = new CryptographyClient(
#if SNIPPET
CryptographyClient cryptoClient = new CryptographyClient(
#else
cryptoClient = new CryptographyClient(
#endif
new Uri("https://myvault.vault.azure.net"),
new DefaultAzureCredential(),
cryptoOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public void GetSecretsSync()
IEnumerable<SecretProperties> secrets = client.GetPropertiesOfSecrets();
foreach (SecretProperties secret in secrets)
{
/*@@*/ if (secret.Managed) continue;
#if !SNIPPET
if (secret.Managed) continue;
#endif
// Getting a disabled secret will fail, so skip disabled secrets.
if (!secret.Enabled.GetValueOrDefault())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public async Task GetSecretsAsync()

await foreach (SecretProperties secret in client.GetPropertiesOfSecretsAsync())
{
/*@@*/ if (secret.Managed) continue;
#if !SNIPPET
if (secret.Managed) continue;
#endif
// Getting a disabled secret will fail, so skip disabled secrets.
if (!secret.Enabled.GetValueOrDefault())
{
Expand Down
Loading

0 comments on commit 133c33c

Please sign in to comment.