Skip to content

fix(auth): retry M2M token exchange on transient failures - #399

Merged
vuanhphung merged 2 commits into
mainfrom
fix/m2m-token-exchange-retry
Oct 15, 2026
Merged

vuanhphung merged 2 commits into
mainfrom
fix/m2m-token-exchange-retry

Conversation

@vuanhphung

@vuanhphung vuanhphung commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

The client-secret M2M token exchange and the Azure SP management-token (v1.0 resource=) exchange were sent on the raw reqwest client, bypassing the kernel's retry ladder. A single 429 or 503 from the token endpoint failed the request and surfaced as Unauthenticated. Thrift-mode Python retries these, so this is a kernel-mode regression.

Both exchanges now go through execute_without_auth with RequestType::AuthTokenRequest, using the session's Auth retry budget (3 retries / 30s, honouring Retry-After). The oauth2 crate still builds the M2M request via its pluggable HTTP client hook, so the wire format is unchanged.

Failures are classified with the retry policy's own non-retryable status list: a status the ladder refuses to retry (e.g. 400/401 invalid_client) stays Unauthenticated, while anything it retried and gave up on (429, 408, 5xx, network errors) keeps its status-based code. The JWT private-key M2M flow, which already retried, now uses the same classification (a 400 there was previously InvalidArgument).

New wiremock tests cover 429 → 503 → 200 (asserting the same Basic auth and form body on every attempt), a 401 that is not retried, exhausted 429 and 408 not reported as auth errors, 503 → 200 on the v1 resource path, and a JWT 400 mapped to Unauthenticated.

This pull request and its description were written by Isaac.

The client-secret M2M exchange and the Azure SP management-token (v1.0
resource) exchange sent requests on the raw reqwest client, skipping the
kernel's retry ladder. A single 429 or 503 from the token endpoint failed
the request and surfaced as Unauthenticated.

Both now go through execute_without_auth with RequestType::AuthTokenRequest.
The oauth2 crate still builds the M2M request, so the wire format is
unchanged. Failures that outlast retries keep their status-based code
(ResourceExhausted, Unavailable, NetworkError); rejected requests (4xx
other than 429) stay Unauthenticated.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ No issues identified by the review bot.

Reuse the retry ladder's non-retryable status list instead of a separate
"4xx except 429" rule. A retryable 4xx like 408 that outlasts the retry
budget is no longer reported as Unauthenticated.

Apply the same classification to the JWT private-key M2M flow, which
previously surfaced a 400 invalid_client as InvalidArgument.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
@vuanhphung vuanhphung added the integration-test Maintainer-only: triggers cross-repo integration tests. Auto-removed on push for re-review. label Oct 15, 2026
@github-actions

Copy link
Copy Markdown

Kernel integration tests triggered. View status. Results post back here as the "Kernel Integration Tests" check.

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verdict: 1 Low

Solid, well-tested fix: routing the client-secret M2M, Azure v1.0 resource=, and JWT M2M token exchanges through execute_without_auth/RequestType::AuthTokenRequest correctly puts them on the Auth retry ladder, and token_exchange_error classification matches the ladder's behavior for the paths exercised (429/503→success, 401/400 rejection, exhausted 429/408). Verified against src/client/retry.rs, src/kernel_error.rs::from_http_status, and the execute_impl return paths in src/client/http.rs. One Low note inline about classification divergence under override_retryable_codes.

Nits (not filed inline): (1) token_exchange_error's _ => arm preserves retryable: true on an exhausted 429/503 terminal error (src/auth/oauth/m2m.rs:423) — harmless for get_auth_header which is terminal, but a wrapper keying off .retryable could over-retry. (2) A token-endpoint 403 is reclassified from PermissionDenied to Unauthenticated, los

[...truncated to keep verdict scannable]

Comment thread src/auth/oauth/m2m.rs
/// becomes `Unauthenticated`; anything it retried and gave up on (429, 5xx,
/// network errors) keeps its own code so it isn't reported as bad credentials.
pub(super) fn token_exchange_error(e: Error, what: &str) -> Error {
let message = format!("{what} failed: {}", e.message);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Low — token_exchange_error classifies failures off the static IDEMPOTENT_NON_RETRYABLE list via is_non_retryable_status, which (by its own doc) ignores any per-session override_retryable_codes. The actual retry ladder in RetryStrategy::is_retryable_status honors those overrides. So if an Auth-category session sets override_retryable_codes to e.g. make 400 retryable, the ladder will retry it, but on exhaustion token_exchange_error still treats 400 as non-retryable and labels it Unauthenticated (credentials-rejected) — mislabeling an exhausted-transient as a hard auth rejection. The reverse (override excluding a 5xx) is also possible. This is a documented trade-off and override_retryable_codes on the Auth category is unusual, so impact is small, but the classification and the retry decision can disagree. Consider classifying off what the ladder actually did (e.g. carry the retryable flag / a 'retries-exhausted' marker on the returned Error) rather than re-deriving from a status list.

@vuanhphung
vuanhphung added this pull request to the merge queue Oct 15, 2026
Merged via the queue into main with commit 2546f52 Oct 15, 2026
29 of 30 checks passed

This branch was previously deployed

1 inactive deployment
azure-prod — 410c6871 Deployed Oct 15, 2026 by oss-reviewer-bot[bot] via followup #1998
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

integration-test Maintainer-only: triggers cross-repo integration tests. Auto-removed on push for re-review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants