Skip to content

feat(audit): add a pluggable audit sink writer - #326

Closed
reyortiz3 wants to merge 1 commit into
mainfrom
feat/audit-sink-writer
Closed

reyortiz3 wants to merge 1 commit into
mainfrom
feat/audit-sink-writer

Conversation

@reyortiz3

@reyortiz3 reyortiz3 commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds audit/sink, a small package that builds the io.WriteCloser an audit *slog.Logger writes to. It fans each JSON audit line out to any combination of stdout and an HTTP webhook, so a component that owns its audit logger can ship events to a SIEM without an external log shipper.

  • sink.NewWriter(Config) returns a fan-out writer. An empty Config is stdout-only, so adopting the package changes nothing until a sink is configured.
  • The fan-out writes to every sink unconditionally and returns the first error, unlike io.MultiWriter, which stops at the first failing writer. A flaky HTTP endpoint therefore never costs the stdout record.
  • The HTTP sink POSTs each line unmodified with optional headers and a bounded timeout (default 5s). Delivery is synchronous and best-effort: no queue, no retry.
  • Construction errors (unknown type, HTTP sink without a URL) are returned to the caller.

It sits beside audit.AuditEvent and audit.NewAuditEvent, which it is meant to carry, and adds no dependencies.

Why here

Several Stacklok components emit audit.AuditEvent and each hardcodes os.Stdout for the logger. A shared sink avoids each of them growing its own forwarder. A Kafka sink was left out on purpose to avoid a new dependency in this module; the Entry type leaves room to add one later.

Verification

  • task lint (golangci-lint, go vet): 0 issues
  • task license-check: clean
  • go test -race -cover ./audit/sink/: pass, 90.7% coverage

Notes for reviewers

CLAUDE.md asks to discuss new packages first. This one was requested explicitly as part of Stacklok's audit standardization work, so please flag if audit/sink is the wrong home.

🤖 Generated with Claude Code

@JAORMX

JAORMX commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

So, I am not convinced we should add direct HTTP delivery here, or that the transport belongs in toolhive-core. We already accept an io.Writer in NewAuditLogger, and ToolHive supports file output as well as stdout. The new functionality here is the webhook and fan-out, rather than support for non-stdout audit logs.

My concern is what happens when delivery fails. This is synchronous, best-effort HTTP with no buffering or retry, and when used through our current slog logger, the errors returned by the writer are discarded. We could lose the remote record without reporting that failure, while a slow endpoint can block the logging path for the configured timeout. That does not give us the audit-delivery guarantees I would want.

I would much rather have a bounded local handoff to another process or sidecar, with backpressure, or publish to a message queue with explicit delivery guarantees. A memory-backed file alone would not provide that: we need to define when the write is acknowledged and what happens if the consumer stops or crashes. A blocking pipe/socket can provide backpressure; durable delivery needs an acknowledgement after persistence or a durable spool.

I would keep core focused on the event schema and emission contract, including making audit failures observable, and let a dedicated component own remote delivery and credentials. We can revisit shared transport code if multiple consumers actually need the same implementation.

Can we step back from the HTTP sink and agree on the consumer requirement and delivery guarantees first? What needs to be true before we consider an audit record accepted: buffered locally, persisted locally, or acknowledged by a broker?

@JAORMX

JAORMX commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

Also, a few statements in the description and code comments need correcting:

  • The Config.Sinks comment calls stdout-only "the pre-existing behavior of every audit emitter in the platform." That is not correct. ToolHive already selects a file or stdout through GetLogWriter, and both its request and workflow auditors use that writer. Core also already accepts any io.Writer. The rationale should distinguish any specific hardcoded consumers from the capabilities we already have.
  • "Construction errors (unknown type, HTTP sink without a URL) are returned to the caller." The missing-URL part does not match the implementation. newSinkWriter checks whether HTTP config is nil, but newHTTPWriter does not validate URL. An HTTPConfig with an empty URL is accepted at construction and fails when writing.
  • "A flaky HTTP endpoint therefore never costs the stdout record" is stronger than the implementation guarantees. Fan-out continues after an error, which is useful, but it is sequential. If HTTP comes first, stdout waits for that attempt to finish, potentially until the timeout. A process exit during that wait can still leave stdout without the record. The accurate guarantee is that a returned error from one sink does not skip the remaining sinks.

And the stdout default only preserves behavior for consumers that already use stdout. Replacing an existing file writer with NewWriter(Config{}) would change its destination.

@JAORMX JAORMX left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting changes pending agreement on the delivery contract and where this functionality belongs. I do not want us to merge the direct HTTP sink as proposed.

We already support arbitrary writers in core and file output in ToolHive. Before adding another transport to core, we need a concrete consumer requirement and an agreed failure policy. My preference is a bounded local handoff to a dedicated delivery process, or a message queue with explicit acknowledgement and durability guarantees.

There is also a concrete failure-handling problem with the intended slog integration: errors returned by this writer are discarded by slog.Logger. Failed remote delivery would not be surfaced through the existing audit emission API. We need an observable failure path and defined backpressure/acknowledgement semantics before relying on this for audit delivery.

Please also address the inaccuracies called out in my earlier comments: platform-wide stdout-only behavior, missing-URL validation at construction, and the unconditional stdout-delivery claim.

Discussion: #326 (comment)
Corrections: #326 (comment)

@reyortiz3
reyortiz3 marked this pull request as draft October 14, 2026 13:29
@reyortiz3

Copy link
Copy Markdown
Contributor Author

Thanks for reviewing @JAORMX 🙏 I'm marking it as draft since haven't had time to go through this in depth, the main thing is trying to standardize the audit logs which there are different implementations/approaches, so trying to standardize it here in toolhive-core to be re-usable across components

@reyortiz3 reyortiz3 closed this Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants