Skip to content

Commit fa4c84f

Browse files
JonathanCrdjsquire
andauthored
[KeyVault] Enable v7.6 preview.2 for Administration (Azure#49060)
* Add headers files * Expose PreRestore API and create PreRestoreOperation type * Expose PreBackup API and refactor BackupOperation to share logic with new PreBackupOperation type * Export API * Add tests * Fix DIagnosticScope name for PreRestore * Remove PreBackupOperation and use BackupOperation instead * Remove PreRestoreOperation and Headers * Fix ClientDiagnostics scope name * Allow the folderUri to be null * nit changes * Record tests * Export API * Remove PreBackup headers * Apply suggestions from code review Co-authored-by: Jesse Squire <[email protected]> * Remove old comment --------- Co-authored-by: Jesse Squire <[email protected]>
1 parent 73c139c commit fa4c84f

File tree

8 files changed

+239
-7
lines changed

8 files changed

+239
-7
lines changed

sdk/keyvault/Azure.Security.KeyVault.Administration/api/Azure.Security.KeyVault.Administration.net8.0.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ public KeyVaultBackupClient(System.Uri vaultUri, Azure.Core.TokenCredential cred
8080
public virtual System.Uri VaultUri { get { throw null; } }
8181
public virtual Azure.Security.KeyVault.Administration.KeyVaultBackupOperation StartBackup(System.Uri blobStorageUri, string sasToken = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
8282
public virtual System.Threading.Tasks.Task<Azure.Security.KeyVault.Administration.KeyVaultBackupOperation> StartBackupAsync(System.Uri blobStorageUri, string sasToken = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
83+
public virtual Azure.Security.KeyVault.Administration.KeyVaultBackupOperation StartPreBackup(System.Uri blobStorageUri, string sasToken = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
84+
public virtual System.Threading.Tasks.Task<Azure.Security.KeyVault.Administration.KeyVaultBackupOperation> StartPreBackupAsync(System.Uri blobStorageUri, string sasToken = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
85+
public virtual Azure.Security.KeyVault.Administration.KeyVaultRestoreOperation StartPreRestore(System.Uri folderUri, string sasToken = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
86+
public virtual System.Threading.Tasks.Task<Azure.Security.KeyVault.Administration.KeyVaultRestoreOperation> StartPreRestoreAsync(System.Uri folderUri, string sasToken = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
8387
public virtual Azure.Security.KeyVault.Administration.KeyVaultRestoreOperation StartRestore(System.Uri folderUri, string sasToken = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
8488
public virtual System.Threading.Tasks.Task<Azure.Security.KeyVault.Administration.KeyVaultRestoreOperation> StartRestoreAsync(System.Uri folderUri, string sasToken = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
8589
public virtual Azure.Security.KeyVault.Administration.KeyVaultSelectiveKeyRestoreOperation StartSelectiveKeyRestore(string keyName, System.Uri folderUri, string sasToken = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }

sdk/keyvault/Azure.Security.KeyVault.Administration/api/Azure.Security.KeyVault.Administration.netstandard2.0.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ public KeyVaultBackupClient(System.Uri vaultUri, Azure.Core.TokenCredential cred
8080
public virtual System.Uri VaultUri { get { throw null; } }
8181
public virtual Azure.Security.KeyVault.Administration.KeyVaultBackupOperation StartBackup(System.Uri blobStorageUri, string sasToken = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
8282
public virtual System.Threading.Tasks.Task<Azure.Security.KeyVault.Administration.KeyVaultBackupOperation> StartBackupAsync(System.Uri blobStorageUri, string sasToken = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
83+
public virtual Azure.Security.KeyVault.Administration.KeyVaultBackupOperation StartPreBackup(System.Uri blobStorageUri, string sasToken = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
84+
public virtual System.Threading.Tasks.Task<Azure.Security.KeyVault.Administration.KeyVaultBackupOperation> StartPreBackupAsync(System.Uri blobStorageUri, string sasToken = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
85+
public virtual Azure.Security.KeyVault.Administration.KeyVaultRestoreOperation StartPreRestore(System.Uri folderUri, string sasToken = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
86+
public virtual System.Threading.Tasks.Task<Azure.Security.KeyVault.Administration.KeyVaultRestoreOperation> StartPreRestoreAsync(System.Uri folderUri, string sasToken = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
8387
public virtual Azure.Security.KeyVault.Administration.KeyVaultRestoreOperation StartRestore(System.Uri folderUri, string sasToken = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
8488
public virtual System.Threading.Tasks.Task<Azure.Security.KeyVault.Administration.KeyVaultRestoreOperation> StartRestoreAsync(System.Uri folderUri, string sasToken = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
8589
public virtual Azure.Security.KeyVault.Administration.KeyVaultSelectiveKeyRestoreOperation StartSelectiveKeyRestore(string keyName, System.Uri folderUri, string sasToken = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }

sdk/keyvault/Azure.Security.KeyVault.Administration/assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "net",
44
"TagPrefix": "net/keyvault/Azure.Security.KeyVault.Administration",
5-
"Tag": "net/keyvault/Azure.Security.KeyVault.Administration_c8243c86d4"
5+
"Tag": "net/keyvault/Azure.Security.KeyVault.Administration_d0d7c3d864"
66
}

sdk/keyvault/Azure.Security.KeyVault.Administration/src/KeyVaultBackupClient.cs

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,98 @@ internal virtual Response<SelectiveKeyRestoreDetailsInternal> GetSelectiveKeyRes
400400
}
401401
}
402402

403+
/// <summary>
404+
/// Initiate a pre-restore check on a Key Vault. This operation checks if it is possible to restore the entire collection of keys from a Key Vault.
405+
/// </summary>
406+
/// <param name="folderUri">
407+
/// The <see cref="Uri"/> for the blob storage resource, including the path to the blob container where the backup resides.
408+
/// This would be the exact value that is returned as the result of a <see cref="KeyVaultBackupOperation"/>.
409+
/// An example <paramref name="folderUri" /> may look like the following: https://contoso.blob.core.windows.net/backup/mhsm-contoso-2020090117323313.
410+
/// </param>
411+
/// <param name="sasToken">Optional Shared Access Signature (SAS) token to authorize access to the blob. If null, Managed Identity will be used to authenticate instead.</param>
412+
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
413+
/// <exception cref="ArgumentNullException"><paramref name="folderUri"/> or <paramref name="sasToken"/> are <c>null</c>.</exception>
414+
/// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> for details returned from the server.</exception>
415+
/// <returns>A <see cref="KeyVaultRestoreOperation"/> representing the result of the asynchronous operation.</returns>
416+
public virtual async Task<KeyVaultRestoreOperation> StartPreRestoreAsync(Uri folderUri, string sasToken = default, CancellationToken cancellationToken = default)
417+
{
418+
Argument.AssertNotNull(folderUri, nameof(folderUri));
419+
Argument.AssertNotNull(sasToken, nameof(sasToken));
420+
421+
using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultBackupClient)}.{nameof(StartPreRestore)}");
422+
scope.Start();
423+
try
424+
{
425+
// Get the folder name from the backupBlobUri returned from a previous BackupOperation
426+
ParseFolderName(folderUri, out string containerUriString, out string folderName);
427+
428+
var operation = await _restClient.PreFullRestoreOperationAsync(
429+
WaitUntil.Started,
430+
new PreRestoreOperationParameters(
431+
new SASTokenParameter(containerUriString, sasToken),
432+
folderUri.AbsoluteUri
433+
),
434+
cancellationToken).ConfigureAwait(false);
435+
436+
// Rest client returns an Operation without headers, so we need to create a new response with headers.
437+
var headers = new AzureSecurityKeyVaultAdministrationFullRestoreOperationHeaders(operation.GetRawResponse());
438+
var responseWithHeaders = ResponseWithHeaders.FromValue(headers, operation.GetRawResponse());
439+
440+
return new KeyVaultRestoreOperation(this, responseWithHeaders);
441+
}
442+
catch (Exception ex)
443+
{
444+
scope.Failed(ex);
445+
throw;
446+
}
447+
}
448+
449+
/// <summary>
450+
/// Initiate a pre-restore check on a Key Vault. This operation checks if it is possible to restore the entire collection of keys from a Key Vault.
451+
/// </summary>
452+
/// <param name="folderUri">
453+
/// The <see cref="Uri"/> for the blob storage resource, including the path to the blob container where the backup resides.
454+
/// This would be the exact value that is returned as the result of a <see cref="KeyVaultBackupOperation"/>.
455+
/// An example <paramref name="folderUri" /> path may look like the following: https://contoso.blob.core.windows.net/backup/mhsm-contoso-2020090117323313.
456+
/// </param>
457+
/// <param name="sasToken">Optional Shared Access Signature (SAS) token to authorize access to the blob. If null, Managed Identity will be used to authenticate instead.</param>
458+
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
459+
/// <exception cref="ArgumentNullException"><paramref name="folderUri"/> or <paramref name="sasToken"/> are <c>null</c>.</exception>
460+
/// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> for details returned from the server.</exception>
461+
/// <returns>A <see cref="KeyVaultRestoreOperation"/> to wait on this long-running operation.</returns>
462+
public virtual KeyVaultRestoreOperation StartPreRestore(Uri folderUri, string sasToken = default, CancellationToken cancellationToken = default)
463+
{
464+
Argument.AssertNotNull(folderUri, nameof(folderUri));
465+
Argument.AssertNotNull(sasToken, nameof(sasToken));
466+
467+
using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultBackupClient)}.{nameof(StartPreRestore)}");
468+
scope.Start();
469+
try
470+
{
471+
// Get the folder name from the backupBlobUri returned from a previous BackupOperation
472+
ParseFolderName(folderUri, out string containerUriString, out string folderName);
473+
474+
var operation = _restClient.PreFullRestoreOperation(
475+
WaitUntil.Started,
476+
new PreRestoreOperationParameters(
477+
new SASTokenParameter(containerUriString, sasToken),
478+
folderUri.AbsoluteUri
479+
),
480+
cancellationToken);
481+
482+
// Rest client returns an Operation without headers, so we need to create a new response with headers.
483+
var headers = new AzureSecurityKeyVaultAdministrationFullRestoreOperationHeaders(operation.GetRawResponse());
484+
var responseWithHeaders = ResponseWithHeaders.FromValue(headers, operation.GetRawResponse());
485+
486+
return new KeyVaultRestoreOperation(this, responseWithHeaders);
487+
}
488+
catch (Exception ex)
489+
{
490+
scope.Failed(ex);
491+
throw;
492+
}
493+
}
494+
403495
/// <summary>
404496
/// Returns the details of a full key backup operation.
405497
/// </summary>
@@ -444,6 +536,86 @@ internal virtual Response<FullBackupDetailsInternal> GetBackupDetails(string job
444536
}
445537
}
446538

539+
/// <summary>
540+
/// Initiates a pre-backup check on the Key Vault. This operation checks if it is possible to back up the entire collection of keys from a Key Vault.
541+
/// </summary>
542+
/// <param name="blobStorageUri">The <see cref="Uri"/> for the blob storage resource.</param>
543+
/// <param name="sasToken">Optional Shared Access Signature (SAS) token to authorize access to the blob. If null, Managed Identity will be used to authenticate instead.</param>
544+
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
545+
/// <exception cref="ArgumentNullException"><paramref name="blobStorageUri"/> or <paramref name="sasToken"/> is null.</exception>
546+
/// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> for details returned from the server.</exception>
547+
/// <returns>A <see cref="KeyVaultBackupOperation"/> representing the result of the asynchronous operation.</returns>
548+
public virtual async Task<KeyVaultBackupOperation> StartPreBackupAsync(Uri blobStorageUri, string sasToken = default, CancellationToken cancellationToken = default)
549+
{
550+
Argument.AssertNotNull(blobStorageUri, nameof(blobStorageUri));
551+
Argument.AssertNotNull(sasToken, nameof(sasToken));
552+
553+
using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultBackupClient)}.{nameof(StartPreBackup)}");
554+
scope.Start();
555+
try
556+
{
557+
Operation<FullBackupDetailsInternal> operation = await _restClient.PreFullBackupAsync(
558+
WaitUntil.Started,
559+
new PreBackupOperationParameters(
560+
blobStorageUri.AbsoluteUri,
561+
sasToken,
562+
useManagedIdentity: (sasToken == default)
563+
),
564+
cancellationToken).ConfigureAwait(false);
565+
566+
// Rest client returns an Operation without headers, so we need to create a new response with headers.
567+
var headers = new AzureSecurityKeyVaultAdministrationFullBackupHeaders(operation.GetRawResponse());
568+
var responseWithHeaders = ResponseWithHeaders.FromValue(headers,operation.GetRawResponse());
569+
570+
return new KeyVaultBackupOperation(this, responseWithHeaders);
571+
}
572+
catch (Exception ex)
573+
{
574+
scope.Failed(ex);
575+
throw;
576+
}
577+
}
578+
579+
/// <summary>
580+
/// Initiates a pre-backup check on the Key Vault. This operation checks if it is possible to back up the entire collection of keys from a Key Vault.
581+
/// </summary>
582+
/// <param name="blobStorageUri">The <see cref="Uri"/> for the blob storage resource.</param>
583+
/// <param name="sasToken">Optional Shared Access Signature (SAS) token to authorize access to the blob. If null, Managed Identity will be used to authenticate instead.</param>
584+
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
585+
/// <exception cref="ArgumentNullException"><paramref name="blobStorageUri"/> or <paramref name="sasToken"/> is null.</exception>
586+
/// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> for details returned from the server.</exception>
587+
/// <returns>A <see cref="KeyVaultBackupOperation"/> representing the result of the operation.</returns>
588+
public virtual KeyVaultBackupOperation StartPreBackup(Uri blobStorageUri, string sasToken = default, CancellationToken cancellationToken = default)
589+
{
590+
Argument.AssertNotNull(blobStorageUri, nameof(blobStorageUri));
591+
Argument.AssertNotNull(sasToken, nameof(sasToken));
592+
593+
using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultBackupClient)}.{nameof(StartPreBackup)}");
594+
scope.Start();
595+
try
596+
{
597+
Operation<FullBackupDetailsInternal> operation = _restClient.PreFullBackup(
598+
WaitUntil.Started,
599+
new PreBackupOperationParameters(
600+
blobStorageUri.AbsoluteUri,
601+
sasToken,
602+
useManagedIdentity: (sasToken == default)
603+
),
604+
cancellationToken);
605+
606+
// Rest client returns an Operation without headers, so we need to create a new response with headers.
607+
var headers = new AzureSecurityKeyVaultAdministrationFullBackupHeaders(operation.GetRawResponse());
608+
var responseWithHeaders = ResponseWithHeaders.FromValue(headers,operation.GetRawResponse());
609+
610+
return new KeyVaultBackupOperation(this, responseWithHeaders);
611+
}
612+
catch (Exception ex)
613+
{
614+
scope.Failed(ex);
615+
throw;
616+
}
617+
}
618+
447619
internal static void ParseFolderName(Uri folderUri, out string containerUriString, out string folderName)
448620
{
449621
int indexOfContainerBoundary = folderUri.AbsoluteUri.IndexOf('/', folderUri.Scheme.Length + 4);

sdk/keyvault/Azure.Security.KeyVault.Administration/src/KeyVaultBackupOperation.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ internal KeyVaultBackupOperation(FullBackupDetailsInternal value, Response respo
7474
}
7575

7676
/// <summary> Initializes a new instance of <see cref="KeyVaultBackupOperation" /> for mocking. </summary>
77-
protected KeyVaultBackupOperation() {}
77+
protected KeyVaultBackupOperation() { }
7878

7979
/// <summary>
8080
/// The start time of the backup operation.
@@ -107,7 +107,13 @@ public override KeyVaultBackupResult Value
107107
throw _requestFailedException;
108108
}
109109
#pragma warning restore CA1065 // Do not raise exceptions in unexpected locations
110-
return new KeyVaultBackupResult(new Uri(_value.AzureStorageBlobContainerUri), _value.StartTime.Value, _value.EndTime.Value);
110+
111+
return new KeyVaultBackupResult(
112+
_value.AzureStorageBlobContainerUri is null ?
113+
null :
114+
new Uri(_value.AzureStorageBlobContainerUri) ,
115+
_value.StartTime.Value,
116+
_value.EndTime.Value);
111117
}
112118
}
113119

sdk/keyvault/Azure.Security.KeyVault.Administration/src/KeyVaultBackupResult.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ public class KeyVaultBackupResult
1313
{
1414
internal KeyVaultBackupResult(Uri folderUri, DateTimeOffset startTime, DateTimeOffset endTime)
1515
{
16-
Argument.AssertNotNull(folderUri, nameof(folderUri));
17-
1816
FolderUri = folderUri;
1917
StartTime = startTime;
2018
EndTime = endTime;

sdk/keyvault/Azure.Security.KeyVault.Administration/src/RestoreOperationInternal.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public RestoreOperationInternal(KeyVaultBackupClient client, string id)
4545
/// Initializes a new instance of a RestoreOperation.
4646
/// </summary>
4747
/// <param name="client">An instance of <see cref="KeyVaultBackupClient" />.</param>
48-
/// <param name="response">The <see cref="ResponseWithHeaders{T, THeaders}" /> returned from <see cref="KeyVaultBackupClient.StartRestore"/> or <see cref="KeyVaultBackupClient.StartRestoreAsync"/>.</param>
48+
/// <param name="response">The <see cref="ResponseWithHeaders{T, THeaders}" /> returned from <see cref="KeyVaultBackupClient.StartRestore"/>, <see cref="KeyVaultBackupClient.StartRestoreAsync"/>, <see cref="KeyVaultBackupClient.StartPreRestoreAsync"/>, or <see cref="KeyVaultBackupClient.StartPreRestore"/>.</param>
4949
/// <exception cref="ArgumentNullException"><paramref name="client"/> or <paramref name="response"/> is null.</exception>
5050
internal RestoreOperationInternal(KeyVaultBackupClient client, ResponseWithHeaders<THeaders> response)
5151
{

0 commit comments

Comments
 (0)