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

This library is in preview and currently supports:

Source code | Package (PyPI) | API reference documentation | Azure Active Directory documentation

Getting started

Prerequisites

  • an Azure subscription
  • Python 2.7 or 3.5.3+
  • an Azure Active Directory service principal. If you need to create one, you can use the Azure Portal, or Azure CLI

Install the package

Install Azure Identity with pip:

pip install azure-identity

Creating a Service Principal with the Azure CLI

Use this Azure CLI snippet to create/get client secret credentials.

  • Create a service principal:
    az ad sp create-for-rbac -n <your-application-name> --skip-assignment
    
    Example output:
    {
        "appId": "generated-app-ID",
        "displayName": "app-name",
        "name": "http://app-name",
        "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.

Key concepts

Credentials

A credential is a class which contains or can obtain the data needed for a service client to authenticate requests. Service clients across Azure SDK accept credentials as constructor parameters. See next steps below for a list of client libraries accepting Azure Identity credentials.

Credential classes are defined in the azure.identity namespace. These differ in the types of Azure Active Directory identities they can authenticate, and in configuration:

credential class identity configuration
DefaultAzureCredential service principal, managed identity, user none for managed identity, environment variables for service principal or user authentication
ManagedIdentityCredential managed identity none
EnvironmentCredential service principal environment variables
ClientSecretCredential service principal constructor parameters
CertificateCredential service principal constructor parameters
DeviceCodeCredential user constructor parameters
InteractiveBrowserCredential user constructor parameters
UsernamePasswordCredential user constructor parameters

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

Service principal and managed identity credentials have an async equivalent in the azure.identity.aio namespace, supported on Python 3.5.3+. See the async credentials example for details. Async user credentials will be part of a future release.

DefaultAzureCredential

DefaultAzureCredential is appropriate for most applications intended to run in Azure. It authenticates as a service principal or managed identity, depending on its environment, and can be configured to work both during local development and when deployed to the cloud.

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.

Single sign-on

During local development on Windows, DefaultAzureCredential can authenticate using a single sign-on shared with Microsoft applications, for example Visual Studio 2019. Because you may have multiple signed in identities, to authenticate this way you must set the environment variable AZURE_USERNAME with your desired identity's username (typically an email address).

Environment variables

DefaultAzureCredential and EnvironmentCredential can be configured with environment variables. Each type of authentication requires values for specific variables:

Service principal with secret

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

Service principal with certificate

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_CERTIFICATE_PATH path to a PEM-encoded certificate file including private key (without password)

Username and password

variable name value
AZURE_CLIENT_ID id of an Azure Active Directory application
AZURE_USERNAME a username (usually an email address)
AZURE_PASSWORD that user's password

Configuration is attempted in the above order. For example, if both AZURE_CLIENT_SECRET and AZURE_CLIENT_CERTIFICATE_PATH have values, AZURE_CLIENT_SECRET will be used.

Examples

Authenticating with DefaultAzureCredential

This example demonstrates authenticating the BlobServiceClient from the azure-storage-blob library using DefaultAzureCredential.

from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient

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

client = BlobServiceClient(account_url=<your storage account url>, credential=credential)

Executing this on a development machine requires first [configuring the environment][#environment-variables] with appropriate values for your service principal.

Authenticating a service principal with a client secret:

This example demonstrates authenticating the KeyClient from the azure-keyvault-keys library using ClientSecretCredential.

from azure.identity import ClientSecretCredential
from azure.keyvault.keys import KeyClient

credential = ClientSecretCredential(client_id, client_secret, tenant_id)

client = KeyClient(vault_url=<your vault url>, credential=credential)

Authenticating a service principal with a certificate:

This example demonstrates authenticating the SecretClient from the azure-keyvault-secrets library using CertificateCredential.

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

# 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)

client = SecretClient(vault_url=<your vault url>, credential=credential)

Chaining credentials:

The ChainedTokenCredential class links multiple credential instances to be tried sequentially when authenticating. The following example demonstrates creating a credential which will attempt to authenticate using managed identity, and fall back to client secret authentication if a managed identity is unavailable in the current environment. This example demonstrates authenticating an EventHubClient from the azure-eventhubs client library.

from azure.eventhub import EventHubClient
from azure.identity import ChainedTokenCredential, ClientSecretCredential, ManagedIdentityCredential

managed_identity = ManagedIdentityCredential()
client_secret = ClientSecretCredential(client_id, client_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(managed_identity, client_secret)

# the ChainedTokenCredential can be used anywhere a credential is required
client = EventHubClient(host, event_hub_path, credential)

Async credentials:

This library includes an async API supported on Python 3.5+. To use the async credentials in azure.identity.aio, you must first install an async transport, such as aiohttp. See azure-core documentation for more information.

This example demonstrates authenticating the asynchronous SecretClient from azure-keyvault-secrets with asynchronous 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 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.

For more details on dealing with Azure Active Directory errors please refer to the Azure Active Directory error code documentation.

Next steps

Client library support

Currently the following client libraries support authenticating with Azure Identity credentials. You can learn more about them, and find additional documentation on using these client libraries along with samples, at the links below.

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.0b3 (2019-09-10)

New features:

  • SharedTokenCacheCredential authenticates with tokens stored in a local cache shared by Microsoft applications. This enables Azure SDK clients to authenticate silently after you've signed in to Visual Studio 2019, for example. DefaultAzureCredential includes SharedTokenCacheCredential when the shared cache is available, and environment variable AZURE_USERNAME is set. See the README for more information.

Dependency changes:

1.0.0b2 (2019-08-05)

Breaking changes:

  • Removed azure.core.Configuration from the public API in preparation for a revamped configuration API. Static create_config methods have been renamed _create_config, and will be removed in a future release.

Dependency changes:

  • Adopted azure-core 1.0.0b2
    • If you later want to revert to a version requiring azure-core 1.0.0b1, of this or another Azure SDK library, you must explicitly install azure-core 1.0.0b1 as well. For example: pip install azure-core==1.0.0b1 azure-identity==1.0.0b1
  • Adopted MSAL 0.4.1
  • New dependency for Python 2.7: mock

New features:

1.0.0b1 (2019-06-28)

Version 1.0.0b1 is the first preview of our efforts to create a user-friendly and Pythonic authentication API for Azure SDK client libraries. For more information about preview releases of other Azure SDK libraries, please visit https://aka.ms/azure-sdk-preview1-python.

This release supports service principal and managed identity authentication. See the documentation for more details. User authentication will be added in an upcoming preview release.

This release supports only global Azure Active Directory tenants, i.e. those using the https://login.microsoftonline.com authentication endpoint.

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.0b3.zip (47.5 kB view details)

Uploaded Source

Built Distribution

azure_identity-1.0.0b3-py2.py3-none-any.whl (34.9 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

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

File hashes

Hashes for azure-identity-1.0.0b3.zip
Algorithm Hash digest
SHA256 4261c8cafb559a08b7512cf6bbb83cd5bb0f210577c799c7152698df7dd0452c
MD5 f74c58b21645c0e15158a3e8d3bd57ae
BLAKE2b-256 95f72a4623a0d727eb614bbcedc4c4259e47c84aee8ec3309b5dbeb2c2f4eeb7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: azure_identity-1.0.0b3-py2.py3-none-any.whl
  • Upload date:
  • Size: 34.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.8

File hashes

Hashes for azure_identity-1.0.0b3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1b70b36559268fcdf8af87b672c6ae9efa728d7db4630718cd146f5f172fb104
MD5 7502de8f9d79c7b91795ea42d842d2c4
BLAKE2b-256 80d26dc4a1b7f2a4478aca221305365a10af0c490659d80106629c4bcff2a028

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