Repository navigation
fix(auth): share M2M tokens across connections and retry exchanges - #398
vuanhphung wants to merge 1 commit into
Conversation
Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
There was a problem hiding this comment.
Verdict: 2 Low
Looks good — solid, well-tested change. Shared M2M token/discovery caching, retry-routed token exchanges, and the refresh_error propagation are correctly ordered and backed by thorough wiremock + unit coverage; new deps are already in Cargo.toml and the custom Debug impl correctly stops leaking client_secret. Two low-severity notes only: a new block_in_place panic path on current-thread runtimes (relies on the multi-thread invariant), and loss of OAuth error_description in error messages.
| // Waiting must release a runtime worker, just like the leader's | ||
| // HTTP exchange, or concurrent sessions can starve its I/O. | ||
| if tokio::runtime::Handle::try_current().is_ok() { | ||
| tokio::task::block_in_place(|| self.wait_for_refresh()) |
There was a problem hiding this comment.
🔵 Low — The new waiter path wraps wait_for_refresh() in tokio::task::block_in_place guarded only by Handle::try_current().is_ok(), not by runtime flavor. block_in_place panics on a current-thread runtime. Before this change a follower simply called wait_for_refresh() directly (blocking the worker but never panicking), so this introduces a new panic path for callers driving get_auth_header() from inside a single-threaded tokio runtime.
In practice the kernel's session runtime is multi-thread (documented invariant in src/util/runtime.rs), and this mirrors the existing block_on_in_runtime guard, so the realistic blast radius is small. But note that block_on_current_runtime in the same util module does branch on handle.runtime_flavor() before calling block_in_place — matching that pattern here would make the fallback panic-free rather than relying on the multi-thread invariant holding for every embedder.
| } | ||
| return err | ||
| .with_error_code(code) | ||
| .message(format!("{context}: OAuth {code}")); |
There was a problem hiding this comment.
🔵 Low — The new OAuth-error branch collapses the message to "{context}: OAuth {code}", surfacing only the short error code and discarding any error_description present in the OAuth error body. OAuth providers routinely put the actionable detail (e.g. "client secret expired", "scope X not granted") in error_description; previously the m2m path folded the raw body into the message via oauth2's ServerResponse/the v1 path's explicit body inclusion. Consider appending error_description when present so operators keep the diagnostic text. Non-blocking — the error_code and http_status are preserved, which is the important part for programmatic handling.
Each kernel connection currently repeats OIDC discovery and the OAuth client-credentials exchange, causing token-request bursts when opening connections concurrently. Share discovery and token state in a bounded process-wide cache keyed by workspace, credentials, scopes, endpoint/resource, and transport settings. Concurrent refreshes are coalesced while HTTP clients and runtimes remain connection-owned.
Route token exchanges through the existing auth retry policy, including Retry-After. Preserve HTTP error metadata for waiting callers and release Tokio workers while they wait; invalid credentials remain non-retryable authentication errors.
Thrift-mode comparison
The drivers reuse tokens through auth/token-source objects. Sharing across connections depends on reusing those objects; none of these default Thrift paths provides a process-wide credential-identity cache.
oauth2.TokenSourceper connector/authenticator; no explicit cache-map key. Pooled connections reuse it.Authenticatemutex plusReuseTokenSourceserialize acquisition/renewal.Retry-After.ExternalAuthProviderinvokescredentials_provider()per connection. Sharing requires the callback to return the same SDK header factory/token source. The example creates a new factory each time.ClientCredentialsTokenSource.tokencaches one token per source; the provider callback constructs separate resource-token sources.UnifiedHttpClient/DatabricksRetryPolicy, includingRetry-After.OAuthPersistenceCachebelongs to the auth instance and is keyed by host. A fresh default auth instance gets a fresh cache.openid-client, without the driver's Thrift retry-policy wrapper.Retry-After, and preserved HTTP error metadata.The kernel bindings construct an auth provider per connection. Sharing token state inside the kernel preserves reuse across those provider instances without sharing connection-owned HTTP clients or runtimes.
Validated with the full cargo test suite, including doc tests, Clippy with warnings denied, and formatting checks. Mock tests verify 32 independent connections perform one discovery and one token exchange, plus refresh sharing, cache isolation, retry exhaustion, and reuse across runtimes.