From e9fce98babf32ae2d8d2d41df6b6c36ffb1a5cc6 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Sun, 29 Nov 2026 00:21:43 +0000 Subject: [PATCH] fix(admin): list the anonymous user in snapshot views The auth snapshot built users from the key digest map only. The keyless principal never appeared in usage, catalog, metrics or fleet views, and its rejection counter was pruned on a users-file reload. The snapshot now adds the anonymous principal once, with source anonymous. --- anonymous_snapshot_test.go | 103 +++++++++++++++++++++++++++++++++++++ auth.go | 13 +++++ webui/src/types/api.ts | 5 +- 3 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 anonymous_snapshot_test.go diff --git a/anonymous_snapshot_test.go b/anonymous_snapshot_test.go new file mode 100644 index 0000000..0e5504d --- /dev/null +++ b/anonymous_snapshot_test.go @@ -0,0 +1,103 @@ +package traefikllmgateway + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func anonSnapshotGateway(t *testing.T, anonGroup string) (http.Handler, *Gateway) { + t.Helper() + return memberLimitsGateway(t, + map[string]*GroupConfig{"friends": {}, "public": {}}, + []*UserConfig{{Name: "a", Group: "friends", APIKey: "sk-a", Admin: true}}, + func(c *Config) { + c.Users.AnonymousGroup = anonGroup + c.Metrics = &MetricsConfig{Enabled: true} + }, + ) +} + +func TestSnapshot_IncludesAnonymousPrincipal(t *testing.T) { + a, err := newAuthStore(anonAuthCfg()) + require.NoError(t, err) + users, groups := a.snapshot() + + var anon []userSummary + for _, u := range users { + if u.name == anonymousUserName { + anon = append(anon, u) + } + } + require.Len(t, anon, 1) + assert.Equal(t, "anonymous", anon[0].source) + assert.False(t, anon[0].admin) + assert.Equal(t, "public", anon[0].groupName) + assert.Equal(t, []string{"public"}, anon[0].groups) + for _, g := range groups { + if g.name == "public" { + assert.Equal(t, 1, g.memberCount) + } + } +} + +func TestSnapshot_NoAnonymousWhenUnset(t *testing.T) { + cfg := anonAuthCfg() + cfg.Users.AnonymousGroup = "" + a, err := newAuthStore(cfg) + require.NoError(t, err) + users, _ := a.snapshot() + for _, u := range users { + assert.NotEqual(t, anonymousUserName, u.name) + } +} + +func TestAdminUsage_ListsAnonymousUser(t *testing.T) { + h, gw := anonSnapshotGateway(t, "public") + rec := memberLimitsChat(t, h, "") + require.Equal(t, http.StatusOK, rec.Code, rec.Body.String()) + + e := findUsageEntry(t, gw.buildAdminUsage().Users, anonymousUserName) + assert.NotZero(t, e.RequestsPerDay) +} + +func TestScopeExists_AnonymousUser(t *testing.T) { + h, _ := anonSnapshotGateway(t, "public") + rec := httptest.NewRecorder() + h.ServeHTTP(rec, adminRequest(http.MethodGet, adminUsageHistoryPath+"?scope=user:anonymous&metric=req&window=hour", "sk-a")) + assert.NotEqual(t, http.StatusNotFound, rec.Code, rec.Body.String()) +} + +func TestPruneRejectionsAfterReload_KeepsAnonymous(t *testing.T) { + _, gw := anonSnapshotGateway(t, "public") + gw.limiter.rejections.increment("user", anonymousUserName) + gw.pruneRejectionsAfterReload() + + var kept bool + for _, s := range gw.limiter.rejections.snapshot() { + if s.kind == "user" && s.id == anonymousUserName { + kept = true + } + } + assert.True(t, kept, "anonymous rejection counter must survive a users-file reload") +} + +func TestUsageMetrics_ExportAnonymousUser(t *testing.T) { + h, _ := anonSnapshotGateway(t, "public") + require.Equal(t, http.StatusOK, memberLimitsChat(t, h, "").Code) + + rec := httptest.NewRecorder() + h.ServeHTTP(rec, metricsRequest(http.MethodGet, metricsPathDefault, "sk-a")) + require.Equal(t, http.StatusOK, rec.Code, rec.Body.String()) + _, samples := parsePrometheusText(t, rec.Body.Bytes()) + var found bool + for _, s := range samples { + if s.name == "llmgateway_requests_total" && s.labels["scope_kind"] == "user" && s.labels["scope_id"] == anonymousUserName { + found = true + } + } + assert.True(t, found) +} diff --git a/auth.go b/auth.go index 9f93ddc..4a0c1d6 100644 --- a/auth.go +++ b/auth.go @@ -1462,6 +1462,19 @@ func (a *authStore) snapshot() ([]userSummary, []groupSummary) { memberCounts[name]++ } } + if anon := a.anonymous; anon != nil { + users = append(users, userSummary{ + limits: anon.user.limits, + limitSources: anon.user.limitSources, + name: anon.user.name, + groupName: anon.user.groupName, + groups: cloneStringSlice(anon.user.groupNames), + source: "anonymous", + }) + for _, name := range anon.user.groupNames { + memberCounts[name]++ + } + } a.mu.RUnlock() sort.Slice(users, func(i, j int) bool { return users[i].name < users[j].name }) diff --git a/webui/src/types/api.ts b/webui/src/types/api.ts index dfef88f..d3b329c 100644 --- a/webui/src/types/api.ts +++ b/webui/src/types/api.ts @@ -902,7 +902,8 @@ export interface AdminCatalogResponse { /** * One configured user's consumer-directory row (admin.go: * adminConsumerUser — phase 2 redesign). `source` is 'inline' (defined - * directly in the plugin config) or 'file' (loaded from users.file); + * directly in the plugin config), 'file' (loaded from users.file) or + * 'anonymous' (the keyless principal); * `providers`/`models` are this user's OWN personal grant, distinct from * anything their group(s) already allow. */ @@ -910,7 +911,7 @@ export interface AdminConsumerUser { limits?: LimitsConfig limitSources?: LimitSources name: string - source: 'inline' | 'file' + source: 'inline' | 'file' | 'anonymous' groups: string[] providers?: string[] models?: string[]