audit

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package audit provides a small append-only, tamper-evident event log.

Index

Constants

View Source
const (
	// FileName is the fixed file name used inside an audit directory.
	FileName = "audit.jsonl"

	// DefaultMaxBytes bounds the audit log when callers do not select a
	// deployment-specific quota.
	DefaultMaxBytes int64 = 256 << 20
)

Variables

View Source
var (
	ErrClosed              = errors.New("audit log is closed")
	ErrLocked              = errors.New("audit log is already open by another writer")
	ErrQuotaExceeded       = errors.New("audit log quota exceeded")
	ErrUnsupportedPlatform = errors.New("secure audit storage is unsupported on this platform")
	ErrUncertainDurability = errors.New("audit durability is uncertain; close and reopen the log")
)

Functions

func Scan

func Scan(dir string, visit func(Entry) error) error

Scan verifies the complete chain and calls visit once for each entry. Entries are decoded one at a time so callers do not need to retain the full log.

func Verify

func Verify(dir string) error

Verify verifies all entries in an audit directory.

Types

type Entry

type Entry struct {
	Seq            uint64            `json:"seq"`
	EventID        string            `json:"event_id"`
	ExchangeID     string            `json:"exchange_id"`
	RequestID      string            `json:"request_id"`
	ConversationID string            `json:"conversation_id"`
	Type           string            `json:"type"`
	Status         string            `json:"status"`
	ErrorCode      string            `json:"error_code"`
	Timestamp      string            `json:"timestamp"`
	Text           string            `json:"text"`
	TextSHA256     string            `json:"text_sha256"`
	Details        map[string]string `json:"details,omitempty"`
	PreviousHash   string            `json:"previous_hash"`
	EntryHash      string            `json:"entry_hash"`
}

Entry is the durable representation of an audit event.

func ReadAll

func ReadAll(dir string) ([]Entry, error)

ReadAll reads and verifies all entries from an audit directory.

type EntryReference

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

EntryReference identifies one verified entry in the append-only log without retaining its text in memory. References are valid for the lifetime of the log: successful appends never move existing bytes.

type Event

type Event struct {
	EventID        string
	ExchangeID     string
	RequestID      string
	ConversationID string
	Type           string
	Status         string
	ErrorCode      string
	Text           string
	Details        map[string]string
}

Event contains the caller-controlled fields for a new audit entry. Timestamp, sequence number, hashes, and the previous-chain link are assigned by Append.

type Log

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

Log serializes appends from all goroutines and holds an exclusive OS lock for its lifetime so another process cannot write the same chain concurrently.

func Open

func Open(dir string) (*Log, error)

Open creates or opens an audit directory, enforces restrictive permissions, and verifies the complete existing chain before allowing another append.

func OpenWithQuota

func OpenWithQuota(dir string, maxBytes int64) (*Log, error)

OpenWithQuota opens an audit log with a maximum encoded size. An existing verified log may be opened above the selected quota for inspection and recovery, but no new entry is admitted until it fits within the quota.

func (*Log) AliasesDirectory

func (l *Log) AliasesDirectory(directory *os.File) (bool, error)

AliasesDirectory reports whether directory is the held audit/state directory. The caller must retain the descriptor through its mutation.

func (*Log) AliasesEntry

func (l *Log) AliasesEntry(parent *os.File, name string) (bool, error)

AliasesEntry reports whether name in the already-open parent directory is the reserved audit entry or an existing hard link to the held audit file. Callers can retain parent through a subsequent descriptor-relative rename, eliminating path-resolution and check-then-reopen gaps.

func (*Log) AliasesPath

func (l *Log) AliasesPath(path string) (bool, error)

AliasesPath performs a best-effort path preflight for clear CLI diagnostics. It is not an authorization boundary: callers that will mutate the path must use AliasesEntry with the exact parent descriptor retained for that mutation.

func (*Log) Append

func (l *Log) Append(event Event) (Entry, error)

Append durably adds an event. It returns only after the entry has been written and fsynced. A write or sync failure makes this handle unusable, because durability is then uncertain; reopen the log to recover safely.

func (*Log) AppendWithReference

func (l *Log) AppendWithReference(event Event) (Entry, EntryReference, error)

AppendWithReference durably adds an event and returns a stable reference that can later be read without scanning the complete log.

func (*Log) Close

func (l *Log) Close() error

Close closes the audit file. Every successful append was already fsynced.

func (*Log) Head

func (l *Log) Head() (uint64, string, error)

Head returns the current verified sequence and hash-chain head.

func (*Log) Path

func (l *Log) Path() string

Path returns the audit JSONL path.

func (*Log) ReadAll

func (l *Log) ReadAll() ([]Entry, error)

ReadAll reads and verifies the complete chain under the writer mutex.

func (*Log) ReadReference

func (l *Log) ReadReference(reference EntryReference) (Entry, error)

ReadReference reads and verifies one previously indexed entry under the writer mutex. It fails closed when the live handle is closed or poisoned.

func (*Log) Scan

func (l *Log) Scan(visit func(Entry) error) error

Scan verifies the complete chain and calls visit once for each entry while holding the writer mutex. Entries are decoded one at a time, so callers can inspect long logs without retaining every message in memory. The visitor must not call methods on l.

func (*Log) ScanWithReferences

func (l *Log) ScanWithReferences(visit func(Entry, EntryReference) error) error

ScanWithReferences verifies the complete chain and visits each entry with a stable byte reference. The visitor must not call methods on l.

func (*Log) Usage

func (l *Log) Usage() (usedBytes, maxBytes int64, err error)

Usage returns the verified encoded size and configured quota. A clean quota rejection leaves Usage and the read-only log operations available.

func (*Log) Verify

func (l *Log) Verify() error

Verify verifies the complete chain under the writer mutex.

Jump to

Keyboard shortcuts

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