auditchain

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package auditchain implements the byte-level primitives used by the audit hash chain. It contains no persistence or chain-state behavior.

Index

Constants

View Source
const (
	DomainTag            = "agentsql-audit-chain-v1"
	GenesisASCII         = "AGENTSQL_AUDIT_CHAIN_GENESIS_V1"
	GenesisPrevHex       = "1fedc65006f788f0d939bfb0fa153df1576be6daf8e4f447f0c6e895eea573d4"
	AlgorithmSHA256      = "sha256"
	AlgorithmHMACSHA256  = "hmac-sha256"
	ChainManagement      = "management"
	ChainTraffic         = "traffic"
	ChainFormatVersionV1 = int64(1)
	TimestampLayout      = "2006-01-02T15:04:05.000000Z"
)

Variables

This section is empty.

Functions

func EncodeCanonical

func EncodeCanonical(row Row) ([]byte, error)

EncodeCanonical serializes all 27 V1 fields in the frozen E3 order. String bytes are used verbatim; JSON and Unicode are never normalized.

func EncodeEnvelope

func EncodeEnvelope(envelope Envelope) ([]byte, error)

EncodeEnvelope validates and serializes all nine E2 fields in frozen order.

func EncodeTLV

func EncodeTLV(items ...Item) ([]byte, error)

EncodeTLV encodes items as TAG || uint32-big-endian-LEN || PAYLOAD. It rejects values that do not have the canonical representation for their tag, including invalid UTF-8 text and non-canonical integers.

func HMACSHA256Hex

func HMACSHA256Hex(key, envelope []byte) string

HMACSHA256Hex returns lowercase hex(HMAC-SHA256(key, envelope)).

func Hash

func Hash(envelope, key []byte) string

Hash computes the keyless hash when key is nil and HMAC-SHA256 otherwise. A present but empty key is intentionally distinct from a nil key.

func HashEnvelope

func HashEnvelope(envelope Envelope, encoded, key []byte) (string, error)

HashEnvelope selects the hash algorithm by the envelope mode and rejects a missing HMAC key or an unexpected key in keyless mode.

func SHA256Hex

func SHA256Hex(envelope []byte) string

SHA256Hex returns lowercase hex(SHA-256(envelope)).

func ValidateTimestamp

func ValidateTimestamp(value string) error

ValidateTimestamp checks the fixed-width UTC microsecond format required by E4. Encoding remains byte-preserving and does not call this function.

Types

type Envelope

type Envelope struct {
	DomainTag     string
	InstanceID    string
	ChainID       string
	FormatVersion int64
	AlgorithmMode string
	KeyVersion    int64
	ChainSeq      int64
	PrevHash      [32]byte
	Canonical     []byte
}

Envelope contains the nine frozen E2 fields. PrevHash has a compile-time fixed width so it is always encoded as exactly 32 raw bytes.

func NewEnvelope

func NewEnvelope(instanceID, chainID, algorithmMode string, keyVersion, chainSeq int64, prevHash [32]byte, canonical []byte) Envelope

NewEnvelope builds a V1 envelope using the fixed domain tag and format.

type Item

type Item struct {
	Tag     Tag
	Payload []byte
}

Item is one decoded TLV item. Payload is always a copy of the encoded input.

func DecodeTLV

func DecodeTLV(encoded []byte) ([]Item, error)

DecodeTLV strictly decodes a complete sequence of TLV items. Partial headers, truncated payloads, unknown tags, and non-canonical values fail.

type Row

type Row struct {
	ID             int64   `json:"id"`
	TS             string  `json:"ts"`
	AgentID        *string `json:"agent_id"`
	DatasourceID   *string `json:"datasource_id"`
	SessionID      *string `json:"session_id"`
	ConversationID *string `json:"conversation_id"`
	MCPTool        *string `json:"mcp_tool"`
	DBType         *string `json:"db_type"`
	SQLRaw         *string `json:"sql_raw"`
	SQLNorm        *string `json:"sql_norm"`
	StmtType       *string `json:"stmt_type"`
	Objects        *string `json:"objects"`
	Decision       string  `json:"decision"`
	RuleHits       *string `json:"rule_hits"`
	RiskLevel      *int64  `json:"risk_level"`
	EstRows        *int64  `json:"est_rows"`
	RowsReturned   *int64  `json:"rows_returned"`
	LatencyMS      *int64  `json:"latency_ms"`
	ClientIP       *string `json:"client_ip"`
	ModelName      *string `json:"model_name"`
	ErrorMsg       *string `json:"error_msg"`
	ErrorCode      *string `json:"error_code"`
	Action         *string `json:"action"`
	ActorType      *string `json:"actor_type"`
	ActorID        *string `json:"actor_id"`
	DetailsJSON    *string `json:"details_json"`
	EventUUID      *string `json:"event_uuid"`
}

Row is the complete V1 canonical business row. Pointer fields distinguish SQL NULL from a present zero value (including an empty string or integer 0).

type Tag

type Tag byte

Tag identifies the payload type of a TLV item.

const (
	TagNil   Tag = 0x00
	TagText  Tag = 0x01
	TagInt   Tag = 0x02
	TagBytes Tag = 0x03
)

Jump to

Keyboard shortcuts

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