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
@@ -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
|
||||
}
|
||||
@@ -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
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user