Files
meshcore-analyzer/internal/users/schema.go
T
efitenandClaude Opus 5.5 d29957fbdc feat(observer-logins): lock the shared login out of migrated observers (#2348)
Follow-up to #2306 / #2319. With personal observer logins, a migrated
observer still is not safe from the shared login: the hook answers
`ignore` for the shared login, so EMQX's built-in database lets it
publish under any observer's topic, including one whose owner has moved
to a personal login. This PR lets an operator close that per migrated
observer, before the shared login is removed altogether.

## Changes

**users.db v8.** `observer_bindings.confirmed_at`: the moment the
owner's personal login first published under that pubkey.
- An auto-bind (the owner's first publish) is confirmed at once.
- An admin assignment stays unconfirmed until the owner's login
publishes under that pubkey; that publish sets it.
- Reassigning to another user or unbinding clears it; reassigning to the
same owner keeps it.
- Migration: existing `auto` rows get `confirmed_at = bound_at`, `admin`
rows stay NULL.

**Config.** `userManagement.observerLogins.sharedUsernames` (default
empty): the broker's shared login names. A name starting with `obs_` is
refused at startup.

**Authz hook.** For a login in `sharedUsernames`, a publish under
`meshcore/<x>/<pubkey>/...` is denied when that pubkey's binding is
confirmed, whatever the number of topic segments (the ingestor
attributes `meshcore/<x>/<pubkey>` and longer topics to that pubkey).
Logged as `publish denied: user=<name> observer=<8 hex>: confirmed
owner`. Every other request from a shared login keeps `ignore`, so the
built-in database decides as before. Personal logins are unchanged.

An orphaned binding (owner account deleted) does not count as confirmed.

**Admin.** The observers table of the Observer logins tab gets a
Confirmed column; the bindings JSON gets `confirmedAt`.

**Docs.** `docs/observer-mqtt-logins.md`: "Locking the shared login out
of migrated observers" (what it denies, how to see `confirmedAt`, the
risk below). `config.example.json` has the key.

## Risk for operators

An observer that is confirmed but still runs a second uploader on the
shared login loses that second uploader once its name is in
`sharedUsernames`. The deny log line names the observer.

## Tests

- `internal/users`: v7 to v8 migration on a real v7 database, auto
confirm, admin assignment then owner confirm, non-owner and repeated
confirm, reassign clears, orphan reassign.
- `cmd/server`: the decision table (confirmed, unconfirmed, unbound,
non-publish), topics with 3, 4 and 5+ segments, trailing slash, empty
and `client` iata, lowercase key; owner publish confirms, confirm
failure still allows and retries; reassign, unbind and user removal
clear the flag; the index load against a real store; config validation;
the hook and admin endpoints end to end.
- `tests/e2e/test-user-management-e2e.js`: the Confirmed column for an
auto-bound observer.
- Local: `internal/users`, `cmd/server` (two Windows-only tests skipped,
as on master) and `cmd/ingestor` green. The E2E assertion was not run
locally; CI runs it.

## Not in this PR

- Using `confirmed_at` for verified `/neighbors` reports: a separate PR,
built on #2300.
- Removing the shared login: still an operator step after migration.

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 19:10:41 +01:00

220 lines
7.5 KiB
Go

package users
import (
"database/sql"
"errors"
"fmt"
)
// migrations[i] upgrades the schema from version i to i+1. Forward-only:
// never edit a shipped entry, append a new one.
var migrations = [][]string{
{ // v1: sub-project A
`CREATE TABLE users (
id INTEGER PRIMARY KEY,
email TEXT NOT NULL UNIQUE,
display_name TEXT NOT NULL,
password_hash TEXT NOT NULL,
role TEXT NOT NULL DEFAULT 'user' CHECK (role IN ('user','admin')),
status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending','active','disabled')),
created_at INTEGER NOT NULL,
activated_at INTEGER,
activated_by INTEGER,
last_login_at INTEGER,
email_bouncing INTEGER NOT NULL DEFAULT 0
)`,
`CREATE TABLE sessions (
id INTEGER PRIMARY KEY,
token_hash TEXT NOT NULL UNIQUE,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
csrf_token TEXT NOT NULL,
created_at INTEGER NOT NULL,
expires_at INTEGER NOT NULL,
last_seen_at INTEGER NOT NULL,
user_agent TEXT NOT NULL DEFAULT ''
)`,
`CREATE INDEX sessions_user ON sessions(user_id)`,
`CREATE TABLE tokens (
token_hash TEXT PRIMARY KEY,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
purpose TEXT NOT NULL CHECK (purpose IN ('activate','reset','email_change')),
new_email TEXT,
expires_at INTEGER NOT NULL,
used_at INTEGER
)`,
`CREATE INDEX tokens_user ON tokens(user_id, purpose)`,
`CREATE TABLE audit_log (
id INTEGER PRIMARY KEY,
at INTEGER NOT NULL,
actor_user_id INTEGER,
action TEXT NOT NULL,
target_user_id INTEGER,
detail TEXT NOT NULL DEFAULT '{}'
)`,
`CREATE INDEX audit_target ON audit_log(target_user_id)`,
`CREATE INDEX audit_actor ON audit_log(actor_user_id)`,
`CREATE TABLE mail_log (
id INTEGER PRIMARY KEY,
user_id INTEGER REFERENCES users(id) ON DELETE SET NULL,
to_email TEXT NOT NULL,
purpose TEXT NOT NULL,
provider_message_id TEXT,
sent_at INTEGER NOT NULL,
last_event TEXT NOT NULL DEFAULT 'sent',
last_event_at INTEGER NOT NULL,
last_reason TEXT NOT NULL DEFAULT ''
)`,
`CREATE INDEX mail_user ON mail_log(user_id)`,
`CREATE UNIQUE INDEX mail_msgid ON mail_log(provider_message_id) WHERE provider_message_id IS NOT NULL`,
`CREATE TABLE mail_events (
mail_id INTEGER NOT NULL REFERENCES mail_log(id) ON DELETE CASCADE,
event TEXT NOT NULL,
at INTEGER NOT NULL,
reason TEXT NOT NULL DEFAULT '',
UNIQUE (mail_id, event, at)
)`,
},
{ // v2: sub-project B, settings sync (one document per user)
`CREATE TABLE user_settings (
user_id INTEGER PRIMARY KEY REFERENCES users(id) ON DELETE CASCADE,
doc TEXT NOT NULL,
revision INTEGER NOT NULL,
generation TEXT NOT NULL,
updated_at INTEGER NOT NULL
)`,
},
{ // v3: sub-project C, global audit list ordered and filtered by time
`CREATE INDEX audit_at ON audit_log(at)`,
},
{ // v4: sub-project D, proposals (one generic table; this version ships the kind 'hashtag_channel')
`CREATE TABLE proposals (
id INTEGER PRIMARY KEY,
kind TEXT NOT NULL,
subject TEXT NOT NULL,
status TEXT NOT NULL CHECK (status IN ('pending','approved','rejected','revoked')),
proposer_id INTEGER REFERENCES users(id) ON DELETE SET NULL,
reviewer_id INTEGER REFERENCES users(id) ON DELETE SET NULL,
note TEXT NOT NULL DEFAULT '',
created_at INTEGER NOT NULL,
decided_at INTEGER
)`,
`CREATE UNIQUE INDEX proposals_kind_subject ON proposals(kind, subject)`,
`CREATE INDEX proposals_status ON proposals(status)`,
},
{ // v5: sub-project E, node notifications
`CREATE TABLE notification_prefs (
user_id INTEGER PRIMARY KEY REFERENCES users(id) ON DELETE CASCADE,
enabled INTEGER NOT NULL DEFAULT 1,
events TEXT NOT NULL DEFAULT '',
unsub_token TEXT NOT NULL UNIQUE,
updated_at INTEGER NOT NULL
)`,
`CREATE TABLE notification_watches (
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
pubkey TEXT NOT NULL,
created_at INTEGER NOT NULL,
PRIMARY KEY (user_id, pubkey)
)`,
`CREATE TABLE notification_state (
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
event TEXT NOT NULL,
subject TEXT NOT NULL,
state TEXT NOT NULL,
changed_at INTEGER NOT NULL,
PRIMARY KEY (user_id, event, subject)
)`,
},
{ // v6: sub-project F, companion linking (device sessions, linked companions)
`ALTER TABLE sessions ADD COLUMN kind TEXT NOT NULL DEFAULT 'web'
CHECK (kind IN ('web','device'))`,
`ALTER TABLE sessions ADD COLUMN label TEXT NOT NULL DEFAULT ''`,
`ALTER TABLE sessions ADD COLUMN scopes TEXT NOT NULL DEFAULT ''`,
`CREATE TABLE companion_links (
pubkey TEXT PRIMARY KEY,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
name TEXT NOT NULL DEFAULT '',
linked_at INTEGER NOT NULL
)`,
`CREATE INDEX companion_links_user ON companion_links(user_id)`,
`CREATE TABLE link_challenges (
challenge_hash TEXT PRIMARY KEY,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
pubkey TEXT NOT NULL,
expires_at INTEGER NOT NULL
)`,
},
// v7: personal MQTT logins for observers, and the observer pubkeys bound
// to their owners (first claim wins; NULL user_id = orphaned).
{
`CREATE TABLE mqtt_logins (
user_id INTEGER PRIMARY KEY REFERENCES users(id) ON DELETE CASCADE,
username TEXT NOT NULL UNIQUE,
password_hash TEXT NOT NULL,
password_enc TEXT NOT NULL,
created_at INTEGER NOT NULL,
revoked_at INTEGER,
last_auth_at INTEGER
)`,
`CREATE TABLE observer_bindings (
pubkey TEXT PRIMARY KEY,
user_id INTEGER REFERENCES users(id) ON DELETE SET NULL,
bound_at INTEGER NOT NULL,
bound_by TEXT NOT NULL CHECK (bound_by IN ('auto','admin'))
)`,
`CREATE INDEX observer_bindings_user ON observer_bindings(user_id)`,
},
// v8: confirmed_at is set once the owner's own personal login has
// published under the bound pubkey; 'auto' bindings are born from that
// login, so they count as confirmed at bound_at.
{
`ALTER TABLE observer_bindings ADD COLUMN confirmed_at INTEGER`,
`UPDATE observer_bindings SET confirmed_at = bound_at WHERE bound_by = 'auto'`,
},
}
func (s *Store) migrate() error {
if _, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS schema_version (version INTEGER NOT NULL)`); err != nil {
return fmt.Errorf("users: schema_version: %w", err)
}
v, err := s.SchemaVersion()
if errors.Is(err, sql.ErrNoRows) {
if _, err := s.db.Exec(`INSERT INTO schema_version (version) VALUES (0)`); err != nil {
return fmt.Errorf("users: init schema_version: %w", err)
}
v = 0
} else if err != nil {
return fmt.Errorf("users: read schema_version: %w", err)
}
if v > len(migrations) {
return fmt.Errorf("users: database schema version %d is newer than this binary supports (%d)", v, len(migrations))
}
for i := v; i < len(migrations); i++ {
tx, err := s.db.Begin()
if err != nil {
return err
}
for _, stmt := range migrations[i] {
if _, err := tx.Exec(stmt); err != nil {
tx.Rollback()
return fmt.Errorf("users: migration %d: %w", i+1, err)
}
}
if _, err := tx.Exec(`UPDATE schema_version SET version = ?`, i+1); err != nil {
tx.Rollback()
return fmt.Errorf("users: migration %d: %w", i+1, err)
}
if err := tx.Commit(); err != nil {
return fmt.Errorf("users: migration %d: %w", i+1, err)
}
}
return nil
}
// SchemaVersion returns the applied schema version (sql.ErrNoRows on a
// database that has never been migrated).
func (s *Store) SchemaVersion() (int, error) {
var v int
err := s.db.QueryRow(`SELECT version FROM schema_version`).Scan(&v)
return v, err
}