Files
traefik-llmgateway/oidc_registry_test.go
T
lukaszraczylo 212c43f4a0 fix(auth): hide renamed oidc override names in the user list
A renamed users file override left its old name in the Redis registry.
The admin list showed that name for up to 32 days.

The list now shows a registry record only under the name its identity
resolves to now. This is the override name, or the default oidc name when
no override exists. Usage counters stay untouched.
2026-12-02 02:02:44 +00:00

217 lines
6.9 KiB
Go

package traefikllmgateway
import (
"net/http"
"reflect"
"strconv"
"testing"
"time"
)
func findUserSummary(users []userSummary, name string) (userSummary, bool) {
for _, u := range users {
if u.name == name {
return u, true
}
}
return userSummary{}, false
}
func TestSnapshot_IncludesOIDCUsers(t *testing.T) {
fx := newOIDCGateway(t, nil)
if rec := fx.chat(t, fx.token(t, nil)); rec.Code != http.StatusOK {
t.Fatalf("status %d", rec.Code)
}
users, groups := fx.gw.auth.snapshot()
u, ok := findUserSummary(users, "oidc:[email protected]")
if !ok || u.source != "oidc" || u.groupName != "default" || u.limits.RequestsPerMinute != 50 {
t.Fatalf("virtual user summary = %+v (found %v)", u, ok)
}
for _, g := range groups {
if g.name == "default" && g.memberCount != 3 {
t.Fatalf("default memberCount = %d, want 3 (admin1, dotted, oidc user)", g.memberCount)
}
}
}
func TestScopeExists_OIDCUser(t *testing.T) {
fx := newOIDCGateway(t, nil)
if fx.gw.scopeExists("user", "oidc:[email protected]") {
t.Fatal("unseen virtual user must not exist yet")
}
if rec := fx.chat(t, fx.token(t, nil)); rec.Code != http.StatusOK {
t.Fatalf("status %d", rec.Code)
}
if !fx.gw.scopeExists("user", "oidc:[email protected]") {
t.Fatal("seen virtual user must be a known scope")
}
}
func TestPruneRejections_KeepsOIDCUsers(t *testing.T) {
fx := newOIDCGateway(t, func(cfg *Config) {
cfg.Groups["default"].MemberLimits = &LimitsConfig{RequestsPerMinute: 1}
})
tok := fx.token(t, nil)
fx.chat(t, tok)
if rec := fx.chat(t, tok); rec.Code != http.StatusTooManyRequests {
t.Fatalf("second request status %d, want 429", rec.Code)
}
has := func() bool {
for _, r := range fx.gw.limiter.rejections.snapshot() {
if r.kind == "user" && r.id == "oidc:[email protected]" {
return true
}
}
return false
}
if !has() {
t.Fatal("rejection not recorded for the virtual user")
}
fx.gw.pruneRejectionsAfterReload()
if !has() {
t.Fatal("a users-file reload must not prune a live virtual user's rejections")
}
}
func newRegistryFixture(t *testing.T) (*oidcRegistry, *respClient, *fakeClock) {
t.Helper()
ln := newBehavioralRedisServer(t)
client, err := buildRedisClient(&RedisConfig{Address: ln.Addr().String()})
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = client.Close() })
clock := newFakeClock()
r := newOIDCRegistry(client, clock.Now, func(f func()) { f() }, func(string, ...any) {}, 4*oidcDefaultMaxUsers)
return r, client, clock
}
func TestRegistry_PersistAndLoad(t *testing.T) {
r, client, clock := newRegistryFixture(t)
r.touch("oidc:[email protected]", "[email protected]", []string{"default"})
recs, err := r.load()
if err != nil {
t.Fatal(err)
}
want := oidcRegistryRecord{Identity: "[email protected]", Groups: []string{"default"}, LastSeen: clock.Now().Unix()}
if got := recs["oidc:[email protected]"]; !reflect.DeepEqual(got, want) {
t.Fatalf("record = %+v, want %+v", got, want)
}
marker := `{"identity":"[email protected]","groups":["marker"],"lastSeen":` + itoa(clock.Now().Unix()) + `}`
if _, err = client.do("HSET", oidcRegistryKey, "oidc:[email protected]", marker); err != nil {
t.Fatal(err)
}
clock.Advance(30 * time.Second)
r.touch("oidc:[email protected]", "[email protected]", []string{"default"})
recs, err = r.load()
if err != nil {
t.Fatal(err)
}
if got := recs["oidc:[email protected]"]; !reflect.DeepEqual(got.Groups, []string{"marker"}) {
t.Fatalf("a touch inside 60s must not write: %+v", got)
}
clock.Advance(31 * time.Second)
r.touch("oidc:[email protected]", "[email protected]", []string{"default"})
clock.Advance(3 * time.Second)
recs, err = r.load()
if err != nil {
t.Fatal(err)
}
if got := recs["oidc:[email protected]"]; !reflect.DeepEqual(got.Groups, []string{"default"}) {
t.Fatalf("a touch after 60s must write: %+v", got)
}
}
func TestSnapshot_RegistryAcrossReplicas(t *testing.T) {
ln := newBehavioralRedisServer(t)
withRedis := func(cfg *Config) { cfg.Redis = &RedisConfig{Address: ln.Addr().String()} }
a := newOIDCGateway(t, withRedis)
b := newOIDCGateway(t, func(cfg *Config) {
withRedis(cfg)
cfg.OIDC.Issuer = a.cfg.OIDC.Issuer
})
if rec := a.chat(t, a.token(t, nil)); rec.Code != http.StatusOK {
t.Fatalf("status %d", rec.Code)
}
users, _ := b.gw.auth.snapshot()
u, ok := findUserSummary(users, "oidc:[email protected]")
if !ok || u.source != "oidc" || u.limits.RequestsPerMinute != 50 {
t.Fatalf("replica B must list the user from the registry: %+v (found %v)", u, ok)
}
}
func TestSnapshot_RegistryHidesStaleAndUnknownGroups(t *testing.T) {
ln := newBehavioralRedisServer(t)
fx := newOIDCGateway(t, func(cfg *Config) { cfg.Redis = &RedisConfig{Address: ln.Addr().String()} })
now := fx.gw.auth.nowFn().Unix()
stale := now - int64(oidcRegistryRetention/time.Second) - 1
for name, rec := range map[string]string{
"oidc:[email protected]": `{"identity":"[email protected]","groups":["default"],"lastSeen":` + itoa(stale) + `}`,
"oidc:[email protected]": `{"identity":"[email protected]","groups":["removed"],"lastSeen":` + itoa(now) + `}`,
"oidc:[email protected]": `{"identity":"[email protected]","groups":["default"],"lastSeen":` + itoa(now) + `}`,
} {
if _, err := fx.gw.redisClient.do("HSET", oidcRegistryKey, name, rec); err != nil {
t.Fatal(err)
}
}
users, _ := fx.gw.auth.snapshot()
if _, ok := findUserSummary(users, "oidc:[email protected]"); !ok {
t.Fatal("current record missing")
}
for _, hidden := range []string{"oidc:[email protected]", "oidc:[email protected]"} {
if _, ok := findUserSummary(users, hidden); ok {
t.Fatalf("%s must be hidden", hidden)
}
}
}
func TestSnapshot_NoRedis(t *testing.T) {
fx := newOIDCGateway(t, nil)
if fx.gw.auth.oidcRegistry != nil {
t.Fatal("no registry without redis")
}
fx.chat(t, fx.token(t, nil))
users, _ := fx.gw.auth.snapshot()
if _, ok := findUserSummary(users, "oidc:[email protected]"); !ok {
t.Fatal("in-memory virtual user missing")
}
}
func itoa(n int64) string { return strconv.FormatInt(n, 10) }
func TestSnapshot_RegistryHidesRenamedOverride(t *testing.T) {
ln := newBehavioralRedisServer(t)
fx := newOIDCGateway(t, func(cfg *Config) { cfg.Redis = &RedisConfig{Address: ln.Addr().String()} })
now := fx.gw.auth.nowFn().Unix()
put := func(name, identity string) {
t.Helper()
rec := `{"identity":"` + identity + `","groups":["default"],"lastSeen":` + itoa(now) + `}`
if _, err := fx.gw.redisClient.do("HSET", oidcRegistryKey, name, rec); err != nil {
t.Fatal(err)
}
}
put("alice-old", "[email protected]")
put("oidc:[email protected]", "[email protected]")
put("bob-old", "[email protected]")
if err := fx.gw.auth.replaceFileUsers([]*UserConfig{{Name: "alice-new", OIDCIdentity: "[email protected]"}}); err != nil {
t.Fatal(err)
}
put("alice-new", "[email protected]")
reg := fx.gw.auth.oidcRegistry
reg.mu.Lock()
reg.cached = nil
reg.mu.Unlock()
users, _ := fx.gw.auth.snapshot()
for name, want := range map[string]bool{
"alice-old": false,
"alice-new": true,
"oidc:[email protected]": true,
"bob-old": false,
} {
if _, ok := findUserSummary(users, name); ok != want {
t.Errorf("%s listed = %v, want %v", name, ok, want)
}
}
}