feat: snapshot Linux-first RMM MVP and pause handover
Rust development slice / linux (push) Failing after 1m10s
Rust development slice / linux (push) Failing after 1m10s
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
CREATE TABLE tenants (tenant_id uuid PRIMARY KEY, name text NOT NULL);
|
||||
INSERT INTO tenants VALUES
|
||||
('11111111-1111-4111-8111-111111111111','Development A'),
|
||||
('22222222-2222-4222-8222-222222222222','Development B');
|
||||
CREATE TABLE enrollment_tokens (
|
||||
token_hash text PRIMARY KEY, tenant_id uuid NOT NULL REFERENCES tenants,
|
||||
expires_at timestamptz NOT NULL, consumed_at timestamptz
|
||||
);
|
||||
CREATE TABLE assets (
|
||||
tenant_id uuid NOT NULL REFERENCES tenants, asset_id uuid NOT NULL,
|
||||
PRIMARY KEY(tenant_id,asset_id)
|
||||
);
|
||||
CREATE TABLE agent_installations (
|
||||
tenant_id uuid NOT NULL, agent_id uuid NOT NULL, asset_id uuid NOT NULL,
|
||||
certificate_sha256 text NOT NULL UNIQUE,
|
||||
enrollment_generation bigint NOT NULL DEFAULT 1 CHECK(enrollment_generation=1),
|
||||
connection_generation bigint NOT NULL DEFAULT 0,
|
||||
revoked_at timestamptz, last_contact_at timestamptz,
|
||||
PRIMARY KEY(tenant_id,agent_id),
|
||||
FOREIGN KEY(tenant_id,asset_id) REFERENCES assets(tenant_id,asset_id)
|
||||
);
|
||||
CREATE TABLE observations (
|
||||
tenant_id uuid NOT NULL, agent_id uuid NOT NULL, message_id uuid NOT NULL,
|
||||
boot_id uuid NOT NULL, sequence bigint NOT NULL CHECK(sequence>0),
|
||||
observed_at timestamptz NOT NULL, received_at timestamptz NOT NULL DEFAULT now(),
|
||||
source_id text NOT NULL CHECK(source_id='linux.base'),
|
||||
quality text NOT NULL CHECK(quality='valid'),
|
||||
completeness text NOT NULL CHECK(completeness='partial'),
|
||||
snapshot_id uuid NOT NULL, data jsonb NOT NULL, payload_sha256 text NOT NULL,
|
||||
PRIMARY KEY(tenant_id,message_id),
|
||||
UNIQUE(tenant_id,agent_id,sequence),
|
||||
FOREIGN KEY(tenant_id,agent_id) REFERENCES agent_installations(tenant_id,agent_id)
|
||||
);
|
||||
-- Narrow identity bootstrap functions, owned only by the migration identity.
|
||||
CREATE FUNCTION consume_enrollment(hash text) RETURNS uuid
|
||||
LANGUAGE sql SECURITY DEFINER SET search_path = pg_catalog, public AS $$
|
||||
UPDATE public.enrollment_tokens SET consumed_at=now()
|
||||
WHERE token_hash=hash AND consumed_at IS NULL AND expires_at>now() RETURNING tenant_id;
|
||||
$$;
|
||||
CREATE FUNCTION device_binding(fingerprint text)
|
||||
RETURNS TABLE(tenant_id uuid, agent_id uuid)
|
||||
LANGUAGE sql SECURITY DEFINER SET search_path = pg_catalog, public AS $$
|
||||
SELECT a.tenant_id,a.agent_id FROM public.agent_installations a
|
||||
WHERE a.certificate_sha256=fingerprint AND a.revoked_at IS NULL;
|
||||
$$;
|
||||
REVOKE ALL ON FUNCTION consume_enrollment(text), device_binding(text) FROM PUBLIC;
|
||||
ALTER TABLE tenants ENABLE ROW LEVEL SECURITY;
|
||||
CREATE POLICY tenant_scope ON tenants USING (tenant_id::text=current_setting('app.tenant_id',true)) WITH CHECK (tenant_id::text=current_setting('app.tenant_id',true));
|
||||
ALTER TABLE assets ENABLE ROW LEVEL SECURITY;
|
||||
CREATE POLICY tenant_scope ON assets USING (tenant_id::text=current_setting('app.tenant_id',true)) WITH CHECK (tenant_id::text=current_setting('app.tenant_id',true));
|
||||
ALTER TABLE agent_installations ENABLE ROW LEVEL SECURITY;
|
||||
CREATE POLICY tenant_scope ON agent_installations USING (tenant_id::text=current_setting('app.tenant_id',true)) WITH CHECK (tenant_id::text=current_setting('app.tenant_id',true));
|
||||
ALTER TABLE observations ENABLE ROW LEVEL SECURITY;
|
||||
CREATE POLICY tenant_scope ON observations USING (tenant_id::text=current_setting('app.tenant_id',true)) WITH CHECK (tenant_id::text=current_setting('app.tenant_id',true));
|
||||
GRANT SELECT ON tenants,assets,agent_installations,observations TO rmm_app;
|
||||
GRANT INSERT ON assets,agent_installations,observations TO rmm_app;
|
||||
GRANT UPDATE(connection_generation,last_contact_at) ON agent_installations TO rmm_app;
|
||||
GRANT EXECUTE ON FUNCTION consume_enrollment(text),device_binding(text) TO rmm_app;
|
||||
@@ -0,0 +1,82 @@
|
||||
ALTER TABLE tenants ADD COLUMN changes_stopped boolean NOT NULL DEFAULT false;
|
||||
ALTER TABLE agent_installations ADD COLUMN assignment_version bigint NOT NULL DEFAULT 1;
|
||||
ALTER TABLE agent_installations ADD COLUMN capabilities jsonb NOT NULL DEFAULT '[]';
|
||||
GRANT UPDATE(capabilities) ON agent_installations TO rmm_app;
|
||||
CREATE TABLE operation_plans (
|
||||
tenant_id uuid NOT NULL REFERENCES tenants, plan_id uuid NOT NULL,
|
||||
principal text NOT NULL, payload bytea NOT NULL, plan_hash text NOT NULL,
|
||||
expires_at timestamptz NOT NULL, created_at timestamptz NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY(tenant_id,plan_id), UNIQUE(tenant_id,plan_id,plan_hash)
|
||||
);
|
||||
CREATE TABLE approval_requests (
|
||||
tenant_id uuid NOT NULL, operation_id uuid NOT NULL, plan_id uuid NOT NULL,
|
||||
principal text NOT NULL, idempotency_key text NOT NULL, request_hash text NOT NULL,
|
||||
approval_state text NOT NULL DEFAULT 'pending_approval' CHECK(approval_state IN ('pending_approval','approved','rejected','expired','revoked')),
|
||||
created_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY(tenant_id,operation_id),
|
||||
UNIQUE(tenant_id,plan_id), UNIQUE(tenant_id,principal,idempotency_key),
|
||||
FOREIGN KEY(tenant_id,plan_id) REFERENCES operation_plans(tenant_id,plan_id)
|
||||
);
|
||||
CREATE TABLE authorizations (
|
||||
tenant_id uuid NOT NULL, authorization_id uuid NOT NULL, operation_id uuid NOT NULL,
|
||||
plan_id uuid NOT NULL, plan_hash text NOT NULL, principal text NOT NULL,
|
||||
session_hash text NOT NULL, approval_origin text NOT NULL CHECK(approval_origin='dev_test_browser_simulation'),
|
||||
testsimulation boolean NOT NULL CHECK(testsimulation), policy_version integer NOT NULL CHECK(policy_version=1),
|
||||
created_at timestamptz NOT NULL DEFAULT now(), revoked_at timestamptz,
|
||||
PRIMARY KEY(tenant_id,authorization_id), UNIQUE(tenant_id,operation_id),
|
||||
FOREIGN KEY(tenant_id,operation_id) REFERENCES approval_requests(tenant_id,operation_id),
|
||||
FOREIGN KEY(tenant_id,plan_id,plan_hash) REFERENCES operation_plans(tenant_id,plan_id,plan_hash)
|
||||
);
|
||||
CREATE TABLE operations (
|
||||
tenant_id uuid NOT NULL, operation_id uuid NOT NULL, authorization_id uuid NOT NULL,
|
||||
PRIMARY KEY(tenant_id,operation_id),
|
||||
FOREIGN KEY(tenant_id,operation_id) REFERENCES approval_requests(tenant_id,operation_id),
|
||||
FOREIGN KEY(tenant_id,authorization_id) REFERENCES authorizations(tenant_id,authorization_id)
|
||||
);
|
||||
CREATE TABLE jobs (
|
||||
tenant_id uuid NOT NULL, job_id uuid NOT NULL, attempt_id uuid NOT NULL, operation_id uuid NOT NULL,
|
||||
agent_id uuid NOT NULL, asset_id uuid NOT NULL, payload bytea NOT NULL, payload_hash text NOT NULL,
|
||||
state text NOT NULL DEFAULT 'queued' CHECK(state IN ('queued','accepted','running','verifying','awaiting_reboot','succeeded','failed','cancelled','expired','unknown')),
|
||||
revision bigint NOT NULL DEFAULT 0, journal_proof text, result jsonb,
|
||||
cancel_requested boolean NOT NULL DEFAULT false, start_reserved boolean NOT NULL DEFAULT false,
|
||||
resource text NOT NULL, PRIMARY KEY(tenant_id,job_id), UNIQUE(tenant_id,job_id,attempt_id),
|
||||
FOREIGN KEY(tenant_id,operation_id) REFERENCES operations(tenant_id,operation_id),
|
||||
FOREIGN KEY(tenant_id,agent_id) REFERENCES agent_installations(tenant_id,agent_id),
|
||||
FOREIGN KEY(tenant_id,asset_id) REFERENCES assets(tenant_id,asset_id)
|
||||
);
|
||||
CREATE TABLE outbox (
|
||||
tenant_id uuid NOT NULL, job_id uuid NOT NULL, attempt_id uuid NOT NULL,
|
||||
acknowledged_at timestamptz, lease_until timestamptz,
|
||||
PRIMARY KEY(tenant_id,job_id),
|
||||
FOREIGN KEY(tenant_id,job_id,attempt_id) REFERENCES jobs(tenant_id,job_id,attempt_id)
|
||||
);
|
||||
CREATE TABLE execution_permits (
|
||||
tenant_id uuid NOT NULL, grant_id uuid NOT NULL, job_id uuid NOT NULL, attempt_id uuid NOT NULL,
|
||||
payload bytea NOT NULL, signature bytea NOT NULL, created_at timestamptz NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY(tenant_id,grant_id), UNIQUE(tenant_id,job_id,attempt_id),
|
||||
FOREIGN KEY(tenant_id,job_id,attempt_id) REFERENCES jobs(tenant_id,job_id,attempt_id)
|
||||
);
|
||||
CREATE TABLE job_receipts (
|
||||
tenant_id uuid NOT NULL, agent_id uuid NOT NULL, message_id uuid NOT NULL, payload_hash text NOT NULL,
|
||||
PRIMARY KEY(tenant_id,message_id), FOREIGN KEY(tenant_id,agent_id) REFERENCES agent_installations(tenant_id,agent_id)
|
||||
);
|
||||
CREATE TABLE audit_events (
|
||||
tenant_id uuid NOT NULL REFERENCES tenants, event_id uuid NOT NULL, principal text NOT NULL,
|
||||
event_type text NOT NULL, data jsonb NOT NULL, created_at timestamptz NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY(tenant_id,event_id)
|
||||
);
|
||||
DO $$ DECLARE t text; BEGIN
|
||||
FOREACH t IN ARRAY ARRAY['operation_plans','approval_requests','authorizations','operations','jobs','outbox','execution_permits','job_receipts','audit_events'] LOOP
|
||||
EXECUTE format('ALTER TABLE %I ENABLE ROW LEVEL SECURITY',t);
|
||||
EXECUTE format('ALTER TABLE %I FORCE ROW LEVEL SECURITY',t);
|
||||
EXECUTE format('CREATE POLICY tenant_scope ON %I USING (tenant_id::text=current_setting(''app.tenant_id'',true)) WITH CHECK (tenant_id::text=current_setting(''app.tenant_id'',true))',t);
|
||||
EXECUTE format('GRANT SELECT, INSERT ON %I TO rmm_app',t);
|
||||
END LOOP;
|
||||
END $$;
|
||||
GRANT UPDATE(approval_state) ON approval_requests TO rmm_app;
|
||||
GRANT UPDATE(revoked_at) ON authorizations TO rmm_app;
|
||||
GRANT UPDATE(state,revision,journal_proof,result,cancel_requested,start_reserved) ON jobs TO rmm_app;
|
||||
GRANT UPDATE(acknowledged_at,lease_until) ON outbox TO rmm_app;
|
||||
-- No UPDATE/DELETE grant on immutable payloads, grants or audit. Also reject an
|
||||
-- accidental future broad application UPDATE grant on the immutable plan table.
|
||||
CREATE FUNCTION immutable_plan() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'immutable plan'; END $$;
|
||||
CREATE TRIGGER immutable_plan BEFORE UPDATE OR DELETE ON operation_plans FOR EACH ROW EXECUTE FUNCTION immutable_plan();
|
||||
@@ -0,0 +1,10 @@
|
||||
CREATE TABLE live_query_requests (
|
||||
tenant_id uuid NOT NULL, principal text NOT NULL, idempotency_key text NOT NULL,
|
||||
request_hash text NOT NULL, plan_id uuid NOT NULL,
|
||||
PRIMARY KEY(tenant_id,principal,idempotency_key),
|
||||
FOREIGN KEY(tenant_id,plan_id) REFERENCES operation_plans(tenant_id,plan_id)
|
||||
);
|
||||
ALTER TABLE live_query_requests ENABLE ROW LEVEL SECURITY;
|
||||
ALTER TABLE live_query_requests FORCE ROW LEVEL SECURITY;
|
||||
CREATE POLICY tenant_scope ON live_query_requests USING (tenant_id::text=current_setting('app.tenant_id',true)) WITH CHECK (tenant_id::text=current_setting('app.tenant_id',true));
|
||||
GRANT SELECT, INSERT ON live_query_requests TO rmm_app;
|
||||
@@ -0,0 +1,21 @@
|
||||
-- A plan has one request, but every caller/key still needs its own immutable
|
||||
-- idempotency proof when it deduplicates against an already submitted plan.
|
||||
CREATE TABLE operation_submissions (
|
||||
tenant_id uuid NOT NULL, principal text NOT NULL, idempotency_key text NOT NULL,
|
||||
request_hash text NOT NULL, operation_id uuid NOT NULL,
|
||||
PRIMARY KEY(tenant_id,principal,idempotency_key),
|
||||
FOREIGN KEY(tenant_id,operation_id) REFERENCES approval_requests(tenant_id,operation_id)
|
||||
);
|
||||
-- Backfill old proofs with explicit transaction-local tenant context, including
|
||||
-- FORCE-RLS tables. The migration identity does not need a BYPASSRLS exception.
|
||||
DO $$ DECLARE tenant uuid; BEGIN
|
||||
FOR tenant IN SELECT tenant_id FROM tenants LOOP
|
||||
PERFORM set_config('app.tenant_id',tenant::text,true);
|
||||
INSERT INTO operation_submissions SELECT tenant_id,principal,idempotency_key,request_hash,operation_id FROM approval_requests;
|
||||
END LOOP;
|
||||
PERFORM set_config('app.tenant_id','',true);
|
||||
END $$;
|
||||
ALTER TABLE operation_submissions ENABLE ROW LEVEL SECURITY;
|
||||
ALTER TABLE operation_submissions FORCE ROW LEVEL SECURITY;
|
||||
CREATE POLICY tenant_scope ON operation_submissions USING (tenant_id::text=current_setting('app.tenant_id',true)) WITH CHECK (tenant_id::text=current_setting('app.tenant_id',true));
|
||||
GRANT SELECT, INSERT ON operation_submissions TO rmm_app;
|
||||
@@ -0,0 +1,47 @@
|
||||
ALTER TABLE agent_installations ADD COLUMN transport text NOT NULL DEFAULT 'wss' CHECK(transport IN ('wss','https'));
|
||||
ALTER TABLE agent_installations ADD COLUMN connection_boot_id uuid;
|
||||
ALTER TABLE agent_installations ADD COLUMN last_sequence bigint NOT NULL DEFAULT 0;
|
||||
ALTER TABLE agent_installations ADD COLUMN created_at timestamptz NOT NULL DEFAULT now();
|
||||
ALTER TABLE agent_installations ADD COLUMN restore_blocked boolean NOT NULL DEFAULT false;
|
||||
ALTER TABLE agent_installations ADD COLUMN ingress_window timestamptz NOT NULL DEFAULT now();
|
||||
ALTER TABLE agent_installations ADD COLUMN ingress_count integer NOT NULL DEFAULT 0;
|
||||
GRANT UPDATE(restore_blocked,ingress_window,ingress_count) ON agent_installations TO rmm_app;
|
||||
ALTER TABLE agent_installations ADD COLUMN previous_certificate_sha256 text;
|
||||
ALTER TABLE agent_installations ADD COLUMN previous_certificate_until timestamptz;
|
||||
ALTER TABLE agent_installations ADD COLUMN certificate_pem text;
|
||||
ALTER TABLE agent_installations ADD COLUMN renewal_csr_sha256 text;
|
||||
GRANT UPDATE(transport,connection_boot_id,last_sequence,previous_certificate_sha256,previous_certificate_until,certificate_pem,renewal_csr_sha256,certificate_sha256,revoked_at) ON agent_installations TO rmm_app;
|
||||
GRANT UPDATE(changes_stopped) ON tenants TO rmm_app;
|
||||
CREATE OR REPLACE FUNCTION device_binding(fingerprint text)
|
||||
RETURNS TABLE(tenant_id uuid, agent_id uuid)
|
||||
LANGUAGE sql SECURITY DEFINER SET search_path = pg_catalog, public AS $$
|
||||
SELECT a.tenant_id,a.agent_id FROM public.agent_installations a
|
||||
WHERE a.revoked_at IS NULL AND (a.certificate_sha256=fingerprint OR
|
||||
(a.previous_certificate_sha256=fingerprint AND a.previous_certificate_until>now()));
|
||||
$$;
|
||||
CREATE TABLE memberships (
|
||||
tenant_id uuid NOT NULL REFERENCES tenants, principal text NOT NULL,
|
||||
kind text NOT NULL CHECK(kind IN ('viewer','automation','test_human')),
|
||||
permissions text[] NOT NULL, revoked_at timestamptz,
|
||||
PRIMARY KEY(tenant_id,principal)
|
||||
);
|
||||
INSERT INTO memberships SELECT tenant_id,'viewer:'||tenant_id,'viewer',ARRAY['inventory:read'],NULL FROM tenants;
|
||||
INSERT INTO memberships SELECT tenant_id,'automation:'||tenant_id,'automation',ARRAY['inventory:read','logs:read','operations:read','operations:plan','operations:submit','jobs:cancel','scripts:execute'],NULL FROM tenants;
|
||||
INSERT INTO memberships VALUES('11111111-1111-4111-8111-111111111111','simulated-testprincipal-a','test_human',ARRAY['inventory:read','logs:read','operations:read','operations:plan','operations:submit','jobs:cancel','scripts:execute','plans:approve','plans:revoke','devices:revoke','tenant:stop','catalog:write'],NULL);
|
||||
ALTER TABLE memberships ENABLE ROW LEVEL SECURITY;
|
||||
ALTER TABLE memberships FORCE ROW LEVEL SECURITY;
|
||||
CREATE POLICY tenant_scope ON memberships USING(tenant_id::text=current_setting('app.tenant_id',true)) WITH CHECK(tenant_id::text=current_setting('app.tenant_id',true));
|
||||
GRANT SELECT ON memberships TO rmm_app;
|
||||
-- Registry is intentionally explicit in this development profile; no worker RLS bypass.
|
||||
CREATE TABLE tenant_limits (
|
||||
tenant_id uuid PRIMARY KEY REFERENCES tenants,
|
||||
starts_per_hour integer NOT NULL DEFAULT 100 CHECK(starts_per_hour BETWEEN 1 AND 10000),
|
||||
max_parallel integer NOT NULL DEFAULT 10 CHECK(max_parallel BETWEEN 1 AND 100)
|
||||
);
|
||||
INSERT INTO tenant_limits(tenant_id) SELECT tenant_id FROM tenants;
|
||||
ALTER TABLE tenant_limits ENABLE ROW LEVEL SECURITY;
|
||||
ALTER TABLE tenant_limits FORCE ROW LEVEL SECURITY;
|
||||
CREATE POLICY tenant_scope ON tenant_limits USING(tenant_id::text=current_setting('app.tenant_id',true)) WITH CHECK(tenant_id::text=current_setting('app.tenant_id',true));
|
||||
GRANT SELECT ON tenant_limits TO rmm_app;
|
||||
ALTER TABLE jobs ADD COLUMN pending_reason text;
|
||||
GRANT UPDATE(pending_reason) ON jobs TO rmm_app;
|
||||
@@ -0,0 +1,22 @@
|
||||
CREATE TABLE package_recipes (
|
||||
tenant_id uuid NOT NULL REFERENCES tenants, recipe_id uuid NOT NULL,
|
||||
package text NOT NULL, package_version text NOT NULL, architecture text NOT NULL CHECK(architecture='amd64'),
|
||||
recipe_version text NOT NULL, payload bytea NOT NULL, payload_hash text NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY(tenant_id,recipe_id), UNIQUE(tenant_id,package,package_version,architecture),
|
||||
UNIQUE(tenant_id,package,package_version,architecture,recipe_version)
|
||||
);
|
||||
CREATE TABLE artifacts (
|
||||
tenant_id uuid NOT NULL REFERENCES tenants, artifact_id uuid NOT NULL, sha256 text NOT NULL,
|
||||
size_bytes bigint NOT NULL CHECK(size_bytes BETWEEN 1 AND 262144), media_type text NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY(tenant_id,artifact_id)
|
||||
);
|
||||
DO $$ DECLARE t text; BEGIN
|
||||
FOREACH t IN ARRAY ARRAY['package_recipes','artifacts'] LOOP
|
||||
EXECUTE format('ALTER TABLE %I ENABLE ROW LEVEL SECURITY',t);
|
||||
EXECUTE format('ALTER TABLE %I FORCE ROW LEVEL SECURITY',t);
|
||||
EXECUTE format('CREATE POLICY tenant_scope ON %I USING (tenant_id::text=current_setting(''app.tenant_id'',true)) WITH CHECK (tenant_id::text=current_setting(''app.tenant_id'',true))',t);
|
||||
EXECUTE format('GRANT SELECT, INSERT ON %I TO rmm_app',t);
|
||||
EXECUTE format('CREATE TRIGGER immutable_content BEFORE UPDATE OR DELETE ON %I FOR EACH ROW EXECUTE FUNCTION immutable_plan()',t);
|
||||
END LOOP;
|
||||
END $$;
|
||||
@@ -0,0 +1,26 @@
|
||||
ALTER TABLE agent_installations ADD COLUMN sync_required boolean NOT NULL DEFAULT false;
|
||||
ALTER TABLE agent_installations ADD COLUMN sync_ready boolean NOT NULL DEFAULT false;
|
||||
ALTER TABLE agent_installations ADD COLUMN helper_sync_ready boolean NOT NULL DEFAULT false;
|
||||
ALTER TABLE agent_installations ADD COLUMN sequence_regressed boolean NOT NULL DEFAULT false;
|
||||
GRANT UPDATE(sync_required,sync_ready,helper_sync_ready,sequence_regressed) ON agent_installations TO rmm_app;
|
||||
CREATE TABLE agent_sync_state (
|
||||
tenant_id uuid NOT NULL, agent_id uuid NOT NULL, journal_id uuid NOT NULL,
|
||||
checkpoint bigint NOT NULL, decision_id uuid NOT NULL,
|
||||
helper_journal_id uuid, helper_checkpoint bigint NOT NULL DEFAULT 0,
|
||||
PRIMARY KEY(tenant_id,agent_id), FOREIGN KEY(tenant_id,agent_id) REFERENCES agent_installations(tenant_id,agent_id)
|
||||
);
|
||||
CREATE TABLE sync_checkpoints (
|
||||
tenant_id uuid NOT NULL, agent_id uuid NOT NULL, request_id uuid NOT NULL,
|
||||
decision_id uuid NOT NULL, request_hash text NOT NULL, journal_id uuid NOT NULL,
|
||||
checkpoint bigint NOT NULL, connection_generation bigint NOT NULL,
|
||||
payload bytea NOT NULL, signature bytea NOT NULL, created_at timestamptz NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY(tenant_id,agent_id,request_id), UNIQUE(tenant_id,agent_id,decision_id),
|
||||
FOREIGN KEY(tenant_id,agent_id) REFERENCES agent_installations(tenant_id,agent_id)
|
||||
);
|
||||
DO $$ DECLARE t text; BEGIN FOREACH t IN ARRAY ARRAY['agent_sync_state','sync_checkpoints'] LOOP
|
||||
EXECUTE format('ALTER TABLE %I ENABLE ROW LEVEL SECURITY',t);
|
||||
EXECUTE format('ALTER TABLE %I FORCE ROW LEVEL SECURITY',t);
|
||||
EXECUTE format('CREATE POLICY tenant_scope ON %I USING(tenant_id::text=current_setting(''app.tenant_id'',true)) WITH CHECK(tenant_id::text=current_setting(''app.tenant_id'',true))',t);
|
||||
EXECUTE format('GRANT SELECT, INSERT ON %I TO rmm_app',t);
|
||||
END LOOP;END $$;
|
||||
GRANT UPDATE(checkpoint,decision_id,helper_checkpoint,helper_journal_id) ON agent_sync_state TO rmm_app;
|
||||
@@ -0,0 +1,7 @@
|
||||
-- A dedicated dev test principal permission is not an enablement switch:
|
||||
-- root additionally requires a private expiring selected-device policy, exact
|
||||
-- independently provisioned helper-policy capabilities and successful sync.
|
||||
SELECT set_config('app.tenant_id','11111111-1111-4111-8111-111111111111',true);
|
||||
UPDATE memberships SET permissions=array_append(permissions,'root:execute')
|
||||
WHERE principal='simulated-testprincipal-a' AND NOT('root:execute'=ANY(permissions));
|
||||
SELECT set_config('app.tenant_id','',true);
|
||||
@@ -0,0 +1,5 @@
|
||||
-- Missing authoritative jobs/attempts cannot be forgotten by omitting them in a
|
||||
-- later "complete" journal summary. Only controlled identity recovery can reset
|
||||
-- this fault; sync itself is deliberately not granted that authority.
|
||||
ALTER TABLE agent_installations ADD COLUMN sync_recovery_required boolean NOT NULL DEFAULT false;
|
||||
GRANT UPDATE(sync_recovery_required) ON agent_installations TO rmm_app;
|
||||
@@ -0,0 +1,4 @@
|
||||
SELECT set_config('app.tenant_id','11111111-1111-4111-8111-111111111111',true);
|
||||
UPDATE memberships SET permissions=array_append(permissions,'apt:native_acceptance')
|
||||
WHERE principal='simulated-testprincipal-a' AND NOT('apt:native_acceptance'=ANY(permissions));
|
||||
SELECT set_config('app.tenant_id','',true);
|
||||
@@ -0,0 +1,47 @@
|
||||
-- Global PREAUTH storage, managed by the migration identity, not application DDL.
|
||||
CREATE SCHEMA rmm_oidc_auth;
|
||||
REVOKE ALL ON SCHEMA rmm_oidc_auth FROM PUBLIC;
|
||||
CREATE TABLE rmm_oidc_auth.login_states (
|
||||
state_hash bytea PRIMARY KEY CHECK(octet_length(state_hash)=32),
|
||||
binding_hash bytea NOT NULL CHECK(octet_length(binding_hash)=32),
|
||||
expires_at bigint NOT NULL CHECK(expires_at>0),
|
||||
created_at bigint NOT NULL DEFAULT floor(extract(epoch FROM clock_timestamp()))::bigint,
|
||||
encryption_key_ref text NOT NULL CHECK(length(encryption_key_ref) BETWEEN 1 AND 256),
|
||||
encryption_nonce bytea NOT NULL CHECK(octet_length(encryption_nonce)=12),
|
||||
ciphertext bytea NOT NULL CHECK(octet_length(ciphertext) BETWEEN 16 AND 4096),
|
||||
CHECK(expires_at>created_at AND expires_at<=created_at+600)
|
||||
);
|
||||
CREATE INDEX login_states_expiry ON rmm_oidc_auth.login_states(expires_at,state_hash);
|
||||
REVOKE ALL ON rmm_oidc_auth.login_states FROM PUBLIC;
|
||||
-- The administrator grants these privileges to a SEPARATE login identity later.
|
||||
-- No automatic role/password creation or grants to the tenant application role.
|
||||
|
||||
CREATE TABLE public.oidc_principals (
|
||||
principal text PRIMARY KEY CHECK(principal ~ '^oidc:[0-9a-f]{64}$'),
|
||||
issuer text NOT NULL, subject text NOT NULL CHECK(length(subject) BETWEEN 1 AND 1024),
|
||||
tenant_ids uuid[] NOT NULL CHECK(cardinality(tenant_ids) BETWEEN 1 AND 100),
|
||||
disabled boolean NOT NULL DEFAULT false,
|
||||
UNIQUE(issuer,subject)
|
||||
);
|
||||
REVOKE ALL ON public.oidc_principals FROM PUBLIC;
|
||||
-- This index is administrator-provisioned routing metadata, NOT membership/roles.
|
||||
-- Current membership must still be checked in each selected tenant transaction.
|
||||
CREATE FUNCTION public.oidc_identity_binding(issuer_value text, subject_value text)
|
||||
RETURNS TABLE(principal text,tenant_ids uuid[])
|
||||
LANGUAGE sql SECURITY DEFINER SET search_path=pg_catalog,public AS $$
|
||||
SELECT p.principal,p.tenant_ids FROM public.oidc_principals p
|
||||
WHERE p.issuer=issuer_value AND p.subject=subject_value AND NOT p.disabled;
|
||||
$$;
|
||||
CREATE FUNCTION public.oidc_principal_enabled(principal_value text) RETURNS boolean
|
||||
LANGUAGE sql SECURITY DEFINER SET search_path=pg_catalog,public AS $$
|
||||
SELECT EXISTS(SELECT 1 FROM public.oidc_principals p WHERE p.principal=principal_value AND NOT p.disabled);
|
||||
$$;
|
||||
REVOKE ALL ON FUNCTION public.oidc_identity_binding(text,text),public.oidc_principal_enabled(text) FROM PUBLIC;
|
||||
GRANT EXECUTE ON FUNCTION public.oidc_identity_binding(text,text),public.oidc_principal_enabled(text) TO rmm_app;
|
||||
ALTER TABLE memberships DROP CONSTRAINT memberships_kind_check;
|
||||
ALTER TABLE memberships ADD CONSTRAINT memberships_kind_check CHECK(kind IN ('viewer','automation','test_human','human'));
|
||||
ALTER TABLE authorizations DROP CONSTRAINT authorizations_approval_origin_check;
|
||||
ALTER TABLE authorizations DROP CONSTRAINT authorizations_testsimulation_check;
|
||||
ALTER TABLE authorizations ADD CONSTRAINT authorization_interactive_origin CHECK(
|
||||
(approval_origin='dev_test_browser_simulation' AND testsimulation) OR
|
||||
(approval_origin='human_webui' AND NOT testsimulation));
|
||||
@@ -0,0 +1,28 @@
|
||||
-- Issued/registered certificate identity is not evidence that it was used.
|
||||
-- Do not infer/backfill authentication from last_contact_at or the current pin.
|
||||
ALTER TABLE agent_installations
|
||||
ADD COLUMN last_authenticated_certificate_sha256 text
|
||||
CHECK(last_authenticated_certificate_sha256 ~ '^[0-9a-f]{64}$'),
|
||||
ADD COLUMN last_authenticated_at timestamptz,
|
||||
ADD CONSTRAINT authentication_evidence_pair CHECK
|
||||
((last_authenticated_certificate_sha256 IS NULL) = (last_authenticated_at IS NULL));
|
||||
|
||||
-- Every caller passes only the leaf DER SHA-256 obtained from verified rustls
|
||||
-- peer identity, never an HTTP header/agent payload. Keep binding and evidence
|
||||
-- atomic so revocation/expired overlap cannot leave a false authentication row.
|
||||
-- This narrowly scoped SECURITY DEFINER function already owns identity lookup;
|
||||
-- do not grant the application general UPDATE rights on the evidence columns.
|
||||
CREATE OR REPLACE FUNCTION device_binding(fingerprint text)
|
||||
RETURNS TABLE(tenant_id uuid, agent_id uuid)
|
||||
LANGUAGE sql VOLATILE SECURITY DEFINER SET search_path = pg_catalog, public AS $$
|
||||
UPDATE public.agent_installations AS a
|
||||
SET last_authenticated_certificate_sha256=fingerprint,
|
||||
last_authenticated_at=clock_timestamp()
|
||||
WHERE a.revoked_at IS NULL
|
||||
AND fingerprint ~ '^[0-9a-f]{64}$'
|
||||
AND (a.certificate_sha256=fingerprint OR
|
||||
(a.previous_certificate_sha256=fingerprint AND a.previous_certificate_until>clock_timestamp()))
|
||||
RETURNING a.tenant_id,a.agent_id;
|
||||
$$;
|
||||
REVOKE ALL ON FUNCTION device_binding(text) FROM PUBLIC;
|
||||
GRANT EXECUTE ON FUNCTION device_binding(text) TO rmm_app;
|
||||
Reference in New Issue
Block a user