Registry / azure / azure-keyvault

azure-keyvault

JSON →
library4.2.0pypypi✓ verified 25d ago

The `azure-keyvault` library provides client access to Azure Key Vault, a cloud service for securely storing and accessing secrets, keys, and certificates. It offers distinct clients for managing each resource type within the unified `azure-keyvault` umbrella package. As part of the Azure SDK for Python (Track 2), it integrates with `azure-identity` for authentication. The current stable version is 4.2.0, with minor updates typically released on a bi-annual basis.

pip install azure-keyvault
INSTALL
IMPORT
SIG · AZURE-KEYVAULT
A
azure-keyvault
azurepythonv4.2.0
Install
3.9s avg
Import
455ms
Disk
44MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v4.2.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.484s · 45.2MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 3.9s · import 0.425s · 46MB
44MB installed
● package 44MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

SecretClient
from azure.keyvault.secrets import SecretClient
from azure.keyvault import SecretClient
Clients are modularized into `secrets`, `keys`, and `certificates` sub-packages in Track 2 SDKs.
KeyClient
from azure.keyvault.keys import KeyClient
from azure.keyvault import KeyClient
Clients are modularized into `secrets`, `keys`, and `certificates` sub-packages in Track 2 SDKs.
CertificateClient
from azure.keyvault.certificates import CertificateClient
from azure.keyvault import CertificateClient
Clients are modularized into `secrets`, `keys`, and `certificates` sub-packages in Track 2 SDKs.
DefaultAzureCredential
from azure.identity import DefaultAzureCredential

This quickstart demonstrates how to authenticate with Azure Key Vault using `DefaultAzureCredential` and perform basic secret operations: setting, getting, and deleting a secret. Ensure your environment is configured for Azure authentication and you have sufficient permissions on the Key Vault.

import os from azure.keyvault.secrets import SecretClient from azure.identity import DefaultAzureCredential # For authentication, ensure you have set up environment variables or Azure CLI login. # For local development, DefaultAzureCredential will try: # 1. Environment variables (AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET) # 2. Managed Identity # 3. Azure CLI (e.g., `az login`) # 4. Azure Developer CLI # 5. Visual Studio Code # Get your Key Vault URL from environment variable or replace with your actual URL key_vault_url = os.environ.get("AZURE_KEYVAULT_URL", "https://your-key-vault-name.vault.azure.net/") if not key_vault_url: raise ValueError("AZURE_KEYVAULT_URL environment variable or explicit URL is required.") # Authenticate using DefaultAzureCredential credential = DefaultAzureCredential() # Create a SecretClient secret_client = SecretClient(vault_url=key_vault_url, credential=credential) secret_name = "MyTestSecret" secret_value = "HelloFromPythonSDK" try: print(f"Setting secret '{secret_name}'...") set_secret = secret_client.set_secret(secret_name, secret_value) print(f"Secret set: Name={set_secret.name}, Value={set_secret.value}") print(f"Getting secret '{secret_name}'...") retrieved_secret = secret_client.get_secret(secret_name) print(f"Secret retrieved: Name={retrieved_secret.name}, Value={retrieved_secret.value}") print(f"Deleting secret '{secret_name}'...") # Poller for long-running operation, often involved in deletion poller = secret_client.begin_delete_secret(secret_name) deleted_secret = poller.result() # Wait for deletion to complete print(f"Secret deleted: Name={deleted_secret.name}") except Exception as e: print(f"An error occurred: {e}") print("Ensure you have set AZURE_KEYVAULT_URL and authenticated (e.g., via `az login`).") print("Also ensure the authenticated principal has 'Get', 'Set', and 'Delete' secret permissions on the Key Vault.")
Debug
Known issues
breakingThe `azure-keyvault` library (version 4.x) is part of the 'Track 2' Azure SDK for Python. This introduced a complete redesign of the API surface compared to older 'Track 1' libraries (e.g., `azure-keyvault-secrets` < 4.0). Client constructors, method names, and return types are fundamentally different.
fix
Refer to the official Azure SDK documentation for migration guides. Update import statements, client instantiation, and method calls to the new Track 2 patterns. For example, `VaultClient` is replaced by `SecretClient`, `KeyClient`, `CertificateClient`.
affects: <4.0.0
gotchaAzure Key Vault clients are modular. While `azure-keyvault` is a meta-package, you instantiate `SecretClient`, `KeyClient`, and `CertificateClient` from their respective sub-packages (`azure.keyvault.secrets`, `azure.keyvault.keys`, `azure.keyvault.certificates`).
fix
Always import clients from their specific sub-modules: `from azure.keyvault.secrets import SecretClient` etc. Do not attempt to import them directly from `azure.keyvault`.
affects: >=4.0.0
gotchaAuthentication is handled by the `azure-identity` library. Misconfiguration of credentials (e.g., missing environment variables, unauthenticated Azure CLI session) is a common initial hurdle, leading to `ClientAuthenticationError`.
fix
Ensure `azure-identity` is installed and correctly configured. For local development, `DefaultAzureCredential` relies on environment variables (`AZURE_TENANT_ID`, `AZURE_CLIENT_ID`, `AZURE_CLIENT_SECRET`) or an active `az login` session.
affects: >=4.0.0
gotchaAll Key Vault client constructors (e.g., `SecretClient`, `KeyClient`) require the `vault_url` parameter, which specifies the URI of your Azure Key Vault instance. This URL typically follows the pattern `https://<your-key-vault-name>.vault.azure.net/`.
fix
Always provide the correct and complete `vault_url` when instantiating any Key Vault client. It's often sourced from an environment variable or Azure resource properties.
affects: >=4.0.0
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'azure.keyvault.secrets'
The specific client library for Key Vault secrets (`azure-keyvault-secrets`) has not been installed, or an outdated import from the deprecated `azure-keyvault` package is being used.
fix
Install the correct package using `pip install azure-keyvault-secrets` and ensure your import statements are updated to `from azure.keyvault.secrets import SecretClient`.
ClientAuthenticationError: DefaultAzureCredential failed to retrieve a token from the included credentials.
The `DefaultAzureCredential` from `azure-identity` is unable to find valid authentication credentials in the current environment, often due to missing environment variables, an expired Azure CLI token, or a misconfigured managed identity.
fix
Ensure you are logged into Azure CLI (`az login`), that required environment variables for authentication (e.g., `AZURE_CLIENT_ID`, `AZURE_CLIENT_SECRET`, `AZURE_TENANT_ID` for Service Principal) are set, or that the executing Azure resource has a Managed Identity with the appropriate permissions.
(Forbidden)AKV10032: Invalid permission
The authenticated identity (user, service principal, or managed identity) lacks the necessary access policies or Azure RBAC role assignments on the Azure Key Vault to perform the requested operation (e.g., 'Get Secret', 'Set Secret').
fix
Assign the correct access policies (e.g., 'Get', 'List', 'Set' under 'Secret Management') to the identity in the Azure Key Vault's Access Policies blade, or assign a suitable Azure RBAC role like 'Key Vault Secrets User'.
AttributeError: 'KeyVaultManagementClient' object has no attribute 'get_secret'
Attempting to perform data plane operations (like getting secrets) using a client from the management plane (`azure.mgmt.keyvault.KeyVaultManagementClient`) instead of the dedicated data plane client.
fix
Use `SecretClient` from `azure.keyvault.secrets` for managing secrets, `KeyClient` from `azure.keyvault.keys` for keys, and `CertificateClient` from `azure.keyvault.certificates` for certificates.
Upgrade
Version history
4.2.0latest on PyPI · released Mar 29, 2022
Audit
Dependencies
azure-identityrequiredRequired for authenticating with Azure services, following the standard Azure SDK for Python authentication pattern.
Agent activity
27 hits · last 30 days
node
22
OpenAI (training)
1
Resources