audit

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: Apache-2.0 Imports: 20 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 makes the audit log tamper-evident: a hash chain over the rows of a tenant, signed anchors every 100 rows (in the database and in a file outside it), and a check that finds an edited row or a cut tail.

Input: a row of audit_log (Row) to hash; a pool or transaction for the anchor writer (Anchorer.Run) and for Verify. Output: the row hash (Hash), signed anchors in audit_anchors and in <dir>/<tenant>.jsonl, and the verdict of Verify.

The chain of a tenant: every row holds prev_hash, the hash of the previous row of the tenant (32 zero bytes for the first one), and hash, the SHA-256 of the canonical JSON (RFC 8785) of all fields of the row except hash. Writing the rows is done by internal/store (it takes a lock per tenant, so the chain never forks).

Index

Constants

View Source
const AnchorEvery = 100

AnchorEvery is the number of rows of a tenant between two anchors.

View Source
const HashSize = sha256.Size

HashSize is the length of a row hash and of prev_hash in bytes.

Variables

This section is empty.

Functions

func FilePath

func FilePath(dir, tenant string) string

FilePath is the anchor file of a tenant inside dir.

func GenerateKey

func GenerateKey() (pub, priv string, err error)

GenerateKey makes a key pair and returns both halves in base64.

func Hash

func Hash(r Row) ([]byte, error)

Hash returns SHA-256 of the canonical record of the row. The database may store a JSON field in another form than it was written (jsonb reorders keys and changes spaces), so the JSON fields are canonicalized first and the answer does not depend on that.

func KeyID

func KeyID(pub ed25519.PublicKey) string

KeyID names a public key in anchors: the first 8 bytes of its SHA-256, in hex.

func ParsePrivateKey

func ParsePrivateKey(b64 string) (ed25519.PrivateKey, error)

ParsePrivateKey reads the base64 form of an ed25519 private key: 64 bytes, or a 32 byte seed.

func ParsePublicKey

func ParsePublicKey(b64 string) (ed25519.PublicKey, error)

ParsePublicKey reads the base64 form of an ed25519 public key.

func TimeText

func TimeText(t time.Time) string

TimeText is the one way a timestamp enters the hash: UTC, six fractional digits.

func VerifyAnchor

func VerifyAnchor(pub ed25519.PublicKey, a Anchor) bool

VerifyAnchor reports whether the signature of the anchor is valid for the key.

func ZeroHash

func ZeroHash() []byte

ZeroHash is prev_hash of the first row of a tenant.

Types

type Anchor

type Anchor struct {
	TenantID  string `json:"tenant_id"`
	SeqUpto   int64  `json:"seq_upto"`
	HeadHash  string `json:"head_hash"` // hex
	Signature string `json:"signature"` // base64
	KeyID     string `json:"key_id"`
	SignedAt  string `json:"signed_at"` // RFC 3339, UTC
}

Anchor is one signed head of a chain. In the file it is one JSON line.

func ReadAnchorFile

func ReadAnchorFile(f *os.File) ([]Anchor, error)

ReadAnchorFile reads every line of an anchor file (from its start, whatever the position of the handle is). A line that is not an anchor is an error.

type Anchorer

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

Anchorer signs and writes the anchors. Dir is empty when no file is kept (the anchors then live in the database only).

func NewAnchorer

func NewAnchorer(privateKeyB64, dir string) (*Anchorer, error)

NewAnchorer makes an anchor writer from the base64 private key. dir may be empty.

func (*Anchorer) Once

func (a *Anchorer) Once(ctx context.Context, db DB) error

Once makes the anchors that are due (one for every full 100 rows of a tenant since its last anchor) and brings the anchor files up to date. Safe to call from several processes.

func (*Anchorer) Run

func (a *Anchorer) Run(ctx context.Context, db DB, interval time.Duration, log *slog.Logger)

Run writes the anchors every interval until ctx ends, and once more at the end. A failed pass is logged and tried again at the next tick.

type DB

type DB interface {
	Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)
	Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
	QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
}

DB is the part of a pool that the anchor writer and Verify use.

type Options

type Options struct {
	Tenant    string
	From, To  int64
	AnchorDir string
	PubKey    ed25519.PublicKey
}

Options say what Verify checks. From and To limit the rows and the anchors checked by seq (0 means no limit). PubKey nil skips the signatures. AnchorDir empty skips the file.

type Result

type Result struct {
	Rows    int
	Failure string
}

Result is the verdict. Failure is empty when everything holds; Rows is the number of rows whose chain was checked.

func Verify

func Verify(ctx context.Context, db DB, o Options) (Result, error)

Verify runs the checks. The error is for a failed query or file; a log that does not verify is a Result with a Failure.

type Row

type Row struct {
	Seq            int64
	TenantID       string
	At             time.Time // stored with microsecond precision
	EventType      string
	TraceID        string
	PrincipalID    string
	Tool           string
	IdemKey        []byte
	ControlNumbers []byte // JSON
	PolicyExplain  []byte // JSON
	ManifestSHA256 []byte
	PayloadSHA256  []byte
	Outcome        string
	Detail         []byte // JSON
	PrevHash       []byte
}

Row holds the fields of an audit_log row that the hash covers. Empty Tool and Outcome, and nil byte fields, stand for NULL.

Jump to

Keyboard shortcuts

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