| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Threading; |
| | 6 | | using System.Threading.Tasks; |
| | 7 | | using Azure.Core; |
| | 8 | | using Azure.Core.Pipeline; |
| | 9 | |
|
| | 10 | | namespace Azure.Security.KeyVault.Certificates |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// A long-running operation for <see cref="CertificateClient.StartRecoverDeletedCertificate(string, CancellationTok |
| | 14 | | /// </summary> |
| | 15 | | public class RecoverDeletedCertificateOperation : Operation<KeyVaultCertificateWithPolicy> |
| | 16 | | { |
| 0 | 17 | | private static readonly TimeSpan s_defaultPollingInterval = TimeSpan.FromSeconds(2); |
| | 18 | |
|
| | 19 | | private readonly KeyVaultPipeline _pipeline; |
| | 20 | | private readonly KeyVaultCertificateWithPolicy _value; |
| | 21 | | private Response _response; |
| | 22 | | private bool _completed; |
| | 23 | |
|
| 4 | 24 | | internal RecoverDeletedCertificateOperation(KeyVaultPipeline pipeline, Response<KeyVaultCertificateWithPolicy> r |
| | 25 | | { |
| 4 | 26 | | _pipeline = pipeline; |
| 4 | 27 | | _value = response.Value ?? throw new InvalidOperationException("The response does not contain a value."); |
| 4 | 28 | | _response = response.GetRawResponse(); |
| 4 | 29 | | } |
| | 30 | |
|
| | 31 | | /// <inheritdoc/> |
| 0 | 32 | | public override string Id => _value.Id.ToString(); |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// Gets the <see cref="KeyVaultCertificate"/> of the certificate being recovered. |
| | 36 | | /// You should await <see cref="WaitForCompletionAsync(CancellationToken)"/> before attempting to use a certific |
| | 37 | | /// </summary> |
| | 38 | | /// <remarks> |
| | 39 | | /// Azure Key Vault will return a <see cref="KeyVaultCertificate"/> immediately but may take time to actually re |
| | 40 | | /// </remarks> |
| 0 | 41 | | public override KeyVaultCertificateWithPolicy Value => _value; |
| | 42 | |
|
| | 43 | | /// <inheritdoc/> |
| 0 | 44 | | public override bool HasCompleted => _completed; |
| | 45 | |
|
| | 46 | | /// <inheritdoc/> |
| 0 | 47 | | public override bool HasValue => true; |
| | 48 | |
|
| | 49 | | /// <inheritdoc/> |
| 0 | 50 | | public override Response GetRawResponse() => _response; |
| | 51 | |
|
| | 52 | | /// <inheritdoc/> |
| | 53 | | public override Response UpdateStatus(CancellationToken cancellationToken = default) |
| | 54 | | { |
| 0 | 55 | | if (!_completed) |
| | 56 | | { |
| 0 | 57 | | using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(RecoverDeletedCertificateOperation)}.{name |
| 0 | 58 | | scope.AddAttribute("secret", _value.Name); |
| 0 | 59 | | scope.Start(); |
| | 60 | |
|
| | 61 | | try |
| | 62 | | { |
| 0 | 63 | | _response = _pipeline.GetResponse(RequestMethod.Get, cancellationToken, CertificateClient.Certificat |
| 0 | 64 | | _completed = CheckCompleted(_response); |
| 0 | 65 | | } |
| 0 | 66 | | catch (Exception e) |
| | 67 | | { |
| 0 | 68 | | scope.Failed(e); |
| 0 | 69 | | throw; |
| | 70 | | } |
| | 71 | | } |
| | 72 | |
|
| 0 | 73 | | return GetRawResponse(); |
| | 74 | | } |
| | 75 | |
|
| | 76 | | /// <inheritdoc/> |
| | 77 | | public override async ValueTask<Response> UpdateStatusAsync(CancellationToken cancellationToken = default) |
| | 78 | | { |
| 0 | 79 | | if (!_completed) |
| | 80 | | { |
| 0 | 81 | | using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(RecoverDeletedCertificateOperation)}.{name |
| 0 | 82 | | scope.AddAttribute("secret", _value.Name); |
| 0 | 83 | | scope.Start(); |
| | 84 | |
|
| | 85 | | try |
| | 86 | | { |
| 0 | 87 | | _response = await _pipeline.GetResponseAsync(RequestMethod.Get, cancellationToken, CertificateClient |
| 0 | 88 | | _completed = await CheckCompletedAsync(_response).ConfigureAwait(false); |
| 0 | 89 | | } |
| 0 | 90 | | catch (Exception e) |
| | 91 | | { |
| 0 | 92 | | scope.Failed(e); |
| 0 | 93 | | throw; |
| | 94 | | } |
| 0 | 95 | | } |
| | 96 | |
|
| 0 | 97 | | return GetRawResponse(); |
| 0 | 98 | | } |
| | 99 | |
|
| | 100 | | /// <inheritdoc /> |
| | 101 | | public override ValueTask<Response<KeyVaultCertificateWithPolicy>> WaitForCompletionAsync(CancellationToken canc |
| 0 | 102 | | this.DefaultWaitForCompletionAsync(s_defaultPollingInterval, cancellationToken); |
| | 103 | |
|
| | 104 | | /// <inheritdoc /> |
| | 105 | | public override ValueTask<Response<KeyVaultCertificateWithPolicy>> WaitForCompletionAsync(TimeSpan pollingInterv |
| 0 | 106 | | this.DefaultWaitForCompletionAsync(pollingInterval, cancellationToken); |
| | 107 | |
|
| | 108 | | private async ValueTask<bool> CheckCompletedAsync(Response response) |
| | 109 | | { |
| 0 | 110 | | switch (response.Status) |
| | 111 | | { |
| | 112 | | case 200: |
| | 113 | | case 403: // Access denied but proof the certificate was recovered. |
| 0 | 114 | | return true; |
| | 115 | |
|
| | 116 | | case 404: |
| 0 | 117 | | return false; |
| | 118 | |
|
| | 119 | | default: |
| 0 | 120 | | throw await _pipeline.Diagnostics.CreateRequestFailedExceptionAsync(response).ConfigureAwait(false); |
| | 121 | | } |
| 0 | 122 | | } |
| | 123 | | private bool CheckCompleted(Response response) |
| | 124 | | { |
| 0 | 125 | | switch (response.Status) |
| | 126 | | { |
| | 127 | | case 200: |
| | 128 | | case 403: // Access denied but proof the certificate was recovered. |
| 0 | 129 | | return true; |
| | 130 | |
|
| | 131 | | case 404: |
| 0 | 132 | | return false; |
| | 133 | |
|
| | 134 | | default: |
| 0 | 135 | | throw _pipeline.Diagnostics.CreateRequestFailedException(response); |
| | 136 | | } |
| | 137 | | } |
| | 138 | | } |
| | 139 | | } |