Documentation
¶
Overview ¶
Package signedhead is the signed snapshot of a table's live rows: a name, a monotonic epoch, and the SHA-256 of each stored row. The signature is HMAC-SHA256 with a type prefix and length-prefixed fields, so holding the database file alone cannot widen a limit by putting rows back, deleting them, or rolling the head to an older pair. An epochs file beside the secrets is the rollback anchor; Beacon does not rebuild a head by scanning rows except on the explicit Rebuild command.
Index ¶
- Constants
- Variables
- func Accept(result LoadResult, key string, row []byte) bool
- func HashRow(row []byte) string
- func Sign(sk []byte, h Head) []byte
- func Verify(sk []byte, h Head) bool
- type FileAnchor
- type Head
- type LoadResult
- type Status
- type Table
- func (t *Table) AfterCommit(h Head) error
- func (t *Table) Commit(tx kv.Tx, put map[string][]byte, del []string) (Head, error)
- func (t *Table) CommitFrom(tx kv.Tx, res LoadResult, put map[string][]byte, del []string) (Head, error)
- func (t *Table) Load(tx kv.Tx) LoadResult
- func (t *Table) Rebuild(tx kv.Tx, rows map[string][]byte) (Head, error)
Constants ¶
const TypePrefix = "beacon/signed-head/v1\n"
TypePrefix is written first in the HMAC so a signature for something else cannot be reused.
Variables ¶
var ( ErrFailsafe = errors.New("signed head is failsafe") ErrNoKey = errors.New("no signature key") )
Errors the tables return.
Functions ¶
func Accept ¶
func Accept(result LoadResult, key string, row []byte) bool
Accept reports whether row (the exact stored bytes, or nil if missing) matches the head.
Types ¶
type FileAnchor ¶
type FileAnchor struct {
Dir string
}
FileAnchor is the rollback anchor: Dir+"/epochs", one line per head ("<name> <epoch>\n").
func (FileAnchor) Epoch ¶
func (a FileAnchor) Epoch(name string) uint64
Epoch is name's recorded epoch, or 0 when the file or the name is not there. A corrupt file is 0; Load treats a read error as failsafe instead of calling this.
type Head ¶
Head is one signed snapshot. Entries maps a row key to the hex SHA-256 of the exact stored bytes. Sig is the HMAC; it is not part of the canonical form.
type Status ¶
type Status int
Status is how Load judged the stored head against the epochs file.
const ( // StatusEmpty is a first start: no head and the epochs file does not expect one. StatusEmpty Status = iota // StatusOK is a verified head whose epoch matches the file (or the file has caught up). StatusOK // StatusCatchUp is a verified head newer than the file (crash after the DB commit); Load // rewrites the file and Accept treats it as OK. StatusCatchUp // StatusFailsafe is a missing, bad, or stale head: the whole table is unusable until Rebuild. StatusFailsafe )
type Table ¶
type Table struct {
Name string
Bucket []byte
HeadKey []byte
SK func() []byte
Anchor FileAnchor
Alert func(string)
}
Table is one signed table: the head row in Bucket at HeadKey, signed with SK, anchored in Dir.
func (*Table) AfterCommit ¶
AfterCommit writes the epochs file. Call it after the database transaction has committed.
func (*Table) Commit ¶
Commit updates the head for the keys this transaction wrote or deleted, signs it, and writes it in the same transaction. It does not scan the bucket. A failsafe head is an error.
func (*Table) CommitFrom ¶
func (t *Table) CommitFrom(tx kv.Tx, res LoadResult, put map[string][]byte, del []string) (Head, error)
CommitFrom is Commit using a head already loaded and verified (a key rotation that must verify under the previous key and sign under the new one).