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:
- Service principal authentication
- Managed identity authentication
- User authentication
Source code | Package (PyPI) | API reference documentation | Azure Active Directory documentation
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
Creating a Service Principal with the Azure CLI
This library doesn't require a service principal, but Azure applications commonly use them for authentication. If you need to create one, you can use this Azure CLI snippet. Before using it, replace "http://my-application" 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
Example output:
{
"appId": "generated-app-id",
"displayName": "app-name",
"name": "http://my-application",
"password": "random-password",
"tenant": "tenant-id"
}
Azure Identity can authenticate as this service principal using its tenant id ("tenant" above), client id ("appId" above), and client secret ("password" above).
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 the Azure SDK accept credentials as constructor parameters, as described in their documentation. The next steps section below contains a partial list of client libraries accepting Azure Identity credentials.
Credential classes are found in the azure.identity
namespace. They differ
in the types of identities they can authenticate as, and in their 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, user | 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 async equivalents 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 can authenticate as a service principal, managed identity, or user, and can be configured for local development and production environments without code changes.
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 is only possible in a supported hosting environment. See Azure Active Directory's 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. This may require additional configuration when
multiple identities have signed in. In that case, set the environment variable
AZURE_USERNAME
with the 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
id of an Azure Active Directory application AZURE_TENANT_ID
id of the application's Azure Active Directory tenant AZURE_CLIENT_SECRET
one of the application's client secrets
Service principal with certificate
variable name value AZURE_CLIENT_ID
id of an Azure Active Directory application AZURE_TENANT_ID
id of the application's Azure Active Directory tenant AZURE_CLIENT_CERTIFICATE_PATH
path to a PEM-encoded certificate file including private key (without password protection)
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
Note: username/password authentication is not supported by the async API (azure.identity.aio)
Configuration is attempted in the above order. For example, if values for a client secret and certificate are both present, the 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
# This 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, credential=credential)
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(tenant_id, client_id, client_secret)
client = KeyClient("https://my-vault.vault.azure.net", 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(tenant_id, client_id, cert_path)
client = SecretClient("https://my-vault.vault.azure.net", credential)
Chaining credentials:
ChainedTokenCredential 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 a service principal if a managed identity is unavailable. This
example uses the EventHubClient
from the azure-eventhubs
client library.
from azure.eventhub import EventHubClient
from azure.identity import ChainedTokenCredential, ClientSecretCredential, ManagedIdentityCredential
managed_identity = ManagedIdentityCredential()
service_principal = ClientSecretCredential(tenant_id, client_id, client_secret)
# when an access token is needed, the chain will try each
# credential in order, stopping when one provides a token
credential_chain = ChainedTokenCredential(managed_identity, service_principal)
# the ChainedTokenCredential can be used anywhere a credential is required
client = EventHubClient(host, event_hub_path, credential_chain)
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 an asynchronous
credential.
# most credentials have async equivalents supported on Python 3.5.3+
from azure.identity.aio import DefaultAzureCredential
from azure.keyvault.secrets.aio import SecretClient
# async credentials have the same API and configuration as their synchronous
# counterparts, and are used with (async) Azure SDK clients in the same way
default_credential = DefaultAzureCredential()
client = SecretClient("https://my-vault.vault.azure.net", default_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
DefaultAzureCredential or ChainedTokenCredential
,
the message collects error messages from each credential in the chain.
For more details on handling Azure Active Directory errors please refer to the Azure Active Directory error code documentation.
Next steps
Client library support
This is an incomplete list of client libraries accepting Azure Identity credentials. You can learn more about these libraries, and find additional documentation of them, at the links below.
- azure-appconfiguration
- azure-eventhubs
- azure-keyvault-certificates
- azure-keyvault-keys
- azure-keyvault-secrets
- azure-storage-blob
- azure-storage-queue
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.
Release History
1.0.0 (2019-10-29)
Breaking changes:
- Async credentials now default to
aiohttp
for transport but the library does not require it as a dependency because the async API is optional. To use async credentials, please installaiohttp
or see azure-core documentation for information about customizing the transport. - Renamed
ClientSecretCredential
parameter "secret
" to "client_secret
" - All credentials with
tenant_id
andclient_id
positional parameters now accept them in that order - Changes to
InteractiveBrowserCredential
parameters- positional parameter
client_id
is now an optional keyword argument. If no value is provided, the Azure CLI's client ID will be used. - Optional keyword argument
tenant
renamedtenant_id
- positional parameter
- Changes to
DeviceCodeCredential
- optional positional parameter
prompt_callback
is now a keyword argument prompt_callback
's third argument is now adatetime
representing the expiration time of the device code- optional keyword argument
tenant
renamedtenant_id
- optional positional parameter
- Changes to
ManagedIdentityCredential
- now accepts no positional arguments, and only one keyword argument:
client_id
- transport configuration is now done through keyword arguments as
described in
azure-core
documentation
- now accepts no positional arguments, and only one keyword argument:
Fixes and improvements:
- Authenticating with a single sign-on shared with other Microsoft applications only requires a username when multiple users have signed in (#8095)
DefaultAzureCredential
accepts anauthority
keyword argument, enabling its use in national clouds (#8154)
Dependency changes
- Adopted
msal_extensions
0.1.2 - Constrained
msal
requirement to >=0.4.1, <1.0.0
1.0.0b4 (2019-10-07)
New features:
AuthorizationCodeCredential
authenticates with a previously obtained authorization code. See Azure Active Directory's authorization code documentation for more information about this authentication flow.- Multi-cloud support: client credentials accept the authority of an Azure Active
Directory authentication endpoint as an
authority
keyword argument. Known authorities are defined inazure.identity.KnownAuthorities
. The default authority is for Azure Public Cloud,login.microsoftonline.com
(KnownAuthorities.AZURE_PUBLIC_CLOUD
). An application running in Azure Government would useKnownAuthorities.AZURE_GOVERNMENT
instead:
from azure.identity import DefaultAzureCredential, KnownAuthorities credential = DefaultAzureCredential(authority=KnownAuthorities.AZURE_GOVERNMENT)
Breaking changes:
- Removed
client_secret
parameter fromInteractiveBrowserCredential
Fixes and improvements:
UsernamePasswordCredential
correctly handles environment configuration with no tenant information (#7260)- user realm discovery requests are sent through credential pipelines (#7260)
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
includesSharedTokenCacheCredential
when the shared cache is available, and environment variableAZURE_USERNAME
is set. See the README for more information.
Dependency changes:
- New dependency:
msal-extensions
0.1.1
1.0.0b2 (2019-08-05)
Breaking changes:
- Removed
azure.core.Configuration
from the public API in preparation for a revamped configuration API. Staticcreate_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
- 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:
- Adopted MSAL 0.4.1
- New dependency for Python 2.7: mock
New features:
- Added credentials for authenticating users:
DeviceCodeCredential
,InteractiveBrowserCredential
,UsernamePasswordCredential
- async versions of these credentials will be added in a future release
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
Built Distribution
File details
Details for the file azure-identity-1.0.0.zip
.
File metadata
- Download URL: azure-identity-1.0.0.zip
- Upload date:
- Size: 90.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 833d9e4f73f809a1235147e2e89bd605a45341362122214af5039ece32826a6e |
|
MD5 | 93e0cfeb884d59c3e1273121bc8ad33b |
|
BLAKE2b-256 | 598706c3a8a55a4d15191e553d7c4a97f5e6170f9246e30356e4f66963b5eda1 |
File details
Details for the file azure_identity-1.0.0-py2.py3-none-any.whl
.
File metadata
- Download URL: azure_identity-1.0.0-py2.py3-none-any.whl
- Upload date:
- Size: 52.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bd230d824923faf6d37fe307b3a7a822785914e3c27deeb364fded5ee7ce5568 |
|
MD5 | 738c54d81e024013ce96543e388ba3b5 |
|
BLAKE2b-256 | 17861e49c3de666511d42bc0f40fe99701ce59d1d606c6e0697b6a26a1d16122 |