feat(api): record an audit log and show it to organizations, users and admins

Every state-changing API route is mapped to an action; a test fails for
unmapped routes. Successful changes are recorded with actor, API token,
target, IP, user agent and request ID; failed sign-ins too. Names are
taken from the response, never secrets. Organizations read their log with
audit.read under Settings, users their account activity on the security
page, admins the platform log. Entries expire after UPTIME_RETENTION_AUDIT.

Closes #65

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-22 09:59:52 +01:00
1 parent 789ef077eb
commit 358b191976
18 files changed
+1184 -76

No files matched your search

+8 -3
View File
@@ -47,7 +47,7 @@ func (s *Server) RunHousekeeping(ctx context.Context) {
// tokens and incidents older than the daily aggregate.
func (s *Server) Housekeep(ctx context.Context, now time.Time) error {
keep := s.cfg.Retention
var counts [4]int64
var counts [5]int64
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
var err error
if keep.Housekeeping > 0 {
@@ -62,15 +62,20 @@ func (s *Server) Housekeep(ctx context.Context, now time.Time) error {
return err
}
}
if keep.Audit > 0 {
if counts[4], err = q.PurgeAuditEvents(ctx, now.Add(-keep.Audit)); err != nil {
return err
}
}
if keep.Daily > 0 {
before := now.Add(-keep.Daily)
counts[3], err = q.PurgeIncidents(ctx, &before)
}
return err
})
if err == nil && counts != [4]int64{} {
if err == nil && counts != [5]int64{} {
s.log.InfoContext(ctx, "housekeeping purged rows",
"notifications", counts[0], "invitations", counts[1], "api_tokens", counts[2], "incidents", counts[3])
"notifications", counts[0], "invitations", counts[1], "api_tokens", counts[2], "incidents", counts[3], "audit_events", counts[4])
}
return err
}