#!/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>'boot_id' FROM audit_events WHERE tenant_id=a.tenant_id AND event_type='start_reserved' AND data->>'job_id'='"+job+"' AND data->>'grant_id'=p.grant_id::text)," "'inventory_message_id',o.message_id,'inventory_boot_id',o.boot_id,'inventory_received_at',o.received_at) FROM agent_installations a " "JOIN execution_permits p ON p.tenant_id=a.tenant_id AND p.job_id='"+job+"' JOIN observations o ON o.tenant_id=a.tenant_id AND o.agent_id=a.agent_id AND o.message_id='"+receipt+"' " "WHERE a.tenant_id='"+tenant+"' AND a.agent_id='"+agent+"';") 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'] raw=subprocess.run(cmd,input=query.encode(),capture_output=True,check=True,timeout=15).stdout.strip();require(raw,'bound authenticated reboot/inventory evidence required') return json.loads(raw) def prerequisites(path,target): m=cloud.contract;manifest=m.protected_json(Path(path));require(manifest['schema_version']==1 and manifest['target']==target and set(manifest['checks'])==set(GATES),'all earlier MVP gates for this permanent target') selected={} for name,item in manifest['checks'].items(): p=Path(item['path']).resolve();require(p.is_relative_to(cloud.ROOT/'devstate') and hashlib.sha256(p.read_bytes()).hexdigest()==item['sha256'],'exact protected native gate witness') data=m.protected_json(p);require(data.get('verified') is True,'native gate not verified');selected[name]=data for name in ('apt_native_update','ensure_package','apply_updates'): result=selected[name]['result'];proof=result['result']['evidence']['apt'] require(result['state']=='succeeded' and proof['packages']==[dict(name='rmm-mvp-test',architecture='all',version='1.1')] and proof['fixture_payload_sha256']==hashlib.sha256(b'1.1\n').hexdigest(),'earlier approved stored fixture1.1 postcondition; no SSH package scan') return dict(path=str(Path(path).resolve()),sha256=hashlib.sha256(Path(path).read_bytes()).hexdigest(),checks=manifest['checks']) def fixed(): installed=cloud.contract.protected_json(cloud.ROOT/'devstate/remote-vm.json');runtime,_=apt.runtime_session() return dict(tenant_id=cloud.TENANT,**installed['identity']),dict(agent_id=installed['identity']['agent_id'],agent_sha256=installed['binary_sha256'],helper_sha256=runtime['helper_sha256'],general_unit_sha256=hashlib.sha256((cloud.ROOT/'deploy/linux/rmm-helper-cloud.service').read_bytes()).hexdigest()) def evidence(target,kind,**extra):return cloud.bootstrap.rpc('server.conf',cloud.server,dict(kind=kind,tenant_id=target['tenant_id'],agent_id=target['agent_id'],**extra)) def browser(path): cloud.prepare_browser();result=subprocess.run(['node',str(cloud.ROOT/'deploy/browser/cloud_reboot_acceptance.cjs'),str(path)],capture_output=True,timeout=120) require(result.returncode==0,'actual reboot UI incomplete; retain fixed checkpoint, never repeat approval') def enable(path): m=cloud.contract;target,pins=fixed();gates=prerequisites(path,target) require(evidence(target,'gate')['unresolved']==0,'no earlier unresolved work') old=cloud.bootstrap.rpc('server.conf',server_policy,dict(action='state'));require(old['closed'],'all earlier Root scopes must be closed') before=cloud.bootstrap.rpc('linux-vm.conf',vm_state,pins) require(before['root_unresolved']==before['other_unresolved']==0,'healthy fully reconciled runtime') for name in ('rmm-agent-remote.service','rmm-helper-cloud.service'): require(before['services'][name]['ActiveState']=='active' and before['services'][name]['UnitFileState']=='enabled','agent and General helper must already autostart after boot') ident=str(uuid.uuid4());folder=STATE/ident;folder.mkdir(mode=0o700,parents=True) session=dict(id=ident,target=target,pins=pins,prerequisites=gates,before=before,old_boot_id=before['boot_id']) m.write_private(folder/'before.json',session) changed=cloud.bootstrap.rpc('linux-vm.conf',vm_policy,dict(pins,id=ident,enabled=True,config_sha256=before['config_sha256'],pin_sha256=before['pin_sha256'])) m.write_private(folder/'helper-enabled.json',changed) scope={k:changed['general_pin']['identity'][k] for k in ('tenant_id','asset_id','agent_id','enrollment_generation','assignment_version')} scope.update(principals=['simulated-testprincipal-a'],helper_policy_sha256=changed['general_pin']['policy_sha256'],acceptance_evidence_id='final-cloud-reboot:'+ident,allowed_services=[],allow_root_scripts=False,allow_reboot=True) policy=dict(schema_version=1,profile='development',expires_at=(m.now()+dt.timedelta(minutes=30)).isoformat(),scopes=[scope]) opened=cloud.bootstrap.rpc('server.conf',server_policy,dict(id=ident,action='enable',previous_sha256=old['policy_sha256'],policy=policy)) m.write_private(folder/'policy-open.json',dict(opened,policy=policy));print(dict(id=ident,reboot_requested=False,reboot_only_limits_enabled=True)) def accept(ident): m=cloud.contract;folder=STATE/str(uuid.UUID(ident));session=m.protected_json(folder/'before.json');target=session['target'];pins=session['pins'] prerequisites(session['prerequisites']['path'],target) require(not (folder/'prepare.json').exists() and evidence(target,'gate')['unresolved']==0,'one new fixed attempt only') deadline=time.monotonic()+100 while True: device=cloud.Backend().call('inventory.get_device',{k:target[k] for k in ('tenant_id','asset_id')}) if device['presence']=='online' and {'reboot','root:reboot'}<=set(device['capabilities']):break require(time.monotonic()