Documentation
¶
Overview ¶
Package sealdb is komando's local store: one SQLite file whose records are sealed rows (package sealrow).
Readable columns are only a random ID, the type, the version, the change time and the deletion marker. Everything else, names included, is sealed.
Several processes (the CLI, its background processes, the desktop app) share the file through SQLite's own locking: a write takes the write lock at its start (BEGIN IMMEDIATE) and waits up to BusyTimeout when another writer holds it. The rollback journal keeps the store one file, and every commit is flushed fully to disk (F_FULLFSYNC on macOS). Freed space is zeroed (secure_delete), so a deleted record or one sealed under an old key leaves no copy in the file.
The deletion marker and the change time are also inside the seal, so editing those columns makes the row fail to open. Audit entries are bound to their sequence number, so a removed or moved entry shows as a gap.
A row replaced with an older sealed copy of itself, a whole row removed, or the last audit entries cut off are caught by the index kept in the generation record (index.go, audit.go). The safety rows are bound by the policy MAC too, so a process that holds only the row key cannot change them unseen (policy.go). An old copy of the whole store is caught by the generation number also kept in the OS keychain (section 16).
Every step first checks that its key still opens the store: another process may have changed the key (Rekey) since this one opened it, and a write sealed with the old key would make the store unreadable. Such a step stops with ErrKeyChanged; the caller opens the store again.
A View or Update called from inside another step, on the same goroutine, joins that step: a write runs in a savepoint of it, so it is kept or undone with the outer step, and its own error undoes only its part. An Update inside a View, or a Backup inside any step, returns ErrNested.
The package reads no environment and no global state: the caller passes the path and the key.
Index ¶
- Constants
- Variables
- func Peek(ctx context.Context, path string) (map[string]string, error)
- func ResealMeta(ctx context.Context, path string, key *sealrow.Key, check func(*DB) error) (map[string]string, error)
- func Whole(data []byte) ([]byte, string, error)
- type AuditRow
- type DB
- func (d *DB) Backup(ctx context.Context, dst string) error
- func (d *DB) Close() error
- func (d *DB) CloseIdle(err error) bool
- func (d *DB) Generation(ctx context.Context) (uint64, error)
- func (d *DB) MetaSealed(ctx context.Context) (bool, error)
- func (d *DB) Rekey(ctx context.Context, newKey *sealrow.Key, then func(*Tx) error) error
- func (d *DB) SetPolicy(ctx context.Context, p *Policy) error
- func (d *DB) StopWrites(err error)
- func (d *DB) Update(ctx context.Context, fn func(*Tx) error) error
- func (d *DB) Upgrade(ctx context.Context, rowKey *sealrow.Key, p *Policy) error
- func (d *DB) View(ctx context.Context, fn func(*Tx) error) error
- func (d *DB) WritesStopped() error
- type Part
- type Policy
- type PolicyError
- type Record
- type Tx
- func (t *Tx) AppendAudit(data []byte) error
- func (t *Tx) Audit(after int64, limit int) ([]AuditRow, error)
- func (t *Tx) AuditLast() (int64, error)
- func (t *Tx) Delete(id string) error
- func (t *Tx) Get(id string) (Record, error)
- func (t *Tx) List(typ string) ([]Record, error)
- func (t *Tx) Meta(k string) (string, error)
- func (t *Tx) Put(typ, id string, data []byte) (Record, error)
- func (t *Tx) SealMeta() error
- func (t *Tx) SealPolicy(p *Policy) error
- func (t *Tx) SetMeta(k, v string) error
Constants ¶
const BusyTimeout = 10 * time.Second
BusyTimeout is how long a writer waits for another writer.
const Format = 2
Format is the database format this komando writes. A file with a higher number was made by a newer komando. Format 2 binds the safety rows with the policy MAC (policy.go); a format-1 store is upgraded to it.
Variables ¶
var ( // ErrNewerFormat: the file was made by a newer komando. ErrNewerFormat = errors.New("sealdb: store made by a newer komando; update komando") // ErrNotFound: no record with that ID. ErrNotFound = errors.New("sealdb: record not found") // ErrReadOnly: a change inside View. ErrReadOnly = errors.New("sealdb: change inside a read step") // ErrNested: a DB method called from inside View or Update. ErrNested = errors.New("sealdb: store used from inside its own read or write step") // ErrTampered: a readable column does not match what was sealed, or an // audit entry is missing. ErrTampered = errors.New("sealdb: store was changed outside komando") // ErrWrongKey: the store was sealed with another key. ErrWrongKey = errors.New("sealdb: the key does not open this store") // ErrKeyChanged: another process changed the store's key after this // handle was opened. Close it and open the store with the new key. ErrKeyChanged = errors.New("sealdb: the store's key was changed by another komando; open it again") )
var CreateFormat = Format
CreateFormat is the format Create writes. Tests set it to 1 to make a store as an older komando did (the caller then seals it as format 1); nothing else changes it.
var ErrClosed = errors.New("sealdb: the store was closed")
ErrClosed: a step on a handle Close closed.
var ErrMetaTampered = fmt.Errorf("the store's settings row does not match its sealed copy: %w", ErrTampered)
ErrMetaTampered: the meta table does not match its sealed copy.
var ErrNotStore = errors.New("sealdb: not a komando store")
ErrNotStore: the file at path is not a finished komando store.
var ErrPolicyMissing = fmt.Errorf("the store's safety rows have no policy MAC: %w", ErrTampered)
ErrPolicyMissing: a store of format 2 without its policy MAC.
var SealNewStores = true
SealNewStores: Create seals a new store's meta table. Tests set it false to make a store as an older komando did; nothing else changes it.
Functions ¶
func Peek ¶
Peek reads the readable settings of the store at path (format, key mode, salt) without a key and without making the file. A missing file is os.ErrNotExist.
func ResealMeta ¶ added in v0.17.0
func ResealMeta(ctx context.Context, path string, key *sealrow.Key, check func(*DB) error) (map[string]string, error)
ResealMeta is the way back from ErrMetaTampered, for a person: it opens the store at path with key without comparing the meta table, checks that key opens every record and audit entry (so the key, and the meta it was made from, are the right ones), and seals the meta table as it is now. It returns the meta it sealed. A record that does not open stops it and changes nothing. check, when set, runs on the opened store before anything is written; its error stops the reseal.
Types ¶
type DB ¶
type DB struct {
// OnGeneration, when set, is told the new generation after each write
// step that changed a record (the store keeps it in the keychain).
OnGeneration func(uint64)
// contains filtered or unexported fields
}
DB is an open store.
func Create ¶ added in v0.14.0
Create makes a new store at path, which must not exist (mode 0600), in one write step: a crash leaves no half-made store that Open takes.
func Open ¶
Open opens the store at path. It never makes one: a missing file is os.ErrNotExist (SQLite is told not to make it either), and a file without a finished store is ErrNotStore. It stops with ErrNewerFormat for a file from a newer komando.
func (*DB) Backup ¶
Backup writes a consistent copy of the store to dst, which must not exist. A plain copy of the file taken during a write may be broken.
func (*DB) CloseIdle ¶ added in v0.14.0
CloseIdle closes the store when no step runs on it, and reports whether it did; every later step fails with err. A caller that still holds the handle gets err, never a raw database error.
func (*DB) Generation ¶
Generation is how many write steps changed this store's records or added audit entries (readable settings do not count).
func (*DB) MetaSealed ¶ added in v0.17.0
MetaSealed reports whether the store has the sealed copy of its meta table (an older store has none until a person carries it over).
func (*DB) Rekey ¶
Rekey seals every record, deleted ones included, and every audit entry again under newKey, in one write step, then runs then (still inside that step, with newKey in force) for what the caller changes along with it, such as the readable key settings and the policy MAC under the new master key (SealPolicy). Nothing changes unless all of it succeeds. From then on the store uses newKey.
func (*DB) SetPolicy ¶ added in v0.17.0
SetPolicy checks the store's safety rows against their policy MAC and, when they match, makes every later step check them too. Call it right after Open, before the store is shared.
func (*DB) StopWrites ¶ added in v0.14.0
StopWrites makes every later write step fail with err: the store is read, never changed (it could not be checked, or the caller stopped handing this handle out). Safe while other goroutines use d; a write step already running ends as it would have.
func (*DB) Update ¶
Update runs fn in a write step, which holds the write lock from its start, so a read, check and write inside fn (a unique name) cannot race another process. A non-nil error from fn undoes every change.
func (*DB) Upgrade ¶ added in v0.17.0
Upgrade moves a format-1 store to this format in one write step: every record and audit entry is sealed again under rowKey, the format goes up, and the safety rows get their policy MAC. From then on the store uses rowKey and p.
func (*DB) WritesStopped ¶ added in v0.14.0
WritesStopped is the error StopWrites set, or nil.
type Part ¶ added in v0.17.0
Part says what of a record its type's MAC covers: nil when the record holds nothing it covers (a server without a safety mark). label names the row in an error; it is not covered.
type Policy ¶ added in v0.17.0
type Policy struct {
// contains filtered or unexported fields
}
Policy is the policy key and the record types whose rows it covers.
type PolicyError ¶ added in v0.17.0
type PolicyError struct {
Type, ID string
Label string // the row's name, when the record is there to read it
// Change is "changed", "added" (a row the MAC does not list) or
// "removed" (a row it lists that is gone or has no mark now); "" when
// the policymac record itself does not match the policy key.
Change string
More int // how many other rows differ too
}
PolicyError: a safety row does not match the policy MAC.
func (*PolicyError) Error ¶ added in v0.17.0
func (e *PolicyError) Error() string
func (*PolicyError) Unwrap ¶ added in v0.17.0
func (e *PolicyError) Unwrap() error
type Record ¶
type Record struct {
ID string
Type string
Version uint64
MTime time.Time
Deleted bool
Data []byte
}
Record is one record, opened.
type Tx ¶
type Tx struct {
// contains filtered or unexported fields
}
Tx is one read or write step.
func (*Tx) AppendAudit ¶
AppendAudit adds one audit entry. It raises the generation like a changed record, so the entry cannot be cut off later unnoticed.
func (*Tx) Audit ¶
Audit returns up to limit audit entries after seq, oldest first. A missing, damaged or moved entry, or entries cut from the end, stop it with ErrTampered, together with the entries before it.
func (*Tx) Delete ¶
Delete marks record id deleted and drops its content. The ID and the marker stay, so a later sync can tell a deletion from a record never seen. Deleting a missing record is ErrNotFound.
func (*Tx) Put ¶
Put writes data as record id of type typ, at the next version. An empty id makes a new record with a random ID. A record keeps its type.
func (*Tx) SealMeta ¶ added in v0.17.0
SealMeta writes the sealed copy of the meta table as it is now: on a new store, after every meta write, and when a person carries an older store over (or reseals it after checking it, doctor --reseal-meta).
func (*Tx) SealPolicy ¶ added in v0.17.0
SealPolicy writes the policy MAC over the safety rows as they are now, and makes p the policy of this step, and of the store once the step is kept: on a new store, on an upgrade, and with a new master key (store Rekey).