Skip to main content

Microsoft Azure Identity Library for Python

Project description

Azure Identity client library for Python

Azure Identity simplifies authentication across the Azure SDK. It supports token authentication using an Azure Active Directory service principal or managed identity.

Getting started

Prerequisites

  • an Azure subscription
  • Python 2.7 or 3.5.3+

Install the package

Install Azure Identity with pip:

pip install azure-identity

Key concepts

Credentials

Azure Identity offers a variety of credential classes in the azure.identity namespace. These are accepted by Azure SDK data plane clients. Each client library documents its Azure Identity integration in its README and samples. Azure SDK resource management libraries (which have mgmt in their names) do not accept these credentials.

Credentials differ mostly in configuration:

credential class identity configuration
DefaultAzureCredential service principal or managed identity none for managed identity; environment variables for service principal
ManagedIdentityCredential managed identity none
EnvironmentCredential service principal environment variables
ClientSecretCredential service principal constructor parameters
CertificateCredential service principal constructor parameters

Credentials can be chained and tried in turn until one succeeds; see chaining credentials for details.

All credentials have an async equivalent in the azure.identity.aio namespace. These are supported on Python 3.5.3+. See the async credentials example for details.

DefaultAzureCredential

DefaultAzureCredential is appropriate for most scenarios. It supports authenticating as a service principal or managed identity. To authenticate as a service principal, provide configuration in environment variables as described in the next section.

Authenticating as a managed identity requires no configuration, but does require platform support. See the managed identity documentation for more information.

Environment variables

DefaultAzureCredential and EnvironmentCredential are configured for service principal authentication with these environment variables:

variable name value
AZURE_CLIENT_ID service principal's app id
AZURE_TENANT_ID id of the principal's Azure Active Directory tenant
AZURE_CLIENT_SECRET one of the service principal's client secrets
AZURE_CLIENT_CERTIFICATE_PATH path to a PEM-encoded certificate file including private key (without password)

Either AZURE_CLIENT_SECRET or AZURE_CLIENT_CERTIFICATE_PATH must be set. If both are set, the client secret will be used.

Examples

DefaultAzureCredential

# The default credential first checks environment variables for configuration as described above.
# If environment configuration is incomplete, it will try managed identity.
from azure.identity import DefaultAzureCredential

credential = DefaultAzureCredential()

# Azure SDK clients accept the credential as a parameter
from azure.keyvault.secrets import SecretClient

client = SecretClient(vault_url, credential)

Authenticating as a service principal:

# using a client secret
from azure.identity import ClientSecretCredential

credential = ClientSecretCredential(client_id, client_secret, tenant_id)

# using a certificate
from azure.identity import CertificateCredential

# requires a PEM-encoded certificate with private key, not protected with a password
cert_path = "/app/certs/certificate.pem"
credential = CertificateCredential(client_id, tenant_id, cert_path)

# using environment variables
from azure.identity import EnvironmentCredential

# authenticate with client secret or certificate,
# depending on environment variable settings
# (see "Environment variables" above for variable names and expected values)
credential = EnvironmentCredential()

Chaining credentials:

from azure.identity import ClientSecretCredential, ManagedIdentityCredential, ChainedTokenCredential

first_principal = ClientSecretCredential(client_id, client_secret, tenant_id)
second_principal = ClientSecretCredential(another_client_id, another_secret, tenant_id)

# when an access token is requested, the chain will try each
# credential in order, stopping when one provides a token
credential_chain = ChainedTokenCredential(first_principal, second_principal)

# the chain can be used anywhere a credential is required
from azure.keyvault.secrets import SecretClient

client = SecretClient(vault_url, credential=credential_chain)

Async credentials:

# all credentials have async equivalents supported on Python 3.5.3+
from azure.identity.aio import DefaultAzureCredential

default_credential = DefaultAzureCredential()

# async credentials have the same API and configuration as
# their synchronous counterparts...
from azure.identity.aio import ClientSecretCredential

credential = ClientSecretCredential(client_id, client_secret, tenant_id)

# ...and are used with async Azure SDK clients in the same way
from azure.keyvault.aio import SecretClient

client = SecretClient(vault_url, credential)

Troubleshooting

General

Credentials raise azure.core.exceptions.ClientAuthenticationError when they fail to authenticate. ClientAuthenticationError has a message attribute which describes why authentication failed. When raised by ChainedTokenCredential, the message collects error messages from each credential in the chain.

Next steps

Provide Feedback

If you encounter bugs or have suggestions, please open an issue.

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

Release History

1.0.0b1

First preview release of the library.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

azure-identity-1.0.0b1.zip (27.5 kB view details)

Uploaded Source

Built Distributions

azure_identity-1.0.0b1-py3-none-any.whl (19.2 kB view details)

Uploaded Python 3

azure_identity-1.0.0b1-py2.py3-none-any.whl (19.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file azure-identity-1.0.0b1.zip.

File metadata

  • Download URL: azure-identity-1.0.0b1.zip
  • Upload date:
  • Size: 27.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.8

File hashes

Hashes for azure-identity-1.0.0b1.zip
Algorithm Hash digest
SHA256 44c843c52a874e05547400165c53fac47726be24943a59f6d3fdd9b3a84db777
MD5 a6f9341fbc6ebc9d6e6f799df8ac61c3
BLAKE2b-256 6a183fc714e98b87f26d7e313577dd24a67fa401cd07eda4df0bea0ab285a81d

See more details on using hashes here.

File details

Details for the file azure_identity-1.0.0b1-py3-none-any.whl.

File metadata

  • Download URL: azure_identity-1.0.0b1-py3-none-any.whl
  • Upload date:
  • Size: 19.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.8

File hashes

Hashes for azure_identity-1.0.0b1-py3-none-any.whl
Algorithm Hash digest
SHA256 89445f7eb8763dd57889fae488d85aaa54ffaacb417b49c151a55b9eee3a057c
MD5 8672efefc3bf5cc67d8ee228bb37e4bd
BLAKE2b-256 6eecc06d6ec847dbb86b6c877ea0d90c98dfa6ddd7b39cc364a28bf4df4c7b0b

See more details on using hashes here.

File details

Details for the file azure_identity-1.0.0b1-py2.py3-none-any.whl.

File metadata

  • Download URL: azure_identity-1.0.0b1-py2.py3-none-any.whl
  • Upload date:
  • Size: 19.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.1

File hashes

Hashes for azure_identity-1.0.0b1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1a2f4d5eb3c75b9be115974e9dbb66c3fee218da8592918d86c37324c7d46b89
MD5 a118a41ec3e8b29d35b6c56bac51bb47
BLAKE2b-256 37c2cc019e8927150337cb90c92ddb06381c2327da297ac51f6dae5aeb068dce

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page