Skip to content

Add lazy OIDC discovery with retry#3579

Open
jhrozek wants to merge 1 commit intomainfrom
lazy_discovery
Open

Add lazy OIDC discovery with retry#3579
jhrozek wants to merge 1 commit intomainfrom
lazy_discovery

Conversation

@jhrozek
Copy link
Contributor

@jhrozek jhrozek commented Feb 3, 2026

Implement deferred OIDC discovery for the token validator to handle the chicken-and-egg problem where the authserver handlers may not be ready when the validator is created. This occurs with embedded auth servers running in the same pod, where Kubernetes Service endpoints take time to propagate even after the container is listening.

Discovery is now deferred until the first ValidateToken call using a sync.Once pattern. If discovery fails, exponential backoff retries up to 3 times with a ~3.5 second total window, which handles the typical networking propagation delays. Each attempt has a 5-second timeout and respects context cancellation.

This enables the auth proxy to validate tokens from its own embedded auth server without requiring complex startup sequencing or readiness coordination.

@jhrozek jhrozek marked this pull request as draft February 3, 2026 22:26
@github-actions github-actions bot added the size/S Small PR: 100-299 lines changed label Feb 3, 2026
@jhrozek
Copy link
Contributor Author

jhrozek commented Feb 3, 2026

drafting because the tests suck

@codecov
Copy link

codecov bot commented Feb 3, 2026

Codecov Report

❌ Patch coverage is 90.62500% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 65.65%. Comparing base (f16d0cc) to head (97d083a).

Files with missing lines Patch % Lines
pkg/auth/token.go 90.62% 6 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3579      +/-   ##
==========================================
+ Coverage   65.61%   65.65%   +0.03%     
==========================================
  Files         407      407              
  Lines       40246    40300      +54     
==========================================
+ Hits        26407    26458      +51     
- Misses      11779    11786       +7     
+ Partials     2060     2056       -4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Defer OIDC discovery until the first ValidateToken call to handle the
case where the OIDC provider (e.g. an embedded auth server) starts
after the token validator is created. This occurs with embedded auth
servers running in the same pod, where Kubernetes Service endpoints
take time to propagate even after the container is listening.

Use a mutex+flag pattern so that failed discovery can be retried on
subsequent calls — transient failures should not be permanent. On
failure, exponential backoff retries up to 3 attempts with a ~3.5s
total window, which handles typical networking propagation delays.
Each attempt has a 5-second timeout and respects context cancellation.

Add a WithEnvReader functional option to NewTokenValidator for
injecting the env.Reader dependency, defaulting to env.OSReader.
This eliminates process-wide os.Setenv calls in tests, enabling
t.Parallel() on all OIDC-related tests.
@jhrozek jhrozek marked this pull request as ready for review February 4, 2026 22:36
@github-actions github-actions bot added size/M Medium PR: 300-599 lines changed and removed size/S Small PR: 100-299 lines changed labels Feb 4, 2026
@jhrozek jhrozek requested a review from tgrunnagle February 4, 2026 22:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/M Medium PR: 300-599 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant