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
- Variables
- func FormatDiff(lines []DiffLine) string
- func Hash(b []byte) string
- func NewOpID() string
- func Now() string
- type AuditLine
- type BackfillResult
- type DiffLine
- type Event
- type Filter
- type Op
- type Sources
- type Store
- func (s *Store) AddEvent(opID, ts, name string, fields []log.Field, source, dedupe string) error
- func (s *Store) Backfill(src Sources) (BackfillResult, error)
- func (s *Store) Begin(op *Op) error
- func (s *Store) Close() error
- func (s *Store) Counts() (ops, events int)
- func (s *Store) Finish(op *Op) error
- func (s *Store) Get(id string) (Op, bool, error)
- func (s *Store) LiveAuditEvents(since string) int
- func (s *Store) Manifest(sha string) ([]byte, error)
- func (s *Store) Meta(key string) string
- func (s *Store) NeedsBackfill() bool
- func (s *Store) Path() string
- func (s *Store) PutManifest(body []byte) (string, error)
- func (s *Store) Query(f Filter) ([]Op, error)
- func (s *Store) ReadOnly() bool
- func (s *Store) SetMeta(key, value string) error
- func (s *Store) SetSnapshot(opID, snapshot string) error
- type Subject
Constants ¶
const ( SourceCLI = "cli" SourceTUI = "tui" SourceWeb = "web" SourceBackfill = "backfill" )
Sources an operation can come from.
const ( ResultOK = "ok" ResultError = "error" ResultAborted = "aborted" ResultDryRun = "dry-run" )
Results an operation can end with.
const ( KindProfile = "profile" KindHost = "host" KindKey = "key" KindTarget = "target" )
Subject kinds, for filtering.
const MetaBackfillDone = "backfill_done"
MetaBackfillDone is set once the first open has imported what was on disk.
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 ¶
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 ¶
FormatDiff renders a Diff as text, one prefixed line each.
Types ¶
type AuditLine ¶
AuditLine is one parsed audit.log line, with its fields in written order.
func ParseAuditLine ¶
ParseAuditLine reads one audit.log line, keeping the order its fields were written in, which a map would lose.
type BackfillResult ¶
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 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 ¶
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 ¶
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 ¶
Begin records the start of an operation. op.OpID and op.StartedAt are filled in when empty.
func (*Store) Get ¶
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 ¶
LiveAuditEvents counts the audit events recorded live - not imported, and not the store's own snapshot.failed notes - strictly after since.
func (*Store) NeedsBackfill ¶
NeedsBackfill reports a store that has not yet imported what was on disk.
func (*Store) PutManifest ¶
PutManifest stores a manifest body by its SHA-256 and returns the hash. A body already stored is not stored again.
func (*Store) SetSnapshot ¶
SetSnapshot records the snapshot an operation took.