Help us shape the future of Go! Take the 2026 Go Developer Survey

audit

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package audit writes and reads the audit log.

Index

Constants

This section is empty.

Variables

View Source
var ActorFromContext = func(ctx context.Context) (Actor, bool) { return Actor{}, false }

ActorFromContext resolves the acting principal. The auth package (later in the boot order, so audit cannot import it) replaces this at init.

View Source
var SystemActor = Actor{Kind: "system", ID: "", Name: "system"}

SystemActor is recorded when no actor is found in the context.

Functions

func IPFromContext

func IPFromContext(ctx context.Context) string

IPFromContext returns the client IP stored by WithIP, or "".

func Migrations

func Migrations() fs.FS

Migrations returns the audit_log migrations ("migrations/NNNN_x.sql").

func WithIP

func WithIP(ctx context.Context, ip string) context.Context

WithIP stores the client IP for Record (set by HTTP middleware).

Types

type Actor

type Actor struct{ Kind, ID, Name string }

Actor is who performed an action. Kinds: user, api_key, system, extension.

type Entry

type Entry struct {
	ID         string          `json:"id"`
	At         string          `json:"at"`
	Actor      Actor           `json:"actor"`
	Action     string          `json:"action"`
	TargetKind string          `json:"target_kind"`
	TargetID   string          `json:"target_id"`
	IP         string          `json:"ip"`
	Meta       json.RawMessage `json:"meta"`
}

Entry is one audit_log row.

type ListQuery

type ListQuery struct {
	Action, ActorKind, ActorID, TargetKind, TargetID string
	Since, Until                                     string
	Limit, Offset                                    int
}

ListQuery filters List. Empty fields match everything. Since is inclusive and Until exclusive (RFC3339). Limit defaults to 50, max 500.

type Writer

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

Writer records and lists audit entries.

func New

func New(d *sql.DB, log *slog.Logger) *Writer

New returns a writer on d. The audit migrations must already be applied.

func (*Writer) List

func (w *Writer) List(ctx context.Context, q ListQuery) ([]Entry, int, error)

List returns matching entries newest first and the total match count.

func (*Writer) Record

func (w *Writer) Record(ctx context.Context, action, targetKind, targetID string, meta any)

Record appends an entry. The actor comes from ActorFromContext (system when absent) and the IP from WithIP. The write ignores ctx cancellation so an entry is not lost when the request ends. Failures are logged, never returned, so auditing cannot break the audited operation.

Record writes on its own pool connection: never call it inside db.InTx (it would wait on the transaction's lock for busy_timeout and fail). Use RecordTx inside a transaction, or call Record after commit.

func (*Writer) RecordTx

func (w *Writer) RecordTx(ctx context.Context, tx *sql.Tx, action, targetKind, targetID string, meta any)

RecordTx is Record inside tx: the entry commits or rolls back with it.

Jump to

Keyboard shortcuts

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