perf: cache OIDC verification keys in k8sjwt#391
Open
botengyao wants to merge 3 commits into
Open
Conversation
Previously every k8sjwt.Verify call performed a full OIDC discovery (two HTTP round trips: the discovery document plus the JWKS) against the issuer. In JWT auth mode that cost was paid on every authenticated RPC, and MintJWT paid it again for the client token. Introduce k8sjwt.Verifier, which caches each issuer's verification keys in memory. Keys are only refetched when a JWT presents an unknown key ID (i.e. on key rotation). Since the triggering key ID comes from an unverified token, refetches are rate-limited to one per issuer per 10s so bogus key IDs cannot force a fetch storm, and concurrent misses are coalesced with singleflight. The auth interceptor and the session-identity service share one Verifier. Implements the TODO formerly at k8sjwt.go:177.
| flight singleflight.Group | ||
|
|
||
| mu sync.Mutex | ||
| issuers map[string]*issuerKeys |
Collaborator
There was a problem hiding this comment.
Is it a concern that we could end up with a lot of entries cached over time? Do we need to proactively clear out old entries at all?
Author
There was a problem hiding this comment.
good point, the eviction for issuer should be bounded by the config now (the expected issuer), but you prompted a real gap - revoked keys never aged out and added a 5-minute max-age so revoked keys stop being trusted within one interval, PTAL.
- Rename Verifier to CachedVerifier so Verifier stays available as a future interface name. - Document why the issuers map is bounded (only configured trusted issuers reach the cache; Verify rejects other issuers first). - Include throttling and the last-fetch time in the throttled-refetch error, and the keyID in the discovery error. - Use "keyID" consistently in messages and comments for searchability. - Comment why mu.Unlock() is managed manually instead of deferred.
Unknown-keyID refetches only pick up keys added to the issuer's JWKS. A key removed from the JWKS (revoked) would previously be trusted until process restart, because tokens signed by it keep hitting the cache. Cached keys older than keyMaxAge (5m) now trigger an asynchronous refresh: requests are served from the current cache while the fetch runs, so the hot path stays free of network I/O, and revoked keys stop verifying within one interval. Refresh failures keep the previous keys and are retried after the next interval.
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.
Previously every
k8sjwt.Verifycall performed a full OIDC discovery (two HTTP round trips: the discovery document plus the JWKS) against the issuer. In JWT auth mode that cost was paid on every authenticated RPC, and MintJWT paid it again for the client token.Introduce
k8sjwt.Verifier, which caches each issuer's verification keys in memory. Keys are only refetched when a JWT presents an unknown key ID (i.e. on key rotation). Since the triggering key ID comes from an unverified token, refetches are rate-limited to one per issuer per 10s so bogus key IDs cannot force a fetch storm, and concurrent misses are coalesced withsingleflight. The auth interceptor and the session-identity service share one Verifier.