Skip to content

Add Redis storage for exchange grants - #6981

Merged
jhrozek merged 2 commits into
mainfrom
exchange-storage-redis
Oct 29, 2026
Merged

jhrozek merged 2 commits into
mainfrom
exchange-storage-redis

Conversation

@jhrozek

@jhrozek jhrozek commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Summary

Token-exchange refresh tokens need a server-side grant record, and the Redis backend had no implementation of ExchangeGrantStorage. Without it, a deployment that runs the auth server on Redis cannot keep exchange grants across restarts or share them between replicas. This adds the Redis backend, so the memory backend is no longer the only one.

  • Each grant is a HASH at {prefix}xgrant:{signature}. The immutable record is JSON in data; revocation time is a separate rev_ms field, so a revoke is one write.
  • Creating a grant is one Lua script. It runs every check (wrong key type, duplicate signature, reused subject token jti) before any write, so a failed create leaves nothing behind and concurrent creates for one jti admit exactly one. A top-level grant also writes a replay marker for the subject access token's (issuer, jti).
  • Revoke reads the record to apply the expiry check, then sets rev_ms with HSETNX inside a small script. It keeps the first revocation time and leaves both TTLs alone.
  • Record expiry decisions use the store clock; the Redis TTL is only garbage collection. A create whose ExpiresAt has already passed is refused before the script runs, as the memory backend does, so it cannot burn the subject token's jti.
  • docs/arch/11-auth-server-storage.md and docs/redis-storage.md document the keys, TTLs and persistence expectations.

Part of #6895

Type of change

  • New feature

Test plan

  • Unit tests: go test -race ./pkg/authserver/storage/ passes. This covers the new redis_exchange_grant_test.go (against miniredis) and the shared storage contract tests. I ran the package directly, not the whole task test suite.
  • E2E tests (task test-e2e)
  • Linting (task lint-fix)
  • Manual testing (describe below)

TestIntegration_ExchangeGrantConcurrentCreateOneJTI starts 20 concurrent creates for one jti and requires exactly one to win. It is in redis_integration_test.go and I did not run it against a real Redis in this PR.

Changes

File Change
pkg/authserver/storage/redis_exchange_grant.go ExchangeGrantStorage on RedisStorage: create and revoke scripts, get, TTL helper
pkg/authserver/storage/redis_keys.go KeyTypeExchangeGrant
pkg/authserver/storage/redis.go Clock comment now covers exchange grants
pkg/authserver/storage/redis_exchange_grant_test.go Unit tests
pkg/authserver/storage/redis_integration_test.go Concurrent-create test
docs/arch/11-auth-server-storage.md, docs/redis-storage.md Keys, TTLs, create/revoke behavior, persistence

Does this introduce a user-facing change?

No behavior changes until a caller starts using ExchangeGrantStorage. Operators who later run exchange refresh tokens on Redis should enable AOF: without it, or after a failover to a replica that missed the latest writes, a lost replay marker allows a second exchange of the same access token, and a lost revocation makes a grant usable again until it expires. This is documented in docs/redis-storage.md.

Special notes for reviewers

  • Replay marker window: The marker's window follows Redis's own TTL (the script checks EXISTS); Go only skips the marker once its window has already passed. ExchangeGrantMarkerMargin (1 minute) absorbs clock skew, which can only lengthen the window.
  • Duplicate check differs from memory: Redis treats any existing record hash as a duplicate signature, while the memory backend ignores a record past its ExpiresAt. The gap is about 1 ms plus network latency around expiry, and either outcome is safe.
  • 1 ms TTL slack: Every key TTL has 1 ms added so liveness stays inclusive at the boundary instant under miniredis. On real Redis it only extends garbage collection (records) or the replay block (markers) by 1 ms.

Implement ExchangeGrantStorage on RedisStorage so exchange grants
survive restarts and are shared by replicas. Each grant is a HASH at
<prefix>xgrant:<signature>; a top-level grant also writes a replay
marker for its subject token's (issuer, jti).

Creation is one Lua script that runs every check before any write, so
concurrent creates for one jti admit exactly one and a failure leaves
nothing behind. Revoke is a single HSETNX of rev_ms that keeps the
first revocation time and leaves the TTLs alone. All time decisions
and TTLs come from the store clock.

Co-authored-by: Mecatl <[email protected]>
@github-actions github-actions Bot added the size/L Large PR: 600-999 lines changed label Oct 28, 2026
@codecov

codecov Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.11111% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.25%. Comparing base (1710945) to head (b70f3a8).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
pkg/authserver/storage/redis_exchange_grant.go 91.11% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6981      +/-   ##
==========================================
+ Coverage   80.24%   80.25%   +0.01%     
==========================================
  Files         847      848       +1     
  Lines       86097    86232     +135     
==========================================
+ Hits        69085    69203     +118     
- Misses      17007    17024      +17     
  Partials        5        5              

☔ View full report in Codecov by Harness.
📢 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.

@ChrisJBurns ChrisJBurns 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.

See the inline comments.

ChrisJBurns
ChrisJBurns previously approved these changes Oct 29, 2026

@ChrisJBurns ChrisJBurns 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.

Small nit. approving

The shared contract now requires a create whose ExpiresAt is already
past to fail without writing anything. Redis accepted such a grant with
a 1 ms record TTL and still wrote the replay marker, so a corrected
retry with the same subject token got ErrJTIKnown.

Check ExpiresAt against the store clock before the script runs, as the
memory backend does, and drop the TTL floor that only existed for this
case.

Also correct the CreateExchangeGrant contract. It said a failed call
writes nothing, which a networked backend cannot promise: if the
connection drops after Redis ran the script, the record and marker
exist while the caller sees an error. Document that, and that the
replay marker is only as durable as the Redis replication setup.

Co-authored-by: Mecatl <[email protected]>
@jhrozek
jhrozek force-pushed the exchange-storage-redis branch from b84f273 to b70f3a8 Compare October 29, 2026 14:01
@github-actions github-actions Bot added size/L Large PR: 600-999 lines changed and removed size/L Large PR: 600-999 lines changed labels Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/L Large PR: 600-999 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants