Skip to content

{Core} Handle 403/DecodeError when listing tenants in az login - #33745

Open
Aditya Pujara (a0x1ab) with Copilot wants to merge 6 commits into
devfrom
copilot/fix-login-error
Open

{Core} Handle 403/DecodeError when listing tenants in az login#33745
Aditya Pujara (a0x1ab) with Copilot wants to merge 6 commits into
devfrom
copilot/fix-login-error

Conversation

Copilot AI commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Related command
az login

Description

When az login calls GET /tenants and the response is a 403 with an HTML body (e.g. blocked by a network firewall or proxy), the SDK's JSON deserializer raises azure.core.exceptions.DecodeError, surfacing the cryptic error JSON is invalid: Expecting value: line 1 column 1 (char 0) instead of anything actionable.

Changes:

  • In SubscriptionFinder.find_using_common_tenant (_profile.py), wrap client.tenants.list() in a try/except (DecodeError, HttpResponseError) block:
    • DecodeError (non-JSON response, e.g. HTML block page): raises AzureResponseError explaining the parse failure and suggesting --tenant TENANT_ID as a workaround
    • HttpResponseError 403: raises AzureResponseError explicitly calling out the 403 and suggesting network/proxy/CAP as causes
    • Other HttpResponseError: re-raised as-is to preserve SDK error context

Before this fix, users behind a restrictive network got:

ERROR: JSON is invalid: Expecting value: line 1 column 1 (char 0)

After this fix, the same scenario produces:

ERROR: Failed to retrieve tenants. 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, or use 'az login --tenant TENANT_ID' to log in to a specific tenant.

Testing Guide

Unit tests added in test_profile.py:

  • test_login_tenant_list_403_raises_friendly_error — mocks tenants.list() raising HttpResponseError(status_code=403)
  • test_login_tenant_list_decode_error_raises_friendly_error — mocks tenants.list() raising DecodeError (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 of JSON is invalid


This checklist is used to make sure that common guidelines for a pull request are followed.

@azure-client-tools-bot-prd

Copy link
Copy Markdown
Validation for Azure CLI Full Test Starting...

Thanks for your contribution!

@azure-client-tools-bot-prd

Copy link
Copy Markdown
Validation for Breaking Change Starting...

Thanks for your contribution!

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI changed the title [WIP] Fix login error during az login with device code {Core} Handle 403/DecodeError when listing tenants in az login Jul 17, 2026
@x-engineering-agent

Copy link
Copy Markdown
Contributor

Live test skipped

⏭️ Skipping the live test for this revision because the only test file(s) changed are azure-cli-core unit tests, which the live-test pipeline (azdev test --live) does not run — it covers command-module and extension tests only.

These azure-cli-core tests are exercised by upstream CI's unit-test jobs instead. This is informational; no action is required.


Posted by agent-assist (autonomous bug-fix pipeline).

@x-engineering-agent x-engineering-agent Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Automated Review Summary

Result: ✅ Pass

  • Live test (Tester): Skipped (neutral) — this PR only changes azure-cli-core unit test files, which are not runnable via azdev 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).

@yonzhan

Copy link
Copy Markdown
Collaborator

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
@x-engineering-agent

Copy link
Copy Markdown
Contributor

Live test skipped

⏭️ Skipping the live test for this revision because the only test file(s) changed are azure-cli-core unit tests, which the live-test pipeline (azdev test --live) does not run — it covers command-module and extension tests only.

These azure-cli-core tests are exercised by upstream CI's unit-test jobs instead. This is informational; no action is required.


Posted by agent-assist (autonomous bug-fix pipeline).

@x-engineering-agent

Copy link
Copy Markdown
Contributor

Live test skipped

⏭️ Skipping the live test for this revision because the only test file(s) changed are azure-cli-core unit tests, which the live-test pipeline (azdev test --live) does not run — it covers command-module and extension tests only.

These azure-cli-core tests are exercised by upstream CI's unit-test jobs instead. This is informational; no action is required.


Posted by agent-assist (autonomous bug-fix pipeline).

@x-engineering-agent
x-engineering-agent Bot marked this pull request as ready for review August 4, 2026 02:20
@x-engineering-agent
x-engineering-agent Bot requested a review from a team as a code owner August 4, 2026 02:20
@x-engineering-agent
x-engineering-agent Bot requested review from a team as code owners August 4, 2026 02:20
Copilot AI review requested due to automatic review settings August 4, 2026 02:20
@x-engineering-agent
x-engineering-agent Bot requested a review from a team as a code owner August 4, 2026 02:20
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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_error to translate DecodeError and HttpResponseError(403) into friendly CLI errors.
  • Forced tenant/subscription pagers to execute within try/except via list(...) 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.

Comment on lines +813 to +829
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
Comment on lines +710 to +712
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))

@x-engineering-agent x-engineering-agent Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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-core unit tests, which are not runnable via azdev test --live.

No outstanding failures detected. This PR looks good from an automated-check perspective.


Posted by agent-assist (autonomous bug-fix pipeline).

@a0x1ab

Copy link
Copy Markdown
Member

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).

@x-engineering-agent x-engineering-agent Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ 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.

@a0x1ab Aditya Pujara (a0x1ab) added X Engineering Agent Reviewed Pull request reviewed by X Engineering Agent and removed azure-client-tools-agent labels Sep 2, 2026
@microsoft-github-policy-service

Copy link
Copy Markdown
Contributor

🔔 Routing this PR to @Azure/act-identity-squad.

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

Labels

Account az login/account act-identity-squad Auto-Assign Auto assign by bot X Engineering Agent Reviewed Pull request reviewed by X Engineering Agent

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Login Error

5 participants