Bound the key cache and ration lookups of unknown keys per client address

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5.5 committed 2026-10-04 10:19:38 +02:00
1 parent ebf437a36a
commit d3fb217b31
5 files changed
+154 -4

No files matched your search

+46 -3
View File
@@ -96,6 +96,9 @@ var (
ErrInvalid = errors.New("invalid api key")
ErrExpired = errors.New("api key expired")
ErrRevoked = errors.New("api key revoked")
// ErrThrottled is a client presenting more unknown keys than it is
// allowed to have looked up.
ErrThrottled = errors.New("too many unrecognised api keys from this address")
)
// Authenticator resolves keys and caches the result.
@@ -111,6 +114,10 @@ type Authenticator struct {
// and writing it per request would put a database write back on the path.
touchMu sync.Mutex
touch map[string]time.Time
// lookups rations the database reads of keys not in the cache, per
// client address.
lookups *buckets
}
type entry struct {
@@ -124,13 +131,22 @@ func New(s store.Store, cfg Config) *Authenticator {
cfg.normalize()
return &Authenticator{
store: s, cfg: cfg, now: time.Now,
cache: map[string]entry{},
touch: map[string]time.Time{},
cache: map[string]entry{},
touch: map[string]time.Time{},
lookups: newBuckets(20, 50),
}
}
// Authenticate resolves a presented credential.
func (a *Authenticator) Authenticate(ctx context.Context, presented string) (Principal, error) {
return a.AuthenticateFrom(ctx, presented, "")
}
// AuthenticateFrom resolves a credential presented by client, an address:
// a key not in the cache costs a database read, so those are rationed per
// client, and one that has made too many is told to slow down rather than
// read for. Empty client is not rationed.
func (a *Authenticator) AuthenticateFrom(ctx context.Context, presented, client string) (Principal, error) {
if presented == "" {
return Principal{}, ErrMissing
}
@@ -150,6 +166,9 @@ func (a *Authenticator) Authenticate(ctx context.Context, presented string) (Pri
return e.principal, e.err
}
if client != "" && !a.lookups.allow(client, a.now()) {
return Principal{}, ErrThrottled
}
p, err := a.resolve(ctx, hash)
if err != nil && !isRejection(err) {
// A database problem is not an authentication answer, so it must not be
@@ -211,10 +230,34 @@ func (a *Authenticator) lookup(id string) (entry, bool) {
return e, true
}
// maxCached bounds the cache. Every well-formed key anyone presents is an
// entry, a rejected one included, so without a bound a stream of made-up keys
// grows it until the process runs out of memory.
const maxCached = 100_000
func (a *Authenticator) store_(id string, e entry) {
a.mu.Lock()
defer a.mu.Unlock()
if len(a.cache) >= maxCached {
now := a.now()
for k, v := range a.cache {
if now.After(v.expires) {
delete(a.cache, k)
}
}
// Still full of live entries: half of them go, which costs only a
// lookup each for the keys in use.
if len(a.cache) >= maxCached {
n := 0
for k := range a.cache {
if n%2 == 0 {
delete(a.cache, k)
}
n++
}
}
}
a.cache[id] = e
a.mu.Unlock()
}
// Invalidate drops every cached decision.