69 lines
6.7 KiB
Python
69 lines
6.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Real PostgreSQL/API cursor, scope and catalog-reference tests; no approvals."""
|
|
import datetime
|
|
import hashlib
|
|
import json
|
|
import uuid
|
|
from local_integration import STATE, A, B, sql, check
|
|
from backend_contract import request
|
|
|
|
|
|
def main():
|
|
operator=(STATE/"operator-a.token").read_text().strip()
|
|
viewer=(STATE/"viewer-a.token").read_text().strip()
|
|
other=(STATE/"operator-b.token").read_text().strip()
|
|
devices=[(str(uuid.uuid4()),str(uuid.uuid4())) for _ in range(2)]
|
|
base=f"/api/v1/tenants/{A}"
|
|
for asset,agent in devices:
|
|
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}','{uuid.uuid4().hex+uuid.uuid4().hex}','[\"ensure_package\",\"apply_updates\"]');")
|
|
try:
|
|
status,_,first=request(base+"/devices?limit=1",token=operator)
|
|
check(status==200 and len(first["items"])==1 and first["truncated"] and first["next_cursor"],"device list has bounded signed next cursor")
|
|
cursor=first["next_cursor"]
|
|
status,_,second=request(base+"/devices?cursor="+cursor,token=operator)
|
|
check(status==200 and first["as_of"]==second["as_of"] and first["items"][0]["agent_id"]!=second["items"][0]["agent_id"],"cursor preserves as-of and stable nonoverlapping order")
|
|
tampered=cursor[:-1]+("0" if cursor[-1]!="0" else "1")
|
|
check(request(base+"/devices?cursor="+tampered,token=operator)[0]==400,"cursor signature rejects tampering")
|
|
check(request(base+"/devices?cursor="+cursor,token=viewer)[0]==400,"cursor bound to authenticated principal")
|
|
check(request(base+"/operations?cursor="+cursor,token=operator)[0]==400,"cursor bound to resource/sort context")
|
|
check(request(base+"/devices?limit=501",token=operator)[0]==400,"page size maximum enforced")
|
|
check(request(f"/api/v1/tenants/{B}/devices?cursor="+cursor,token=operator)[0]==404,"foreign tenant does not expose cursor or inventory")
|
|
check(request(base+"/operations",token=viewer)[0]==403,"viewer has inventory scope but not operation-output scope")
|
|
check(sql("SELECT count(*) FROM memberships;","rmm_app")=="0","membership registry fails closed without tenant context")
|
|
# Metadata fixture only: never approve or install this synthetic package.
|
|
package="rmm-fixture-"+uuid.uuid4().hex
|
|
recipe={"schema_version":1,"provider":"apt","package":package,"version":"1.0+test1","architecture":"amd64","recipe_version":"fixture-1","installation_context":"system","reboot_policy":"never"}
|
|
payload=json.dumps(recipe,separators=(",",":")).encode(); digest=hashlib.sha256(payload).hexdigest(); recipe_id=str(uuid.uuid4())
|
|
sql(f"INSERT INTO package_recipes(tenant_id,recipe_id,package,package_version,architecture,recipe_version,payload,payload_hash) VALUES('{A}','{recipe_id}','{package}','1.0+test1','amd64','fixture-1',decode('{payload.hex()}','hex'),'{digest}');")
|
|
now=datetime.datetime.now(datetime.timezone.utc)
|
|
action={"action":"ensure_package","provider":"apt","package":package,"version":"1.0+test1","architecture":"amd64","recipe_version":"fixture-1","installation_context":"system"}
|
|
plan_input={"targets":{"asset_ids":[devices[0][0]]},"action":action,"execution_identity":"agent_service","schedule":{"not_before":now.isoformat(),"start_before":(now+datetime.timedelta(hours=1)).isoformat()},"max_runtime_seconds":60,"max_output_bytes":65536,"rollout":{"max_parallel":1,"halt_after_failures":1},"reason":"synthetic catalog binding; never install"}
|
|
status,_,plan=request(base+"/operation-plans","POST",plan_input,operator)
|
|
check(status==200 and plan["plan_payload"]["recipe_references"][0]["sha256"]==digest,"package plan hash binds immutable resolved recipe digest")
|
|
check(hashlib.sha256(bytes(plan["plan_payload_bytes"])).hexdigest()==plan["plan_hash"],"recipe references belong to exact approved-plan bytes")
|
|
check(plan["authorization_status"]=="unsupported_manifest_required","legacy catalog preview is explicitly not execution-ready")
|
|
legacy_submit=request(base+f"/operation-plans/{plan['plan_id']}/submit","POST",{"idempotency_key":"missing-manifest-"+uuid.uuid4().hex},operator)
|
|
check(legacy_submit[0]==409 and legacy_submit[2]["code"]=="APT_MANIFEST_REQUIRED","missing schema-2 manifest cannot become an approval request/execution path")
|
|
check(request(base+"/package-recipes?limit=1",token=operator)[0]==200,"immutable catalog exposed through scoped bounded list")
|
|
sql(f"BEGIN; SELECT set_config('app.tenant_id','{A}',true); UPDATE package_recipes SET payload='changed' WHERE recipe_id='{recipe_id}';","rmm_app",fail=True)
|
|
print("PASS application cannot mutate immutable recipe version")
|
|
plan_input["action"]["recipe_version"]="different"
|
|
check(request(base+"/operation-plans","POST",plan_input,operator)[2]["code"]=="RECIPE_BINDING_MISMATCH","wrong recipe version cannot silently resolve")
|
|
plan_input["action"]={"action":"apply_updates","provider":"apt","packages":[package+"=1.0+test1"],"reboot_policy":"never"}
|
|
status,_,updates=request(base+"/operation-plans","POST",plan_input,operator)
|
|
check(status==200 and updates["plan_payload"]["recipe_references"][0]["sha256"]==digest,"narrow version-pinned update selection resolves same immutable recipe")
|
|
plan_input["action"]["packages"]=[package]
|
|
check(request(base+"/operation-plans","POST",plan_input,operator)[0]==400,"unversioned broad update selection rejected")
|
|
check(request(base+"/package-recipes","POST",recipe,operator)[0]==403,"automation cannot import privileged catalog content")
|
|
check(request(base+"/artifacts","POST",{"bytes":[1,2],"media_type":"application/octet-stream"},operator)[0]==403,"automation cannot use generic artifact upload")
|
|
for path,body in [(f"/devices/{devices[0][0]}/revocation",{"reason":"deny automation"}),(f"/operation-plans/{plan['plan_id']}/revocation",{"reason":"deny automation"}),("/execution-stop",{"stopped":True,"reason":"deny automation"})]:
|
|
check(request(base+path,"POST",body,operator)[0]==403,"administrative control requires separate interactive human context")
|
|
check(request(base+f"/operation-plans/{plan['plan_id']}",token=other)[0]==404,"catalog-backed plan remains tenant isolated")
|
|
check(sql(f"SELECT count(*) FROM jobs WHERE agent_id IN ('{devices[0][1]}','{devices[1][1]}');")=="0","catalog preparation creates no executable work")
|
|
finally:
|
|
for _,agent in devices:sql(f"UPDATE agent_installations SET revoked_at=now() WHERE agent_id='{agent}';")
|
|
print("Backend paging/membership/catalog tests passed locally; no approvals or native package effects performed.")
|
|
|
|
|
|
if __name__ == "__main__":main()
|