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.
190 lines
6.9 KiB
Go
190 lines
6.9 KiB
Go
package traefikllmgateway
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func boolPtr(v bool) *bool { return &v }
|
|
|
|
func TestDiscoveredVisionOf(t *testing.T) {
|
|
t.Parallel()
|
|
tests := []struct {
|
|
want *bool
|
|
name string
|
|
entry string
|
|
}{
|
|
{nil, "vllm sends nothing", `{"id":"m","max_model_len":8192}`},
|
|
{boolPtr(false), "llama.cpp text only", `{"id":"m","architecture":{"input_modalities":["text"],"output_modalities":["text"]}}`},
|
|
{boolPtr(true), "openrouter image", `{"id":"m","architecture":{"input_modalities":["text","image"]}}`},
|
|
{nil, "empty modality list is unknown", `{"id":"m","architecture":{"input_modalities":[]}}`},
|
|
{boolPtr(true), "open webui true", `{"id":"m","info":{"meta":{"capabilities":{"vision":true}}}}`},
|
|
{boolPtr(false), "open webui false beats image modality", `{"id":"m","architecture":{"input_modalities":["image"]},"info":{"meta":{"capabilities":{"vision":false}}}}`},
|
|
{nil, "architecture as a string is ignored", `{"id":"m","architecture":"llama"}`},
|
|
{boolPtr(true), "info without vision falls back to modalities", `{"id":"m","architecture":{"input_modalities":["text","image"]},"info":{"meta":{}}}`},
|
|
{nil, "vision of wrong type is ignored", `{"id":"m","info":{"meta":{"capabilities":{"vision":"yes"}}}}`},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
var e modelMetadataEntry
|
|
require.NoError(t, json.Unmarshal([]byte(tt.entry), &e))
|
|
got := discoveredVisionOf(e.Architecture, e.Info)
|
|
if tt.want == nil {
|
|
assert.Nil(t, got)
|
|
return
|
|
}
|
|
require.NotNil(t, got)
|
|
assert.Equal(t, *tt.want, *got)
|
|
})
|
|
}
|
|
}
|
|
|
|
// A llama.cpp-style `capabilities` array next to the fields we read must not fail the whole payload.
|
|
func TestFetchModelMetadata_CapturesVisionPerUpstream(t *testing.T) {
|
|
t.Parallel()
|
|
main := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
_, _ = fmt.Fprint(w, `{"data":[{"id":"main","max_model_len":4096,"capabilities":["completion"],"architecture":{"input_modalities":["text"]}}]}`)
|
|
}))
|
|
t.Cleanup(main.Close)
|
|
vis := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
_, _ = fmt.Fprint(w, `{"data":[{"id":"ds","max_model_len":1048576,"info":{"meta":{"capabilities":{"vision":true}}}},{"id":"main","architecture":{"input_modalities":["image"]}}]}`)
|
|
}))
|
|
t.Cleanup(vis.Close)
|
|
ad := newOpenAIAdapter("d1", main.URL, "")
|
|
ad.modelBases = map[string]string{"ds": vis.URL}
|
|
ad.metadataPath = "/v1/models"
|
|
|
|
ctxs, err := ad.fetchModelMetadata(context.Background())
|
|
require.NoError(t, err)
|
|
assert.Equal(t, map[string]int{"main": 4096, "ds": 1048576}, ctxs)
|
|
|
|
got := ad.discoveredVision()
|
|
require.Len(t, got, 2)
|
|
assert.False(t, got["main"], "main's own upstream wins over another upstream's entry for the same id")
|
|
assert.True(t, got["ds"])
|
|
}
|
|
|
|
func TestRegistry_ResolveMetaUsesDiscoveredVision(t *testing.T) {
|
|
t.Parallel()
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
_, _ = fmt.Fprint(w, `{"data":[{"id":"ds","info":{"meta":{"capabilities":{"vision":true}}}},{"id":"txt","architecture":{"input_modalities":["text"]}},{"id":"plain"}]}`)
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
ad := newOpenAIAdapter("d1", srv.URL, "")
|
|
ad.metadataPath = "/v1/models"
|
|
cfg := &Config{
|
|
Providers: map[string]*ProviderConfig{"d1": {Discovery: true, MetadataPath: "/v1/models"}},
|
|
ModelMeta: map[string]*ModelMetaConfig{"txt": {Vision: boolPtr(true)}},
|
|
}
|
|
reg, err := newModelRegistry(map[string]providerAdapter{"d1": ad}, cfg, func(string, ...any) {})
|
|
require.NoError(t, err)
|
|
reg.captureModelMetadata(context.Background(), "d1", ad, reg.states["d1"])
|
|
|
|
ds := reg.resolveMetaFor("d1", "ds")
|
|
require.NotNil(t, ds.Vision)
|
|
assert.True(t, *ds.Vision)
|
|
|
|
txt := reg.resolveMetaFor("d1", "txt")
|
|
require.NotNil(t, txt.Vision)
|
|
assert.True(t, *txt.Vision, "operator modelMeta beats discovery")
|
|
|
|
assert.Nil(t, reg.resolveMetaFor("d1", "plain").Vision, "nothing known stays unknown")
|
|
}
|
|
|
|
func TestResolveAliasModelMeta_AliasVisionWins(t *testing.T) {
|
|
t.Parallel()
|
|
cfg := map[string]*ModelMetaConfig{
|
|
"m": {Vision: boolPtr(true)},
|
|
"fast": {Vision: boolPtr(false)},
|
|
}
|
|
got := resolveAliasModelMeta("fast", "p", "m", cfg, 0, false)
|
|
require.NotNil(t, got.Vision)
|
|
assert.False(t, *got.Vision)
|
|
|
|
inherited := resolveAliasModelMeta("other", "p", "m", cfg, 0, false)
|
|
require.NotNil(t, inherited.Vision)
|
|
assert.True(t, *inherited.Vision)
|
|
}
|
|
|
|
func TestModelObject_Capabilities(t *testing.T) {
|
|
t.Parallel()
|
|
tests := []struct {
|
|
vision *bool
|
|
name string
|
|
modalities []any
|
|
}{
|
|
{name: "unknown omits both fields"},
|
|
{name: "vision", vision: boolPtr(true), modalities: []any{"text", "image"}},
|
|
{name: "text only", vision: boolPtr(false), modalities: []any{"text"}},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
raw, err := json.Marshal(modelObject("m", "p", resolvedModelMeta{Vision: tt.vision}))
|
|
require.NoError(t, err)
|
|
var obj map[string]any
|
|
require.NoError(t, json.Unmarshal(raw, &obj))
|
|
if tt.vision == nil {
|
|
assert.NotContains(t, obj, "architecture")
|
|
assert.NotContains(t, obj, "info")
|
|
return
|
|
}
|
|
assert.Equal(t, tt.modalities, obj["architecture"].(map[string]any)["input_modalities"])
|
|
caps := obj["info"].(map[string]any)["meta"].(map[string]any)["capabilities"].(map[string]any)
|
|
assert.Equal(t, *tt.vision, caps["vision"])
|
|
|
|
var e modelMetadataEntry
|
|
require.NoError(t, json.Unmarshal(raw, &e))
|
|
back := discoveredVisionOf(e.Architecture, e.Info)
|
|
require.NotNil(t, back, "a downstream gateway reads back what this one emits")
|
|
assert.Equal(t, *tt.vision, *back)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestHandleModels_EmitsVision(t *testing.T) {
|
|
t.Parallel()
|
|
cfg := CreateConfig()
|
|
cfg.Providers = map[string]*ProviderConfig{
|
|
"uni": {Type: "openai", APIKey: "sk-test", Models: []string{"ds", "plain"}},
|
|
}
|
|
cfg.ModelMeta = map[string]*ModelMetaConfig{"ds": {Vision: boolPtr(true)}}
|
|
cfg.ModelAliases = map[string]string{"model-fast": "uni/ds"}
|
|
h := newModelsCatalogGateway(t, cfg)
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/v1/models", nil)
|
|
req.Header.Set("Authorization", "Bearer sk-user")
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, req)
|
|
require.Equal(t, http.StatusOK, rec.Code, rec.Body.String())
|
|
|
|
var body struct {
|
|
Data []modelMetadataEntry `json:"data"`
|
|
}
|
|
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &body))
|
|
got := map[string]*bool{}
|
|
for _, e := range body.Data {
|
|
got[e.ID] = discoveredVisionOf(e.Architecture, e.Info)
|
|
}
|
|
for _, id := range []string{"ds", "model-fast"} {
|
|
require.NotNil(t, got[id], id)
|
|
assert.True(t, *got[id], id)
|
|
}
|
|
assert.Nil(t, got["plain"])
|
|
}
|
|
|
|
func TestVisionCapable(t *testing.T) {
|
|
t.Parallel()
|
|
assert.True(t, visionCapable(nil), "unknown keeps the candidate")
|
|
assert.True(t, visionCapable(boolPtr(true)))
|
|
assert.False(t, visionCapable(boolPtr(false)))
|
|
}
|