Documentation
¶
Overview ¶
Package audit provides audit log entries and store interface.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Entry ¶
type Entry struct {
ID id.ID `json:"id"`
Action string `json:"action"`
Resource string `json:"resource"`
Key string `json:"key"`
AppID string `json:"app_id"`
TenantID string `json:"tenant_id,omitempty"`
UserID string `json:"user_id,omitempty"`
IP string `json:"ip,omitempty"`
Outcome string `json:"outcome"`
Metadata map[string]any `json:"metadata,omitempty"`
CreatedAt time.Time `json:"created_at"`
}
Entry represents a single audit log entry.
type ListOpts ¶
type ListOpts struct {
Limit int
Offset int
// Resource restricts the result to entries whose Resource matches
// exactly. Empty means every resource.
Resource string
// Key restricts the result to entries whose Key matches exactly. Empty
// means every key. ListAuditByKey takes its key as an argument and
// ignores this field.
Key string
// Action restricts the result to entries whose Action matches exactly.
// Empty means every action.
Action string
// Outcome restricts the result to entries whose Outcome matches exactly
// ("success" or "failure"). Empty means every outcome.
Outcome string
// Since keeps only entries created at or after it (created_at >= Since).
// The zero time means no lower bound.
Since time.Time
// ExcludeActions drops entries whose Action is any of these. Empty means
// nothing is excluded.
ExcludeActions []string
}
ListOpts configures list queries for audit entries.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger records audit entries and optionally broadcasts them via an audit hook.
func NewLogger ¶
func NewLogger(store Store, opts ...LoggerOption) *Logger
NewLogger creates an audit logger.
type LoggerOption ¶
type LoggerOption func(*Logger)
LoggerOption configures the Logger.
func WithHook ¶
func WithHook(h *audithook.Extension) LoggerOption
WithHook attaches an audit hook extension for event broadcasting.
func WithLogger ¶
func WithLogger(sl log.Logger) LoggerOption
WithLogger sets the slog logger for internal errors.
type Store ¶
type Store interface {
// RecordAudit persists an audit log entry.
RecordAudit(ctx context.Context, e *Entry) error
// ListAudit returns audit entries for an app that match opts, newest
// first, filtered before paging.
ListAudit(ctx context.Context, appID string, opts ListOpts) ([]*Entry, error)
// ListAuditByKey returns audit entries for a specific key within an app.
// The key argument decides the key and opts.Key is ignored; every other
// filter in opts applies as it does for ListAudit.
ListAuditByKey(ctx context.Context, key, appID string, opts ListOpts) ([]*Entry, error)
// CountAudit returns the total number of audit entries for an app,
// independent of any paging.
CountAudit(ctx context.Context, appID string) (int64, error)
// CountAuditMatching returns the number of audit entries for an app that
// match opts. Limit and Offset are ignored, so the count is the total a
// paged list would page through.
CountAuditMatching(ctx context.Context, appID string, opts ListOpts) (int64, error)
}
Store defines the persistence interface for audit log entries.
Every list and count honours the same ListOpts filters (Resource, Key, Action, Outcome, Since, ExcludeActions) in the query itself, never after the fact, so a page and its total always describe the same rows. Lists are ordered created_at DESC, id DESC: the id breaks ties between entries with the same timestamp, so paging never repeats or skips a row.