Azure Key Vault Management Client
Microsoft Azure Keyvault Management Client Library for Python. It provides an interface to manage Azure Key Vault resources, such as creating, deleting, and updating vaults, and configuring access policies. Current version is 14.0.1. Releases follow the Azure SDK for Python's frequent cadence, often coinciding with new API versions or bug fixes.
Warnings
- breaking Version 14.0.0 introduced breaking changes, specifically regarding how `api_version` is handled and changes to the default models namespace. `VaultsOperations` methods no longer accept keyword arguments for `api_version`.
- gotcha This library (`azure-mgmt-keyvault`) is for *managing* Key Vault resources (create, delete, update policies). It is NOT for interacting with secrets, keys, or certificates *inside* a vault. For data plane operations, use `azure-keyvault-secrets`, `azure-keyvault-keys`, or `azure-keyvault-certificates`.
- gotcha Authentication with `DefaultAzureCredential` relies on a chain of authentication methods. Misconfigured environment variables or lack of `az login` can lead to authentication failures.
Install
-
pip install azure-mgmt-keyvault azure-identity
Imports
- KeyVaultManagementClient
from azure.mgmt.keyvault import KeyVaultManagementClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.keyvault import KeyVaultManagementClient
# --- Authentication ---
# The DefaultAzureCredential attempts to authenticate via several methods,
# including environment variables, managed identity, Azure CLI, and more.
# For local development, set these environment variables or ensure 'az login' is active:
# - AZURE_SUBSCRIPTION_ID (required)
# - AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, AZURE_TENANT_ID for service principal
# - AZURE_USERNAME, AZURE_PASSWORD for developer accounts
subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID")
if not subscription_id:
raise ValueError("AZURE_SUBSCRIPTION_ID environment variable must be set.")
credential = DefaultAzureCredential()
# --- Client Initialization ---
client = KeyVaultManagementClient(credential, subscription_id)
# --- Example: List all Key Vaults in the subscription ---
print(f"Listing all Key Vaults in subscription: {subscription_id}")
try:
vaults_iterator = client.vaults.list()
found_vaults = False
for vault in vaults_iterator:
print(f" - Vault Name: {vault.name}, Location: {vault.location}")
found_vaults = True
if not found_vaults:
print(" No Key Vaults found.")
except Exception as e:
print(f"Error listing vaults: {e}")
print("Ensure your credential has the 'Microsoft.KeyVault/vaults/read' permission at the subscription scope.")