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
- func FilePath(dir, tenant string) string
- func GenerateKey() (pub, priv string, err error)
- func Hash(r Row) ([]byte, error)
- func KeyID(pub ed25519.PublicKey) string
- func ParsePrivateKey(b64 string) (ed25519.PrivateKey, error)
- func ParsePublicKey(b64 string) (ed25519.PublicKey, error)
- func TimeText(t time.Time) string
- func VerifyAnchor(pub ed25519.PublicKey, a Anchor) bool
- func ZeroHash() []byte
- type Anchor
- type Anchorer
- type DB
- type Options
- type Result
- type Row
Constants ¶
const AnchorEvery = 100
AnchorEvery is the number of rows of a tenant between two anchors.
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 GenerateKey ¶
GenerateKey makes a key pair and returns both halves in base64.
func Hash ¶
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 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 ¶
ParsePublicKey reads the base64 form of an ed25519 public key.
func VerifyAnchor ¶
VerifyAnchor reports whether the signature of the anchor is valid for the key.
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.
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 ¶
NewAnchorer makes an anchor writer from the base64 private key. dir may be empty.
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 ¶
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 ¶
Result is the verdict. Failure is empty when everything holds; Rows is the number of rows whose chain was checked.
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.