fix(auth): refresh the oidc discovery document with the keys

The gateway cached the discovery document for the life of the process.
A changed jwks_uri or userinfo_endpoint stayed hidden until a restart.

The provider now fetches the document again when it is older than
jwksRefresh. A failed refresh keeps the old document. The next try waits
for the fetch retry interval.
This commit is contained in:
2026-12-02 02:02:44 +00:00
parent a5758a6674
commit e94225a569
2 changed files with 106 additions and 14 deletions
+40 -14
View File
@@ -53,8 +53,12 @@ type oidcDiscovery struct {
type oidcProvider struct {
fetchedAt time.Time
failedAt time.Time
client *http.Client
now func() time.Time
// discAt is when disc was fetched; discFailedAt is the last failed
// refresh of an already cached disc.
discAt time.Time
discFailedAt time.Time
client *http.Client
now func() time.Time
// userinfoSlots is a semaphore of oidcUserinfoMaxInFlight tokens.
userinfoSlots chan struct{}
settings *oidcSettings
@@ -160,29 +164,47 @@ func (p *oidcProvider) fetchKeys(ctx context.Context) (*jwkSet, error) {
return parseJWKS(body)
}
// discovery returns the cached discovery document, fetching it once. A
// failure is held for oidcFetchRetryAfter. The caller holds fetchMu.
// discovery returns the discovery document. It re-fetches once the cached
// copy is older than jwksRefresh; a failed refresh keeps the old copy and
// retries after oidcFetchRetryAfter. A failure with no copy is held for
// oidcFetchRetryAfter. The caller holds fetchMu.
func (p *oidcProvider) discovery(ctx context.Context) (*oidcDiscovery, error) {
p.stateMu.RLock()
disc := p.disc
disc, at, failed := p.disc, p.discAt, p.discFailedAt
p.stateMu.RUnlock()
if disc != nil {
if disc != nil && !p.discoveryDue(at, failed) {
return disc, nil
}
if err := p.recentFailure(); err != nil {
return nil, err
if disc == nil {
if err := p.recentFailure(); err != nil {
return nil, err
}
}
doc, err := p.fetchDiscovery(ctx)
p.stateMu.Lock()
defer p.stateMu.Unlock()
if err != nil {
if disc != nil {
p.discFailedAt = p.now()
return disc, nil
}
p.failedAt, p.failErr = p.now(), err
return nil, err
}
p.disc = doc
p.disc, p.discAt, p.discFailedAt = doc, p.now(), time.Time{}
return doc, nil
}
// discoveryDue reports whether a cached document fetched at `at` needs a
// refresh, given the last failed refresh at `failed`.
func (p *oidcProvider) discoveryDue(at, failed time.Time) bool {
now := p.now()
if now.Sub(at) < p.settings.jwksRefresh {
return false
}
return failed.IsZero() || now.Sub(failed) >= oidcFetchRetryAfter
}
func (p *oidcProvider) fetchDiscovery(ctx context.Context) (*oidcDiscovery, error) {
url := strings.TrimSuffix(p.settings.issuer, "/") + "/.well-known/openid-configuration"
body, err := p.get(ctx, url, "")
@@ -247,16 +269,20 @@ func (p *oidcProvider) userinfo(ctx context.Context, token string) (map[string]a
return claims, nil
}
// cachedDiscovery returns the discovery document, taking fetchMu only when
// it is not cached yet, so userinfo slots never wait behind a JWKS fetch.
// cachedDiscovery returns the discovery document. It takes fetchMu only to
// fetch a missing document, or to refresh a due one when the lock is free, so
// userinfo slots never wait behind a JWKS fetch.
func (p *oidcProvider) cachedDiscovery() (*oidcDiscovery, error) {
p.stateMu.RLock()
disc := p.disc
disc, at, failed := p.disc, p.discAt, p.discFailedAt
p.stateMu.RUnlock()
if disc != nil {
return disc, nil
if !p.discoveryDue(at, failed) || !p.fetchMu.TryLock() {
return disc, nil
}
} else {
p.fetchMu.Lock()
}
p.fetchMu.Lock()
defer p.fetchMu.Unlock()
ctx, cancel := context.WithTimeout(context.Background(), p.timeout)
defer cancel()
+66
View File
@@ -281,3 +281,69 @@ func TestProvider_ConcurrentFetchSingleFlight(t *testing.T) {
t.Fatalf("jwks hits = %d, want 1", j)
}
}
func TestProvider_DiscoveryRefreshesWithKeys(t *testing.T) {
f := newFakeIDP(t)
clock := newFakeClock()
p := newTestProvider(f, clock)
if _, err := p.keysFor("rsa"); err != nil {
t.Fatal(err)
}
f.jwksURI.Store(f.srv.URL + "/jwks-moved")
clock.Advance(time.Hour + time.Second)
if _, err := p.keysFor("rsa"); err != nil {
t.Fatal(err)
}
if d := f.discoveryHits.Load(); d != 2 {
t.Fatalf("discovery hits = %d, want 2 after jwksRefresh", d)
}
p.stateMu.RLock()
got := p.disc.JWKSURI
p.stateMu.RUnlock()
if got != f.srv.URL+"/jwks-moved" {
t.Fatalf("jwks_uri = %q, want the refreshed document's value", got)
}
}
func TestProvider_StaleDiscoverySurvivesFailedRefresh(t *testing.T) {
f := newFakeIDP(t)
clock := newFakeClock()
p := newTestProvider(f, clock)
if _, err := p.keysFor("rsa"); err != nil {
t.Fatal(err)
}
f.failDiscovery.Store(true)
clock.Advance(time.Hour + time.Second)
if _, err := p.keysFor("rsa"); err != nil {
t.Fatalf("a failed discovery refresh must keep the old document: %v", err)
}
if j := f.jwksHits.Load(); j != 2 {
t.Fatalf("jwks hits = %d, want 2: keys refresh uses the stale document", j)
}
hits := f.discoveryHits.Load()
clock.Advance(oidcFetchRetryAfter - time.Second)
if _, err := p.cachedDiscovery(); err != nil {
t.Fatal(err)
}
if got := f.discoveryHits.Load(); got != hits {
t.Fatalf("discovery retried inside oidcFetchRetryAfter: %d -> %d", hits, got)
}
}
func TestProvider_UserinfoPathRefreshesDiscovery(t *testing.T) {
f := newFakeIDP(t)
clock := newFakeClock()
p := newTestProvider(f, clock)
if _, err := p.cachedDiscovery(); err != nil {
t.Fatal(err)
}
f.userinfoURI.Store(f.srv.URL + "/userinfo-moved")
clock.Advance(time.Hour + time.Second)
disc, err := p.cachedDiscovery()
if err != nil {
t.Fatal(err)
}
if disc.UserinfoEndpoint != f.srv.URL+"/userinfo-moved" {
t.Fatalf("userinfo endpoint = %q, want the refreshed value", disc.UserinfoEndpoint)
}
}