Repository navigation
Description
Problem
A caller emitting an audit event needs to know what delivery was attempted and when it failed. Today AuditEvent.LogTo returns no error and calls slog.Logger.LogAttrs; NewAuditLogger wraps the supplied writer in a standard JSON handler. Errors returned by a writer/handler are not returned through the slog logging call.
Lines 177 to 221 in 5ba2b36
| // LogTo logs the audit event to the provided slog.Logger using the custom audit level. | |
| func (e *AuditEvent) LogTo(ctx context.Context, logger *slog.Logger, level slog.Level) { | |
| // Create slog attributes for the audit event | |
| attrs := []slog.Attr{ | |
| slog.String("audit_id", e.Metadata.AuditID), | |
| slog.String("type", e.Type), | |
| slog.Time("logged_at", e.LoggedAt), | |
| slog.String("outcome", e.Outcome), | |
| slog.String("component", e.Component), | |
| slog.Group("source", | |
| slog.String("type", e.Source.Type), | |
| slog.String("value", e.Source.Value), | |
| slog.Any("extra", e.Source.Extra), | |
| ), | |
| slog.Any("subjects", e.Subjects), | |
| } | |
| // Add target if present | |
| if e.Target != nil { | |
| attrs = append(attrs, slog.Any("target", e.Target)) | |
| } | |
| // Add metadata extra if present | |
| if e.Metadata.Extra != nil { | |
| attrs = append(attrs, slog.Group("metadata", slog.Any("extra", e.Metadata.Extra))) | |
| } | |
| // Add data if present | |
| if e.Data != nil { | |
| attrs = append(attrs, slog.Any("data", e.Data)) | |
| } | |
| // Add delegation chain if present and non-zero. The IsZero gate plus the | |
| // field's omitempty tag together guarantee that, when no chain is set, | |
| // the log output is identical to before this field existed (no "delegation" | |
| // key is emitted). The chain resolves through [DelegationChain.LogValue], | |
| // which mirrors its JSON shape, so the slog and JSON wire forms of the same | |
| // event are structurally identical and no handler — JSON, text, or custom — | |
| // ever sees the unexported extra claims (PII guard). | |
| if !e.DelegationChain.IsZero() { | |
| attrs = append(attrs, slog.Any("delegation", e.DelegationChain)) | |
| } | |
| // Log with the specified level | |
| logger.LogAttrs(ctx, level, "audit_event", attrs...) |
https://github.com/stacklok/toolhive-core/blob/5ba2b36edf8051b47bab103aad93223cb3dcd0b3/audit/logger.go
The open sink work in #326 adds useful stdout/HTTP fan-out and returns writer errors. This issue complements that work: a writer error still needs to reach a consumer when delivery happens through the documented AuditEvent.LogTo -> slog -> writer composition.
Requested enhancement
Provide a small opt-in way for consumers to observe audit delivery failures through the real logging path. This could be a caller-visible emitter result or an injected failure-reporting mechanism; choose the smallest API that meets the contract while preserving existing logging behavior.
Specify:
- What success means for each path: accepted by a writer, accepted by a remote endpoint, or durably persisted. Do not describe an HTTP success response or stdout write as durable storage without a corresponding receiver contract.
- How consumers observe individual sink failures and partial fan-out without preventing attempts to other sinks.
- How failures are reported without recursively logging into the failing audit sink. Expose safe categories and sink identifiers; do not disclose authorization headers, credential-bearing URLs, raw event payloads, or unrestricted remote error bodies.
- How any applicable flush/close failures reach the owner and how concurrent emitters are supported.
- How callers choose their operational response. A service may alert, buffer through another component, reject selected protected actions, or explicitly accept degraded behavior. The library should not impose a universal fail-closed policy.
Do not turn this into a mandatory durable spool, retry system, SIEM implementation, or a replacement for the sink abstraction being added in #326. A failed send may already have reached its destination, so automatic replay and exactly-once guarantees are outside this issue.
Acceptance criteria
- Integration tests emit an actual
AuditEventthrough the public logger/emitter composition, then read the configured destination. Testing only the raw writer return value is insufficient. - Inject write failure, short write, HTTP non-success/timeout where the sink package is available, and partial fan-out. The consumer receives the documented failure signal and healthy sinks still receive the event.
- A failure notification reaches a separate test-owned receiver even when the audit destination is unavailable, without recursive emission or leaked secrets.
- Cover concurrent owners and applicable shutdown/resource-release failures.
- Package documentation distinguishes schema support, delivery attempts, receiver acknowledgment, and durable retention. Replace blanket compliance wording with the specific NIST-oriented capabilities and consumer responsibilities.
Why this matters
NIST SP 800-53 AU-5 calls for a defined response to audit-logging failures; AU-4, AU-9, and AU-11 also need deployment decisions about capacity, protection, and retention. An interoperable event schema cannot meet those operational responsibilities alone.
The goal is to let consuming projects move toward those controls without requiring every control, a particular backend, or a fixed retention period: https://csrc.nist.gov/pubs/sp/800/53/r5/upd1/final
One motivating consumer is stacklok/mecatl#2121.
Activity