{Core} Handle 403/DecodeError when listing tenants in az login - #33745
{Core} Handle 403/DecodeError when listing tenants in az login#33745Aditya Pujara (a0x1ab) with Copilot wants to merge 6 commits into
az login#33745Conversation
|
Validation for Azure CLI Full Test Starting...
Thanks for your contribution! |
|
Validation for Breaking Change Starting...
Thanks for your contribution! |
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
az login
Live test skipped⏭️ Skipping the live test for this revision because the only test file(s) changed are These Posted by agent-assist (autonomous bug-fix pipeline). |
There was a problem hiding this comment.
Automated Review Summary
Result: ✅ Pass
- Live test (Tester): Skipped (neutral) — this PR only changes
azure-cli-coreunit test files, which are not runnable viaazdev test --live. - CI checks: 1/1 passed, 0 failed, 0 pending.
No blocking issues found. This PR looks good to merge from an automated-review perspective.
Posted by agent-assist (autonomous bug-fix pipeline).
|
Core |
… listing Extract error translation into shared `_raise_friendly_error` helper - Apply the same try/except to `client.subscriptions.list()` in `find_using_specific_tenant` so `az login --tenant TENANT_ID` also surfaces an actionable message instead of a raw `JSON is invalid` traceback - Add unit tests for the specific-tenant 403 and DecodeError paths
Live test skipped⏭️ Skipping the live test for this revision because the only test file(s) changed are These Posted by agent-assist (autonomous bug-fix pipeline). |
Live test skipped⏭️ Skipping the live test for this revision because the only test file(s) changed are These Posted by agent-assist (autonomous bug-fix pipeline). |
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
Pull request overview
Note
Copilot could not run the full agentic suite for this review because it was automatically requested on a bot-authored pull request. Request a review from Copilot under Reviewers to retry with the full agentic suite. Improved support for bot-authored pull requests is coming soon.
This PR improves az login error handling when tenant or subscription discovery is blocked (HTTP 403) or returns a non-JSON response, raising a more actionable AzureResponseError instead of raw SDK exceptions.
Changes:
- Added
_raise_friendly_errorto translateDecodeErrorandHttpResponseError(403)into friendly CLI errors. - Forced tenant/subscription pagers to execute within
try/exceptvialist(...)to catch lazy pager exceptions deterministically. - Added unit tests covering 403 and decode-error cases for both tenant listing and subscription listing.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/azure-cli-core/azure/cli/core/_profile.py | Adds friendly exception translation and wraps tenant/subscription discovery calls to raise actionable AzureResponseErrors. |
| src/azure-cli-core/azure/cli/core/tests/test_profile.py | Adds test coverage to ensure blocked/non-JSON discovery errors are surfaced as friendly AzureResponseErrors. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| def _raise_friendly_error(ex, resource_name): | ||
| # Translate blocked-transport errors (HTTP 403 or non-JSON body) into an actionable | ||
| # AzureResponseError. Any other exception is re-raised unchanged. resource_name is | ||
| # interpolated into the error message (e.g. "tenants", "subscriptions"). | ||
| if isinstance(ex, DecodeError): | ||
| raise AzureResponseError( | ||
| "Failed to retrieve {name}. The response from the server could not be parsed. " | ||
| "This may be caused by a network firewall or proxy returning an unexpected response. " | ||
| "Please check your network settings and try again.".format(name=resource_name) | ||
| ) from ex | ||
| if isinstance(ex, HttpResponseError) and ex.status_code == 403: | ||
| raise AzureResponseError( | ||
| "Failed to retrieve {name}. The request was blocked (HTTP 403 Forbidden). " | ||
| "This may be caused by a network firewall, proxy, or Conditional Access policy. " | ||
| "Please check your network settings and try again.".format(name=resource_name) | ||
| ) from ex | ||
| raise ex |
| with self.assertRaises(AzureResponseError) as cm: | ||
| profile.login(True, None, None, False, None, use_device_code=False, allow_no_subscriptions=False) | ||
| self.assertIn("403", str(cm.exception)) |
There was a problem hiding this comment.
Automated Review Summary
PR: {Core} Handle 403/DecodeError when listing tenants in az login
- CI checks: 92/92 passed, 0 failed, 0 pending. ✅
- Live test: Skipped (neutral) — this PR only changes
azure-cli-coreunit tests, which are not runnable viaazdev test --live.
No outstanding failures detected. This PR looks good from an automated-check perspective.
Posted by agent-assist (autonomous bug-fix pipeline).
|
Copilot please review and implement all unaddressed human feedback through Yong Zhang (@yonzhan)'s latest comment at #33745 (comment), then push the fix to this PR. Posted by agent-assist (autonomous bug-fix pipeline). |
There was a problem hiding this comment.
✅ Automated Review Summary
All checks are green for this PR:
- CI checks: 93/93 passed, 0 failed, 0 pending.
- Live test: no runnable test files changed for this PR (skipped neutrally); relying on the completed CI suite above.
No outstanding issues found. This PR looks ready for a maintainer's final review/merge decision.
|
🔔 Routing this PR to @Azure/act-identity-squad. |
Related command
az loginDescription
When
az logincallsGET /tenantsand the response is a 403 with an HTML body (e.g. blocked by a network firewall or proxy), the SDK's JSON deserializer raisesazure.core.exceptions.DecodeError, surfacing the cryptic errorJSON is invalid: Expecting value: line 1 column 1 (char 0)instead of anything actionable.Changes:
SubscriptionFinder.find_using_common_tenant(_profile.py), wrapclient.tenants.list()in atry/except (DecodeError, HttpResponseError)block:DecodeError(non-JSON response, e.g. HTML block page): raisesAzureResponseErrorexplaining the parse failure and suggesting--tenant TENANT_IDas a workaroundHttpResponseError403: raisesAzureResponseErrorexplicitly calling out the 403 and suggesting network/proxy/CAP as causesHttpResponseError: re-raised as-is to preserve SDK error contextBefore this fix, users behind a restrictive network got:
After this fix, the same scenario produces:
Testing Guide
Unit tests added in
test_profile.py:test_login_tenant_list_403_raises_friendly_error— mockstenants.list()raisingHttpResponseError(status_code=403)test_login_tenant_list_decode_error_raises_friendly_error— mockstenants.list()raisingDecodeError(HTML body)History Notes
[Core]
az login: Show a user-friendly error when tenant listing is blocked by a firewall or proxy (HTTP 403 / non-JSON response) instead ofJSON is invalidThis 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.