| | 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.Keys |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// A long-running operation for <see cref="KeyClient.StartDeleteKey(string, CancellationToken)"/> or <see cref="Key |
| | 14 | | /// </summary> |
| | 15 | | public class DeleteKeyOperation : Operation<DeletedKey> |
| | 16 | | { |
| 2 | 17 | | private static readonly TimeSpan s_defaultPollingInterval = TimeSpan.FromSeconds(2); |
| | 18 | |
|
| | 19 | | private readonly KeyVaultPipeline _pipeline; |
| | 20 | | private readonly DeletedKey _value; |
| | 21 | | private Response _response; |
| | 22 | | private bool _completed; |
| | 23 | |
|
| 44 | 24 | | internal DeleteKeyOperation(KeyVaultPipeline pipeline, Response<DeletedKey> response) |
| | 25 | | { |
| 44 | 26 | | _pipeline = pipeline; |
| 44 | 27 | | _value = response.Value ?? throw new InvalidOperationException("The response does not contain a value."); |
| 44 | 28 | | _response = response.GetRawResponse(); |
| | 29 | |
|
| | 30 | | // The recoveryId is only returned if soft-delete is enabled. |
| 44 | 31 | | if (_value.RecoveryId is null) |
| | 32 | | { |
| 0 | 33 | | _completed = true; |
| | 34 | | } |
| 44 | 35 | | } |
| | 36 | |
|
| | 37 | | /// <inheritdoc/> |
| 0 | 38 | | public override string Id => _value.Id.ToString(); |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Gets the <see cref="DeletedKey"/>. |
| | 42 | | /// You should await <see cref="WaitForCompletionAsync(CancellationToken)"/> before attempting to purge or recov |
| | 43 | | /// </summary> |
| | 44 | | /// <remarks> |
| | 45 | | /// Azure Key Vault will return a <see cref="DeletedKey"/> immediately but may take time to actually delete the |
| | 46 | | /// </remarks> |
| 36 | 47 | | public override DeletedKey Value => _value; |
| | 48 | |
|
| | 49 | | /// <inheritdoc/> |
| 126 | 50 | | public override bool HasCompleted => _completed; |
| | 51 | |
|
| | 52 | | /// <inheritdoc/> |
| 0 | 53 | | public override bool HasValue => true; |
| | 54 | |
|
| | 55 | | /// <inheritdoc/> |
| 150 | 56 | | public override Response GetRawResponse() => _response; |
| | 57 | |
|
| | 58 | | /// <inheritdoc/> |
| | 59 | | public override Response UpdateStatus(CancellationToken cancellationToken = default) |
| | 60 | | { |
| 0 | 61 | | if (!_completed) |
| | 62 | | { |
| 0 | 63 | | using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(DeleteKeyOperation)}.{nameof(UpdateStatus) |
| 0 | 64 | | scope.AddAttribute("secret", _value.Name); |
| 0 | 65 | | scope.Start(); |
| | 66 | |
|
| | 67 | | try |
| | 68 | | { |
| 0 | 69 | | _response = _pipeline.GetResponse(RequestMethod.Get, cancellationToken, KeyClient.DeletedKeysPath, _ |
| 0 | 70 | | _completed = CheckCompleted(_response); |
| 0 | 71 | | } |
| 0 | 72 | | catch (Exception e) |
| | 73 | | { |
| 0 | 74 | | scope.Failed(e); |
| 0 | 75 | | throw; |
| | 76 | | } |
| | 77 | | } |
| | 78 | |
|
| 0 | 79 | | return GetRawResponse(); |
| | 80 | | } |
| | 81 | |
|
| | 82 | | /// <inheritdoc/> |
| | 83 | | public override async ValueTask<Response> UpdateStatusAsync(CancellationToken cancellationToken = default) |
| | 84 | | { |
| 126 | 85 | | if (!_completed) |
| | 86 | | { |
| 126 | 87 | | using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(DeleteKeyOperation)}.{nameof(UpdateStatus) |
| 126 | 88 | | scope.AddAttribute("secret", _value.Name); |
| 126 | 89 | | scope.Start(); |
| | 90 | |
|
| | 91 | | try |
| | 92 | | { |
| 126 | 93 | | _response = await _pipeline.GetResponseAsync(RequestMethod.Get, cancellationToken, KeyClient.Deleted |
| 126 | 94 | | _completed = await CheckCompletedAsync(_response).ConfigureAwait(false); |
| 126 | 95 | | } |
| 0 | 96 | | catch (Exception e) |
| | 97 | | { |
| 0 | 98 | | scope.Failed(e); |
| 0 | 99 | | throw; |
| | 100 | | } |
| 126 | 101 | | } |
| | 102 | |
|
| 126 | 103 | | return GetRawResponse(); |
| 126 | 104 | | } |
| | 105 | |
|
| | 106 | | /// <inheritdoc /> |
| | 107 | | public override ValueTask<Response<DeletedKey>> WaitForCompletionAsync(CancellationToken cancellationToken = def |
| 24 | 108 | | this.DefaultWaitForCompletionAsync(s_defaultPollingInterval, cancellationToken); |
| | 109 | |
|
| | 110 | | /// <inheritdoc /> |
| | 111 | | public override ValueTask<Response<DeletedKey>> WaitForCompletionAsync(TimeSpan pollingInterval, CancellationTok |
| 0 | 112 | | this.DefaultWaitForCompletionAsync(pollingInterval, cancellationToken); |
| | 113 | |
|
| | 114 | | private async ValueTask<bool> CheckCompletedAsync(Response response) |
| | 115 | | { |
| 126 | 116 | | switch (response.Status) |
| | 117 | | { |
| | 118 | | case 200: |
| | 119 | | case 403: // Access denied but proof the key was deleted. |
| 24 | 120 | | return true; |
| | 121 | |
|
| | 122 | | case 404: |
| 102 | 123 | | return false; |
| | 124 | |
|
| | 125 | | default: |
| 0 | 126 | | throw await _pipeline.Diagnostics.CreateRequestFailedExceptionAsync(response).ConfigureAwait(false); |
| | 127 | | } |
| 126 | 128 | | } |
| | 129 | | private bool CheckCompleted(Response response) |
| | 130 | | { |
| 0 | 131 | | switch (response.Status) |
| | 132 | | { |
| | 133 | | case 200: |
| | 134 | | case 403: // Access denied but proof the key was deleted. |
| 0 | 135 | | return true; |
| | 136 | |
|
| | 137 | | case 404: |
| 0 | 138 | | return false; |
| | 139 | |
|
| | 140 | | default: |
| 0 | 141 | | throw _pipeline.Diagnostics.CreateRequestFailedException(response); |
| | 142 | | } |
| | 143 | | } |
| | 144 | | } |
| | 145 | | } |