history

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 22 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 history is the operation history: a SQLite database in the config home's log directory that records every command that changed something, the audit events it raised, and the manifest before and after.

audit.log stays the record of what happened, and is written first and regardless. This store indexes it so it can be filtered by host, profile or key and outlive the log's two-generation rotation, and adds what the log never held: which command ran, how it ended, and what the manifest was on either side of it.

Rows are written only by a process holding the advisory lock, so the store relies on that and not on SQLite for ordering. Reads take no lock; WAL lets them run beside a writer.

Index

Constants

View Source
const (
	SourceCLI      = "cli"
	SourceTUI      = "tui"
	SourceWeb      = "web"
	SourceBackfill = "backfill"
)

Sources an operation can come from.

View Source
const (
	ResultOK      = "ok"
	ResultError   = "error"
	ResultAborted = "aborted"
	ResultDryRun  = "dry-run"
)

Results an operation can end with.

View Source
const (
	KindProfile = "profile"
	KindHost    = "host"
	KindKey     = "key"
	KindTarget  = "target"
)

Subject kinds, for filtering.

View Source
const MetaBackfillDone = "backfill_done"

MetaBackfillDone is set once the first open has imported what was on disk.

View Source
const TimeFormat = "2006-01-02T15:04:05Z"

TimeFormat is how every timestamp in the store is written: UTC, to the second, the form audit.log already uses, so the two sort together.

Variables

View Source
var ErrNewerSchema = errors.New("the history database was written by a newer authama; it is open read-only")

ErrNewerSchema reports a database written by a newer authama. It is opened read-only rather than migrated down, and never written.

Functions

func FormatDiff

func FormatDiff(lines []DiffLine) string

FormatDiff renders a Diff as text, one prefixed line each.

func Hash

func Hash(b []byte) string

Hash is the store's content hash: SHA-256, hex.

func NewOpID

func NewOpID() string

NewOpID returns a fresh operation id: 16 random bytes, hex.

func Now

func Now() string

Now is the current time in the store's format.

Types

type AuditLine

type AuditLine struct {
	TS, Event string
	Fields    []log.Field
}

AuditLine is one parsed audit.log line, with its fields in written order.

func ParseAuditLine

func ParseAuditLine(line []byte) (AuditLine, bool)

ParseAuditLine reads one audit.log line, keeping the order its fields were written in, which a map would lose.

type BackfillResult

type BackfillResult struct {
	Events    int
	Skipped   int // unreadable audit lines
	Manifests int
}

BackfillResult counts what a backfill imported.

func (BackfillResult) String

func (r BackfillResult) String() string

String summarizes a backfill for the one line `history` prints after it.

type DiffLine

type DiffLine struct {
	Op   byte
	Text string
}

DiffLine is one line of a diff: Op is ' ', '-' or '+'.

func Diff

func Diff(a, b string, context int) []DiffLine

Diff returns the changed lines between a and b with context lines around each change, as unified diffs show them. An unchanged pair returns nil. Hunks are separated by a DiffLine with Op '@'.

type Event

type Event struct {
	TS     string          `json:"ts"`
	Event  string          `json:"event"`
	Fields json.RawMessage `json:"fields"`
	Source string          `json:"source"`
}

Event is one recorded audit event.

type Filter

type Filter struct {
	Host, Profile, Key string
	Since, Until       string // inclusive, in TimeFormat or a prefix of it ("2026-10-27")
	Command            string // exact, or a prefix ending in a space-free word ("host" matches "host add")
	Source             string
	Limit              int
	// contains filtered or unexported fields
}

Filter narrows a query. Zero values match everything.

type Op

type Op struct {
	OpID            string    `json:"op_id"`
	StartedAt       string    `json:"started_at"`
	FinishedAt      string    `json:"finished_at,omitempty"`
	Command         string    `json:"command"`
	Args            []string  `json:"args"`
	Source          string    `json:"source"`
	OSUser          string    `json:"os_user,omitempty"`
	Machine         string    `json:"machine,omitempty"`
	Version         string    `json:"version,omitempty"`
	Result          string    `json:"result,omitempty"`
	Error           string    `json:"error,omitempty"`
	Snapshot        string    `json:"snapshot,omitempty"`
	ManifestBefore  string    `json:"manifest_before,omitempty"`
	ManifestAfter   string    `json:"manifest_after,omitempty"`
	InventoryBefore string    `json:"inventory_before,omitempty"`
	InventoryAfter  string    `json:"inventory_after,omitempty"`
	Subjects        []Subject `json:"subjects,omitempty"`
	Events          []Event   `json:"events"`
}

Op is one recorded operation, with the events it raised.

type Sources

type Sources struct {
	AuditLog     string // audit.log; its rotated .1 is read too, first
	Inventory    string
	SnapshotsDir string
	Manifest     string
}

Sources is what a backfill reads. Any of them may be missing.

type Store

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

Store is an open history database.

func Open

func Open(path string) (*Store, error)

Open opens the database at path, creating it owner-only and applying any migrations it is missing. A database from a newer authama opens read-only and Open returns it together with ErrNewerSchema.

func (*Store) AddEvent

func (s *Store) AddEvent(opID, ts, name string, fields []log.Field, source, dedupe string) error

AddEvent records one audit event. opID "" records it on no operation. dedupe "" is a live event; a backfilled one passes a key so that running the backfill twice records it once.

func (*Store) Backfill

func (s *Store) Backfill(src Sources) (BackfillResult, error)

Backfill imports the history that existed before the store did: every audit.log event, the creation and deployment dates inventory.json records, the snapshots on disk, and the current manifest. Each becomes an operation of source "backfill", so the timeline starts where the tool's records do.

It runs in one transaction and marks itself done in the same one, so a backfill that fails leaves nothing behind and the next open tries again. Every event carries a dedupe key besides, so importing the same line twice records it once.

func (*Store) Begin

func (s *Store) Begin(op *Op) error

Begin records the start of an operation. op.OpID and op.StartedAt are filled in when empty.

func (*Store) Close

func (s *Store) Close() error

Close closes the database.

func (*Store) Counts

func (s *Store) Counts() (ops, events int)

Counts reports how many operations and events the store holds.

func (*Store) Finish

func (s *Store) Finish(op *Op) error

Finish closes an operation with its result.

func (*Store) Get

func (s *Store) Get(id string) (Op, bool, error)

Get returns one operation by id, or by a unique prefix of it (as git does with commits). ok is false when nothing matches.

func (*Store) LiveAuditEvents

func (s *Store) LiveAuditEvents(since string) int

LiveAuditEvents counts the audit events recorded live - not imported, and not the store's own snapshot.failed notes - strictly after since.

func (*Store) Manifest

func (s *Store) Manifest(sha string) ([]byte, error)

Manifest returns a stored manifest body, or nil when the hash is unknown.

func (*Store) Meta

func (s *Store) Meta(key string) string

Meta reads a meta value, "" when unset.

func (*Store) NeedsBackfill

func (s *Store) NeedsBackfill() bool

NeedsBackfill reports a store that has not yet imported what was on disk.

func (*Store) Path

func (s *Store) Path() string

Path is the database file.

func (*Store) PutManifest

func (s *Store) PutManifest(body []byte) (string, error)

PutManifest stores a manifest body by its SHA-256 and returns the hash. A body already stored is not stored again.

func (*Store) Query

func (s *Store) Query(f Filter) ([]Op, error)

Query returns the operations matching f, newest first, each with its events.

func (*Store) ReadOnly

func (s *Store) ReadOnly() bool

ReadOnly reports a database this binary must not write.

func (*Store) SetMeta

func (s *Store) SetMeta(key, value string) error

SetMeta writes a meta value.

func (*Store) SetSnapshot

func (s *Store) SetSnapshot(opID, snapshot string) error

SetSnapshot records the snapshot an operation took.

type Subject

type Subject struct {
	Kind string `json:"kind"`
	Name string `json:"name"`
}

Subject is one name an operation or event is about.

func SubjectsOf

func SubjectsOf(fields []log.Field) []Subject

SubjectsOf names what an audit event is about, from the fields authama's events use for it.

Jump to

Keyboard shortcuts

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