fix: force token refresh on 401 and add debug logging#4
Open
drewr wants to merge 1 commit into
Open
Conversation
The heartbeat's 401 handler only logged the rejection and never triggered a refresh — a stale comment claimed refresh was 'external/out-of-band'. So when a token was rejected early (clock skew, revocation, IdP rotation), the heartbeat kept retrying with the dead token until the proactive timer eventually fired (up to ~1h), causing tunnels to stop working. - Add DatumCloudClient::force_token_refresh() delegating to ExternalTokenSource::force_refresh(). - Rewrite heartbeat force_refresh_auth to actually call it; fix the misleading doc comment. LoginState::Missing guard preserved. - Add --debug CLI flag and DATUM_CONNECT_DEBUG=1 env var to datum-connect; bumps tracing filter to debug so refresh events print to stderr. - Add structured info!/debug! events in ExternalTokenSource covering forced-refresh requests, proactive-timer fires, successful swaps (with old/new JWT expiry, forced-vs-proactive flag), and failures. - Add force_refresh_swaps_token_via_loop test guarding the regression.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Important
This PR depends on #3 and should be merged after it.
Summary
Fixes the "tunnels stop working because of stale auth" failure mode and adds opt-in debug logging for the refresh process.
Root cause: The token refresh machinery existed and worked (proactive timer in
ExternalTokenSource::run_refresh_loop+ on-demandforce_refresh()), but the heartbeat's 401 handlerforce_refresh_authinconnect-lib/lib/src/heartbeat.rsonly logged the 401 and never triggered a refresh — a stale comment claimed refresh was "external/out-of-band". So when a token was rejected early (clock skew, revocation, IdP-side rotation), the heartbeat kept retrying with the dead token until the proactive timer eventually fired (up to ~1h), causing tunnels to stop working.Changes
(a) Refresh now actually fires on 401
DatumCloudClient::force_token_refresh()(connect-lib/lib/src/datum_cloud/mod.rs) delegating toExternalTokenSource::force_refresh().force_refresh_auth(connect-lib/lib/src/heartbeat.rs) to calldatum.force_token_refresh()instead of just logging, and corrected the misleading doc comment. TheLoginState::Missingguard is preserved.(b) Debug logging under a flag
--debugCLI flag andDATUM_CONNECT_DEBUG=1env var todatum-connect(connect-lib/bin/src/main.rs). When enabled (andRUST_LOGisn't overriding), the tracing filter is bumped todatum_connect=debug,connect_lib=debug, printing refresh events to stderr.info!/debug!events inExternalTokenSourcecovering: forced-refresh requests, proactive-timer fires, successful swaps (with old/new JWT expiry, forced-vs-proactive flag), and failures with backoff.Test
force_refresh_swaps_token_via_loop— a new#[tokio::test]that runs the real refresh loop with a counter-based helper, triggersforce_refresh(), and asserts the token actually rotates and watchers get notified (guards the regression).All 66 connect-lib tests pass;
cargo build/clippyintroduce no new warnings.