integrity

package
v0.0.0-...-1f1a93d Latest Latest
Warning

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

Go to latest
Published: May 13, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

NemesisBot - AI agent License: MIT Copyright (c) 2026 NemesisBot contributors Package integrity provides a tamper-evident persistent audit chain

NemesisBot - AI agent License: MIT Copyright (c) 2026 NemesisBot contributors Package integrity provides a Merkle hash tree for audit log integrity

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrIndexOutOfRange = errors.New("integrity: index out of range")
	ErrChainCorrupted  = errors.New("integrity: chain corruption detected")
	ErrChainClosed     = errors.New("integrity: chain is closed")
	ErrHashMismatch    = errors.New("integrity: hash mismatch")
)

Functions

func VerifyFromHash

func VerifyFromHash(leafHash Hash, proof []ProofStep, root Hash) bool

VerifyFromHash checks that a leaf with the given hash exists in the tree.

Types

type AuditChain

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

AuditChain is a tamper-evident, append-only log of security audit events. Each event is hashed and chained to the previous entry (prev_hash), and a Merkle tree is maintained over the entire log so that the root hash summarises all events up to that point.

Persistence is in JSONL format under the configured StoragePath directory. When the current file exceeds MaxFileSize a new segment is created.

All public methods are thread-safe.

func NewAuditChain

func NewAuditChain(cfg AuditChainConfig) (*AuditChain, error)

NewAuditChain opens or creates an audit chain at the configured path. If the directory exists and contains prior segment files they are loaded and (optionally) verified.

func (*AuditChain) Append

func (ac *AuditChain) Append(_ context.Context, event *AuditEvent) error

Append adds a new event to the chain. The event is hashed, linked to the previous entry, and appended to the Merkle tree before being persisted to disk.

func (*AuditChain) Close

func (ac *AuditChain) Close() error

Close flushes pending writes and releases file handles.

func (*AuditChain) GetEvent

func (ac *AuditChain) GetEvent(index int) (*ChainEntry, error)

GetEvent retrieves a chain entry by its index.

func (*AuditChain) RootHash

func (ac *AuditChain) RootHash() Hash

RootHash returns the current Merkle root hash as a hex string.

func (*AuditChain) Size

func (ac *AuditChain) Size() int

Size returns the total number of entries in the chain.

func (*AuditChain) Verify

func (ac *AuditChain) Verify(_ context.Context) error

Verify checks the integrity of the entire chain from the first entry to the last.

func (*AuditChain) VerifyRange

func (ac *AuditChain) VerifyRange(_ context.Context, from, to int) error

VerifyRange checks the integrity of the chain from index `from` to `to` inclusive.

type AuditChainConfig

type AuditChainConfig struct {
	Enabled      bool   // Enable the audit chain
	StoragePath  string // Directory for JSONL files
	MaxFileSize  int64  // Rotate after this many bytes (default 50 MB)
	VerifyOnLoad bool   // Verify full chain integrity on startup
}

AuditChainConfig configures the audit chain.

func DefaultAuditChainConfig

func DefaultAuditChainConfig() AuditChainConfig

DefaultAuditChainConfig returns a reasonable default configuration.

type AuditEvent

type AuditEvent struct {
	Timestamp time.Time              `json:"timestamp"`
	Operation string                 `json:"operation"`
	ToolName  string                 `json:"tool_name"`
	User      string                 `json:"user"`
	Source    string                 `json:"source"`
	Target    string                 `json:"target"`
	Decision  string                 `json:"decision"` // "allowed" or "denied"
	Reason    string                 `json:"reason"`
	Metadata  map[string]interface{} `json:"metadata,omitempty"`
}

AuditEvent describes a single security audit event.

type ChainEntry

type ChainEntry struct {
	Index      int        `json:"index"`
	Hash       Hash       `json:"hash"`
	PrevHash   Hash       `json:"prev_hash"`
	Event      AuditEvent `json:"event"`
	MerkleRoot Hash       `json:"merkle_root"`
	Timestamp  time.Time  `json:"timestamp"`
}

ChainEntry is one line in the persistent JSONL file.

type Hash

type Hash = string

Hash is a hex-encoded SHA256 digest string.

type MerkleTree

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

MerkleTree implements a simple binary Merkle tree using SHA256. Leaves are added sequentially. The tree is rebuilt from scratch on every insertion so that the root hash is always current.

The tree is NOT thread-safe; callers must synchronize access.

func NewMerkleTree

func NewMerkleTree() *MerkleTree

NewMerkleTree creates an empty Merkle tree.

func (*MerkleTree) AddLeaf

func (t *MerkleTree) AddLeaf(data []byte) Hash

AddLeaf appends data as a new leaf node and returns its hash. The tree root is recomputed.

func (*MerkleTree) Leaves

func (t *MerkleTree) Leaves() []Hash

Leaves returns a copy of all leaf hashes.

func (*MerkleTree) Proof

func (t *MerkleTree) Proof(leafIndex int) ([]ProofStep, error)

Proof generates a Merkle inclusion proof for the leaf at leafIndex. The proof is the list of sibling hashes needed to reconstruct the root, ordered from the leaf level up toward the root. Each entry is accompanied by a direction flag: "left" means the proof hash is on the left (prepend it), "right" means the proof hash is on the right (append it).

Verification recomputes: hash = H(proof[i].Hash + hash) or hash = H(hash + proof[i].Hash) depending on the direction.

func (*MerkleTree) RootHash

func (t *MerkleTree) RootHash() Hash

RootHash returns the current root hash of the tree. For an empty tree this returns the SHA256 of an empty byte slice.

func (*MerkleTree) Size

func (t *MerkleTree) Size() int

Size returns the number of leaves in the tree.

func (*MerkleTree) Verify

func (t *MerkleTree) Verify(leafData []byte, proof []ProofStep) bool

Verify checks that leafData exists in the tree by recomputing the root from the leaf hash and the supplied proof steps.

type ProofStep

type ProofStep struct {
	Hash      Hash   // sibling hash
	Direction string // "left" or "right"
}

ProofStep represents one step in a Merkle proof.

Jump to

Keyboard shortcuts

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