mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-12-06 11:03:52 +00:00
* feat(auth): add oidc config and per-identity overrides Add Config.OIDC with issuer, audiences, claim and group-map settings. The constructor validates the settings and applies defaults. A users-config entry with oidcIdentity is now a per-identity override. It sets the accounting name and the limits for one OIDC identity. It must not set an API key, groups, grants or admin. The admin config view sanitizes the issuer URL like a base URL. * feat(auth): verify oidc jwt access tokens locally Add a stdlib-only JWS verifier for OIDC access tokens. It accepts RS256, RS384, RS512, PS256, ES256 and ES384 only. It rejects "none", HMAC algorithms and a header algorithm that does not match the key. The verifier checks iss, exp and nbf with 60 seconds of skew, aud or azp against the configured audiences, and email_verified on request. JWKS parsing skips encryption keys and unsupported key types. It rejects an EC point that is not on its curve. * feat(auth): fetch oidc discovery, jwks and userinfo Add the OIDC provider client. It reads the discovery document on first use and checks that its issuer equals the configured issuer. It caches the JWKS and refreshes it every jwksRefresh. An unknown kid starts a refetch at most once per 60 seconds. A failed fetch is held for 5 seconds, and the last good keys stay in use. One fetch runs at a time, and each IdP response is capped at 1 MiB. * feat(auth): map oidc claims to cached virtual users Add the OIDC authenticator. It verifies a JWT locally, or an opaque token at the userinfo endpoint when that path is on. It then maps the group claim to configured gateway groups. A valid token with no mapped group gets a forbidden verdict. The virtual user is named "oidc:<identity>", or takes the override name. Its limits merge the override with the group memberLimits per field. Verdicts are cached until the token expires or cacheTTL ends. A refusal is cached for 30 seconds. An IdP outage is not cached. Virtual users are held in an LRU bounded by maxUsers. * feat(auth): admit oidc bearer tokens as virtual users With oidc configured, a Bearer token that matches no configured key goes to the OIDC authenticator. A configured key always wins first, and the anonymous fallbacks keep their previous order. A valid token without a mapped group gets 403 permission_error, in the route's own error shape. An invalid token gets 401 as before. The admin API and the metrics route stay key-only. All routes now use one requireAuth helper. The auth failure log line names the OIDC refusal reason. The yaegi-check harness signs RS256, PS256 and ES256 tokens and runs them through the interpreted plugin. * feat(admin): list oidc virtual users across replicas The admin snapshot now lists OIDC virtual users with source "oidc". Each one counts in the memberCount of every mapped group. Scope checks and the rejection prune after a users-file reload see them too. With Redis configured, each replica records the users it admits in the llmgw:oidc:users hash, at most once per minute per user. The snapshot reads the hash at most once every two seconds. It hides users whose groups are all gone, and users not seen for 32 days. Without Redis the list holds only the users this replica has seen. * feat(admin): badge oidc users The user panel shows an "OIDC" badge for a virtual user that an OIDC bearer token admitted. The consumer source type now includes "oidc". * docs(auth): document oidc virtual users Add the "OIDC users (Open WebUI)" section and the OIDCConfig reference table. The section covers precedence, 401 against 403, the oidc: name prefix, per-person overrides, the token cache, the Redis registry and the userinfo audience limit. It also lists the Open WebUI connection steps. * fix(auth): harden oidc key fetch, userinfo and jwks handling The discovery and JWKS fetch now runs on its own 5s deadline, so one caller's cancel cannot fail it for every request. Discovery failures now use the same 5s retry hold on the userinfo path. Userinfo calls are capped at 16 in flight. Only an IdP 401 or 403 is cached as a rejection. Timeouts, 5xx and 429 are not cached. Refused tokens use their own cache, so a flood cannot evict valid verdicts. An unknown kid is not cached, because the keys can be stale. A disallowed alg is refused before any key fetch. The jwks_uri and userinfo_endpoint must be https or loopback http. A malformed key, or an RSA key under 2048 bits, is skipped instead of failing the set. The identity is case-folded only for the email claim. IdP failure causes are logged, rate-gated, and the no_group reason names the identity. The Redis registry deletes records past the 32-day retention. * fix(auth): close oidc audit leads on azp and userinfo slots A JWT now passes the audience check only when aud names a configured audience. The azp claim counts only with the new acceptAzp option, because a token that the client obtained for another service carries the same azp. Concurrent requests with one cold token now share one verification. A userinfo call waits up to 2s for a free slot instead of failing at once, and it no longer waits on the key fetch lock once discovery is cached. The IdP client does not follow redirects. A bearer over 16384 bytes and an identity over 256 bytes get 401. Email identities fold only ASCII A-Z, so a look-alike character stays a separate identity.
192 lines
8.3 KiB
Go
192 lines
8.3 KiB
Go
package traefikllmgateway
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// logSafe escapes the line breaks in msg. A value an upstream controls, such
|
|
// as a panic or error string, must not forge extra log lines.
|
|
func logSafe(msg string) string {
|
|
if !strings.ContainsAny(msg, "\r\n") {
|
|
return msg
|
|
}
|
|
return strings.NewReplacer("\r", "\\r", "\n", "\\n").Replace(msg)
|
|
}
|
|
|
|
// logf writes an info-level log line to stderr. No timestamps — Traefik
|
|
// adds its own. Never log key material.
|
|
func (g *Gateway) logf(format string, args ...any) {
|
|
fmt.Fprintf(os.Stderr, "llmgw[%s] INFO %s\n", g.name, logSafe(fmt.Sprintf(format, args...)))
|
|
}
|
|
|
|
// warnf writes a warning-level log line to stderr. No timestamps —
|
|
// Traefik adds its own. Never log key material.
|
|
//
|
|
// Use this for a condition an operator may want to know about but that
|
|
// the gateway resolved by itself, deterministically, with no request
|
|
// affected — a model id served by two providers, for instance. Those
|
|
// lines were ERROR until 2026-08-22, which put 144 of them in a healthy
|
|
// pod's boot log on a cluster with overlapping catalogs and made real
|
|
// errors harder to find. Reserve errorf for something actually broken.
|
|
//
|
|
// Also calls noteConfigWarning (F10, v0.3 dashboard task): while
|
|
// newGateway is still constructing g (collectingWarnings true), the
|
|
// formatted message is additionally collected into g.configWarnings —
|
|
// GET /admin/api/overview's Warnings field. A warnf call from
|
|
// request-handling code, after construction finished, is NOT collected —
|
|
// see noteConfigWarning's own doc comment for why.
|
|
func (g *Gateway) warnf(format string, args ...any) {
|
|
msg := fmt.Sprintf(format, args...)
|
|
fmt.Fprintf(os.Stderr, "llmgw[%s] WARN %s\n", g.name, logSafe(msg))
|
|
g.noteConfigWarning(msg)
|
|
}
|
|
|
|
// configWarningsCap bounds how many construction-time warnings
|
|
// g.configWarnings ever holds (F10, v0.3 dashboard task) — a
|
|
// misconfigured deployment logging hundreds of warnings during
|
|
// construction must not make GET /admin/api/overview's response itself
|
|
// unbounded. configWarningsDropped counts what did not fit, so the
|
|
// dashboard can say "and N more" instead of silently truncating.
|
|
const configWarningsCap = 50
|
|
|
|
// noteConfigWarning appends msg to g.configWarnings while newGateway is
|
|
// still collecting them — collectingWarnings is set true right after g is
|
|
// constructed and false just before newGateway returns (llmgateway.go),
|
|
// so every warnf call in that window is a CONSTRUCTION-time warning,
|
|
// exactly what GET /admin/api/overview's Warnings field surfaces. Once
|
|
// collectingWarnings is false (every warnf call from request-handling
|
|
// code, for the rest of the process lifetime), this is a no-op: such a
|
|
// warning already reached the operator via the stderr line warnf itself
|
|
// wrote, and an unbounded stream of runtime warnings must never keep
|
|
// growing this slice for the life of the process.
|
|
func (g *Gateway) noteConfigWarning(msg string) {
|
|
g.warnMu.Lock()
|
|
defer g.warnMu.Unlock()
|
|
if !g.collectingWarnings {
|
|
return
|
|
}
|
|
if len(g.configWarnings) >= configWarningsCap {
|
|
g.configWarningsDropped++
|
|
return
|
|
}
|
|
g.configWarnings = append(g.configWarnings, msg)
|
|
}
|
|
|
|
// configWarningsSnapshot returns a fresh copy of g.configWarnings — never
|
|
// nil, matching GET /admin/api/overview's own "never nil" contract for
|
|
// its Warnings field — and the dropped count, safe for concurrent use
|
|
// alongside noteConfigWarning.
|
|
func (g *Gateway) configWarningsSnapshot() (warnings []string, dropped int) {
|
|
g.warnMu.Lock()
|
|
defer g.warnMu.Unlock()
|
|
out := make([]string, len(g.configWarnings))
|
|
copy(out, g.configWarnings)
|
|
return out, g.configWarningsDropped
|
|
}
|
|
|
|
// errorf writes an error-level log line to stderr. No timestamps — Traefik
|
|
// adds its own. Never log key material.
|
|
func (g *Gateway) errorf(format string, args ...any) {
|
|
fmt.Fprintf(os.Stderr, "llmgw[%s] ERROR %s\n", g.name, logSafe(fmt.Sprintf(format, args...)))
|
|
}
|
|
|
|
// pricingWarn is g's own per-instance pricing-warning sink, passed to
|
|
// costMicrosKnownFor (pricing.go) so an unpriced-model warning always logs
|
|
// under this instance's `llmgw[name]` prefix (finding F11 / review-auth
|
|
// F8, 2026-09 review: a package-level warn func replaced on every New()
|
|
// call made the most recently constructed instance log every other
|
|
// instance's warnings).
|
|
//
|
|
// A plain method, not a stored closure field: it reads g.name fresh on
|
|
// every call, exactly like logf/warnf/errorf above, so there is nothing
|
|
// new to keep in sync across a config reload that rebuilds g.
|
|
func (g *Gateway) pricingWarn(msg string) {
|
|
if msg == warnCapMessage {
|
|
g.logf("%s", msg)
|
|
return
|
|
}
|
|
g.logf("pricing: no price configured for model %q; cost will be recorded as 0", msg)
|
|
// F3 hook 4 (v0.3 dashboard task): msg is the bare unpriced model id
|
|
// here (warnUnknownModelFor's own "warn(model)" call, pricing.go) —
|
|
// the cap-message branch above is a process-wide meta-warning, not
|
|
// about any one model, so it is deliberately excluded from the event
|
|
// feed. Route is the fixed "pricing" label: this fires deep inside
|
|
// cost accounting, with no request-scoped route name available.
|
|
g.recordEvent(gatewayEvent{
|
|
Model: msg, Route: routePricing, Kind: eventKindUnpriced,
|
|
Message: fmt.Sprintf("no price configured for model %q; cost recorded as 0", msg),
|
|
})
|
|
}
|
|
|
|
// logAuthEvent logs one authentication attempt, at the point one of
|
|
// authStore.identify's six call sites resolves it. A success logs the
|
|
// resolved user's name alongside the request's method and path. Neither
|
|
// line this function ever writes includes the presented API key itself —
|
|
// identify resolves a key to a *user and hands this function only that
|
|
// user's name, never the key it authenticated.
|
|
//
|
|
// A failure logs one of two distinct lines (security review round 2,
|
|
// 2026-08-22, important finding 5): if the caller's source IP
|
|
// (clientIP) is currently authThrottled, a distinct "throttle engaged"
|
|
// line naming the IP and the limit it exceeded — an operator seeing this
|
|
// knows immediately that a SOURCE, not just an individual request, needs
|
|
// attention, which the routine per-attempt line alone does not convey.
|
|
// Otherwise the routine failure line (method, path, remote address).
|
|
// Both lines are independently rate-limited (authStore.shouldLog
|
|
// ThrottleEngaged / shouldLogAuthFailure, auth.go — the identical
|
|
// technique limiter.logStoreError, limits.go, already applies to
|
|
// store-error lines) so an unauthenticated caller driving unlimited
|
|
// failed attempts can never drive unlimited synchronous stderr writes on
|
|
// the shared Traefik ingress — a slow log pipe would otherwise stall
|
|
// request handling for every tenant, not just the one failing auth. Each
|
|
// gate reports how many events it suppressed since its last written line
|
|
// (security review round 2, important finding 6): a single global gate
|
|
// would otherwise make several DISTINCT attacker IPs failing within one
|
|
// interval indistinguishable from one noisy IP — suppressedSuffix
|
|
// appends that count to the line when non-zero. The success line stays
|
|
// completely unbounded — see shouldLogAuthFailure's own doc comment for
|
|
// why that is safe.
|
|
// detail, when non-empty, names why an OIDC token was refused; it never
|
|
// holds the token.
|
|
func (g *Gateway) logAuthEvent(ok bool, userName string, r *http.Request, detail string) {
|
|
if !ok {
|
|
ip := clientIP(r)
|
|
if g.auth.authThrottled(ip) {
|
|
if log, suppressed := g.auth.shouldLogThrottleEngaged(); log {
|
|
g.logf("auth throttle: source %s exceeded %d failed attempts in the last %s%s", ip, authFailureLimit, authFailureWindow, suppressedSuffix(suppressed))
|
|
}
|
|
return
|
|
}
|
|
if log, suppressed := g.auth.shouldLogAuthFailure(); log {
|
|
if detail != "" {
|
|
detail = " (oidc: " + detail + ")"
|
|
}
|
|
g.logf("auth failed: %s %q from %s%s%s", r.Method, r.URL.Path, r.RemoteAddr, detail, suppressedSuffix(suppressed))
|
|
}
|
|
return
|
|
}
|
|
g.logf("auth ok: user %q %s %q", userName, r.Method, r.URL.Path)
|
|
}
|
|
|
|
// suppressedSuffix formats n (a logGate's own suppressed-event count,
|
|
// auth.go) as a log-line suffix, or "" when there is nothing to report.
|
|
func suppressedSuffix(n int64) string {
|
|
if n <= 0 {
|
|
return ""
|
|
}
|
|
return fmt.Sprintf(" (%d more suppressed since last log)", n)
|
|
}
|
|
|
|
// authEventUserName extracts u's name for logAuthEvent's userName
|
|
// argument, or "" when u is nil — identify's failure path, which never
|
|
// resolves a user.
|
|
func authEventUserName(u *user) string {
|
|
if u == nil {
|
|
return ""
|
|
}
|
|
return u.name
|
|
}
|