audit

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: Apache-2.0 Imports: 13 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 records security-relevant events (sign-ins, account and credential changes, administrative changes and, when enabled, read access) in the gk_audit_event table.

Recording never fails the caller's request: a write error is logged and the request carries on. Every recorded event is also written to the application log as one structured "audit" line, for log shippers.

Index

Constants

View Source
const (
	CategoryAuth   = "auth"   // sign-in, sign-out, 2FA, passwords, tokens, sessions
	CategoryAdmin  = "admin"  // changes made through admin pages and admin APIs
	CategoryAccess = "access" // read access (ticket views, downloads, exports); opt-in
	CategorySystem = "system" // retention purges and other unattended actions
)

Categories group actions in the viewer.

View Source
const (
	OutcomeSuccess = "success"
	OutcomeFailure = "failure" // the action was attempted and failed (wrong password, invalid input)
	OutcomeDenied  = "denied"  // the caller was not allowed to do it
)

Outcomes.

View Source
const (
	ActorAgent     = "agent"
	ActorCustomer  = "customer"
	ActorAnonymous = "anonymous" // not signed in (failed sign-ins, password reset requests)
	ActorSystem    = "system"
	ActorPlugin    = "plugin"
)

Actor types.

View Source
const Redacted = "[redacted]"

Redacted replaces the value of a sensitive field in Changes.

Variables

This section is empty.

Functions

func Changes

func Changes(before, after map[string]any) map[string]any

Changes compares before and after field values and returns the fields that differ as {"field": {"from": old, "to": new}}. Values of sensitive fields are replaced with Redacted, so the log shows that a secret changed but not what it was or became. A field missing on one side counts as nil. It returns nil when nothing changed.

func ID

func ID(id int64) string

ID formats a numeric id for Event.TargetID.

func Purge

func Purge(ctx context.Context, db *sql.DB, cutoff time.Time) (int64, error)

Purge deletes events created before cutoff and returns how many it removed.

func Record

func Record(ctx context.Context, e Event)

Record writes e to gk_audit_event using the shared database connection.

func RecordDB

func RecordDB(ctx context.Context, db *sql.DB, e Event)

RecordDB writes e to gk_audit_event using db.

func RecordRequest

func RecordRequest(c *gin.Context, e Event)

RecordRequest records e for the request: an empty actor, IP or user agent is filled from c, and the request is marked as recorded.

func Recorded

func Recorded(c *gin.Context) bool

Recorded reports whether a handler already recorded an event for c.

func Sensitive

func Sensitive(field string) bool

Sensitive reports whether a field name looks like it holds a secret.

func Snapshot

func Snapshot(fields map[string]any) map[string]any

Snapshot returns the fields of a single-level map with sensitive values replaced, for the details of a create or delete event.

Types

type Event

type Event struct {
	Time     time.Time
	Category string
	// Action is a dotted name: area.object.verb, e.g. "auth.login",
	// "admin.user.update", "access.ticket.view".
	Action  string
	Outcome string

	ActorType  string
	ActorID    int64
	ActorLogin string
	IP         string
	UserAgent  string

	TargetType  string
	TargetID    string
	TargetLabel string

	// Details holds event-specific context: a reason, the request route,
	// field changes from Changes. Secrets must not be put here.
	Details map[string]any
}

Event is one audit record. Category, Action and Outcome are required; Time defaults to now.

func FromRequest

func FromRequest(c *gin.Context, category, action, outcome string) Event

FromRequest returns an event with the caller (as set by the auth middleware), client IP and user agent filled in.

Jump to

Keyboard shortcuts

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