audit

package
v0.1.0-alpha.63 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: GPL-3.0 Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package audit owns bounded access/control history. Events have reviewed fields and codes; credentials, content, request URLs and raw failures have no representation here. It borrows the process datastore and cannot drive motion.

Index

Constants

View Source
const (
	RetentionDays = 30
	PageSize      = 100
	PageMaxBytes  = 256 << 10
)

Retention and response limits bound the durable history and each read.

View Source
const QueueLimit = 256

QueueLimit is the maximum number of waiting runtime events.

Variables

This section is empty.

Functions

func AppendTx

func AppendTx(ctx context.Context, tx *sql.Tx, event Event) error

AppendTx makes an access mutation and its audit record one durable outcome. Callers must propagate failure so the transaction cannot silently omit history.

func CorrelateCommand

func CorrelateCommand(id string) string

CorrelateCommand produces a domain-separated digest without retaining raw IDs.

func WithActor

func WithActor(ctx context.Context, actor Actor) context.Context

WithActor carries server-resolved attribution into an owning transaction.

Types

type Actor

type Actor struct {
	Type      string `json:"type"`
	AccountID string `json:"account_id,omitempty"`
	SessionID string `json:"session_id,omitempty"`
}

Actor identifies the authenticated caller or a non-account system/public lane.

func ActingAccount

func ActingAccount(ctx context.Context, id string) Actor

ActingAccount records the account checked by a domain write, retaining a session reference only when the context belongs to that same account.

func ContextActor

func ContextActor(ctx context.Context) Actor

ContextActor returns the caller attribution, defaulting to system work.

type Event

type Event struct {
	ID              string `json:"id"`
	Sequence        int64  `json:"sequence"`
	OccurredAt      int64  `json:"occurred_at_ms"`
	Kind            Kind   `json:"kind"`
	Outcome         string `json:"outcome"`
	Actor           Actor  `json:"actor"`
	TargetAccountID string `json:"target_account_id,omitempty"`
	TargetSessionID string `json:"target_session_id,omitempty"`
	GrantID         string `json:"grant_id,omitempty"`
	Operation       string `json:"operation,omitempty"`
	Epoch           string `json:"epoch,omitempty"`
	Generation      uint64 `json:"generation,omitempty"`
	StopSequence    uint64 `json:"stop_sequence,omitempty"`
	TraceSequence   uint64 `json:"trace_sequence,omitempty"`
	Correlation     string `json:"correlation,omitempty"`
	HTTPStatus      int    `json:"http_status,omitempty"`
	Count           uint64 `json:"count,omitempty"`
	ExpiresAt       int64  `json:"expires_at_ms,omitempty"`
	Permanent       bool   `json:"permanent,omitempty"`
}

Event contains only reviewed fields. IDs are server-issued non-authenticating references. Correlation is a digest of the command ID, never the command body, token or private session key.

type Kind

type Kind string

Kind identifies a reviewed access or control transition.

const (
	AccountCreated           Kind = "account_created"
	AccountEnabled           Kind = "account_enabled"
	AccountDisabled          Kind = "account_disabled"
	AccountAccessChanged     Kind = "account_access_changed"
	PasswordChanged          Kind = "password_changed"
	PasswordRecovered        Kind = "password_recovered"
	RecoveryCodesReplaced    Kind = "recovery_codes_replaced"
	RecoveryCodesRemoved     Kind = "recovery_codes_removed"
	SessionCreated           Kind = "session_created"
	SessionRevoked           Kind = "session_revoked"
	SessionsRevoked          Kind = "sessions_revoked"
	GrantIssued              Kind = "grant_issued"
	GrantRevoked             Kind = "grant_revoked"
	LoginFailed              Kind = "login_failed"
	LoginThrottled           Kind = "login_throttled"
	CredentialCheckFailed    Kind = "credential_check_failed"    // #nosec G101 -- public event classification, not a credential.
	CredentialCheckThrottled Kind = "credential_check_throttled" // #nosec G101 -- public event classification, not a credential.
	ControlClaimed           Kind = "control_claimed"
	ControlTransferred       Kind = "control_transferred"
	ControlLost              Kind = "control_lost"
	CommandFinished          Kind = "command_finished"
	StopFinished             Kind = "stop_finished"
	ServerStarted            Kind = "server_started"
	ServerStopped            Kind = "server_stopped"
	HistoryGap               Kind = "history_gap"
)

Supported event kinds deliberately exclude free-form application content.

type Page

type Page struct {
	Events         []Event `json:"events"`
	NewestSequence int64   `json:"newest_sequence"`
	OldestSequence int64   `json:"oldest_sequence"`
	NextBefore     int64   `json:"next_before"`
	HasMore        bool    `json:"has_more"`
	RetentionDays  int     `json:"retention_days"`
	Limit          int     `json:"limit"`
	RowLimit       int     `json:"row_limit"`
}

Page is a bounded descending history window with stable sequence pagination.

type Recorder

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

Recorder never puts database work on a motion/Stop caller. One bounded queue, one writer and short cancelable transactions limit failure and shutdown costs.

func NewRecorder

func NewRecorder(store *Store) *Recorder

NewRecorder starts one worker; its owner must close it before the datastore.

func (*Recorder) Close

func (r *Recorder) Close()

Close drains after motion has quiesced, for at most two seconds; a blocked writer is canceled rather than leaking into the next app lifetime.

func (*Recorder) Record

func (r *Recorder) Record(event Event) bool

Record validates and enqueues without waiting for storage; false means no enqueue.

func (*Recorder) Status

func (r *Recorder) Status() Status

Status reads memory-only counters without touching storage.

type Status

type Status struct {
	QueueDepth       int64  `json:"queue_depth"`
	QueueLimit       int    `json:"queue_limit"`
	Dropped          uint64 `json:"dropped_since_startup"`
	WriteFailures    uint64 `json:"write_failures_since_startup"`
	StorageAvailable bool   `json:"storage_available"`
	LastWrite        int64  `json:"last_write_ms"`
}

Status reports bounded queue occupancy and explicit loss since process start.

type Store

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

Store borrows the shared process database; it does not own its lifetime.

func NewStore

func NewStore(db *appstore.DB) *Store

NewStore binds bounded history queries to the existing datastore.

func (*Store) Append

func (s *Store) Append(ctx context.Context, events []Event) error

Append persists an idempotent batch and removes events older than retention.

func (*Store) Page

func (s *Store) Page(ctx context.Context, before int64) (Page, error)

Page reads a coherent bounded batch, then releases its connection before any HTTP/socket write. Sequence pagination remains valid as newer events arrive.

func (*Store) Prune

func (s *Store) Prune(ctx context.Context) error

Prune removes expired events even while the application is otherwise idle.

Jump to

Keyboard shortcuts

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