Azure Key Vault Administration Client Library for Python

4.6.0 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

This quickstart demonstrates how to authenticate with Azure Key Vault Managed HSM using `DefaultAzureCredential` and then list role definitions using the `KeyVaultAccessControlClient`. It requires setting the `KEY_VAULT_MANAGED_HSM_URL` environment variable to your Managed HSM's URL. For local development, ensure you are logged in via Azure CLI (`az login`) and have sufficient permissions (e.g., 'Managed HSM Administrator' role).

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.")

view raw JSON →