Files
BuTzZ 6c3203d725
Rust development slice / linux (push) Failing after 1m10s
feat: snapshot Linux-first RMM MVP and pause handover
2026-11-12 22:06:25 +01:00

171 lines
15 KiB
Python

#!/usr/bin/env python3
"""Local TLS/PostgreSQL backend contracts; no VM, no approval or execution.
Creates a clearly synthetic device fixture and pending requests, then revokes the
fixture. Never calls authorization with browser credentials or writes approvals.
"""
import datetime
import hashlib
import json
import secrets
import uuid
import subprocess
import tempfile
from pathlib import Path
from local_integration import LocalHTTPS, WebSocket, field, envelope, token as enrollment_token, CA, STATE, A, B, sql, check
import ssl
def request(path, method="GET", body=None, token=None, extra=None):
c = LocalHTTPS("rmm-dev.internal", 18443, context=ssl.create_default_context(cafile=str(CA)), timeout=10)
h = {"Content-Type": "application/json", **(extra or {})}
if token:
h["Authorization"] = "Bearer " + token
c.request(method, path, body=json.dumps(body) if body is not None else None, headers=h)
r = c.getresponse()
status, headers = r.status, dict(r.getheaders())
if "text/event-stream" in headers.get("content-type", headers.get("Content-Type", "")):
data = None
while True:
line = r.readline()
if not line:
break
if line.startswith(b"data: ") and line[6:].strip():
parsed = json.loads(line[6:])
if "result" in parsed or "error" in parsed:
data = parsed
break
else:
raw = r.read()
data = (raw.decode() if "text/html" in headers.get("content-type", headers.get("Content-Type", "")) else json.loads(raw)) if raw else None
c.close()
return status, headers, data
def main():
operator = (STATE / "operator-a.token").read_text().strip()
viewer = (STATE / "viewer-a.token").read_text().strip()
operator_b = (STATE / "operator-b.token").read_text().strip()
asset, agent = str(uuid.uuid4()), str(uuid.uuid4())
sql(f"INSERT INTO assets VALUES('{A}','{asset}'); INSERT INTO agent_installations(tenant_id,agent_id,asset_id,certificate_sha256,capabilities) VALUES('{A}','{agent}','{asset}','{secrets.token_hex(32)}','[\"run_script\",\"query_logs\"]');")
base = f"/api/v1/tenants/{A}"
try:
now = datetime.datetime.now(datetime.timezone.utc)
end = now + datetime.timedelta(hours=1)
plan_input = {"tenant_id": A, "action": "run_script", "targets": {"asset_ids": [asset]}, "parameters": {"variants": [{"os_family": "linux", "interpreter": "posix_sh", "encoding": "utf-8", "script_utf8": "printf 'Hello World\\n'\n"}], "arguments": [], "execution_identity": "agent_service", "timeout_seconds": 60, "max_output_bytes": 65536, "expected_stdout": "Hello World\n"}, "schedule": {"not_before": now.isoformat(), "start_before": end.isoformat(), "offline_policy": "wait_until_deadline"}, "rollout": {"max_parallel": 1, "halt_after_failures": 1}, "reason": "synthetic backend contract fixture; never execute"}
before = sql(f"SELECT (SELECT count(*) FROM jobs WHERE agent_id='{agent}')||':'||(SELECT count(*) FROM outbox b JOIN jobs j USING(tenant_id,job_id,attempt_id) WHERE j.agent_id='{agent}')||':'||(SELECT count(*) FROM execution_permits e JOIN jobs j USING(tenant_id,job_id,attempt_id) WHERE j.agent_id='{agent}');")
check(request(base + "/operation-plans", "POST", plan_input, viewer)[0] == 403, "viewer cannot plan")
status, _, plan = request(base + "/operation-plans", "POST", plan_input, operator)
check(status == 200, "operator creates immutable UI-shaped plan")
pid = plan["plan_id"]
check(hashlib.sha256(bytes(plan["plan_payload_bytes"])).hexdigest() == plan["plan_hash"], "plan hash binds stored exact bytes")
check(plan["jobs"][0]["asset_id"] == asset and plan["resolved_targets"][0]["asset_id"] == asset and plan["excluded_targets"] == [], "UI gets complete fixed target/job previews")
check(request(base + f"/operation-plans/{pid}", token=operator_b)[0] == 404, "foreign plan indistinguishable from missing")
key = "backend-" + uuid.uuid4().hex
status, _, pending = request(base + f"/operation-plans/{pid}/submit", "POST", {"idempotency_key": key}, operator)
check(status == 200 and pending["approval_state"] == "pending_approval" and pending["job_count"] == 0 and pending["execution_state"] is None, "submit persists only pending approval with zero jobs")
again = request(base + f"/operation-plans/{pid}/submit", "POST", {"idempotency_key": key}, operator)[2]
check(again["operation_id"] == pending["operation_id"], "submit redelivery preserves operation identity")
detail = request(base + f"/operation-plans/{pid}", token=operator)[2]
check(detail["approval_state"] == "pending_approval", "plan reload exposes pending request to UI")
p2 = request(base + "/operation-plans", "POST", plan_input, operator)[2]
alias_key="alias-"+uuid.uuid4().hex
check(request(base + f"/operation-plans/{pid}/submit", "POST", {"idempotency_key": alias_key}, operator)[2]["operation_id"]==pending["operation_id"],"new caller key deduplicates against the same submitted plan")
alias_conflict=request(base + f"/operation-plans/{p2['plan_id']}/submit", "POST", {"idempotency_key": alias_key}, operator)
check(alias_conflict[0]==409 and alias_conflict[2]["code"]=="IDEMPOTENCY_CONFLICT","deduplicated submit still durably binds its new key")
conflict = request(base + f"/operation-plans/{p2['plan_id']}/submit", "POST", {"idempotency_key": key}, operator)
check(conflict[0] == 409 and conflict[2]["code"] == "IDEMPOTENCY_CONFLICT", "same key changed plan conflicts")
check(request(base + f"/operation-plans/{pid}/authorizations", "POST", {"plan_hash": plan["plan_hash"]}, operator)[0] == 403, "operator bearer cannot approve")
after = sql(f"SELECT (SELECT count(*) FROM jobs WHERE agent_id='{agent}')||':'||(SELECT count(*) FROM outbox b JOIN jobs j USING(tenant_id,job_id,attempt_id) WHERE j.agent_id='{agent}')||':'||(SELECT count(*) FROM execution_permits e JOIN jobs j USING(tenant_id,job_id,attempt_id) WHERE j.agent_id='{agent}');")
check(before == after, "no jobs/outbox/grants created before browser approval")
sql(f"BEGIN; SELECT set_config('app.tenant_id','{A}',true); UPDATE operation_plans SET payload='changed' WHERE plan_id='{pid}';", "rmm_app", fail=True)
check(sql("SELECT count(*) FROM operation_plans;", "rmm_app") == "0", "new plan RLS denies missing context")
check(sql(f"BEGIN; SELECT set_config('app.tenant_id','{B}',true); SELECT count(*) FROM operation_plans WHERE plan_id='{pid}'; ROLLBACK;", "rmm_app").splitlines()[-1] == "0", "new plan RLS blocks cross tenant")
log_query={"tenant_id":A,"asset_id":asset,"unit":"ssh.service","since":(now-datetime.timedelta(minutes=5)).isoformat(),"until":now.isoformat(),"max_lines":20,"live":True,"idempotency_key":"logs-"+uuid.uuid4().hex}
status,_,logs_pending=request(base+"/log-queries","POST",log_query,operator)
check(status==200 and logs_pending["approval_state"]=="pending_approval" and logs_pending["job_count"]==0,"live logs create only a fixed pending request")
check(request(base+"/log-queries","POST",log_query,operator)[2]["operation_id"]==logs_pending["operation_id"],"live logs retry preserves fixed plan/request identity")
log_query["max_lines"]=21
check(request(base+"/log-queries","POST",log_query,operator)[2]["code"]=="IDEMPOTENCY_CONFLICT","live logs changed parameters conflict with same key")
log_query["live"]=False
check(request(base+"/log-queries","POST",log_query,viewer)[0]==403,"viewer cannot read operator-scoped logs")
# Dedicated login is tested only for session/CSRF. No browser approval.
login = json.loads((STATE / "browser-login.json").read_text())
check(request("/auth/dev/login", "POST", login)[0] == 403, "login denies missing Origin")
origin = {"Origin": "https://rmm-dev.internal:18443"}
status, headers, _ = request("/auth/dev/login", "POST", login, extra=origin)
cookie = headers.get("set-cookie", headers.get("Set-Cookie", ""))
check(status == 200 and all(x in cookie for x in ["HttpOnly", "Secure", "SameSite=Strict", "__Host-"]), "dedicated interactive secure cookie")
browser = {"Cookie": cookie.split(";")[0]}
session = request("/auth/session", extra=browser)[2]
check(session["testsimulation"] is True and session["csrf_token"], "session explicitly labels test simulation")
check(request(base + "/operation-plans", "POST", plan_input, extra={**origin, **browser})[0] == 403, "browser mutations require CSRF")
csrf = {**browser, **origin, "X-CSRF-Token": session["csrf_token"]}
root_input=json.loads(json.dumps(plan_input));root_input["parameters"]["execution_identity"]="root"
root_denied=request(base+"/operation-plans","POST",root_input,extra=csrf)
check(root_denied[0]==409 and root_denied[2]["code"]=="UNSUPPORTED_CAPABILITY","even dev human root permission cannot enable root without selected verified-helper policy")
check(request(base + f"/operation-plans/{pid}/authorizations", "POST", {"plan_hash": plan["plan_hash"]}, operator, csrf)[0] == 403, "bearer cannot borrow an interactive cookie's approval rights")
check(request("/auth/logout", "POST", {}, extra={**csrf,"Origin":"https://wrong.internal"})[0] == 403, "valid CSRF cannot bypass Origin check")
check(request("/auth/logout", "POST", {}, extra=csrf)[0] == 204, "CSRF protected logout")
check(request("/auth/session", extra=browser)[0] == 401, "logout revokes session")
check(request("/", token=None)[0] == 200, "same-origin compiled frontend served")
# rmcp real Streamable HTTP lifecycle, sessions and tenant checks.
h = {"Accept": "application/json, text/event-stream", "MCP-Protocol-Version": "2025-11-25"}
init = {"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2025-11-25", "capabilities": {}, "clientInfo": {"name": "rmm-backend-contract", "version": "1"}}}
status, headers, response = request("/mcp", "POST", init, operator, h)
check(status == 200 and response["result"]["protocolVersion"] == "2025-11-25", "rmcp Streamable HTTP negotiates pinned protocol")
session_id = headers.get("mcp-session-id", headers.get("Mcp-Session-Id"))
if session_id:
h["Mcp-Session-Id"] = session_id
check(request("/mcp", "POST", {"jsonrpc": "2.0", "method": "notifications/initialized"}, operator, h)[0] == 202, "MCP initialized notification")
result = request("/mcp", "POST", {"jsonrpc": "2.0", "id": 2, "method": "tools/list", "params": {}}, operator, h)[2]
names = [x["name"] for x in result["result"]["tools"]]
check(len(names) == 10 and not any("approv" in n or "authoriz" in n for n in names), "ten real MCP tools and no approval tool")
call = {"jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": {"name": "inventory.list_devices", "arguments": {"tenant_id": B}}}
result = request("/mcp", "POST", call, operator, h)[2]["result"]
check(result["isError"] and result["structuredContent"]["code"] == "NOT_FOUND", "MCP enforces same tenant boundary")
call["params"] = {"name": "jobs.get", "arguments": {"tenant_id": A, "operation_id": pending["operation_id"]}}
result = request("/mcp", "POST", call, operator, h)[2]["result"]["structuredContent"]
check(result["approval_state"] == "pending_approval" and result["job_count"] == 0, "MCP jobs.get sees same persisted pending request")
call["params"]={"name":"operations.plan","arguments":{"tenant_id":A,"input":plan_input}}
mcp_plan=request("/mcp","POST",call,operator,h)[2]["result"]["structuredContent"]
check(mcp_plan["jobs"][0]["asset_id"]==asset,"MCP planning uses same exact DTO and fixed job contract")
call["params"]={"name":"operations.submit","arguments":{"tenant_id":A,"plan_id":mcp_plan["plan_id"],"idempotency_key":"mcp-"+uuid.uuid4().hex}}
mcp_pending=request("/mcp","POST",call,operator,h)[2]["result"]["structuredContent"]
check(mcp_pending["approval_state"]=="pending_approval" and mcp_pending["job_count"]==0,"MCP submit cannot enqueue executable work")
check(request("/mcp", "POST", init, extra=h)[0] == 401, "MCP always requires bearer, not session ID")
# Exercise the actual mTLS job gateway without approving any operation.
with tempfile.TemporaryDirectory(prefix="backend-device-", dir=STATE) as folder:
folder=Path(folder)
subprocess.run(["openssl","req","-new","-newkey","ec","-pkeyopt","ec_paramgen_curve:prime256v1","-nodes","-keyout",str(folder/"device.key"),"-out",str(folder/"device.csr"),"-subj","/CN=synthetic-backend-contract"],check=True,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
enrolled=request("/agent/v1/enroll","POST",{"token":enrollment_token(A),"csr_pem":(folder/"device.csr").read_text()})[2]
(folder/"device.pem").write_text(enrolled["certificate_pem"])
context=ssl.create_default_context(cafile=str(CA));context.load_cert_chain(folder/"device.pem",folder/"device.key")
ws=WebSocket(context)
try:
ws.send(envelope(enrolled["agent_id"],1,0,10,field(1,"backend-contract")+field(2,"run_script")))
check(ws.receive()[0]==2,"job-aware mTLS gateway negotiates and reads durable outbox")
generation=int(sql(f"SELECT connection_generation FROM agent_installations WHERE agent_id='{enrolled['agent_id']}';"))
plan_input["targets"]["asset_ids"]=[enrolled["asset_id"]]
target_plan=request(base+"/operation-plans","POST",plan_input,operator)[2]
target_pending=request(base+f"/operation-plans/{target_plan['plan_id']}/submit","POST",{"idempotency_key":"gateway-"+uuid.uuid4().hex},operator)[2]
check(target_pending["job_count"]==0,"connected capable device still has no unapproved jobs")
target_job=target_plan["jobs"][0]
permit_request=field(1,target_job["job_id"])+field(2,target_job["attempt_id"])+field(3,target_job["job_payload_hash"])+field(4,1)
ws.send(envelope(enrolled["agent_id"],2,generation,22,permit_request))
opcode,reply=ws.receive()
check(opcode==2 and b"NOT_FOUND" in reply,"preapproval start request denied with no signed grant")
check(sql(f"SELECT count(*) FROM execution_permits WHERE job_id='{target_job['job_id']}';")=="0","denied grant request leaves no start reservation")
ws.send(envelope(enrolled["agent_id"],3,generation,12,field(1,str(uuid.uuid4()))))
check(ws.receive()[0]==2,"fresh heartbeat remains available after denied start")
finally:
ws.close()
sql(f"UPDATE agent_installations SET revoked_at=now() WHERE agent_id='{enrolled['agent_id']}';")
finally:
sql(f"UPDATE agent_installations SET revoked_at=now() WHERE agent_id='{agent}';")
print("Backend local contracts passed; no browser approval, VM job, result replay or native execution acceptance performed.")
if __name__ == "__main__":
main()