Azure Key Vault Administration Client Library for Python
The `azure-keyvault-administration` client library for Python provides functionalities for managing Azure Key Vault Managed HSMs, including role-based access control (RBAC) and vault-level backup and restore operations. It is part of the Azure SDK for Python, which follows a regular release cadence, and is currently at version 4.6.0. This library is specifically designed for Managed HSMs and will not work with standard Azure Key Vaults.
Warnings
- breaking This library is exclusively for Azure Key Vault Managed HSM. It will NOT work with standard Azure Key Vault instances. Attempting to use it with a standard Key Vault will result in errors.
- gotcha Authentication requires proper setup of `DefaultAzureCredential`. This often means logging in via Azure CLI (`az login`), setting specific environment variables (e.g., `AZURE_CLIENT_ID`, `AZURE_CLIENT_SECRET`, `AZURE_TENANT_ID`), or configuring managed identities in Azure. Incorrect authentication setup is a common source of errors.
- gotcha Operations on Managed HSMs require specific Azure RBAC permissions. Simply having contributor access on the resource group is often insufficient. For administration tasks like managing role definitions or performing backups, roles like 'Managed HSM Administrator' or 'Managed HSM Crypto User' are typically required.
- gotcha The library provides both synchronous and asynchronous (async) clients. Asynchronous clients are located in the `azure.keyvault.administration.aio` namespace (e.g., `azure.keyvault.administration.aio.KeyVaultAccessControlClient`). Mixing synchronous and asynchronous client imports or usage can lead to unexpected behavior or runtime errors if not handled correctly within an async event loop.
- breaking Python 3.9 or later is required for this package. Older Python versions (3.8 and below) are no longer supported by the Azure SDK for Python.
Install
-
pip install azure-keyvault-administration azure-identity
Imports
- KeyVaultAccessControlClient
from azure.keyvault.administration import KeyVaultAccessControlClient
- KeyVaultBackupClient
from azure.keyvault.administration import KeyVaultBackupClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
- KeyVaultRoleScope
from azure.keyvault.administration import KeyVaultRoleScope
- KeyVaultAccessControlClient
from azure.keyvault.administration.aio import KeyVaultAccessControlClient
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.keyvault.administration import KeyVaultAccessControlClient
# Set these environment variables or ensure DefaultAzureCredential can find them
# For local development, 'az login' is often sufficient.
# KEY_VAULT_MANAGED_HSM_URL should be in the format: "https://<your-hsm-name>.managedhsm.azure.net"
hsm_url = os.environ.get("KEY_VAULT_MANAGED_HSM_URL", "<your-hsm-url>")
if hsm_url == "<your-hsm-url>":
raise ValueError("Please set the KEY_VAULT_MANAGED_HSM_URL environment variable.")
try:
# Authenticate using DefaultAzureCredential
credential = DefaultAzureCredential()
# Create a KeyVaultAccessControlClient
access_control_client = KeyVaultAccessControlClient(vault_url=hsm_url, credential=credential)
print(f"Listing role definitions for {hsm_url}...")
role_definitions = access_control_client.list_role_definitions()
for role_def in role_definitions:
print(f" Role Name: {role_def.role_name}, ID: {role_def.id}")
print("Successfully listed role definitions.")
except Exception as e:
print(f"An error occurred: {e}")
print("Ensure that your environment is authenticated (e.g., via 'az login') ")
print("and the service principal/user has 'Managed HSM Administrator' or equivalent RBAC role on the HSM.")