-
Notifications
You must be signed in to change notification settings - Fork 28
fix: OIDC token refresh recovery after network disruption #991
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dmuensterer
wants to merge
7
commits into
openziti:main
Choose a base branch
from
dmuensterer:fix/oidc-reconnect-refresh
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2aeab5d
fix: OIDC token refresh recovery after network disruption
dmuensterer e63e1d9
fix: escalate to full re-auth after sustained OIDC refresh failure
dmuensterer 3e4e554
Use exponential backoff for OIDC refresh retries.
dmuensterer 53c9e00
Consolidate refresh failure counters and remove 3-failure reset
dmuensterer b0f3edb
Add NULL checks for redirect parsing in OIDC auth flow
dmuensterer 8060597
Merge branch 'main' into fix/oidc-reconnect-refresh
ekoby fbee24d
Merge branch 'main' into fix/oidc-reconnect-refresh
ekoby File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -102,7 +102,9 @@ int oidc_client_init(uv_loop_t *loop, oidc_client_t *clt, | |
| return rc; | ||
| } | ||
| tlsuv_http_set_ssl(&clt->http, tls); | ||
| tlsuv_http_connect_timeout(&clt->http, 15000); | ||
|
|
||
| clt->token_expiry = 0; | ||
| clt->timer = calloc(1, sizeof(*clt->timer)); | ||
| uv_timer_init(loop, clt->timer); | ||
| clt->timer->data = clt; | ||
|
|
@@ -570,12 +572,8 @@ static void oidc_client_set_tokens(oidc_client_t *clt, json_object *tok_json) { | |
| } | ||
| if (ttl) { | ||
| int32_t t = json_object_get_int(ttl); | ||
| if (t <= 60) { | ||
| OIDC_LOG(WARN, "token lifetime is too short[%d seconds]. this may cause problems", t); | ||
| t = t / 2; | ||
| } else { | ||
| t = t - 30; // refresh 30 seconds before expiry | ||
| } | ||
| clt->token_expiry = uv_now(clt->timer->loop) + (uint64_t)t * 1000; | ||
| t = t / 2; // refresh at half the token lifetime | ||
| OIDC_LOG(DEBUG, "scheduling token refresh in %d seconds", t); | ||
| uv_timer_start(clt->timer, refresh_time_cb, t * 1000, 0); | ||
| } | ||
|
|
@@ -588,33 +586,75 @@ static void refresh_cb(tlsuv_http_resp_t *http_resp, const char *err, json_objec | |
|
|
||
| if (http_resp->code == 200 && resp != NULL) { | ||
| OIDC_LOG(DEBUG, "token refresh success"); | ||
| clt->refresh_failures = 0; | ||
| clt->total_refresh_failures = 0; | ||
| oidc_client_set_tokens(clt, resp); | ||
| return; | ||
| } | ||
|
|
||
| if (http_resp->code >= 0 || http_resp->code == UV_EOF) { | ||
| // controller may abruptly terminate shutdown connection (EOF) if auth has failed | ||
| OIDC_LOG(WARN, "OIDC token refresh failed: %d %s [%s]", | ||
| http_resp->code, http_resp->status, err); | ||
| if (resp) { | ||
| OIDC_LOG(WARN, "response: %s", json_object_get_string(resp)); | ||
| } | ||
| json_object_put(clt->tokens); | ||
| clt->tokens = NULL; | ||
|
|
||
| oidc_client_start(clt, clt->token_cb); | ||
| } | ||
|
|
||
| if (http_resp->code == UV_ECANCELED) { | ||
| OIDC_LOG(DEBUG, "OIDC token refresh was canceled"); | ||
| return; | ||
| } | ||
|
|
||
| if (http_resp->code < 0) { // connection failure, try another refresh | ||
| OIDC_LOG(WARN, "OIDC token refresh failed (trying again): %d/%s", http_resp->code, err); | ||
| uv_timer_start(clt->timer, refresh_time_cb, 5 * 1000, 0); | ||
| if (http_resp->code < 0) { | ||
| clt->refresh_failures++; | ||
| clt->total_refresh_failures++; | ||
| OIDC_LOG(WARN, "OIDC token refresh failed (%d/%s), attempt %d (total: %d)", | ||
| http_resp->code, err, clt->refresh_failures, clt->total_refresh_failures); | ||
|
|
||
| // token expired, give up on refresh and restart full auth | ||
| uint64_t now = uv_now(clt->timer->loop); | ||
| if (clt->token_expiry > 0 && now >= clt->token_expiry) { | ||
ekoby marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| OIDC_LOG(WARN, "OIDC token has expired after %d refresh attempts, restarting full authentication", | ||
| clt->total_refresh_failures); | ||
| tlsuv_http_cancel_all(&clt->http); | ||
| clt->refresh_failures = 0; | ||
| clt->total_refresh_failures = 0; | ||
| clt->token_expiry = 0; | ||
| json_object_put(clt->tokens); | ||
| clt->tokens = NULL; | ||
| oidc_client_start(clt, clt->token_cb); | ||
| return; | ||
| } | ||
|
|
||
| if (clt->refresh_failures >= 3) { | ||
|
||
| OIDC_LOG(WARN, "resetting OIDC connection after %d consecutive failures", clt->refresh_failures); | ||
| tlsuv_http_cancel_all(&clt->http); | ||
| clt->refresh_failures = 0; | ||
| } | ||
|
|
||
| // exponential backoff with jitter to avoid stampeding controller after outage | ||
| int shift = clt->total_refresh_failures - 1; | ||
| if (shift > 4) shift = 4; // cap at 80s base | ||
| uint64_t delay = 5000ULL << shift; | ||
| if (delay > 60000) delay = 60000; | ||
| // jitter: [delay/2, delay] | ||
| uint32_t half = (uint32_t)(delay / 2); | ||
| if (half > 0) delay = half + randombytes_uniform(half + 1); | ||
| // clamp to remaining token lifetime | ||
| if (clt->token_expiry > 0 && now + delay > clt->token_expiry) { | ||
| delay = clt->token_expiry - now; | ||
| } | ||
|
|
||
| OIDC_LOG(DEBUG, "scheduling token refresh retry in %llu ms", (unsigned long long)delay); | ||
| uv_timer_start(clt->timer, refresh_time_cb, delay, 0); | ||
| return; | ||
| } | ||
|
|
||
| // http_resp->code > 0 but not 200: server-side rejection (e.g. 401, 403, etc.) | ||
| OIDC_LOG(WARN, "OIDC token refresh failed: %d %s [%s]", | ||
| http_resp->code, http_resp->status, err); | ||
| if (resp) { | ||
| OIDC_LOG(WARN, "response: %s", json_object_get_string(resp)); | ||
| } | ||
| clt->refresh_failures = 0; | ||
| clt->total_refresh_failures = 0; | ||
| clt->token_expiry = 0; | ||
| json_object_put(clt->tokens); | ||
| clt->tokens = NULL; | ||
|
|
||
| oidc_client_start(clt, clt->token_cb); | ||
| } | ||
|
|
||
| static const char* get_basic_auth_header(const char *client_id) { | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you only need one counter. I see they are set and incremented at the same time
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, fixed!