@@ -65,21 +65,30 @@ client = KeyVaultAccessControlClient(vault_url=MANAGED_HSM_URL, credential=crede
65
65
> ** NOTE:** For an asynchronous client, import ` azure.keyvault.administration.aio ` 's ` KeyVaultAccessControlClient ` instead.
66
66
67
67
#### Create a KeyVaultBackupClient
68
- 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):
68
+ After creating a user-assigned [ managed identity] [ managed_identity ] and
69
+ [ granting it access to your Managed HSM] [ managed_identity_backup_setup ] , you can do the following to create a backup
70
+ client (setting the value of ` CLIENT_ID ` to your managed identity's client ID):
69
71
70
72
<!-- SNIPPET:backup_restore_operations.create_a_backup_restore_client -->
71
73
72
74
``` python
73
- from azure.identity import DefaultAzureCredential
75
+ from azure.identity import ManagedIdentityCredential
74
76
from azure.keyvault.administration import KeyVaultBackupClient
75
77
76
78
MANAGED_HSM_URL = os.environ[" MANAGED_HSM_URL" ]
77
- credential = DefaultAzureCredential()
79
+ MANAGED_IDENTITY_CLIENT_ID = os.environ[" CLIENT_ID" ]
80
+ credential = ManagedIdentityCredential(client_id = MANAGED_IDENTITY_CLIENT_ID )
78
81
client = KeyVaultBackupClient(vault_url = MANAGED_HSM_URL , credential = credential)
79
82
```
80
83
81
84
<!-- END SNIPPET -->
82
85
86
+ Using the ` ManagedIdentityCredential ` is preferred in order to enable authenticating backup and restore operations with
87
+ Managed Identity. Any other ` azure-identity ` credential could be provided instead if SAS tokens are used in these
88
+ operations.
89
+
90
+ See [ azure-identity] [ managed_identity_ref ] documentation for more information on Managed Identity authentication.
91
+
83
92
> ** NOTE:** For an asynchronous client, import ` azure.keyvault.administration.aio ` 's ` KeyVaultBackupClient ` instead.
84
93
85
94
#### Create a KeyVaultSettingsClient
@@ -265,7 +274,11 @@ client.delete_role_assignment(scope=scope, name=role_assignment.name)
265
274
266
275
### Perform a full key backup
267
276
The ` KeyVaultBackupClient ` can be used to back up your entire collection of keys. The backing store for full key
268
- backups is a blob storage container using Shared Access Signature (SAS) authentication.
277
+ backups is a blob storage container using either Managed Identity (which is preferred) or Shared Access Signature (SAS)
278
+ authentication.
279
+
280
+ If using Managed Identity, first make sure your user-assigned managed identity has the correct access to your Storage
281
+ account and Managed HSM per [ the service's guidance] [ managed_identity_backup_setup ] .
269
282
270
283
For more details on creating a SAS token using a ` BlobServiceClient ` from [ ` azure-storage-blob ` ] [ storage_blob ] , refer
271
284
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
275
288
276
289
``` python
277
290
CONTAINER_URL = os.environ[" CONTAINER_URL" ]
278
- SAS_TOKEN = os.environ[" SAS_TOKEN" ]
279
291
280
- backup_result: KeyVaultBackupResult = client.begin_backup(CONTAINER_URL , sas_token = SAS_TOKEN ).result()
292
+ backup_result: KeyVaultBackupResult = client.begin_backup(CONTAINER_URL , use_managed_identity = True ).result()
281
293
print (f " Azure Storage Blob URL of the backup: { backup_result.folder_url} " )
282
294
```
283
295
@@ -289,8 +301,12 @@ the operation is complete without returning an object.
289
301
290
302
### Perform a full key restore
291
303
The ` KeyVaultBackupClient ` can be used to restore your entire collection of keys from a backup. The data source for a
292
- full key restore is a storage blob accessed using Shared Access Signature authentication. You will also need the URL of
293
- the backup (` KeyVaultBackupResult.folder_url ` ) from the [ above snippet] ( #perform-a-full-key-backup ) .
304
+ full key restore is a storage blob accessed using either Managed Identity (which is preferred) or Shared Access
305
+ Signature (SAS) authentication. You will also need the URL of the backup (` KeyVaultBackupResult.folder_url ` ) from the
306
+ [ above snippet] ( #perform-a-full-key-backup ) .
307
+
308
+ If using Managed Identity, first make sure your user-assigned managed identity has the correct access to your Storage
309
+ account and Managed HSM per [ the service's guidance] [ managed_identity_backup_setup ] .
294
310
295
311
For more details on creating a SAS token using a ` BlobServiceClient ` from [ ` azure-storage-blob ` ] [ storage_blob ] , refer
296
312
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
299
315
<!-- SNIPPET:backup_restore_operations.begin_restore -->
300
316
301
317
``` python
302
- SAS_TOKEN = os.environ[" SAS_TOKEN" ]
303
-
304
318
# `backup_result` is the KeyVaultBackupResult returned by `begin_backup`
305
- client.begin_restore(backup_result.folder_url, sas_token = SAS_TOKEN ).wait()
319
+ client.begin_restore(backup_result.folder_url, use_managed_identity = True ).wait()
306
320
print (" Vault restored successfully." )
307
321
```
308
322
@@ -398,7 +412,9 @@ contact
[email protected] with any additional questions or comments.
398
412
399
413
[ managed_hsm ] : https://docs.microsoft.com/azure/key-vault/managed-hsm/overview
400
414
[ managed_hsm_cli ] : https://docs.microsoft.com/azure/key-vault/managed-hsm/quick-create-cli
401
- [ managed_identity ] : https://docs.microsoft.com/azure/active-directory/managed-identities-azure-resources/overview
415
+ [ managed_identity ] : https://learn.microsoft.com/entra/identity/managed-identities-azure-resources/overview
416
+ [ 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
417
+ [ managed_identity_ref ] : https://aka.ms/azsdk/python/identity/docs#azure.identity.ManagedIdentityCredential
402
418
403
419
[ pip ] : https://pypi.org/project/pip/
404
420
[ pypi_package_administration ] : https://pypi.org/project/azure-keyvault-administration
0 commit comments