| | | 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 | | using Azure.Security.KeyVault.Administration.Models; |
| | | 10 | | |
| | | 11 | | namespace Azure.Security.KeyVault.Administration |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// The KeyVaultBackupClient provides synchronous and asynchronous methods to perform full backup and restore of the |
| | | 15 | | /// </summary> |
| | | 16 | | public class KeyVaultBackupClient |
| | | 17 | | { |
| | | 18 | | private readonly ClientDiagnostics _diagnostics; |
| | | 19 | | private readonly BackupRestoreRestClient _restClient; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// The vault Uri. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <value></value> |
| | 30 | 25 | | public virtual Uri VaultUri { get; } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Initializes a new instance of the <see cref="KeyVaultBackupClient"/> class for mocking. |
| | | 29 | | /// </summary> |
| | 8 | 30 | | protected KeyVaultBackupClient() |
| | 8 | 31 | | { } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Initializes a new instance of the <see cref="KeyVaultBackupClient"/> class for the specified vault. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="vaultUri">A <see cref="Uri"/> to the vault on which the client operates. Appears as "DNS Name" |
| | | 37 | | /// <param name="credential">A <see cref="TokenCredential"/> used to authenticate requests to the vault, such as |
| | | 38 | | /// <exception cref="ArgumentNullException"><paramref name="vaultUri"/> or <paramref name="credential"/> is null |
| | | 39 | | public KeyVaultBackupClient(Uri vaultUri, TokenCredential credential) |
| | 0 | 40 | | : this(vaultUri, credential, null) |
| | 0 | 41 | | { } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Initializes a new instance of the <see cref="KeyVaultBackupClient"/> class for the specified vault. |
| | | 45 | | /// </summary> |
| | | 46 | | /// <param name="vaultUri">A <see cref="Uri"/> to the vault on which the client operates. Appears as "DNS Name" |
| | | 47 | | /// <param name="credential">A <see cref="TokenCredential"/> used to authenticate requests to the vault, such as |
| | | 48 | | /// <param name="options"><see cref="KeyVaultBackupClientOptions"/> that allow to configure the management of th |
| | | 49 | | /// <exception cref="ArgumentNullException"><paramref name="vaultUri"/> or <paramref name="credential"/> is null |
| | 8 | 50 | | public KeyVaultBackupClient(Uri vaultUri, TokenCredential credential, KeyVaultBackupClientOptions options) |
| | | 51 | | { |
| | 8 | 52 | | Argument.AssertNotNull(vaultUri, nameof(vaultUri)); |
| | 8 | 53 | | Argument.AssertNotNull(credential, nameof(credential)); |
| | | 54 | | |
| | 8 | 55 | | VaultUri = vaultUri; |
| | | 56 | | |
| | 8 | 57 | | options ??= new KeyVaultBackupClientOptions(); |
| | 8 | 58 | | string apiVersion = options.GetVersionString(); |
| | | 59 | | |
| | 8 | 60 | | HttpPipeline pipeline = HttpPipelineBuilder.Build(options, |
| | 8 | 61 | | new ChallengeBasedAuthenticationPolicy(credential)); |
| | | 62 | | |
| | 8 | 63 | | _diagnostics = new ClientDiagnostics(options); |
| | 8 | 64 | | _restClient = new BackupRestoreRestClient(_diagnostics, pipeline, apiVersion); |
| | 8 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Initiates a full backup of the Key Vault. |
| | | 69 | | /// </summary> |
| | | 70 | | /// <param name="blobStorageUri">The <see cref="Uri"/> for the blob storage resource.</param> |
| | | 71 | | /// <param name="sasToken">A Shared Access Signature (SAS) token to authorize access to the blob.</param> |
| | | 72 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 73 | | /// <exception cref="ArgumentNullException"><paramref name="blobStorageUri"/> or <paramref name="sasToken"/> is |
| | | 74 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | | 75 | | /// <returns>A <see cref="BackupOperation"/> to wait on this long-running operation.</returns> |
| | | 76 | | public virtual async Task<BackupOperation> StartBackupAsync(Uri blobStorageUri, string sasToken, CancellationTok |
| | | 77 | | { |
| | 2 | 78 | | using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultBackupClient)}.{nameof(StartBackup) |
| | 2 | 79 | | scope.Start(); |
| | | 80 | | try |
| | | 81 | | { |
| | 2 | 82 | | var response = await _restClient.FullBackupAsync( |
| | 2 | 83 | | VaultUri.AbsoluteUri, |
| | 2 | 84 | | new SASTokenParameter(blobStorageUri.AbsoluteUri, sasToken), |
| | 2 | 85 | | cancellationToken) |
| | 2 | 86 | | .ConfigureAwait(false); |
| | | 87 | | |
| | 2 | 88 | | return new BackupOperation(this, response); |
| | | 89 | | } |
| | 0 | 90 | | catch (Exception ex) |
| | | 91 | | { |
| | 0 | 92 | | scope.Failed(ex); |
| | 0 | 93 | | throw; |
| | | 94 | | } |
| | 2 | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Initiates a full backup of the Key Vault. |
| | | 99 | | /// </summary> |
| | | 100 | | /// <param name="blobStorageUri">The <see cref="Uri"/> for the blob storage resource.</param> |
| | | 101 | | /// <param name="sasToken">A Shared Access Signature (SAS) token to authorize access to the blob.</param> |
| | | 102 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 103 | | /// <exception cref="ArgumentNullException"><paramref name="blobStorageUri"/> or <paramref name="sasToken"/> is |
| | | 104 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | | 105 | | /// <returns>A <see cref="BackupOperation"/> to wait on this long-running operation.</returns> |
| | | 106 | | public virtual BackupOperation StartBackup(Uri blobStorageUri, string sasToken, CancellationToken cancellationTo |
| | | 107 | | { |
| | 2 | 108 | | using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultBackupClient)}.{nameof(StartBackup) |
| | 2 | 109 | | scope.Start(); |
| | | 110 | | try |
| | | 111 | | { |
| | 2 | 112 | | var response = _restClient.FullBackup( |
| | 2 | 113 | | VaultUri.AbsoluteUri, |
| | 2 | 114 | | new SASTokenParameter(blobStorageUri.AbsoluteUri, sasToken), |
| | 2 | 115 | | cancellationToken); |
| | | 116 | | |
| | 2 | 117 | | return new BackupOperation(this, response); |
| | | 118 | | } |
| | 0 | 119 | | catch (Exception ex) |
| | | 120 | | { |
| | 0 | 121 | | scope.Failed(ex); |
| | 0 | 122 | | throw; |
| | | 123 | | } |
| | 2 | 124 | | } |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// Initiates a full restore of the Key Vault. |
| | | 128 | | /// </summary> |
| | | 129 | | /// <param name="blobStorageUri">The <see cref="Uri"/> for the blob storage resource.</param> |
| | | 130 | | /// <param name="sasToken">A Shared Access Signature (SAS) token to authorize access to the blob.</param> |
| | | 131 | | /// <param name="folderName">The name of the container containing the backup data to restore.</param> |
| | | 132 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 133 | | /// <exception cref="ArgumentNullException"><paramref name="blobStorageUri"/> or <paramref name="sasToken"/> is |
| | | 134 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | | 135 | | /// <returns>A <see cref="RestoreOperation"/> to wait on this long-running operation.</returns> |
| | | 136 | | public virtual async Task<RestoreOperation> StartRestoreAsync(Uri blobStorageUri, string sasToken, string folder |
| | | 137 | | { |
| | 2 | 138 | | using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultBackupClient)}.{nameof(StartRestore |
| | 2 | 139 | | scope.Start(); |
| | | 140 | | try |
| | | 141 | | { |
| | 2 | 142 | | var response = await _restClient.FullRestoreOperationAsync( |
| | 2 | 143 | | VaultUri.AbsoluteUri, |
| | 2 | 144 | | new RestoreOperationParameters( |
| | 2 | 145 | | new SASTokenParameter(blobStorageUri.AbsoluteUri, sasToken), |
| | 2 | 146 | | folderName), |
| | 2 | 147 | | cancellationToken).ConfigureAwait(false); |
| | | 148 | | |
| | 2 | 149 | | return new RestoreOperation(this, response); |
| | | 150 | | } |
| | 0 | 151 | | catch (Exception ex) |
| | | 152 | | { |
| | 0 | 153 | | scope.Failed(ex); |
| | 0 | 154 | | throw; |
| | | 155 | | } |
| | 2 | 156 | | } |
| | | 157 | | |
| | | 158 | | /// <summary> |
| | | 159 | | /// Initiates a full Restore of the Key Vault. |
| | | 160 | | /// </summary> |
| | | 161 | | /// <param name="blobStorageUri">The <see cref="Uri"/> for the blob storage resource.</param> |
| | | 162 | | /// <param name="sasToken">A Shared Access Signature (SAS) token to authorize access to the blob.</param> |
| | | 163 | | /// <param name="folderName">The name of the container containing the backup data to restore.</param> |
| | | 164 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 165 | | /// <exception cref="ArgumentNullException"><paramref name="blobStorageUri"/> or <paramref name="sasToken"/> is |
| | | 166 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | | 167 | | /// <returns>A <see cref="RestoreOperation"/> to wait on this long-running operation.</returns> |
| | | 168 | | public virtual RestoreOperation StartRestore(Uri blobStorageUri, string sasToken, string folderName = default, C |
| | | 169 | | { |
| | 2 | 170 | | using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultBackupClient)}.{nameof(StartRestore |
| | 2 | 171 | | scope.Start(); |
| | | 172 | | try |
| | | 173 | | { |
| | 2 | 174 | | var response = _restClient.FullRestoreOperation( |
| | 2 | 175 | | VaultUri.AbsoluteUri, |
| | 2 | 176 | | new RestoreOperationParameters(new SASTokenParameter(blobStorageUri.AbsoluteUri, sasToken), folderNa |
| | 2 | 177 | | cancellationToken); |
| | | 178 | | |
| | 2 | 179 | | return new RestoreOperation(this, response); |
| | | 180 | | } |
| | 0 | 181 | | catch (Exception ex) |
| | | 182 | | { |
| | 0 | 183 | | scope.Failed(ex); |
| | 0 | 184 | | throw; |
| | | 185 | | } |
| | 2 | 186 | | } |
| | | 187 | | |
| | | 188 | | /// <summary> |
| | | 189 | | /// Initiates a selective restore of the Key Vault. |
| | | 190 | | /// </summary> |
| | | 191 | | /// <param name="keyName">The name of the key to be restored from the supplied backup.</param> |
| | | 192 | | /// <param name="blobStorageUri">The <see cref="Uri"/> for the blob storage resource.</param> |
| | | 193 | | /// <param name="sasToken">A Shared Access Signature (SAS) token to authorize access to the blob.</param> |
| | | 194 | | /// <param name="folderName">The name of the container containing the backup data to restore.</param> |
| | | 195 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 196 | | /// <exception cref="ArgumentNullException"><paramref name="blobStorageUri"/> or <paramref name="sasToken"/> is |
| | | 197 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | | 198 | | /// <returns>A <see cref="RestoreOperation"/> to wait on this long-running operation.</returns> |
| | | 199 | | public virtual async Task<RestoreOperation> StartSelectiveRestoreAsync(string keyName, Uri blobStorageUri, strin |
| | | 200 | | { |
| | 0 | 201 | | using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultBackupClient)}.{nameof(StartSelecti |
| | 0 | 202 | | scope.Start(); |
| | | 203 | | try |
| | | 204 | | { |
| | 0 | 205 | | var response = await _restClient.SelectiveKeyRestoreOperationAsync( |
| | 0 | 206 | | VaultUri.AbsoluteUri, |
| | 0 | 207 | | keyName, |
| | 0 | 208 | | new SelectiveKeyRestoreOperationParameters(new SASTokenParameter(blobStorageUri.AbsoluteUri, sasToke |
| | 0 | 209 | | cancellationToken).ConfigureAwait(false); |
| | | 210 | | |
| | 0 | 211 | | return new RestoreOperation(this, response); |
| | | 212 | | } |
| | 0 | 213 | | catch (Exception ex) |
| | | 214 | | { |
| | 0 | 215 | | scope.Failed(ex); |
| | 0 | 216 | | throw; |
| | | 217 | | } |
| | 0 | 218 | | } |
| | | 219 | | |
| | | 220 | | /// <summary> |
| | | 221 | | /// Initiates a selective Restore of the Key Vault. |
| | | 222 | | /// </summary> |
| | | 223 | | /// <param name="keyName">The name of the key to be restored from the supplied backup.</param> |
| | | 224 | | /// <param name="blobStorageUri">The <see cref="Uri"/> for the blob storage resource.</param> |
| | | 225 | | /// <param name="sasToken">A Shared Access Signature (SAS) token to authorize access to the blob.</param> |
| | | 226 | | /// <param name="folderName">The name of the container containing the backup data to restore.</param> |
| | | 227 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 228 | | /// <exception cref="ArgumentNullException"><paramref name="blobStorageUri"/> or <paramref name="sasToken"/> is |
| | | 229 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | | 230 | | /// <returns>A <see cref="RestoreOperation"/> to wait on this long-running operation.</returns> |
| | | 231 | | public virtual RestoreOperation StartSelectiveRestore(string keyName, Uri blobStorageUri, string sasToken, strin |
| | | 232 | | { |
| | 0 | 233 | | using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultBackupClient)}.{nameof(StartSelecti |
| | 0 | 234 | | scope.Start(); |
| | | 235 | | try |
| | | 236 | | { |
| | 0 | 237 | | var response = _restClient.SelectiveKeyRestoreOperation( |
| | 0 | 238 | | VaultUri.AbsoluteUri, |
| | 0 | 239 | | keyName, |
| | 0 | 240 | | new SelectiveKeyRestoreOperationParameters(new SASTokenParameter(blobStorageUri.AbsoluteUri, sasToke |
| | 0 | 241 | | cancellationToken); |
| | | 242 | | |
| | 0 | 243 | | return new RestoreOperation(this, response); |
| | | 244 | | } |
| | 0 | 245 | | catch (Exception ex) |
| | | 246 | | { |
| | 0 | 247 | | scope.Failed(ex); |
| | 0 | 248 | | throw; |
| | | 249 | | } |
| | 0 | 250 | | } |
| | | 251 | | |
| | | 252 | | /// <summary> |
| | | 253 | | /// Returns the details of full restore operation. |
| | | 254 | | /// </summary> |
| | | 255 | | /// <param name="jobId"> The Job Id returned part of the full restore operation. </param> |
| | | 256 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | | 257 | | /// <exception cref="ArgumentNullException"><paramref name="jobId"/> is null.</exception> |
| | | 258 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | | 259 | | internal virtual async Task<Response<RestoreDetailsInternal>> GetRestoreDetailsAsync(string jobId, CancellationT |
| | | 260 | | { |
| | 12 | 261 | | using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultBackupClient)}.{nameof(GetRestoreDe |
| | 12 | 262 | | scope.Start(); |
| | | 263 | | try |
| | | 264 | | { |
| | 12 | 265 | | return await _restClient.RestoreStatusAsync(VaultUri.AbsoluteUri, jobId, cancellationToken).ConfigureAwa |
| | | 266 | | } |
| | 0 | 267 | | catch (Exception ex) |
| | | 268 | | { |
| | 0 | 269 | | scope.Failed(ex); |
| | 0 | 270 | | throw; |
| | | 271 | | } |
| | 12 | 272 | | } |
| | | 273 | | |
| | | 274 | | /// <summary> |
| | | 275 | | /// Returns the details of full restore operation. |
| | | 276 | | /// </summary> |
| | | 277 | | /// <param name="jobId"> The Job Id returned part of the full restore operation. </param> |
| | | 278 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | | 279 | | /// <exception cref="ArgumentNullException"><paramref name="jobId"/> is null.</exception> |
| | | 280 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | | 281 | | internal virtual Response<RestoreDetailsInternal> GetRestoreDetails(string jobId, CancellationToken cancellation |
| | | 282 | | { |
| | 0 | 283 | | using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultBackupClient)}.{nameof(GetRestoreDe |
| | 0 | 284 | | scope.Start(); |
| | | 285 | | try |
| | | 286 | | { |
| | 0 | 287 | | return _restClient.RestoreStatus(VaultUri.AbsoluteUri, jobId, cancellationToken); |
| | | 288 | | } |
| | 0 | 289 | | catch (Exception ex) |
| | | 290 | | { |
| | 0 | 291 | | scope.Failed(ex); |
| | 0 | 292 | | throw; |
| | | 293 | | } |
| | 0 | 294 | | } |
| | | 295 | | |
| | | 296 | | /// <summary> |
| | | 297 | | /// Returns the details of full backup operation. |
| | | 298 | | /// </summary> |
| | | 299 | | /// <param name="jobId"> The Job Id returned part of the full backup operation. </param> |
| | | 300 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | | 301 | | /// <exception cref="ArgumentNullException"><paramref name="jobId"/> is null.</exception> |
| | | 302 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | | 303 | | internal virtual async Task<Response<FullBackupDetailsInternal>> GetBackupDetailsAsync(string jobId, Cancellatio |
| | | 304 | | { |
| | 10 | 305 | | using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultBackupClient)}.{nameof(GetBackupDet |
| | 10 | 306 | | scope.Start(); |
| | | 307 | | try |
| | | 308 | | { |
| | 10 | 309 | | return await _restClient.FullBackupStatusAsync(VaultUri.AbsoluteUri, jobId, cancellationToken).Configure |
| | | 310 | | } |
| | 0 | 311 | | catch (Exception ex) |
| | | 312 | | { |
| | 0 | 313 | | scope.Failed(ex); |
| | 0 | 314 | | throw; |
| | | 315 | | } |
| | 10 | 316 | | } |
| | | 317 | | |
| | | 318 | | /// <summary> |
| | | 319 | | /// Returns the details of full backup operation. |
| | | 320 | | /// </summary> |
| | | 321 | | /// <param name="jobId"> The Job Id returned part of the full backup operation. </param> |
| | | 322 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | | 323 | | /// <exception cref="ArgumentNullException"><paramref name="jobId"/> is null.</exception> |
| | | 324 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | | 325 | | internal virtual Response<FullBackupDetailsInternal> GetBackupDetails(string jobId, CancellationToken cancellati |
| | | 326 | | { |
| | 0 | 327 | | using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultBackupClient)}.{nameof(GetBackupDet |
| | 0 | 328 | | scope.Start(); |
| | | 329 | | try |
| | | 330 | | { |
| | 0 | 331 | | return _restClient.FullBackupStatus(VaultUri.AbsoluteUri, jobId, cancellationToken); |
| | | 332 | | } |
| | 0 | 333 | | catch (Exception ex) |
| | | 334 | | { |
| | 0 | 335 | | scope.Failed(ex); |
| | 0 | 336 | | throw; |
| | | 337 | | } |
| | 0 | 338 | | } |
| | | 339 | | } |
| | | 340 | | } |