Documentation
¶
Overview ¶
Package audit provides audit event storage and streaming functionality.
Index ¶
- Constants
- func RequestID(ctx context.Context) string
- type Actor
- type Context
- type Emitter
- type Event
- type LogEmitter
- type MemoryStore
- func (s *MemoryStore) Acknowledge(ctx context.Context, sequence int64) error
- func (s *MemoryStore) GetByActor(ctx context.Context, actorID uuid.UUID, limit int) ([]*Event, error)
- func (s *MemoryStore) GetByResource(ctx context.Context, resourceType string, resourceID uuid.UUID, limit int) ([]*Event, error)
- func (s *MemoryStore) GetBySequence(ctx context.Context, fromSequence int64, limit int) ([]*Event, error)
- func (s *MemoryStore) GetByTimeRange(ctx context.Context, from, to time.Time, limit int) ([]*Event, error)
- func (s *MemoryStore) GetLastAcknowledged(ctx context.Context) (int64, error)
- func (s *MemoryStore) GetLastSequence(ctx context.Context) (int64, error)
- func (s *MemoryStore) Record(ctx context.Context, event *Event) (int64, error)
- type NopEmitter
- type Resource
- type Store
- type StreamConfig
Constants ¶
const ( OutcomeSuccess = "success" OutcomeFailure = "failure" )
Outcomes of an event.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Actor ¶
type Actor struct {
ID uuid.UUID `json:"id"`
Type string `json:"type"` // "human" | "application" | "agent" | "service"
Identifier string `json:"identifier"`
}
Actor represents the actor in an audit event.
type Context ¶
type Context struct {
TenantID *uuid.UUID `json:"tenant_id,omitempty"`
SessionID string `json:"session_id,omitempty"`
ClientIP string `json:"client_ip,omitempty"`
UserAgent string `json:"user_agent,omitempty"`
// RequestID is the ID of the HTTP request that caused the event, as
// the router's request-ID middleware assigned it.
RequestID string `json:"request_id,omitempty"`
}
Context represents the context of an audit event.
type Emitter ¶
type Emitter interface {
// Emit records an audit event.
Emit(ctx context.Context, event *Event) error
}
Emitter defines the interface for emitting audit events.
type Event ¶
type Event struct {
ID uuid.UUID `json:"id"`
Sequence int64 `json:"sequence"`
Timestamp time.Time `json:"timestamp"`
EventType string `json:"event_type"`
Action string `json:"action"`
Actor Actor `json:"actor"`
Resource Resource `json:"resource"`
Context Context `json:"context"`
Outcome string `json:"outcome"` // "success" | "failure"
Details map[string]any `json:"details,omitempty"`
}
Event represents a standardized audit event.
type LogEmitter ¶
type LogEmitter struct {
// contains filtered or unexported fields
}
LogEmitter writes every event as one structured log record, a security event log for deployments without an audit store. Failures are logged at warning level, successes at info.
func NewLogEmitter ¶
func NewLogEmitter(logger *slog.Logger) *LogEmitter
NewLogEmitter returns a LogEmitter writing to logger, or to slog.Default when logger is nil.
type MemoryStore ¶
type MemoryStore struct {
// contains filtered or unexported fields
}
MemoryStore is an in-memory implementation of the audit Store interface. This is suitable for development and testing but should not be used in production.
func NewMemoryStore ¶
func NewMemoryStore(maxEvents int) *MemoryStore
NewMemoryStore creates a new in-memory audit store.
func (*MemoryStore) Acknowledge ¶
func (s *MemoryStore) Acknowledge(ctx context.Context, sequence int64) error
Acknowledge marks events up to a sequence as acknowledged.
func (*MemoryStore) GetByActor ¶
func (s *MemoryStore) GetByActor(ctx context.Context, actorID uuid.UUID, limit int) ([]*Event, error)
GetByActor retrieves events for a specific actor.
func (*MemoryStore) GetByResource ¶
func (s *MemoryStore) GetByResource(ctx context.Context, resourceType string, resourceID uuid.UUID, limit int) ([]*Event, error)
GetByResource retrieves events for a specific resource.
func (*MemoryStore) GetBySequence ¶
func (s *MemoryStore) GetBySequence(ctx context.Context, fromSequence int64, limit int) ([]*Event, error)
GetBySequence retrieves events starting from a sequence number.
func (*MemoryStore) GetByTimeRange ¶
func (s *MemoryStore) GetByTimeRange(ctx context.Context, from, to time.Time, limit int) ([]*Event, error)
GetByTimeRange retrieves events within a time range.
func (*MemoryStore) GetLastAcknowledged ¶
func (s *MemoryStore) GetLastAcknowledged(ctx context.Context) (int64, error)
GetLastAcknowledged returns the last acknowledged sequence number.
func (*MemoryStore) GetLastSequence ¶
func (s *MemoryStore) GetLastSequence(ctx context.Context) (int64, error)
GetLastSequence returns the last recorded sequence number.
type NopEmitter ¶
type NopEmitter struct{}
NopEmitter discards every event. It is the default where an emitter is optional.
type Resource ¶
type Resource struct {
Type string `json:"type"`
ID uuid.UUID `json:"id"`
Identifier string `json:"identifier,omitempty"`
}
Resource represents the resource in an audit event.
type Store ¶
type Store interface {
// Record stores an audit event and returns its sequence number.
Record(ctx context.Context, event *Event) (int64, error)
// GetBySequence retrieves events starting from a sequence number.
GetBySequence(ctx context.Context, fromSequence int64, limit int) ([]*Event, error)
// GetByTimeRange retrieves events within a time range.
GetByTimeRange(ctx context.Context, from, to time.Time, limit int) ([]*Event, error)
// GetByActor retrieves events for a specific actor.
GetByActor(ctx context.Context, actorID uuid.UUID, limit int) ([]*Event, error)
// GetByResource retrieves events for a specific resource.
GetByResource(ctx context.Context, resourceType string, resourceID uuid.UUID, limit int) ([]*Event, error)
// GetLastSequence returns the last recorded sequence number.
GetLastSequence(ctx context.Context) (int64, error)
// Acknowledge marks events up to a sequence as acknowledged.
Acknowledge(ctx context.Context, sequence int64) error
// GetLastAcknowledged returns the last acknowledged sequence number.
GetLastAcknowledged(ctx context.Context) (int64, error)
}
Store defines the interface for audit event storage.