mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-12-06 11:03:52 +00:00
## Summary This change makes the gateway advertise and discover the vision capability of each model. It also adds OIDC follow-ups and the fixes from a security audit of the change. ### Model vision capability - `GET /v1/models` writes `architecture.input_modalities` and `info.meta.capabilities.vision` for each model with a known vision flag. Open WebUI reads `info.meta.capabilities.vision`, and other clients read `architecture.input_modalities`. - Discovery reads the same two fields from an upstream `/v1/models` when `metadataPath` is set. A downstream gateway thus reads the flag from an upstream gateway. - Resolution order: alias `modelMeta`, then target `modelMeta`, then discovery. An unknown flag counts as capable, as before. - The smart router uses the resolved flag. For an alias target, it now uses the alias `modelMeta` for vision and `contextTokens`, as `/v1/models` does. ### OIDC follow-ups - The gateway refreshes the discovery document on the `jwksRefresh` cadence. A failed refresh keeps the last good copy. - The user list hides a registry record whose name is not the name its identity resolves to now. - A `groupClaim` that contains `://` is one top-level claim name. A dotted `groupClaim` walks the path only, so a flat claim of the same spelling cannot shadow it. - The gateway refuses ID tokens: header `typ` `id+jwt` (also with the `application/` prefix) and payload `typ` `ID`. - The user registry reads Redis with `HSCAN` pages. One `HGETALL` reply failed above 512 users, and then prune stopped. ## Verification - `go test -race ./...`: pass in both packages. - `golangci-lint run`: 0 issues. `make yaegi-check`: OK. `govulncheck ./...`: no vulnerabilities. - Each audit fix has a test that failed before the fix. - A test against a local `redis-server` 7 loaded 2000 registry records and pruned the stale ones. - A quick security audit (run-2) covered 15 units. It confirmed one low-severity availability defect, the registry read, which this change fixes.
281 lines
10 KiB
Go
281 lines
10 KiB
Go
package traefikllmgateway
|
|
|
|
import (
|
|
"net/url"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func validOIDCConfig() *OIDCConfig {
|
|
return &OIDCConfig{
|
|
Issuer: "https://idp.example.com/realms/main",
|
|
Audiences: []string{"openwebui"},
|
|
GroupClaim: "groups",
|
|
GroupMap: map[string]string{"llm-users": "staff"},
|
|
}
|
|
}
|
|
|
|
func oidcTestGroups() map[string]*group {
|
|
return map[string]*group{"staff": {name: "staff"}, "power": {name: "power"}}
|
|
}
|
|
|
|
func TestParseOIDCConfig(t *testing.T) {
|
|
tests := []struct {
|
|
mutate func(c *OIDCConfig)
|
|
name string
|
|
wantErr string
|
|
}{
|
|
{name: "empty issuer", mutate: func(c *OIDCConfig) { c.Issuer = "" }, wantErr: "issuer"},
|
|
{name: "plain http issuer", mutate: func(c *OIDCConfig) { c.Issuer = "http://idp.example.com" }, wantErr: "issuer"},
|
|
{name: "unparsable issuer", mutate: func(c *OIDCConfig) { c.Issuer = "https://%zz" }, wantErr: "issuer"},
|
|
{name: "empty audiences", mutate: func(c *OIDCConfig) { c.Audiences = nil }, wantErr: "audiences"},
|
|
{name: "blank audience", mutate: func(c *OIDCConfig) { c.Audiences = []string{" "} }, wantErr: "audiences"},
|
|
{name: "empty groupClaim", mutate: func(c *OIDCConfig) { c.GroupClaim = "" }, wantErr: "groupClaim"},
|
|
{name: "groupClaim empty segment", mutate: func(c *OIDCConfig) { c.GroupClaim = "a..b" }, wantErr: "groupClaim"},
|
|
{name: "empty groupMap", mutate: func(c *OIDCConfig) { c.GroupMap = nil }, wantErr: "groupMap"},
|
|
{name: "unknown group", mutate: func(c *OIDCConfig) { c.GroupMap = map[string]string{"x": "nope"} }, wantErr: `unknown group "nope"`},
|
|
{name: "negative maxUsers", mutate: func(c *OIDCConfig) { c.MaxUsers = -1 }, wantErr: "maxUsers"},
|
|
{name: "bad cacheTTL", mutate: func(c *OIDCConfig) { c.CacheTTL = "soon" }, wantErr: "cacheTTL"},
|
|
{name: "zero cacheTTL", mutate: func(c *OIDCConfig) { c.CacheTTL = "0s" }, wantErr: "cacheTTL"},
|
|
{name: "bad jwksRefresh", mutate: func(c *OIDCConfig) { c.JWKSRefresh = "-1m" }, wantErr: "jwksRefresh"},
|
|
{name: "empty identityClaim path segment", mutate: func(c *OIDCConfig) { c.IdentityClaim = " " }, wantErr: "identityClaim"},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
c := validOIDCConfig()
|
|
tc.mutate(c)
|
|
_, err := parseOIDCConfig(c, oidcTestGroups())
|
|
if err == nil || !strings.Contains(err.Error(), tc.wantErr) {
|
|
t.Fatalf("err = %v, want containing %q", err, tc.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseOIDCConfig_Defaults(t *testing.T) {
|
|
s, err := parseOIDCConfig(validOIDCConfig(), oidcTestGroups())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if s.identityClaim != "email" || !s.requireEmailVerified || s.userinfo {
|
|
t.Fatalf("identity defaults: %+v", s)
|
|
}
|
|
if s.cacheTTL != 5*time.Minute || s.jwksRefresh != time.Hour || s.maxUsers != 10000 {
|
|
t.Fatalf("duration/size defaults: %+v", s)
|
|
}
|
|
if !s.audiences["openwebui"] || len(s.audiences) != 1 {
|
|
t.Fatalf("audiences = %v", s.audiences)
|
|
}
|
|
if !reflect.DeepEqual(s.groupClaim, []string{"groups"}) {
|
|
t.Fatalf("groupClaim = %v", s.groupClaim)
|
|
}
|
|
}
|
|
|
|
func TestParseOIDCConfig_Overrides(t *testing.T) {
|
|
c := validOIDCConfig()
|
|
c.Issuer = "http://127.0.0.1:8080/"
|
|
c.IdentityClaim = "sub"
|
|
c.GroupClaim = "realm_access.roles"
|
|
c.CacheTTL = "30s"
|
|
c.JWKSRefresh = "10m"
|
|
c.MaxUsers = 3
|
|
c.Userinfo = true
|
|
s, err := parseOIDCConfig(c, oidcTestGroups())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if s.issuer != "http://127.0.0.1:8080/" {
|
|
t.Fatalf("issuer = %q", s.issuer)
|
|
}
|
|
if s.identityClaim != "sub" || s.requireEmailVerified {
|
|
t.Fatalf("non-email identity must not require email_verified by default: %+v", s)
|
|
}
|
|
if !reflect.DeepEqual(s.groupClaim, []string{"realm_access", "roles"}) {
|
|
t.Fatalf("groupClaim = %v", s.groupClaim)
|
|
}
|
|
if s.cacheTTL != 30*time.Second || s.jwksRefresh != 10*time.Minute || s.maxUsers != 3 || !s.userinfo {
|
|
t.Fatalf("overrides: %+v", s)
|
|
}
|
|
|
|
off := false
|
|
c.IdentityClaim = "email"
|
|
c.RequireEmailVerified = &off
|
|
s, err = parseOIDCConfig(c, oidcTestGroups())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if s.requireEmailVerified {
|
|
t.Fatal("explicit requireEmailVerified=false must win")
|
|
}
|
|
}
|
|
|
|
func oidcAuthCfg(users ...*UserConfig) *Config {
|
|
return &Config{
|
|
Providers: map[string]*ProviderConfig{"openai": {Type: "openai", APIKey: "k"}},
|
|
Groups: map[string]*GroupConfig{
|
|
"staff": {MemberLimits: &LimitsConfig{RequestsPerMinute: 10}},
|
|
"power": {},
|
|
},
|
|
OIDC: validOIDCConfig(),
|
|
Users: &UsersConfig{Inline: users},
|
|
}
|
|
}
|
|
|
|
func TestNewAuthStore_OIDCOverrides(t *testing.T) {
|
|
a, err := newAuthStore(oidcAuthCfg(
|
|
&UserConfig{Name: "bob", Group: "staff", APIKey: "sk-bob"},
|
|
&UserConfig{Name: "alice", OIDCIdentity: "[email protected]", Limits: &LimitsConfig{RequestsPerMinute: 5}},
|
|
))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ov, ok := a.oidcOverrides["[email protected]"]
|
|
if !ok || ov.name != "alice" || ov.limits.RequestsPerMinute != 5 {
|
|
t.Fatalf("override index = %+v", a.oidcOverrides)
|
|
}
|
|
if len(a.byDigest) != 1 {
|
|
t.Fatalf("override must not be a keyed user; byDigest has %d entries", len(a.byDigest))
|
|
}
|
|
if !a.oidcEnabled {
|
|
t.Fatal("oidcEnabled must be set when cfg.OIDC is set")
|
|
}
|
|
|
|
bad := []struct {
|
|
uc *UserConfig
|
|
name string
|
|
wantErr string
|
|
}{
|
|
{name: "apiKey", uc: &UserConfig{Name: "c", OIDCIdentity: "c@x", APIKey: "k"}, wantErr: "apiKey"},
|
|
{name: "group", uc: &UserConfig{Name: "c", OIDCIdentity: "c@x", Group: "staff"}, wantErr: "group"},
|
|
{name: "groups", uc: &UserConfig{Name: "c", OIDCIdentity: "c@x", Groups: []string{"staff"}}, wantErr: "group"},
|
|
{name: "providers", uc: &UserConfig{Name: "c", OIDCIdentity: "c@x", Providers: []string{"openai"}}, wantErr: "providers"},
|
|
{name: "models", uc: &UserConfig{Name: "c", OIDCIdentity: "c@x", Models: []string{"m"}}, wantErr: "models"},
|
|
{name: "admin", uc: &UserConfig{Name: "c", OIDCIdentity: "c@x", Admin: true}, wantErr: "admin"},
|
|
{name: "no name", uc: &UserConfig{OIDCIdentity: "c@x"}, wantErr: "name"},
|
|
{name: "bad limits", uc: &UserConfig{Name: "c", OIDCIdentity: "c@x", Limits: &LimitsConfig{RequestsPerMinute: -1}}, wantErr: `"c"`},
|
|
{name: "oidc prefix on keyed user", uc: &UserConfig{Name: "oidc:bob", Group: "staff", APIKey: "k2"}, wantErr: "oidc:"},
|
|
{name: "oidc prefix on override", uc: &UserConfig{Name: "oidc:c", OIDCIdentity: "c@x"}, wantErr: "oidc:"},
|
|
{name: "duplicate identity", uc: &UserConfig{Name: "alice2", OIDCIdentity: "[email protected]"}, wantErr: "duplicate oidcIdentity"},
|
|
{name: "name clashes with keyed user", uc: &UserConfig{Name: "bob", OIDCIdentity: "b@x"}, wantErr: "duplicate user name"},
|
|
{name: "name clashes with override", uc: &UserConfig{Name: "alice", OIDCIdentity: "other@x"}, wantErr: "duplicate user name"},
|
|
}
|
|
for _, tc := range bad {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
_, err := newAuthStore(oidcAuthCfg(
|
|
&UserConfig{Name: "bob", Group: "staff", APIKey: "sk-bob"},
|
|
&UserConfig{Name: "alice", OIDCIdentity: "[email protected]"},
|
|
tc.uc,
|
|
))
|
|
if err == nil || !strings.Contains(err.Error(), tc.wantErr) {
|
|
t.Fatalf("err = %v, want containing %q", err, tc.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNewAuthStore_OIDCIdentityNeedsOIDC(t *testing.T) {
|
|
cfg := oidcAuthCfg(&UserConfig{Name: "alice", OIDCIdentity: "[email protected]"})
|
|
cfg.OIDC = nil
|
|
_, err := newAuthStore(cfg)
|
|
if err == nil || !strings.Contains(err.Error(), "oidcIdentity needs oidc") {
|
|
t.Fatalf("err = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestNewAuthStore_OIDCPrefixAllowedWithoutOIDC(t *testing.T) {
|
|
cfg := oidcAuthCfg(&UserConfig{Name: "oidc:legacy", Group: "staff", APIKey: "k"})
|
|
cfg.OIDC = nil
|
|
if _, err := newAuthStore(cfg); err != nil {
|
|
t.Fatalf("a keyed user named oidc:* must stay valid while oidc is off: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestReplaceFileUsers_OIDCOverrides(t *testing.T) {
|
|
a, err := newAuthStore(oidcAuthCfg(
|
|
&UserConfig{Name: "bob", Group: "staff", APIKey: "sk-bob"},
|
|
&UserConfig{Name: "alice", OIDCIdentity: "[email protected]", Limits: &LimitsConfig{RequestsPerMinute: 5}},
|
|
&UserConfig{Name: "carol", OIDCIdentity: "[email protected]"},
|
|
))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
err = a.replaceFileUsers([]*UserConfig{
|
|
{Name: "dave", Group: "power", APIKey: "sk-dave"},
|
|
{Name: "alice-file", OIDCIdentity: "[email protected]", Limits: &LimitsConfig{RequestsPerMinute: 9}},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(a.byDigest) != 2 {
|
|
t.Fatalf("byDigest = %d entries, want 2 (bob inline + dave file)", len(a.byDigest))
|
|
}
|
|
if ov := a.oidcOverrides["[email protected]"]; ov == nil || ov.name != "alice-file" || ov.limits.RequestsPerMinute != 9 {
|
|
t.Fatalf("file override must replace the inline one: %+v", ov)
|
|
}
|
|
if ov := a.oidcOverrides["[email protected]"]; ov == nil || ov.name != "carol" {
|
|
t.Fatalf("untouched inline override must survive: %+v", ov)
|
|
}
|
|
|
|
err = a.replaceFileUsers([]*UserConfig{
|
|
{Name: "x", OIDCIdentity: "[email protected]"},
|
|
{Name: "y", OIDCIdentity: "[email protected]"},
|
|
})
|
|
if err == nil || !strings.Contains(err.Error(), "duplicate oidcIdentity") {
|
|
t.Fatalf("err = %v", err)
|
|
}
|
|
if ov := a.oidcOverrides["[email protected]"]; ov == nil || ov.name != "alice-file" {
|
|
t.Fatal("a rejected reload must keep the previous overrides")
|
|
}
|
|
}
|
|
|
|
func TestRedactConfig_OIDCIssuerSanitized(t *testing.T) {
|
|
cfg := oidcAuthCfg()
|
|
issuer := url.URL{Scheme: "https", User: url.UserPassword("user", "hunter2"), Host: "idp.example.com", Path: "/realms/main", RawQuery: "token=s3cret"}
|
|
cfg.OIDC.Issuer = issuer.String()
|
|
redacted, err := redactConfig(cfg)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := redacted["oidc"].(map[string]any)["issuer"].(string)
|
|
if strings.Contains(got, "hunter2") || strings.Contains(got, "s3cret") {
|
|
t.Fatalf("issuer not sanitized: %q", got)
|
|
}
|
|
if !strings.Contains(got, "idp.example.com") {
|
|
t.Fatalf("issuer host lost: %q", got)
|
|
}
|
|
}
|
|
|
|
func TestParseOIDCConfig_NamespacedGroupClaim(t *testing.T) {
|
|
for _, key := range []string{"https://example.com/groups", "https://a..b/x"} {
|
|
c := validOIDCConfig()
|
|
c.GroupClaim = key
|
|
s, err := parseOIDCConfig(c, oidcTestGroups())
|
|
if err != nil {
|
|
t.Fatalf("%q: %v", key, err)
|
|
}
|
|
if len(s.groupClaim) != 1 || s.groupClaim[0] != key {
|
|
t.Fatalf("groupClaim = %q, want one segment %q", s.groupClaim, key)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A flat top-level claim spelled like a dotted groupClaim must not shadow the
|
|
// nested value the IdP asserts.
|
|
func TestGroupValues_FlatKeyDoesNotShadowDottedPath(t *testing.T) {
|
|
c := validOIDCConfig()
|
|
c.GroupClaim = "realm_access.roles"
|
|
s, err := parseOIDCConfig(c, oidcTestGroups())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
claims := map[string]any{
|
|
"realm_access.roles": []any{"unmapped"},
|
|
"realm_access": map[string]any{"roles": []any{"llm-users"}},
|
|
}
|
|
if got := s.groupValues(claims); !reflect.DeepEqual(got, []string{"llm-users"}) {
|
|
t.Fatalf("groupValues = %v, want the nested [llm-users]", got)
|
|
}
|
|
}
|