mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-12-06 11:03:52 +00:00
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.
This commit is contained in:
@@ -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)
|
||||
}
|
||||
@@ -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 })
|
||||
|
||||
|
||||
@@ -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[]
|
||||
|
||||
Reference in New Issue
Block a user