[BUG] Azure DevOps auth error tells users to run `az login`, or says a token was rejected, when `az` failed to issue a token
Describe the bug
When az is on PATH but az account get-access-token fails, for example after a multi-factor
authentication or Conditional Access requirement, an expired refresh token or a broken profile, apm install of an Azure DevOps package prints one of two remedies. Neither shows what az said:
- If
az account showalso fails, APM prints "Sign in with Azure CLI ...az login". Users who are already signed in runaz login, and nothing changes. - If
az account showsucceeds, APM prints "Your az cli session (tenant: ...) returned a bearer token, but Azure DevOps rejected it (HTTP 401)". That is not what happened:azreturned no token, and the probes ran without a credential (ls-remote failed via ADO HTTPS without resolved credential).
APM keeps the error from az, but logs it only at DEBUG level. The install error ends with "(run with
--verbose for the full probe log)", and apm install --verbose prints the probe log without any DEBUG
line. Only the root flag, apm --verbose install, or APM_LOG_LEVEL=DEBUG shows the real cause.
The reporter in the case behind this issue saw the "az login" text. They were signed in, az login
and git clone worked in their shell, and they could not tell why APM still failed.
Root cause:
_run_get_access_tokenturns every non-zero exit intokind="not_logged_in"and keeps the stderr (azure_cli.py#L245-L251)._resolve_tokenlogs that error at DEBUG and drops it (auth.py#L1365-L1373). Its comment expectsbuild_error_contextto render Case 3.build_error_contextinstead chooses between Case 3 and Case 2 fromget_current_tenant_id()alone (auth.py#L1218-L1245), which swallows its own errors (azure_cli.py#L165-L190). A known tenant produces Case 2, whether or not a token was ever issued.
The triage of #1671 asked to surface az login errors, including MFA expiry, as APM errors rather than
silently falling back. #1675 fixed the other items from that triage; this one is still open.
To Reproduce
A fake az stands in for a real failure, so no Azure account is needed.
APM=/absolute/path/to/apm # the release binary
work=$(mktemp -d) && mkdir -p "$work/a" "$work/b" "$work/home" "$work/proj"
# a: az fails on every call
printf '#!/bin/sh\necho "ERROR: simulated az failure: cannot load the Azure CLI profile" >&2\nexit 1\n' > "$work/a/az"
# b: az account show works, token acquisition fails
cat > "$work/b/az" <<'EOF'
#!/bin/sh
if [ "$1 $2" = "account show" ]; then echo 00000000-0000-0000-0000-000000000000; exit 0; fi
echo "ERROR: AADSTS50076: you must use multi-factor authentication to access '499b84ac-1321-427f-aa17-267ca6975798'." >&2
exit 1
EOF
chmod +x "$work/a/az" "$work/b/az"
cd "$work/proj"
pkg='https://dev.azure.com/example-org/example-project/_git/example-repo/packages/demo#v1.0.0'
for v in a b; do
env -i PATH="$work/$v:/usr/bin:/bin" HOME="$work/home" "$APM" install "$pkg" --target copilot --verbose
env -i PATH="$work/$v:/usr/bin:/bin" HOME="$work/home" "$APM" --verbose install "$pkg" --target copilot
done
Expected behavior
When az is present but cannot issue a token, the error says so and includes the last line of its
stderr, for example Azure CLI could not get a token for Azure DevOps: ERROR: AADSTS50076: .... It
never claims that a token was issued or rejected unless one was.
Possible fix: keep the last AzureCliBearerError on the shared bearer provider. In
build_error_context, before the tenant check, render that error when it is present: "the Azure CLI
is installed but could not get a token" plus the stderr tail, with az login and ADO_APM_PAT as the
remedies. Case 2 then means what it says: a token was issued and rejected. Case 3 remains for az
with no profile and no stderr.
Test: next to test_ado_no_pat_az_available_logged_in_but_rejected in tests/unit/test_auth.py, mock
get_current_tenant_id to return a tenant and get_bearer_token to raise AzureCliBearerError(..., stderr="ERROR: AADSTS50076: ..."). The message contains AADSTS50076 and does not contain "returned a
bearer token". The existing Case 2 and Case 3 tests keep passing.
Alternative: print the az error at the default log level when token acquisition fails for an Azure
DevOps dependency, and make the subcommand install --verbose show DEBUG logging.
Out of scope: the credential helper fallback for apm install, which is #3752.
Environment (please complete the following information):
- OS: macOS 26.7.3 (arm64)
- Python Version: the standalone release binary (
apm-darwin-arm64);mainfrom source with Python 3.12.12 - APM Version: 0.35.1 (787a97c), the latest release, and
main(f574a62) - VSCode Version (if relevant): n/a
Logs
Variant a, apm install --verbose, excerpt:
[x] ls-remote failed via ADO HTTPS without resolved credential:
...
Azure DevOps requires authentication. You have two options:
1. Sign in with Azure CLI (recommended for Entra ID users):
az login
apm install # retry -- no env var needed
2. Use a Personal Access Token:
export ADO_APM_PAT=your_token
...
All packages failed validation. Nothing to install.
Variant b, apm install --verbose, excerpt:
[x] ls-remote failed via ADO HTTPS without resolved credential:
...
Your az cli session (tenant: 00000000-0000-0000-0000-000000000000)
returned a bearer token,
but Azure DevOps rejected it (HTTP 401).
Check that you are signed into the correct tenant:
az account show
az login --tenant <correct-tenant-id>
...
All packages failed validation. Nothing to install.
Neither output contains a DEBUG line. The root flag shows the cause, here for variant b:
$ apm --verbose install "$pkg" --target copilot
[*] Validating 1 package...
DEBUG apm_cli.core.auth ADO [REDACTED] acquisition failed for dev.azure.com: az CLI returned exit code 1: ERROR: AADSTS50076: you must use multi-factor authentication to access '499b84ac-1321-427f-aa17-267ca6975798'.
[x]
https://dev.azure.com/example-org/example-project/_git/example-repo/packages/dem
o#v1.0.0 -- all probes failed (marker-file, Contents API, git ls-remote,
shallow-fetch) -- verify the path and ref exist and that your credentials have
read access (run with --verbose for the full probe log)
Additional context
Two smaller problems are visible in the same run:
_SECRET_REmatches the words "bearer token" as a secret, so the debug line readsADO [REDACTED] acquisition failed(auth.py#L75-L79).- The tenant lookup behind the error message is not cached. Variant a ran
az account showseven times andaz account get-access-tokenonce for a single package, and a realazcall took between 0.6 and 15 seconds here.
Tools that wrap apm currently re-run az themselves to explain the failure to their users.
Decision: approve Area: project Scope: Render Azure DevOps authentication diagnostics from the attempt log recorded by the auth fallback chain (what was tried, what each step returned and why) instead of re-probing az; keep the Azure CLI error detail, cache the tenant lookup, and stop redaction from hiding the phrase bearer token. Done when: When az fails to issue a token the error shows the redacted az reason (for example an AADSTS code) and never claims a token was rejected, the exhausted-chain error lists the steps tried in order, and az account show runs at most once per process, all proven by hermetic tests. Out of scope: New auth commands or flags; changes to credential precedence. Review contact: @danielmeppiel
Context for the scope record above: this is delivered by the 0.36.1 auth track, from a design reviewed and approved by the maintainer on 2026-11-17. The fix gives one owner to the decision "is this response an authentication failure, and which credential comes next", and routes every Azure DevOps and policy egress through it, instead of patching each reported call site. Validation is hermetic (local fixtures and stubs); no live Azure DevOps org was available, so behavior follows the documented Azure DevOps and Git credential contracts.