Skip to content

Conversation

@jiasli
Copy link
Member

@jiasli jiasli commented Mar 20, 2025

Related command
az login --identity

Description
Migrating from msrestazure to MSAL for managed identity authentication (#25959) in one shot can be risky.

This PR reuses the code of #29187, #30267 to add a preview of managed identity authentication with MSAL. Run az config set core.use_msal_managed_identity=true or set environment variable AZURE_CORE_USE_MSAL_MANAGED_IDENTITY=true to enable it.

Testing Guide

# system-assigned
az login --identity

# user-assigned
az login --identity --client-id xxx
az login --identity --object-id xxx
az login --identity --resource-id /subscriptions/xxx/resourcegroupsxxx/providers/microsoft.managedidentity/userassignedidentities/xxx

az group list
az account get-access-token

History Notes

[Core] PREVIEW: Support managed identity authentication with MSAL. Run az config set core.use_msal_managed_identity=true or set environment variable AZURE_CORE_USE_MSAL_MANAGED_IDENTITY=true to enable it

@azure-client-tools-bot-prd
Copy link

azure-client-tools-bot-prd bot commented Mar 20, 2025

️✔️AzureCLI-FullTest
️✔️acr
️✔️2020-09-01-hybrid
️✔️3.12
️✔️3.9
️✔️latest
️✔️3.12
️✔️3.9
️✔️acs
️✔️2020-09-01-hybrid
️✔️3.12
️✔️3.9
️✔️latest
️✔️3.12
️✔️3.9
️✔️advisor
️✔️latest
️✔️3.12
️✔️3.9
️✔️ams
️✔️latest
️✔️3.12
️✔️3.9
️✔️apim
️✔️latest
️✔️3.12
️✔️3.9
️✔️appconfig
️✔️latest
️✔️3.12
️✔️3.9
️✔️appservice
️✔️latest
️✔️3.12
️✔️3.9
️✔️aro
️✔️latest
️✔️3.12
️✔️3.9
️✔️backup
️✔️latest
️✔️3.12
️✔️3.9
️✔️batch
️✔️latest
️✔️3.12
️✔️3.9
️✔️batchai
️✔️latest
️✔️3.12
️✔️3.9
️✔️billing
️✔️latest
️✔️3.12
️✔️3.9
️✔️botservice
️✔️latest
️✔️3.12
️✔️3.9
️✔️cdn
️✔️latest
️✔️3.12
️✔️3.9
️✔️cloud
️✔️latest
️✔️3.12
️✔️3.9
️✔️cognitiveservices
️✔️latest
️✔️3.12
️✔️3.9
️✔️compute_recommender
️✔️latest
️✔️3.12
️✔️3.9
️✔️computefleet
️✔️latest
️✔️3.12
️✔️3.9
️✔️config
️✔️latest
️✔️3.12
️✔️3.9
️✔️configure
️✔️latest
️✔️3.12
️✔️3.9
️✔️consumption
️✔️latest
️✔️3.12
️✔️3.9
️✔️container
️✔️latest
️✔️3.12
️✔️3.9
️✔️containerapp
️✔️latest
️✔️3.12
️✔️3.9
️✔️core
️✔️2018-03-01-hybrid
️✔️3.12
️✔️3.9
️✔️2019-03-01-hybrid
️✔️3.12
️✔️3.9
️✔️2020-09-01-hybrid
️✔️3.12
️✔️3.9
️✔️latest
️✔️3.12
️✔️3.9
️✔️cosmosdb
️✔️latest
️✔️3.12
️✔️3.9
️✔️databoxedge
️✔️2019-03-01-hybrid
️✔️3.12
️✔️3.9
️✔️2020-09-01-hybrid
️✔️3.12
️✔️3.9
️✔️latest
️✔️3.12
️✔️3.9
️✔️dls
️✔️latest
️✔️3.12
️✔️3.9
️✔️dms
️✔️latest
️✔️3.12
️✔️3.9
️✔️eventgrid
️✔️latest
️✔️3.12
️✔️3.9
️✔️eventhubs
️✔️latest
️✔️3.12
️✔️3.9
️✔️feedback
️✔️latest
️✔️3.12
️✔️3.9
️✔️find
️✔️latest
️✔️3.12
️✔️3.9
️✔️hdinsight
️✔️latest
️✔️3.12
️✔️3.9
️✔️identity
️✔️latest
️✔️3.12
️✔️3.9
️✔️iot
️✔️2019-03-01-hybrid
️✔️3.12
️✔️3.9
️✔️2020-09-01-hybrid
️✔️3.12
️✔️3.9
️✔️latest
️✔️3.12
️✔️3.9
️✔️keyvault
️✔️2018-03-01-hybrid
️✔️3.12
️✔️3.9
️✔️2020-09-01-hybrid
️✔️3.12
️✔️3.9
️✔️latest
️✔️3.12
️✔️3.9
️✔️lab
️✔️latest
️✔️3.12
️✔️3.9
️✔️managedservices
️✔️latest
️✔️3.12
️✔️3.9
️✔️maps
️✔️latest
️✔️3.12
️✔️3.9
️✔️marketplaceordering
️✔️latest
️✔️3.12
️✔️3.9
️✔️monitor
️✔️latest
️✔️3.12
️✔️3.9
️✔️mysql
️✔️latest
️✔️3.12
️✔️3.9
️✔️netappfiles
️✔️latest
️✔️3.12
️✔️3.9
️✔️network
️✔️2018-03-01-hybrid
️✔️3.12
️✔️3.9
️✔️latest
️✔️3.12
️✔️3.9
️✔️policyinsights
️✔️latest
️✔️3.12
️✔️3.9
️✔️privatedns
️✔️latest
️✔️3.12
️✔️3.9
️✔️profile
️✔️latest
️✔️3.12
️✔️3.9
️✔️rdbms
️✔️latest
️✔️3.12
️✔️3.9
️✔️redis
️✔️latest
️✔️3.12
️✔️3.9
️✔️relay
️✔️latest
️✔️3.12
️✔️3.9
️✔️resource
️✔️2018-03-01-hybrid
️✔️3.12
️✔️3.9
️✔️2019-03-01-hybrid
️✔️3.12
️✔️3.9
️✔️latest
️✔️3.12
️✔️3.9
️✔️role
️✔️latest
️✔️3.12
️✔️3.9
️✔️search
️✔️latest
️✔️3.12
️✔️3.9
️✔️security
️✔️latest
️✔️3.12
️✔️3.9
️✔️servicebus
️✔️latest
️✔️3.12
️✔️3.9
️✔️serviceconnector
️✔️latest
️✔️3.12
️✔️3.9
️✔️servicefabric
️✔️latest
️✔️3.12
️✔️3.9
️✔️signalr
️✔️latest
️✔️3.12
️✔️3.9
️✔️sql
️✔️latest
️✔️3.12
️✔️3.9
️✔️sqlvm
️✔️latest
️✔️3.12
️✔️3.9
️✔️storage
️✔️2018-03-01-hybrid
️✔️3.12
️✔️3.9
️✔️2019-03-01-hybrid
️✔️3.12
️✔️3.9
️✔️2020-09-01-hybrid
️✔️3.12
️✔️3.9
️✔️latest
️✔️3.12
️✔️3.9
️✔️synapse
️✔️latest
️✔️3.12
️✔️3.9
️✔️telemetry
️✔️2018-03-01-hybrid
️✔️3.12
️✔️3.9
️✔️2019-03-01-hybrid
️✔️3.12
️✔️3.9
️✔️2020-09-01-hybrid
️✔️3.12
️✔️3.9
️✔️latest
️✔️3.12
️✔️3.9
️✔️util
️✔️latest
️✔️3.12
️✔️3.9
️✔️vm
️✔️2018-03-01-hybrid
️✔️3.12
️✔️3.9
️✔️2019-03-01-hybrid
️✔️3.12
️✔️3.9
️✔️2020-09-01-hybrid
️✔️3.12
️✔️3.9
️✔️latest
️✔️3.12
️✔️3.9

@azure-client-tools-bot-prd
Copy link

Hi @jiasli,
Since the current milestone time is less than 7 days, this pr will be reviewed in the next milestone.

@azure-client-tools-bot-prd
Copy link

azure-client-tools-bot-prd bot commented Mar 20, 2025

️✔️AzureCLI-BreakingChangeTest
️✔️Non Breaking Changes

@yonzhan
Copy link
Collaborator

yonzhan commented Mar 20, 2025

Thank you for your contribution! We will review the pull request and get back to you soon.

@github-actions
Copy link

The git hooks are available for azure-cli and azure-cli-extensions repos. They could help you run required checks before creating the PR.

Please sync the latest code with latest dev branch (for azure-cli) or main branch (for azure-cli-extensions).
After that please run the following commands to enable git hooks:

pip install azdev --upgrade
azdev setup -c <your azure-cli repo path> -r <your azure-cli-extensions repo path>

@microsoft-github-policy-service microsoft-github-policy-service bot added the Auto-Assign Auto assign by bot label Mar 20, 2025
@microsoft-github-policy-service microsoft-github-policy-service bot added the ARM az resource/group/lock/tag/deployment/policy/managementapp/account management-group label Mar 20, 2025
@microsoft-github-policy-service microsoft-github-policy-service bot added Account az login/account Core CLI core infrastructure labels Mar 20, 2025
@jiasli jiasli requested a review from rayluo March 20, 2025 10:05
@yonzhan yonzhan removed the ARM az resource/group/lock/tag/deployment/policy/managementapp/account management-group label Mar 20, 2025
Copy link
Member

@rayluo rayluo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM. Just adding a minor comment below. Approving.

self._msal_client = ManagedIdentityClient(SystemAssignedManagedIdentity(), http_client=requests.Session())
if client_id or resource_id or object_id:
managed_identity = UserAssignedManagedIdentity(
client_id=client_id, resource_id=resource_id, object_id=object_id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note (unblocking)
If there is not exactly one ID being used, a ManagedIdentityError exception will be thrown here. A fyi, in case you would want to catch it and provide your more suitable error message.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already check this by ourselves:

id_arg_count = len([arg for arg in (client_id, object_id, resource_id) if arg])
if id_arg_count > 1:
raise CLIError('Usage error: Provide only one of --client-id, --object-id, --resource-id.')

@jiasli
Copy link
Member Author

jiasli commented Mar 21, 2025

Bash script to create an Azure VM for testing managed identity

This script does below things:

  1. Create a Linux Azure VM with SSH key configured
  2. Only open the SSH port to Corp Net
  3. Assign a system-assigned managed identity and a user-assigned managed identity to the VM
  4. Create Reader role assignments on subscription to both managed identities
  5. Map/"Smuggle" the managed identity IMDS endpoint to a local machine (allow MI endpoint changing through environment variable AzureAD/microsoft-authentication-library-for-python#754)
  6. Because allow MI endpoint changing through environment variable AzureAD/microsoft-authentication-library-for-python#754 has been reverted (Revert "allow MI endpoint changing through environment variable" AzureAD/microsoft-authentication-library-for-python#769), you need to manually edit MSAL's http://169.254.169.254 endpoint in _obtain_token_on_azure_vm
# Populate below variables
tenant=xxx
sub=xxx
vm=xxx
ssh_key='ssh-rsa AAAAB...'

mi=${vm}mi
rg=${vm}rg
username=azureuser
location=southeastasia
corpnet_prefix=CorpNetPublic

az login --tenant $tenant
az account set -s $sub

az group create -g $rg -l $location
ip=$(az vm create -n $vm -g $rg --image Ubuntu2404 --size Standard_B2s --nsg-rule NONE --admin-username $username --ssh-key-values "$ssh_key" --assign-identity [system] --query publicIpAddress --output tsv)
az network nsg rule create -g $rg --nsg-name "${vm}NSG" -n allow_ssh --priority 100 --source-address-prefixes $corpnet_prefix --destination-port-ranges 22 --direction Inbound --access Allow --protocol Tcp --description "Allow SSH from CorpNet"

vm_object_id=$(az vm identity show -n $vm -g $rg --query principalId --output tsv)
az role assignment create --role Reader --assignee $vm_object_id --scope /subscriptions/$sub

mi_client_id=$(az identity create --name $mi -g $rg --query clientId --output tsv)
az vm identity assign -g $rg -n $vm --identities /subscriptions/$sub/resourcegroups/$rg/providers/Microsoft.ManagedIdentity/userAssignedIdentities/$mi
az role assignment create --role Reader --assignee $mi_client_id --scope /subscriptions/$sub

echo ssh -L 8000:169.254.169.254:80 -o "StrictHostKeyChecking no" $username@$ip

# Test MI auth
mi_client_id=$(az identity show --name $mi -g $rg --query clientId --output tsv)
mi_object_id=$(az identity show --name $mi -g $rg --query principalId --output tsv)
mi_resource_id=$(az identity show --name $mi -g $rg --query id --output tsv)

echo az login --identity
echo az login --identity --client-id $mi_client_id
echo az login --identity --object-id $mi_object_id
echo az login --identity --resource-id $mi_resource_id
echo az group list
echo az account get-access-token

evelyn-ys
evelyn-ys previously approved these changes Mar 24, 2025
subscriptions = subscription_finder.find_using_specific_tenant(tenant, cred)
base_name = ('{}-{}'.format(identity_type, identity_id) if identity_id else identity_type)
user = _USER_ASSIGNED_IDENTITY if identity_id else _SYSTEM_ASSIGNED_IDENTITY
base_name = ('{}-{}'.format(identity_id_type, identity_id_value) if identity_id_value else identity_id_type)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous variable name identity_type is not accurate. Identity type means systemAssignedIdentity or userAssignedIdentity.

@jiasli
Copy link
Member Author

jiasli commented Mar 24, 2025

Rerun CI after #31115.

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@jiasli
Copy link
Member Author

jiasli commented Mar 25, 2025

Rerun CI after #31117.

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@jiasli jiasli changed the title [Core] PREVIEW: Support managed identity authentication with MSAL [Core] PREVIEW: Support MSAL managed identity Mar 25, 2025
@jiasli jiasli merged commit b9d34b4 into Azure:dev Mar 25, 2025
53 checks passed
@jiasli jiasli deleted the mi-msal branch March 25, 2025 06:29
@jiasli
Copy link
Member Author

jiasli commented Apr 10, 2025

MSAL supports below managed identity variations:

https://github.com/AzureAD/microsoft-authentication-library-for-python/blob/45e39669ca8e3fd768610d748235c3775ca4db84/msal/managed_identity.py#L385-L391

  1. APP_SERVICE: TO BE TESTED
  2. AZURE_ARC: Already tested in [Core] PREVIEW: Support managed identity on Azure Arc-enabled Windows server #29187, [Core] PREVIEW: Support managed identity on Azure Arc-enabled Linux server #30267
  3. CLOUD_SHELL: Implemented by PublicClientApplication. Already tested in [Core] Use MSAL for Cloud Shell authentication #29637
  4. MACHINE_LEARNING: TO BE TESTED
  5. SERVICE_FABRIC: TO BE TESTED
  6. DEFAULT_TO_VM: Already tested by this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Account az login/account Auto-Assign Auto assign by bot Core CLI core infrastructure

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants