2026-07-20 10:48:05 +02:00
|
|
|
package db
|
|
|
|
|
|
2026-07-23 17:03:52 +02:00
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"log/slog"
|
|
|
|
|
)
|
2026-07-20 10:48:05 +02:00
|
|
|
|
|
|
|
|
// Auditor is the minimal audit-write surface WriteAudit needs. *DB satisfies
|
|
|
|
|
// it directly, and the service layer's Store interface does too, so every
|
|
|
|
|
// caller — api, admin, ws, service — can route its audit writes through this
|
|
|
|
|
// one helper regardless of whether it holds a *DB or a narrower interface.
|
|
|
|
|
type Auditor interface {
|
2026-07-23 17:03:52 +02:00
|
|
|
LogAudit(ctx context.Context, actorID int64, action, targetType string, targetID int64, detail string) error
|
2026-07-20 10:48:05 +02:00
|
|
|
}
|
|
|
|
|
|
2026-07-31 15:41:57 +02:00
|
|
|
// AsyncAuditor is the optional asynchronous fast path for WriteAudit. An
|
|
|
|
|
// Auditor that also implements it — in practice *DB, once main.go installs
|
|
|
|
|
// an AuditWriter via SetAuditWriter — can take the entry off the request
|
|
|
|
|
// path. EnqueueAudit reports true when it took responsibility for the entry
|
|
|
|
|
// (the background writer may still drop it under load, but never silently —
|
|
|
|
|
// see AuditWriter.Enqueue), and false when no writer is installed, in which
|
|
|
|
|
// case WriteAudit performs the synchronous best-effort write below. The
|
|
|
|
|
// token CLI and tests never install a writer, so they stay synchronous.
|
|
|
|
|
type AsyncAuditor interface {
|
|
|
|
|
EnqueueAudit(actorID int64, action, targetType string, targetID int64, detail string) bool
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-20 10:48:05 +02:00
|
|
|
// WriteAudit records an audit entry best-effort.
|
|
|
|
|
//
|
|
|
|
|
// Per the D8 policy decision (docs/plans/audit-2026-07-19-decisions.md), audit
|
|
|
|
|
// writes stay best-effort: a LogAudit failure must never fail or abort the
|
|
|
|
|
// caller's request. But a failed write must never be silently discarded
|
|
|
|
|
// either — this helper logs it with the actor/action/target context so the
|
|
|
|
|
// gap is visible in the logs. The detail string is intentionally not logged;
|
|
|
|
|
// it can carry request-specific or sensitive text and the structured fields
|
|
|
|
|
// already identify what was attempted.
|
2026-07-23 17:03:52 +02:00
|
|
|
func WriteAudit(ctx context.Context, a Auditor, actorID int64, action, targetType string, targetID int64, detail string) {
|
2026-07-31 15:41:57 +02:00
|
|
|
if aa, ok := a.(AsyncAuditor); ok && aa.EnqueueAudit(actorID, action, targetType, targetID, detail) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-07-23 17:03:52 +02:00
|
|
|
if err := a.LogAudit(ctx, actorID, action, targetType, targetID, detail); err != nil {
|
2026-07-20 10:48:05 +02:00
|
|
|
slog.Error("audit log write failed",
|
|
|
|
|
"action", action,
|
|
|
|
|
"actor_id", actorID,
|
|
|
|
|
"target_type", targetType,
|
|
|
|
|
"target_id", targetID,
|
|
|
|
|
"error", err,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|