Skip to content

server: shared per-principal rate limiter config, key function and constructor #151

Open

Description

@patdowney

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-burst and disable-rate-limit in eventbroker's server config and in all three connect-control apps.
  • Defaults: DefaultRateLimitRPS = 10000.0 and DefaultRateLimitBurst = 1000 in both eventbroker/internal/cmd/server/server.go and connect-control/internal/server/ratelimit.go.
  • Key function: eventbroker principalFromRequest uses authctx.AuthenticatedPrincipalFromContext (the token subject); connect-control PrincipalFromRequest uses auth.PrincipalContextFromContext(...).ID. Both fall back to dch.GetClientIP.
  • Wiring: each passes dch.WithStaticRateLimit, dch.WithPrincipalFunc and dch.WithRateLimitLogger(log.Logger) (the global logger). connect passes dch.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 dch pieces, 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 and desc tags, the shared defaults, and a Check that rejects negatives (eventbroker's own Validate does today).
  • PrincipalKey(r *http.Request) (string, error): the extracted principal's ID if present, else the authenticated token subject, else the safe client address from dioad/net once 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-wiring dch.NewRateLimiter.
  • Document the ordering rule connect-control already states: it must run after principal extraction, or every request shares the address bucket.

Migration

  1. 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.
  2. 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 Disable yields a pass-through.
  • A test that a limiter built with a registerer registers there and not on the default.
  • make verify with GOWORK=off.

Related

🤖 Generated with Claude Code

https://claude.ai/code/session_01SS4MAwAn85D1gkEzLtjYaZ

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions