identcache

package
v1.0.489 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 20, 2026 License: MIT Imports: 2 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package identcache is the bounded, client-attributed result cache shared by every identity backend on Culvert's proxy credential path: the LDAP directory bind (auth_ldap.go), the legacy OIDC introspection backend (auth_oidc.go) and the IdP-registry introspection provider (auth_oidc_flow.go), all in package main. It is a self-contained stdlib-only leaf (ADR-0041): package main owns the backends, their keys and TTLs, the cap it passes in, and the process-wide eviction total /metrics exports, which it receives through Options.OnEvict.

Why the eviction policy is a security control (CHAOS-76)

All three caches are populated by UNAUTHENTICATED requests. The proxy credential path hands whatever username/password or bearer token the client supplied straight to the configured backend, and an AUTHORITATIVE denial is cached deliberately — an RFC 7662 endpoint answers an unknown or expired token with 200 {"active":false}, and a directory that answers "no such user" is an authoritative deny — because not caching those would make every wrong credential a fresh round trip. So the key space is derived entirely from caller-supplied bytes, and whoever can reach the listener decides what is in the map.

All three previously evicted "an arbitrary entry when the cache is full": a Go map range that stops at the first key, i.e. whichever entry Go's randomised map iteration happened to yield, live or already expired. (Go randomises where a range STARTS; it does not promise a uniform draw over keys, so the victim is arbitrary rather than uniformly distributed — the property that matters is only that it is unrelated to age, ownership or liveness.) That is register row AU-3c exactly, which CHAOS-57 closed in the LOCAL auth cache (store.go) and which was never ported to these three siblings. Two consequences followed, both reachable by an anonymous client:

  • A flood of distinct bogus credentials displaced OTHER clients' cached positives at random. Each insertion past the cap evicts an honest entry with probability (honest entries / cap), so the honest population decays geometrically: one cap-worth of flood removes ~63% of it and three cap-worths remove ~95%. The victim then pays a full directory bind or introspection round trip on EVERY subsequent request, which lands the attacker's amplification on legitimate traffic and on the IdP itself.

  • Worse, the victim is chosen without regard to whether it is still LIVE. Nothing reclaims an expired entry (the lookup path reported a miss and left it in place), so the steady state under a flood is a map full of DEAD attacker verdicts while eviction destroys the few live ones. An expired entry is free to drop — it would have been a miss anyway — so evicting a live entry in preference to one is pure loss.

The sharp end is the interaction with the CHAOS-47/CHAOS-49 unreachable cooldown. Displacing an honest client's cached positive while the backend is in its cooldown turns a cache HIT into a DENIAL, because the gated path denies without a round trip. The cache's stated purpose is to let a cached verdict ride out a short backend blip; a flood inverts that for exactly the users it exists to serve. Failure stays CLOSED — there is no bypass — but the gateway's authentication becomes remotely deniable by a client holding no VALID credential — bogus ones are all it needs, since those are what the cache records — which is the failure shape internal/authstate's package doc was written for.

The policy

internal/authstate's, the same one CHAOS-57 ported into store.go, so there is ONE eviction policy in the tree rather than a fourth variant:

  • Entries are attributed to a CLIENT fairness key. Eviction takes the OLDEST entry of the client holding the MOST live entries, ties broken by oldest entry then by client key (BetterVictim, which store.go's local cache also uses), so the victim never depends on Go's map iteration order. A flooding source evicts ITSELF until it is no longer the largest holder, and a client holding one entry cannot be displaced until every other client is down to one too.

  • An EXPIRED entry is always the better victim and is taken wherever it is found, because dropping it costs nobody anything. The lookup path also reclaims an expired entry in place, so dead verdicts no longer occupy capacity until an eviction happens to land on them.

  • Work per insertion is O(active clients) — bucket FRONTS only, never a scan of the whole map. The O(cache) scan under a process-wide mutex is register row AU-3d, and re-introducing it here would trade one measured defect for another.

Entries carry a PER-ENTRY expiry rather than one store-wide TTL, because a positive introspection verdict is clamped to the token's own declared lifetime (package main's clampCacheTTLToTokenExpiry) and may therefore be shorter-lived than the backend's configured TTL. A bucket front is consequently the client's oldest entry by INSERTION, which is not necessarily its first to expire; that only means a deeper expired entry waits until it reaches the front, and it leaves the fairness property — the one an attacker has to beat — untouched.

What package main supplies

The cap and the eviction sink are construction dependencies (Options), not package state: the cap is main's maxAuthCacheSize, shared with the local credential cache in store.go, and OnEvict feeds the process-wide culvert_identity_cache_evictions_total counter, which has to outlive any one cache because the IdP registry compiles a fresh provider (and so a fresh, zero cache) whenever an operator edits a profile. The package holds no globals, so independent caches, and tests, never share a count.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BetterVictim

func BetterVictim(live int, added time.Time, client string, bestLive int, bestAdded time.Time, bestClient string) bool

BetterVictim reports whether candidate (live, added, client) is a better eviction victim than the incumbent (bestLive, bestAdded, bestClient). Ordering: most live entries first, then the oldest entry, then the lexicographically smaller client key — total and deterministic, so the victim never depends on Go's map iteration order. It is internal/authstate's ordering; package main's local credential cache (store.go) uses this definition too.

Types

type Cache

type Cache[T any] struct {
	// contains filtered or unexported fields
}

Cache is one backend's result cache. Construct it with New; the policy is described in the package documentation. All methods are safe for concurrent use.

func New

func New[T any](opts Options) *Cache[T]

New returns an empty Cache configured by opts.

func (*Cache[T]) Evictions

func (c *Cache[T]) Evictions() uint64

Evictions reports how many cached verdicts have been displaced to stay under the cap.

func (*Cache[T]) Expiry

func (c *Cache[T]) Expiry(key string) (time.Time, bool)

Expiry reports the absolute instant the entry under key stops being served, and whether an entry is recorded. It does NOT apply the expiry check: callers use it to assert ON the recorded deadline (that a positive verdict was clamped to the token's own declared lifetime rather than the backend's TTL).

func (*Cache[T]) Get

func (c *Cache[T]) Get(key string) (val T, ok, hit bool)

Get returns a live cached verdict. An expired entry is removed in place and reported as absent, so a dead verdict stops occupying capacity as soon as anybody looks at it.

func (*Cache[T]) Len

func (c *Cache[T]) Len() int

Len reports the number of entries currently held, live or expired.

func (*Cache[T]) Set

func (c *Cache[T]) Set(key, client string, val T, ok bool, ttl time.Duration)

Set records a verdict for key, attributed to client, live for ttl.

client is an opaque fairness key: it is never compared against anything a caller supplies later and never reaches a lookup, so a wrong or empty value can only make eviction less fair, never admit an entry that should not be admitted. An empty client is legitimate — all such entries share one bucket and evict each other, which is the pre-CHAOS-76 behaviour for any caller that cannot name its peer.

func (*Cache[T]) SetUntil

func (c *Cache[T]) SetUntil(key, client string, val T, ok bool, expiry time.Time)

SetUntil records a verdict that stops being served at an ABSOLUTE deadline the caller has already computed. Same fairness-key contract as Set.

It exists because re-deriving that deadline from a duration here would move it. A caller that clamps a positive verdict to the token's own `exp` (clampCacheTTLToTokenExpiry) measures the remaining lifetime against its own clock sample, and every microsecond it then spends before reaching this mutex — cloning the identity, contending with another request on the credential path, waiting out a GC pause — would be added back to the entry's life. The entry would outlive the credential it was derived from by exactly that interval, which is the one thing the clamp exists to prevent, and the pre-CHAOS-76 implementations avoided by construction: they wrote `expiry: now.Add(ttl)` with the SAME now the clamp had used.

Callers with no external deadline — an LDAP bind governed only by the configured TTL — use Set.

type Options

type Options struct {
	// MaxEntries is the hard cap on entries held, live or expired. Values
	// below 1 are treated as 1.
	MaxEntries int

	// OnEvict, when non-nil, is called once for every entry displaced to stay
	// under the cap, AFTER the cache's own per-instance count has been
	// incremented. It runs with the cache's lock held, so it must be cheap,
	// must not block and must not call back into the same Cache. Package main
	// uses it to accumulate the process-wide eviction total that /metrics
	// exports.
	OnEvict func()
}

Options are a Cache's construction dependencies.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL