perf: cache OIDC verification keys in k8sjwt#391
Open
botengyao wants to merge 1 commit into
Open
Conversation
botengyao
marked this pull request as ready for review
July 4, 2026 05:05
| 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 there could be 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.
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.CachedVerifier, which caches each issuer's verification keys in memory: - Keys are fetched on first use and refetched synchronously when a JWT presents an unknown keyID (key rotation). Since that keyID comes from an unverified token, refetches are rate-limited to one per issuer per 10s so bogus keyIDs cannot force a fetch storm, and concurrent misses are coalesced with singleflight. - Cached keys older than 5m trigger an asynchronous refresh so keys removed from the JWKS (revoked) age out; requests are served from the current cache while the fetch runs (stale-while-revalidate). - The issuers map is bounded by the configured trusted issuers: Verify rejects other issuers before consulting the cache. The auth interceptor and the session-identity service share one CachedVerifier. The package-level Verify remains as an uncached one-shot wrapper.
botengyao
force-pushed
the
k8sjwt-key-cache
branch
from
July 25, 2026 01:22
8a4edfa to
51e14e1
Compare
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.