forked from Azure-Samples/key-vault-dotnet-recovery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyVaultRecoverySamples.cs
246 lines (211 loc) · 12 KB
/
KeyVaultRecoverySamples.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
using Microsoft.Azure.Management.KeyVault.Fluent;
using Microsoft.Azure.Management.KeyVault.Fluent.Models;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
namespace AzureKeyVaultRecoverySamples
{
/// <summary>
/// Contains samples illustrating enabling recoverable deletion for Azure key vaults,
/// as well as exercising the recovery and purge functionality, respectively.
/// </summary>
public sealed class KeyVaultRecoverySamples : KeyVaultSampleBase
{
/// <summary>
/// Builds a vault recovery sample object with the specified parameters.
/// </summary>
/// <param name="tenantId">Tenant id.</param>
/// <param name="objectId">Object id of the Service Principal used to run the sample.</param>
/// <param name="appId">AD application id.</param>
/// <param name="appCredX5T">Thumbprint of the certificate set as the credential for the AD application.</param>
/// <param name="subscriptionId">Subscription id.</param>
/// <param name="resourceGroupName">Resource group name.</param>
/// <param name="vaultLocation">Location of the vault.</param>
/// <param name="vaultName">Vault name.</param>
public KeyVaultRecoverySamples(string tenantId, string objectId, string appId, string appCredX5T, string subscriptionId, string resourceGroupName, string vaultLocation, string vaultName)
: base(tenantId, objectId, appId, appCredX5T, subscriptionId, resourceGroupName, vaultLocation, vaultName)
{ }
/// <summary>
/// Builds a vault recovery sample object from configuration.
/// </summary>
public KeyVaultRecoverySamples()
: base()
{ }
#region samples
/// <summary>
/// Demonstrates how to enable soft delete on an existing vault, and then proceeds to delete, recover and purge the vault.
/// Assumes the caller has the KeyVaultContributor role in the subscription.
/// </summary>
/// <returns>Task representing this functionality.</returns>
public static async Task DemonstrateRecoveryAndPurgeForNewVaultAsync()
{
// instantiate the samples object
var sample = new KeyVaultRecoverySamples();
var rgName = sample.context.ResourceGroupName;
// derive a unique vault name for this sample
var vaultName = sample.context.VaultName + "new";
DeletedVaultInner deletedVault = null;
try
{
var vaultParameters = sample.CreateVaultParameters(rgName, vaultName, sample.context.PreferredLocation, enableSoftDelete: true, enablePurgeProtection: false);
Console.WriteLine("Operating with vault name '{0}' in resource group '{1}' and location '{2}'", vaultName, rgName, vaultParameters.Location);
// create new soft-delete-enabled vault
Console.Write("Creating vault...");
var vault = await sample.ManagementClient.Vaults.CreateOrUpdateAsync(rgName, vaultName, vaultParameters).ConfigureAwait(false);
Console.WriteLine("done.");
// wait for the DNS record to propagate; verify properties
Console.Write("Waiting for DNS propagation..");
Thread.Sleep(10 * 1000);
Console.WriteLine("done.");
Console.Write("Retrieving newly created vault...");
var retrievedVault = await sample.ManagementClient.Vaults.GetAsync(rgName, vaultName).ConfigureAwait(false);
Console.WriteLine("done.");
// delete vault
Console.Write("Deleting vault...");
await sample.ManagementClient.Vaults.DeleteAsync(rgName, vaultName).ConfigureAwait(false);
Console.WriteLine("done.");
// confirm the existence of the deleted vault
Console.Write("Retrieving deleted vault...");
deletedVault = await sample.ManagementClient.Vaults.GetDeletedAsync(vaultName, retrievedVault.Location).ConfigureAwait(false);
Console.WriteLine("done; '{0}' deleted on: {1}, scheduled for purge on: {2}", deletedVault.Id, deletedVault.Properties.DeletionDate, deletedVault.Properties.ScheduledPurgeDate);
// recover; set the creation mode as 'recovery' in the vault parameters
Console.Write("Recovering deleted vault...");
vaultParameters.Properties.CreateMode = CreateMode.Recover;
await sample.ManagementClient.Vaults.CreateOrUpdateAsync(rgName, vaultName, vaultParameters).ConfigureAwait(false);
Console.WriteLine("done.");
// confirm recovery
Console.Write("Verifying the existence of recovered vault...");
var recoveredVault = await sample.ManagementClient.Vaults.GetAsync(rgName, vaultName).ConfigureAwait(false);
Console.WriteLine("done.");
// delete vault
Console.Write("Deleting vault...");
await sample.ManagementClient.Vaults.DeleteAsync(rgName, vaultName).ConfigureAwait(false);
Console.WriteLine("done.");
// purge vault
Console.Write("Purging vault...");
deletedVault = await sample.ManagementClient.Vaults.GetDeletedAsync(vaultName, recoveredVault.Location).ConfigureAwait(false);
await sample.ManagementClient.Vaults.PurgeDeletedAsync(vaultName, recoveredVault.Location).ConfigureAwait(false);
Console.WriteLine("done.");
}
catch (Exception e)
{
Console.WriteLine("unexpected exception encountered running the test: {message}", e.Message);
throw;
}
// verify purge
try
{
Console.Write("Verifying vault deletion succeeded...");
await sample.ManagementClient.Vaults.GetAsync(rgName, vaultName);
}
catch (Exception e)
{
// no op; expected
VerifyExpectedARMException(e, HttpStatusCode.NotFound);
Console.WriteLine("done.");
}
try
{
Console.Write("Verifying vault purging succeeded...");
await sample.ManagementClient.Vaults.GetDeletedAsync(vaultName, deletedVault.Properties.Location).ConfigureAwait(false);
}
catch (Exception e)
{
// no op; expected
VerifyExpectedARMException(e, HttpStatusCode.NotFound);
Console.WriteLine("done.");
}
}
/// <summary>
/// Demonstrates how to enable soft delete on an existing vault, and then proceeds to delete, recover and purge the vault.
/// Assumes the caller has the KeyVaultContributor role in the subscription.
/// </summary>
/// <returns>Task representing this functionality.</returns>
public static async Task DemonstrateRecoveryAndPurgeForExistingVaultAsync()
{
// instantiate the samples object
var sample = new KeyVaultRecoverySamples();
var rgName = sample.context.ResourceGroupName;
// derive a unique vault name for this sample
var vaultName = sample.context.VaultName + "existing";
DeletedVaultInner deletedVault = null;
try
{
var vaultParameters = sample.CreateVaultParameters(rgName, vaultName, sample.context.PreferredLocation, enableSoftDelete: false, enablePurgeProtection: false);
Console.WriteLine("Operating with vault name '{0}' in resource group '{1}' and location '{2}'", vaultName, rgName, vaultParameters.Location);
// create new vault, not enabled for soft delete
Console.Write("Creating vault...");
var vault = await sample.ManagementClient.Vaults.CreateOrUpdateAsync(rgName, vaultName, vaultParameters).ConfigureAwait(false);
Console.WriteLine("done.");
// wait for the DNS record to propagate; verify properties
Console.Write("Waiting for DNS propagation..");
Thread.Sleep(10 * 1000);
Console.WriteLine("done.");
Console.Write("Retrieving newly created vault...");
var retrievedVault = await sample.ManagementClient.Vaults.GetAsync(rgName, vaultName).ConfigureAwait(false);
Console.WriteLine("done.");
// enable soft delete on existing vault
Console.Write("Enabling soft delete on existing vault...");
await sample.EnableRecoveryOptionsOnExistingVaultAsync(rgName, vaultName, enablePurgeProtection: false).ConfigureAwait(false);
Console.WriteLine("done.");
// delete vault
Console.Write("Deleting vault...");
await sample.ManagementClient.Vaults.DeleteAsync(rgName, vaultName).ConfigureAwait(false);
Console.WriteLine("done.");
// confirm the existence of the deleted vault
Console.Write("Retrieving deleted vault...");
deletedVault = await sample.ManagementClient.Vaults.GetDeletedAsync(vaultName, retrievedVault.Location).ConfigureAwait(false);
Console.WriteLine("done; '{0}' deleted on: {1}, scheduled for purge on: {2}", deletedVault.Id, deletedVault.Properties.DeletionDate, deletedVault.Properties.ScheduledPurgeDate);
// recover; set the creation mode as 'recovery' in the vault parameters
Console.Write("Recovering deleted vault...");
vaultParameters.Properties.CreateMode = CreateMode.Recover;
await sample.ManagementClient.Vaults.CreateOrUpdateAsync(rgName, vaultName, vaultParameters).ConfigureAwait(false);
Console.WriteLine("done.");
// confirm recovery
Console.Write("Verifying the existence of recovered vault...");
var recoveredVault = await sample.ManagementClient.Vaults.GetAsync(rgName, vaultName).ConfigureAwait(false);
Console.WriteLine("done.");
// delete vault
Console.Write("Deleting vault...");
await sample.ManagementClient.Vaults.DeleteAsync(rgName, vaultName).ConfigureAwait(false);
Console.WriteLine("done.");
// purge vault
Console.Write("Purging vault...");
deletedVault = await sample.ManagementClient.Vaults.GetDeletedAsync(vaultName, recoveredVault.Location).ConfigureAwait(false);
await sample.ManagementClient.Vaults.PurgeDeletedAsync(vaultName, recoveredVault.Location).ConfigureAwait(false);
Console.WriteLine("done.");
}
catch (Exception e)
{
Console.WriteLine("unexpected exception encountered running the test: {0}", e.Message);
throw;
}
// verify purge
try
{
Console.Write("Verifying vault deletion succeeded...");
await sample.ManagementClient.Vaults.GetAsync(rgName, vaultName);
}
catch (Exception e)
{
// no op; expected
VerifyExpectedARMException(e, HttpStatusCode.NotFound);
Console.WriteLine("done.");
}
try
{
Console.Write("Verifying vault purging succeeded...");
await sample.ManagementClient.Vaults.GetDeletedAsync(vaultName, deletedVault.Properties.Location).ConfigureAwait(false);
}
catch (Exception e)
{
// no op; expected
VerifyExpectedARMException(e, HttpStatusCode.NotFound);
Console.WriteLine("done.");
}
}
#endregion
}
}