.. role:: raw-html-m2r(raw)
:format: html
Azure Identity client library for Python
========================================
The Azure Identity library provides a set of credential classes for use with
Azure SDK clients which support Azure Active Directory (AAD) token authentication.
`Source code `_
| `Package (PyPI) `_
| `API reference documentation `_
| `Azure Active Directory documentation `_
Getting started
===============
Install the package
-------------------
Install Azure Identity with pip:
.. code-block:: sh
pip install azure-identity
Prerequisites
-------------
* an `Azure subscription `_
* Python 2.7 or 3.5.3+
Authenticating during local development
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
When debugging and executing code locally it is typical for developers to use
their own accounts for authenticating calls to Azure services. The Azure
Identity library supports authenticating through developer tools to simplify
local development.
Authenticating via Visual Studio Code
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
``DefaultAzureCredential`` and ``VisualStudioCodeCredential`` can authenticate as
the user signed in to Visual Studio Code's
`Azure Account extension `_.
After installing the extension, sign in to Azure in Visual Studio Code by
pressing ``F1`` to open the command palette and running the ``Azure: Sign In``
command.
.. image:: https://raw.githubusercontent.com/Azure/azure-sdk-for-python/master/sdk/identity/azure-identity/images/VsCodeLoginCommand.png
:target: https://raw.githubusercontent.com/Azure/azure-sdk-for-python/master/sdk/identity/azure-identity/images/VsCodeLoginCommand.png
:alt: Visual Studio Code Account Sign In
Authenticating via the Azure CLI
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
``DefaultAzureCredential`` and ``AzureCliCredential`` can authenticate as the user
signed in to the `Azure CLI `_. To sign in to the Azure CLI, run
``az login``. On a system with a default web browser, the Azure CLI will launch
the browser to authenticate a user.
.. image:: https://raw.githubusercontent.com/Azure/azure-sdk-for-python/master/sdk/identity/azure-identity/images/AzureCliLogin.png
:target: https://raw.githubusercontent.com/Azure/azure-sdk-for-python/master/sdk/identity/azure-identity/images/AzureCliLogin.png
:alt: Azure CLI Account Sign In
When no default browser is available, ``az login`` will use the device code
authentication flow. This can also be selected manually by running ``az login --use-device-code``.
.. image:: https://raw.githubusercontent.com/Azure/azure-sdk-for-python/master/sdk/identity/azure-identity/images/AzureCliLoginDeviceCode.png
:target: https://raw.githubusercontent.com/Azure/azure-sdk-for-python/master/sdk/identity/azure-identity/images/AzureCliLoginDeviceCode.png
:alt: Azure CLI Account Device Code Sign In
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 a credential instance when they are constructed, and use that credential
to authenticate requests.
The Azure Identity library focuses on OAuth authentication with Azure Active
Directory (AAD). It offers a variety of credential classes capable of acquiring
an AAD access token. See :raw-html-m2r:`Credential Classes` below for a list of this library's credential classes.
DefaultAzureCredential
^^^^^^^^^^^^^^^^^^^^^^
``DefaultAzureCredential`` is appropriate for most applications which will run in
the Azure Cloud because it combines common production credentials with
development credentials. ``DefaultAzureCredential`` attempts to authenticate via
the following mechanisms in this order, stopping when one succeeds:
.. image:: https://raw.githubusercontent.com/Azure/azure-sdk-for-python/master/sdk/identity/azure-identity/images/DefaultAzureCredentialAuthenticationFlow.png
:target: https://raw.githubusercontent.com/Azure/azure-sdk-for-python/master/sdk/identity/azure-identity/images/DefaultAzureCredentialAuthenticationFlow.png
:alt: DefaultAzureCredential authentication flow
* Environment - ``DefaultAzureCredential`` will read account information specified
via :raw-html-m2r:`environment variables`
and use it to authenticate.
* Managed Identity - if the application is deployed to an Azure host with
Managed Identity enabled, ``DefaultAzureCredential`` will authenticate with it.
* Visual Studio Code - if a user has signed in to the Visual Studio Code Azure
Account extension, ``DefaultAzureCredential`` will authenticate as that user.
* Azure CLI - If a user has signed in via the Azure CLI ``az login`` command,
``DefaultAzureCredential`` will authenticate as that user.
* Interactive - If enabled, ``DefaultAzureCredential`` will interactively
authenticate a user via the current system's default browser.
Examples
========
The following examples are provided below:
* :raw-html-m2r:`Authenticating with DefaultAzureCredential`
* :raw-html-m2r:`Defining a custom authentication flow with ChainedTokenCredential`
* :raw-html-m2r:`Async credentials`
Authenticating with ``DefaultAzureCredential``
--------------------------------------------------
This example demonstrates authenticating the ``BlobServiceClient`` from the
`azure-storage-blob `_ library using
``DefaultAzureCredential``.
.. code-block:: py
from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient
default_credential = DefaultAzureCredential()
client = BlobServiceClient(account_url, credential=default_credential)
Enabling interactive authentication with ``DefaultAzureCredential``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Interactive authentication is disabled in the ``DefaultAzureCredential`` by
default and can be enabled with a keyword argument:
.. code-block:: py
DefaultAzureCredential(exclude_interactive_browser_credential=False)
When enabled, ``DefaultAzureCredential`` falls back to interactively
authenticating via the system's default web browser when no other credential is
available.
Defining a custom authentication flow with ``ChainedTokenCredential``
-------------------------------------------------------------------------
``DefaultAzureCredential`` is generally the quickest way to get started developing
applications for Azure. For more advanced scenarios,
`ChainedTokenCredential `_ links multiple credential instances
to be tried sequentially when authenticating. It will try each chained
credential in turn until one provides a token or fails to authenticate due to
an error.
The following example demonstrates creating a credential which will attempt to
authenticate using managed identity, and fall back to authenticating via the
Azure CLI when a managed identity is unavailable. This example uses the
``EventHubClient`` from the `azure-eventhub `_ client library.
.. code-block:: py
from azure.eventhub import EventHubClient
from azure.identity import AzureCliCredential, ChainedTokenCredential, ManagedIdentityCredential
managed_identity = ManagedIdentityCredential()
azure_cli = AzureCliCredential()
credential_chain = ChainedTokenCredential(managed_identity, azure_cli)
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.
Async credentials should be closed when they're no longer needed. Each async
credential is an async context manager and defines an async ``close`` method. For
example:
.. code-block:: py
from azure.identity.aio import DefaultAzureCredential
# call close when the credential is no longer needed
credential = DefaultAzureCredential()
...
await credential.close()
# alternatively, use the credential as an async context manager
credential = DefaultAzureCredential()
async with credential:
...
This example demonstrates authenticating the asynchronous ``SecretClient`` from
`azure-keyvault-secrets `_ with an asynchronous
credential.
.. code-block:: py
from azure.identity.aio import DefaultAzureCredential
from azure.keyvault.secrets.aio import SecretClient
default_credential = DefaultAzureCredential()
client = SecretClient("https://my-vault.vault.azure.net", default_credential)
Credential Classes
------------------
Authenticating Azure Hosted Applications
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. list-table::
:header-rows: 1
* - credential
- usage
* - `DefaultAzureCredential `_
- simplified authentication to get started developing applications for the Azure cloud
* - `ChainedTokenCredential `_
- define custom authentication flows composing multiple credentials
* - `EnvironmentCredential `_
- authenticate a service principal or user configured by environment variables
* - `ManagedIdentityCredential `_
- authenticate the managed identity of an Azure resource
Authenticating Service Principals
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. list-table::
:header-rows: 1
* - credential
- usage
* - `ClientSecretCredential `_
- authenticate a service principal using a secret
* - `CertificateCredential `_
- authenticate a service principal using a certificate
Authenticating Users
^^^^^^^^^^^^^^^^^^^^
.. list-table::
:header-rows: 1
* - credential
- usage
* - `InteractiveBrowserCredential `_
- interactively authenticate a user with the default web browser
* - `DeviceCodeCredential `_
- interactively authenticate a user on a device with limited UI
* - `UsernamePasswordCredential `_
- authenticate a user with a username and password
Authenticating via Development Tools
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. list-table::
:header-rows: 1
* - credential
- usage
* - `AzureCliCredential `_
- authenticate as the user signed in to the Azure CLI
* - `VisualStudioCodeCredential `_
- authenticate as the user signed in to the Visual Studio Code Azure Account extension
Environment Variables
---------------------
`DefaultAzureCredential `_ and
`EnvironmentCredential `_ can be configured with
environment variables. Each type of authentication requires values for specific
variables:
Service principal with secret
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. list-table::
:header-rows: 1
* - 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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. list-table::
:header-rows: 1
* - 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
~~~~~~~~~~~~~~~~~~~~~
.. list-table::
:header-rows: 1
* - 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 values for a
client secret and certificate are both present, the client secret will be used.
Troubleshooting
---------------
Error Handling
^^^^^^^^^^^^^^
Credentials raise ``CredentialUnavailableError`` when they're unable to attempt
authentication because they lack required data or state. For example,
`EnvironmentCredential `_ will raise this exception when
:raw-html-m2r:`its configuration` is incomplete.
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 specific Azure Active Directory errors please refer to the
Azure Active Directory
`error code documentation `_.
Logging
^^^^^^^
This library uses the standard
`logging `_ library for logging.
Credentials log basic information, including HTTP sessions (URLs, headers, etc.) at INFO level. These log entries do not contain authentication secrets.
Detailed DEBUG level logging, including request/response bodies and header values, is not enabled by default. It can be enabled with the ``logging_enable`` argument, for example:
.. code-block:: py
credential = DefaultAzureCredential(logging_enable=True)
..
CAUTION: DEBUG level logs from credentials contain sensitive information.
These logs must be protected to avoid compromising account security.
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-eventhub `_
* `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.
.. image:: https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-python%2Fsdk%2Fidentity%2Fazure-identity%2FREADME.png
:target: https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-python%2Fsdk%2Fidentity%2Fazure-identity%2FREADME.png
:alt: Impressions
Indices and tables
------------------
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
.. toctree::
:maxdepth: 5
:glob:
:caption: Developer Documentation
azure.identity.rst