sealdb

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: AGPL-3.0 Imports: 22 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const BusyTimeout = 10 * time.Second

BusyTimeout is how long SQLite itself waits for another process's lock in one try.

View Source
const Format = 3

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. Format 3 is the v2.7 shape of the records (store/format3.go); a format-2 store is moved to it once, by a person (store/migrate3.go).

View Source
const WriteWait = 60 * time.Second

WriteWait is how long a write step keeps trying, in all, to take the write lock and, separately, to commit, while other komando processes hold the store. Past it the step fails with ErrBusy.

Variables

View Source
var (
	// ErrBadFormat: the file's format is not a number komando wrote.
	ErrBadFormat = errors.New("sealdb: the store's format is not a number")
	// 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")
)
View Source
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.

View Source
var ErrBusy = errors.New("sealdb: store busy")

ErrBusy: other komando processes held the store past WriteWait.

View Source
var ErrClosed = errors.New("sealdb: the store was closed")

ErrClosed: a step on a handle Close closed.

View Source
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.

View Source
var ErrNotStore = errors.New("sealdb: not a komando store")

ErrNotStore: the file at path is not a finished komando store.

View Source
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.

View Source
var ErrSafetyChanged = errors.New("sealdb: the migration changed a safety row")

ErrSafetyChanged: a migration would have changed a safety row it must carry as it was.

View Source
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 IsBusy added in v0.18.0

func IsBusy(err error) bool

IsBusy reports a step that failed because other komando processes held the store: a write step past WriteWait (ErrBusy), or a read step SQLite gave up on after BusyTimeout.

func JournalLeft added in v0.18.0

func JournalLeft(path string) bool

JournalLeft reports whether store.db-journal is next to the store at path: a write step at work, or one that a crash cut off (a hot journal), which only a read-write open rolls back.

func Peek

func Peek(ctx context.Context, path string) (map[string]string, error)

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.

func RollBack added in v0.18.0

func RollBack(ctx context.Context, path string) error

RollBack opens the store at path read-write once and reads its header, which makes SQLite roll back a write step that a crash cut off; the read-only opens (Peek, OpenReadOnly) cannot, and fail on it. It needs no key, reads no record and never makes the file. The caller holds the store lock.

func Whole added in v0.17.0

func Whole(data []byte) ([]byte, string, error)

Whole covers a record whole.

Types

type AuditRow

type AuditRow struct {
	Seq  int64
	Time time.Time
	Data []byte
}

AuditRow is one audit entry, opened.

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)
	// OnWait, when set, is told once per write step, when it first finds
	// another process holding the store.
	OnWait func()
	// contains filtered or unexported fields
}

DB is an open store.

func Create added in v0.14.0

func Create(ctx context.Context, path string, key *sealrow.Key) (*DB, error)

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

func Open(ctx context.Context, path string, key *sealrow.Key) (*DB, error)

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 OpenReadOnly added in v0.18.0

func OpenReadOnly(ctx context.Context, path string, key *sealrow.Key) (*DB, error)

OpenReadOnly opens the store at path to read it only (a dry run): SQLite opens the file read-only and refuses every write, and nothing in its folder is made or changed, not even the file's mode.

func (*DB) Backup

func (d *DB) Backup(ctx context.Context, dst string) error

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) Close

func (d *DB) Close() error

Close closes the store; a later step fails with ErrClosed.

func (*DB) CloseIdle added in v0.14.0

func (d *DB) CloseIdle(err error) bool

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

func (d *DB) Generation(ctx context.Context) (uint64, error)

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

func (d *DB) MetaSealed(ctx context.Context) (bool, error)

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) Migrate added in v0.18.0

func (d *DB) Migrate(ctx context.Context, from, to int, rowKey *sealrow.Key, p *Policy, change, verify func(*Tx) error) error

Migrate moves the store from format from to format to in one write step: it checks the audit chain, seals every record and audit entry again under rowKey, lets change rewrite records, sets the format, seals the policy MAC under p, and checks the chain again. The safety rows must come out as they went in (only their labels may differ), then verify reads the result inside the same step. Any error undoes all of it.

The handle must check the store's own policy MAC (SetPolicy), so a row changed outside komando is never sealed under a new MAC. Once the step is kept this handle takes no more writes: the code that holds it reads format from.

func (*DB) Rekey

func (d *DB) Rekey(ctx context.Context, newKey *sealrow.Key, then func(*Tx) error) error

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

func (d *DB) SetPolicy(ctx context.Context, p *Policy) error

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

func (d *DB) StopWrites(err error)

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

func (d *DB) Update(ctx context.Context, fn func(*Tx) error) error

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

func (d *DB) Upgrade(ctx context.Context, rowKey *sealrow.Key, p *Policy) error

Upgrade moves a format-1 store to format 2 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) View

func (d *DB) View(ctx context.Context, fn func(*Tx) error) error

View runs fn in a read step: it sees one consistent store.

func (*DB) WritesStopped added in v0.14.0

func (d *DB) WritesStopped() error

WritesStopped is the error StopWrites set, or nil.

type Part added in v0.17.0

type Part func(data []byte) (covered []byte, label string, err error)

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.

func NewPolicy added in v0.17.0

func NewPolicy(key []byte, parts map[string]Part) (*Policy, error)

NewPolicy makes the policy for key (sealkey.Policy) over parts. The store's meta seal is always covered whole.

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

func (t *Tx) AppendAudit(data []byte) error

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

func (t *Tx) Audit(after int64, limit int) ([]AuditRow, error)

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) AuditLast

func (t *Tx) AuditLast() (int64, error)

AuditLast is the number of the newest audit entry, 0 when there is none.

func (*Tx) Delete

func (t *Tx) Delete(id string) error

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) Get

func (t *Tx) Get(id string) (Record, error)

Get returns the record with id, deleted or not.

func (*Tx) List

func (t *Tx) List(typ string) ([]Record, error)

List returns every record of type typ that is not deleted, oldest change first.

func (*Tx) Meta

func (t *Tx) Meta(k string) (string, error)

Meta returns one readable setting, "" when it is not set.

func (*Tx) Put

func (t *Tx) Put(typ, id string, data []byte) (Record, error)

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

func (t *Tx) SealMeta() error

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

func (t *Tx) SealPolicy(p *Policy) error

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).

func (*Tx) SetMeta

func (t *Tx) SetMeta(k, v string) error

SetMeta sets one readable setting. Readable means anyone with the file sees it: never a name, a secret or a host.

Jump to

Keyboard shortcuts

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