Files
rust-discovery/deploy/cloud_reboot_proof_upgrade.py
2026-11-13 22:03:24 +01:00

82 lines
6.7 KiB
Python

#!/usr/bin/env python3
"""Preserve the one consumed reboot while updating its read-only verifier."""
import argparse
import base64
import hashlib
import io
import json
from pathlib import Path
import tarfile
import cloud_reboot_acceptance as reboot
import cloud_server_upgrade as upgrade
cloud=reboot.cloud
require=cloud.contract.require
def agent_transport(payload):
import contextlib
import sqlite3
require(payload['action'] in ('stop','start'),'own agent transport direction')
require(json.loads(Path('/var/lib/rmm-agent-remote/identity.json').read_text())['agent_id']==payload['agent_id'],'same device')
require(hashlib.sha256(Path('/usr/local/libexec/rmm-agent-remote').read_bytes()).hexdigest()==payload['agent_sha256'],'qualified agent')
boot=Path('/proc/sys/kernel/random/boot_id').read_text().strip()
require(boot==payload['new_boot_id'] and boot!=payload['old_boot_id'],'already completed one guest boot')
with contextlib.closing(sqlite3.connect('file:/var/lib/rmm-helper/consumption.sqlite?mode=ro',uri=True)) as db:
require(db.execute('PRAGMA integrity_check').fetchone()[0]=='ok' and db.execute('SELECT count(*) FROM helper_sync_meta WHERE fault IS NOT NULL').fetchone()[0]==0,'preserved healthy Root ledger')
rows=db.execute('SELECT c.grant_id,c.job,c.attempt,e.outcome FROM consumed c JOIN execution_evidence e USING(grant_id) WHERE c.result IS NULL').fetchall()
require(len(rows)==1 and rows[0][:3]==(payload['grant_id'],payload['job_id'],payload['attempt_id']) and json.loads(rows[0][3])['state']=='awaiting_reboot','only original consumed reboot may await proof')
subprocess.run(['systemctl',payload['action'],'rmm-agent-remote.service'],capture_output=True,check=True,timeout=20)
return dict(action=payload['action'],new_boot_id=boot,jobs_created=False,grants_created=False,reboot_requested=False)
def main():
p=argparse.ArgumentParser(description=__doc__);p.add_argument('--id',required=True);p.add_argument('--reboot-id',required=True);args=p.parse_args()
import uuid
candidate=str(uuid.UUID(args.id));sid=str(uuid.UUID(args.reboot_id));folder=reboot.STATE/sid
session=cloud.contract.protected_json(folder/'before.json');checkpoint=cloud.contract.protected_json(folder/'checkpoint.json');job=checkpoint['job'];target=session['target']
require(sid=='d015c3ef-f3aa-4522-a3c2-fcb6099df17b' and job['job_id']=='8ffd0ce5-187e-4227-9fd4-374ac5fdf4aa','one exact already approved reboot')
gate=lambda:reboot.evidence(target,'gate')
require(gate()==dict(jobs=30,outbox=30,permits=26,unresolved=1),'only known consumed reboot unresolved')
current=cloud.Backend().call('jobs.get',dict(tenant_id=target['tenant_id'],job_id=job['job_id']))
grant='25a4bf3f-772c-487d-825d-73bcbbc418fd'
require(current['state'] in ('unknown','awaiting_reboot') and current['result']['grant_id']==grant,'same immutable reboot attempt')
scope=dict(session['pins'],job_id=job['job_id'],attempt_id=job['attempt_id'])
before=cloud.bootstrap.rpc('linux-vm.conf',reboot.vm_state,scope)
require(before['root_unresolved']==1 and before['other_unresolved']==0 and before['selected_root_grants']==[grant]
and before['boot_id']!=session['old_boot_id'] and len(before['root_rows'])==len(session['before']['root_rows'])+1
and all(before['root_rows'].get(k)==v for k,v in session['before']['root_rows'].items()),'all old Root rows and exact consumed reboot preserved')
audit=reboot.evidence(target,'audit',operation_id=checkpoint['operation_id'])
require(audit==dict(origin='dev_test_browser_simulation',simulation=True,principal='simulated-testprincipal-a',plan_hash=checkpoint['plan']['plan_hash']),'original actual UI approval')
status=cloud.bootstrap.rpc('server.conf',upgrade.status,dict(id=candidate))
require(status['finished'] and status['exit_code']==0 and status['source_unchanged'] and status['runtime_tag_unchanged'],'qualified separate server image')
policy=cloud.bootstrap.rpc('server.conf',reboot.server_policy,dict(action='state'))
if not policy['closed']:
policy=cloud.bootstrap.rpc('server.conf',reboot.server_policy,dict(id=sid,action='close',previous_sha256=policy['policy_sha256']))
cloud.contract.write_private(folder/'policy-closed-for-upgrade.json',policy)
transport=dict(agent_id=target['agent_id'],agent_sha256=session['pins']['agent_sha256'],old_boot_id=session['old_boot_id'],new_boot_id=before['boot_id'],grant_id=grant,job_id=job['job_id'],attempt_id=job['attempt_id'])
stopped=cloud.bootstrap.rpc('linux-vm.conf',agent_transport,dict(transport,action='stop'))
frozen=cloud.bootstrap.rpc('linux-vm.conf',reboot.vm_state,scope)
require(frozen['root_rows']==before['root_rows'] and frozen['database_inodes']==before['database_inodes'] and frozen['services']['rmm-agent-remote.service']['ActiveState']=='inactive','quiesced agent and original evidence retained')
cloud.contract.write_private(folder/'proof-upgrade-before.json',dict(candidate=candidate,gate=gate(),current=current,native=frozen,transport=stopped,policy=policy))
stream=io.BytesIO()
with tarfile.open(fileobj=stream,mode='w:') as archive:
for path in sorted((cloud.ROOT/'web/dist').rglob('*')):
if path.is_file():
require(not path.is_symlink(),'built UI symlink');archive.add(path,arcname=str(path.relative_to(cloud.ROOT/'web/dist')),recursive=False)
raw=stream.getvalue()
result=cloud.bootstrap.rpc('server.conf',upgrade.deploy,dict(id=candidate,closed_policy_sha256=policy['policy_sha256'],previous_ui_index_sha256='a3513f928e20ea11d53f1cd27e8735d911555e1c66d5d97ca8bdefaff60e71fa',web_tar=base64.b64encode(raw).decode(),web_sha256=hashlib.sha256(raw).hexdigest()))
after=cloud.bootstrap.rpc('linux-vm.conf',reboot.vm_state,scope)
require(result['deployed'] and gate()==dict(jobs=30,outbox=30,permits=26,unresolved=1) and after['root_rows']==frozen['root_rows'] and after['database_inodes']==frozen['database_inodes'],'deployment preserves exact unresolved effect before normal resumption')
cloud.contract.write_private(folder/'proof-upgrade-deployed.json',dict(verified=True,candidate=candidate,result=result,native=after,counts=gate()))
resumed=cloud.bootstrap.rpc('linux-vm.conf',agent_transport,dict(transport,action='start'))
cloud.contract.write_private(folder/'proof-upgrade-resumed.json',resumed)
print('PASS exact reboot evidence preserved during qualified server upgrade; ordinary signed resumption required; no second reboot')
if __name__=='__main__':
try:main()
except Exception as error:
message=str(error);print(message if message.startswith('remote bootstrap incomplete: ') else 'REBOOT_PROOF_UPGRADE_INCOMPLETE:'+type(error).__name__)
raise SystemExit(2) from None