mirror of
https://github.com/lukaszraczylo/traefikoidc.git
synced 2026-11-24 11:05:53 +00:00
fix(metadata): retry discovery HTTP errors and recover in the background (#169)
A provider behind the same Traefik answers 404 while its router is not loaded, and 502 or 503 while it restarts. GetProviderMetadata returned these statuses as a plain error, so the startup retries stopped at the first attempt. After that, nothing retried until a request arrived, and that request got the 503 page (#147). - GetProviderMetadata returns a non-200 status as *HTTPError, so the startup retries apply to 429 and 5xx. - RetryConfig gets RetryableStatusCodes. MetadataFetchRetryConfig sets it to 404, so only discovery retries a 404. - If startup discovery fails, the init goroutine retries in the background (5 s, doubling up to 60 s, with jitter). It stops when metadata is adopted or when Close cancels the instance context. - GetMetadataWithRecovery takes a context, so Close also ends the startup retries. - Background attempts log at ERROR for the first three and every tenth, at DEBUG otherwise. A document with no usable issuer does not end recovery. A Close during the startup retries logs at DEBUG only. - The 503 served while metadata is unavailable or initialization times out carries Retry-After: 10, and the startup error log names the middleware. TestMetadataRecoveryOnProviderFailure and the issue152 cold-start test now serve 400 for a failed discovery, because startup retries 500 and 503 and these tests need init to fail at once.
This commit is contained in:
@@ -466,6 +466,11 @@ func (t *TraefikOidc) bypassSessionClaims(session *SessionData) (map[string]inte
|
||||
return claims, err
|
||||
}
|
||||
|
||||
// metadataUnavailableRetryAfter is the Retry-After value (seconds) of the 503
|
||||
// served while provider metadata is unavailable or initialization times out.
|
||||
// Background recovery retries every 5 to 60 seconds.
|
||||
const metadataUnavailableRetryAfter = "10"
|
||||
|
||||
// ServeHTTP implements the main middleware logic for processing HTTP requests.
|
||||
// It handles the complete OIDC authentication flow including:
|
||||
// - Excluded URL bypass
|
||||
@@ -763,6 +768,7 @@ func (t *TraefikOidc) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
|
||||
t.logger.Error("OIDC provider metadata initialization failed or incomplete")
|
||||
rw.Header().Set("Retry-After", metadataUnavailableRetryAfter)
|
||||
t.sendErrorResponse(rw, req, "OIDC provider metadata initialization failed - please check provider availability and configuration", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
@@ -774,6 +780,7 @@ func (t *TraefikOidc) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
case <-initTimer.C:
|
||||
atomic.AddInt32(&t.initWaiters, -1)
|
||||
t.logger.Error("Timeout waiting for OIDC initialization")
|
||||
rw.Header().Set("Retry-After", metadataUnavailableRetryAfter)
|
||||
t.sendErrorResponse(rw, req, "Timeout waiting for OIDC provider initialization - please try again later", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user