Repository navigation
Open
Description
Why this issue
Four services (eventbroker, and connect-control's accounting, entitlements and agent registry) each build the same per-principal rate limiter: the same three config keys, the same defaults, and a near-identical "principal, else client address" key function. connect does a variant of it. This is repeated enough to share, and the copies share a defect (the fallback key is spoofable, see dioad/net#369). It comes from dioad/eventbroker#108 (candidate X3, row E13).
What exists today
- Config:
rate-limit-rps,rate-limit-burstanddisable-rate-limitin eventbroker's server config and in all three connect-control apps. - Defaults:
DefaultRateLimitRPS = 10000.0andDefaultRateLimitBurst = 1000in botheventbroker/internal/cmd/server/server.goandconnect-control/internal/server/ratelimit.go. - Key function: eventbroker
principalFromRequestusesauthctx.AuthenticatedPrincipalFromContext(the token subject); connect-controlPrincipalFromRequestusesauth.PrincipalContextFromContext(...).ID. Both fall back todch.GetClientIP. - Wiring: each passes
dch.WithStaticRateLimit,dch.WithPrincipalFuncanddch.WithRateLimitLogger(log.Logger)(the global logger). connect passesdch.WithRateLimiterRegistry; the others do not, so their limiter metrics land on the default registerer. - connect has two limiters of its own (per client address before authentication, per principal after) built from the same
dchpieces, with a lazily built limiter and an injected registerer.
Proposal
In dioad/auth http/server, next to APIMiddlewares:
RateLimitConfig{RPS, Burst, Disable}with the existing keys anddesctags, the shared defaults, and aCheckthat rejects negatives (eventbroker's ownValidatedoes today).PrincipalKey(r *http.Request) (string, error): the extracted principal's ID if present, else the authenticated token subject, else the safe client address fromdioad/netonce net#369 exists. It must never use a spoofable header.RateLimiter(cfg RateLimitConfig, logger zerolog.Logger, registerer prometheus.Registerer) (nethttp.Middleware, error), returning a pass-through when disabled, so a service stops hand-wiringdch.NewRateLimiter.- Document the ordering rule connect-control already states: it must run after principal extraction, or every request shares the address bucket.
Migration
- eventbroker (E13 in feat: add WithScope RequestOpt for client-credentials token requests #108) and connect-control adopt it and delete their local copies, in the same change.
- connect adopts the key function for its post-authentication limiter and keeps its own address-keyed one.
Acceptance criteria
- Tests that two principals get separate buckets, that an unauthenticated caller falls back to the connection address and cannot choose its bucket with a forwarding header, and that
Disableyields a pass-through. - A test that a limiter built with a registerer registers there and not on the default.
make verifywithGOWORK=off.
Related
🤖 Generated with Claude Code
Metadata
Metadata
Assignees
Labels
No labels
Activity