Skip to content
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

Managed Identity for Azure VM, App Service, Service Fabric, etc. #480

Merged
merged 6 commits into from
Jun 22, 2024
Merged

Conversation

rayluo
Copy link
Collaborator

@rayluo rayluo commented Jun 2, 2022

Note: This is a proof-of-concept, which means there is no guarantee that this behavior will be eventually included into MSAL Python.

There are two new APIs added.

  • The high level API works for your confidential client which federated with a managed identity. This will be moved into a separated PR for its own consideration.

     import msal
     cca = msal.ConfidentialClientApplication(
         "my_client_id",
         client_credential=msal.SystemAssignedManagedIdentity(),  # Or it can be an msal.UserAssignedManagedIdentity(client_id="guid")
         ...)
     result = cca.acquire_token_for_client(scopes["scope1", "scope2"])  # It uses scopes
  • The low level API acquires token for managed identity

    import msal, requests
    mi = msal.ManagedIdentityClient(
        msal.SystemAssignedManagedIdentity(),  # Or it can be an msal.UserAssignedManagedIdentity(client_id="guid")
        http_client=requests.Session(),  # This is a required parameter
        token_cache=msal.TokenCache(),  # Optional. In your production code, you shall persist a SerializableTokenCache https://msal-python.readthedocs.io/en/latest/#msal.SerializableTokenCache
    )
    result = mi.acquire_token_for_client(resource="resource_abc")  # It uses resource

More details of the new APIs are available here.

  • In order to test this PR on Azure VM, you would need to:

    1. Create and then ssh into your Azure VM
    2. Install this proof-of-concept by pip install --force-reinstall "git+https://github.com/AzureAD/microsoft-authentication-library-for-python.git@mi"
    3. Write your script using the calling pattern above.
  • To test this on App Service

    1. Create your App Service with Python runtime
    2. SSH into your App Service
    3. Follow the last two steps of Azure VM test method
  • To test this on Azure Functions

    1. We have not yet tested it end-to-end, but the Managed Identity in Azure Function is expected to be the same as App Service.
  • To test this on Azure Automation (we have not tested this)

    1. Create your Automation account
    2. You will need to install msal package. But it seems Azure Automation only supports installing a package with its dependencies from PyPI. This PR is not currently available from PyPI, so, we are unable to test this.
    3. After step 2, you can create a new Python runbook and test the Managed Identity
  • To test this on Service Fabric

    1. We have not yet tested this end-to-end, but you can reference to the
      test steps in Azure SDK

Note:

  • At the end of this internal document, there are brief descriptions for the 6 variations of MIs.
  • Cloud Shell's IMDS is NOT part of this PR, because we already provide a higher level API for it in https://github.com/AzureAD/microsoft-authentication-library-for-python/pull/420.

Once merged, this PR will resolve #548. Also, it will officially close #487 as the callback is no longer needed.

msal/application.py Outdated Show resolved Hide resolved
msal/imds.py Outdated Show resolved Hide resolved
msal/imds.py Outdated Show resolved Hide resolved
msal/imds.py Outdated Show resolved Hide resolved
@jiasli
Copy link
Contributor

jiasli commented Mar 17, 2023

Cloud Shell is just one type of managed identity. Perhaps we should follow the same interface for Cloud Shell?

msal/imds.py Outdated Show resolved Hide resolved
msal/managed_identity.py Outdated Show resolved Hide resolved
msal/managed_identity.py Show resolved Hide resolved
msal/managed_identity.py Outdated Show resolved Hide resolved
msal/managed_identity.py Outdated Show resolved Hide resolved
@rayluo rayluo force-pushed the mi branch 2 times, most recently from e3e0412 to c336d2d Compare June 22, 2024 00:01
@jiasli
Copy link
Contributor

jiasli commented Jun 24, 2024

Congratulations on MSAL's support for managed identity. I am sure our customers will benefit from this great new feature! 🎉

@yonzhan
Copy link

yonzhan commented Jun 24, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
9 participants