-
Notifications
You must be signed in to change notification settings - Fork 17
Managed Identity documentation #91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b368e6c
Merge pull request #90 from MicrosoftDocs/main
localden 19dc1fd
Create managed-identity.md
localden 2a8f2bc
Merge branch 'ddelimarsky/mi' of https://github.com/MicrosoftDocs/mic…
localden ac479cd
Update managed-identity.md
localden 6feee76
Add sample
localden 49c4b0d
Update managed-identity.md
localden 7a84070
Update managed-identity.md
localden 916dd97
Update managed-identity.md
localden 4eeacfe
Update msal-python-conceptual/advanced/managed-identity.md
localden b33355f
Update msal-python-conceptual/advanced/managed-identity.md
localden c362e24
Update managed-identity.md
localden 5ca3161
Update managed-identity.md
localden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| --- | ||
| title: Using Managed Identity | ||
| description: Learn how to use Managed Identity with Microsoft Authentication Library (MSAL) for Python. | ||
| author: localden | ||
|
|
||
| ms.service: msal | ||
| ms.subservice: msal-python | ||
| ms.topic: conceptual | ||
| ms.date: 06/25/2024 | ||
| ms.author: ddelimarsky | ||
| ms.reviewer: rayluo | ||
| --- | ||
|
|
||
| # Using Managed Identity | ||
|
|
||
| [Managed identity](/entra/identity/managed-identities-azure-resources/overview) enables developers to remove the need to store, rotate, and otherwise handle credentials for their applications running in the Microsoft Azure cloud. Microsoft Authentication Library (MSAL) for Python supports using authentication with managed identity on supported Azure workloads. | ||
|
|
||
| >[!NOTE] | ||
| >Managed Identity in MSAL Python is supported since version [1.29.0](https://pypi.org/project/msal/1.29.0/) of the [`msal` package](https://pypi.org/project/msal/). | ||
|
|
||
| MSAL Python supports acquiring tokens through the managed identity service when used with applications running inside Azure infrastructure, such as: | ||
|
|
||
| - [Azure App Service](https://azure.microsoft.com/products/app-service/) (API version `2019-08-01`) | ||
| - [Azure VMs](https://azure.microsoft.com/free/virtual-machines/) | ||
| - [Azure Arc](/azure/azure-arc/overview) | ||
| - [Azure Cloud Shell](/azure/cloud-shell/overview) | ||
| - [Azure Service Fabric](/azure/service-fabric/service-fabric-overview) | ||
| - [Azure ML](/azure/machine-learning/how-to-identity-based-service-authentication) | ||
|
|
||
| For a complete list, refer to [Azure services that can use managed identities to access other services](/azure/active-directory/managed-identities-azure-resources/managed-identities-status). | ||
|
|
||
| ## Which SDK to use - Azure SDK or MSAL? | ||
|
|
||
| MSAL libraries provide lower level APIs that are closer to the OAuth2 and OIDC protocols. | ||
|
|
||
| Both MSAL Python and Azure SDK allow to acquire tokens via managed identity. Internally, Azure SDK uses MSAL Python, and it provides a higher-level API via its [`DefaultAzureCredential`](/python/api/azure-identity/azure.identity.defaultazurecredential) and [`ManagedIdentityCredential`](/python/api/azure-identity/azure.identity.ManagedIdentityCredential) abstractions. | ||
|
|
||
| If your application already uses one of the SDKs, continue using the same SDK. | ||
|
|
||
| - Use Azure SDK if you are writing a new application and plan to call other Azure resources, as this SDK provides a better developer experience by allowing the app to run on private developer machines where managed identity doesn't exist. | ||
| - Use MSAL if you need to call other downstream web APIs like Microsoft Graph or your own web API. | ||
|
|
||
| ## How to use managed identities | ||
|
|
||
| There are two types of managed identities available to developers - **system-assigned** and **user-assigned**. You can learn more about the differences in the [Managed identity types](/azure/active-directory/managed-identities-azure-resources/overview#managed-identity-types) article. MSAL Python supports acquiring tokens with both. MSAL Python logging allows to keep track of requests and related metadata. | ||
|
|
||
| Prior to using managed identities from MSAL Python, developers must enable them for the resources they want to use through Azure CLI or the Azure Portal. | ||
|
|
||
| ## Examples | ||
|
|
||
| In both system- and user-assigned identities, developers need to use <xref:msal.managed_identity.ManagedIdentityClient> to access managed identities. | ||
|
|
||
| ### System-assigned managed identities | ||
|
|
||
| System-assigned managed identities can be used by instantiating <xref:msal.managed_identity.SystemAssignedManagedIdentity> and passing to <xref:msal.managed_identity.ManagedIdentityClient>. | ||
|
|
||
| >[!NOTE] | ||
| >You need to include a `http_client` reference, which can be set to `requests.Session()`. This enables MSAL to maintain a pool of connections to the IMDS endpoint. | ||
|
|
||
| You can specify the target resource scope when calling [`acquire_token_for_client`](xref:msal.managed_identity.ManagedIdentityClient.acquire_token_for_client). | ||
|
|
||
| ```python | ||
| import msal | ||
| import requests | ||
|
|
||
| managed_identity = msal.SystemAssignedManagedIdentity() | ||
|
|
||
| global_app = msal.ManagedIdentityClient(managed_identity, http_client=requests.Session()) | ||
|
|
||
| result = global_app.acquire_token_for_client(resource='https://vault.azure.net') | ||
|
|
||
| if "access_token" in result: | ||
| print("Token obtained!") | ||
| ``` | ||
|
|
||
| >[!IMPORTANT] | ||
| >You need to enable a system-assigned identity for the resource where the Python code runs; otherwise, no token will be returned. | ||
|
|
||
| ### User-assigned managed identities | ||
|
|
||
| User-assigned managed identities can be used by instantiating <xref:msal.managed_identity.UserAssignedManagedIdentity> and passing to <xref:msal.managed_identity.ManagedIdentityClient>. You will need to specify the **one of the following**: | ||
|
|
||
| - Client ID (`client_id`) | ||
| - Resource ID (`resource_id`) | ||
| - Object ID (`object_id`) | ||
|
|
||
| >[!NOTE] | ||
| >You need to include a `http_client` reference, which can be set to `requests.Session()`. This enables MSAL to maintain a pool of connections to the IMDS endpoint. | ||
|
|
||
| You can specify the target resource scope when calling [`acquire_token_for_client`](xref:msal.managed_identity.ManagedIdentityClient.acquire_token_for_client). | ||
|
|
||
| ```python | ||
| import msal | ||
| import requests | ||
|
|
||
| managed_identity = msal.UserAssignedManagedIdentity(client_id='YOUR_CLIENT_ID') | ||
localden marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| global_app = msal.ManagedIdentityClient(managed_identity, http_client=requests.Session()) | ||
|
|
||
| result = global_app.acquire_token_for_client(resource='https://vault.azure.net') | ||
|
|
||
| if "access_token" in result: | ||
| print("Token obtained!") | ||
| ``` | ||
|
|
||
| >[!NOTE] | ||
| >MSAL Python's [built-in Managed Identity (MI) sample](https://github.com/AzureAD/microsoft-authentication-library-for-python/blob/1.29.0/sample/managed_identity_sample.py#L38-L42) showcases how user-assigned managed identity can be inferred from environment variables. It's an advanced usage pattern that can be used instead of explicit definition of the client ID in code. | ||
|
|
||
| >[!IMPORTANT] | ||
| >You need to attach a user-assigned identity for the resource where the Python code runs; otherwise, no token will be returned. If an incorrect identifier is used for the user-assigned managed identity, no token will be returned as well. | ||
|
|
||
| ## Caching | ||
|
|
||
| By default, MSAL Python supports in-memory caching. | ||
|
|
||
| >[!IMPORTANT] | ||
| >MSAL Python also supports cache extensibility for managed identity, so that you may persist the token cache on disk. This can be useful if you are writing a command-line script and a few other limited scenarios. We **do not recommend** sharing managed identity token cache among multiple machines as this can result in unexpected access behaviors for users of the cache. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.