|
| 1 | +# Azure Key Vault Security Domain client library for Python |
| 2 | + |
| 3 | +Azure Key Vault helps solve the following problems: |
| 4 | + |
| 5 | +- Managed HSM security domain management (this library) - securely download and restore a managed HSM's security domain |
| 6 | +- Cryptographic key management ([azure-keyvault-keys](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/keyvault/azure-keyvault-keys))- create, store, and control |
| 7 | +access to the keys used to encrypt your data |
| 8 | +- Secrets management |
| 9 | +([azure-keyvault-secrets](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/keyvault/azure-keyvault-secrets)) - |
| 10 | +securely store and control access to tokens, passwords, certificates, API keys, |
| 11 | +and other secrets |
| 12 | +- Certificate management |
| 13 | +([azure-keyvault-certificates](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/keyvault/azure-keyvault-certificates)) - |
| 14 | +create, manage, and deploy public and private SSL/TLS certificates |
| 15 | +- Vault administration ([azure-keyvault-administration](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/keyvault/azure-keyvault-administration)) - role-based access control (RBAC), and vault-level backup and restore options |
| 16 | + |
| 17 | +[Source code][library_src] |
| 18 | +| [Package (PyPI)][pypi_package] |
| 19 | +| [API reference documentation][reference_docs] |
| 20 | +| [Key Vault documentation][azure_keyvault] |
| 21 | +| [Managed HSM documentation][azure_managedhsm] |
| 22 | +| [Samples][samples] |
| 23 | + |
| 24 | +## Getting started |
| 25 | + |
| 26 | +### Install the package |
| 27 | + |
| 28 | +Install [azure-keyvault-securitydomain][pypi_package] and [azure-identity][azure_identity_pypi] with [pip][pip]: |
| 29 | + |
| 30 | +```Bash |
| 31 | +python -m pip install azure-keyvault-securitydomain azure-identity |
| 32 | +``` |
| 33 | + |
| 34 | +[azure-identity][azure_identity] is used for Microsoft Entra ID authentication as demonstrated below. |
| 35 | + |
| 36 | +#### Prequisites |
| 37 | + |
| 38 | +- Python 3.9 or later |
| 39 | +- An [Azure subscription][azure_sub] |
| 40 | +- An existing [Key Vault Managed HSM][azure_managedhsm]. If you need to create a Managed HSM, you can do so using the Azure CLI by following the steps in [this document][managed_hsm_cli]. |
| 41 | + |
| 42 | +### Authenticate the client |
| 43 | + |
| 44 | +In order to interact with the Azure Key Vault service, you will need an instance of a |
| 45 | +[SecurityDomainClient][securitydomain_client_docs], as well as a **vault URL** and a credential object. This document |
| 46 | +demonstrates using a [DefaultAzureCredential][default_cred_ref], which is appropriate for most scenarios. We recommend |
| 47 | +using a [managed identity][managed_identity] for authentication in production environments. |
| 48 | + |
| 49 | +See [azure-identity][azure_identity] documentation for more information about other methods of authentication and their |
| 50 | +corresponding credential types. |
| 51 | + |
| 52 | +#### Create a client |
| 53 | + |
| 54 | +After configuring your environment for the [DefaultAzureCredential][default_cred_ref] to use a suitable method of |
| 55 | +authentication, you can do the following to create a security domain client (replacing the value of `VAULT_URL` with |
| 56 | +your vault's URL): |
| 57 | + |
| 58 | +<!-- SNIPPET:hello_world.create_a_security_domain_client --> |
| 59 | + |
| 60 | +```python |
| 61 | +from azure.identity import DefaultAzureCredential |
| 62 | +from azure.keyvault.securitydomain import SecurityDomainClient |
| 63 | + |
| 64 | +VAULT_URL = os.environ["VAULT_URL"] |
| 65 | +credential = DefaultAzureCredential() |
| 66 | +client = SecurityDomainClient(vault_url=VAULT_URL, credential=credential) |
| 67 | +``` |
| 68 | + |
| 69 | +<!-- END SNIPPET --> |
| 70 | + |
| 71 | +> **NOTE:** For an asynchronous client, import `azure.keyvault.securitydomain.aio`'s `SecurityDomainClient` instead. |
| 72 | +
|
| 73 | +## Key concepts |
| 74 | + |
| 75 | +### Security domain |
| 76 | + |
| 77 | +To operate, a managed HSM must have a security domain. The security domain is an encrypted blob file that contains |
| 78 | +artifacts like the HSM backup, user credentials, the signing key, and the data encryption key that's unique to the |
| 79 | +managed HSM. For more information, please see [service documentation][securitydomain_docs]. |
| 80 | + |
| 81 | +### SecurityDomainClient |
| 82 | + |
| 83 | +A `SecurityDomainClient` can download and upload managed HSM security domains and get transfer keys. |
| 84 | + |
| 85 | +### Download operation |
| 86 | + |
| 87 | +A download operation retrieves the security domain of a managed HSM. This can be used to activate a provisioned |
| 88 | +managed HSM. |
| 89 | + |
| 90 | +### Upload operation |
| 91 | + |
| 92 | +An upload operation restores a managed HSM using a provided security domain. |
| 93 | + |
| 94 | +### Transfer key |
| 95 | + |
| 96 | +A transfer key, or exchange key, is used to encrypt a security domain before uploading it to a managed HSM. For more |
| 97 | +information, please see the [disaster recovery guide][disaster_recovery]. |
| 98 | + |
| 99 | +## Examples |
| 100 | + |
| 101 | +This section contains code snippets covering common tasks: |
| 102 | + |
| 103 | +- [Download a security domain](#download-a-security-domain) |
| 104 | +- [Get a transfer key](#get-a-transfer-key) |
| 105 | +- [Upload a security domain](#upload-a-security-domain) |
| 106 | + |
| 107 | +### Download a security domain |
| 108 | + |
| 109 | +`begin_download` can be used by a `SecurityDomainClient` to fetch a managed HSM's security domain, and this will also |
| 110 | +activate a provisioned managed HSM. By default, the poller returned by this operation will poll on the managed HSM's |
| 111 | +activation status, finishing when it's activated. To return immediately with the security domain object without waiting |
| 112 | +for activation, you can pass the keyword argument `skip_activation_polling=True`. |
| 113 | + |
| 114 | +```python |
| 115 | +from azure.keyvault.securitydomain.models import SecurityDomain |
| 116 | + |
| 117 | +security_domain: SecurityDomain = client.begin_download(certificate_info=certs_object).result() |
| 118 | +assert security_domain.value |
| 119 | +print("The managed HSM is now active.") |
| 120 | +``` |
| 121 | + |
| 122 | +### Get a transfer key |
| 123 | + |
| 124 | +Using a different managed HSM than the one the security domain was downloaded from, `get_transfer_key` can be used by |
| 125 | +a `SecurityDomainClient` to fetch a transfer key (also known as an exchange key). |
| 126 | + |
| 127 | +```python |
| 128 | +from azure.keyvault.securitydomain.models import TransferKey |
| 129 | + |
| 130 | +NEW_VAULT_URL = os.environ["NEW_VAULT_URL"] |
| 131 | +upload_client = SecurityDomainClient(vault_url=NEW_VAULT_URL, credential=credential) |
| 132 | + |
| 133 | +transfer_key: TransferKey = upload_client.get_transfer_key() |
| 134 | +assert transfer_key.transfer_key_jwk |
| 135 | +``` |
| 136 | + |
| 137 | +### Upload a security domain |
| 138 | + |
| 139 | +`begin_upload` can be used by a `SecurityDomainClient` to restore a different managed HSM with a security domain, for |
| 140 | +example for disaster recovery. Like the download operation this will activate a provisioned managed HSM, but the poller |
| 141 | +will return None if successful (and an error if unsuccessful) instead of the security domain object. |
| 142 | + |
| 143 | +```python |
| 144 | +from azure.keyvault.securitydomain.models import SecurityDomainOperationStatus |
| 145 | + |
| 146 | +result: SecurityDomainOperationStatus = upload_client.begin_upload(security_domain=result).result() |
| 147 | +print("The managed HSM has been successfully restored with the security domain.") |
| 148 | +``` |
| 149 | + |
| 150 | +## Troubleshooting |
| 151 | + |
| 152 | +See the Azure Key Vault SDK's |
| 153 | +[troubleshooting guide](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/keyvault/TROUBLESHOOTING.md) for |
| 154 | +details on how to diagnose various failure scenarios. |
| 155 | + |
| 156 | +## Next steps |
| 157 | +Samples are available in the Azure SDK for Python GitHub repository. These samples provide example code for the |
| 158 | +following scenarios: |
| 159 | + |
| 160 | +- [Download a security domain][hello_world_sample] ([async version][hello_world_async_sample]) |
| 161 | + |
| 162 | +## Contributing |
| 163 | + |
| 164 | +This project welcomes contributions and suggestions. Most contributions require |
| 165 | +you to agree to a Contributor License Agreement (CLA) declaring that you have |
| 166 | +the right to, and actually do, grant us the rights to use your contribution. |
| 167 | +For details, visit https://cla.microsoft.com. |
| 168 | + |
| 169 | +When you submit a pull request, a CLA-bot will automatically determine whether |
| 170 | +you need to provide a CLA and decorate the PR appropriately (e.g., label, |
| 171 | +comment). Simply follow the instructions provided by the bot. You will only |
| 172 | +need to do this once across all repos using our CLA. |
| 173 | + |
| 174 | +This project has adopted the |
| 175 | +[Microsoft Open Source Code of Conduct][code_of_conduct]. For more information, |
| 176 | +see the Code of Conduct FAQ or contact [email protected] with any |
| 177 | +additional questions or comments. |
| 178 | + |
| 179 | +<!-- LINKS --> |
| 180 | +[azure_identity]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity |
| 181 | +[azure_identity_pypi]: https://pypi.org/project/azure-identity/ |
| 182 | +[azure_keyvault]: https://learn.microsoft.com/azure/key-vault/ |
| 183 | +[azure_managedhsm]: https://learn.microsoft.com/azure/key-vault/managed-hsm/ |
| 184 | +[azure_sub]: https://azure.microsoft.com/free/ |
| 185 | + |
| 186 | +[code_of_conduct]: https://opensource.microsoft.com/codeofconduct/ |
| 187 | + |
| 188 | +[default_cred_ref]: https://aka.ms/azsdk/python/identity/docs#azure.identity.DefaultAzureCredential |
| 189 | +[disaster_recovery]: https://learn.microsoft.com/azure/key-vault/managed-hsm/disaster-recovery-guide |
| 190 | + |
| 191 | +[hello_world_sample]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/keyvault/azure-keyvault-securitydomain/samples/hello_world.py |
| 192 | +[hello_world_async_sample]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/keyvault/azure-keyvault-securitydomain/samples/hello_world_async.py |
| 193 | + |
| 194 | +[library_src]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/keyvault/azure-keyvault-securitydomain/azure/keyvault/securitydomain |
| 195 | + |
| 196 | +[managed_hsm_cli]: https://learn.microsoft.com/azure/key-vault/managed-hsm/quick-create-cli |
| 197 | +[managed_identity]: https://learn.microsoft.com/entra/identity/managed-identities-azure-resources/overview |
| 198 | + |
| 199 | +[pip]: https://pypi.org/project/pip/ |
| 200 | +[pypi_package]: https://pypi.org/project/azure-keyvault-securitydomain/ |
| 201 | + |
| 202 | +[reference_docs]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/keyvault/azure-keyvault-securitydomain/azure/keyvault/securitydomain |
| 203 | + |
| 204 | +[samples]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/keyvault/azure-keyvault-securitydomain/samples |
| 205 | +[securitydomain_client_docs]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/keyvault/azure-keyvault-securitydomain/azure/keyvault/securitydomain/_patch.py |
| 206 | +[securitydomain_docs]: https://learn.microsoft.com/azure/key-vault/managed-hsm/security-domain |
0 commit comments