audit

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package audit is the platform's ONE audit writer: every service writes its own audit.<module>__* table through this package instead of hand-rolling INSERT statements per service. Record must be called inside the SAME transaction as the state change it documents (atomicity) — it takes a pgx.Tx, never a pool.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Denial

func Denial(ctx context.Context, pool *pgxpool.Pool, w *Writer, actor, action, subject, reason string)

Denial best-effort records an authorization denial in its own transaction (denials are audited too). reason is the payload's `reason`: authz's own decision reason for a confirmed denial (403), or "AUTHORIZATION_UNAVAILABLE" when no definite answer was reached (503). A failure to write the denial row must not mask the response the caller already gets — it's logged nowhere here (the services have no logger wired in); the httpx.Recover middleware and access log still cover operational visibility.

Types

type Event

type Event struct {
	Actor         string
	Action        string
	Subject       string
	Payload       map[string]any
	CorrelationID string
}

Event is one audit record. Actor MUST be derived server-side from the validated auth context — never from a request body. Payload is marshaled to JSON as-is; nil becomes an empty object. CorrelationID left empty is filled from ctx (the id httpx.Correlation stored there), so a handler that passes its request context through to Record gets the row joined to its request for free.

type Tx

type Tx interface {
	Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)
}

Tx is the subset of pgx.Tx that Record needs (structurally satisfied by *pgx.Tx and by sqlc's generated DBTX-backed transactions — no adapter required).

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer records events to one fixed, validated audit table.

func NewWriter

func NewWriter(table string) (*Writer, error)

NewWriter builds a Writer for the given fully-qualified audit table name (e.g. "audit.registry__events"). It returns an error if the name doesn't match the audit.<module>__<name> convention.

func (*Writer) Record

func (w *Writer) Record(ctx context.Context, tx Tx, ev Event) error

Record writes ev to the writer's table using tx — the caller's transaction, so the audit row commits or rolls back atomically with the state change it documents. An empty ev.CorrelationID is taken from ctx (see Event).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL