Audit log: every change through the admin API and the administration pages, with ctl audit

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5.5 committed 2026-10-04 16:25:56 +02:00
1 parent bcd2413535
commit f8fcecd640
18 files changed
+681 -7

No files matched your search

+152
View File
@@ -0,0 +1,152 @@
// Package audit records who changed what: every change made through the
// admin API and the administration pages, written to the store with the
// request's fields — anything secret taken out — and how it ended.
package audit
import (
"bytes"
"context"
"encoding/json"
"io"
"log/slog"
"net/http"
"net/url"
"regexp"
"sort"
"strings"
"time"
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
)
// Sink is where events go: the store.
type Sink interface {
WriteAudit(ctx context.Context, e store.AuditEvent) error
}
// secretName is a field whose value is never recorded.
// The password form's fields are current and new.
var secretName = regexp.MustCompile(`(?i)(pass|secret|token|api_?key|^key$|dsn|private|credential|csrf|cookie|^current$|^new$|^confirm)`)
// Redact is a request's fields with secrets replaced, as JSON.
func Redact(fields map[string]any) string {
clean := redactValue(fields)
b, _ := json.Marshal(clean)
if len(b) > 8192 {
b = append(b[:8192], []byte("…")...)
}
return string(b)
}
func redactValue(v any) any {
switch t := v.(type) {
case map[string]any:
out := make(map[string]any, len(t))
for k, val := range t {
if secretName.MatchString(k) {
out[k] = "***"
continue
}
out[k] = redactValue(val)
}
return out
case []any:
out := make([]any, len(t))
for i, val := range t {
out[i] = redactValue(val)
}
return out
case string:
// A secret pasted where a name was expected.
if strings.HasPrefix(t, "sk-") || strings.Contains(t, "PRIVATE KEY") {
return "***"
}
return t
}
return v
}
// FormFields is a form as fields, for Redact.
func FormFields(v url.Values) map[string]any {
out := map[string]any{}
keys := make([]string, 0, len(v))
for k := range v {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
if len(v[k]) == 1 {
out[k] = v[k][0]
} else {
out[k] = v[k]
}
}
return out
}
// statusWriter keeps the status a handler answered with.
type statusWriter struct {
http.ResponseWriter
status int
}
func (w *statusWriter) WriteHeader(code int) {
if w.status == 0 {
w.status = code
}
w.ResponseWriter.WriteHeader(code)
}
func (w *statusWriter) Write(b []byte) (int, error) {
if w.status == 0 {
w.status = http.StatusOK
}
return w.ResponseWriter.Write(b)
}
func (w *statusWriter) Flush() {
if f, ok := w.ResponseWriter.(http.Flusher); ok {
f.Flush()
}
}
// API records every change to the admin API: a request that is not a GET or
// HEAD, under /api/. actor says who made it.
func API(sink Sink, actor func(*http.Request) string, log *slog.Logger, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet || r.Method == http.MethodHead || !strings.HasPrefix(r.URL.Path, "/api/") {
next.ServeHTTP(w, r)
return
}
var body []byte
if r.Body != nil {
body, _ = io.ReadAll(io.LimitReader(r.Body, 64<<10))
r.Body = io.NopCloser(bytes.NewReader(body))
}
sw := &statusWriter{ResponseWriter: w}
next.ServeHTTP(sw, r)
e := store.AuditEvent{At: time.Now(), Actor: actor(r), Source: "api",
Action: r.Method + " " + r.URL.Path, Status: sw.status}
var fields map[string]any
if json.Unmarshal(body, &fields) == nil {
e.Detail = Redact(fields)
}
for _, k := range []string{"name", "target", "scope", "model"} {
if s, ok := fields[k].(string); ok && e.Target == "" {
e.Target = s
}
}
Write(r.Context(), sink, log, e)
})
}
// Write records an event, saying so in the log when the store will not take
// it: a change that happened and is not in the audit is worth a warning.
func Write(ctx context.Context, sink Sink, log *slog.Logger, e store.AuditEvent) {
if sink == nil {
return
}
if err := sink.WriteAudit(context.WithoutCancel(ctx), e); err != nil && log != nil {
log.Warn("a change could not be written to the audit log", "action", e.Action, "actor", e.Actor, "err", err)
}
}
+60
View File
@@ -0,0 +1,60 @@
package audit
import (
"context"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
)
func TestNothingSecretIsRecorded(t *testing.T) {
got := Redact(FormFields(url.Values{
"current": {"old-pw"}, "new": {"new-pw"}, "password": {"pw"}, "csrf": {"tok"},
"name": {"ok-name"}, "note": {"sk-proj-abcdefgh"},
}))
for _, secret := range []string{"old-pw", "new-pw", `"pw"`, "tok", "sk-proj"} {
if strings.Contains(got, secret) {
t.Errorf("%s recorded: %s", secret, got)
}
}
if !strings.Contains(got, "ok-name") {
t.Errorf("an ordinary field was dropped: %s", got)
}
nested := Redact(map[string]any{"external": map[string]any{"api_key": "x1", "url": "https://p"}})
if strings.Contains(nested, "x1") || !strings.Contains(nested, "https://p") {
t.Errorf("nested: %s", nested)
}
}
type memSink struct{ got []store.AuditEvent }
func (m *memSink) WriteAudit(_ context.Context, e store.AuditEvent) error {
m.got = append(m.got, e)
return nil
}
func TestEveryChangeToTheAdminAPIIsRecorded(t *testing.T) {
sink := &memSink{}
h := API(sink, func(*http.Request) string { return "ilker" }, nil, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusCreated)
}))
for _, req := range []*http.Request{
httptest.NewRequest("GET", "/api/v1/models", nil),
httptest.NewRequest("POST", "/api/v1/models", strings.NewReader(`{"name":"m1","external":{"api_key":"sk-1"}}`)),
httptest.NewRequest("DELETE", "/api/v1/nodes/n1", nil),
} {
h.ServeHTTP(httptest.NewRecorder(), req)
}
if len(sink.got) != 2 {
t.Fatalf("recorded %d: %+v", len(sink.got), sink.got)
}
e := sink.got[0]
if e.Actor != "ilker" || e.Action != "POST /api/v1/models" || e.Target != "m1" || e.Status != 201 ||
strings.Contains(e.Detail, "sk-1") || e.Source != "api" {
t.Errorf("%+v", e)
}
}
+67
View File
@@ -0,0 +1,67 @@
package cli
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
"github.com/spf13/cobra"
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
)
func newAuditCmd(client func() *ctlClient, asJSON *bool) *cobra.Command {
var (
since, actor, contains string
limit int
detail bool
)
c := &cobra.Command{
Use: "audit",
Short: "Who changed what: every change through the admin API and the administration pages",
Long: "Each change made with `llmgw ctl`, the console, or the pages under /admin —\n" +
"and a person's own keys and password — is recorded: who, when, through\n" +
"what, the fields it was given with anything secret taken out, and how it\n" +
"ended. The newest first.",
Example: " llmgw ctl audit --since 24h\n llmgw ctl audit --actor ilker --contains models\n llmgw ctl audit --contains bob@example.com --detail",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
q := url.Values{"since": {since}, "limit": {strconv.Itoa(limit)}}
if actor != "" {
q.Set("actor", actor)
}
if contains != "" {
q.Set("contains", contains)
}
var ev []store.AuditEvent
if err := client().get(cmd.Context(), "/api/v1/audit?"+q.Encode(), &ev); err != nil {
return err
}
if *asJSON {
return json.NewEncoder(cmd.OutOrStdout()).Encode(ev)
}
if len(ev) == 0 {
fmt.Fprintln(cmd.OutOrStdout(), "No changes recorded in that time.")
return nil
}
w := tab()
fmt.Fprintln(w, "WHEN (UTC)\tWHO\tVIA\tWHAT\tTARGET\tSTATUS")
for _, e := range ev {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%d\n", e.At.UTC().Format("2006-01-02 15:04:05"),
e.Actor, e.Source, e.Action, dash(e.Target), e.Status)
if detail && e.Detail != "" && e.Detail != "{}" {
fmt.Fprintf(w, "\t\t\t %s\t\t\n", e.Detail)
}
}
return w.Flush()
},
}
f := c.Flags()
f.StringVar(&since, "since", "7d", "how far back: 90m, 24h, 30d")
f.StringVar(&actor, "actor", "", "only changes by this person")
f.StringVar(&contains, "contains", "", "only changes whose action or target contains this")
f.IntVar(&limit, "limit", 200, "at most this many")
f.BoolVar(&detail, "detail", false, "show the fields each change was given")
return c
}
+1
View File
@@ -161,6 +161,7 @@ func newCtlCmd() *cobra.Command {
newCapacityCmd(client, &asJSON),
newQuotaCmd(client, &asJSON),
newGuardCmd(client, &asJSON),
newAuditCmd(client, &asJSON),
newUsageCmd(client, &asJSON),
newCertCmd(),
)
+13 -1
View File
@@ -31,6 +31,7 @@ import (
"golang.org/x/sync/errgroup"
llmgwv1 "gitea.mynodes.xyz/ilker/llmgw/api/gen/llmgw/v1"
"gitea.mynodes.xyz/ilker/llmgw/internal/audit"
"gitea.mynodes.xyz/ilker/llmgw/internal/config"
"gitea.mynodes.xyz/ilker/llmgw/internal/control"
"gitea.mynodes.xyz/ilker/llmgw/internal/control/adminapi"
@@ -269,6 +270,7 @@ func newServerCmd() *cobra.Command {
if st := rt.Store(); st != nil {
// Removing a node revokes its certificate.
api.SetRevoker(st.RevokeNode)
api.SetAudit(st)
}
// Catalog changes need somewhere to persist, so this is wired
// only when there is a store. Without one, the endpoints say so
@@ -404,7 +406,7 @@ func newServerCmd() *cobra.Command {
return err
}
adminSrv := withLimits(&http.Server{
Handler: router.AdminGuard(router.AdminAuth(adminToken, cfg.DebugPprof, adminMux)),
Handler: router.AdminGuard(router.AdminAuth(adminToken, cfg.DebugPprof, auditAPI(rt, adminMux, log))),
ReadTimeout: time.Minute,
})
@@ -660,6 +662,16 @@ func startControlPlane(ctx context.Context, cfg config.Server, cat *catalog.Live
return ctl, svc, nil
}
// auditAPI records every change made through the admin API, when there is a
// store to record it in.
func auditAPI(rt *router.Router, next http.Handler, log *slog.Logger) http.Handler {
st := rt.Store()
if st == nil {
return next
}
return audit.API(st, adminapi.Actor, log, next)
}
// metricsAddr renders the metrics listener for the startup line, saying so
// plainly when there is none rather than printing an empty string.
func metricsAddr(ln net.Listener) string {
+7
View File
@@ -48,6 +48,8 @@ type API struct {
serve *modelserve.Server
// history is the Prometheus the console's charts read from.
history History
// audit reads the audit log.
audit AuditReader
// guard is the guardrail policies' library and attachments.
guard *GuardAdmin
// revoke records a removed node, so its certificate is refused.
@@ -80,6 +82,7 @@ func (a *API) Register(mux *http.ServeMux) {
mux.HandleFunc("POST /api/v1/models/{name}/resources", a.setResources)
a.registerQuotas(mux)
a.registerGuard(mux)
mux.HandleFunc("GET /api/v1/audit", a.auditEvents)
mux.HandleFunc("POST /api/v1/models/{name}/adapters", a.setAdapters)
mux.HandleFunc("GET /api/v1/models/{name}/target", a.target)
mux.HandleFunc("POST /api/v1/models/{name}/class", func(w http.ResponseWriter, r *http.Request) {
@@ -1635,6 +1638,10 @@ func (a *API) planModel(w http.ResponseWriter, r *http.Request) {
// the admin API answers only this machine and trusts whoever reaches it. Kept
// to a short printable name, so a claim cannot pass for more than one — no
// line breaks to forge a second log line, no length to bury the rest.
// Actor is who an admin API request says it comes from: the person who ran
// the command holding the admin token, as the CLI reports it.
func Actor(r *http.Request) string { return actorOf(r) }
func actorOf(r *http.Request) string {
claimed := r.Header.Get("X-Llmgw-Actor")
var b strings.Builder
+77
View File
@@ -0,0 +1,77 @@
package adminapi
import (
"context"
"net/http"
"strconv"
"time"
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
)
// AuditReader lists recorded changes.
type AuditReader interface {
AuditEvents(ctx context.Context, f store.AuditFilter) ([]store.AuditEvent, error)
}
// SetAudit enables GET /api/v1/audit.
func (a *API) SetAudit(r AuditReader) { a.audit = r }
// audit answers GET /api/v1/audit?since=24h&actor=&contains=&limit=.
func (a *API) auditEvents(w http.ResponseWriter, r *http.Request) {
if a.audit == nil {
writeError(w, http.StatusNotImplemented, "this control plane keeps no audit log: it has no store")
return
}
q := r.URL.Query()
since := 7 * 24 * time.Hour
if s := q.Get("since"); s != "" {
d, err := parseSince(s)
if err != nil {
writeError(w, http.StatusBadRequest, err.Error())
return
}
since = d
}
limit, _ := strconv.Atoi(q.Get("limit"))
ev, err := a.audit.AuditEvents(r.Context(), store.AuditFilter{
Since: time.Now().Add(-since), Actor: q.Get("actor"), Contains: q.Get("contains"), Limit: limit,
})
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
if ev == nil {
ev = []store.AuditEvent{}
}
writeJSON(w, http.StatusOK, ev)
}
// parseSince reads 90m, 24h, 7d.
func parseSince(s string) (time.Duration, error) {
if n, ok := cutSuffix(s, "d"); ok {
days, err := strconv.Atoi(n)
if err != nil || days < 0 {
return 0, errBadSince(s)
}
return time.Duration(days) * 24 * time.Hour, nil
}
d, err := time.ParseDuration(s)
if err != nil || d < 0 {
return 0, errBadSince(s)
}
return d, nil
}
type errBadSince string
func (e errBadSince) Error() string {
return "since " + string(e) + " is not a duration like 90m, 24h or 7d"
}
func cutSuffix(s, suf string) (string, bool) {
if len(s) > len(suf) && s[len(s)-len(suf):] == suf {
return s[:len(s)-len(suf)], true
}
return "", false
}
+25
View File
@@ -4,8 +4,10 @@ import (
"context"
"slices"
"testing"
"time"
"gitea.mynodes.xyz/ilker/llmgw/internal/guard"
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
)
type memGuard struct{ att []guard.Attachment }
@@ -57,3 +59,26 @@ func TestGuardPoliciesAreAttachedByTarget(t *testing.T) {
t.Errorf("detach again: %d", code)
}
}
type memAudit struct{ f store.AuditFilter }
func (m *memAudit) AuditEvents(_ context.Context, f store.AuditFilter) ([]store.AuditEvent, error) {
m.f = f
return []store.AuditEvent{{Actor: "ilker", Action: "POST /api/v1/models"}}, nil
}
func TestTheAuditLogIsReadWithFilters(t *testing.T) {
a, _ := setup(t, testModels())
m := &memAudit{}
a.SetAudit(m)
var ev []store.AuditEvent
if code := call(t, a, "GET", "/api/v1/audit?since=2d&actor=ilker&contains=models&limit=5", "", &ev); code != 200 || len(ev) != 1 {
t.Fatalf("%d %v", code, ev)
}
if m.f.Actor != "ilker" || m.f.Contains != "models" || m.f.Limit != 5 || time.Since(m.f.Since) < 47*time.Hour {
t.Errorf("filter %+v", m.f)
}
if code := call(t, a, "GET", "/api/v1/audit?since=soon", "", nil); code != 400 {
t.Errorf("a bad since: %d", code)
}
}
+3
View File
@@ -180,6 +180,9 @@ func (r *Router) Handler() http.Handler {
pages.SetPersonalOff(r.personalOff)
pages.SetLlamaCppVersion(r.llamaVersion)
pages.SetPersonalDefaults(r.personalDefaults)
if r.store != nil {
pages.SetAudit(r.store)
}
if pol, ok := r.guard.(interface {
Policy(guard.Subject) (guard.Policy, string)
}); ok {
+72
View File
@@ -0,0 +1,72 @@
package store
import (
"context"
"database/sql"
"strings"
"time"
)
// AuditEvent is one change: who made it, through what, to what, and how it
// ended. Detail never holds a secret; the writer takes them out.
type AuditEvent struct {
ID int64 `json:"id"`
At time.Time `json:"at"`
Actor string `json:"actor"`
Source string `json:"source"` // api, web
Action string `json:"action"` // "POST /api/v1/models", "POST /admin/users/{id}/suspend"
Target string `json:"target,omitempty"`
Detail string `json:"detail,omitempty"`
Status int `json:"status"`
}
// WriteAudit records one change.
func (s *DB) WriteAudit(ctx context.Context, e AuditEvent) error {
if e.At.IsZero() {
e.At = time.Now()
}
_, err := s.db.ExecContext(ctx, `INSERT INTO audit_events (ts, actor, source, action, target, detail, status)
VALUES (?, ?, ?, ?, ?, ?, ?)`, millis(e.At), e.Actor, e.Source, e.Action, e.Target, e.Detail, e.Status)
return err
}
// AuditFilter narrows a listing; empty fields match everything.
type AuditFilter struct {
Since, Until time.Time
Actor string
// Contains matches the action or the target.
Contains string
Limit int
}
// AuditEvents lists changes, the newest first.
func (s *DB) AuditEvents(ctx context.Context, f AuditFilter) ([]AuditEvent, error) {
q := `SELECT id, ts, actor, source, action, target, detail, status FROM audit_events WHERE ts >= ?`
args := []any{millis(f.Since)}
if !f.Until.IsZero() {
q += ` AND ts < ?`
args = append(args, millis(f.Until))
}
if f.Actor != "" {
q += ` AND actor = ?`
args = append(args, f.Actor)
}
if f.Contains != "" {
q += ` AND (LOWER(action) LIKE ? OR LOWER(target) LIKE ?)`
like := "%" + strings.ToLower(f.Contains) + "%"
args = append(args, like, like)
}
limit := f.Limit
if limit <= 0 || limit > 10000 {
limit = 200
}
q += ` ORDER BY ts DESC, id DESC LIMIT ?`
args = append(args, limit)
return queryAll(ctx, s.db, q, args, func(rows *sql.Rows) (AuditEvent, error) {
var e AuditEvent
var ts int64
err := rows.Scan(&e.ID, &ts, &e.Actor, &e.Source, &e.Action, &e.Target, &e.Detail, &e.Status)
e.At = fromMillis(ts)
return e, err
})
}
+34
View File
@@ -0,0 +1,34 @@
package store
import (
"testing"
"time"
)
func TestAuditEventsAreKeptAndFound(t *testing.T) {
db := capacityStore(t)
ctx := t.Context()
now := time.Now()
for i, e := range []AuditEvent{
{At: now.Add(-2 * time.Hour), Actor: "ilker", Source: "api", Action: "POST /api/v1/models", Target: "m1", Status: 200},
{At: now.Add(-time.Hour), Actor: "ann@example.com", Source: "web", Action: "POST /admin/users/u1/suspend", Target: "bob@example.com", Status: 200},
{At: now, Actor: "ilker", Source: "api", Action: "DELETE /api/v1/nodes/n1", Status: 409},
} {
if err := db.WriteAudit(ctx, e); err != nil {
t.Fatal(i, err)
}
}
all, err := db.AuditEvents(ctx, AuditFilter{Since: now.Add(-24 * time.Hour)})
if err != nil || len(all) != 3 || all[0].Action != "DELETE /api/v1/nodes/n1" {
t.Fatalf("%+v %v", all, err)
}
if got, _ := db.AuditEvents(ctx, AuditFilter{Since: now.Add(-24 * time.Hour), Actor: "ilker"}); len(got) != 2 {
t.Errorf("by actor: %d", len(got))
}
if got, _ := db.AuditEvents(ctx, AuditFilter{Since: now.Add(-24 * time.Hour), Contains: "BOB@"}); len(got) != 1 {
t.Errorf("by target: %d", len(got))
}
if got, _ := db.AuditEvents(ctx, AuditFilter{Since: now.Add(-90 * time.Minute)}); len(got) != 2 {
t.Errorf("since: %d", len(got))
}
}
+3 -1
View File
@@ -55,6 +55,8 @@ var copyOrder = []string{
// Or a removed node's certificate would work again in the new store.
"revoked_nodes",
"guard_attachments",
// Who changed what is not lost by moving the store.
"audit_events",
}
// notCopied are tables deliberately left behind.
@@ -227,7 +229,7 @@ func (s *DB) resetIdentities(ctx context.Context) error {
return nil
}
// Only the tables with a generated id. SQLite's rowid needs nothing.
for _, t := range []string{"memory_notes", "usage"} {
for _, t := range []string{"memory_notes", "usage", "audit_events"} {
var has bool
if err := s.db.QueryRowContext(ctx,
`SELECT EXISTS (SELECT 1 FROM information_schema.columns
@@ -0,0 +1,15 @@
-- Ported from the SQLite migration of the same number.
-- Who changed what, from where: every change through the admin API and the
-- administration pages. Detail is the request's fields with anything secret
-- taken out; status is how it ended.
CREATE TABLE audit_events (
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
ts BIGINT NOT NULL,
actor TEXT NOT NULL,
source TEXT NOT NULL,
action TEXT NOT NULL,
target TEXT NOT NULL DEFAULT '',
detail TEXT NOT NULL DEFAULT '',
status INTEGER NOT NULL DEFAULT 0
);
CREATE INDEX audit_events_ts ON audit_events (ts);
@@ -0,0 +1,14 @@
-- Who changed what, from where: every change through the admin API and the
-- administration pages. Detail is the request's fields with anything secret
-- taken out; status is how it ended.
CREATE TABLE audit_events (
id INTEGER PRIMARY KEY AUTOINCREMENT,
ts INTEGER NOT NULL,
actor TEXT NOT NULL,
source TEXT NOT NULL,
action TEXT NOT NULL,
target TEXT NOT NULL DEFAULT '',
detail TEXT NOT NULL DEFAULT '',
status INTEGER NOT NULL DEFAULT 0
);
CREATE INDEX audit_events_ts ON audit_events (ts);
+7 -4
View File
@@ -14,6 +14,7 @@ import (
"time"
"gitea.mynodes.xyz/ilker/llmgw/internal/account"
"gitea.mynodes.xyz/ilker/llmgw/internal/audit"
"gitea.mynodes.xyz/ilker/llmgw/internal/clientip"
"gitea.mynodes.xyz/ilker/llmgw/internal/guard"
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
@@ -65,6 +66,8 @@ type AccountPages struct {
hasMirror bool
// hasQuotas is set when /admin/quotas is registered.
hasQuotas bool
// audit records every change made through these pages; nil records none.
audit audit.Sink
// hasGuard is set when /admin/guard is registered.
hasGuard bool
// guardFor names the guardrail policies in force for someone, and
@@ -150,10 +153,10 @@ func (p *AccountPages) Register(mux *http.ServeMux) {
mux.HandleFunc("POST /login", rationed(newRation(30, 30), p.login))
mux.HandleFunc("POST /logout", p.logout)
mux.HandleFunc("GET /profile", p.profile)
mux.HandleFunc("POST /profile/keys", p.createKey)
mux.HandleFunc("POST /profile/keys/{id}/revoke", p.revokeKey)
mux.HandleFunc("POST /profile/password", p.changePassword)
mux.HandleFunc("POST /profile/connect", p.connect)
mux.HandleFunc("POST /profile/keys", p.selfAudited(p.createKey))
mux.HandleFunc("POST /profile/keys/{id}/revoke", p.selfAudited(p.revokeKey))
mux.HandleFunc("POST /profile/password", p.selfAudited(p.changePassword))
mux.HandleFunc("POST /profile/connect", p.selfAudited(p.connect))
mux.HandleFunc("GET /setup/{tool}", p.setupScript)
}
+41
View File
@@ -90,9 +90,22 @@ func (f *fakeAdmin) AllKeys(context.Context) ([]store.KeyRow, error)
func (f *fakeAdmin) RevokeKey(context.Context, string, time.Time) error { return f.err }
func tenancyPage(t *testing.T, s *fakeAdmin, admin bool) *http.ServeMux {
t.Helper()
return tenancyPageAs(t, s, admin, nil)
}
func tenancyPageWith(t *testing.T, s *fakeAdmin, with func(*AccountPages)) *http.ServeMux {
t.Helper()
return tenancyPageAs(t, s, true, with)
}
func tenancyPageAs(t *testing.T, s *fakeAdmin, admin bool, with func(*AccountPages)) *http.ServeMux {
t.Helper()
a := fakeAccounts{user: store.User{ID: "u1", Email: "a@example.com", IsAdmin: admin}}
p := NewAccountPages(a, nil, nil, "v0", slog.New(slog.NewTextHandler(io.Discard, nil)))
if with != nil {
with(p)
}
mux := http.NewServeMux()
p.RegisterAdminUsers(mux, s)
return mux
@@ -199,3 +212,31 @@ func TestThisPageSetsNoQuotas(t *testing.T) {
}
}
}
type auditMem struct{ got []store.AuditEvent }
func (a *auditMem) WriteAudit(_ context.Context, e store.AuditEvent) error {
a.got = append(a.got, e)
return nil
}
func TestAChangeOnTheAdminPagesIsAudited(t *testing.T) {
s := &fakeAdmin{}
am := &auditMem{}
mux := tenancyPageWith(t, s, func(p *AccountPages) { p.SetAudit(am) })
mirrorPost(t, mux, "/admin/tenancy/org", url.Values{"csrf": {"tok"}, "name": {"Scania"}})
s.err = store.ErrDuplicateName
mirrorPost(t, mux, "/admin/tenancy/org", url.Values{"csrf": {"tok"}, "name": {"Scania"}})
if len(am.got) != 2 {
t.Fatalf("recorded %d", len(am.got))
}
ok, refused := am.got[0], am.got[1]
if ok.Source != "web" || ok.Action != "POST /admin/tenancy/org" || ok.Actor == "" ||
!strings.Contains(ok.Detail, `"name":"Scania"`) || !strings.Contains(ok.Detail, "created") ||
strings.Contains(ok.Detail, "tok") {
t.Errorf("%+v", ok)
}
if !strings.Contains(refused.Detail, "refused") {
t.Errorf("a refusal reads as one: %+v", refused)
}
}
+5 -1
View File
@@ -420,11 +420,13 @@ func (p *AccountPages) tenancyAction(s AccountAdmin,
err = plainError(err)
p.log.Warn("a tenancy change was refused", "admin", admin.Email,
"path", r.URL.Path, "err", err)
noteAudit(r, "", "", err.Error())
p.renderUsers(w, r, s, admin, "", err.Error(), r.FormValue("open"))
return
}
p.log.Info("the tenancy tree was changed", "admin", admin.Email,
"path", r.URL.Path, "what", msg)
noteAudit(r, "", msg, "")
p.renderUsers(w, r, s, admin, msg, "", r.FormValue("open"))
})
}
@@ -445,7 +447,7 @@ func (p *AccountPages) adminOnly(
http.NotFound(w, r)
return
}
next(w, p.withNav(r, user), user)
p.audited(w, p.withNav(r, user), user, func(w http.ResponseWriter, r *http.Request) { next(w, r, user) })
}
}
@@ -474,11 +476,13 @@ func (p *AccountPages) adminAction(s AccountAdmin,
// action is as worth knowing about as one that went through.
p.log.Warn("an administrative action was refused",
"admin", admin.Email, "subject", subject, "err", err)
noteAudit(r, subject, "", err.Error())
p.renderUsers(w, r, s, admin, "", subject+": "+err.Error(), r.FormValue("open"))
return
}
p.log.Info("an account was changed",
"admin", admin.Email, "subject", subject, "what", msg)
noteAudit(r, subject, msg, "")
p.renderUsers(w, r, s, admin, subject+" "+msg, "", r.FormValue("open"))
})
}
+85
View File
@@ -0,0 +1,85 @@
package web
import (
"context"
"net/http"
"time"
"gitea.mynodes.xyz/ilker/llmgw/internal/audit"
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
)
// SetAudit has every change made through these pages recorded.
func (p *AccountPages) SetAudit(s audit.Sink) { p.audit = s }
// auditNote is what a handler says about the change it made: whom it was
// about, and how it ended in words — pages answer 200 to a refusal too.
type auditNote struct{ subject, result, refused string }
type auditNoteKey struct{}
// noteAudit lets the handler of a change say whom it concerned and how it
// ended.
func noteAudit(r *http.Request, subject, result, refused string) {
if n, ok := r.Context().Value(auditNoteKey{}).(*auditNote); ok {
n.subject, n.result, n.refused = subject, result, refused
}
}
type statusRecorder struct {
http.ResponseWriter
status int
}
func (w *statusRecorder) WriteHeader(code int) {
if w.status == 0 {
w.status = code
}
w.ResponseWriter.WriteHeader(code)
}
func (w *statusRecorder) Write(b []byte) (int, error) {
if w.status == 0 {
w.status = http.StatusOK
}
return w.ResponseWriter.Write(b)
}
// audited runs a change and records it: who, which page, the form with its
// secrets taken out, and how it ended.
func (p *AccountPages) audited(w http.ResponseWriter, r *http.Request, actor store.User,
run func(w http.ResponseWriter, r *http.Request),
) {
if p.audit == nil || r.Method == http.MethodGet || r.Method == http.MethodHead {
run(w, r)
return
}
note := &auditNote{}
r = r.WithContext(context.WithValue(r.Context(), auditNoteKey{}, note))
sw := &statusRecorder{ResponseWriter: w}
run(sw, r)
fields := audit.FormFields(r.PostForm)
if note.result != "" {
fields["result"] = note.result
}
if note.refused != "" {
fields["refused"] = note.refused
}
audit.Write(r.Context(), p.audit, p.log, store.AuditEvent{
At: time.Now(), Actor: actor.Email, Source: "web", Action: r.Method + " " + r.URL.Path,
Target: note.subject, Detail: audit.Redact(fields), Status: sw.status,
})
}
// selfAudited records a change a person makes to their own account: a key
// made or revoked, a password changed.
func (p *AccountPages) selfAudited(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
_, user, ok := p.session(r)
if !ok {
h(w, r)
return
}
p.audited(w, r, user, h)
}
}