[ACR] Support ARM scoped token in ACR for Azure Local Disconnected Operations#33290
[ACR] Support ARM scoped token in ACR for Azure Local Disconnected Operations#33290RohanPawarMSFT wants to merge 1 commit intoAzure:devfrom
Conversation
|
Validation for Azure CLI Full Test Starting...
Thanks for your contribution! |
|
Validation for Breaking Change Starting...
Thanks for your contribution! |
|
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>
|
@microsoft-github-policy-service agree [company="Microsoft"] |
There was a problem hiding this comment.
Pull request overview
This PR updates the ACR (az acr login) token acquisition flow to support Azure Local Disconnected Operations (ALDO) environments where the ACR audience resource may not be registered, by attempting an ARM-scoped token fallback for the ACR token exchange.
Changes:
- Added
_acquire_aad_token_for_acr_exchangeto first request an ACR-audience token and fall back to the cloud’s ARM resource when the ACR resource is not recognized. - Updated
_get_aad_token_after_challengeto use the new helper for token acquisition.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| try: | ||
| creds, _, tenant = profile.get_raw_token(subscription=subscription, resource=acr_resource) | ||
| except CLIError as e: | ||
| arm_resource = getattr(cli_ctx.cloud.endpoints, 'active_directory_resource_id', None) | ||
| if not arm_resource or not any(m in str(e) for m in unknown_resource_markers): | ||
| raise |
There was a problem hiding this comment.
The fallback logic won’t trigger for the common “invalid resource/scope” case because Profile.get_raw_token() ultimately raises azure.cli.core.azclierror.AuthenticationError via auth.util.check_result(), not knack.util.CLIError. As a result, ALDO/disconnected clouds will still fail instead of falling back to the ARM resource. Catch AuthenticationError (and/or the broader Azure CLI error base) here, and then apply the same unknown_resource_markers check before retrying with active_directory_resource_id.
| def _acquire_aad_token_for_acr_exchange(cli_ctx, profile): | ||
| unknown_resource_markers = ('invalid_resource', 'AADSTS50001') | ||
| subscription = get_subscription_id(cli_ctx) | ||
| acr_resource = "https://{}.azure.net".format(ACR_AUDIENCE_RESOURCE_NAME) | ||
| try: | ||
| creds, _, tenant = profile.get_raw_token(subscription=subscription, resource=acr_resource) | ||
| except CLIError as e: | ||
| arm_resource = getattr(cli_ctx.cloud.endpoints, 'active_directory_resource_id', None) | ||
| if not arm_resource or not any(m in str(e) for m in unknown_resource_markers): | ||
| raise | ||
| logger.warning("AAD resource '%s' not registered in this cloud; falling back to ARM resource '%s' " | ||
| "(expected in disconnected environments such as ALDO).", acr_resource, arm_resource) | ||
| creds, _, tenant = profile.get_raw_token(subscription=subscription, resource=arm_resource) | ||
| return creds, tenant |
There was a problem hiding this comment.
This new AAD→ARM fallback behavior is not covered by the existing mocked token-flow tests in test_acr_commands_mock.py (they only assert the happy-path get_raw_token call). Please add a unit test that simulates Profile.get_raw_token() failing with an invalid-resource AuthenticationError (e.g., AADSTS500011/invalid_resource) and asserts the second call uses cli_ctx.cloud.endpoints.active_directory_resource_id and the warning path is exercised.
Related command
az acr loginDescription
Azure Local Disconnected Operations (ALDO) does not use AAD scoped tokens for token exchange and rely on ARM scoped tokens to acquire ACR token.
Testing Guide
az acr login --debug
History Notes
[ACR] az acr login: Support fallback for ARM scoped tokens for token exchange
This checklist is used to make sure that common guidelines for a pull request are followed.
The PR title and description has followed the guideline in Submitting Pull Requests.
I adhere to the Command Guidelines.
I adhere to the Error Handling Guidelines.