Skip to content

Commit 5c50bdf

Browse files
authored
[Key Vault] Update administration README for MHSM and custom role definitions (Azure#16307)
1 parent d43aff9 commit 5c50bdf

File tree

1 file changed

+76
-23
lines changed
  • sdk/keyvault/azure-keyvault-administration

1 file changed

+76
-23
lines changed

sdk/keyvault/azure-keyvault-administration/README.md

Lines changed: 76 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
# Azure KeyVault Administration client library for Python
1+
# Azure Key Vault Administration client library for Python
2+
3+
>**Note:** The Administration library only works with [Managed HSM][managed_hsm] – functions targeting a Key Vault will fail.
4+
25
Azure Key Vault helps solve the following problems:
36
- Vault administration (this library) - role-based access control (RBAC), and vault-level backup and restore options
47
- Cryptographic key management ([azure-keyvault-keys](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/keyvault/azure-keyvault-keys)) - create, store, and control
@@ -24,7 +27,7 @@ authentication as demonstrated below.
2427
### Prerequisites
2528
* An [Azure subscription][azure_sub]
2629
* Python 2.7, 3.5.3, or later
27-
* A Key Vault. If you need to create one, See the final two steps in the next section for details on creating the Key Vault with the Azure CLI.
30+
* A [managed HSM][managed_hsm]. If you need to create one, see the final two steps in the next section for details on creating the managed HSM with the Azure CLI.
2831

2932
### Authenticate the client
3033
This document demonstrates using [DefaultAzureCredential][default_cred_ref]
@@ -74,14 +77,23 @@ a more appropriate name for your service principal.
7477
export AZURE_TENANT_ID="tenant id"
7578
```
7679
77-
* Create the Key Vault and grant the above mentioned application authorization to perform administrative operations on the Azure Key Vault (replace `<your-resource-group-name>` and `<your-key-vault-name>` with your own, unique names and `<your-service-principal-object-id>` with the value from above):
80+
* Create the managed HSM and grant the above mentioned application authorization to perform administrative operations on the managed HSM (replace `<your-resource-group-name>` and `<your-managed-hsm-name>` with your own, unique names and `<your-service-principal-object-id>` with the value from above):
7881
```Bash
79-
az keyvault create --hsm-name <your-key-vault-name> --resource-group <your-resource-group-name> --administrators <your-service-principal-object-id> --location <your-azure-location>
82+
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>"
83+
```
84+
85+
* 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:
86+
> **Important:** Create and store the RSA key pairs and security domain file generated in this step securely.
87+
```Bash
88+
openssl req -newkey rsa:2048 -nodes -keyout cert_0.key -x509 -days 365 -out cert_0.cer
89+
openssl req -newkey rsa:2048 -nodes -keyout cert_1.key -x509 -days 365 -out cert_1.cer
90+
openssl req -newkey rsa:2048 -nodes -keyout cert_2.key -x509 -days 365 -out cert_2.cer
91+
az keyvault security-domain download --hsm-name "<your-managed-hsm-name>" --sd-wrapping-keys ./certs/cert_0.cer ./certs/cert_1.cer ./certs/cert_2.cer --sd-quorum 2 --security-domain-file <your-managed-hsm-name>-SD.json
8092
```
8193
82-
* Use the above mentioned Azure Key Vault name to retrieve details of your Vault which also contains your Azure Key Vault URL:
94+
* Use the above mentioned managed HSM name to retrieve details of your managed HSM instance which also contains your manged HSM URL (`hsmUri`):
8395
```Bash
84-
az keyvault show --hsm-name <your-key-vault-name>
96+
az keyvault show --hsm-name "<your-managed-hsm-name>"
8597
```
8698
8799
#### Create a client
@@ -90,10 +102,9 @@ Once the **AZURE_CLIENT_ID**, **AZURE_CLIENT_SECRET** and
90102
[DefaultAzureCredential][default_cred_ref] will be able to authenticate the
91103
clients.
92104
93-
There are two clients available in this package, below are snippets demonstrating how to construct
94-
each one of these clients. Constructing a client also requires your vault's URL, which you can
95-
get from the Azure CLI or the Azure Portal. In the Azure Portal, this URL is
96-
the vault's "DNS Name".
105+
There are two clients available in this package – below are snippets demonstrating how to construct
106+
each one of these clients. Constructing a client also requires your managed HSM's URL, which you can
107+
get from the Azure CLI (shown above).
97108
98109
##### Create a KeyVaultAccessControlClient
99110
```python
@@ -102,7 +113,7 @@ from azure.keyvault.administration import KeyVaultAccessControlClient
102113
103114
credential = DefaultAzureCredential()
104115
105-
client = KeyVaultAccessControlClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
116+
client = KeyVaultAccessControlClient(vault_url="https://my-managed-hsm-name.managedhsm.azure.net/", credential=credential)
106117
```
107118
108119
##### Create a KeyVaultBackupClient
@@ -112,7 +123,7 @@ from azure.keyvault.administration import KeyVaultBackupClient
112123
113124
credential = DefaultAzureCredential()
114125
115-
client = KeyVaultBackupClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
126+
client = KeyVaultBackupClient(vault_url="https://my-managed-hsm-name.managedhsm.azure.net/", credential=credential)
116127
```
117128
118129
## Key concepts
@@ -135,6 +146,7 @@ A `KeyVaultBackupClient` performs full key backups, full key restores, and selec
135146
This section conntains code snippets covering common tasks:
136147
* Access Control
137148
* [List all role definitions](#list-all-role-definitions "List all role definitions")
149+
* [Set, Get, and Delete a role definition](#set-get-and-delete-a-role-defintion "Set, Get, and Delete a role definition")
138150
* [List all role assignments](#list-all-role-assignments "List all role assignments")
139151
* [Create, Get, and Delete a role assignment](#create-get-and-delete-a-role-assignment "Create, Get, and Delete a role assignment")
140152
* Backup and Restore
@@ -150,30 +162,66 @@ from azure.keyvault.administration import KeyVaultAccessControlClient, KeyVaultR
150162
151163
credential = DefaultAzureCredential()
152164
153-
client = KeyVaultAccessControlClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
165+
client = KeyVaultAccessControlClient(vault_url="https://my-managed-hsm-name.managedhsm.azure.net/", credential=credential)
154166
155167
# this will list all role definitions available for assignment
156-
role_definitions = client.list_role_definitions(role_scope=KeyVaultRoleScope.global_value)
168+
role_definitions = client.list_role_definitions(role_scope=KeyVaultRoleScope.GLOBAL)
157169
158170
for role_definition in role_definitions:
159171
print(role_definition.id)
160172
print(role_definition.role_name)
161173
print(role_definition.description)
162174
```
163175
176+
### Set, Get, and Delete a role definition
177+
178+
`set_role_definition` can be used to either create a custom role definition or update an existing definition with the specified name.
179+
180+
```python
181+
import uuid
182+
from azure.identity import DefaultAzureCredential
183+
from azure.keyvault.administration import KeyVaultAccessControlClient, KeyVaultDataAction, KeyVaultPermission
184+
185+
credential = DefaultAzureCredential()
186+
187+
client = KeyVaultAccessControlClient(vault_url="https://my-managed-hsm-name.managedhsm.azure.net/", credential=credential)
188+
189+
# create the custom role definition
190+
role_scope = "/" # the global scope
191+
definition_name = uuid.uuid4()
192+
permissions = [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+
197+
# update the custom role definition
198+
permissions = [
199+
KeyVaultPermission(allowed_data_actions=[], denied_data_actions=[KeyVaultDataAction.READ_HSM_KEY])
200+
]
201+
updated_definition = client.set_role_definition(
202+
role_scope=role_scope, permissions=permissions, role_definition_name=definition_name
203+
)
204+
205+
# get the custom role definition
206+
definition = client.get_role_definition(role_scope=role_scope, role_definition_name=definition_name)
207+
208+
# delete the custom role definition
209+
deleted_definition = client.delete_role_definition(role_scope=role_scope, role_definition_name=definition_name)
210+
```
211+
164212
### List all role assignments
165-
Before creating a new role assignment in the [next snippet](#create-get-and-delete-a-role-assignment), list all of the current role assignments
213+
Before creating a new role assignment in the [next snippet](#create-get-and-delete-a-role-assignment), list all of the current role assignments:
166214
167215
```python
168216
from azure.identity import DefaultAzureCredential
169217
from azure.keyvault.administration import KeyVaultAccessControlClient, KeyVaultRoleScope
170218
171219
credential = DefaultAzureCredential()
172220
173-
client = KeyVaultAccessControlClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
221+
client = KeyVaultAccessControlClient(vault_url="https://my-managed-hsm-name.managedhsm.azure.net/", credential=credential)
174222
175223
# this will list all role assignments
176-
role_assignments = client.list_role_assignments(role_scope=KeyVaultRoleScope.global_value)
224+
role_assignments = client.list_role_assignments(role_scope=KeyVaultRoleScope.GLOBAL)
177225
178226
for role_assignment in role_assignments:
179227
print(role_assignment.name)
@@ -182,15 +230,15 @@ for role_assignment in role_assignments:
182230
```
183231
184232
### Create, Get, and Delete a role assignment
185-
Assign 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)
233+
Assign 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.
186234
187235
```python
188236
from azure.identity import DefaultAzureCredential
189237
from azure.keyvault.administration import KeyVaultAccessControlClient
190238
191239
credential = DefaultAzureCredential()
192240
193-
client = KeyVaultAccessControlClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
241+
client = KeyVaultAccessControlClient(vault_url="https://my-managed-hsm-name.managedhsm.azure.net/", credential=credential)
194242
195243
role_scope = "/" # the global scope
196244
role_definition_id = "<role-definition-id>" # Replace <role-definition-id> with the id of a definition returned from the previous example
@@ -226,7 +274,7 @@ from azure.identity import DefaultAzureCredential
226274
from azure.keyvault.administration import KeyVaultBackupClient
227275
228276
credential = DefaultAzureCredential()
229-
client = KeyVaultBackupClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
277+
client = KeyVaultBackupClient(vault_url="https://my-managed-hsm-name.managedhsm.azure.net/", credential=credential)
230278
231279
# blob storage container URL, for example https://<account name>.blob.core.windows.net/backup
232280
blob_storage_url = "<your-blob-storage-url>"
@@ -254,7 +302,7 @@ from azure.identity import DefaultAzureCredential
254302
from azure.keyvault.administration import KeyVaultBackupClient
255303
256304
credential = DefaultAzureCredential()
257-
client = KeyVaultBackupClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
305+
client = KeyVaultBackupClient(vault_url="https://my-managed-hsm-name.managedhsm.azure.net/", credential=credential)
258306
259307
sas_token = "<your-sas-token>" # replace with a sas token to your storage account
260308
@@ -281,14 +329,16 @@ from azure.keyvault.administration import KeyVaultAccessControlClient
281329
from azure.core.exceptions import ResourceNotFoundError
282330
283331
credential = DefaultAzureCredential()
284-
client = KeyVaultAccessControlClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
332+
client = KeyVaultAccessControlClient(vault_url="https://my-managed-hsm-name.managedhsm.azure.net/", credential=credential)
285333
286334
try:
287335
client.get_role_assignment("/", "which-does-not-exist")
288336
except ResourceNotFoundError as e:
289337
print(e.message)
290338
```
291339
340+
Clients from the Administration library can only be used to perform operations on a managed HSM, so attempting to do so on a Key Vault will raise an error.
341+
292342
## Next steps
293343
294344
Content forthcoming
@@ -297,6 +347,8 @@ Content forthcoming
297347
For more extensive documentation on Azure Key Vault, see the
298348
[API reference documentation][reference_docs].
299349
350+
For more extensive documentation on Managed HSM, see the [service documentation][managed_hsm].
351+
300352
## Contributing
301353
This project welcomes contributions and suggestions. Most contributions require
302354
you to agree to a Contributor License Agreement (CLA) declaring that you have
@@ -319,9 +371,10 @@ contact opencode@microsoft.com with any additional questions or comments.
319371
[azure_identity]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/identity/azure-identity
320372
[azure_identity_pypi]: https://pypi.org/project/azure-identity/
321373
[azure_sub]: https://azure.microsoft.com/free/
322-
[default_cred_ref]: https://aka.ms/azsdk/python/identity/docs#azure.identity.DefaultAzureCredential
323374
[code_of_conduct]: https://opensource.microsoft.com/codeofconduct/
375+
[default_cred_ref]: https://aka.ms/azsdk/python/identity/docs#azure.identity.DefaultAzureCredential
324376
[keyvault_docs]: https://docs.microsoft.com/azure/key-vault/
377+
[managed_hsm]: https://docs.microsoft.com/azure/key-vault/managed-hsm/
325378
[pip]: https://pypi.org/project/pip/
326379
[pypi_package_administration]: https://aka.ms/azsdk/python/keyvault-administration/pypi
327380
[reference_docs]: https://aka.ms/azsdk/python/keyvault-administration/docs

0 commit comments

Comments
 (0)