Improve Error Handling for 'az login --identity; with Multiple Assigned Identities #31012
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.
Related command
az login --identity
Description
This PR improves login error handling for situations where multiple identities are assigned to a resource by surfacing the error_description returned by ARM in the HTTPError response body. The current CLI logic for
az login --identityreturns a 400 Bad Request error without surfacing the actual error, making it unactionable for the user.Services like AKS make use of this, where the enablement of certain features creates a secondary identity. Doing so will cause AKS nodes and pods to no longer be authenticate to Azure resources simply by running
az login --identity, returning a generic 'Failed to connect to MSI' error message.During tests, I noticed the error object returned from the MSRestAzure library does not include the response body. Adding stream=True to the request mitigated, but this library has been deprecated and is read-only. Because of this, I've added the logic to replay the request and extract the error_description in the adal_authentication file.
Testing Guide
az aks create --resource-group <rgName>--name <cluster_name> --kubernetes-version 1.31.5 --enable-cluster-autoscaler --min-count 1 --max-count 30 --auto-upgrade-channel stable --node-count 1 --node-vm-size Standard_E4bs_v5 --network-plugin kubenet --vnet-subnet-id $SUBNET_ID --node-osdisk-size 128 --enable-managed-identity --enable-addons monitoring --max-pods 250 --enable-node-public-ip --generate-ssh-keys --tier Standardaz login --identityto observe it's working.az aks update -g <rgName> -n <cluster_name> --enable-cost-analysisaz login --identityand observe the 400 Bad Request error.az login --identityagain and observe the error message now includes additional error details:ERROR: Failed to connect to MSI. Please make sure MSI is configured correctly. Get Token request returned http error: 400, reason: Bad Request, details: Multiple user assigned identities exist, please specify the clientId / resourceId of the identity in the token requestHistory Notes
[Azure CLI] Append HTTPError response body to error message for
az login --identitycommand when multiple identities are present.