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+ * an Azure Active Directory service principal. If you need to create one, you can use the Azure Portal, or `Azure CLI <#creating-a-service-principal-with-the-azure-cli>`_ Install the package ------------------- Install Azure Identity with pip: .. code-block:: sh 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: .. code-block:: sh az ad sp create-for-rbac -n --skip-assignment Example output: .. code-block:: json { "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 <#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 <#client-library-support>`_ 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: .. list-table:: :header-rows: 1 * - credential class - identity - configuration * - ``DefaultAzureCredential`` - service principal, managed identity, user - none for managed identity, `environment variables <#environment-variables>`_ for service principal or user authentication * - ``ManagedIdentityCredential`` - managed identity - none * - ``EnvironmentCredential`` - service principal - `environment variables <#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 <#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 <#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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. .. list-table:: :header-rows: 1 * - 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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. .. list-table:: :header-rows: 1 * - 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 ~~~~~~~~~~~~~~~~~~~~~ .. .. 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 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``. .. code-block:: py 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, 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``. .. code-block:: py from azure.identity import ClientSecretCredential from azure.keyvault.keys import KeyClient credential = ClientSecretCredential(tenant_id, client_id, client_secret) client = KeyClient(vault_endpoint, credential) Authenticating a service principal with a certificate: ------------------------------------------------------ This example demonstrates authenticating the ``SecretClient`` from the `\ ``azure-keyvault-secrets`` `_ library using ``CertificateCredential``. .. code-block:: py 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(vault_endpoint, 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. .. code-block:: py from azure.eventhub import EventHubClient from azure.identity import ChainedTokenCredential, ClientSecretCredential, ManagedIdentityCredential managed_identity = ManagedIdentityCredential() client_secret = ClientSecretCredential(tenant_id, client_id, client_secret) # 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_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 asynchronous credentials. .. code-block:: py # most 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(tenant_id, client_id, client_secret) # 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 handling 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. * `azure-eventhubs `_ * `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