audit

package
v1.0.0 Latest Latest
Warning

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

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

Documentation

Overview

Package audit writes the immutable audit log: who did what to which target, before and after, from where, and whether it was allowed.

Index

Constants

View Source
const (
	Allowed = "allowed"
	Denied  = "denied"
	Failed  = "error"
)

Result of an audited action.

Variables

This section is empty.

Functions

func PrincipalFrom

func PrincipalFrom(ctx context.Context) *authz.Principal

PrincipalFrom returns the acting principal stored in ctx, if any.

func WithPrincipal

func WithPrincipal(ctx context.Context, p *authz.Principal) context.Context

WithPrincipal stores the acting principal in ctx.

func WithRequest

func WithRequest(ctx context.Context, r Request) context.Context

WithRequest stores request details in ctx for later entries.

Types

type Actor

type Actor struct {
	Type authz.ActorType
	ID   *uuid.UUID
	Name string
}

Actor identifies who acted.

type Event

type Event struct {
	WorkspaceID *uuid.UUID
	Action      string // dotted verb: "routing.update", "apikey.create", "auth.sign_in"
	TargetType  string
	TargetID    string
	TargetName  string
	Result      string
	Reason      string
	Before      any
	After       any
	Metadata    map[string]any
	// Actor overrides the actor taken from the context (system jobs, sign-in).
	Actor *Actor
}

Event is one audit entry. Use Log to write it.

type Logger

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

Logger writes audit entries. Writes go through the caller's transaction when one is passed, so an action and its audit entry commit together.

func New

func New(q store.Querier, log *slog.Logger) *Logger

New returns an audit logger.

func (*Logger) Log

func (l *Logger) Log(ctx context.Context, e Event) error

Log writes an entry using the logger's own connection.

func (*Logger) LogWith

func (l *Logger) LogWith(ctx context.Context, q store.Querier, e Event) error

LogWith writes an entry through q (for example a transaction's Queries).

type Request

type Request struct {
	IP        *netip.Addr
	UserAgent string
	RequestID string
}

Request carries per-request details stored with every entry.

func RequestFrom

func RequestFrom(ctx context.Context) Request

RequestFrom returns the request details stored in ctx.

Jump to

Keyboard shortcuts

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