Until now anything that could reach the client port could use every model in the catalog. A key now resolves to an identity, and that identity decides what it may do. Authentication wraps the whole mux rather than checking inside each handler, so adding a route cannot add an unauthenticated one by omission. Health stays open, because a load balancer has no key and a health check that needs one reports the gateway down. Nothing on the request path reads the database. A key is resolved once and answered from memory until the entry expires; rejections are cached too, so a client looping with a bad key cannot turn into a database read per request. The price is that a revocation takes up to cache_ttl to take effect, which is the trade stated in the configuration next to the knob. A store failure is not an authentication answer: it is never cached as one and returns 503, not 401 — sending someone hunting for a key problem that does not exist wastes the one thing an incident is short of. A revoked key is refused with the same message as an unknown one, because saying otherwise confirms the key was real. With auth on the band comes from the key and the band header is ignored. The header was trusted only while there was nothing to consult, and it let any client promote itself into the top priority band. Tenant fairness within a band is now per key rather than per session, so one caller cannot take a band by opening many sessions. Allowlists are decided per model, not per string: a model's canonical name and every alias name the same thing, so either form works in an allowlist and no alias can reach a model that was not allowed. The first version of that test passed without the resolution being there at all, which is why it now checks all four combinations. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
29617f8cb1
commit
45c8cd23a3
11 files changed
+1491
-14
No files matched your search
@@ -12,6 +12,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/auth"
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/router/admission"
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/router/proxy"
|
||||
)
|
||||
@@ -22,13 +23,18 @@ func (r *Router) admit(ctx context.Context, req *parsedRequest) (*admission.Leas
|
||||
if r.adm == nil {
|
||||
return nil, nil
|
||||
}
|
||||
// The key is the tenant when there is one: fairness within a band has to be
|
||||
// per caller, and a session id is something the caller picks, so one client
|
||||
// could take a whole band by opening many sessions. Without authentication
|
||||
// the session is still the best available grouping.
|
||||
tenant := req.Tenant
|
||||
if tenant == "" {
|
||||
tenant = req.SessionID
|
||||
}
|
||||
return r.adm.Acquire(ctx, admission.Request{
|
||||
Model: req.Model,
|
||||
Band: req.Band,
|
||||
// Until authentication arrives the session is the best tenant key we
|
||||
// have: it groups one caller's traffic without pretending to identify
|
||||
// them (plan §7.9).
|
||||
Tenant: req.SessionID,
|
||||
Model: req.Model,
|
||||
Band: req.Band,
|
||||
Tenant: tenant,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -74,7 +80,13 @@ func (r *Router) Handler() http.Handler {
|
||||
mux.HandleFunc("GET /health", func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
return mux
|
||||
if r.auth == nil {
|
||||
return mux
|
||||
}
|
||||
// Wrapping the whole mux rather than checking inside each handler: adding a
|
||||
// route must not be able to add an unauthenticated one by omission. Health
|
||||
// stays open because a load balancer has no key.
|
||||
return auth.Middleware(r.auth, r.log, "/health")(mux)
|
||||
}
|
||||
|
||||
// parsedRequest is what the router needs from a request. Everything else in the
|
||||
@@ -89,8 +101,12 @@ type parsedRequest struct {
|
||||
UpstreamBody []byte
|
||||
Header http.Header
|
||||
|
||||
SessionID string
|
||||
Band string
|
||||
SessionID string
|
||||
Band string
|
||||
// KeyID, ProjectID and Tenant are empty when authentication is off.
|
||||
KeyID string
|
||||
ProjectID string
|
||||
Tenant string
|
||||
ProfileOverride string
|
||||
PromptPrefix string
|
||||
PromptTokens int
|
||||
@@ -127,6 +143,12 @@ func (r *Router) handleInference(path string) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
if p, ok := auth.FromContext(req.Context()); ok && !r.permits(p, parsed.Model) {
|
||||
auth.WriteForbiddenModel(w, parsed.Model)
|
||||
r.m.observeRequest(parsed.Model, "", http.StatusForbidden, time.Since(start), nil, 0)
|
||||
return
|
||||
}
|
||||
|
||||
// Step 5 of the lifecycle: "now?" before "where?" (plan §7.1).
|
||||
lease, err := r.admit(req.Context(), parsed)
|
||||
if err != nil {
|
||||
@@ -145,6 +167,27 @@ func (r *Router) handleInference(path string) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// permits reports whether a key's allowlist covers the requested model.
|
||||
//
|
||||
// The decision is per model, not per string. A model's canonical name and every
|
||||
// alias of it name the same thing, so the allowlist is matched against all of
|
||||
// them: writing either form works, and no alias can reach a model that was not
|
||||
// allowed. Matching only what the client sent would make every alias a way
|
||||
// around the allowlist; matching only the canonical name would silently ignore
|
||||
// an allowlist written in the names clients actually use.
|
||||
func (r *Router) permits(p auth.Principal, requested string) bool {
|
||||
canonical, m := r.resolve(requested)
|
||||
if p.Allows(canonical) {
|
||||
return true
|
||||
}
|
||||
for _, alias := range m.Aliases {
|
||||
if p.Allows(alias) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// finish records metrics and, when nothing was written, produces the error body.
|
||||
func (r *Router) finish(w http.ResponseWriter, req *parsedRequest, res dispatchResult, start time.Time) {
|
||||
status := res.outcome.Status
|
||||
@@ -203,6 +246,20 @@ func (r *Router) parse(req *http.Request, path string) (*parsedRequest, error) {
|
||||
MaxTokens: max(b.MaxTokens, b.MaxCompletionTokens),
|
||||
PromptPrefix: promptPrefix(&b),
|
||||
}
|
||||
// With authentication on, the key decides the band and the routing profile,
|
||||
// and the headers stop being believed. Trusting the header was only ever
|
||||
// acceptable while there was no identity to consult — it let any client
|
||||
// promote itself into the top priority band.
|
||||
if principal, ok := auth.FromContext(req.Context()); ok {
|
||||
p.Band = principal.Band
|
||||
p.ProfileOverride = principal.RoutingProfile
|
||||
p.KeyID = principal.KeyID
|
||||
p.ProjectID = principal.ProjectID
|
||||
// Tenant fairness within a band is per key rather than per session, so
|
||||
// one key cannot take a whole band by opening many sessions.
|
||||
p.Tenant = principal.KeyID
|
||||
}
|
||||
|
||||
// Bytes over four: a crude stand-in for a tokenizer, good enough for
|
||||
// scoring. It becomes exact only with plan §7.5 tier 2 (see plan §14).
|
||||
p.PromptTokens = len(p.PromptPrefix) / 4
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/auth"
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/config"
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/control/catalog"
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/router/admission"
|
||||
@@ -23,6 +24,7 @@ import (
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/router/proxy"
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/router/scheduler"
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/router/scrape"
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
|
||||
)
|
||||
|
||||
// Router serves the OpenAI API in front of a set of engine replicas.
|
||||
@@ -41,8 +43,19 @@ type Router struct {
|
||||
// models maps every name a client may use — canonical names and aliases —
|
||||
// onto its catalog entry.
|
||||
models map[string]catalog.Model
|
||||
|
||||
// auth and store are nil when authentication is disabled.
|
||||
auth *auth.Authenticator
|
||||
store *store.SQLite
|
||||
}
|
||||
|
||||
// Auth exposes the authenticator so ctl and the batch writer can reach it.
|
||||
// Nil when authentication is off.
|
||||
func (r *Router) Auth() *auth.Authenticator { return r.auth }
|
||||
|
||||
// Store exposes the key store, or nil when authentication is off.
|
||||
func (r *Router) Store() *store.SQLite { return r.store }
|
||||
|
||||
// New builds a router from configuration. Start must be called to begin scraping.
|
||||
func New(cfg config.Server, log *slog.Logger) (*Router, error) {
|
||||
if log == nil {
|
||||
@@ -101,6 +114,18 @@ func New(cfg config.Server, log *slog.Logger) (*Router, error) {
|
||||
Timeout: cfg.Router.ScrapeTimeout,
|
||||
}, log)
|
||||
|
||||
if cfg.Auth.Enabled {
|
||||
st, err := store.OpenSQLite(context.Background(), cfg.Auth.Store)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("auth store: %w", err)
|
||||
}
|
||||
r.store = st
|
||||
r.auth = auth.New(st, auth.Config{
|
||||
TTL: cfg.Auth.CacheTTL,
|
||||
NegativeTTL: cfg.Auth.NegativeTTL,
|
||||
})
|
||||
}
|
||||
|
||||
if cfg.Router.Admission.Enabled {
|
||||
adm, err := admission.New(admissionConfig(cfg.Router), r.saturation)
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user