mirror of
https://github.com/netbirdio/netbird.git
synced 2026-10-11 07:59:08 +02:00
The listing was narrowed by the provider record's enumerated models, which is the right bound only while one policy reaches a provider. Where two teams share a provider under different allowlists, every caller was offered the union: each model outside their own policy is a request the guardrail refuses a moment later, which is the empty-or-wrong picker this endpoint exists to avoid, moved one level up. A gateway record enumerating nothing was worse still — it offered the upstream's entire catalogue however narrow the policy. The synthesiser already knows which policies authorise a provider and which groups each binds, so the router can answer this at request time where it knows the caller's groups. Each route now carries one rule per authorising policy — its source groups and the models it permits — and the listing is bounded to the union across the rules matching the caller, intersected with what the provider serves. This is deliberately finer than the guardrail's own per-provider allowlist, which stays as it is: that list is a fail-closed backstop and cannot tell who is asking, so discovery is now narrower than the backstop rather than wider. A policy setting no allowlist lifts the restriction for the groups it binds, so nil and empty model lists stay distinct end to end — collapsing them would let a listing that should offer nothing fall open to everything.
129 lines
5.7 KiB
Go
129 lines
5.7 KiB
Go
package llm_router
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/netbirdio/netbird/proxy/internal/middleware"
|
|
"github.com/netbirdio/netbird/proxy/internal/middleware/builtin"
|
|
)
|
|
|
|
// ProviderRoute describes one upstream LLM provider the router can
|
|
// hand a request to. Models lists the model identifiers the provider
|
|
// claims; UpstreamScheme + UpstreamHost replace the synth target's
|
|
// placeholder URL on a match. UpstreamPath is the path component of
|
|
// the configured upstream URL — the router uses it to disambiguate
|
|
// providers that claim the same model: when more than one provider
|
|
// matches the model, the route whose UpstreamPath is a prefix of the
|
|
// incoming request path is preferred (longest match wins, empty path
|
|
// is the catchall). AuthHeaderName + AuthHeaderValue are the
|
|
// per-provider credential the router injects after stripping the
|
|
// vendor auth headers from the inbound request.
|
|
//
|
|
// AllowedGroupIDs is the union of source-group IDs across every
|
|
// enabled policy that authorises this provider. The router treats it
|
|
// as a hard filter: a route whose AllowedGroupIDs has no intersection
|
|
// with the caller's UserGroups is removed from the candidate list
|
|
// before the path-prefix tiebreak. A route with empty AllowedGroupIDs
|
|
// is unreachable; the synthesiser only emits policy-bound routes.
|
|
type ProviderRoute struct {
|
|
ID string `json:"id"`
|
|
// Vendor is the parser surface this provider speaks ("openai",
|
|
// "anthropic", …), matching the llm.provider value llm_request_parser
|
|
// emits from the request. When set, the router keeps a vendor-tagged
|
|
// request on a same-vendor route so catch-all gateways of a different
|
|
// vendor can't swallow it. Empty disables vendor filtering for this
|
|
// route.
|
|
Vendor string `json:"vendor,omitempty"`
|
|
Models []string `json:"models"`
|
|
UpstreamScheme string `json:"upstream_scheme"`
|
|
UpstreamHost string `json:"upstream_host"`
|
|
UpstreamPath string `json:"upstream_path,omitempty"`
|
|
AuthHeaderName string `json:"auth_header_name"`
|
|
AuthHeaderValue string `json:"auth_header_value"`
|
|
AllowedGroupIDs []string `json:"allowed_group_ids"`
|
|
// ModelPolicies carries, per authorising policy, the source groups it
|
|
// binds and the models it permits. The router uses it to bound a model
|
|
// listing to what THIS caller may use: a provider reachable by two groups
|
|
// under different allowlists must not offer either group the other's
|
|
// models. Empty means no policy restricts models on this route.
|
|
ModelPolicies []ModelPolicyRule `json:"model_policies,omitempty"`
|
|
// Vertex marks a Google Vertex AI provider. Vertex requests carry the
|
|
// model in the URL path, so the router selects this route by path
|
|
// (isVertexPath) and bypasses the model/vendor table entirely.
|
|
Vertex bool `json:"vertex,omitempty"`
|
|
// Bedrock marks an AWS Bedrock provider. Bedrock requests carry the model
|
|
// in the URL path (/model/{id}/{action}), so the router selects this route
|
|
// by path (isBedrockPath) and bypasses the model/vendor table; auth is the
|
|
// static AuthHeaderValue bearer token (no token minting).
|
|
Bedrock bool `json:"bedrock,omitempty"`
|
|
// GCPServiceAccountKeyB64 is a base64-encoded GCP service-account JSON
|
|
// key. When set, the router mints + refreshes a short-lived OAuth2 access
|
|
// token from it at request time and injects it as the auth header value
|
|
// (instead of the static AuthHeaderValue) — so the gateway holds a durable
|
|
// Vertex credential rather than a 1-hour token.
|
|
GCPServiceAccountKeyB64 string `json:"gcp_sa_key_b64,omitempty"`
|
|
// SkipTLSVerify disables upstream TLS certificate verification when dialing
|
|
// this route's upstream. For self-hosted / internal gateways behind a
|
|
// private or self-signed certificate.
|
|
SkipTLSVerify bool `json:"skip_tls_verify,omitempty"`
|
|
}
|
|
|
|
// ModelPolicyRule is one authorising policy's contribution to what a caller
|
|
// may use on a route: the source groups it binds, and the models it permits.
|
|
//
|
|
// Models is nil when the policy sets no model allowlist — an unrestricted
|
|
// policy, which lifts the restriction for the groups it binds. That is why
|
|
// nil and empty must stay distinct: an empty list is a guardrail that permits
|
|
// nothing, and collapsing the two would let a listing fail open.
|
|
type ModelPolicyRule struct {
|
|
GroupIDs []string `json:"group_ids"`
|
|
Models []string `json:"models"`
|
|
}
|
|
|
|
// Config is the on-wire configuration accepted by the factory. An
|
|
// empty Providers slice yields a router that denies every request as
|
|
// not-routable; the synthesiser is responsible for stamping the
|
|
// account's enabled providers into this slice.
|
|
type Config struct {
|
|
Providers []ProviderRoute `json:"providers"`
|
|
}
|
|
|
|
// Factory builds llm_router instances from raw config bytes.
|
|
type Factory struct{}
|
|
|
|
// ID returns the registry identifier.
|
|
func (Factory) ID() string { return ID }
|
|
|
|
// New constructs a middleware instance. Empty, null, and {} configs
|
|
// yield a router with an empty Providers slice — every request denies
|
|
// with model_not_routable. Non-empty payloads must parse cleanly so
|
|
// misconfigurations surface at chain build time.
|
|
func (Factory) New(rawConfig []byte) (middleware.Middleware, error) {
|
|
cfg := Config{}
|
|
if !isEmptyJSON(rawConfig) {
|
|
if err := json.Unmarshal(rawConfig, &cfg); err != nil {
|
|
return nil, fmt.Errorf("decode config: %w", err)
|
|
}
|
|
}
|
|
return New(cfg), nil
|
|
}
|
|
|
|
// isEmptyJSON reports whether the payload is whitespace, null, or an
|
|
// empty object/array. The caller skips Unmarshal in that case so the
|
|
// zero-value Config flows through unchanged.
|
|
func isEmptyJSON(raw []byte) bool {
|
|
trimmed := strings.TrimSpace(string(bytes.TrimSpace(raw)))
|
|
switch trimmed {
|
|
case "", "null", "{}", "[]":
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func init() {
|
|
builtin.Register(Factory{})
|
|
}
|