Skip to content

Commit ac2ce1e

Browse files
authored
Merge pull request #92 from MicrosoftDocs/main
Update with MI docs
2 parents 02f9488 + 8297d80 commit ac2ce1e

File tree

2 files changed

+119
-1
lines changed

2 files changed

+119
-1
lines changed

msal-python-conceptual/TOC.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
href: advanced/aad-b2c.md
2626
- name: Active Directory Federation Services (ADFS) Support
2727
href: advanced/msal-python-adfs-support.md
28-
- name: National clouds
28+
- name: Using Managed Identity
29+
href: advanced/managed-identity.md
2930
- name: Username and password authentication
3031
href: advanced/username-password-authentication.md
3132
- name: How to generate secret and/or certificate for Confidential Client
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
---
2+
title: Using Managed Identity
3+
description: Learn how to use Managed Identity with Microsoft Authentication Library (MSAL) for Python.
4+
author: localden
5+
6+
ms.service: msal
7+
ms.subservice: msal-python
8+
ms.topic: conceptual
9+
ms.date: 06/25/2024
10+
ms.author: ddelimarsky
11+
ms.reviewer: rayluo
12+
---
13+
14+
# Using Managed Identity
15+
16+
[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.
17+
18+
>[!NOTE]
19+
>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/).
20+
21+
MSAL Python supports acquiring tokens through the managed identity service when used with applications running inside Azure infrastructure, such as:
22+
23+
- [Azure App Service](https://azure.microsoft.com/products/app-service/) (API version `2019-08-01`)
24+
- [Azure VMs](https://azure.microsoft.com/free/virtual-machines/)
25+
- [Azure Arc](/azure/azure-arc/overview)
26+
- [Azure Cloud Shell](/azure/cloud-shell/overview)
27+
- [Azure Service Fabric](/azure/service-fabric/service-fabric-overview)
28+
- [Azure ML](/azure/machine-learning/how-to-identity-based-service-authentication)
29+
30+
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).
31+
32+
## Which SDK to use - Azure SDK or MSAL?
33+
34+
MSAL libraries provide lower level APIs that are closer to the OAuth2 and OIDC protocols.
35+
36+
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.
37+
38+
If your application already uses one of the SDKs, continue using the same SDK.
39+
40+
- 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.
41+
- Use MSAL if you need to call other downstream web APIs like Microsoft Graph or your own web API.
42+
43+
## How to use managed identities
44+
45+
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.
46+
47+
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.
48+
49+
## Examples
50+
51+
In both system- and user-assigned identities, developers need to use <xref:msal.managed_identity.ManagedIdentityClient> to access managed identities.
52+
53+
### System-assigned managed identities
54+
55+
System-assigned managed identities can be used by instantiating <xref:msal.managed_identity.SystemAssignedManagedIdentity> and passing to <xref:msal.managed_identity.ManagedIdentityClient>.
56+
57+
>[!NOTE]
58+
>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.
59+
60+
You can specify the target resource scope when calling [`acquire_token_for_client`](xref:msal.managed_identity.ManagedIdentityClient.acquire_token_for_client).
61+
62+
```python
63+
import msal
64+
import requests
65+
66+
managed_identity = msal.SystemAssignedManagedIdentity()
67+
68+
global_app = msal.ManagedIdentityClient(managed_identity, http_client=requests.Session())
69+
70+
result = global_app.acquire_token_for_client(resource='https://vault.azure.net')
71+
72+
if "access_token" in result:
73+
print("Token obtained!")
74+
```
75+
76+
>[!IMPORTANT]
77+
>You need to enable a system-assigned identity for the resource where the Python code runs; otherwise, no token will be returned.
78+
79+
### User-assigned managed identities
80+
81+
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**:
82+
83+
- Client ID (`client_id`)
84+
- Resource ID (`resource_id`)
85+
- Object ID (`object_id`)
86+
87+
>[!NOTE]
88+
>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.
89+
90+
You can specify the target resource scope when calling [`acquire_token_for_client`](xref:msal.managed_identity.ManagedIdentityClient.acquire_token_for_client).
91+
92+
```python
93+
import msal
94+
import requests
95+
96+
managed_identity = msal.UserAssignedManagedIdentity(client_id='YOUR_CLIENT_ID')
97+
98+
global_app = msal.ManagedIdentityClient(managed_identity, http_client=requests.Session())
99+
100+
result = global_app.acquire_token_for_client(resource='https://vault.azure.net')
101+
102+
if "access_token" in result:
103+
print("Token obtained!")
104+
```
105+
106+
>[!NOTE]
107+
>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.
108+
109+
>[!IMPORTANT]
110+
>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.
111+
112+
## Caching
113+
114+
By default, MSAL Python supports in-memory caching.
115+
116+
>[!IMPORTANT]
117+
>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.

0 commit comments

Comments
 (0)