Azure Key Vault Certificates client library for Python

Azure Key Vault helps solve the following problems:

Source code | Package (PyPI) | API reference documentation | Product documentation | Samples

Getting started

Install the package

Install the Azure Key Vault client library for Python with pip:

pip install azure-keyvault-certificates

Prerequisites

  • An Azure subscription

  • Python 2.7, 3.5.3, or later

  • A Key Vault. If you need to create one, you can use the Azure Cloud Shell to create one with these commands (replace "my-resource-group" and "my-key-vault" with your own, unique names):

    • (Optional) if you want a new resource group to hold the Key Vault: .. code-block:: sh

      az group create –name my-resource-group –location westus2

    • Create the Key Vault:

      az keyvault create --resource-group my-resource-group --name my-key-vault
      

      Output:

      {
          "id": "...",
          "location": "westus2",
          "name": "my-key-vault",
          "properties": {
              "accessPolicies": [...],
              "createMode": null,
              "enablePurgeProtection": null,
              "enableSoftDelete": null,
              "enabledForDeployment": false,
              "enabledForDiskEncryption": null,
              "enabledForTemplateDeployment": null,
              "networkAcls": null,
              "provisioningState": "Succeeded",
              "sku": { "name": "standard" },
              "tenantId": "...",
              "vaultUri": "https://my-key-vault.vault.azure.net/"
          },
          "resourceGroup": "my-resource-group",
          "type": "Microsoft.KeyVault/vaults"
      }
      

      The "vaultUri" property is the vault_url used by CertificateClient

Authenticate the client

In order to interact with a Key Vault’s certificates, you’ll need an instance of the CertificateClient class. Creating one requires a vault url and credential. This document demonstrates using DefaultAzureCredential as the credential, authenticating with a service principal’s client id, secret, and tenant id. Other authentication methods are supported. See the azure-identity documentation for more details.

Create a service principal

This Azure Cloud Shell snippet shows how to create a new service principal. Before using it, replace “your-application-name” with a more appropriate name for your service principal.

  • Create a service principal:

       az ad sp create-for-rbac --name http://my-application --skip-assignment
    
    Output:
    
    {
        "appId": "generated app id",
        "displayName": "my-application",
        "name": "http://my-application",
        "password": "random password",
        "tenant": "tenant id"
    }
    
    • Use the output to set AZURE_CLIENT_ID (appId), AZURE_CLIENT_SECRET (password) and AZURE_TENANT_ID (tenant) environment variables. The following example shows a way to do this in Bash: .. code-block:: Bash

      export AZURE_CLIENT_ID=”generated app id” export AZURE_CLIENT_SECRET=”random password” export AZURE_TENANT_ID=”tenant id”

  • Authorize the service principal to perform certificate operations in your Key Vault: .. code-block:: Bash

    az keyvault set-policy –name my-key-vault –spn $AZURE_CLIENT_ID –certificate-permissions backup create delete get import list purge recover restore update

    Possible certificate permissions: backup, create, delete, deleteissuers, get, getissuers, import, list, listissuers, managecontacts, manageissuers, purge, recover, restore, setissuers, update

Create a client

After setting the AZURE_CLIENT_ID, AZURE_CLIENT_SECRET and AZURE_TENANT_ID environment variables, you can create the CertificateClient:

from azure.identity import DefaultAzureCredential
from azure.keyvault.certificates import CertificateClient

credential = DefaultAzureCredential()

# Create a new certificate client using the default credential
certificate_client = CertificateClient(vault_url=<your-vault-url>, credential=credential)

Key concepts

With a CertificateClient you can get certificates from the vault, create new certificates and new versions of existing certificates, update certificate metadata, and delete certificates. You can also manage certificate issuers, contacts, and management policies of certificates. This is illustrated in the examples below.

Certificate Client:

Examples

This section contains code snippets covering common tasks:

Create a Certificate

begin_create_certificate creates a certificate to be stored in the Azure Key Vault. If a certificate with the same name already exists, then a new version of the certificate is created. Before creating a certificate, a management policy for the certificate can be created or our default policy will be used. The begin_create_certificate operation returns a long running operation poller.

create_certificate_poller = certificate_client.begin_create_certificate(name="cert-name", policy=CertificatePolicy.get_default())

print(create_certificate_poller.result())

Retrieve a Certificate

get_certificate retrieves a certificate previously stored in the Key Vault without having to specify version.

certificate = certificate_client.get_certificate(name="cert-name")

print(certificate.name)
print(certificate.properties.version)
print(certificate.policy.id)

get_certificate_version retrieves a certificate based on the certificate name and the version of the certificate. Version is required.

certificate = certificate_client.get_certificate_version(name="cert-name", version="cert-version")

print(certificate.name)
print(certificate.properties.version)

Update properties of an existing Certificate]

update_certificate_properties updates a certificate previously stored in the Key Vault.

# You can specify additional application-specific metadata in the form of tags.
tags = {"foo": "updated tag"}

updated_certificate= certificate_client.update_certificate_properties(name="cert-name", tags=tags)

print(updated_certificate.name)
print(updated_certificate.properties.version)
print(updated_certificate.properties.updated_on)
print(updated_certificate.properties.tags)

Delete a Certificate

delete_certificate deletes a certificate previously stored in the Key Vault. When soft-delete is not enabled for the Key Vault, this operation permanently deletes the certificate.

deleted_certificate = certificate_client.delete_certificate(name="cert-name")

print(deleted_certificate.name)
print(deleted_certificate.deleted_date)

List properties of Certificates

This example lists the properties of all certificates in the specified Key Vault.

certificates = certificate_client.list_properites_of_certificates()

for certificate in certificates:
    # this list doesn't include versions of the certificates
    print(certificate.name)

Async operations

This library includes a complete async API supported on Python 3.5+. To use it, you must first install an async transport, such as ``aiohttp` <https://pypi.org/project/aiohttp/>`_. See azure-core documentation for more information.

Asynchronously create a Certificate

create_certificate creates a certificate to be stored in the Azure Key Vault. If a certificate with the same name already exists, then a new version of the certificate is created. Before creating a certificate, a management policy for the certificate can be created or our default policy will be used. Awaiting the call to create_certificate returns your created certificate if creation is successful, and a CertificateOperation if creation is not.

create_certificate_result = await certificate_client.create_certificate(name="cert-name", policy=CertificatePolicy.get_default())
print(create_certificate_result)

Asynchronously list properties of Certificates

This example lists all the certificates in the client’s vault:

certificates = certificate_client.list_certificates()

async for certificate in certificates:
    print(certificate.name)

Troubleshooting

General

Key Vault clients raise exceptions defined in ``azure-core` <https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/core/azure-core/docs/exceptions.md>`_.

For example, if you try to retrieve a certificate after it is deleted a 404 error is returned, indicating resource not found. In the following snippet, the error is handled gracefully by catching the exception and displaying additional information about the error.

from azure.core.exceptions import ResourceNotFoundError
try:
    certificate_client.get_certificate(name="deleted_certificate")
except ResourceNotFoundError as e:
    print(e.message)

Output: "certificate not found:deleted_certificate"

Logging

Network trace logging is disabled by default for this library. When enabled, HTTP requests will be logged at DEBUG level using the logging library. You can configure logging to print debugging information to stdout or write it to a file:

import sys
import logging

 # Create a logger for the 'azure' SDK
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

 # Configure a console output
handler = logging.StreamHandler(stream=sys.stdout)
logger.addHandler(handler)

 # Configure a file output
file_handler = logging.FileHandler(filename)
logger.addHandler(file_handler)

# Enable network trace logging. Each HTTP request will be logged at DEBUG level.
client = CertificateClient(vault_url=url, credential=credential, logging_enable=True))

Network trace logging can also be enabled for any single operation:

certificate = certificate_client.get_certificate(name="cert-name", logging_enable=True)

Next steps

Several samples are available in the Azure SDK for Python GitHub repository. These samples provide example code for additional Key Vault scenarios:

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Impressions