Documentation
¶
Overview ¶
Package sqlstore is a durable client.Store on SQLite (brief AL §8; ADR 0020, stage 4): the images, settings and commissioning records of a controller's client nodes, in the tables matter_client_values and matter_client_versions. It is the place of matter.js's storage contexts under nodes/<id>/endpoints/<ep>/<cluster>/<attr> with the version under "__version__" (packages/node/src/storage/client/ at 0ceb202d).
The schema ships the way store/ ships the device's: Schema is the DDL as one idempotent script, Apply runs it, Migrations are the same schema as versioned steps for a host's own migration tool, and SchemaFingerprint tells a host that its tables match this release's. The list is this package's own, so a device-side host's pin on len(store.Migrations()) does not move for a controller table.
The package takes an open *sql.DB and never opens one; it is written against modernc.org/sqlite, which the module already depends on.
Index ¶
- func Apply(ctx context.Context, db *sql.DB) error
- func Reseal(ctx context.Context, db *sql.DB, old, next store.Sealer) error
- func Schema() string
- func SchemaFingerprint(ctx context.Context, db *sql.DB) (string, error)
- func Upgrade(ctx context.Context, db *sql.DB) error
- type Migration
- type Store
- func (s *Store) EraseCluster(nodeID string, ep uint16, cluster uint32) error
- func (s *Store) EraseEndpoint(nodeID string, ep uint16) error
- func (s *Store) EraseNode(nodeID string) error
- func (s *Store) Load(nodeID string) ([]client.StoredEndpoint, error)
- func (s *Store) NodeIDs() ([]string, error)
- func (s *Store) Save(nodeID string, ep uint16, cluster uint32, values map[uint32][]byte, ...) error
- func (s *Store) ScrubOwed(ctx context.Context) (bool, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Apply ¶
Apply executes Schema against db, then Upgrade. Every statement is idempotent, so it is meant to run on every boot. It neither opens the database nor sets pragmas.
func Reseal ¶
Reseal moves a sealed registry from the sealer old to the sealer next — the rotation of the host's key — in one transaction: the seal canary opened under old (the key check), every sealed value opened under old and sealed under next, the canary last; then the store.Scrub that removes the values sealed under old from the file, with the mark of an owed scrub until it finished — store.Reseal's shape on the registry's tables (decision 195). A wrong old key or a value that does not open under it is store.ErrUnseal, and so is a secret value in clear (the host opens the registry with NewSealed under old first, which seals it); either leaves the registry as it was. A registry the release before schema v3 sealed carries no canary: every sealed value opening under old is the key check, and Reseal marks it. Every error after the commit is store.ErrScrubIncomplete: the values are sealed under next, the scrub stays marked as owed (Store.ScrubOwed) and the next NewSealed runs it again.
It is for a host that keeps the registry alone. A controller's database holds store's tables beside it under the same sealer: controller.Reseal moves both in one transaction, so no state exists in which one opens under the new key and the other only under the old.
No Store may be open on db while Reseal runs, and no other process may write the file. db must carry schema v3. There is no unseal.
func Schema ¶
func Schema() string
Schema returns the DDL of the matter_client_* tables as one idempotent SQL script — store.Schema's counterpart for the controller's client nodes. A host with its own migration tool feeds it through that tool; a host without one calls Apply.
func SchemaFingerprint ¶
SchemaFingerprint returns the SHA-256, hex-encoded, of db's matter_client_* schema: each table Schema creates and the automatic indexes of its keys, as sqlite_schema records them (type, name, table and the CREATE statement with identifier quotes dropped and whitespace collapsed) — the method of store.SchemaFingerprint. Rows and other tables do not count.
func Upgrade ¶
Upgrade rewrites what an idempotent CREATE TABLE cannot reach in a database an earlier release created; a host that runs Schema through its own tool still calls it after, so a later step arrives with the dependency bump. Steps, in order:
- matter_client_values gains migration v2's seal mark when it lacks it, with v2's statements (matter_client_metadata, which v2 also creates, comes with the schema's CREATE TABLE IF NOT EXISTS).
- matter_client_metadata gains migration v3's data column, which holds the seal canary, when it lacks it, with v3's statement.
Types ¶
type Migration ¶
type Migration struct {
// Version names the step: "v1", "v2", … in order; never reused, and a
// released step's text never changes.
Version string
// Up moves a database from the previous step to this one.
Up string
// Down reverts Up.
Down string
}
Migration is one versioned step of the matter_client_* schema, for a host that runs its own migration tool — the shape of store.Migration, declared here because that type's documentation and pin are the device tables'.
func Migrations ¶
func Migrations() []Migration
Migrations returns the schema's steps in order. Applied one after the other to an empty database they produce the schema Apply produces — the same SchemaFingerprint — which TestMigrationsMatchApply holds.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is a client.Store over a database the schema was applied to. It is safe for concurrent use: database/sql is, and SQLite serializes the writers.
func New ¶
New returns the store over db, which Apply (or the host's migration of Migrations) prepared. It keeps every value as it is; a sealed value it reads is store.ErrNoSealer, and it refuses to overwrite one with a clear value, and to write a secret value at all into a registry that carries the seal canary (NewSealed): store.ErrNoSealer, with nothing of the Save written.
func NewSealed ¶
NewSealed returns the store over db with its secret values sealed under s — the kept onboarding codes and the ICD Check-In keys ([isSecret]); every other value stays clear. db must carry migration v3 (Apply). It does what store.NewSealed does for the device's store (store/seal.go, NewSealed and sealClearRows), on the registry's own tables.
The seal canary (decision 194). The first NewSealed marks the registry with one: a reserved matter_client_metadata row holds a known value sealed under s. Every later NewSealed opens the canary first, so a sealer under another key fails with store.ErrUnseal before anything is written — whether or not the registry holds a sealed value yet — and a registry that carries the canary has a secret value in clear refused with ErrUnseal instead of adopted. A store without a sealer (New) refuses to write a secret value into a registry that carries it (store.ErrNoSealer). The canary is not tamper protection: whoever can write the file can delete the row, and the registry then counts as never sealed. Reseal moves it to a new sealer; the controller's factory reset drops it once the registry is empty.
The first NewSealed (no canary yet) checks the key on one sealed value the registry already holds, if any, then seals every secret value it finds in clear — a registry a store without a sealer wrote — and writes the canary, in one transaction that also records that a scrub of the file is owed when it sealed a value; a failure leaves the registry as it was. While a scrub is owed NewSealed rewrites the file and empties the -wal (store.Scrub), so the clear values do not stay behind in free pages, and then deletes the record. A scrub that does not finish (a full disk, a reader holding the -wal) does not fail NewSealed (decision 132): the values are sealed, the store is returned, the scrub stays owed (Store.ScrubOwed) and every later NewSealed tries it again.
While the store is open it writes no secret value in clear, and a secret value it reads without the seal mark is refused with store.ErrUnseal.
func (*Store) EraseCluster ¶
EraseCluster implements client.Store.
func (*Store) EraseEndpoint ¶
EraseEndpoint implements client.Store.
func (*Store) Load ¶
func (s *Store) Load(nodeID string) ([]client.StoredEndpoint, error)
Load implements client.Store: the endpoints in number order, their clusters in id order. A cluster with a version row but no values loads empty, as client.MemoryStore keeps a cluster a Save wrote.
func (*Store) NodeIDs ¶
NodeIDs implements client.Store: the distinct node ids of both tables, sorted.
func (*Store) Save ¶
func (s *Store) Save(nodeID string, ep uint16, cluster uint32, values map[uint32][]byte, version *uint32) error
Save implements client.Store in one transaction: each given attribute is written, a nil value deleted (EndpointStore.ts:104-105 deletes an undefined key); the cluster's version is set to version, NULL for nil, as client.MemoryStore replaces it.
func (*Store) ScrubOwed ¶
ScrubOwed reports whether the registry owes the scrub of a sealing or a Reseal: the values are sealed, but the clear (or old-key) bytes they replaced may remain in the file's free pages, its -wal or a kept -journal until a store.Scrub of the file finishes — a full disk or a reader holding the -wal stops one. NewSealed retries the scrub on every open while it is owed and opens the store either way (decision 132, as store.Store.ScrubOwed); a host that wants to fail closed checks ScrubOwed after NewSealed and stops, and one that does not logs it.