forked from Azure/azure-sdk-for-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyClient.cs
708 lines (622 loc) · 40.4 KB
/
KeyClient.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for
// license information.
using Azure.Core;
using Azure.Core.Pipeline;
using Azure.Core.Pipeline.Policies;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Azure.Security.KeyVault.Keys
{
/// <summary>
/// The KeyClient provides synchronous and asynchronous methods to manage <see cref="Key"/> in the Azure Key Vault. The client
/// supports creating, retrieving, updating, deleting, purging, backing up, restoring and listing the <see cref="Key"/>.
/// The client also supports listing <see cref="DeletedKey"/> for a soft-delete enabled Azure Key Vault.
/// </summary>
public partial class KeyClient
{
private readonly Uri _vaultUri;
private readonly HttpPipeline _pipeline;
/// <summary>
/// Protected constructor to allow mocking
/// </summary>
protected KeyClient()
{
}
/// <summary>
/// Initializes a new instance of the KeyClient class.
/// </summary>
/// <param name="vaultUri">Endpoint URL for the Azure Key Vault service.</param>
/// <param name="credential">Represents a credential capable of providing an OAuth token.</param>
public KeyClient(Uri vaultUri, TokenCredential credential)
: this(vaultUri, credential, null)
{
}
/// <summary>
/// Initializes a new instance of the KeyClient class.
/// </summary>
/// <param name="vaultUri">Endpoint URL for the Azure Key Vault service.</param>
/// <param name="credential">Represents a credential capable of providing an OAuth token.</param>
/// <param name="options">Options that allow to configure the management of the request sent to Key Vault.</param>
public KeyClient(Uri vaultUri, TokenCredential credential, KeyClientOptions options)
{
_vaultUri = vaultUri ?? throw new ArgumentNullException(nameof(credential));
options = options ?? new KeyClientOptions();
_pipeline = HttpPipelineBuilder.Build(options,
bufferResponse: true,
new BearerTokenAuthenticationPolicy(credential, "https://vault.azure.net/.default"));
}
/// <summary>
/// Creates and stores a new key in Key Vault.
/// </summary>
/// <remarks>
/// The create key operation can be used to create any key type in Azure Key
/// Vault. If the named key already exists, Azure Key Vault creates a new
/// version of the key. It requires the keys/create permission.
/// </remarks>
/// <param name="name">The name of the key.</param>
/// <param name="keyType">The type of key to create. See <see cref="KeyType"/> for valid values.</param>
/// <param name="keyOptions">Specific attributes with information about the key.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual Response<Key> CreateKey(string name, KeyType keyType, KeyCreateOptions keyOptions = default, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(name)) throw new ArgumentException($"{nameof(name)} can't be empty or null");
if (keyType == default) throw new ArgumentNullException(nameof(keyType));
var parameters = new KeyRequestParameters(keyType, keyOptions);
return SendRequest(HttpPipelineMethod.Post, parameters, () => new Key(name), cancellationToken, KeysPath, name, "/create");
}
/// <summary>
/// Creates and stores a new key in Key Vault.
/// </summary>
/// <remarks>
/// The create key operation can be used to create any key type in Azure Key
/// Vault. If the named key already exists, Azure Key Vault creates a new
/// version of the key. It requires the keys/create permission.
/// </remarks>
/// <param name="name">The name of the key.</param>
/// <param name="keyType">The type of key to create. See <see cref="KeyType"/> for valid values.</param>
/// <param name="keyOptions">Specific attributes with information about the key.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual async Task<Response<Key>> CreateKeyAsync(string name, KeyType keyType, KeyCreateOptions keyOptions = default, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(name)) throw new ArgumentException($"{nameof(name)} can't be empty or null");
if (keyType == default) throw new ArgumentNullException(nameof(keyType));
var parameters = new KeyRequestParameters(keyType, keyOptions);
return await SendRequestAsync(HttpPipelineMethod.Post, parameters, () => new Key(name), cancellationToken, KeysPath, name, "/create");
}
/// <summary>
/// Creates and stores a new Elliptic Curve key in Key Vault.
/// </summary>
/// <remarks>
/// If the named key already exists, Azure Key Vault creates a new
/// version of the key. It requires the keys/create permission.
/// </remarks>
/// <param name="ecKey">The key options object containing information about the Elliptic Curve key being created.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual Response<Key> CreateEcKey(EcKeyCreateOptions ecKey, CancellationToken cancellationToken = default)
{
if (ecKey == default) throw new ArgumentNullException(nameof(ecKey));
var parameters = new KeyRequestParameters(ecKey);
return SendRequest(HttpPipelineMethod.Post, parameters, () => new Key(ecKey.Name), cancellationToken, KeysPath, ecKey.Name, "/create");
}
/// <summary>
/// Creates and stores a new Elliptic Curve key in Key Vault.
/// </summary>
/// <remarks>
/// If the named key already exists, Azure Key Vault creates a new
/// version of the key. It requires the keys/create permission.
/// </remarks>
/// <param name="ecKey">The key options object containing information about the Elliptic Curve key being created.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual async Task<Response<Key>> CreateEcKeyAsync(EcKeyCreateOptions ecKey, CancellationToken cancellationToken = default)
{
if (ecKey == default) throw new ArgumentNullException(nameof(ecKey));
var parameters = new KeyRequestParameters(ecKey);
return await SendRequestAsync(HttpPipelineMethod.Post, parameters, () => new Key(ecKey.Name), cancellationToken, KeysPath, ecKey.Name, "/create");
}
/// <summary>
/// Creates and stores a new RSA key in Key Vault.
/// </summary>
/// <remarks>
/// If the named key already exists, Azure Key Vault creates a new
/// version of the key. It requires the keys/create permission.
/// </remarks>
/// <param name="rsaKey">The key options object containing information about the RSA key being created.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual Response<Key> CreateRsaKey(RsaKeyCreateOptions rsaKey, CancellationToken cancellationToken = default)
{
if (rsaKey == default) throw new ArgumentNullException(nameof(rsaKey));
var parameters = new KeyRequestParameters(rsaKey);
return SendRequest(HttpPipelineMethod.Post, parameters, () => new Key(rsaKey.Name), cancellationToken, KeysPath, rsaKey.Name, "/create");
}
/// <summary>
/// Creates and stores a new RSA key in Key Vault.
/// </summary>
/// <remarks>
/// If the named key already exists, Azure Key Vault creates a new
/// version of the key. It requires the keys/create permission.
/// </remarks>
/// <param name="rsaKey">The key options object containing information about the RSA key being created.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual async Task<Response<Key>> CreateRsaKeyAsync(RsaKeyCreateOptions rsaKey, CancellationToken cancellationToken = default)
{
if (rsaKey == default) throw new ArgumentNullException(nameof(rsaKey));
var parameters = new KeyRequestParameters(rsaKey);
return await SendRequestAsync(HttpPipelineMethod.Post, parameters, () => new Key(rsaKey.Name), cancellationToken, KeysPath, rsaKey.Name, "/create");
}
/// <summary>
/// The update key operation changes specified attributes of a stored key and
/// can be applied to any key type and key version stored in Azure Key Vault.
/// </summary>
/// <remarks>
/// In order to perform this operation, the key must already exist in the Key
/// Vault. Note: The cryptographic material of a key itself cannot be changed.
/// This operation requires the keys/update permission.
/// </remarks>
/// <param name="key">The <see cref="KeyBase"/> object with updated properties.</param>
/// <param name="keyOperations">List of supported <see cref="KeyOperations"/>.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual Response<Key> UpdateKey(KeyBase key, IEnumerable<KeyOperations> keyOperations, CancellationToken cancellationToken = default)
{
if (key == null) throw new ArgumentNullException(nameof(key));
if (key.Version == null) throw new ArgumentNullException($"{nameof(key)}.{nameof(key.Version)}");
if (keyOperations == null) throw new ArgumentNullException(nameof(keyOperations));
var parameters = new KeyRequestParameters(key, keyOperations);
return SendRequest(HttpPipelineMethod.Patch, parameters, () => new Key(key.Name), cancellationToken, KeysPath, key.Name, "/", key.Version);
}
/// <summary>
/// The update key operation changes specified attributes of a stored key and
/// can be applied to any key type and key version stored in Azure Key Vault.
/// </summary>
/// <remarks>
/// In order to perform this operation, the key must already exist in the Key
/// Vault. Note: The cryptographic material of a key itself cannot be changed.
/// This operation requires the keys/update permission.
/// </remarks>
/// <param name="key">The <see cref="KeyBase"/> object with updated properties.</param>
/// <param name="keyOperations">List of supported <see cref="KeyOperations"/>.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual async Task<Response<Key>> UpdateKeyAsync(KeyBase key, IEnumerable<KeyOperations> keyOperations, CancellationToken cancellationToken = default)
{
if (key == null) throw new ArgumentNullException(nameof(key));
if (key.Version == null) throw new ArgumentNullException($"{nameof(key)}.{nameof(key.Version)}");
if (keyOperations == null) throw new ArgumentNullException(nameof(keyOperations));
var parameters = new KeyRequestParameters(key, keyOperations);
return await SendRequestAsync(HttpPipelineMethod.Patch, parameters, () => new Key(key.Name), cancellationToken, KeysPath, key.Name, "/", key.Version);
}
/// <summary>
/// Gets the public part of a stored key.
/// </summary>
/// <remarks>
/// The get key operation is applicable to all key types. If the requested key
/// is symmetric, then no key material is released in the response. This
/// operation requires the keys/get permission.
/// </remarks>
/// <param name="name">The name of the key.</param>
/// <param name="version">The version of the key.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual Response<Key> GetKey(string name, string version = null, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(name)) throw new ArgumentException($"{nameof(name)} can't be empty or null");
return SendRequest(HttpPipelineMethod.Get, () => new Key(name), cancellationToken, KeysPath, name, "/", version);
}
/// <summary>
/// Gets the public part of a stored key.
/// </summary>
/// <remarks>
/// The get key operation is applicable to all key types. If the requested key
/// is symmetric, then no key material is released in the response. This
/// operation requires the keys/get permission.
/// </remarks>
/// <param name="name">The name of the key.</param>
/// <param name="version">The version of the key.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual async Task<Response<Key>> GetKeyAsync(string name, string version = null, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(name)) throw new ArgumentException($"{nameof(name)} can't be empty or null");
return await SendRequestAsync(HttpPipelineMethod.Get, () => new Key(name), cancellationToken, KeysPath, name, "/", version);
}
/// <summary>
/// List keys in the specified vault.
/// </summary>
/// <remarks>
/// Retrieves a list of the keys in the Key Vault that contains the public part of a stored key.
/// The list operation is applicable to all key types, however only the base key identifier,
/// attributes, and tags are provided in the response. Individual versions of a
/// key are not listed in the response. This operation requires the keys/list
/// permission.
/// </remarks>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual IEnumerable<Response<KeyBase>> GetKeys(CancellationToken cancellationToken = default)
{
Uri firstPageUri = CreateFirstPageUri(KeysPath);
return PageResponseEnumerator.CreateEnumerable(nextLink => GetPage(firstPageUri, nextLink, () => new KeyBase(), cancellationToken));
}
/// <summary>
/// List keys in the specified vault.
/// </summary>
/// <remarks>
/// Retrieves a list of the keys in the Key Vault that contains the public part of a stored key.
/// The list operation is applicable to all key types, however only the base key identifier,
/// attributes, and tags are provided in the response. Individual versions of a
/// key are not listed in the response. This operation requires the keys/list
/// permission.
/// </remarks>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual IAsyncEnumerable<Response<KeyBase>> GetKeysAsync(CancellationToken cancellationToken = default)
{
Uri firstPageUri = CreateFirstPageUri(KeysPath);
return PageResponseEnumerator.CreateAsyncEnumerable(nextLink => GetPageAsync(firstPageUri, nextLink, () => new KeyBase(), cancellationToken));
}
/// <summary>
/// Retrieves a list of individual key versions with the same key name.
/// </summary>
/// <remarks>
/// The full key identifier, attributes, and tags are provided in the response.
/// This operation requires the keys/list permission.
/// </remarks>
/// <param name="name">The name of the key.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual IEnumerable<Response<KeyBase>> GetKeyVersions(string name, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(name)) throw new ArgumentException($"{nameof(name)} can't be empty or null");
Uri firstPageUri = CreateFirstPageUri($"{KeysPath}{name}/versions");
return PageResponseEnumerator.CreateEnumerable(nextLink => GetPage(firstPageUri, nextLink, () => new KeyBase(), cancellationToken));
}
/// <summary>
/// Retrieves a list of individual key versions with the same key name.
/// </summary>
/// <remarks>
/// The full key identifier, attributes, and tags are provided in the response.
/// This operation requires the keys/list permission.
/// </remarks>
/// <param name="name">The name of the key.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual IAsyncEnumerable<Response<KeyBase>> GetKeyVersionsAsync(string name, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(name)) throw new ArgumentException($"{nameof(name)} can't be empty or null");
Uri firstPageUri = CreateFirstPageUri($"{KeysPath}{name}/versions");
return PageResponseEnumerator.CreateAsyncEnumerable(nextLink => GetPageAsync(firstPageUri, nextLink, () => new KeyBase(), cancellationToken));
}
/// <summary>
/// Gets the public part of a deleted key.
/// </summary>
/// <remarks>
/// The Get Deleted Key operation is applicable for soft-delete enabled vaults.
/// While the operation can be invoked on any vault, it will return an error if
/// invoked on a non soft-delete enabled vault. This operation requires the
/// keys/get permission.
/// </remarks>
/// <param name="name">The name of the key.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual Response<DeletedKey> GetDeletedKey(string name, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(name)) throw new ArgumentException($"{nameof(name)} can't be empty or null");
return SendRequest(HttpPipelineMethod.Get, () => new DeletedKey(name), cancellationToken, DeletedKeysPath, name);
}
/// <summary>
/// Gets the public part of a deleted key.
/// </summary>
/// <remarks>
/// The Get Deleted Key operation is applicable for soft-delete enabled vaults.
/// While the operation can be invoked on any vault, it will return an error if
/// invoked on a non soft-delete enabled vault. This operation requires the
/// keys/get permission.
/// </remarks>
/// <param name="name">The name of the key.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual async Task<Response<DeletedKey>> GetDeletedKeyAsync(string name, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(name)) throw new ArgumentException($"{nameof(name)} can't be empty or null");
return await SendRequestAsync(HttpPipelineMethod.Get, () => new DeletedKey(name), cancellationToken, DeletedKeysPath, name);
}
/// <summary>
/// Deletes a key of any type from storage in Azure Key Vault.
/// </summary>
/// <remarks>
/// The delete key operation cannot be used to remove individual versions of a
/// key. This operation removes the cryptographic material associated with the
/// key, which means the key is not usable for Sign/Verify, Wrap/Unwrap or
/// Encrypt/Decrypt operations. This operation requires the keys/delete
/// permission.
/// </remarks>
/// <param name="name">The name of the key.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual Response<DeletedKey> DeleteKey(string name, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(name)) throw new ArgumentException($"{nameof(name)} can't be empty or null");
return SendRequest(HttpPipelineMethod.Delete, () => new DeletedKey(name), cancellationToken, KeysPath, name);
}
/// <summary>
/// Deletes a key of any type from storage in Azure Key Vault.
/// </summary>
/// <remarks>
/// The delete key operation cannot be used to remove individual versions of a
/// key. This operation removes the cryptographic material associated with the
/// key, which means the key is not usable for Sign/Verify, Wrap/Unwrap or
/// Encrypt/Decrypt operations. This operation requires the keys/delete
/// permission.
/// </remarks>
/// <param name="name">The name of the key.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual async Task<Response<DeletedKey>> DeleteKeyAsync(string name, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(name)) throw new ArgumentException($"{nameof(name)} can't be empty or null");
return await SendRequestAsync(HttpPipelineMethod.Delete, () => new DeletedKey(name), cancellationToken, KeysPath, name);
}
/// <summary>
/// Lists the deleted keys in the specified vault.
/// </summary>
/// <remarks>
/// Retrieves a list of the keys in the Key Vault that contains the public part of a deleted key.
/// This operation includes deletion-specific information.
/// The Get Deleted Keys operation is applicable
/// for vaults enabled for soft-delete. While the operation can be invoked on
/// any vault, it will return an error if invoked on a non soft-delete enabled
/// vault. This operation requires the keys/list permission.
/// </remarks>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual IEnumerable<Response<DeletedKey>> GetDeletedKeys(CancellationToken cancellationToken = default)
{
Uri firstPageUri = CreateFirstPageUri(DeletedKeysPath);
return PageResponseEnumerator.CreateEnumerable(nextLink => GetPage(firstPageUri, nextLink, () => new DeletedKey(), cancellationToken));
}
/// <summary>
/// Lists the deleted keys in the specified vault.
/// </summary>
/// <remarks>
/// Retrieves a list of the keys in the Key Vault that contains the public part of a deleted key.
/// This operation includes deletion-specific information.
/// The Get Deleted Keys operation is applicable
/// for vaults enabled for soft-delete. While the operation can be invoked on
/// any vault, it will return an error if invoked on a non soft-delete enabled
/// vault. This operation requires the keys/list permission.
/// </remarks>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual IAsyncEnumerable<Response<DeletedKey>> GetDeletedKeysAsync(CancellationToken cancellationToken = default)
{
Uri firstPageUri = CreateFirstPageUri(DeletedKeysPath);
return PageResponseEnumerator.CreateAsyncEnumerable(nextLink => GetPageAsync(firstPageUri, nextLink, () => new DeletedKey(), cancellationToken));
}
/// <summary>
/// Permanently deletes the specified key.
/// </summary>
/// <remarks>
/// The Purge Deleted Key operation is applicable for soft-delete enabled
/// vaults. While the operation can be invoked on any vault, it will return an
/// error if invoked on a non soft-delete enabled vault. This operation
/// requires the keys/purge permission.
/// </remarks>
/// <param name="name">The name of the key.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual Response PurgeDeletedKey(string name, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(name)) throw new ArgumentException($"{nameof(name)} can't be empty or null");
return SendRequest(HttpPipelineMethod.Delete, cancellationToken, DeletedKeysPath, name);
}
/// <summary>
/// Permanently deletes the specified key.
/// </summary>
/// <remarks>
/// The Purge Deleted Key operation is applicable for soft-delete enabled
/// vaults. While the operation can be invoked on any vault, it will return an
/// error if invoked on a non soft-delete enabled vault. This operation
/// requires the keys/purge permission.
/// </remarks>
/// <param name="name">The name of the key.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual async Task<Response> PurgeDeletedKeyAsync(string name, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(name)) throw new ArgumentException($"{nameof(name)} can't be empty or null");
return await SendRequestAsync(HttpPipelineMethod.Delete, cancellationToken, DeletedKeysPath, name);
}
/// <summary>
/// Recovers the deleted key to its latest version.
/// </summary>
/// <remarks>
/// The Recover Deleted Key operation is applicable for deleted keys in
/// soft-delete enabled vaults. It recovers the deleted key back to its latest
/// version under /keys. An attempt to recover an non-deleted key will return
/// an error. Consider this the inverse of the delete operation on soft-delete
/// enabled vaults. This operation requires the keys/recover permission.
/// </remarks>
/// <param name="name">The name of the key.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual Response<Key> RecoverDeletedKey(string name, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(name)) throw new ArgumentException($"{nameof(name)} can't be empty or null");
return SendRequest(HttpPipelineMethod.Post, () => new Key(name), cancellationToken, DeletedKeysPath, name, "/recover");
}
/// <summary>
/// Recovers the deleted key to its latest version.
/// </summary>
/// <remarks>
/// The Recover Deleted Key operation is applicable for deleted keys in
/// soft-delete enabled vaults. It recovers the deleted key back to its latest
/// version under /keys. An attempt to recover an non-deleted key will return
/// an error. Consider this the inverse of the delete operation on soft-delete
/// enabled vaults. This operation requires the keys/recover permission.
/// </remarks>
/// <param name="name">The name of the key.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual async Task<Response<Key>> RecoverDeletedKeyAsync(string name, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(name)) throw new ArgumentException($"{nameof(name)} can't be empty or null");
return await SendRequestAsync(HttpPipelineMethod.Post, () => new Key(name), cancellationToken, DeletedKeysPath, name, "/recover");
}
/// <summary>
/// Requests that a backup of the specified key be downloaded to the client.
/// </summary>
/// <remarks>
/// The Key Backup operation exports a key from Azure Key Vault in a protected
/// form. Note that this operation does NOT return key material in a form that
/// can be used outside the Azure Key Vault system, the returned key material
/// is either protected to a Azure Key Vault HSM or to Azure Key Vault itself.
/// The intent of this operation is to allow a client to GENERATE a key in one
/// Azure Key Vault instance, BACKUP the key, and then RESTORE it into another
/// Azure Key Vault instance. The BACKUP operation may be used to export, in
/// protected form, any key type from Azure Key Vault. Individual versions of a
/// key cannot be backed up. BACKUP / RESTORE can be performed within
/// geographical boundaries only; meaning that a BACKUP from one geographical
/// area cannot be restored to another geographical area. For example, a backup
/// from the US geographical area cannot be restored in an EU geographical
/// area. This operation requires the key/backup permission.
/// </remarks>
/// <param name="name">The name of the key.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual Response<byte[]> BackupKey(string name, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(name)) throw new ArgumentException($"{nameof(name)} can't be empty or null");
var backup = SendRequest(HttpPipelineMethod.Post, () => new KeyBackup(), cancellationToken, KeysPath, name, "/backup");
return new Response<byte[]>(backup.GetRawResponse(), backup.Value.Value);
}
/// <summary>
/// Requests that a backup of the specified key be downloaded to the client.
/// </summary>
/// <remarks>
/// The Key Backup operation exports a key from Azure Key Vault in a protected
/// form. Note that this operation does NOT return key material in a form that
/// can be used outside the Azure Key Vault system, the returned key material
/// is either protected to a Azure Key Vault HSM or to Azure Key Vault itself.
/// The intent of this operation is to allow a client to GENERATE a key in one
/// Azure Key Vault instance, BACKUP the key, and then RESTORE it into another
/// Azure Key Vault instance. The BACKUP operation may be used to export, in
/// protected form, any key type from Azure Key Vault. Individual versions of a
/// key cannot be backed up. BACKUP / RESTORE can be performed within
/// geographical boundaries only; meaning that a BACKUP from one geographical
/// area cannot be restored to another geographical area. For example, a backup
/// from the US geographical area cannot be restored in an EU geographical
/// area. This operation requires the key/backup permission.
/// </remarks>
/// <param name="name">The name of the key.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual async Task<Response<byte[]>> BackupKeyAsync(string name, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(name)) throw new ArgumentException($"{nameof(name)} can't be empty or null");
var backup = await SendRequestAsync(HttpPipelineMethod.Post, () => new KeyBackup(), cancellationToken, KeysPath, name, "/backup");
return new Response<byte[]>(backup.GetRawResponse(), backup.Value.Value);
}
/// <summary>
/// Restores a backed up key to a vault.
/// </summary>
/// <remarks>
/// Imports a previously backed up key into Azure Key Vault, restoring the key,
/// its key identifier, attributes and access control policies. The RESTORE
/// operation may be used to import a previously backed up key. Individual
/// versions of a key cannot be restored. The key is restored in its entirety
/// with the same key name as it had when it was backed up. If the key name is
/// not available in the target Key Vault, the RESTORE operation will be
/// rejected. While the key name is retained during restore, the final key
/// identifier will change if the key is restored to a different vault. Restore
/// will restore all versions and preserve version identifiers. The RESTORE
/// operation is subject to security constraints: The target Key Vault must be
/// owned by the same Microsoft Azure Subscription as the source Key Vault The
/// user must have RESTORE permission in the target Key Vault. This operation
/// requires the keys/restore permission.
/// </remarks>
/// <param name="backup">The backup blob associated with a key.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual Response<Key> RestoreKey(byte[] backup, CancellationToken cancellationToken = default)
{
if (backup == null) throw new ArgumentNullException(nameof(backup));
return SendRequest(HttpPipelineMethod.Post, new KeyBackup { Value = backup }, () => new Key(), cancellationToken, KeysPath, "/restore");
}
/// <summary>
/// Restores a backed up key to a vault.
/// </summary>
/// <remarks>
/// Imports a previously backed up key into Azure Key Vault, restoring the key,
/// its key identifier, attributes and access control policies. The RESTORE
/// operation may be used to import a previously backed up key. Individual
/// versions of a key cannot be restored. The key is restored in its entirety
/// with the same key name as it had when it was backed up. If the key name is
/// not available in the target Key Vault, the RESTORE operation will be
/// rejected. While the key name is retained during restore, the final key
/// identifier will change if the key is restored to a different vault. Restore
/// will restore all versions and preserve version identifiers. The RESTORE
/// operation is subject to security constraints: The target Key Vault must be
/// owned by the same Microsoft Azure Subscription as the source Key Vault The
/// user must have RESTORE permission in the target Key Vault. This operation
/// requires the keys/restore permission.
/// </remarks>
/// <param name="backup">The backup blob associated with a key.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual async Task<Response<Key>> RestoreKeyAsync(byte[] backup, CancellationToken cancellationToken = default)
{
if (backup == null) throw new ArgumentNullException(nameof(backup));
return await SendRequestAsync(HttpPipelineMethod.Post, new KeyBackup { Value = backup }, () => new Key(), cancellationToken, KeysPath, "/restore");
}
/// <summary>
/// Imports an externally created key, stores it, and returns key parameters
/// and attributes to the client.
/// </summary>
/// <remarks>
/// The import key operation may be used to import any key type into an Azure
/// Key Vault. If the named key already exists, Azure Key Vault creates a new
/// version of the key. This operation requires the keys/import permission.
/// </remarks>
/// <param name="name">The name of the key.</param>
/// <param name="keyMaterial">The <see cref="JsonWebKey"/> being imported.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual Response<Key> ImportKey(string name, JsonWebKey keyMaterial, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(name)) throw new ArgumentException($"{nameof(name)} can't be empty or null");
if (keyMaterial == default) throw new ArgumentNullException(nameof(keyMaterial));
var keyImportOptions = new KeyImportOptions(name, keyMaterial);
return SendRequest(HttpPipelineMethod.Put, keyImportOptions, () => new Key(name), cancellationToken, KeysPath, name);
}
/// <summary>
/// Imports an externally created key, stores it, and returns key parameters
/// and attributes to the client.
/// </summary>
/// <remarks>
/// The import key operation may be used to import any key type into an Azure
/// Key Vault. If the named key already exists, Azure Key Vault creates a new
/// version of the key. This operation requires the keys/import permission.
/// </remarks>
/// <param name="name">The name of the key.</param>
/// <param name="keyMaterial">The <see cref="JsonWebKey"/> being imported.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual async Task<Response<Key>> ImportKeyAsync(string name, JsonWebKey keyMaterial, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(name)) throw new ArgumentException($"{nameof(name)} can't be empty or null");
if (keyMaterial == default) throw new ArgumentNullException(nameof(keyMaterial));
var keyImportOptions = new KeyImportOptions(name, keyMaterial);
return await SendRequestAsync(HttpPipelineMethod.Put, keyImportOptions, () => new Key(name), cancellationToken, KeysPath, name);
}
/// <summary>
/// Imports an externally created key, stores it, and returns key parameters
/// and attributes to the client.
/// </summary>
/// <remarks>
/// The import key operation may be used to import any key type into an Azure
/// Key Vault. If the named key already exists, Azure Key Vault creates a new
/// version of the key. This operation requires the keys/import permission.
/// </remarks>
/// <param name="keyImportOptions">The key import configuration object containing information about the <see cref="JsonWebKey"/> being imported.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual Response<Key> ImportKey(KeyImportOptions keyImportOptions, CancellationToken cancellationToken = default)
{
if (keyImportOptions == default) throw new ArgumentNullException(nameof(keyImportOptions));
return SendRequest(HttpPipelineMethod.Put, keyImportOptions, () => new Key(keyImportOptions.Name), cancellationToken, KeysPath, keyImportOptions.Name);
}
/// <summary>
/// Imports an externally created key, stores it, and returns key parameters
/// and attributes to the client.
/// </summary>
/// <remarks>
/// The import key operation may be used to import any key type into an Azure
/// Key Vault. If the named key already exists, Azure Key Vault creates a new
/// version of the key. This operation requires the keys/import permission.
/// </remarks>
/// <param name="keyImportOptions">The key import configuration object containing information about the <see cref="JsonWebKey"/> being imported.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
public virtual async Task<Response<Key>> ImportKeyAsync(KeyImportOptions keyImportOptions, CancellationToken cancellationToken = default)
{
if (keyImportOptions == default) throw new ArgumentNullException(nameof(keyImportOptions));
return await SendRequestAsync(HttpPipelineMethod.Put, keyImportOptions, () => new Key(keyImportOptions.Name), cancellationToken, KeysPath, keyImportOptions.Name);
}
}
}