Give AlbumentationsX a star on GitHub — it powers this leaderboard

Star on GitHub

azure-keyvault-secrets

Microsoft Corporation Key Vault Secrets Client Library for Python

Downloads: 0 (30 days)

Description

Azure Key Vault Secrets client library for Python

Azure Key Vault helps solve the following problems:

  • Secrets management (this library) - securely store and control access to tokens, passwords, certificates, API keys, and other secrets
  • Cryptographic key management (azure-keyvault-keys) - create, store, and control access to the keys used to encrypt your data
  • Certificate management (azure-keyvault-certificates) - create, manage, and deploy public and private SSL/TLS certificates
  • Vault administration (azure-keyvault-administration) - role-based access control (RBAC), and vault-level backup and restore options

[Source code][library_src] | [Package (PyPI)][pypi_package_secrets] | Package (Conda) | [API reference documentation][reference_docs] | [Product documentation][azure_keyvault] | [Samples][secret_samples]

Disclaimer

Azure SDK Python packages support for Python 2.7 has ended 01 January 2022. For more information and questions, please refer to https://github.com/Azure/azure-sdk-for-python/issues/20691. Python 3.9 or later is required to use this package. For more details, please refer to Azure SDK for Python version support policy.

Getting started

Install packages

Install [azure-keyvault-secrets][pypi_package_secrets] and [azure-identity][azure_identity_pypi] with [pip][pip]:

pip install azure-keyvault-secrets azure-identity

[azure-identity][azure_identity] is used for Azure Active Directory authentication as demonstrated below.

Prerequisites

  • An [Azure subscription][azure_sub]
  • Python 3.9 or later
  • An existing [Azure Key Vault][azure_keyvault]. If you need to create one, you can do so using the Azure CLI by following the steps in [this document][azure_keyvault_cli].

Authenticate the client

In order to interact with the Azure Key Vault service, you will need an instance of a [SecretClient][secret_client_docs], as well as a vault url and a credential object. This document demonstrates using a [DefaultAzureCredential][default_cred_ref], which is appropriate for most scenarios, including local development and production environments. We recommend using a [managed identity][managed_identity] for authentication in production environments.

See [azure-identity][azure_identity] documentation for more information about other methods of authentication and their corresponding credential types.

Create a client

After configuring your environment for the [DefaultAzureCredential][default_cred_ref] to use a suitable method of authentication, you can do the following to create a secret client (replacing the value of VAULT_URL with your vault's URL):

<!-- SNIPPET:hello_world.create_secret_client -->
VAULT_URL = os.environ["VAULT_URL"]
credential = DefaultAzureCredential()
client = SecretClient(vault_url=VAULT_URL, credential=credential)
<!-- END SNIPPET -->

NOTE: For an asynchronous client, import azure.keyvault.secrets.aio's SecretClient instead.

Key concepts

Secret

A secret consists of a secret value and its associated metadata and management information. This library handles secret values as strings, but Azure Key Vault doesn't store them as such. For more information about secrets and how Key Vault stores and manages them, see the Key Vault documentation.

[SecretClient][secret_client_docs] can set secret values in the vault, update secret metadata, and delete secrets, as shown in the examples below.

Examples

This section contains code snippets covering common tasks:

Set a secret

set_secret creates new secrets and changes the values of existing secrets. If no secret with the given name exists, set_secret creates a new secret with that name and the given value. If the given name is in use, set_secret creates a new version of that secret, with the given value.

from azure.identity import DefaultAzureCredential
from azure.keyvault.secrets import SecretClient

credential = DefaultAzureCredential()

secret_client = SecretClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
secret = secret_client.set_secret("secret-name", "secret-value")

print(secret.name)
print(secret.value)
print(secret.properties.version)

Retrieve a secret

get_secret retrieves a secret previously stored in the Key Vault.

from azure.identity import DefaultAzureCredential
from azure.keyvault.secrets import SecretClient

credential = DefaultAzureCredential()

secret_client = SecretClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
secret = secret_client.get_secret("secret-name")

print(secret.name)
print(secret.value)

Update secret metadata

update_secret_properties updates a secret's metadata. It cannot change the secret's value; use set_secret to set a secret's value.

from azure.identity import DefaultAzureCredential
from azure.keyvault.secrets import SecretClient

credential = DefaultAzureCredential()

secret_client = SecretClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)

# Clients may specify the content type of a secret to assist in interpreting the secret data when it's retrieved
content_type = "text/plain"

# We will also disable the secret for further use

updated_secret_properties = secret_client.update_secret_properties("secret-name", content_type=content_type, enabled=False)

print(updated_secret_properties.updated_on)
print(updated_secret_properties.content_type)
print(updated_secret_properties.enabled)

Delete a secret

begin_delete_secret requests Key Vault delete a secret, returning a poller which allows you to wait for the deletion to finish. Waiting is helpful when the vault has [soft-delete][soft_delete] enabled, and you want to purge (permanently delete) the secret as soon as possible. When [soft-delete][soft_delete] is disabled, begin_delete_secret itself is permanent.

from azure.identity import DefaultAzureCredential
from azure.keyvault.secrets import SecretClient

credential = DefaultAzureCredential()

secret_client = SecretClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
deleted_secret = secret_client.begin_delete_secret("secret-name").result()

print(deleted_secret.name)
print(deleted_secret.deleted_date)

List secrets

list_properties_of_secrets lists the properties of all of the secrets in the client's vault. This list doesn't include the secret's values.

from azure.identity import DefaultAzureCredential
from azure.keyvault.secrets import SecretClient

credential = DefaultAzureCredential()

secret_client = SecretClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
secret_properties = secret_client.list_properties_of_secrets()

for secret_property in secret_properties:
    # the list doesn't include values or versions of the secrets
    print(secret_property.name)

Async API

This library includes a complete set of async APIs. To use them, you must first install an async transport, such as aiohttp. See azure-core documentation for more information.

Async clients and credentials should be closed when they're no longer needed. These objects are async context managers and define async close methods. For example:

from azure.identity.aio import DefaultAzureCredential
from azure.keyvault.secrets.aio import SecretClient

credential = DefaultAzureCredential()

# call close when the client and credential are no longer needed
client = SecretClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
...
await client.close()
await credential.close()

# alternatively, use them as async context managers (contextlib.AsyncExitStack can help)
client = SecretClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
async with client:
  async with credential:
    ...

Asynchronously create a secret

set_secret creates a secret in the Key Vault with the specified optional arguments.

from azure.identity.aio import DefaultAzureCredential
from azure.keyvault.secrets.aio import SecretClient

credential = DefaultAzureCredential()
secret_client = SecretClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)

secret = await secret_c