forked from Azure/azure-sdk-for-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSample1_HelloWorldAsync.cs
90 lines (75 loc) · 3.85 KB
/
Sample1_HelloWorldAsync.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for
// license information.
using Azure.Identity;
using NUnit.Framework;
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace Azure.Security.KeyVault.Secrets.Samples
{
/// <summary>
/// Sample demonstrates how to set, get, update and delete a secret using the asynchronous methods of the SecretClient.
/// </summary>
[Category("Live")]
public partial class HelloWorld
{
[Test]
public async Task HelloWorldAsync()
{
// Environment variable with the Key Vault endpoint.
string keyVaultUrl = Environment.GetEnvironmentVariable("AZURE_KEYVAULT_URL");
// Instantiate a secret client that will be used to call the service. Notice that the client is using default Azure
// credentials. To make default credentials work, ensure that environment variables 'AZURE_CLIENT_ID',
// 'AZURE_CLIENT_KEY' and 'AZURE_TENANT_ID' are set with the service principal credentials.
var client = new SecretClient(new Uri(keyVaultUrl), new DefaultAzureCredential());
// Let's create a secret holding bank account credentials valid for 1 year. if the secret
// already exists in the key vault, then a new version of the secret is created.
string secretName = $"BankAccountPassword-{Guid.NewGuid()}";
var secret = new Secret(secretName, "f4G34fMh8v")
{
Expires = DateTimeOffset.Now.AddYears(1)
};
await client.SetAsync(secret);
// Let's Get the bank secret from the key vault.
Secret bankSecret = await client.GetAsync(secretName);
Debug.WriteLine($"Secret is returned with name {bankSecret.Name} and value {bankSecret.Value}");
// After one year, the bank account is still active, we need to update the expiry time of the secret.
// The update method can be used to update the expiry attribute of the secret. It cannot be used to update
// the value of the secret.
bankSecret.Expires = bankSecret.Expires.Value.AddYears(1);
SecretBase updatedSecret = await client.UpdateAsync(bankSecret);
Debug.WriteLine($"Secret's updated expiry time is {updatedSecret.Expires}");
// Bank forced a password update for security purposes. Let's change the value of the secret in the key vault.
// To achieve this, we need to create a new version of the secret in the key vault. The update operation cannot
// change the value of the secret.
var secretNewValue = new Secret(secretName, "bhjd4DDgsa");
secretNewValue.Expires = DateTimeOffset.Now.AddYears(1);
await client.SetAsync(secretNewValue);
// The bank account was closed. You need to delete its credentials from the key vault.
await client.DeleteAsync(secretName);
// To ensure secret is deleted on server side.
Assert.IsTrue(await WaitForDeletedSecretAsync(client, secretName));
// If the keyvault is soft-delete enabled, then for permanent deletion, deleted secret needs to be purged.
await client.PurgeDeletedAsync(secretName);
}
private async Task<bool> WaitForDeletedSecretAsync(SecretClient client, string secretName)
{
int maxIterations = 20;
for (int i = 0; i < maxIterations; i++)
{
try
{
await client.GetDeletedAsync(secretName);
return true;
}
catch
{
Thread.Sleep(5000);
}
}
return false;
}
}
}