-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Key Vault] Update tests to only record with Managed Identity (#35859)
- Loading branch information
Showing
10 changed files
with
207 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,21 +65,30 @@ client = KeyVaultAccessControlClient(vault_url=MANAGED_HSM_URL, credential=crede | |
> **NOTE:** For an asynchronous client, import `azure.keyvault.administration.aio`'s `KeyVaultAccessControlClient` instead. | ||
#### Create a KeyVaultBackupClient | ||
After configuring your environment for the [DefaultAzureCredential][default_cred_ref] to use a suitable method of authentication, you can do the following to create a backup client (replacing the value of `vault_url` with your Managed HSM's URL): | ||
After creating a user-assigned [managed identity][managed_identity] and | ||
[granting it access to your Managed HSM][managed_identity_backup_setup], you can do the following to create a backup | ||
client (setting the value of `CLIENT_ID` to your managed identity's client ID): | ||
|
||
<!-- SNIPPET:backup_restore_operations.create_a_backup_restore_client --> | ||
|
||
```python | ||
from azure.identity import DefaultAzureCredential | ||
from azure.identity import ManagedIdentityCredential | ||
from azure.keyvault.administration import KeyVaultBackupClient | ||
|
||
MANAGED_HSM_URL = os.environ["MANAGED_HSM_URL"] | ||
credential = DefaultAzureCredential() | ||
MANAGED_IDENTITY_CLIENT_ID = os.environ["CLIENT_ID"] | ||
credential = ManagedIdentityCredential(client_id=MANAGED_IDENTITY_CLIENT_ID) | ||
client = KeyVaultBackupClient(vault_url=MANAGED_HSM_URL, credential=credential) | ||
``` | ||
|
||
<!-- END SNIPPET --> | ||
|
||
Using the `ManagedIdentityCredential` is preferred in order to enable authenticating backup and restore operations with | ||
Managed Identity. Any other `azure-identity` credential could be provided instead if SAS tokens are used in these | ||
operations. | ||
|
||
See [azure-identity][managed_identity_ref] documentation for more information on Managed Identity authentication. | ||
|
||
> **NOTE:** For an asynchronous client, import `azure.keyvault.administration.aio`'s `KeyVaultBackupClient` instead. | ||
#### Create a KeyVaultSettingsClient | ||
|
@@ -265,7 +274,11 @@ client.delete_role_assignment(scope=scope, name=role_assignment.name) | |
|
||
### Perform a full key backup | ||
The `KeyVaultBackupClient` can be used to back up your entire collection of keys. The backing store for full key | ||
backups is a blob storage container using Shared Access Signature (SAS) authentication. | ||
backups is a blob storage container using either Managed Identity (which is preferred) or Shared Access Signature (SAS) | ||
authentication. | ||
|
||
If using Managed Identity, first make sure your user-assigned managed identity has the correct access to your Storage | ||
account and Managed HSM per [the service's guidance][managed_identity_backup_setup]. | ||
|
||
For more details on creating a SAS token using a `BlobServiceClient` from [`azure-storage-blob`][storage_blob], refer | ||
to the library's [credential documentation][sas_docs]. Alternatively, it is possible to | ||
|
@@ -275,9 +288,8 @@ to the library's [credential documentation][sas_docs]. Alternatively, it is poss | |
|
||
```python | ||
CONTAINER_URL = os.environ["CONTAINER_URL"] | ||
SAS_TOKEN = os.environ["SAS_TOKEN"] | ||
|
||
backup_result: KeyVaultBackupResult = client.begin_backup(CONTAINER_URL, sas_token=SAS_TOKEN).result() | ||
backup_result: KeyVaultBackupResult = client.begin_backup(CONTAINER_URL, use_managed_identity=True).result() | ||
print(f"Azure Storage Blob URL of the backup: {backup_result.folder_url}") | ||
``` | ||
|
||
|
@@ -289,8 +301,12 @@ the operation is complete without returning an object. | |
|
||
### Perform a full key restore | ||
The `KeyVaultBackupClient` can be used to restore your entire collection of keys from a backup. The data source for a | ||
full key restore is a storage blob accessed using Shared Access Signature authentication. You will also need the URL of | ||
the backup (`KeyVaultBackupResult.folder_url`) from the [above snippet](#perform-a-full-key-backup). | ||
full key restore is a storage blob accessed using either Managed Identity (which is preferred) or Shared Access | ||
Signature (SAS) authentication. You will also need the URL of the backup (`KeyVaultBackupResult.folder_url`) from the | ||
[above snippet](#perform-a-full-key-backup). | ||
|
||
If using Managed Identity, first make sure your user-assigned managed identity has the correct access to your Storage | ||
account and Managed HSM per [the service's guidance][managed_identity_backup_setup]. | ||
|
||
For more details on creating a SAS token using a `BlobServiceClient` from [`azure-storage-blob`][storage_blob], refer | ||
to the library's [credential documentation][sas_docs]. Alternatively, it is possible to | ||
|
@@ -299,10 +315,8 @@ to the library's [credential documentation][sas_docs]. Alternatively, it is poss | |
<!-- SNIPPET:backup_restore_operations.begin_restore --> | ||
|
||
```python | ||
SAS_TOKEN = os.environ["SAS_TOKEN"] | ||
|
||
# `backup_result` is the KeyVaultBackupResult returned by `begin_backup` | ||
client.begin_restore(backup_result.folder_url, sas_token=SAS_TOKEN).wait() | ||
client.begin_restore(backup_result.folder_url, use_managed_identity=True).wait() | ||
print("Vault restored successfully.") | ||
``` | ||
|
||
|
@@ -398,7 +412,9 @@ contact [email protected] with any additional questions or comments. | |
|
||
[managed_hsm]: https://docs.microsoft.com/azure/key-vault/managed-hsm/overview | ||
[managed_hsm_cli]: https://docs.microsoft.com/azure/key-vault/managed-hsm/quick-create-cli | ||
[managed_identity]: https://docs.microsoft.com/azure/active-directory/managed-identities-azure-resources/overview | ||
[managed_identity]: https://learn.microsoft.com/entra/identity/managed-identities-azure-resources/overview | ||
[managed_identity_backup_setup]: https://learn.microsoft.com/azure/key-vault/managed-hsm/backup-restore#prerequisites-if-backing-up-and-restoring-using-user-assigned-managed-identity | ||
[managed_identity_ref]: https://aka.ms/azsdk/python/identity/docs#azure.identity.ManagedIdentityCredential | ||
|
||
[pip]: https://pypi.org/project/pip/ | ||
[pypi_package_administration]: https://pypi.org/project/azure-keyvault-administration | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.