• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

azure/H03-May-2022-6,0444,308

azure_identity.egg-info/H03-May-2022-796650

samples/H03-May-2022-7246

tests/H03-May-2022-9,1656,699

CHANGELOG.mdH A D11-Nov-202021.5 KiB421368

MANIFEST.inH A D11-Nov-202099 54

PKG-INFOH A D11-Nov-202044.4 KiB796650

README.mdH A D11-Nov-202015.9 KiB352261

setup.cfgH A D11-Nov-202067 85

setup.pyH A D11-Nov-20202.7 KiB8669

README.md

1# Azure Identity client library for Python
2
3The Azure Identity library provides a set of credential classes for use with
4Azure SDK clients which support Azure Active Directory (AAD) token authentication.
5
6[Source code](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/identity/azure-identity)
7| [Package (PyPI)](https://pypi.org/project/azure-identity/)
8| [API reference documentation][ref_docs]
9| [Azure Active Directory documentation](https://docs.microsoft.com/azure/active-directory/)
10
11## Getting started
12
13### Install the package
14
15Install Azure Identity with pip:
16
17```sh
18pip install azure-identity
19```
20
21### Prerequisites
22
23- an [Azure subscription](https://azure.microsoft.com/free/)
24- Python 2.7 or 3.5.3+
25
26### Authenticating during local development
27
28When debugging and executing code locally it is typical for developers to use
29their own accounts for authenticating calls to Azure services. The Azure
30Identity library supports authenticating through developer tools to simplify
31local development.
32
33#### Authenticating via Visual Studio Code
34
35`DefaultAzureCredential` and `VisualStudioCodeCredential` can authenticate as
36the user signed in to Visual Studio Code's
37[Azure Account extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.azure-account).
38After installing the extension, sign in to Azure in Visual Studio Code by
39pressing `F1` to open the command palette and running the `Azure: Sign In`
40command.
41
42![Visual Studio Code Account Sign In](https://raw.githubusercontent.com/Azure/azure-sdk-for-python/master/sdk/identity/azure-identity/images/VsCodeLoginCommand.png)
43
44#### Authenticating via the Azure CLI
45
46`DefaultAzureCredential` and `AzureCliCredential` can authenticate as the user
47signed in to the [Azure CLI][azure_cli]. To sign in to the Azure CLI, run
48`az login`. On a system with a default web browser, the Azure CLI will launch
49the browser to authenticate a user.
50
51![Azure CLI Account Sign In](https://raw.githubusercontent.com/Azure/azure-sdk-for-python/master/sdk/identity/azure-identity/images/AzureCliLogin.png)
52
53When no default browser is available, `az login` will use the device code
54authentication flow. This can also be selected manually by running `az login --use-device-code`.
55
56![Azure CLI Account Device Code Sign In](https://raw.githubusercontent.com/Azure/azure-sdk-for-python/master/sdk/identity/azure-identity/images/AzureCliLoginDeviceCode.png)
57
58## Key concepts
59
60### Credentials
61
62A credential is a class which contains or can obtain the data needed for a
63service client to authenticate requests. Service clients across the Azure SDK
64accept a credential instance when they are constructed, and use that credential
65to authenticate requests.
66
67The Azure Identity library focuses on OAuth authentication with Azure Active
68Directory (AAD). It offers a variety of credential classes capable of acquiring
69an AAD access token. See [Credential Classes](#credential-classes "Credential Classes") below for a list of this library's credential classes.
70
71### DefaultAzureCredential
72
73`DefaultAzureCredential` is appropriate for most applications which will run in
74the Azure Cloud because it combines common production credentials with
75development credentials. `DefaultAzureCredential` attempts to authenticate via
76the following mechanisms in this order, stopping when one succeeds:
77
78![DefaultAzureCredential authentication flow](https://raw.githubusercontent.com/Azure/azure-sdk-for-python/master/sdk/identity/azure-identity/images/DefaultAzureCredentialAuthenticationFlow.png)
79
80- Environment - `DefaultAzureCredential` will read account information specified
81  via [environment variables](#environment-variables "environment variables")
82  and use it to authenticate.
83- Managed Identity - if the application is deployed to an Azure host with
84  Managed Identity enabled, `DefaultAzureCredential` will authenticate with it.
85- Visual Studio Code - if a user has signed in to the Visual Studio Code Azure
86  Account extension, `DefaultAzureCredential` will authenticate as that user.
87- Azure CLI - If a user has signed in via the Azure CLI `az login` command,
88  `DefaultAzureCredential` will authenticate as that user.
89- Interactive - If enabled, `DefaultAzureCredential` will interactively
90  authenticate a user via the current system's default browser.
91
92## Examples
93
94The following examples are provided below:
95
96- [Authenticating with DefaultAzureCredential](#authenticating-with-defaultazurecredential "Authenticating with DefaultAzureCredential")
97- [Defining a custom authentication flow with ChainedTokenCredential](#defining-a-custom-authentication-flow-with-chainedtokencredential "Defining a custom authentication flow with ChainedTokenCredential")
98- [Async credentials](#async-credentials "Async credentials")
99
100### Authenticating with `DefaultAzureCredential`
101
102This example demonstrates authenticating the `BlobServiceClient` from the
103[azure-storage-blob][azure_storage_blob] library using
104`DefaultAzureCredential`.
105
106```py
107from azure.identity import DefaultAzureCredential
108from azure.storage.blob import BlobServiceClient
109
110default_credential = DefaultAzureCredential()
111
112client = BlobServiceClient(account_url, credential=default_credential)
113```
114
115### Enabling interactive authentication with `DefaultAzureCredential`
116
117Interactive authentication is disabled in the `DefaultAzureCredential` by
118default and can be enabled with a keyword argument:
119
120```py
121DefaultAzureCredential(exclude_interactive_browser_credential=False)
122```
123
124When enabled, `DefaultAzureCredential` falls back to interactively
125authenticating via the system's default web browser when no other credential is
126available.
127
128### Defining a custom authentication flow with `ChainedTokenCredential`
129
130`DefaultAzureCredential` is generally the quickest way to get started developing
131applications for Azure. For more advanced scenarios,
132[ChainedTokenCredential][chain_cred_ref] links multiple credential instances
133to be tried sequentially when authenticating. It will try each chained
134credential in turn until one provides a token or fails to authenticate due to
135an error.
136
137The following example demonstrates creating a credential which will attempt to
138authenticate using managed identity, and fall back to authenticating via the
139Azure CLI when a managed identity is unavailable. This example uses the
140`EventHubProducerClient` from the [azure-eventhub][azure_eventhub] client library.
141
142```py
143from azure.eventhub import EventHubProducerClient
144from azure.identity import AzureCliCredential, ChainedTokenCredential, ManagedIdentityCredential
145
146managed_identity = ManagedIdentityCredential()
147azure_cli = AzureCliCredential()
148credential_chain = ChainedTokenCredential(managed_identity, azure_cli)
149
150client = EventHubProducerClient(namespace, eventhub_name, credential_chain)
151```
152
153### Async credentials
154
155This library includes an async API supported on Python 3.5+. To use the async
156credentials in [azure.identity.aio][ref_docs_aio], you must first install an
157async transport, such as [aiohttp](https://pypi.org/project/aiohttp/). See
158[azure-core documentation][azure_core_transport_doc] for more information.
159
160Async credentials should be closed when they're no longer needed. Each async
161credential is an async context manager and defines an async `close` method. For
162example:
163
164```py
165from azure.identity.aio import DefaultAzureCredential
166
167# call close when the credential is no longer needed
168credential = DefaultAzureCredential()
169...
170await credential.close()
171
172# alternatively, use the credential as an async context manager
173credential = DefaultAzureCredential()
174async with credential:
175  ...
176```
177
178This example demonstrates authenticating the asynchronous `SecretClient` from
179[azure-keyvault-secrets][azure_keyvault_secrets] with an asynchronous
180credential.
181
182```py
183from azure.identity.aio import DefaultAzureCredential
184from azure.keyvault.secrets.aio import SecretClient
185
186default_credential = DefaultAzureCredential()
187client = SecretClient("https://my-vault.vault.azure.net", default_credential)
188```
189
190## Credential Classes
191
192### Authenticating Azure Hosted Applications
193
194|credential|usage
195|-|-
196|[DefaultAzureCredential][default_cred_ref]|simplified authentication to get started developing applications for the Azure cloud
197|[ChainedTokenCredential][chain_cred_ref]|define custom authentication flows composing multiple credentials
198|[EnvironmentCredential][environment_cred_ref]|authenticate a service principal or user configured by environment variables
199|[ManagedIdentityCredential][managed_id_cred_ref]|authenticate the managed identity of an Azure resource
200
201### Authenticating Service Principals
202
203|credential|usage
204|-|-
205|[ClientSecretCredential][client_secret_cred_ref]| authenticate a service principal using a secret
206|[CertificateCredential][cert_cred_ref]| authenticate a service principal using a certificate
207
208### Authenticating Users
209
210|credential|usage
211|-|-
212|[InteractiveBrowserCredential][interactive_cred_ref]|interactively authenticate a user with the default web browser
213|[DeviceCodeCredential][device_code_cred_ref]| interactively authenticate a user on a device with limited UI
214|[UsernamePasswordCredential][userpass_cred_ref]| authenticate a user with a username and password
215
216### Authenticating via Development Tools
217
218|credential|usage
219|-|-
220|[AzureCliCredential][cli_cred_ref]|authenticate as the user signed in to the Azure CLI
221|[VisualStudioCodeCredential][vscode_cred_ref]|authenticate as the user signed in to the Visual Studio Code Azure Account extension
222
223## Environment Variables
224
225[DefaultAzureCredential][default_cred_ref] and
226[EnvironmentCredential][environment_cred_ref] can be configured with
227environment variables. Each type of authentication requires values for specific
228variables:
229
230#### Service principal with secret
231|variable name|value
232|-|-
233|`AZURE_CLIENT_ID`|id of an Azure Active Directory application
234|`AZURE_TENANT_ID`|id of the application's Azure Active Directory tenant
235|`AZURE_CLIENT_SECRET`|one of the application's client secrets
236
237#### Service principal with certificate
238|variable name|value
239|-|-
240|`AZURE_CLIENT_ID`|id of an Azure Active Directory application
241|`AZURE_TENANT_ID`|id of the application's Azure Active Directory tenant
242|`AZURE_CLIENT_CERTIFICATE_PATH`|path to a PEM-encoded certificate file including private key (without password protection)
243
244#### Username and password
245|variable name|value
246|-|-
247|`AZURE_CLIENT_ID`|id of an Azure Active Directory application
248|`AZURE_USERNAME`|a username (usually an email address)
249|`AZURE_PASSWORD`|that user's password
250
251Configuration is attempted in the above order. For example, if values for a
252client secret and certificate are both present, the client secret will be used.
253
254## Troubleshooting
255
256### Error Handling
257
258Credentials raise `CredentialUnavailableError` when they're unable to attempt
259authentication because they lack required data or state. For example,
260[EnvironmentCredential][environment_cred_ref] will raise this exception when
261[its configuration](#environment-variables "its configuration") is incomplete.
262
263Credentials raise `azure.core.exceptions.ClientAuthenticationError` when they fail
264to authenticate. `ClientAuthenticationError` has a `message` attribute which
265describes why authentication failed. When raised by
266`DefaultAzureCredential` or `ChainedTokenCredential`,
267the message collects error messages from each credential in the chain.
268
269For more details on handling specific Azure Active Directory errors please refer to the
270Azure Active Directory
271[error code documentation](https://docs.microsoft.com/azure/active-directory/develop/reference-aadsts-error-codes).
272
273### Logging
274
275This library uses the standard
276[logging](https://docs.python.org/3/library/logging.html) library for logging.
277Credentials log basic information, including HTTP sessions (URLs, headers, etc.) at INFO level. These log entries do not contain authentication secrets.
278
279Detailed 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:
280
281```py
282credential = DefaultAzureCredential(logging_enable=True)
283```
284
285> CAUTION: DEBUG level logs from credentials contain sensitive information.
286> These logs must be protected to avoid compromising account security.
287
288## Next steps
289
290### Client library support
291
292This is an incomplete list of client libraries accepting Azure Identity
293credentials. You can learn more about these libraries, and find additional
294documentation of them, at the links below.
295
296- [azure-appconfiguration][azure_appconfiguration]
297- [azure-eventhub][azure_eventhub]
298- [azure-keyvault-certificates][azure_keyvault_certificates]
299- [azure-keyvault-keys][azure_keyvault_keys]
300- [azure-keyvault-secrets][azure_keyvault_secrets]
301- [azure-storage-blob][azure_storage_blob]
302- [azure-storage-queue][azure_storage_queue]
303
304### Provide Feedback
305
306If you encounter bugs or have suggestions, please
307[open an issue](https://github.com/Azure/azure-sdk-for-python/issues).
308
309## Contributing
310
311This project welcomes contributions and suggestions. Most contributions require
312you to agree to a Contributor License Agreement (CLA) declaring that you have
313the right to, and actually do, grant us the rights to use your contribution.
314For details, visit [https://cla.microsoft.com](https://cla.microsoft.com).
315
316When you submit a pull request, a CLA-bot will automatically determine whether
317you need to provide a CLA and decorate the PR appropriately (e.g., label,
318comment). Simply follow the instructions provided by the bot. You will only
319need to do this once across all repos using our CLA.
320
321This project has adopted the
322[Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
323For more information, see the
324[Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/)
325or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any
326additional questions or comments.
327
328[azure_appconfiguration]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/appconfiguration/azure-appconfiguration
329[azure_cli]: https://docs.microsoft.com/cli/azure
330[azure_core_transport_doc]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/core/azure-core/CLIENT_LIBRARY_DEVELOPER.md#transport
331[azure_eventhub]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventhub/azure-eventhub
332[azure_keyvault_certificates]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk//keyvault/azure-keyvault-certificates
333[azure_keyvault_keys]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/keyvault/azure-keyvault-keys
334[azure_keyvault_secrets]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/keyvault/azure-keyvault-secrets
335[azure_storage_blob]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/storage/azure-storage-blob
336[azure_storage_queue]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/storage/azure-storage-queue
337[cert_cred_ref]: https://aka.ms/azsdk/python/identity/docs#azure.identity.CertificateCredential
338[chain_cred_ref]: https://aka.ms/azsdk/python/identity/docs#azure.identity.ChainedTokenCredential
339[cli_cred_ref]: https://aka.ms/azsdk/python/identity/docs#azure.identity.AzureCliCredential
340[client_secret_cred_ref]: https://aka.ms/azsdk/python/identity/docs#azure.identity.ClientSecretCredential
341[default_cred_ref]: https://aka.ms/azsdk/python/identity/docs#azure.identity.DefaultAzureCredential
342[device_code_cred_ref]: https://aka.ms/azsdk/python/identity/docs#azure.identity.DeviceCodeCredential
343[environment_cred_ref]: https://aka.ms/azsdk/python/identity/docs#azure.identity.EnvironmentCredential
344[interactive_cred_ref]: https://aka.ms/azsdk/python/identity/docs#azure.identity.InteractiveBrowserCredential
345[managed_id_cred_ref]: https://aka.ms/azsdk/python/identity/docs#azure.identity.ManagedIdentityCredential
346[ref_docs]: https://aka.ms/azsdk/python/identity/docs
347[ref_docs_aio]: https://aka.ms/azsdk/python/identity/aio/docs
348[userpass_cred_ref]: https://aka.ms/azsdk/python/identity/docs#azure.identity.UsernamePasswordCredential
349[vscode_cred_ref]: https://aka.ms/azsdk/python/identity/docs#azure.identity.VisualStudioCodeCredential
350
351![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-python%2Fsdk%2Fidentity%2Fazure-identity%2FREADME.png)
352