Skip to content

🐛 Start gRPC listener before credential resolution - #323

Merged
huang195 merged 1 commit into
rossoctl:mainfrom
huang195:fix/authbridge-lazy-startup
Apr 16, 2026
Merged

huang195 merged 1 commit into
rossoctl:mainfrom
huang195:fix/authbridge-lazy-startup

Conversation

@huang195

Copy link
Copy Markdown
Member

Summary

  • Start the ext_proc gRPC listener before resolving credential files, so Envoy can connect immediately
  • Replace eager NewJWKSVerifier with LazyJWKSVerifier that fetches JWKS keys on first Verify() call (with retry on failure)
  • Resolve credential files in a background goroutine after listeners start
  • Use atomic.Pointer for identity and sync.RWMutex for exchanger auth to safely update from the background goroutine

Problem

The authbridge binary blocks for up to 2 minutes at startup waiting for /shared/client-id.txt and /shared/client-secret.txt (60s timeout each). These files are written by the client-registration sidecar, which may not be ready yet on a fresh cluster. During this wait, the ext_proc gRPC server on port 9090 is not running -- Envoy gets Connection refused on every outbound request.

This caused flaky E2E failures: test_agent_simple_query fails because the weather-service agent cannot reach weather-tool-mcp through Envoy.

Fix

Before After
Resolve() blocks on credential files (up to 120s) Resolve() returns immediately
NewJWKSVerifier() makes HTTP call at startup LazyJWKSVerifier defers to first Verify()
gRPC listener starts after all blocking completes gRPC listener starts immediately
Outbound passthrough fails during wait Outbound passthrough works from the start

Files changed

  • authbridge/authlib/validation/lazy.go -- new: LazyJWKSVerifier with retry on init failure
  • authbridge/authlib/config/resolve.go -- remove blocking waits, export ResolveCredentialFiles and ResolveClientAuth
  • authbridge/authlib/auth/auth.go -- atomic.Pointer[IdentityConfig], add UpdateIdentity
  • authbridge/authlib/exchange/client.go -- sync.RWMutex for auth, add UpdateAuth
  • authbridge/cmd/authbridge/main.go -- start listeners first, resolve credentials in background

Test plan

  • go test ./authlib/... -race passes
  • go test ./cmd/authbridge/... -race passes
  • go build succeeds for both modules
  • E2E: deploy on Kind, verify weather-agent reaches weather-tool within seconds of pod creation
  • E2E: verify inbound JWT validation works after JWKS keys are lazily fetched

Assisted-By: Claude (Anthropic AI) [email protected]

The authbridge binary blocked for up to 2 minutes at startup waiting
for credential files written by client-registration. During this time,
the ext_proc gRPC server was not running, causing Envoy to get
Connection refused on every outbound request.

- Move credential file resolution to a background goroutine that runs
  after the gRPC listener starts
- Replace eager JWKS verifier with LazyJWKSVerifier that fetches keys
  on first Verify() call, with retry on failure
- Use atomic.Pointer for identity and sync.RWMutex for exchanger auth
  to safely update credentials from the background goroutine
- Export ResolveCredentialFiles and ResolveClientAuth for use by main

Outbound passthrough traffic now works immediately on startup.
Token exchange and inbound validation work once credentials and
JWKS keys are available.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Hai Huang <[email protected]>

@cwiklik cwiklik left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

Good fix for a real startup race. The architecture is sound: lazy JWKS verification, atomic.Pointer for identity (lock-free hot-path reads), RWMutex for exchanger auth (allows concurrent token exchanges), and credential resolution moved to a background goroutine.

Verified:

  • Audience: cfg.Identity.ClientID (main.go:309) matches the existing pattern in Resolve() on main (// inbound audience defaults to client ID) — not a bug
  • All a.identity read sites in auth.go properly migrated to .Load() — no missed call sites
  • Comment claim that goroutine has exclusive cfg.Identity write access checks out

Suggestions (non-blocking)

1. Missing tests for validation/lazy.go — 51 lines of new concurrency-sensitive code (mutex, lazy init, retry-on-failure) with zero test coverage. Worth adding lazy_test.go covering:

  • Verify() initializes only on first call
  • Init failure is retried on next Verify (not cached)
  • Concurrent Verify calls during init don't trigger multiple HTTP fetches (single-flight via mutex)

The existing jwks_test.go is a good template.

2. Silent failure mode in cmd/authbridge/main.go — if credential files never appear (e.g., client-registration sidecar crashed) or JWKS endpoint is permanently unreachable, the system runs in degraded state with only slog.Warn messages. Consider exposing a readiness signal (e.g., a flag flipped by UpdateIdentity and surfaced via a health endpoint) so Kubernetes can detect the degraded state instead of silently routing traffic to a non-functional authbridge.

Praise

  • Retry-on-failure (vs sync.Once which would permanently cache the error) is the right call — temporarily unreachable JWKS shouldn't permanently break validation. Good comment explaining the choice.
  • atomic.Pointer[IdentityConfig] for lock-free reads on the inbound hot path + sync.RWMutex for exchanger auth — right concurrency primitives for each access pattern.

Areas reviewed: Go (auth, exchange, validation, config, main)
Commits: 1 commit, signed-off ✓, Assisted-By trailer ✓
CI: 18/18 passing ✓

LGTM.

@huang195
huang195 merged commit e59fb43 into rossoctl:main Apr 16, 2026
18 checks passed
@huang195
huang195 deleted the fix/authbridge-lazy-startup branch April 16, 2026 17:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants