-
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
2aeab5d
e63e1d9
3e4e554
53c9e00
b0f3edb
8060597
fbee24d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -102,6 +102,7 @@ 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->timer = calloc(1, sizeof(*clt->timer)); | ||
| uv_timer_init(loop, clt->timer); | ||
|
|
@@ -570,12 +571,7 @@ 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 | ||
| } | ||
| 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 +584,40 @@ 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; | ||
| 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); | ||
| if (http_resp->code < 0) { | ||
| clt->refresh_failures++; | ||
| OIDC_LOG(WARN, "OIDC token refresh failed (%d/%s), attempt %d", | ||
| http_resp->code, err, clt->refresh_failures); | ||
| 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; | ||
| } | ||
| uv_timer_start(clt->timer, refresh_time_cb, 5 * 1000, 0); | ||
| return; | ||
| } | ||
|
|
||
| // http_resp->code >= 0 but not 200: server-side rejection (e.g. EOF, 401, 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; | ||
| 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) { | ||
|
|
||
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!