@@ -81,7 +81,7 @@ a more appropriate name for your service principal.
8181 ` ` ` Bash
8282 az keyvault create --hsm-name " <your-managed-hsm-name>" --resource-group " <your-resource-group-name>" --administrators < your-service-principal-object-id> --location " <your-azure-location>"
8383 ` ` `
84-
84+
8585* Activate your managed HSM to enable key and role management. Detailed instructions can be found in [this quickstart guide](https://docs.microsoft.com/azure/key-vault/managed-hsm/quick-create-cli#activate-your-managed-hsm). Create three self signed certificates and download the [Security Domain](https://docs.microsoft.com/azure/key-vault/managed-hsm/security-domain) for your managed HSM:
8686 > ** Important:** Create and store the RSA key pairs and security domain file generated in this step securely.
8787 ` ` ` Bash
@@ -165,12 +165,12 @@ credential = DefaultAzureCredential()
165165client = KeyVaultAccessControlClient(vault_url="https://my-managed-hsm-name.managedhsm.azure.net/", credential=credential)
166166
167167# this will list all role definitions available for assignment
168- role_definitions = client.list_role_definitions(role_scope= KeyVaultRoleScope.GLOBAL)
168+ role_definitions = client.list_role_definitions(KeyVaultRoleScope.GLOBAL)
169169
170- for role_definition in role_definitions:
171- print(role_definition .id)
172- print(role_definition .role_name)
173- print(role_definition .description)
170+ for definition in role_definitions:
171+ print(definition .id)
172+ print(definition .role_name)
173+ print(definition .description)
174174```
175175
176176### Set, Get, and Delete a role definition
@@ -180,33 +180,34 @@ for role_definition in role_definitions:
180180```python
181181import uuid
182182from azure.identity import DefaultAzureCredential
183- from azure.keyvault.administration import KeyVaultAccessControlClient, KeyVaultDataAction, KeyVaultPermission
183+ from azure.keyvault.administration import (
184+ KeyVaultAccessControlClient,
185+ KeyVaultDataAction,
186+ KeyVaultPermission,
187+ KeyVaultRoleScope
188+ )
184189
185190credential = DefaultAzureCredential()
186191
187192client = KeyVaultAccessControlClient(vault_url="https://my-managed-hsm-name.managedhsm.azure.net/", credential=credential)
188193
189- # create the custom role definition
190- role_scope = "/" # the global scope
191- definition_name = uuid.uuid4()
194+ # create a custom role definition
192195permissions = [KeyVaultPermission(allowed_data_actions=[KeyVaultDataAction.READ_HSM_KEY])]
193- created_definition = client.set_role_definition(
194- role_scope=role_scope, permissions=permissions, role_definition_name=definition_name
195- )
196+ created_definition = client.set_role_definition(KeyVaultRoleScope.GLOBAL, permissions=permissions)
196197
197198# update the custom role definition
198199permissions = [
199200 KeyVaultPermission(allowed_data_actions=[], denied_data_actions=[KeyVaultDataAction.READ_HSM_KEY])
200201]
201202updated_definition = client.set_role_definition(
202- role_scope=role_scope , permissions=permissions, role_definition_name=definition_name
203+ KeyVaultRoleScope.GLOBAL , permissions=permissions, role_name=created_definition.name
203204)
204205
205206# get the custom role definition
206- definition = client.get_role_definition(role_scope=role_scope, role_definition_name =definition_name)
207+ definition = client.get_role_definition(KeyVaultRoleScope.GLOBAL, role_name =definition_name)
207208
208209# delete the custom role definition
209- deleted_definition = client.delete_role_definition(role_scope=role_scope, role_definition_name =definition_name)
210+ deleted_definition = client.delete_role_definition(KeyVaultRoleScope.GLOBAL, role_name =definition_name)
210211```
211212
212213### List all role assignments
@@ -221,43 +222,42 @@ credential = DefaultAzureCredential()
221222client = KeyVaultAccessControlClient(vault_url="https://my-managed-hsm-name.managedhsm.azure.net/", credential=credential)
222223
223224# this will list all role assignments
224- role_assignments = client.list_role_assignments(role_scope= KeyVaultRoleScope.GLOBAL)
225+ role_assignments = client.list_role_assignments(KeyVaultRoleScope.GLOBAL)
225226
226- for role_assignment in role_assignments:
227- print(role_assignment .name)
228- print(role_assignment .principal_id)
229- print(role_assignment .role_definition_id)
227+ for assignment in role_assignments:
228+ print(assignment .name)
229+ print(assignment .principal_id)
230+ print(assignment .role_definition_id)
230231```
231232
232233### Create, Get, and Delete a role assignment
233234Assign a role to a service principal. This will require a role definition id from the list retrieved in the [above snippet](#list-all-role-definitions) and the principal object id retrieved in the [Create and Get credentials](#create-and-get-credentials) section.
234235
235236```python
236237from azure.identity import DefaultAzureCredential
237- from azure.keyvault.administration import KeyVaultAccessControlClient
238+ from azure.keyvault.administration import KeyVaultAccessControlClient, KeyVaultRoleScope
238239
239240credential = DefaultAzureCredential()
240241
241242client = KeyVaultAccessControlClient(vault_url="https://my-managed-hsm-name.managedhsm.azure.net/", credential=credential)
242243
243- role_scope = "/" # the global scope
244244role_definition_id = "<role-definition-id>" # Replace <role-definition-id> with the id of a definition returned from the previous example
245245principal_id = "<your-service-principal-object-id>"
246246
247247# first, let' s create the role assignment
248- role_assignment = client.create_role_assignment(role_scope , role_definition_id, principal_id)
248+ role_assignment = client.create_role_assignment(KeyVaultRoleScope.GLOBAL , role_definition_id, principal_id)
249249print(role_assignment.name)
250250print(role_assignment.principal_id)
251251print(role_assignment.role_definition_id)
252252
253253# now, we get it
254- role_assignment = client.get_role_assignment(role_scope , role_assignment.name)
254+ role_assignment = client.get_role_assignment(KeyVaultRoleScope.GLOBAL , role_assignment.name)
255255print(role_assignment.name)
256256print(role_assignment.principal_id)
257257print(role_assignment.role_definition_id)
258258
259259# finally, we delete this role assignment
260- role_assignment = client.delete_role_assignment(role_scope , role_assignment.name)
260+ role_assignment = client.delete_role_assignment(KeyVaultRoleScope.GLOBAL , role_assignment.name)
261261print(role_assignment.name)
262262print(role_assignment.principal_id)
263263print(role_assignment.role_definition_id)
@@ -280,13 +280,13 @@ client = KeyVaultBackupClient(vault_url="https://my-managed-hsm-name.managedhsm.
280280blob_storage_url = " <your-blob-storage-url>"
281281sas_token = " <your-sas-token>" # replace with a sas token to your storage account
282282
283- # performing a full key backup is a long-running operation. Calling result() on the poller will wait
284- # until the backup is completed, then return an object representing the backup operation.
285- backup_operation = client.begin_backup(blob_storage_url, sas_token).result()
283+ # Backup is a long-running operation. The client returns a poller object whose result() method
284+ # blocks until the backup is complete, then returns an object representing the backup operation.
285+ backup_poller = client.begin_backup(blob_storage_url, sas_token)
286+ backup_operation = backup_poller.result()
286287
288+ # this is the Azure Storage Blob URL of the backup
287289print(backup_operation.folder_url)
288- print(backup_operation.status)
289- print(backup_operation.job_id)
290290` ` `
291291
292292
@@ -309,12 +309,10 @@ sas_token = "<your-sas-token>" # replace with a sas token to your storage accou
309309# URL to a storage blob, for example https://<account name>.blob.core.windows.net/backup/mhsm-account-2020090117323313
310310blob_url = "<your-blob-url>"
311311
312- # performing a full key restore is a long-running operation. Calling ` result()` on the poller will wait
313- # until the restore is completed, then return an object representing the restore operation.
314- restore_operation = client.begin_restore(blob_url, sas_token).result ()
315-
316- print(restore_operation.status)
317- print(restore_operation.job_id)
312+ # Restore is a long-running operation. The client returns a poller object whose wait() method
313+ # blocks until the restore is complete.
314+ restore_poller = client.begin_restore(blob_url, sas_token)
315+ restore_poller.wait()
318316` ` `
319317
320318# # Troubleshooting
0 commit comments