/
CertificateClient.cs
1797 lines (1575 loc) · 107 KB
/
CertificateClient.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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Core.Pipeline;
namespace Azure.Security.KeyVault.Certificates
{
/// <summary>
/// The CertificateClient provides synchronous and asynchronous methods to manage <see cref="KeyVaultCertificate"/>s in Azure Key Vault. The client
/// supports creating, retrieving, updating, deleting, purging, backing up, restoring, and listing the <see cref="KeyVaultCertificate"/>, along with managing
/// certificate <see cref="CertificateIssuer"/>s and <see cref="CertificateContact"/>s. The client also supports listing <see cref="DeletedCertificate"/> for a soft delete
/// enabled key vault.
/// </summary>
public class CertificateClient
{
internal const string CertificatesPath = "/certificates/";
internal const string DeletedCertificatesPath = "/deletedcertificates/";
private const string IssuersPath = "/certificates/issuers/";
private const string ContactsPath = "/certificates/contacts/";
private readonly KeyVaultPipeline _pipeline;
/// <summary>
/// Initializes a new instance of the <see cref="CertificateClient"/> class for mocking.
/// </summary>
protected CertificateClient()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="CertificateClient"/> class for the specified vault.
/// </summary>
/// <param name="vaultUri">
/// A <see cref="Uri"/> to the vault on which the client operates. Appears as "DNS Name" in the Azure portal.
/// If you have a certificate <see cref="Uri"/>, use <see cref="KeyVaultCertificateIdentifier"/> to parse the <see cref="KeyVaultCertificateIdentifier.VaultUri"/> and other information.
/// </param>
/// <param name="credential">A <see cref="TokenCredential"/> used to authenticate requests to the vault, such as DefaultAzureCredential.</param>
/// <exception cref="ArgumentNullException"><paramref name="vaultUri"/> or <paramref name="credential"/> is null.</exception>
public CertificateClient(Uri vaultUri, TokenCredential credential)
: this(vaultUri, credential, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="CertificateClient"/> class for the specified vault.
/// </summary>
/// <param name="vaultUri">
/// A <see cref="Uri"/> to the vault on which the client operates. Appears as "DNS Name" in the Azure portal.
/// If you have a certificate <see cref="Uri"/>, use <see cref="KeyVaultCertificateIdentifier"/> to parse the <see cref="KeyVaultCertificateIdentifier.VaultUri"/> and other information.
/// </param>
/// <param name="credential">A <see cref="TokenCredential"/> used to authenticate requests to the vault, such as DefaultAzureCredential.</param>
/// <param name="options"><see cref="CertificateClientOptions"/> that allow to configure the management of the request sent to Key Vault.</param>
/// <exception cref="ArgumentNullException"><paramref name="vaultUri"/> or <paramref name="credential"/> is null.</exception>
public CertificateClient(Uri vaultUri, TokenCredential credential, CertificateClientOptions options)
{
Argument.AssertNotNull(vaultUri, nameof(vaultUri));
Argument.AssertNotNull(credential, nameof(credential));
options ??= new CertificateClientOptions();
HttpPipeline pipeline = HttpPipelineBuilder.Build(options, new ChallengeBasedAuthenticationPolicy(credential));
_pipeline = new KeyVaultPipeline(vaultUri, options.GetVersionString(), pipeline, new ClientDiagnostics(options));
}
/// <summary>
/// Gets the <see cref="Uri"/> of the vault used to create this instance of the <see cref="CertificateClient"/>.
/// </summary>
public virtual Uri VaultUri => _pipeline.VaultUri;
/// <summary>
/// Starts a long running operation to create a <see cref="KeyVaultCertificate"/> in the vault with the specified certificate policy.
/// </summary>
/// <remarks>
/// If no certificate with the specified name exists it will be created; otherwise, a new version of the existing certificate will be created.
/// This operation requires the certificates/create permission.
/// </remarks>
/// <param name="certificateName">The name of the certificate to create.</param>
/// <param name="policy">The <see cref="CertificatePolicy"/> which governs the properties and lifecycle of the created certificate.</param>
/// <param name="enabled">Specifies whether the certificate should be created in an enabled state. If null, the server default will be used.</param>
/// <param name="tags">Tags to be applied to the created certificate.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>A <see cref="CertificateOperation"/> which contains details on the create operation, and can be used to retrieve updated status.</returns>
/// <exception cref="ArgumentException"><paramref name="certificateName"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="certificateName"/> or <paramref name="policy"/> is null.</exception>
public virtual CertificateOperation StartCreateCertificate(string certificateName, CertificatePolicy policy, bool? enabled = default, IDictionary<string, string> tags = default, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(certificateName, nameof(certificateName));
Argument.AssertNotNull(policy, nameof(policy));
var parameters = new CertificateCreateParameters(policy, enabled, tags);
using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CertificateClient)}.{nameof(StartCreateCertificate)}");
scope.AddAttribute("certificate", certificateName);
scope.Start();
try
{
Response<CertificateOperationProperties> response = _pipeline.SendRequest(RequestMethod.Post, parameters, () => new CertificateOperationProperties(), cancellationToken, CertificatesPath, certificateName, "/create");
return new CertificateOperation(response, this);
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
/// <summary>
/// Starts a long running operation to create a <see cref="KeyVaultCertificate"/> in the vault with the specified certificate policy.
/// </summary>
/// <remarks>
/// If no certificate with the specified name exists it will be created; otherwise, a new version of the existing certificate will be created.
/// This operation requires the certificates/create permission.
/// </remarks>
/// <param name="certificateName">The name of the certificate to create.</param>
/// <param name="policy">The <see cref="CertificatePolicy"/> which governs the properties and lifecycle of the created certificate.</param>
/// <param name="enabled">Specifies whether the certificate should be created in an enabled state. If null, the server default will be used.</param>
/// <param name="tags">Tags to be applied to the created certificate.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>A <see cref="CertificateOperation"/> which contains details on the create operation, and can be used to retrieve updated status.</returns>
/// <exception cref="ArgumentException"><paramref name="certificateName"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="certificateName"/> or <paramref name="policy"/> is null.</exception>
public virtual async Task<CertificateOperation> StartCreateCertificateAsync(string certificateName, CertificatePolicy policy, bool? enabled = default, IDictionary<string, string> tags = default, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(certificateName, nameof(certificateName));
Argument.AssertNotNull(policy, nameof(policy));
var parameters = new CertificateCreateParameters(policy, enabled, tags);
using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CertificateClient)}.{nameof(StartCreateCertificate)}");
scope.AddAttribute("certificate", certificateName);
scope.Start();
try
{
Response<CertificateOperationProperties> response = await _pipeline.SendRequestAsync(RequestMethod.Post, parameters, () => new CertificateOperationProperties(), cancellationToken, CertificatesPath, certificateName, "/create").ConfigureAwait(false);
return new CertificateOperation(response, this);
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
#pragma warning disable AZC0002 // DO ensure all service methods, both asynchronous and synchronous, take an optional CancellationToken parameter called cancellationToken.
#pragma warning disable AZC0015 // Unexpected client method return type.
/// <summary>
/// Creates an <see cref="X509Certificate2"/> from the specified certificate.
/// </summary>
/// <remarks>
/// Because <see cref="KeyVaultCertificate.Cer"/> contains only the public key, this method attempts to download the managed secret
/// that contains the full certificate. If you do not have permissions to get the secret,
/// <see cref="RequestFailedException"/> will be thrown with an appropriate error response.
/// If you want an <see cref="X509Certificate2"/> with only the public key, instantiate it passing only the
/// <see cref="KeyVaultCertificate.Cer"/> property.
/// This operation requires the certificates/get and secrets/get permissions.
/// </remarks>
/// <param name="certificateName">The name of the certificate to download.</param>
/// <param name="version">Optional version of a certificate to download.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>An <see cref="X509Certificate2"/> from the specified certificate.</returns>
/// <exception cref="ArgumentException"><paramref name="certificateName"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="certificateName"/> is null.</exception>
/// <exception cref="InvalidDataException">The managed secret did not contain a certificate.</exception>
/// <exception cref="NotSupportedException">The <see cref="CertificateContentType"/> is not supported.</exception>
/// <exception cref="PlatformNotSupportedException">Cannot create an <see cref="X509Certificate2"/> on this platform.</exception>
/// <exception cref="RequestFailedException">The request failed. See <see cref="RequestFailedException.ErrorCode"/> and the exception message for details.</exception>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual Response<X509Certificate2> DownloadCertificate(string certificateName, string version, CancellationToken cancellationToken) =>
DownloadCertificate(certificateName, version, null, cancellationToken);
/// <summary>
/// Creates an <see cref="X509Certificate2"/> from the specified certificate.
/// </summary>
/// <remarks>
/// Because <see cref="KeyVaultCertificate.Cer"/> contains only the public key, this method attempts to download the managed secret
/// that contains the full certificate. If you do not have permissions to get the secret,
/// <see cref="RequestFailedException"/> will be thrown with an appropriate error response.
/// If you want an <see cref="X509Certificate2"/> with only the public key, instantiate it passing only the
/// <see cref="KeyVaultCertificate.Cer"/> property.
/// This operation requires the certificates/get and secrets/get permissions.
/// </remarks>
/// <param name="certificateName">The name of the certificate to download.</param>
/// <param name="version">Optional version of a certificate to download.</param>
/// <param name="options">Additional options for downloading and creating an <see cref="X509Certificate2"/>.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>An <see cref="X509Certificate2"/> from the specified certificate.</returns>
/// <exception cref="ArgumentException"><paramref name="certificateName"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="certificateName"/> is null.</exception>
/// <exception cref="InvalidDataException">The managed secret did not contain a certificate.</exception>
/// <exception cref="NotSupportedException">The <see cref="CertificateContentType"/> is not supported.</exception>
/// <exception cref="PlatformNotSupportedException">Cannot create an <see cref="X509Certificate2"/> on this platform.</exception>
/// <exception cref="RequestFailedException">The request failed. See <see cref="RequestFailedException.ErrorCode"/> and the exception message for details.</exception>
public virtual Response<X509Certificate2> DownloadCertificate(string certificateName, string version = null, DownloadCertificateOptions options = null, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(certificateName, nameof(certificateName));
using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CertificateClient)}.{nameof(DownloadCertificate)}");
scope.AddAttribute("certificate", certificateName);
scope.AddAttribute("version", version);
scope.Start();
try
{
KeyVaultCertificateWithPolicy certificate = _pipeline.SendRequest(RequestMethod.Get, () => new KeyVaultCertificateWithPolicy(), cancellationToken, CertificatesPath, certificateName, "/", version);
Response<KeyVaultSecret> secretResponse = _pipeline.SendRequest(RequestMethod.Get, () => new KeyVaultSecret(), certificate.SecretId, cancellationToken);
KeyVaultSecret secret = secretResponse.Value;
string value = secret.Value;
if (string.IsNullOrEmpty(value))
{
throw new InvalidDataException($"Secret {certificate.SecretId} contains no value");
}
options ??= new();
if (secret.ContentType is null || secret.ContentType == CertificateContentType.Pkcs12)
{
byte[] rawData = Convert.FromBase64String(value);
X509Certificate2 x509 = new(rawData, (string)null, options.KeyStorageFlags);
return Response.FromValue(x509, secretResponse.GetRawResponse());
}
else if (secret.ContentType == CertificateContentType.Pem)
{
X509Certificate2 x509 = PemReader.LoadCertificate(value.AsSpan(), certificate.Cer, allowCertificateOnly: true, keyStorageFlags: options.KeyStorageFlags);
return Response.FromValue(x509, secretResponse.GetRawResponse());
}
throw new NotSupportedException($"Content type {secret.ContentType} not supported");
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
/// <summary>
/// Creates an <see cref="X509Certificate2"/> from the specified certificate.
/// </summary>
/// <remarks>
/// Because <see cref="KeyVaultCertificate.Cer"/> contains only the public key, this method attempts to download the managed secret
/// that contains the full certificate. If you do not have permissions to get the secret,
/// <see cref="RequestFailedException"/> will be thrown with an appropriate error response.
/// If you want an <see cref="X509Certificate2"/> with only the public key, instantiate it passing only the
/// <see cref="KeyVaultCertificate.Cer"/> property.
/// This operation requires the certificates/get and secrets/get permissions.
/// </remarks>
/// <param name="certificateName">The name of the certificate to download.</param>
/// <param name="version">Optional version of a certificate to download.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>An <see cref="X509Certificate2"/> from the specified certificate.</returns>
/// <exception cref="ArgumentException"><paramref name="certificateName"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="certificateName"/> is null.</exception>
/// <exception cref="InvalidDataException">The managed secret did not contain a certificate.</exception>
/// <exception cref="NotSupportedException">The <see cref="CertificateContentType"/> is not supported.</exception>
/// <exception cref="PlatformNotSupportedException">Cannot create an <see cref="X509Certificate2"/> on this platform.</exception>
/// <exception cref="RequestFailedException">The request failed. See <see cref="RequestFailedException.ErrorCode"/> and the exception message for details.</exception>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual Task<Response<X509Certificate2>> DownloadCertificateAsync(string certificateName, string version, CancellationToken cancellationToken) =>
DownloadCertificateAsync(certificateName, version, null, cancellationToken);
/// <summary>
/// Creates an <see cref="X509Certificate2"/> from the specified certificate.
/// </summary>
/// <remarks>
/// Because <see cref="KeyVaultCertificate.Cer"/> contains only the public key, this method attempts to download the managed secret
/// that contains the full certificate. If you do not have permissions to get the secret,
/// <see cref="RequestFailedException"/> will be thrown with an appropriate error response.
/// If you want an <see cref="X509Certificate2"/> with only the public key, instantiate it passing only the
/// <see cref="KeyVaultCertificate.Cer"/> property.
/// This operation requires the certificates/get and secrets/get permissions.
/// </remarks>
/// <param name="certificateName">The name of the certificate to download.</param>
/// <param name="version">Optional version of a certificate to download.</param>
/// <param name="options">Additional options for downloading and creating an <see cref="X509Certificate2"/>.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>An <see cref="X509Certificate2"/> from the specified certificate.</returns>
/// <exception cref="ArgumentException"><paramref name="certificateName"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="certificateName"/> is null.</exception>
/// <exception cref="InvalidDataException">The managed secret did not contain a certificate.</exception>
/// <exception cref="NotSupportedException">The <see cref="CertificateContentType"/> is not supported.</exception>
/// <exception cref="PlatformNotSupportedException">Cannot create an <see cref="X509Certificate2"/> on this platform.</exception>
/// <exception cref="RequestFailedException">The request failed. See <see cref="RequestFailedException.ErrorCode"/> and the exception message for details.</exception>
public virtual async Task<Response<X509Certificate2>> DownloadCertificateAsync(string certificateName, string version = null, DownloadCertificateOptions options = null, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(certificateName, nameof(certificateName));
using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CertificateClient)}.{nameof(DownloadCertificate)}");
scope.AddAttribute("certificate", certificateName);
scope.AddAttribute("version", version);
scope.Start();
try
{
KeyVaultCertificateWithPolicy certificate = await _pipeline.SendRequestAsync(RequestMethod.Get, () => new KeyVaultCertificateWithPolicy(), cancellationToken, CertificatesPath, certificateName, "/", version).ConfigureAwait(false);
Response<KeyVaultSecret> secretResponse = await _pipeline.SendRequestAsync(RequestMethod.Get, () => new KeyVaultSecret(), certificate.SecretId, cancellationToken).ConfigureAwait(false);
KeyVaultSecret secret = secretResponse.Value;
string value = secret.Value;
if (string.IsNullOrEmpty(value))
{
throw new InvalidDataException($"Secret {certificate.SecretId} contains no value");
}
options ??= new();
if (secret.ContentType is null || secret.ContentType == CertificateContentType.Pkcs12)
{
byte[] rawData = Convert.FromBase64String(value);
X509Certificate2 x509 = new(rawData, (string)null, options.KeyStorageFlags);
return Response.FromValue(x509, secretResponse.GetRawResponse());
}
else if (secret.ContentType == CertificateContentType.Pem)
{
X509Certificate2 x509 = PemReader.LoadCertificate(value.AsSpan(), certificate.Cer, allowCertificateOnly: true, keyStorageFlags: options.KeyStorageFlags);
return Response.FromValue(x509, secretResponse.GetRawResponse());
}
throw new NotSupportedException($"Content type {secret.ContentType} not supported");
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
#pragma warning restore AZC0015 // Unexpected client method return type.
#pragma warning restore AZC0002 // DO ensure all service methods, both asynchronous and synchronous, take an optional CancellationToken parameter called cancellationToken.
/// <summary>
/// Returns the latest version of the <see cref="KeyVaultCertificate"/> along with its <see cref="CertificatePolicy"/>. This operation requires the certificates/get permission.
/// </summary>
/// <param name="certificateName">The name of the <see cref="KeyVaultCertificate"/> to retrieve.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>A response containing the certificate and policy as a <see cref="KeyVaultCertificateWithPolicy"/> instance.</returns>
/// <exception cref="ArgumentException"><paramref name="certificateName"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="certificateName"/> is null.</exception>
public virtual Response<KeyVaultCertificateWithPolicy> GetCertificate(string certificateName, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(certificateName, nameof(certificateName));
using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CertificateClient)}.{nameof(GetCertificate)}");
scope.AddAttribute("certificate", certificateName);
scope.Start();
try
{
return _pipeline.SendRequest(RequestMethod.Get, () => new KeyVaultCertificateWithPolicy(), cancellationToken, CertificatesPath, certificateName);
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
/// <summary>
/// Returns the latest version of the <see cref="KeyVaultCertificate"/> along with its <see cref="CertificatePolicy"/>. This operation requires the certificates/get permission.
/// </summary>
/// <param name="certificateName">The name of the <see cref="KeyVaultCertificate"/> to retrieve.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>A response containing the certificate and policy as a <see cref="KeyVaultCertificateWithPolicy"/> instance.</returns>
/// <exception cref="ArgumentException"><paramref name="certificateName"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="certificateName"/> is null.</exception>
public virtual async Task<Response<KeyVaultCertificateWithPolicy>> GetCertificateAsync(string certificateName, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(certificateName, nameof(certificateName));
using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CertificateClient)}.{nameof(GetCertificate)}");
scope.AddAttribute("certificate", certificateName);
scope.Start();
try
{
return await _pipeline.SendRequestAsync(RequestMethod.Get, () => new KeyVaultCertificateWithPolicy(), cancellationToken, CertificatesPath, certificateName).ConfigureAwait(false);
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
/// <summary>
/// Gets a specific version of the <see cref="KeyVaultCertificate"/>. This operation requires the certificates/get permission.
/// </summary>
/// <param name="certificateName">The name of the <see cref="KeyVaultCertificate"/> to retrieve.</param>
/// <param name="version">The version of the <see cref="KeyVaultCertificate"/> to retrieve.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>The requested <see cref="KeyVaultCertificate"/>.</returns>
/// <exception cref="ArgumentException"><paramref name="certificateName"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="certificateName"/> is null.</exception>
public virtual Response<KeyVaultCertificate> GetCertificateVersion(string certificateName, string version, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(certificateName, nameof(certificateName));
using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CertificateClient)}.{nameof(GetCertificateVersion)}");
scope.AddAttribute("certificate", certificateName);
scope.AddAttribute("version", version);
scope.Start();
try
{
return _pipeline.SendRequest(RequestMethod.Get, () => new KeyVaultCertificate(), cancellationToken, CertificatesPath, certificateName, "/", version);
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
/// <summary>
/// Gets a specific version of the <see cref="KeyVaultCertificate"/>. This operation requires the certificates/get permission.
/// </summary>
/// <param name="certificateName">The name of the <see cref="KeyVaultCertificate"/> to retrieve.</param>
/// <param name="version">The version of the <see cref="KeyVaultCertificate"/> to retrieve.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>The requested <see cref="KeyVaultCertificate"/>.</returns>
/// <exception cref="ArgumentException"><paramref name="certificateName"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="certificateName"/> is null.</exception>
public virtual async Task<Response<KeyVaultCertificate>> GetCertificateVersionAsync(string certificateName, string version, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(certificateName, nameof(certificateName));
Argument.AssertNotNullOrEmpty(version, nameof(version));
using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CertificateClient)}.{nameof(GetCertificateVersion)}");
scope.AddAttribute("certificate", certificateName);
scope.AddAttribute("version", version);
scope.Start();
try
{
return await _pipeline.SendRequestAsync(RequestMethod.Get, () => new KeyVaultCertificate(), cancellationToken, CertificatesPath, certificateName, "/", version).ConfigureAwait(false);
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
/// <summary>
/// Updates the specified <see cref="KeyVaultCertificate"/> with the specified values for its mutable properties. This operation requires the certificates/update permission.
/// </summary>
/// <param name="properties">The <see cref="CertificateProperties"/> object with updated properties.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>The updated <see cref="KeyVaultCertificate"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="properties"/> is null.</exception>
public virtual Response<KeyVaultCertificate> UpdateCertificateProperties(CertificateProperties properties, CancellationToken cancellationToken = default)
{
Argument.AssertNotNull(properties, nameof(properties));
var parameters = new CertificateUpdateParameters(properties);
using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CertificateClient)}.{nameof(UpdateCertificateProperties)}");
scope.AddAttribute("certificate", properties.Name);
scope.AddAttribute("version", properties.Version);
scope.Start();
try
{
return _pipeline.SendRequest(RequestMethod.Patch, parameters, () => new KeyVaultCertificate(), cancellationToken, CertificatesPath, properties.Name, "/", properties.Version);
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
/// <summary>
/// Updates the specified <see cref="KeyVaultCertificate"/> with the specified values for its mutable properties. This operation requires the certificates/update permission.
/// </summary>
/// <param name="properties">The <see cref="CertificateProperties"/> object with updated properties.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>The updated <see cref="KeyVaultCertificate"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="properties"/> is null.</exception>
public virtual async Task<Response<KeyVaultCertificate>> UpdateCertificatePropertiesAsync(CertificateProperties properties, CancellationToken cancellationToken = default)
{
Argument.AssertNotNull(properties, nameof(properties));
var parameters = new CertificateUpdateParameters(properties);
using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CertificateClient)}.{nameof(UpdateCertificateProperties)}");
scope.AddAttribute("certificate", properties.Name);
scope.AddAttribute("version", properties.Version);
scope.Start();
try
{
return await _pipeline.SendRequestAsync(RequestMethod.Patch, parameters, () => new KeyVaultCertificate(), cancellationToken, CertificatesPath, properties.Name, "/", properties.Version).ConfigureAwait(false);
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
/// <summary>
/// Deletes all versions of the specified <see cref="KeyVaultCertificate"/>. If the vault is soft delete-enabled, the <see cref="KeyVaultCertificate"/> will be marked for permanent deletion
/// and can be recovered with <see cref="StartRecoverDeletedCertificate"/>, or purged with <see cref="PurgeDeletedCertificate"/>. This operation requires the certificates/delete permission.
/// </summary>
/// <param name="certificateName">The name of the <see cref="KeyVaultCertificate"/> to delete.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>
/// A <see cref="Certificates.DeleteCertificateOperation"/> to wait on this long-running operation.
/// If the Key Vault is soft delete-enabled, you only need to wait for the operation to complete if you need to recover or purge the certificate;
/// otherwise, the certificate is deleted automatically on the <see cref="DeletedCertificate.ScheduledPurgeDate"/>.
/// </returns>
/// <exception cref="ArgumentException"><paramref name="certificateName"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="certificateName"/> is null.</exception>
public virtual DeleteCertificateOperation StartDeleteCertificate(string certificateName, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(certificateName, nameof(certificateName));
using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CertificateClient)}.{nameof(StartDeleteCertificate)}");
scope.AddAttribute("certificate", certificateName);
scope.Start();
try
{
Response<DeletedCertificate> response = _pipeline.SendRequest(RequestMethod.Delete, () => new DeletedCertificate(), cancellationToken, CertificatesPath, certificateName);
return new DeleteCertificateOperation(_pipeline, response);
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
/// <summary>
/// Deletes all versions of the specified <see cref="KeyVaultCertificate"/>. If the vault is soft delete-enabled, the <see cref="KeyVaultCertificate"/> will be marked for permanent deletion
/// and can be recovered with <see cref="StartRecoverDeletedCertificate"/>, or purged with <see cref="PurgeDeletedCertificate"/>. This operation requires the certificates/delete permission.
/// </summary>
/// <param name="certificateName">The name of the <see cref="KeyVaultCertificate"/> to delete.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>
/// A <see cref="Certificates.DeleteCertificateOperation"/> to wait on this long-running operation.
/// If the Key Vault is soft delete-enabled, you only need to wait for the operation to complete if you need to recover or purge the certificate;
/// otherwise, the certificate is deleted automatically on the <see cref="DeletedCertificate.ScheduledPurgeDate"/>.
/// </returns>
/// <exception cref="ArgumentException"><paramref name="certificateName"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="certificateName"/> is null.</exception>
public virtual async Task<DeleteCertificateOperation> StartDeleteCertificateAsync(string certificateName, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(certificateName, nameof(certificateName));
using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CertificateClient)}.{nameof(StartDeleteCertificate)}");
scope.AddAttribute("certificate", certificateName);
scope.Start();
try
{
Response<DeletedCertificate> response = await _pipeline.SendRequestAsync(RequestMethod.Delete, () => new DeletedCertificate(), cancellationToken, CertificatesPath, certificateName).ConfigureAwait(false);
return new DeleteCertificateOperation(_pipeline, response);
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
/// <summary>
/// Retrieves information about the specified deleted <see cref="KeyVaultCertificate"/>. This operation is only applicable in vaults enabled for soft delete, and
/// requires the certificates/get permission.
/// </summary>
/// <param name="certificateName">The name of the <see cref="DeletedCertificate"/>.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>The details of the <see cref="DeletedCertificate"/>.</returns>
/// <exception cref="ArgumentException"><paramref name="certificateName"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="certificateName"/> is null.</exception>
public virtual Response<DeletedCertificate> GetDeletedCertificate(string certificateName, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(certificateName, nameof(certificateName));
using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CertificateClient)}.{nameof(GetDeletedCertificate)}");
scope.AddAttribute("certificate", certificateName);
scope.Start();
try
{
return _pipeline.SendRequest(RequestMethod.Get, () => new DeletedCertificate(), cancellationToken, DeletedCertificatesPath, certificateName);
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
/// <summary>
/// Retrieves information about the specified deleted <see cref="KeyVaultCertificate"/>. This operation is only applicable in vaults enabled for soft delete, and
/// requires the certificates/get permission.
/// </summary>
/// <param name="certificateName">The name of the <see cref="DeletedCertificate"/>.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>The details of the <see cref="DeletedCertificate"/>.</returns>
/// <exception cref="ArgumentException"><paramref name="certificateName"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="certificateName"/> is null.</exception>
public virtual async Task<Response<DeletedCertificate>> GetDeletedCertificateAsync(string certificateName, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(certificateName, nameof(certificateName));
using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CertificateClient)}.{nameof(GetDeletedCertificate)}");
scope.AddAttribute("certificate", certificateName);
scope.Start();
try
{
return await _pipeline.SendRequestAsync(RequestMethod.Get, () => new DeletedCertificate(), cancellationToken, DeletedCertificatesPath, certificateName).ConfigureAwait(false);
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
/// <summary>
/// Recovers the <see cref="DeletedCertificate"/> to its pre-deleted state. This operation is only applicable in vaults enabled for soft delete, and
/// requires the certificates/recover permission.
/// </summary>
/// <param name="certificateName">The name of the <see cref="DeletedCertificate"/>.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>A <see cref="RecoverDeletedCertificateOperation"/> to wait on this long-running operation.</returns>
/// <exception cref="ArgumentException"><paramref name="certificateName"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="certificateName"/> is null.</exception>
public virtual RecoverDeletedCertificateOperation StartRecoverDeletedCertificate(string certificateName, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(certificateName, nameof(certificateName));
using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CertificateClient)}.{nameof(StartRecoverDeletedCertificate)}");
scope.AddAttribute("certificate", certificateName);
scope.Start();
try
{
Response<KeyVaultCertificateWithPolicy> response = _pipeline.SendRequest(RequestMethod.Post, () => new KeyVaultCertificateWithPolicy(), cancellationToken, DeletedCertificatesPath, certificateName, "/recover");
return new RecoverDeletedCertificateOperation(_pipeline, response);
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
/// <summary>
/// Recovers the <see cref="DeletedCertificate"/> to its pre-deleted state. This operation is only applicable in vaults enabled for soft delete, and
/// requires the certificates/recover permission.
/// </summary>
/// <param name="certificateName">The name of the <see cref="DeletedCertificate"/>.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>A <see cref="RecoverDeletedCertificateOperation"/> to wait on this long-running operation.</returns>
/// <exception cref="ArgumentException"><paramref name="certificateName"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="certificateName"/> is null.</exception>
public virtual async Task<RecoverDeletedCertificateOperation> StartRecoverDeletedCertificateAsync(string certificateName, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(certificateName, nameof(certificateName));
using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CertificateClient)}.{nameof(StartRecoverDeletedCertificate)}");
scope.AddAttribute("certificate", certificateName);
scope.Start();
try
{
Response<KeyVaultCertificateWithPolicy> response = await _pipeline.SendRequestAsync(RequestMethod.Post, () => new KeyVaultCertificateWithPolicy(), cancellationToken, DeletedCertificatesPath, certificateName, "/recover").ConfigureAwait(false);
return new RecoverDeletedCertificateOperation(_pipeline, response);
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
/// <summary>
/// Permanently and irreversibly deletes the specified deleted certificate, without the possibility of recovery. This operation is only applicable in vaults enabled for soft delete, and
/// requires the certificates/purge permission. The operation is not available if the DeletedCertificate.RecoveryLevel of the DeletedCertificate does not specify 'Purgeable'.
/// </summary>
/// <param name="certificateName">The name of the <see cref="DeletedCertificate"/> to permanently delete.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>The HTTP response from the service.</returns>
/// <exception cref="ArgumentException"><paramref name="certificateName"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="certificateName"/> is null.</exception>
public virtual Response PurgeDeletedCertificate(string certificateName, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(certificateName, nameof(certificateName));
using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CertificateClient)}.{nameof(PurgeDeletedCertificate)}");
scope.AddAttribute("certificate", certificateName);
scope.Start();
try
{
return _pipeline.SendRequest(RequestMethod.Delete, cancellationToken, DeletedCertificatesPath, certificateName);
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
/// <summary>
/// Permanently and irreversibly deletes the specified deleted certificate, without the possibility of recovery. This operation is only applicable in vaults enabled for soft delete, and
/// requires the certificates/purge permission. The operation is not available if the DeletedCertificate.RecoveryLevel of the DeletedCertificate does not specify 'Purgeable'.
/// </summary>
/// <param name="certificateName">The name of the <see cref="DeletedCertificate"/> to permanently delete.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>The HTTP response from the service.</returns>
/// <exception cref="ArgumentException"><paramref name="certificateName"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="certificateName"/> is null.</exception>
public virtual async Task<Response> PurgeDeletedCertificateAsync(string certificateName, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(certificateName, nameof(certificateName));
using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CertificateClient)}.{nameof(PurgeDeletedCertificate)}");
scope.AddAttribute("certificate", certificateName);
scope.Start();
try
{
return await _pipeline.SendRequestAsync(RequestMethod.Delete, cancellationToken, DeletedCertificatesPath, certificateName).ConfigureAwait(false);
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
/// <summary>
/// Creates a backup of the certificate, including all versions, which can be used to restore the certificate to the state at the time of the backup in the case the certificate is deleted, or to
/// restore the certificate to a different vault in the same region as the original value. This operation requires the certificate/backup permission.
/// </summary>
/// <param name="certificateName">The name of the <see cref="KeyVaultCertificate"/> to backup.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>The certificate backup.</returns>
/// <exception cref="ArgumentException"><paramref name="certificateName"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="certificateName"/> is null.</exception>
public virtual Response<byte[]> BackupCertificate(string certificateName, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(certificateName, nameof(certificateName));
using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CertificateClient)}.{nameof(BackupCertificate)}");
scope.AddAttribute("certificate", certificateName);
scope.Start();
try
{
Response<CertificateBackup> backup = _pipeline.SendRequest(RequestMethod.Post, () => new CertificateBackup(), cancellationToken, CertificatesPath, certificateName, "/backup");
return Response.FromValue(backup.Value.Value, backup.GetRawResponse());
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
/// <summary>
/// Creates a backup of the certificate, including all versions, which can be used to restore the certificate to the state at the time of the backup in the case the certificate is deleted, or to
/// restore the certificate to a different vault in the same region as the original value. This operation requires the certificate/backup permission.
/// </summary>
/// <param name="certificateName">The name of the <see cref="KeyVaultCertificate"/> to backup.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>The certificate backup.</returns>
/// <exception cref="ArgumentException"><paramref name="certificateName"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="certificateName"/> is null.</exception>
public virtual async Task<Response<byte[]>> BackupCertificateAsync(string certificateName, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(certificateName, nameof(certificateName));
using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CertificateClient)}.{nameof(BackupCertificate)}");
scope.AddAttribute("certificate", certificateName);
scope.Start();
try
{
Response<CertificateBackup> backup = await _pipeline.SendRequestAsync(RequestMethod.Post, () => new CertificateBackup(), cancellationToken, CertificatesPath, certificateName, "/backup").ConfigureAwait(false);
return Response.FromValue(backup.Value.Value, backup.GetRawResponse());
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
/// <summary>
/// Restores a <see cref="KeyVaultCertificate"/>, including all versions, from a backup created from the <see cref="BackupCertificate"/> or <see cref="BackupCertificateAsync"/>. The backup must be restored
/// to a vault in the same region as its original vault. This operation requires the certificate/restore permission.
/// </summary>
/// <param name="backup">The backup of the <see cref="KeyVaultCertificate"/> to restore.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>The restored certificate and policy.</returns>
/// <exception cref="ArgumentNullException"><paramref name="backup"/> is null.</exception>
public virtual Response<KeyVaultCertificateWithPolicy> RestoreCertificateBackup(byte[] backup, CancellationToken cancellationToken = default)
{
Argument.AssertNotNull(backup, nameof(backup));
using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CertificateClient)}.{nameof(RestoreCertificateBackup)}");
scope.Start();
try
{
return _pipeline.SendRequest(RequestMethod.Post, new CertificateBackup { Value = backup }, () => new KeyVaultCertificateWithPolicy(), cancellationToken, CertificatesPath, "/restore");
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
/// <summary>
/// Restores a <see cref="KeyVaultCertificate"/>, including all versions, from a backup created from the <see cref="BackupCertificate"/> or <see cref="BackupCertificateAsync"/>. The backup must be restored
/// to a vault in the same region as its original vault. This operation requires the certificate/restore permission.
/// </summary>
/// <param name="backup">The backup of the <see cref="KeyVaultCertificate"/> to restore.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>The restored certificate and policy.</returns>
/// <exception cref="ArgumentNullException"><paramref name="backup"/> is null.</exception>
public virtual async Task<Response<KeyVaultCertificateWithPolicy>> RestoreCertificateBackupAsync(byte[] backup, CancellationToken cancellationToken = default)
{
Argument.AssertNotNull(backup, nameof(backup));
using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CertificateClient)}.{nameof(RestoreCertificateBackup)}");
scope.Start();
try
{
return await _pipeline.SendRequestAsync(RequestMethod.Post, new CertificateBackup { Value = backup }, () => new KeyVaultCertificateWithPolicy(), cancellationToken, CertificatesPath, "/restore").ConfigureAwait(false);
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
/// <summary>
/// Imports a pre-existing certificate to the key vault. The specified certificate must be in PFX or ASCII PEM-format, and must contain the private key as well as the X.509 certificates. This operation requires the
/// certificates/import permission.
/// </summary>
/// <param name="importCertificateOptions">The details of the certificate to import to the key vault.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>The imported certificate and policy.</returns>
/// <exception cref="ArgumentException"><see cref="ImportCertificateOptions.Name"/> of <paramref name="importCertificateOptions"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="importCertificateOptions"/> or <see cref="ImportCertificateOptions.Name"/> of <paramref name="importCertificateOptions"/> is null.</exception>
public virtual Response<KeyVaultCertificateWithPolicy> ImportCertificate(ImportCertificateOptions importCertificateOptions, CancellationToken cancellationToken = default)
{
Argument.AssertNotNull(importCertificateOptions, nameof(importCertificateOptions));
Argument.AssertNotNullOrEmpty(importCertificateOptions.Name, nameof(importCertificateOptions.Name));
using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CertificateClient)}.{nameof(ImportCertificate)}");
scope.AddAttribute("certificate", importCertificateOptions.Name);
scope.Start();
try
{
return _pipeline.SendRequest(RequestMethod.Post, importCertificateOptions, () => new KeyVaultCertificateWithPolicy(), cancellationToken, CertificatesPath, "/", importCertificateOptions.Name, "/import");
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
/// <summary>
/// Imports a pre-existing certificate to the key vault. The specified certificate must be in PFX or ASCII PEM-format, and must contain the private key as well as the X.509 certificates. This operation requires the
/// certificates/import permission.
/// </summary>
/// <param name="importCertificateOptions">The details of the certificate to import to the key vault.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>The imported certificate and policy.</returns>
/// <exception cref="ArgumentException"><see cref="ImportCertificateOptions.Name"/> of <paramref name="importCertificateOptions"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="importCertificateOptions"/> or <see cref="ImportCertificateOptions.Name"/> of <paramref name="importCertificateOptions"/> is null.</exception>
public virtual async Task<Response<KeyVaultCertificateWithPolicy>> ImportCertificateAsync(ImportCertificateOptions importCertificateOptions, CancellationToken cancellationToken = default)
{
Argument.AssertNotNull(importCertificateOptions, nameof(importCertificateOptions));
Argument.AssertNotNullOrEmpty(importCertificateOptions.Name, nameof(importCertificateOptions.Name));
using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CertificateClient)}.{nameof(ImportCertificate)}");
scope.AddAttribute("certificate", importCertificateOptions.Name);
scope.Start();
try
{
return await _pipeline.SendRequestAsync(RequestMethod.Post, importCertificateOptions, () => new KeyVaultCertificateWithPolicy(), cancellationToken, CertificatesPath, "/", importCertificateOptions.Name, "/import").ConfigureAwait(false);
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
/// <summary>
/// Lists the properties of all enabled and disabled certificates in the specified vault. You can use the returned <see cref="CertificateProperties.Name"/> in subsequent calls to <see cref="GetCertificate(string, CancellationToken)"/>.
/// This operation requires the certificates/list permission.
/// </summary>
/// <param name="includePending">Specifies whether to include certificates in a pending state as well.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>An enumerable collection of certificate metadata.</returns>
public virtual Pageable<CertificateProperties> GetPropertiesOfCertificates(bool includePending = default, CancellationToken cancellationToken = default)
{
Uri firstPageUri = _pipeline.CreateFirstPageUri(CertificatesPath, ("includePending", includePending.ToString(CultureInfo.InvariantCulture).ToLowerInvariant()));
return PageResponseEnumerator.CreateEnumerable(nextLink => _pipeline.GetPage(firstPageUri, nextLink, () => new CertificateProperties(), "Azure.Security.KeyVault.Keys.KeyClient.GetPropertiesOfCertificates", cancellationToken));
}
/// <summary>
/// Lists the properties of all enabled and disabled certificates in the specified vault. You can use the returned <see cref="CertificateProperties.Name"/> in subsequent calls to <see cref="GetCertificate(string, CancellationToken)"/>.
/// This operation requires the certificates/list permission.
/// </summary>
/// <param name="includePending">Specifies whether to include certificates in a pending state as well.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>An enumerable collection of certificate metadata.</returns>
public virtual AsyncPageable<CertificateProperties> GetPropertiesOfCertificatesAsync(bool includePending = default, CancellationToken cancellationToken = default)
{
Uri firstPageUri = _pipeline.CreateFirstPageUri(CertificatesPath, ("includePending", includePending.ToString(CultureInfo.InvariantCulture).ToLowerInvariant()));
return PageResponseEnumerator.CreateAsyncEnumerable(nextLink => _pipeline.GetPageAsync(firstPageUri, nextLink, () => new CertificateProperties(), "Azure.Security.KeyVaultCertificates.CertificateClient.GetPropertiesOfCertificates", cancellationToken));
}
/// <summary>
/// Lists the properties of all enabled and disabled versions of the specified certificate in the specified vault. You can use the returned <see cref="CertificateProperties.Name"/> in subsequent calls to <see cref="GetCertificateVersion(string, string, CancellationToken)"/>.
/// This operation requires the certificates/list permission.
/// </summary>
/// <param name="certificateName">The name of the certificate whose versions should be retrieved.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>An enumerable collection of the certificate's versions.</returns>
/// <exception cref="ArgumentException"><paramref name="certificateName"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="certificateName"/> is null.</exception>
public virtual Pageable<CertificateProperties> GetPropertiesOfCertificateVersions(string certificateName, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(certificateName, nameof(certificateName));
Uri firstPageUri = _pipeline.CreateFirstPageUri($"{CertificatesPath}{certificateName}/versions");
return PageResponseEnumerator.CreateEnumerable(nextLink => _pipeline.GetPage(firstPageUri, nextLink, () => new CertificateProperties(), "Azure.Security.KeyVaultCertificates.CertificateClient.GetPropertiesOfCertificateVersions", cancellationToken));
}
/// <summary>
/// Lists the properties of all enabled and disabled versions of the specified certificate in the specified vault. You can use the returned <see cref="CertificateProperties.Name"/> in subsequent calls to <see cref="GetCertificateVersion(string, string, CancellationToken)"/>.
/// This operation requires the certificates/list permission.
/// </summary>
/// <param name="certificateName">The name of the certificate whose versions should be retrieved.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>An enumerable collection of the certificate's versions.</returns>
/// <exception cref="ArgumentException"><paramref name="certificateName"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="certificateName"/> is null.</exception>
public virtual AsyncPageable<CertificateProperties> GetPropertiesOfCertificateVersionsAsync(string certificateName, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(certificateName, nameof(certificateName));
Uri firstPageUri = _pipeline.CreateFirstPageUri($"{CertificatesPath}{certificateName}/versions");
return PageResponseEnumerator.CreateAsyncEnumerable(nextLink => _pipeline.GetPageAsync(firstPageUri, nextLink, () => new CertificateProperties(), "Azure.Security.KeyVaultCertificates.CertificateClient.GetPropertiesOfCertificateVersions", cancellationToken));
}
/// <summary>
/// Enumerates the deleted certificates in the vault. This operation is only available on soft delete-enabled vaults, and requires the certificates/list/get permissions.
/// </summary>
/// <param name="includePending">Specifies whether to include certificates in a delete pending state as well.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>An enumerable collection of deleted certificates.</returns>
public virtual Pageable<DeletedCertificate> GetDeletedCertificates(bool includePending = default, CancellationToken cancellationToken = default)
{
Uri firstPageUri = _pipeline.CreateFirstPageUri(DeletedCertificatesPath, ("includePending", includePending.ToString(CultureInfo.InvariantCulture).ToLowerInvariant()));
return PageResponseEnumerator.CreateEnumerable(nextLink => _pipeline.GetPage(firstPageUri, nextLink, () => new DeletedCertificate(), "Azure.Security.KeyVaultCertificates.CertificateClient.GetDeletedCertificates", cancellationToken));
}
/// <summary>