mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-12-06 11:03:52 +00:00
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.
233 lines
6.5 KiB
Go
233 lines
6.5 KiB
Go
package traefikllmgateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
// oidcRegistryKey is the Redis hash of OIDC virtual users seen by any
|
|
// replica: field = accounting name, value = oidcRegistryRecord JSON.
|
|
oidcRegistryKey = "llmgw:oidc:users"
|
|
// oidcTouchEvery throttles registry writes per user.
|
|
oidcTouchEvery = time.Minute
|
|
// oidcRegistryReadTTL bounds how often snapshot reads the hash.
|
|
oidcRegistryReadTTL = 2 * time.Second
|
|
// oidcRegistryRetention hides, then deletes, users not seen for longer
|
|
// than the monthly usage window.
|
|
oidcRegistryRetention = 32 * 24 * time.Hour
|
|
)
|
|
|
|
// oidcRegistryRecord is one virtual user as stored in the registry.
|
|
type oidcRegistryRecord struct {
|
|
Identity string `json:"identity"`
|
|
Groups []string `json:"groups"`
|
|
LastSeen int64 `json:"lastSeen"`
|
|
}
|
|
|
|
// oidcRegistry records OIDC virtual users in Redis, so every replica and
|
|
// every restart lists the same users in the admin dashboard and metrics.
|
|
type oidcRegistry struct {
|
|
cachedAt time.Time
|
|
client *respClient
|
|
now func() time.Time
|
|
spawn func(func())
|
|
logf func(string, ...any)
|
|
touched map[string]time.Time
|
|
cached map[string]oidcRegistryRecord
|
|
logGate logGate
|
|
// maxTouched bounds the per-replica write throttle map.
|
|
maxTouched int
|
|
mu sync.Mutex
|
|
}
|
|
|
|
func newOIDCRegistry(client *respClient, now func() time.Time, spawn func(func()), logf func(string, ...any), maxTouched int) *oidcRegistry {
|
|
return &oidcRegistry{client: client, now: now, spawn: spawn, logf: logf, touched: map[string]time.Time{}, maxTouched: maxTouched}
|
|
}
|
|
|
|
// touch records that name was seen now, at most once per oidcTouchEvery.
|
|
// The write runs through spawn and never fails a request.
|
|
func (r *oidcRegistry) touch(name, identity string, groups []string) {
|
|
now := r.now()
|
|
r.mu.Lock()
|
|
if last, ok := r.touched[name]; ok && now.Sub(last) < oidcTouchEvery {
|
|
r.mu.Unlock()
|
|
return
|
|
}
|
|
if len(r.touched) >= r.maxTouched {
|
|
r.touched = map[string]time.Time{}
|
|
}
|
|
r.touched[name] = now
|
|
r.mu.Unlock()
|
|
|
|
rec, err := json.Marshal(oidcRegistryRecord{Identity: identity, Groups: groups, LastSeen: now.Unix()})
|
|
if err != nil {
|
|
r.logError("encode", err)
|
|
return
|
|
}
|
|
r.spawn(func() {
|
|
if _, err := r.client.do("HSET", oidcRegistryKey, name, string(rec)); err != nil {
|
|
r.logError("write", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
// load returns every registry record, re-reading Redis at most once per
|
|
// oidcRegistryReadTTL. On a read error it returns the last good records.
|
|
func (r *oidcRegistry) load() (map[string]oidcRegistryRecord, error) {
|
|
now := r.now()
|
|
r.mu.Lock()
|
|
if r.cached != nil && now.Sub(r.cachedAt) < oidcRegistryReadTTL {
|
|
defer r.mu.Unlock()
|
|
return r.cached, nil
|
|
}
|
|
r.mu.Unlock()
|
|
|
|
reply, err := r.client.do("HGETALL", oidcRegistryKey)
|
|
if err == nil {
|
|
if rerr, ok := reply.(error); ok {
|
|
err = rerr
|
|
}
|
|
}
|
|
if err != nil {
|
|
r.logError("read", err)
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
return r.cached, err
|
|
}
|
|
recs := decodeRegistry(reply)
|
|
r.prune(recs, now)
|
|
r.mu.Lock()
|
|
r.cached, r.cachedAt = recs, now
|
|
r.mu.Unlock()
|
|
return recs, nil
|
|
}
|
|
|
|
// prune drops records past oidcRegistryRetention from recs and deletes them
|
|
// from Redis, so the hash stops growing.
|
|
func (r *oidcRegistry) prune(recs map[string]oidcRegistryRecord, now time.Time) {
|
|
cutoff := now.Add(-oidcRegistryRetention).Unix()
|
|
args := []string{"HDEL", oidcRegistryKey}
|
|
for name, rec := range recs {
|
|
if rec.LastSeen < cutoff {
|
|
args = append(args, name)
|
|
delete(recs, name)
|
|
}
|
|
}
|
|
if len(args) == 2 {
|
|
return
|
|
}
|
|
r.spawn(func() {
|
|
if _, err := r.client.do(args...); err != nil {
|
|
r.logError("prune", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
// decodeRegistry reads a RESP2 HGETALL reply (a flat field/value array).
|
|
// A malformed record is skipped.
|
|
func decodeRegistry(reply any) map[string]oidcRegistryRecord {
|
|
arr, _ := reply.([]any)
|
|
out := make(map[string]oidcRegistryRecord, len(arr)/2)
|
|
for i := 0; i+1 < len(arr); i += 2 {
|
|
name, ok1 := arr[i].([]byte)
|
|
val, ok2 := arr[i+1].([]byte)
|
|
if !ok1 || !ok2 {
|
|
continue
|
|
}
|
|
var rec oidcRegistryRecord
|
|
if err := json.Unmarshal(val, &rec); err != nil {
|
|
continue
|
|
}
|
|
out[string(name)] = rec
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (r *oidcRegistry) logError(op string, err error) {
|
|
if log, suppressed := r.logGate.shouldLog(r.now()); log {
|
|
// Yaegi v0.16.1 crashes on a variadic func field called with more
|
|
// than one variadic argument (limiter.logf, limits.go).
|
|
r.logf("%s", fmt.Sprintf("oidc registry %s failed: %v%s", op, err, suppressedSuffix(suppressed)))
|
|
}
|
|
}
|
|
|
|
// oidcSummaries lists the OIDC virtual users for snapshot: the ones in
|
|
// memory, then registry records for users this replica has not seen.
|
|
// Registry users whose groups are all gone, whose last request is older
|
|
// than oidcRegistryRetention, or whose stored name is not the name their
|
|
// identity resolves to now, are hidden.
|
|
func (a *authStore) oidcSummaries(skip map[string]bool) []userSummary {
|
|
if a.oidcAuth == nil {
|
|
return nil
|
|
}
|
|
var out []userSummary
|
|
for _, e := range a.oidcAuth.users() {
|
|
if skip[e.user.name] {
|
|
continue
|
|
}
|
|
skip[e.user.name] = true
|
|
out = append(out, virtualSummary(e.user))
|
|
}
|
|
if a.oidcRegistry == nil {
|
|
return out
|
|
}
|
|
recs, err := a.oidcRegistry.load()
|
|
if err != nil && recs == nil {
|
|
return out
|
|
}
|
|
a.mu.RLock()
|
|
overrides := a.oidcOverrides
|
|
a.mu.RUnlock()
|
|
cutoff := a.nowFn().Add(-oidcRegistryRetention).Unix()
|
|
for name, rec := range recs {
|
|
if skip[name] || rec.LastSeen < cutoff || name != resolvedOIDCName(overrides[rec.Identity], rec.Identity) {
|
|
continue
|
|
}
|
|
var members []*group
|
|
var names []string
|
|
for _, gn := range rec.Groups {
|
|
if g, ok := a.groups[gn]; ok {
|
|
members = append(members, g)
|
|
names = append(names, gn)
|
|
}
|
|
}
|
|
if len(members) == 0 {
|
|
continue
|
|
}
|
|
var own *LimitsConfig
|
|
if ov := overrides[rec.Identity]; ov != nil {
|
|
own = ov.limits
|
|
}
|
|
limits, sources := effectiveUserLimits(own, members)
|
|
skip[name] = true
|
|
out = append(out, virtualSummary(&user{
|
|
limits: limits, limitSources: sources, name: name,
|
|
groupName: names[0], groupNames: names, oidcIdentity: rec.Identity,
|
|
}))
|
|
}
|
|
return out
|
|
}
|
|
|
|
// resolvedOIDCName is the accounting name an identity gets now: its
|
|
// override's name, else the default prefixed name.
|
|
func resolvedOIDCName(ov *oidcOverride, identity string) string {
|
|
if ov != nil {
|
|
return ov.name
|
|
}
|
|
return oidcNamePrefix + identity
|
|
}
|
|
|
|
func virtualSummary(u *user) userSummary {
|
|
return userSummary{
|
|
limits: u.limits,
|
|
limitSources: u.limitSources,
|
|
name: u.name,
|
|
groupName: u.groupName,
|
|
groups: cloneStringSlice(u.groupNames),
|
|
source: "oidc",
|
|
}
|
|
}
|