audit

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: AGPL-3.0 Imports: 15 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 who changed what (#79, ADR-0036): state-changing API requests, terminal sessions, authorization denials and authentication. Request bodies are never stored: they can hold passwords, tokens and environment values.

Index

Constants

View Source
const (
	ResultOK       = "ok"
	ResultFailed   = "failed"
	ResultDenied   = "denied"
	ResultAccepted = "accepted" // a background job runs; set to ok or failed when it ends
)

Results.

View Source
const (
	ActionSetup  = "auth:setup"
	ActionLogin  = "auth:login"
	ActionLogout = "auth:logout"
)

Authentication actions, recorded by the auth service.

View Source
const DefaultRetention = 90 * 24 * time.Hour

DefaultRetention keeps 90 days.

View Source
const (

	// MaxRows bounds one page of the log.
	MaxRows = 500
)

Variables

This section is empty.

Functions

func ClientIP

func ClientIP(r *http.Request) string

ClientIP is the request's client address (behind a trusted proxy, already the real client).

func WriteCSV

func WriteCSV(w io.Writer, entries []Entry) error

WriteCSV writes entries as CSV with a header row.

Types

type Entry

type Entry struct {
	ID     int64     `json:"id"`
	At     time.Time `json:"at"`
	User   string    `json:"user,omitempty"`
	APIKey bool      `json:"apiKey,omitempty"`
	// APIKeyName is "<name> (<prefix>…)" of the key that made the request (#80).
	APIKeyName  string `json:"apiKeyName,omitempty"`
	Action      string `json:"action"`
	Method      string `json:"method,omitempty"`
	Path        string `json:"path,omitempty"`
	Environment string `json:"environment,omitempty"`
	Namespace   string `json:"namespace,omitempty"`
	Object      string `json:"object,omitempty"`
	Status      int    `json:"status,omitempty"`
	Result      string `json:"result"`
	Message     string `json:"message,omitempty"`
	JobID       string `json:"jobId,omitempty"`
	ClientIP    string `json:"clientIp,omitempty"`
	UserAgent   string `json:"userAgent,omitempty"`
}

Entry is one log entry. Keep in sync with api/openapi.yaml.

type Filter

type Filter struct {
	User   string
	Result string
	Query  string
	Since  time.Time
	Until  time.Time
	Before int64 // entries with smaller IDs, for paging
	Limit  int
}

Filter selects log entries.

type Recorder

type Recorder struct {
	Store Store
	// Retention is how long entries are kept; 0 keeps them forever.
	Retention time.Duration
	Logger    *slog.Logger
	Now       func() time.Time
	// contains filtered or unexported fields
}

Recorder writes the audit log. It implements authz.Auditor.

func (*Recorder) Audit

func (rec *Recorder) Audit(r *http.Request, a authz.AuditRecord)

Audit implements authz.Auditor.

func (*Recorder) Auth

func (rec *Recorder) Auth(r *http.Request, action, username string, ok bool, msg string)

Auth records an authentication event; username is who tried, also when it failed.

func (*Recorder) JobFinished

func (rec *Recorder) JobFinished(j jobs.Job)

JobFinished completes the entry of a request that started a job; use as jobs.Manager.OnFinish.

func (*Recorder) List

func (rec *Recorder) List(ctx context.Context, f Filter) ([]Entry, error)

List returns entries newest first.

func (*Recorder) Run

func (rec *Recorder) Run(ctx context.Context)

Run removes entries older than the retention until ctx ends.

type Store

type Store interface {
	InsertAudit(ctx context.Context, arg db.InsertAuditParams) error
	SetAuditJobResult(ctx context.Context, arg db.SetAuditJobResultParams) error
	ListAudit(ctx context.Context, arg db.ListAuditParams) ([]db.AuditLog, error)
	PruneAudit(ctx context.Context, before int64) (int64, error)
}

Store is the database of the log; *db.Queries of the write connection implements it.

Jump to

Keyboard shortcuts

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