Documentation
¶
Overview ¶
Package audit provides engine-agnostic audit event emission using CloudEvents as the envelope format. It defines the Auditor contract and ships MVP backends (noop, stdout, log, kafka, multi) plus a Kratos middleware that intercepts RPC calls and emits structured audit events.
Architecture ¶
The central abstraction is the Auditor interface (auditor.go):
type Auditor interface {
Emit(ctx context.Context, event cloudevents.Event) error
}
Implementations live in sub-packages:
- obs/audit/noop — discards all events (testing / disabled mode)
- obs/audit/stdout — JSON-encodes events to stdout (local dev)
- obs/audit/log — emits structured slog records (local dev / demos)
- obs/audit/kafka — delivers events to Kafka via franz-go and CloudEvents binding
- obs/audit/multi — fans out to multiple auditors
Middleware ¶
The Middleware function (audit_middleware.go) intercepts RPC calls, looks up generated AuditRules by operation, builds servora.audit.rpc.v1 CloudEvents events, and emits through the configured Auditor. Emission errors are logged but never block business logic.
Recommended middleware chain order:
recovery → tracing → logging → ratelimit → validate → metrics → audit.Middleware → authn → authz → handler
CloudEvents Attributes ¶
NewEvent sets CloudEvents required attributes and uses source="//app-name". The generic RPC audit middleware sets subject to the transport operation. Extensions are producer-owned: NewEvent adds traceparent/tracestate when a sampled OTel span is present, the RPC middleware adds errormessage for handler errors, authz emits authid, and backend-specific fields such as partitionkey stay private to their backend package.
Index ¶
- func Collector(auditor Auditor, opts ...Option) middleware.Middleware
- func Middleware(auditor Auditor, opts ...Option) middleware.Middleware
- func NewEvent(ctx context.Context, opts ...EventOption) cloudevents.Event
- func SetProtoData(e *cloudevents.Event, msg proto.Message) error
- type Auditor
- type Closer
- type EventOption
- type Flusher
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Collector ¶ added in v0.4.4
func Collector(auditor Auditor, opts ...Option) middleware.Middleware
Collector is the ChainBuilder-facing alias for the audit middleware.
func Middleware ¶ added in v0.5.0
func Middleware(auditor Auditor, opts ...Option) middleware.Middleware
Middleware returns a Kratos middleware that intercepts RPC calls, constructs a generic RPC audit event when the operation's rule is ENABLED, and emits it through the given Auditor. Audit emission errors are logged but never block business logic.
func NewEvent ¶ added in v0.5.0
func NewEvent(ctx context.Context, opts ...EventOption) cloudevents.Event
NewEvent constructs a CloudEvents event populated with Servora audit defaults. It extracts the app name from context (if available) and sets it as the source in the format "//name", falling back to "//unknown". Options are applied after defaults, allowing full override.
func SetProtoData ¶ added in v0.5.0
func SetProtoData(e *cloudevents.Event, msg proto.Message) error
SetProtoData marshals a protobuf message and sets it as the CloudEvents data payload with content type "application/protobuf" and dataschema set to the fully qualified protobuf type URL.
Types ¶
type Auditor ¶ added in v0.5.0
type Auditor interface {
Emit(ctx context.Context, event cloudevents.Event) error
}
Auditor is the engine-agnostic interface for emitting structured audit events as CloudEvents. Unlike the legacy Emitter (which takes proto AuditEvent), Auditor works directly with the CloudEvents envelope — enabling decoupled, transport-neutral audit pipelines.
Implementations may batch, buffer, or fan-out events as needed.
type Closer ¶ added in v0.5.0
type Closer interface {
Close() error
}
Closer is an optional interface that Auditor implementations may satisfy to release resources on shutdown.
type EventOption ¶ added in v0.5.0
type EventOption func(*cloudevents.Event)
EventOption configures a CloudEvents event during construction.
func WithSource ¶ added in v0.5.0
func WithSource(s string) EventOption
WithSource sets the CloudEvents source attribute.
func WithSubject ¶ added in v0.5.0
func WithSubject(s string) EventOption
WithSubject sets the CloudEvents subject attribute.
func WithType ¶ added in v0.5.0
func WithType(t string) EventOption
WithType sets the CloudEvents type attribute.
type Flusher ¶ added in v0.5.0
Flusher is an optional interface that Auditor implementations may satisfy to flush buffered events before graceful shutdown.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package kafka provides a franz-go backed Auditor and CloudEvents Kafka binding helpers.
|
Package kafka provides a franz-go backed Auditor and CloudEvents Kafka binding helpers. |
|
Package log provides an Auditor that emits CloudEvents through slog.
|
Package log provides an Auditor that emits CloudEvents through slog. |
|
Package multi provides an Auditor that fans out events to multiple backends.
|
Package multi provides an Auditor that fans out events to multiple backends. |
|
Package noop provides a no-op Auditor that discards all events silently.
|
Package noop provides a no-op Auditor that discards all events silently. |
|
Package stdout provides an Auditor that JSON-encodes CloudEvents to stdout.
|
Package stdout provides an Auditor that JSON-encodes CloudEvents to stdout. |