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:
1 parent
bcd2413535
commit
f8fcecd640
18 files changed
+681
-7
No files matched your search
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user