Files
MikrotikManager/backend/src/services/bandwidth-node-registry-schema.ts
T

164 lines
11 KiB
TypeScript

/** External owner installs this component only after mmapp_control and the core
* bandwidth DDL exist. No namespace, authority, node or alias is auto-enrolled.
* Structural guards do not authenticate HMACs, quiescence or physical identity.
* The trusted issuer must verify those independently. No application-server FK,
* endpoint epoch or changing access generation defines a persistent node key.
* Existing unknown occupancy is never read, cleared or rebound by this DDL. */
export const BANDWIDTH_NODE_REGISTRY_TABLES = Object.freeze([
"bandwidth_node_authority", "bandwidth_nodes", "bandwidth_node_aliases", "bandwidth_node_events",
] as const)
/** Fresh-only literal SQL. No IF NOT EXISTS, seed, GUC privilege bypass,
* SECURITY DEFINER, cascade, reconciliation or time-based reclamation. */
export const BANDWIDTH_NODE_REGISTRY_DDL = `
CREATE TABLE mmapp_control.bandwidth_node_authority (
id integer PRIMARY KEY CHECK (id=1),
installation_id uuid NOT NULL,
issuer_key_sha256 text COLLATE "C" NOT NULL CHECK (issuer_key_sha256 ~ '^[a-f0-9]{64}$'),
quiescence_proof text COLLATE "C" NOT NULL CHECK (quiescence_proof ~ '^[a-f0-9]{64}$'),
status text COLLATE "C" NOT NULL CHECK (status IN ('active','quarantined')),
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
CREATE TABLE mmapp_control.bandwidth_nodes (
node_id uuid PRIMARY KEY,
claim_proof text COLLATE "C" NOT NULL UNIQUE CHECK (claim_proof ~ '^[a-f0-9]{64}$'),
enrollment_mac text COLLATE "C" NOT NULL CHECK (enrollment_mac ~ '^[a-f0-9]{64}$'),
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
CREATE TABLE mmapp_control.bandwidth_node_aliases (
server_id bigint PRIMARY KEY CHECK (server_id BETWEEN 1 AND 9007199254740991),
incarnation uuid NOT NULL UNIQUE,
node_id uuid NOT NULL REFERENCES mmapp_control.bandwidth_nodes(node_id) ON DELETE RESTRICT ON UPDATE RESTRICT,
status text COLLATE "C" NOT NULL CHECK (status IN ('active','tombstoned','quarantined')),
record_mac text COLLATE "C" NOT NULL CHECK (record_mac ~ '^[a-f0-9]{64}$'),
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
CREATE INDEX bandwidth_node_aliases_node_idx ON mmapp_control.bandwidth_node_aliases(node_id);
CREATE TABLE mmapp_control.bandwidth_node_events (
event_id uuid PRIMARY KEY,
server_id bigint NOT NULL REFERENCES mmapp_control.bandwidth_node_aliases(server_id) ON DELETE RESTRICT ON UPDATE RESTRICT,
incarnation uuid NOT NULL,
node_id uuid NOT NULL REFERENCES mmapp_control.bandwidth_nodes(node_id) ON DELETE RESTRICT ON UPDATE RESTRICT,
kind text COLLATE "C" NOT NULL CHECK (kind IN ('enroll','tombstone','quarantine')),
old_status text COLLATE "C" CHECK (old_status='active'),
new_status text COLLATE "C" NOT NULL CHECK (new_status IN ('active','tombstoned','quarantined')),
authority_proof text COLLATE "C" NOT NULL CHECK (authority_proof ~ '^[a-f0-9]{64}$'),
event_mac text COLLATE "C" NOT NULL CHECK (event_mac ~ '^[a-f0-9]{64}$'),
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
UNIQUE (server_id,kind),
CHECK (COALESCE(
(kind='enroll' AND old_status IS NULL AND new_status='active')
OR (kind='tombstone' AND old_status='active' AND new_status='tombstoned')
OR (kind='quarantine' AND old_status='active' AND new_status='quarantined'), false))
);
CREATE INDEX bandwidth_node_events_node_idx ON mmapp_control.bandwidth_node_events(node_id);
CREATE FUNCTION mmapp_control.bandwidth_node_authority_guard() RETURNS trigger
LANGUAGE plpgsql SET search_path=pg_catalog,mmapp_control AS $body$
BEGIN
IF TG_OP='DELETE' THEN RAISE EXCEPTION 'bandwidth node authority cannot be deleted' USING ERRCODE='23514'; END IF;
IF TG_OP='INSERT' THEN
IF NEW.status <> 'active' THEN RAISE EXCEPTION 'bandwidth node authority must start active' USING ERRCODE='23514'; END IF;
RETURN NEW;
END IF;
IF ROW(NEW.id,NEW.installation_id,NEW.issuer_key_sha256,NEW.quiescence_proof,NEW.created_at)
IS DISTINCT FROM ROW(OLD.id,OLD.installation_id,OLD.issuer_key_sha256,OLD.quiescence_proof,OLD.created_at)
OR OLD.status <> 'active' OR NEW.status <> 'quarantined' THEN
RAISE EXCEPTION 'bandwidth node authority is immutable except quarantine' USING ERRCODE='23514';
END IF;
RETURN NEW;
END;
$body$;
CREATE FUNCTION mmapp_control.bandwidth_node_immutable_guard() RETURNS trigger
LANGUAGE plpgsql SET search_path=pg_catalog,mmapp_control AS $body$
BEGIN
RAISE EXCEPTION 'bandwidth nodes are immutable' USING ERRCODE='23514';
END;
$body$;
CREATE FUNCTION mmapp_control.bandwidth_node_alias_guard() RETURNS trigger
LANGUAGE plpgsql SET search_path=pg_catalog,mmapp_control AS $body$
BEGIN
IF TG_OP='DELETE' THEN RAISE EXCEPTION 'bandwidth node aliases cannot be deleted' USING ERRCODE='23514'; END IF;
IF TG_OP='INSERT' THEN
IF NEW.status <> 'active' THEN RAISE EXCEPTION 'bandwidth node alias must start active' USING ERRCODE='23514'; END IF;
-- Keep the reference invariant under replica mode as well: ordinary FK
-- implementation triggers are not ALWAYS, while this row guard is.
-- Nodes cannot be updated or deleted, so this existence check needs no row
-- lock (and must not require UPDATE rights for the registry runtime).
PERFORM 1 FROM mmapp_control.bandwidth_nodes WHERE node_id=NEW.node_id;
IF NOT FOUND THEN RAISE EXCEPTION 'bandwidth node alias requires its original node' USING ERRCODE='23514'; END IF;
RETURN NEW;
END IF;
IF ROW(NEW.server_id,NEW.incarnation,NEW.node_id,NEW.created_at)
IS DISTINCT FROM ROW(OLD.server_id,OLD.incarnation,OLD.node_id,OLD.created_at)
OR OLD.status <> 'active' OR NEW.status NOT IN ('tombstoned','quarantined')
OR NEW.record_mac IS NOT DISTINCT FROM OLD.record_mac THEN
RAISE EXCEPTION 'bandwidth node alias cannot be rebound or reactivated' USING ERRCODE='23514';
END IF;
RETURN NEW;
END;
$body$;
CREATE FUNCTION mmapp_control.bandwidth_node_event_guard() RETURNS trigger
LANGUAGE plpgsql SET search_path=pg_catalog,mmapp_control AS $body$
DECLARE alias_row mmapp_control.bandwidth_node_aliases%ROWTYPE;
BEGIN
IF TG_OP <> 'INSERT' THEN RAISE EXCEPTION 'bandwidth node events are append only' USING ERRCODE='23514'; END IF;
SELECT * INTO alias_row FROM mmapp_control.bandwidth_node_aliases WHERE server_id=NEW.server_id FOR UPDATE;
IF NOT FOUND OR alias_row.incarnation <> NEW.incarnation OR alias_row.node_id <> NEW.node_id OR alias_row.status <> NEW.new_status THEN
RAISE EXCEPTION 'bandwidth node event does not match its original alias' USING ERRCODE='23514';
END IF;
RETURN NEW;
END;
$body$;
CREATE FUNCTION mmapp_control.bandwidth_node_alias_consistency_guard() RETURNS trigger
LANGUAGE plpgsql SET search_path=pg_catalog,mmapp_control AS $body$
DECLARE target_id bigint; alias_row mmapp_control.bandwidth_node_aliases%ROWTYPE;
total bigint; enrollments bigint; terminals bigint; terminal mmapp_control.bandwidth_node_events%ROWTYPE;
BEGIN
IF TG_OP='DELETE' THEN target_id:=OLD.server_id; ELSE target_id:=NEW.server_id; END IF;
SELECT * INTO alias_row FROM mmapp_control.bandwidth_node_aliases WHERE server_id=target_id FOR UPDATE;
IF NOT FOUND THEN RAISE EXCEPTION 'bandwidth node alias missing' USING ERRCODE='23514'; END IF;
SELECT count(*),count(*) FILTER (WHERE kind='enroll'),count(*) FILTER (WHERE kind IN ('tombstone','quarantine'))
INTO total,enrollments,terminals FROM mmapp_control.bandwidth_node_events WHERE server_id=target_id;
IF enrollments <> 1 OR terminals > 1 OR total <> enrollments+terminals OR total > 2
OR EXISTS(SELECT 1 FROM mmapp_control.bandwidth_node_events WHERE server_id=target_id
AND (incarnation <> alias_row.incarnation OR node_id <> alias_row.node_id)) THEN
RAISE EXCEPTION 'bandwidth node alias event chain is incomplete' USING ERRCODE='23514';
END IF;
IF alias_row.status='active' THEN
IF total <> 1 OR terminals <> 0 THEN RAISE EXCEPTION 'active bandwidth alias has a terminal event' USING ERRCODE='23514'; END IF;
ELSE
SELECT * INTO terminal FROM mmapp_control.bandwidth_node_events WHERE server_id=target_id AND kind IN ('tombstone','quarantine');
IF terminals <> 1 OR terminal.old_status IS DISTINCT FROM 'active' OR terminal.new_status IS DISTINCT FROM alias_row.status
OR (alias_row.status='tombstoned' AND terminal.kind IS DISTINCT FROM 'tombstone')
OR (alias_row.status='quarantined' AND terminal.kind IS DISTINCT FROM 'quarantine') THEN
RAISE EXCEPTION 'terminal bandwidth node alias event mismatch' USING ERRCODE='23514';
END IF;
END IF;
RETURN NULL;
END;
$body$;
CREATE TRIGGER bandwidth_node_authority_guard BEFORE INSERT OR UPDATE OR DELETE ON mmapp_control.bandwidth_node_authority FOR EACH ROW EXECUTE FUNCTION mmapp_control.bandwidth_node_authority_guard();
CREATE TRIGGER bandwidth_node_immutable_guard BEFORE UPDATE OR DELETE ON mmapp_control.bandwidth_nodes FOR EACH ROW EXECUTE FUNCTION mmapp_control.bandwidth_node_immutable_guard();
CREATE TRIGGER bandwidth_node_alias_guard BEFORE INSERT OR UPDATE OR DELETE ON mmapp_control.bandwidth_node_aliases FOR EACH ROW EXECUTE FUNCTION mmapp_control.bandwidth_node_alias_guard();
CREATE TRIGGER bandwidth_node_event_guard BEFORE INSERT OR UPDATE OR DELETE ON mmapp_control.bandwidth_node_events FOR EACH ROW EXECUTE FUNCTION mmapp_control.bandwidth_node_event_guard();
CREATE TRIGGER bandwidth_node_authority_no_truncate BEFORE TRUNCATE ON mmapp_control.bandwidth_node_authority FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.bandwidth_admission_reject_truncate();
CREATE TRIGGER bandwidth_nodes_no_truncate BEFORE TRUNCATE ON mmapp_control.bandwidth_nodes FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.bandwidth_admission_reject_truncate();
CREATE TRIGGER bandwidth_node_aliases_no_truncate BEFORE TRUNCATE ON mmapp_control.bandwidth_node_aliases FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.bandwidth_admission_reject_truncate();
CREATE TRIGGER bandwidth_node_events_no_truncate BEFORE TRUNCATE ON mmapp_control.bandwidth_node_events FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.bandwidth_admission_reject_truncate();
CREATE CONSTRAINT TRIGGER bandwidth_node_alias_consistency AFTER INSERT OR UPDATE OR DELETE ON mmapp_control.bandwidth_node_aliases DEFERRABLE INITIALLY DEFERRED FOR EACH ROW EXECUTE FUNCTION mmapp_control.bandwidth_node_alias_consistency_guard();
CREATE CONSTRAINT TRIGGER bandwidth_node_event_consistency AFTER INSERT OR UPDATE OR DELETE ON mmapp_control.bandwidth_node_events DEFERRABLE INITIALLY DEFERRED FOR EACH ROW EXECUTE FUNCTION mmapp_control.bandwidth_node_alias_consistency_guard();
ALTER TABLE mmapp_control.bandwidth_node_authority ENABLE ALWAYS TRIGGER bandwidth_node_authority_guard;
ALTER TABLE mmapp_control.bandwidth_nodes ENABLE ALWAYS TRIGGER bandwidth_node_immutable_guard;
ALTER TABLE mmapp_control.bandwidth_node_aliases ENABLE ALWAYS TRIGGER bandwidth_node_alias_guard;
ALTER TABLE mmapp_control.bandwidth_node_events ENABLE ALWAYS TRIGGER bandwidth_node_event_guard;
ALTER TABLE mmapp_control.bandwidth_node_authority ENABLE ALWAYS TRIGGER bandwidth_node_authority_no_truncate;
ALTER TABLE mmapp_control.bandwidth_nodes ENABLE ALWAYS TRIGGER bandwidth_nodes_no_truncate;
ALTER TABLE mmapp_control.bandwidth_node_aliases ENABLE ALWAYS TRIGGER bandwidth_node_aliases_no_truncate;
ALTER TABLE mmapp_control.bandwidth_node_events ENABLE ALWAYS TRIGGER bandwidth_node_events_no_truncate;
ALTER TABLE mmapp_control.bandwidth_node_aliases ENABLE ALWAYS TRIGGER bandwidth_node_alias_consistency;
ALTER TABLE mmapp_control.bandwidth_node_events ENABLE ALWAYS TRIGGER bandwidth_node_event_consistency;
`