83 lines
5.5 KiB
PL/PgSQL
83 lines
5.5 KiB
PL/PgSQL
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();
|