478 lines
42 KiB
Python
478 lines
42 KiB
Python
#!/usr/bin/env python3
|
|
"""One exact benign Cloud Linux script: MCP plan/submit and read-only evidence.
|
|
|
|
No approval endpoint, browser, rights/policy mutation, direct job SQL write,
|
|
cancel, retry, interpreter execution or local agent. Parent executes after review.
|
|
"""
|
|
import argparse
|
|
import base64
|
|
import datetime as dt
|
|
import hashlib
|
|
import inspect
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import re
|
|
import time
|
|
import uuid
|
|
|
|
ROOT=Path(__file__).resolve().parents[1]
|
|
INSTALL_SHA='667b56f7e5f085db249f27a7094919c7c0e6055dabc9f0600cb261c34760cd26'
|
|
if hashlib.sha256((ROOT/'deploy/script_console_install.py').read_bytes()).hexdigest()!=INSTALL_SHA:
|
|
raise RuntimeError('Reviewed bootstrap transport source changed')
|
|
import script_console_install as old
|
|
support=old.support
|
|
TEMPLATE=ROOT/'devstate/output/playwright-private/console-full-v1/mcp-plan-input.template.json'
|
|
TEMPLATE_SHA='66f684ab9c5c31be6583fe04f090ba9d7b8724d6e6c920dd9c67c484a339245e'
|
|
ORIGINAL=ROOT/'devstate/script-console-install/2e27c312-58cc-444d-92a6-863a11ac6ffd/intent.json'
|
|
ORIGINAL_SHA='8aec7bf7df13bc4968f47ca1554c813406409530613ce95241e74a81204cd02e'
|
|
PATTERN_SHA='71ec226ef98acdd7a59cd1cc8648cce910ef143efad08b8e937afde7f26b6d72'
|
|
SCRIPT_SHA='c878ba5b5c37fa3f1b9485c6994d6c57f6f37d01d99d4239c64d264d4ed95336'
|
|
CREATOR='automation:11111111-1111-4111-8111-111111111111'
|
|
APPROVER='simulated-testprincipal-a'
|
|
STDOUT=b'0\nRMM_FULL_CONSOLE_LINUX_V1\n'
|
|
STDERR=b'RMM_FULL_CONSOLE_STDERR_V1\n'
|
|
STATE=ROOT/'devstate/script-console-job-acceptance'
|
|
|
|
|
|
def require(value,code):
|
|
if not value:raise RuntimeError(code)
|
|
|
|
|
|
def encode(value):return json.dumps(value,ensure_ascii=False,separators=(',',':')).encode()
|
|
|
|
|
|
def timestamp_ns(value):
|
|
"""Strict RFC3339 calendar/offset -> integer epoch nanoseconds, no rounding."""
|
|
import datetime as dt
|
|
import re
|
|
require(isinstance(value,str),'RFC3339 timestamp string')
|
|
match=re.fullmatch(r'([0-9]{4})-([0-9]{2})-([0-9]{2})[Tt]([0-9]{2}):([0-9]{2}):([0-9]{2})(?:\.([0-9]{1,9}))?([Zz]|[+-][0-9]{2}:[0-9]{2})',value)
|
|
require(match is not None,'exact RFC3339 with at most nine fractional digits')
|
|
year,month,day,hour,minute,second=(int(match[i]) for i in range(1,7))
|
|
require(hour<24 and minute<60 and second<60,'valid bounded RFC3339 clock; leap-second spelling unsupported')
|
|
try:days=(dt.date(year,month,day)-dt.date(1970,1,1)).days
|
|
except ValueError:raise RuntimeError('valid RFC3339 calendar date') from None
|
|
offset=match[8];offset_seconds=0
|
|
if offset not in ('Z','z'):
|
|
offset_hours=int(offset[1:3]);offset_minutes=int(offset[4:6])
|
|
require(offset_hours<24 and offset_minutes<60,'valid RFC3339 UTC offset')
|
|
offset_seconds=(offset_hours*60+offset_minutes)*60*(1 if offset[0]=='+' else -1)
|
|
seconds=((days*24+hour)*60+minute)*60+second-offset_seconds
|
|
return seconds*1_000_000_000+int((match[7] or '').ljust(9,'0'))
|
|
|
|
|
|
def guards():
|
|
require(support.sha(ROOT/'deploy/script_console_install.py')==INSTALL_SHA
|
|
and support.sha(ROOT/'deploy/script_console_bootstrap.py')==old.PREFLIGHT_SHA
|
|
and support.sha(ROOT/'deploy/cloud_mvp_acceptance.py')==PATTERN_SHA
|
|
and support.sha(TEMPLATE)==TEMPLATE_SHA and support.sha(ORIGINAL)==ORIGINAL_SHA,'exact reviewed template/transport/target')
|
|
old.pre.native.verify_support();manifest=support.load(old.pre.native.PACK/'manifest.json')
|
|
require(support.sha(old.pre.native.PACK/'manifest.json')==old.pre.native.MANIFEST_SHA
|
|
and len(manifest['source_files'])==158 and all(support.sha(ROOT/n)==h for n,h in manifest['source_files'].items()),'frozen158 inputs unchanged')
|
|
template=support.load(TEMPLATE);script=template['parameters']['variants'][0]['script_utf8'].encode()
|
|
require(len(script)==130 and hashlib.sha256(script).hexdigest()==SCRIPT_SHA,'exact benign130 script bytes')
|
|
return template,support.load(ORIGINAL)
|
|
|
|
|
|
def stored_json_slice(raw,path):
|
|
"""Slice a nested value directly from stored UTF-8 JSON, never reencode it."""
|
|
text=raw.decode('utf-8');decoder=json.JSONDecoder(object_pairs_hook=support.object_pairs);start=0
|
|
for component in path:
|
|
while text[start].isspace():start+=1
|
|
opening=text[start];cursor=start+1;found=None
|
|
while True:
|
|
while text[cursor].isspace() or text[cursor]==',':cursor+=1
|
|
if text[cursor] in '}]':break
|
|
if opening=='{':
|
|
key,cursor=decoder.raw_decode(text,cursor)
|
|
while text[cursor].isspace():cursor+=1
|
|
require(text[cursor]==':','stored object separator');cursor+=1
|
|
while text[cursor].isspace():cursor+=1
|
|
else:key=0 if found is None else found
|
|
begin=cursor;_,cursor=decoder.raw_decode(text,cursor)
|
|
if key==component:start=begin;break
|
|
if opening=='[':found=key+1
|
|
else:raise RuntimeError('Stored JSON path')
|
|
require(key==component,'stored value path present')
|
|
_,end=decoder.raw_decode(text,start);return text[start:end].encode()
|
|
|
|
|
|
def checked_plan(record,intent):
|
|
raw=base64.b64decode(record['payload_b64'],validate=True);require(len(raw)<=65536,'bounded original plan')
|
|
plan=json.loads(raw,object_pairs_hook=support.object_pairs);require(hashlib.sha256(raw).hexdigest()==record['plan_hash'],'hash original stored plan bytes')
|
|
require(plan['plan_id']==record['plan_id'] and str(uuid.UUID(plan['plan_id']))==plan['plan_id']
|
|
and plan['schema_version']==1 and plan['tenant_id']==intent['target']['tenant_id'] and plan['principal']==CREATOR
|
|
and plan['reason']==intent['input']['reason'] and plan['max_parallel']==plan['halt_after_failures']==1
|
|
and len(plan['jobs'])==1 and plan.get('recipe_references',[])==plan.get('artifact_references',[])==[], 'one exact creator/target/rollout plan')
|
|
entry=plan['jobs'][0];job=entry['payload'];job_raw=stored_json_slice(raw,['jobs',0,'payload'])
|
|
expected=dict(schema_version=1,**intent['target'],job_id=job['job_id'],attempt_id=job['attempt_id'],
|
|
action=dict(action='run_script',interpreter='posix_sh',script_utf8=intent['input']['parameters']['variants'][0]['script_utf8'],arguments=[],expected_stdout=STDOUT.decode(),privileged_script_profile='full_maintenance_v1'),
|
|
execution_identity='root',not_before=job['not_before'],start_before=job['start_before'],max_runtime_seconds=30,max_output_bytes=4096)
|
|
require(job==expected and all(str(uuid.UUID(job[k]))==job[k] for k in ('job_id','attempt_id'))
|
|
and timestamp_ns(job['not_before'])==timestamp_ns(intent['input']['schedule']['not_before'])
|
|
and timestamp_ns(job['start_before'])==timestamp_ns(intent['input']['schedule']['start_before'])
|
|
and hashlib.sha256(job_raw).hexdigest()==entry['job_payload_hash'] and entry['script_sha256']==SCRIPT_SHA,'exact stored job bytes/IDs/identity/profile/script/limits')
|
|
view=dict(plan_id=plan['plan_id'],plan_hash=record['plan_hash'],payload=plan,plan_payload=plan,plan_payload_bytes=list(raw),
|
|
jobs=[dict(job_id=job['job_id'],attempt_id=job['attempt_id'],asset_id=job['asset_id'],agent_id=job['agent_id'],job_payload_hash=entry['job_payload_hash'],script_sha256=SCRIPT_SHA,payload=job)],
|
|
resolved_targets=[dict(asset_id=job['asset_id'],agent_id=job['agent_id'])],excluded_targets=[],authorization_status='required',approval_state=None,expires_at=job['start_before'])
|
|
return dict(record=record,view=view,job=job,job_bytes_b64=base64.b64encode(job_raw).decode(),job_payload_hash=entry['job_payload_hash'])
|
|
|
|
|
|
def server(request):
|
|
import base64
|
|
import datetime as dt
|
|
import hashlib
|
|
import http.client as http_client
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import socket
|
|
import ssl
|
|
import stat
|
|
import subprocess
|
|
import time
|
|
import uuid
|
|
p=request['intent'];case_id=str(uuid.UUID(p['id']));require(case_id==p['id'] and request['action'] in ('plan','submit','read','mcp-result','isolation'),'fixed MCP/evidence case')
|
|
target=p['target'];tenant=str(uuid.UUID(target['tenant_id']));agent=str(uuid.UUID(target['agent_id']));asset=str(uuid.UUID(target['asset_id']))
|
|
require(tenant=='11111111-1111-4111-8111-111111111111' and target['agent_id']=='e9d85af1-067f-4cbe-a547-3a63b70a8d2e' and asset=='30aad079-86b3-41f2-a1e9-34275e7c08fc','only actual Cloud Linux target')
|
|
creator='automation:'+tenant;approver='simulated-testprincipal-a';root=Path('/opt/rust-discovery')
|
|
def quote(text):return "'"+text.replace("'","''")+"'"
|
|
def sql(query):
|
|
cmd=['docker','compose','--env-file',str(root/'devstate/.env'),'-f',str(root/'deploy/server.compose.yaml'),'exec','-T','postgres','psql','-X','-qAt','-v','ON_ERROR_STOP=1','-U','postgres','-d','rmm']
|
|
done=subprocess.run(cmd,input=('BEGIN READ ONLY; '+query+'; COMMIT;').encode(),capture_output=True,check=True,timeout=25)
|
|
require(len(done.stdout)<=4*1024*1024,'bounded fixed read-only SQL evidence');return json.loads(done.stdout)
|
|
def current_rights():
|
|
rows=sql("SELECT coalesce(json_agg(row_to_json(m)), '[]') FROM (SELECT principal,kind,permissions,revoked_at FROM memberships WHERE tenant_id="+quote(tenant)+" AND principal IN ("+quote(creator)+","+quote(approver)+") ORDER BY principal)m")
|
|
roles={r['principal']:r for r in rows};require(set(roles)=={creator,approver} and all(r['revoked_at'] is None for r in rows),'existing active actors')
|
|
require(roles[creator]['kind']=='automation' and roles[approver]['kind']=='test_human'
|
|
and set(roles[creator]['permissions'])=={'inventory:read','logs:read','operations:plan','operations:submit','operations:read','jobs:cancel','scripts:execute','root:execute'}
|
|
and 'plans:approve' not in roles[creator]['permissions']
|
|
and {'plans:approve','scripts:execute','root:execute'}<=set(roles[approver]['permissions']),'current exact actor kinds and required rights before MCP mutation')
|
|
return rows
|
|
def policy_guard():
|
|
path=root/'devstate/root-policy/policy.json';m=path.lstat();raw=path.read_bytes()
|
|
require(not path.is_symlink() and m.st_uid==10001 and stat.S_IMODE(m.st_mode)==0o600 and hashlib.sha256(raw).hexdigest()==p['policy_sha256'],'explicit reviewed protected Root policy')
|
|
policy=json.loads(raw);require(policy['schema_version']==2 and policy['profile']=='development' and len(policy['scopes'])==1,'single reviewed development Fullscope')
|
|
scope=policy['scopes'][0]
|
|
require(all(scope[k]==v for k,v in target.items()) and set(scope['principals'])=={creator,approver}
|
|
and scope['helper_binary_sha256']=='79442727c3ba31cda02a8bc0e67481f14db9f39fc07520a880a90b64f10f1eaf'
|
|
and scope['helper_policy_sha256']=='8101afd12b303c23bad57339729569262d4c1d66c741da9d6d4a72ebf3495ece'
|
|
and scope['allow_root_scripts'] is True and scope['allowed_script_profiles']==['full_maintenance_v1']
|
|
and scope['allowed_services']==[] and scope['allow_reboot'] is False and 'apt' not in scope,'exact narrow Fullscope/code/policy/actors')
|
|
expiry=timestamp_ns(policy['expires_at']);deadline=timestamp_ns(p['input']['schedule']['start_before'])
|
|
require(expiry>=deadline>time.time_ns(),'explicit current start deadline within policy expiry')
|
|
return dict(sha256=p['policy_sha256'],expires_at=policy['expires_at'])
|
|
def http(body,headers,principal):
|
|
class HTTPS(http_client.HTTPSConnection):
|
|
def connect(self):self.sock=self._context.wrap_socket(socket.create_connection(('127.0.0.1',18443),timeout=15),server_hostname='localhost')
|
|
require(principal in ('a','b'),'fixed existing server-only credential')
|
|
token_path=root/('devstate/operator-'+principal+'.token');m=token_path.lstat();require(not token_path.is_symlink() and stat.S_ISREG(m.st_mode) and m.st_uid in (0,10001) and not m.st_mode&0o077 and m.st_size<=8192,'existing private server-only operator credential')
|
|
client=HTTPS('localhost',18443,timeout=15,context=ssl.create_default_context(cafile=str(root/'devstate/api-pki/ca.pem')))
|
|
try:
|
|
client.request('POST','/mcp',body=json.dumps(body),headers={'Content-Type':'application/json','Authorization':'Bearer '+token_path.read_text().strip(),**headers})
|
|
response=client.getresponse();session=response.getheader('Mcp-Session-Id');status=response.status;data=None
|
|
if status!=202:
|
|
if 'text/event-stream' in response.getheader('Content-Type',''):
|
|
parts=[];total=0
|
|
for _ in range(256):
|
|
line=response.readline(1024*1024+1);total+=len(line);require(total<=1024*1024,'bounded MCP SSE')
|
|
if not line:break
|
|
if line.startswith(b'data:'):
|
|
chunk=line[5:].rstrip(b'\r\n')
|
|
if chunk.startswith(b' '):chunk=chunk[1:]
|
|
parts.append(chunk)
|
|
if not line.strip() and parts:
|
|
frame=b'\n'.join(parts);parts=[]
|
|
if frame==b'':continue
|
|
value=json.loads(frame)
|
|
if 'result' in value or 'error' in value:data=value;break
|
|
else:
|
|
raw=response.read(1024*1024+1);require(len(raw)<=1024*1024,'bounded MCP JSON');data=json.loads(raw) if raw else None
|
|
return status,session,data
|
|
finally:client.close()
|
|
def mcp(name,args,principal='a',expect_denied=False):
|
|
require((principal=='a' and not expect_denied and name in ('operations.plan','operations.submit','jobs.get'))
|
|
or (principal=='b' and expect_denied and name in ('jobs.get','inventory.get_device')),'only fixed mutation or affected readonly MCP methods; no approvals/cancel')
|
|
headers={'Accept':'application/json, text/event-stream','MCP-Protocol-Version':'2025-11-25'}
|
|
status,session,data=http(dict(jsonrpc='2.0',id=1,method='initialize',params=dict(protocolVersion='2025-11-25',capabilities={},clientInfo=dict(name='script-console-cloud-only',version='1'))),headers,principal)
|
|
require(status==200 and session and data['jsonrpc']=='2.0' and data['id']==1
|
|
and data['result']['protocolVersion']=='2025-11-25','existing verified MCP initialize')
|
|
headers['Mcp-Session-Id']=session
|
|
require(http(dict(jsonrpc='2.0',method='notifications/initialized'),headers,principal)[0]==202,'MCP initialized notification')
|
|
status,_,data=http(dict(jsonrpc='2.0',id=2,method='tools/call',params=dict(name=name,arguments=args)),headers,principal)
|
|
if expect_denied:
|
|
require(status==200 and data and data.get('id')==2 and data.get('jsonrpc')=='2.0'
|
|
and data.get('result',{}).get('isError') is True,'actual authenticated MCP tool denial required')
|
|
denial=data['result']['structuredContent']
|
|
require(set(denial)=={'code','retryable','message','correlation_id','details'} and denial['code'] in ('FORBIDDEN','NOT_FOUND')
|
|
and denial['retryable'] is False and denial['details']=={},'no successful other-tenant response or leaked details')
|
|
return dict(tool=name,http_status=status,valid_authenticated_initialize=True,is_error=True,code=denial['code'],details={})
|
|
require(status==200 and data and data['jsonrpc']=='2.0' and data['id']==2
|
|
and 'result' in data and not data['result'].get('isError'),'MCP call incomplete; retain case and read only')
|
|
return data['result']['structuredContent']
|
|
def safe(path,directory=False):
|
|
m=path.lstat();require(not path.is_symlink() and m.st_uid==0 and stat.S_IMODE(m.st_mode)==(0o700 if directory else 0o600)
|
|
and (stat.S_ISDIR(m.st_mode) if directory else stat.S_ISREG(m.st_mode) and m.st_nlink==1),'private bootstrap case')
|
|
def once(path,value):
|
|
raw=json.dumps(value,sort_keys=True,separators=(',',':')).encode();fd=os.open(path,os.O_CREAT|os.O_EXCL|os.O_WRONLY|os.O_NOFOLLOW,0o600)
|
|
with os.fdopen(fd,'wb') as out:out.write(raw);out.flush();os.fsync(out.fileno())
|
|
fd=os.open(path.parent,os.O_RDONLY|os.O_DIRECTORY|os.O_NOFOLLOW)
|
|
try:os.fsync(fd)
|
|
finally:os.close(fd)
|
|
parent=root/'devstate/script-console-job-acceptance';case=parent/case_id
|
|
if request['action'] in ('plan','submit'):
|
|
rights=current_rights();policy=policy_guard()
|
|
os.umask(0o077)
|
|
if request['action']=='plan':
|
|
if not parent.exists():parent.mkdir(mode=0o700)
|
|
safe(parent,True);case.mkdir(mode=0o700);once(case/'intent.json',p)
|
|
safe(parent,True);safe(case,True);safe(case/'intent.json')
|
|
require(json.loads((case/'intent.json').read_bytes())==p,'original immutable remote intent')
|
|
if request['action']=='plan':
|
|
once(case/'plan-entered.json',dict(plan_once=True,rights=rights,policy=policy))
|
|
result=mcp('operations.plan',dict(tenant_id=tenant,input=p['input']));once(case/'plan-result.json',result);return dict(plan=result,rights=rights,policy=policy)
|
|
stored=request['plan'];plan_id=str(uuid.UUID(stored['plan_id']))
|
|
require(plan_id==stored['plan_id'],'canonical stored plan ID')
|
|
actual=sql("SELECT json_build_object('payload_b64',replace(encode(payload,'base64'),chr(10),''),'principal',principal,'reason',convert_from(payload,'UTF8')::jsonb->>'reason') FROM operation_plans WHERE tenant_id="+quote(tenant)+" AND plan_id="+quote(plan_id))
|
|
require(actual['principal']==creator and actual['reason']==p['input']['reason']
|
|
and hashlib.sha256(base64.b64decode(actual['payload_b64'],validate=True)).hexdigest()==stored['plan_hash'],'actual original plan before submit')
|
|
once(case/'submit-entered.json',dict(submit_once=True,plan_id=plan_id,plan_hash=stored['plan_hash'],idempotency_key=p['idempotency_key'],rights=rights,policy=policy))
|
|
result=mcp('operations.submit',dict(tenant_id=tenant,plan_id=plan_id,idempotency_key=p['idempotency_key']));once(case/'submit-result.json',result);return dict(pending=result,rights=rights,policy=policy)
|
|
if request['action'] in ('mcp-result','isolation'):
|
|
stored=request['plan'];plan_id=str(uuid.UUID(stored['plan_id']))
|
|
actual=sql("SELECT json_build_object('payload_b64',replace(encode(payload,'base64'),chr(10),''),'principal',principal) FROM operation_plans WHERE tenant_id="+quote(tenant)+" AND plan_id="+quote(plan_id))
|
|
raw=base64.b64decode(actual['payload_b64'],validate=True)
|
|
require(actual['principal']==creator and hashlib.sha256(raw).hexdigest()==stored['plan_hash'],'same actual stored A-plan for readonly HTTP proof')
|
|
plan=json.loads(raw);require(plan['reason']==p['input']['reason'] and len(plan['jobs'])==1,'same one fixed acceptance plan')
|
|
job=plan['jobs'][0]['payload'];job_id=str(uuid.UUID(job['job_id']))
|
|
require(job['tenant_id']==tenant and job['agent_id']==agent and job['asset_id']==asset,'same existing A-job/asset for readonly HTTP proof')
|
|
if request['action']=='mcp-result':return dict(result=mcp('jobs.get',dict(tenant_id=tenant,job_id=job_id)),read_only=True)
|
|
return dict(read_only=True,credential='existing server-only operator-b',tests=[
|
|
mcp('jobs.get',dict(tenant_id=tenant,job_id=job_id),'b',True),
|
|
mcp('inventory.get_device',dict(tenant_id=tenant,asset_id=asset),'b',True)])
|
|
# All recovery/status queries are read-only. In particular do not use the
|
|
# operation getter, which may expire a pending request in application code.
|
|
reason=quote(p['input']['reason']);key=quote(p['idempotency_key']);scope="tenant_id="+quote(tenant)
|
|
plans=sql("SELECT coalesce(json_agg(row_to_json(q)),'[]') FROM (SELECT plan_id,plan_hash,replace(encode(payload,'base64'),chr(10),'') AS payload_b64 FROM operation_plans WHERE "+scope+" AND principal="+quote(creator)+" AND convert_from(payload,'UTF8')::jsonb->>'reason'="+reason+" ORDER BY created_at LIMIT 2)q")
|
|
require(len(plans)<=1,'ambiguous stored plan; no retry')
|
|
markers={}
|
|
if case.exists():
|
|
safe(parent,True);safe(case,True);safe(case/'intent.json');require(json.loads((case/'intent.json').read_bytes())==p,'original case readback')
|
|
for name in ('plan-entered','submit-entered'):markers[name]=os.path.lexists(case/(name+'.json'))
|
|
result=dict(plans=plans,markers=markers,read_only=True,approvals_performed=False)
|
|
if not plans:return result
|
|
plan_id=str(uuid.UUID(plans[0]['plan_id']));plan=json.loads(base64.b64decode(plans[0]['payload_b64']));job=plan['jobs'][0]['payload'];job_id=str(uuid.UUID(job['job_id']));attempt=str(uuid.UUID(job['attempt_id']))
|
|
require(job['agent_id']==agent and job['asset_id']==asset and job['tenant_id']==tenant,'fixed target in stored plan')
|
|
selected=scope+' AND job_id='+quote(job_id)+' AND attempt_id='+quote(attempt)
|
|
result['requests']=sql("SELECT coalesce(json_agg(row_to_json(q)),'[]') FROM (SELECT r.operation_id,r.plan_id,r.principal,r.approval_state,r.idempotency_key,r.request_hash FROM approval_requests r JOIN operation_submissions s USING(tenant_id,operation_id) WHERE r.tenant_id="+quote(tenant)+" AND r.plan_id="+quote(plan_id)+" AND s.principal="+quote(creator)+" AND s.idempotency_key="+key+")q")
|
|
result['counts']=sql("SELECT json_build_object('jobs',(SELECT count(*) FROM jobs WHERE "+selected+"),'outbox',(SELECT count(*) FROM outbox WHERE "+selected+"),'permits',(SELECT count(*) FROM execution_permits WHERE "+selected+"),'authorizations',(SELECT count(*) FROM authorizations WHERE "+scope+" AND plan_id="+quote(plan_id)+"))")
|
|
result['jobs']=sql("SELECT coalesce(json_agg(row_to_json(q)),'[]') FROM (SELECT job_id,attempt_id,operation_id,agent_id,asset_id,payload_hash,replace(encode(payload,'base64'),chr(10),'') AS payload_b64,state,result FROM jobs WHERE "+selected+")q")
|
|
result['permits']=sql("SELECT coalesce(json_agg(row_to_json(q)),'[]') FROM (SELECT grant_id,replace(encode(payload,'base64'),chr(10),'') AS payload_b64,replace(encode(signature,'base64'),chr(10),'') AS signature_b64 FROM execution_permits WHERE "+selected+")q")
|
|
result['authorizations']=sql("SELECT coalesce(json_agg(row_to_json(q)),'[]') FROM (SELECT authorization_id,operation_id,plan_id,plan_hash,principal,approval_origin,testsimulation FROM authorizations WHERE "+scope+" AND plan_id="+quote(plan_id)+")q")
|
|
result['audit']=sql("SELECT coalesce(json_agg(row_to_json(q)),'[]') FROM (SELECT event_id,principal,event_type,data,created_at FROM audit_events WHERE "+scope+" AND event_type='plan_authorization' AND data->>'plan_id'="+quote(plan_id)+" ORDER BY created_at)q")
|
|
return result
|
|
|
|
|
|
def native_evidence(p):
|
|
import base64
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import sqlite3
|
|
import subprocess
|
|
import uuid
|
|
job=p['job'];require(os.geteuid()==0 and str(uuid.UUID(job['job_id']))==job['job_id'] and str(uuid.UUID(job['attempt_id']))==job['attempt_id'],'fixed native evidence binding')
|
|
require(Path('/proc/sys/kernel/random/boot_id').read_text().strip()==p['boot_id'],'same native boot')
|
|
processes={}
|
|
for name,key,uid in (('rmm-full-maintenance-helper.service','helper',0),('rmm-agent-remote.service','agent',993)):
|
|
pid=int(subprocess.run(['systemctl','show',name,'-p','MainPID','--value'],capture_output=True,check=True,timeout=10).stdout)
|
|
proc=Path('/proc',str(pid));birth=(proc/'stat').read_text().rsplit(')',1)[1].split()[19];status=(proc/'status').read_text().splitlines()
|
|
require(next(x.split()[1:] for x in status if x.startswith('Uid:'))==[str(uid)]*4
|
|
and next(x.split()[1:] for x in status if x.startswith('Gid:'))==['984']*4
|
|
and hashlib.sha256((proc/'exe').read_bytes()).hexdigest()==p['artifacts'][key]['sha256']
|
|
and (proc/'stat').read_text().rsplit(')',1)[1].split()[19]==birth,'actual qualified process image/accounts/birth')
|
|
processes[name]=dict(pid=pid,start_ticks=birth,sha256=p['artifacts'][key]['sha256'])
|
|
path=Path('/var/lib/rmm-helper/consumption.sqlite');m=path.lstat()
|
|
require(not path.is_symlink() and [m.st_dev,m.st_ino]==p['database_identity'],'original Root database inode')
|
|
import time
|
|
original_ids=sorted(p['original_rows']);require(len(original_ids)==18,'exact eighteen original Root IDs')
|
|
started=time.monotonic_ns();db=sqlite3.connect(path.as_uri()+'?mode=ro',uri=True,timeout=0.25)
|
|
try:
|
|
db.set_progress_handler(lambda:int(time.monotonic_ns()-started>1_000_000_000),1000)
|
|
db.execute('PRAGMA query_only=ON');db.execute('BEGIN')
|
|
require(db.execute('PRAGMA journal_mode').fetchone()[0]=='delete','qualified readonly DELETE journal')
|
|
total,unresolved=db.execute('SELECT count(*),coalesce(sum(result IS NULL),0) FROM consumed').fetchone()
|
|
require(total in (18,19),'at most one new Root consumption')
|
|
placeholders=','.join('?' for _ in original_ids)
|
|
rows=db.execute('SELECT c.grant_id,c.agent,c.generation,c.job,c.attempt,c.result,e.start,e.outcome FROM consumed c LEFT JOIN execution_evidence e USING(grant_id) '
|
|
+'WHERE c.grant_id IN ('+placeholders+') OR (c.job=? AND c.attempt=?) ORDER BY c.grant_id LIMIT 20',
|
|
[*original_ids,job['job_id'],job['attempt_id']]).fetchall()
|
|
finally:
|
|
# Materialize only the known eighteen plus this one attempt; close the
|
|
# shared read transaction before hashing/decoding any retained BLOBs.
|
|
try:db.rollback()
|
|
finally:db.close()
|
|
elapsed=time.monotonic_ns()-started;m=path.lstat()
|
|
require(elapsed<=1_000_000_000,'short native read transaction within accepted one-second budget')
|
|
require(not path.is_symlink() and [m.st_dev,m.st_ino]==p['database_identity'],'same Root inode after short read')
|
|
def digest(row):return hashlib.sha256(json.dumps([dict(sha256=hashlib.sha256(v).hexdigest(),size=len(v)) if isinstance(v,bytes) else v for v in row],separators=(',',':')).encode()).hexdigest()
|
|
original={row[0]:digest(row) for row in rows if row[0] in p['original_rows']}
|
|
require(original==p['original_rows'] and len(rows)==total,'all original18 bytes retained, only this possible new consumption')
|
|
found=[row for row in rows if row[3]==job['job_id'] and row[4]==job['attempt_id']]
|
|
require(len(found)<=1 and total==18+len(found),'no unrelated new consumption')
|
|
records=[]
|
|
for row in found:
|
|
require(row[1]==job['agent_id'] and row[2]==job['enrollment_generation'],'native consumption target/generation')
|
|
records.append(dict(grant_id=row[0],result_b64=base64.b64encode(row[5]).decode() if row[5] is not None else None,
|
|
start_b64=base64.b64encode(row[6]).decode() if row[6] is not None else None,outcome_b64=base64.b64encode(row[7]).decode() if row[7] is not None else None))
|
|
return dict(read_only=True,original_root18_unchanged=True,consumed=total,unresolved=unresolved,records=records,processes=processes,
|
|
read_transaction_ns=elapsed,read_transaction_budget_ns=1_000_000_000,sqlite_busy_timeout_ms=250,sqlite_integrity_scan_performed=False)
|
|
|
|
|
|
|
|
def rpc(intent,action,folder,**extra):
|
|
guards();require(intent['runner_sha256']==support.sha(__file__),'same original runner before every call/read')
|
|
require(support.load(folder/'intent-hash.json')==dict(intent_sha256=support.sha(folder/'intent.json')),'original complete local intent bytes before every target call')
|
|
code=inspect.getsource(require)+'\n'+inspect.getsource(timestamp_ns)+'\n'+inspect.getsource(server)
|
|
code+='\ntry:\n print(__import__("json").dumps(server('+repr(dict(intent=intent,action=action,**extra))+')))\nexcept Exception as e:\n print(__import__("json").dumps(dict(bootstrap_error=True,error_class=type(e).__name__)))\n'
|
|
return support.transport(old.server_prefix(),'python3 -',code.encode(),folder,timeout=230)
|
|
|
|
|
|
def retain_plan(folder,record,intent):
|
|
plan=checked_plan(record,intent);path=folder/'stored-plan.json'
|
|
if path.exists():require(support.load(path)==plan,'original plan bytes/IDs/hash never replaced')
|
|
else:support.save(path,plan)
|
|
return plan
|
|
|
|
|
|
def read_case(intent,folder):
|
|
evidence=rpc(intent,'read',folder);support.save(folder/('server-read-'+str(uuid.uuid4())+'.json'),evidence)
|
|
if not evidence['plans']:return dict(state='plan_effect_unknown' if (folder/'plan-entered.json').exists() else 'not_planned',evidence=evidence)
|
|
plan=retain_plan(folder,evidence['plans'][0],intent);_,original=guards()
|
|
require(intent['runner_sha256']==support.sha(__file__)
|
|
and support.load(folder/'intent-hash.json')==dict(intent_sha256=support.sha(folder/'intent.json')),'same original source/intent before native read')
|
|
identity=original['preflight']['database_inodes']['/var/lib/rmm-helper/consumption.sqlite']
|
|
native=old.rpc(support.access_settings(old.pre.ACCESS_ROOT,'linux'),native_evidence,dict(job=plan['job'],boot_id=original['preflight']['boot_id'],artifacts=original['artifacts'],database_identity=identity,original_rows=original['preflight']['root_rows']),folder)
|
|
support.save(folder/('native-read-'+str(uuid.uuid4())+'.json'),native)
|
|
return dict(plan=plan,evidence=evidence,native=native)
|
|
|
|
|
|
def pending_checkpoint(intent,folder,value):
|
|
evidence=value['evidence'];require(len(evidence['requests'])==1,'one immutable submission lookup')
|
|
pending=evidence['requests'][0];require(pending['approval_state']=='pending_approval' and pending['principal']==CREATOR
|
|
and pending['idempotency_key']==intent['idempotency_key'] and evidence['counts']==dict(jobs=0,outbox=0,permits=0,authorizations=0)
|
|
and evidence['jobs']==evidence['permits']==evidence['authorizations']==evidence['audit']==[]
|
|
and value['native']['records']==[] and value['native']['consumed']==18 and value['native']['unresolved']==0,'pending has zero executable artifacts and no native consumption')
|
|
plan=value['plan'];require(pending['plan_id']==plan['record']['plan_id'],'same pending plan')
|
|
checkpoint=dict(profile='dev/test',case='script-console-linux-full-v1',**intent['target'],expected_principal=APPROVER,
|
|
plan=plan['view'],job=plan['view']['jobs'][0],request=intent['input'],operation_id=pending['operation_id'],
|
|
plan_hash=plan['record']['plan_hash'],plan_payload_bytes=plan['view']['plan_payload_bytes'],job_payload=plan['job'],job_payload_hash=plan['job_payload_hash'],
|
|
preapproval=evidence['counts'],operation=dict(operation_id=pending['operation_id'],plan_id=pending['plan_id'],plan_hash=plan['record']['plan_hash'],
|
|
approval_state='pending_approval',planned_job_count=1,job_count=0,execution_state=None,jobs=[]),
|
|
operation_view_origin='read_only_scoped_SQL',stage='pending_approval',script_sha256=SCRIPT_SHA,no_retry=True,approval_performed=False)
|
|
path=folder/'pending-checkpoint.json'
|
|
if path.exists():require(support.load(path)==checkpoint,'original pending checkpoint remains immutable')
|
|
else:support.save(path,checkpoint)
|
|
return checkpoint
|
|
|
|
|
|
def verify_result(intent,value):
|
|
plan=value['plan'];evidence=value['evidence'];native=value['native']
|
|
require(evidence['counts']==dict(jobs=1,outbox=1,permits=1,authorizations=1)
|
|
and len(evidence['requests'])==len(evidence['jobs'])==len(evidence['permits'])==len(evidence['authorizations'])==len(evidence['audit'])==1,'one job/outbox/grant/authorization only')
|
|
request=evidence['requests'][0];job=evidence['jobs'][0];permit=evidence['permits'][0];auth=evidence['authorizations'][0];audit=evidence['audit'][0]
|
|
require(request['approval_state']=='approved' and request['principal']==CREATOR and request['idempotency_key']==intent['idempotency_key']
|
|
and auth['principal']==APPROVER and auth['approval_origin']=='dev_test_browser_simulation' and auth['testsimulation'] is True
|
|
and auth['plan_hash']==plan['record']['plan_hash'] and auth['operation_id']==request['operation_id']==job['operation_id']
|
|
and auth['plan_id']==request['plan_id']==plan['record']['plan_id']
|
|
and audit['principal']==APPROVER and audit['data']['approval_origin']=='dev_test_browser_simulation' and audit['data']['testsimulation'] is True
|
|
and audit['data']['decision']=='approve' and audit['data']['profile']=='development'
|
|
and audit['data']['plan_id']==plan['record']['plan_id'] and audit['data']['plan_hash']==plan['record']['plan_hash'] and audit['data']['operation_id']==request['operation_id'],'actual simulated WebUI audit binding, never production human approval')
|
|
raw_job=base64.b64decode(plan['job_bytes_b64'],validate=True)
|
|
require(job['state']=='succeeded' and job['job_id']==plan['job']['job_id'] and job['attempt_id']==plan['job']['attempt_id']
|
|
and job['agent_id']==plan['job']['agent_id'] and job['asset_id']==plan['job']['asset_id']
|
|
and job['payload_hash']==plan['job_payload_hash'] and base64.b64decode(job['payload_b64'],validate=True)==raw_job,'same successful stored job/attempt bytes')
|
|
result=job['result'];require(result['exit_code']==0 and result['output_truncated'] is False and result['reboot_required'] is False
|
|
and bytes(result['stdout'])==STDOUT and bytes(result['stderr'])==STDERR and result['evidence']['postconditions_passed'] is True
|
|
and result['evidence']['execution_uid']==0 and result['evidence']['privileged_script_profile']=='full_maintenance_v1'
|
|
and result['evidence']['script_sha256']==SCRIPT_SHA,'exact native root/profile/stdout/stderr result')
|
|
require(native['original_root18_unchanged'] is True and native['consumed']==19 and native['unresolved']==0 and len(native['records'])==1,'one finished new Root consumption')
|
|
record=native['records'][0];start=json.loads(base64.b64decode(record['start_b64'],validate=True));outcome=json.loads(base64.b64decode(record['outcome_b64'],validate=True));finished=json.loads(base64.b64decode(record['result_b64'],validate=True));request=start['request']
|
|
permit_raw=base64.b64decode(permit['payload_b64'],validate=True);grant=json.loads(permit_raw)
|
|
require(record['grant_id']==permit['grant_id']==grant['grant_id']==result['grant_id']
|
|
and bytes(request['job_bytes'])==raw_job and bytes(request['permit_bytes'])==permit_raw
|
|
and bytes(request['signature'])==base64.b64decode(permit['signature_b64'],validate=True)
|
|
and request['plan_hash']==grant['plan_hash']==plan['record']['plan_hash'] and request['authorization_id']==grant['authorization_id']==auth['authorization_id']
|
|
and grant['job_payload_hash']==plan['job_payload_hash'] and all(grant[k]==plan['job'][k] for k in (*intent['target'].keys(),'job_id','attempt_id','execution_identity','max_runtime_seconds'))
|
|
and grant['domain']=='rmm.execution-permit' and grant['schema_version']==1
|
|
and grant['action']=='run_script' and grant['key_id']==intent['signing_key_id']
|
|
and timestamp_ns(plan['job']['not_before'])<=timestamp_ns(grant['not_before'])
|
|
<=timestamp_ns(start['reserved_at'])<timestamp_ns(grant['start_before'])
|
|
<=timestamp_ns(plan['job']['start_before'])
|
|
and start['previous_boot_id']==intent['boot_id'] and outcome==finished
|
|
and outcome['state']==job['state']
|
|
and all(outcome[k]==result[k] for k in ('stdout','stderr','exit_code','output_truncated','reboot_required','evidence','error_code')),'retained exact original native request/grant/signature/outcome matches server evidence')
|
|
|
|
|
|
def main():
|
|
parser=argparse.ArgumentParser(description=__doc__);parser.add_argument('mode',choices=('prepare-local','plan','submit','pending','read','verify-result'));parser.add_argument('--case');parser.add_argument('--not-before');parser.add_argument('--start-before');parser.add_argument('--policy-sha');args=parser.parse_args()
|
|
os.umask(0o077);STATE.mkdir(mode=0o700,parents=True,exist_ok=True);template,original=guards()
|
|
if args.mode=='prepare-local':
|
|
require(args.case is None and args.not_before and args.start_before and re.fullmatch('[a-f0-9]{64}',args.policy_sha or ''),'explicit future schedule and reviewed policy SHA')
|
|
not_before=timestamp_ns(args.not_before);start_before=timestamp_ns(args.start_before)
|
|
require(all(re.search(r'(?:Z|[+]00:00)$',v) for v in (args.not_before,args.start_before))
|
|
and time.time_ns()<not_before<start_before and start_before-not_before<=600_000_000_000,'fixed future UTC window, at most ten minutes')
|
|
case=str(uuid.uuid4());folder=STATE/case;folder.mkdir(mode=0o700);target=original['general_pin']['identity'];target={k:target[k] for k in ('tenant_id','asset_id','agent_id','enrollment_generation','assignment_version')}
|
|
template['targets']['asset_ids']=[target['asset_id']];template['schedule'].update(not_before=args.not_before,start_before=args.start_before);template['reason']+='; acceptance_case='+case
|
|
support.save(folder/'intent.json',dict(id=case,runner_sha256=support.sha(__file__),template_sha256=TEMPLATE_SHA,script_sha256=SCRIPT_SHA,policy_sha256=args.policy_sha,
|
|
idempotency_key='script-console-full-'+case,target=target,input=template,boot_id=original['preflight']['boot_id'],signing_key_id=original['general_pin']['identity']['signing_key_id']))
|
|
support.save(folder/'intent-hash.json',dict(intent_sha256=support.sha(folder/'intent.json')))
|
|
else:
|
|
require(args.case and args.not_before is None and args.start_before is None and args.policy_sha is None,'original case only; no changed plan/retry')
|
|
case=str(uuid.UUID(args.case));require(case==args.case,'canonical case');folder=STATE/case;intent=support.load(folder/'intent.json')
|
|
require(intent['runner_sha256']==support.sha(__file__) and intent['template_sha256']==TEMPLATE_SHA and intent['script_sha256']==SCRIPT_SHA,'same original source/input/script')
|
|
require(support.load(folder/'intent-hash.json')==dict(intent_sha256=support.sha(folder/'intent.json')),'original complete intent bytes')
|
|
fixed_target={k:original['general_pin']['identity'][k] for k in ('tenant_id','asset_id','agent_id','enrollment_generation','assignment_version')}
|
|
template['targets']['asset_ids']=[fixed_target['asset_id']]
|
|
template['schedule'].update(not_before=intent['input']['schedule']['not_before'],start_before=intent['input']['schedule']['start_before'])
|
|
template['reason']+='; acceptance_case='+case
|
|
expected=dict(id=case,runner_sha256=support.sha(__file__),template_sha256=TEMPLATE_SHA,script_sha256=SCRIPT_SHA,policy_sha256=intent['policy_sha256'],
|
|
idempotency_key='script-console-full-'+case,target=fixed_target,input=template,boot_id=original['preflight']['boot_id'],signing_key_id=original['general_pin']['identity']['signing_key_id'])
|
|
require(intent==expected and re.fullmatch('[a-f0-9]{64}',intent['policy_sha256']),'exact frozen template/target/profile/limits/actors for every operation')
|
|
if args.mode=='plan':
|
|
support.save(folder/'plan-entered.json',dict(plan_once=True,no_retry=True));reply=rpc(intent,'plan',folder);support.save(folder/'plan-reply.json',reply)
|
|
view=reply['plan'];raw=bytes(view['plan_payload_bytes']);require(view['plan_id']==view['payload']['plan_id'] and view['payload']==view['plan_payload']==json.loads(raw),'actual plan aliases match stored bytes')
|
|
plan=retain_plan(folder,dict(plan_id=view['plan_id'],plan_hash=view['plan_hash'],payload_b64=base64.b64encode(raw).decode()),intent)
|
|
require(view['jobs']==plan['view']['jobs'],'actual job mirrors match original stored bytes')
|
|
elif args.mode=='submit':
|
|
plan=checked_plan(support.load(folder/'stored-plan.json')['record'],intent)
|
|
support.save(folder/'submit-entered.json',dict(submit_once=True,no_retry=True,idempotency_key=intent['idempotency_key']))
|
|
reply=rpc(intent,'submit',folder,plan=plan['record']);support.save(folder/'submit-reply.json',reply)
|
|
pending=reply['pending'];require(pending['plan_id']==plan['record']['plan_id'] and pending['plan_hash']==plan['record']['plan_hash']
|
|
and pending['approval_state']=='pending_approval' and pending['job_count']==0 and pending['execution_state'] is None
|
|
and str(uuid.UUID(pending['operation_id']))==pending['operation_id'],'actual submit reply is the same pending request with zero executable jobs')
|
|
checkpoint=pending_checkpoint(intent,folder,read_case(intent,folder))
|
|
require(checkpoint['operation_id']==pending['operation_id'],'same submitted and stored pending operation')
|
|
else:
|
|
value=read_case(intent,folder);support.save(folder/('readback-'+str(uuid.uuid4())+'.json'),value)
|
|
if args.mode=='pending':pending_checkpoint(intent,folder,value)
|
|
if args.mode=='verify-result':
|
|
verify_result(intent,value)
|
|
actual=rpc(intent,'mcp-result',folder,plan=value['plan']['record']);support.save(folder/('mcp-result-'+str(uuid.uuid4())+'.json'),actual)
|
|
job=value['evidence']['jobs'][0]
|
|
require(actual['read_only'] is True and actual['result']['job_id']==job['job_id'] and actual['result']['attempt_id']==job['attempt_id']
|
|
and actual['result']['state']==job['state'] and actual['result']['result']==job['result'],'complete same-tenant MCP result/raw bytes match SQL/native')
|
|
denied=rpc(intent,'isolation',folder,plan=value['plan']['record']);support.save(folder/('tenant-b-denial-'+str(uuid.uuid4())+'.json'),denied)
|
|
require(denied['read_only'] is True and len(denied['tests'])==2
|
|
and [x['tool'] for x in denied['tests']]==['jobs.get','inventory.get_device']
|
|
and all(x['valid_authenticated_initialize'] is True and x['http_status']==200 and x['is_error'] is True
|
|
and x['code'] in ('FORBIDDEN','NOT_FOUND') and x['details']=={} for x in denied['tests']),'actual affected other-tenant MCP HTTP denials')
|
|
support.save(folder/'passed.json',dict(verified=True,case_id=case,script_sha256=SCRIPT_SHA,plan_hash=value['plan']['record']['plan_hash'],
|
|
result=value,mcp_result=actual,tenant_b_http_denial=denied,approval_performed=False,independent_ed25519_reverification=False))
|
|
print(json.dumps(dict(case_id=case,mode=args.mode,intent=str(folder/'intent.json'),intent_sha256=support.sha(folder/'intent.json'),target_called=args.mode!='prepare-local',approval_performed=False,retries=False)))
|
|
|
|
|
|
if __name__=='__main__':main()
|