#!/usr/bin/env python3 """LAST Linux-MVP gate: one fixed WebUI-approved Cloud-VM reboot. Bootstrap changes only the existing General policy/pin and an expiring central reboot-only scope. Bootstrap never requests a reboot. Unknown means stop, never retry; finish reads the same job and close restores the original policy/pin. """ import argparse import datetime as dt import hashlib import json import os from pathlib import Path import subprocess import time import uuid import cloud_apt_recovery as apt cloud=apt.cloud require=cloud.contract.require STATE=cloud.ROOT/'devstate/cloud-reboot' REASON='Explicit final Linux VM dev/test acceptance; reconnect within 180 seconds of the central start reservation' GATES=('apt_native_update','ensure_package','apply_updates','offline_wait','expired_offline','revoked_before_start','agent_crash','transport_fallback','mcp') def vm_state(payload): import sqlite3 root=Path('/var/lib/rmm-agent-remote');helper=Path('/var/lib/rmm-helper') require(json.loads((root/'identity.json').read_text())['agent_id']==payload['agent_id'],'permanent Cloud identity') for path,key in ((Path('/usr/local/libexec/rmm-agent-remote'),'agent_sha256'),(Path('/usr/local/libexec/rmm-privileged-helper'),'helper_sha256')): require(not path.is_symlink() and hashlib.sha256(path.read_bytes()).hexdigest()==payload[key],'qualified current binaries') require(hashlib.sha256(Path('/etc/systemd/system/rmm-helper-cloud.service').read_bytes()).hexdigest()==payload['general_unit_sha256'],'reviewed General D-Bus service profile with direct reboot syscalls denied') services={} for unit in ('rmm-agent-remote.service','rmm-helper-cloud.service','rmm-apt-helper.service'): raw=subprocess.run(['systemctl','show',unit,'-p','ActiveState','-p','MainPID','-p','UnitFileState'],capture_output=True,check=True,timeout=10).stdout.decode() services[unit]=dict(line.split('=',1) for line in raw.splitlines()) dropin=Path('/etc/systemd/system/rmm-agent-remote.service.d/30-cloud-root.conf') require(not dropin.is_symlink() and 'RMM_APT_HELPER_' not in dropin.read_text() and services['rmm-apt-helper.service']['ActiveState']=='inactive','close APT endpoint before final reboot gate') result=dict(boot_id=Path('/proc/sys/kernel/random/boot_id').read_text().strip(),services=services,root_rows={},database_inodes={}) for path in (root/'journal.sqlite',root/'consumption.sqlite',helper/'consumption.sqlite'): s=path.stat();result['database_inodes'][str(path)]=[s.st_dev,s.st_ino] with sqlite3.connect(path.as_uri()+'?mode=ro',uri=True) as db: require(db.execute('PRAGMA integrity_check').fetchone()[0]=='ok','preserved database integrity') if path==helper/'consumption.sqlite': 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) ORDER BY grant_id').fetchall() for row in rows: serial=[dict(sha256=hashlib.sha256(v).hexdigest(),size=len(v)) if isinstance(v,bytes) else v for v in row] result['root_rows'][row[0]]=hashlib.sha256(json.dumps(serial,separators=(',',':')).encode()).hexdigest() result['root_unresolved']=sum(row[5] is None for row in rows) require(db.execute('SELECT count(*) FROM helper_sync_meta WHERE fault IS NOT NULL').fetchone()[0]==0,'no helper fault') if payload.get('job_id'): selected=[r for r in rows if r[3]==payload['job_id'] and r[4]==payload['attempt_id']] result['selected_root_grants']=[r[0] for r in selected] row=db.execute('SELECT response FROM helper_sync_decisions WHERE decision_id=(SELECT last_decision FROM helper_sync_meta WHERE id=1)').fetchone() signed=json.loads(row[0]);decision=json.loads(bytes(signed['decision_bytes'])) result['signed_sync']=dict(decision_id=decision['decision_id'],ready=decision['ready'],helper_ready=decision['helper_ready'],boot_id=decision['boot_id'], selected=[a for a in decision['attempts'] if a['job_id']==payload['job_id'] and a['attempt_id']==payload['attempt_id']]) elif path==root/'journal.sqlite': require(db.execute('SELECT count(*) FROM reconcile_meta WHERE fault IS NOT NULL').fetchone()[0]==0,'no agent fault') result['agent_last_decision']=db.execute('SELECT last_decision FROM reconcile_meta WHERE id=1').fetchone()[0] if payload.get('job_id'): result['other_unresolved']=db.execute("SELECT count(*) FROM job_attempts WHERE NOT(job=? AND attempt=?) AND state NOT IN ('succeeded','failed','cancelled','expired')",(payload['job_id'],payload['attempt_id'])).fetchone()[0] else:result['other_unresolved']=db.execute("SELECT count(*) FROM job_attempts WHERE state NOT IN ('succeeded','failed','cancelled','expired')").fetchone()[0] config=Path('/etc/rmm-remote/helper-general.json');pin=Path('/etc/rmm-remote/general-helper-proof.json') result.update(config_sha256=hashlib.sha256(config.read_bytes()).hexdigest(),pin_sha256=hashlib.sha256(pin.read_bytes()).hexdigest(),general_pin=json.loads(pin.read_text())) return result def vm_policy(payload): import fcntl import sqlite3 os.umask(0o077) state=Path('/var/lib/rmm-agent-remote');helper=Path('/var/lib/rmm-helper');config=Path('/etc/rmm-remote/helper-general.json');pin=Path('/etc/rmm-remote/general-helper-proof.json') binary=Path('/usr/local/libexec/rmm-privileged-helper');agent=Path('/usr/local/libexec/rmm-agent-remote') require(hashlib.sha256(binary.read_bytes()).hexdigest()==payload['helper_sha256'] and hashlib.sha256(agent.read_bytes()).hexdigest()==payload['agent_sha256'],'same qualified runtime') require(json.loads((state/'identity.json').read_text())['agent_id']==payload['agent_id'],'same permanent identity') require(not config.is_symlink() and not pin.is_symlink() and hashlib.sha256(config.read_bytes()).hexdigest()==payload['config_sha256'] and hashlib.sha256(pin.read_bytes()).hexdigest()==payload['pin_sha256'],'exact General policy/pin required') enabled=payload['enabled'];require(type(enabled) is bool,'fixed policy direction') current=json.loads(config.read_text());require(current['allow_reboot'] is (not enabled),'only one explicit policy transition') backup=Path('/var/lib/rmm-bootstrap-history')/('cloud-reboot-'+str(uuid.UUID(payload['id']))+('-enable' if enabled else '-restore'));backup.mkdir(mode=0o700) def create(path,raw,mode=0o600,gid=0): fd=os.open(path,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,mode) with os.fdopen(fd,'wb') as f:f.write(raw);f.flush();os.fchown(f.fileno(),0,gid);os.fchmod(f.fileno(),mode);os.fsync(f.fileno()) def manifest(path): result={} for p in [path,*sorted(path.rglob('*'))]: s=p.lstat();require(not p.is_symlink() and (p.is_file() or p.is_dir()),'preserved state must be regular') result[str(p.relative_to(path))]=[s.st_dev,s.st_ino,hashlib.sha256(p.read_bytes()).hexdigest() if p.is_file() else None] return result subprocess.run(['systemctl','stop','rmm-agent-remote.service','rmm-helper-cloud.service'],capture_output=True,check=True,timeout=30) lock=os.open(helper/'executor.lock',os.O_RDWR|os.O_NOFOLLOW);fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB) try: with sqlite3.connect((helper/'consumption.sqlite').as_uri()+'?mode=ro',uri=True) as db: require(db.execute('SELECT count(*) FROM consumed WHERE result IS NULL').fetchone()[0]==0 and db.execute('SELECT count(*) FROM helper_sync_meta WHERE fault IS NOT NULL').fetchone()[0]==0,'all Root effects closed') preserved={str(p):manifest(p) for p in (state,helper,Path('/var/lib/rmm-apt-transaction'))} old_config=config.read_bytes();old_pin=pin.read_bytes() if enabled: next_config=dict(current,allow_reboot=True) else: original=Path(payload['original_backup']) require(original.parent==backup.parent and original.name=='cloud-reboot-'+payload['id']+'-enable','same versioned policy origin') original_raw=(original/'general-before.json').read_bytes();next_config=json.loads(original_raw) require(next_config==dict(current,allow_reboot=False),'restore only the exact original policy') for p,name in ((config,'general-before.json'),(pin,'pin-before.json')): require(p.stat().st_dev==backup.stat().st_dev,'same-filesystem versioned original') os.rename(p,backup/name) create(config,json.dumps(next_config).encode() if enabled else original_raw) output=subprocess.run([str(binary),'--inspect-general-profile'],capture_output=True,check=True,timeout=15,env=dict(os.environ,RMM_HELPER_CONFIG=str(config))).stdout new_pin=json.loads(output) require(new_pin['identity']==json.loads(old_pin)['identity'],'helper identity unchanged') if not enabled:require(new_pin==json.loads((Path(payload['original_backup'])/'pin-before.json').read_text()),'original pin restored') create(pin,json.dumps(new_pin).encode() if enabled else (Path(payload['original_backup'])/'pin-before.json').read_bytes(),0o640,984) require(all(manifest(Path(p))==v for p,v in preserved.items()),'journal/ledger/fixture/history bytes and inodes preserved before restart') report=dict(enabled=enabled,backup=str(backup),preserved_state=True,config_sha256=hashlib.sha256(config.read_bytes()).hexdigest(),pin_sha256=hashlib.sha256(pin.read_bytes()).hexdigest(),general_pin=new_pin,reboot_requested=False) create(backup/'report.json',json.dumps(report).encode()) finally:fcntl.flock(lock,fcntl.LOCK_UN);os.close(lock) subprocess.run(['systemctl','start','rmm-helper-cloud.service','rmm-agent-remote.service'],capture_output=True,check=True,timeout=30) return report def server_policy(payload): import datetime as dt root=Path('/opt/rust-discovery');path=root/'devstate/root-policy/policy.json' compose=['docker','compose','--env-file',str(root/'devstate/.env'),'-f',str(root/'deploy/server.compose.yaml')] require(subprocess.run(compose+['exec','-T','gateway','printenv','RMM_PROFILE','RMM_DEV_AUTH'],capture_output=True,check=True,timeout=15).stdout.splitlines()==[b'development',b'true'],'actual development gateway only') raw=path.read_bytes();policy=json.loads(raw);digest=hashlib.sha256(raw).hexdigest() require(not path.is_symlink() and path.stat().st_uid==10001 and path.stat().st_mode&0o777==0o600,'private root policy') if payload['action']=='state':return dict(policy_sha256=digest,closed=dt.datetime.fromisoformat(policy['expires_at'])<=dt.datetime.now(dt.timezone.utc)) require(digest==payload['previous_sha256'],'Root policy drift') if payload['action']=='enable': require(dt.datetime.fromisoformat(policy['expires_at'])<=dt.datetime.now(dt.timezone.utc),'previous policy must be closed') updated=payload['policy'];require(len(updated['scopes'])==1,'one final Cloud VM scope') scope=updated['scopes'][0] now=dt.datetime.now(dt.timezone.utc) require(updated['schema_version']==1 and updated['profile']=='development' and now