-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[Core] PREVIEW: Support MSAL managed identity #31092
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
Conversation
️✔️AzureCLI-FullTest
|
|
Hi @jiasli, |
️✔️AzureCLI-BreakingChangeTest
|
|
Thank you for your contribution! We will review the pull request and get back to you soon. |
|
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). pip install azdev --upgrade
azdev setup -c <your azure-cli repo path> -r <your azure-cli-extensions repo path>
|
rayluo
left a comment
There was a problem hiding this 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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
azure-cli/src/azure-cli-core/azure/cli/core/_profile.py
Lines 817 to 819 in 17444a3
| 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.') |
Bash script to create an Azure VM for testing managed identityThis script does below things:
# 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 |
| 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) |
There was a problem hiding this comment.
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.
|
Rerun CI after #31115. |
|
Azure Pipelines successfully started running 3 pipeline(s). |
|
Rerun CI after #31117. |
|
Azure Pipelines successfully started running 3 pipeline(s). |
|
MSAL supports below managed identity variations:
|
Related command
az login --identityDescription
Migrating from
msrestazureto 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=trueor set environment variableAZURE_CORE_USE_MSAL_MANAGED_IDENTITY=trueto enable it.Testing Guide
History Notes
[Core] PREVIEW: Support managed identity authentication with MSAL. Run
az config set core.use_msal_managed_identity=trueor set environment variableAZURE_CORE_USE_MSAL_MANAGED_IDENTITY=trueto enable it