#!/usr/bin/env python3 """One versioned sandbox upgrade; preserves consumed attempts and failed handoff. No package retry, receipt creation, enrollment or marker reset. The new native probe runs only under the exact dedicated service profile, on a copied dpkg DB. APT endpoint remains absent from the agent configuration and is stopped after the probe. General helper resumes with the existing binding/shared ledger. """ import base64 import argparse import hashlib import json import os import uuid import cloud_apt_followup as followup cloud=followup.cloud require=cloud.require def upgrade(payload): import ast import fcntl import sqlite3 import time import traceback os.umask(0o077) base=Path('/var/lib/rmm-apt-transaction');ledger=Path('/var/lib/rmm-helper/consumption.sqlite') state=Path('/var/lib/rmm-agent-remote');binary=Path('/usr/local/libexec/rmm-privileged-helper') provider=Path('/usr/lib/rmm-apt-transaction/apt_transaction.py');config=Path('/etc/rmm/apt-helper.json') proof=Path('/etc/rmm-remote/apt-helper-proof.json');metadata=base/'install-metadata.json' def run(args,**kw):return subprocess.run(args,capture_output=True,check=True,timeout=90,**kw).stdout def sha(data):return hashlib.sha256(data).hexdigest() def create(path,data,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(data);f.flush();os.fsync(f.fileno());os.fchown(f.fileno(),0,gid);os.fchmod(f.fileno(),mode) def manifest(path): result={} for p in path.rglob('*'): require(not p.is_symlink(),'preserved state symlink') if p.is_file():result[str(p.relative_to(path))]=sha(p.read_bytes()) return result def ledger_guard(): with sqlite3.connect(ledger.as_uri()+'?mode=ro',uri=True) as db: require(db.execute('PRAGMA integrity_check').fetchone()[0]=='ok','ledger integrity') actual={r[0] for r in db.execute('SELECT grant_id FROM consumed')} require(actual==set(payload['expected_grants']) and 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,'exact known closed root attempts required') ledger_guard() require(json.loads((state/'identity.json').read_text())['agent_id']==payload['agent_id'],'cloud identity drift') with sqlite3.connect((state/'journal.sqlite').as_uri()+'?mode=ro',uri=True) as db: require(db.execute("SELECT count(*) FROM job_attempts WHERE state NOT IN ('succeeded','failed','cancelled','expired')").fetchone()[0]==0,'unresolved native job') require(run(['systemctl','show','rmm-apt-helper.service','-p','ActiveState','--value']).strip()==b'inactive','APT helper must be stopped') require(not list(base.glob('acceptance-*.json')) and not (base/'active/hook-armed.json').exists(),'existing acceptance/effect needs reconciliation') markers=list(base.glob('bootstrap-attempt-*.json'));require(len(markers)==1 and markers[0].name=='bootstrap-attempt-rmm-apt-fixture-install-1.0.json','preserve original failed attempt') for name,pin in payload['old_files'].items(): p=Path(name);require(not p.is_symlink() and p.stat().st_uid==0 and p.stat().st_mode&0o022==0 and sha(p.read_bytes())==pin,'installed file drift') require(Path('/etc/systemd/system/rmm-apt-helper.service').read_text()==payload['unit'],'reviewed exact APT profile required') raw=base64.b64decode(payload['binary'],validate=True);code=base64.b64decode(payload['provider'],validate=True) require(sha(raw)==payload['helper_sha256'] and raw[:6]==b'\x7fELF\x02\x01' and raw[18:20]==b'\x3e\x00','reviewed x64 binary required') require(sha(code)==payload['provider_sha256'],'reviewed provider required');ast.parse(code) old_meta=json.loads(metadata.read_text());require(old_meta['schema_version']==2 and old_meta['provider_sha256']==sha(provider.read_bytes()) and old_meta['hook_sha256']==sha(Path('/usr/lib/rmm-apt-transaction/hook').read_bytes()) and old_meta['unit_sha256']==sha(payload['unit'].encode()),'metadata drift') backup=Path('/var/lib/rmm-bootstrap-history')/('cloud-sandbox-'+payload['id']);backup.mkdir(mode=0o700) report=dict(stage='validated',completed=False,package_transactions=False,backups={}) def save(): p=backup/'.next.json';create(p,json.dumps(report,indent=2).encode());os.replace(p,backup/'report.json') def retain(p): target=backup/str(p).lstrip('/').replace('/','__') require(not p.is_symlink() and p.stat().st_dev==backup.stat().st_dev and not target.exists(),'exclusive same-filesystem backup') os.rename(p,target);report['backups'][str(p)]=str(target);save() stopped=False;lockfd=None try: save();staged=backup/'helper-next';create(staged,raw,0o755) run(['/lib64/ld-linux-x86-64.so.2','--list',str(staged)]) run(['systemctl','stop','rmm-agent-remote.service','rmm-helper-cloud.service']);stopped=True lockfd=os.open(ledger.parent/'executor.lock',os.O_RDWR|os.O_NOFOLLOW);fcntl.flock(lockfd,fcntl.LOCK_EX|fcntl.LOCK_NB) ledger_guard();inode=(ledger.stat().st_dev,ledger.stat().st_ino) preserved={str(p):manifest(p) for p in (state,Path('/var/lib/rmm-agent'),ledger.parent, base/'active',base/'archives',base/'baselines',base/'history',base/'log',base/'state')} marker=markers[0].read_bytes();old_general=json.loads(Path('/etc/rmm-remote/general-helper-proof.json').read_text()) old_config=json.loads(config.read_text()) for p in (binary,provider,config,metadata,proof,base/'native-readiness.json'):retain(p) os.rename(staged,binary);create(provider,code,0o755) create(metadata,json.dumps(dict(old_meta,provider_sha256=sha(code))).encode()) cmd=['/usr/bin/python3','-I',str(provider)] runtime=json.loads(run(cmd+['inspect-runtime']));runtime_sha=sha(json.dumps(runtime,sort_keys=True,separators=(',',':'),ensure_ascii=True).encode()) old_config['apt_provider']['runtime_sha256']=runtime_sha config_sha=sha(run(cmd+['print-config'])) require(config_sha==payload['reviewed_config_sha256'],'reviewed APT config required') old_config['apt_provider']['apt_config_sha256']=config_sha create(config,json.dumps(old_config).encode()) pin=json.loads(run([str(binary),'--inspect-apt-profile'],env=dict(os.environ,RMM_HELPER_CONFIG=str(config)))) require(pin['identity']==old_general['identity'],'helper identity changed') create(proof,json.dumps(pin).encode(),0o640,984) general=json.loads(run([str(binary),'--inspect-general-profile'],env=dict(os.environ,RMM_HELPER_CONFIG='/etc/rmm-remote/helper-general.json'))) require(general==old_general,'general policy changed') require(all(manifest(Path(p))==v for p,v in preserved.items()) and markers[0].read_bytes()==marker and (ledger.stat().st_dev,ledger.stat().st_ino)==inode,'preserved identity/attempt/ledger changed') report.update(stage='installed_preserved',preserved_state=True,runtime_sha256=runtime_sha,apt_config_sha256=config_sha,apt_pin=pin,general_pin=general, provider_sha256=sha(code),helper_sha256=sha(raw));save() fcntl.flock(lockfd,fcntl.LOCK_UN);os.close(lockfd);lockfd=None run(['systemd-analyze','verify','/etc/systemd/system/rmm-apt-helper.service']) run(['systemctl','daemon-reload']) run(['systemctl','start','rmm-helper-cloud.service','rmm-apt-helper.service']) for _ in range(10): for name in ('rmm-helper-cloud.service','rmm-apt-helper.service'): status=run(['systemctl','show',name,'-p','ActiveState','-p','SubState','-p','NRestarts']).decode() require('ActiveState=active\n' in status and 'SubState=running\n' in status and 'NRestarts=0\n' in status,'stable helper start required') time.sleep(1) readiness=json.loads((base/'native-readiness.json').read_text()) require(readiness['runtime']==runtime and readiness['readiness_only'] is True and readiness['native_acceptance'] is False,'actual new native probe required') ledger_guard();require(markers[0].read_bytes()==marker and manifest(base/'active')==preserved[str(base/'active')],'failed attempt changed during probe') require(not Path('/usr/share/rmm-mvp-test/version').exists(),'unexpected fixture payload') report.update(stage='native_sandbox_probe_passed',completed=True,readiness_only=True,native_acceptance=False, readiness_sha256=sha((base/'native-readiness.json').read_bytes()),probe_id=readiness['probe_id']);save() except Exception as error: report.update(error_class=type(error).__name__,frames=[dict(function=f.name,line=f.lineno) for f in traceback.extract_tb(error.__traceback__)]);save() finally: if lockfd is not None:os.close(lockfd) subprocess.run(['systemctl','stop','rmm-apt-helper.service'],capture_output=True,timeout=30) if stopped: run(['systemctl','start','rmm-helper-cloud.service','rmm-agent-remote.service']) return dict(report,backup=str(backup)) def installed_metadata(payload): paths=('/usr/local/libexec/rmm-privileged-helper','/usr/lib/rmm-apt-transaction/apt_transaction.py', '/etc/systemd/system/rmm-apt-helper.service','/etc/rmm/apt-helper.json','/etc/rmm-remote/apt-helper-proof.json') values={name:hashlib.sha256(Path(name).read_bytes()).hexdigest() for name in paths} require(values[paths[0]]==payload['helper_sha256'] and values[paths[1]]==payload['provider_sha256'],'previous reviewed artifacts drifted') require(json.loads(Path(paths[3]).read_text())['apt_provider']['runtime_sha256']==payload['runtime_sha256'] and json.loads(Path(paths[4]).read_text())==payload['apt_pin'],'previous independent runtime/proof drifted') return dict(files=values) def main(): parser=argparse.ArgumentParser();group=parser.add_mutually_exclusive_group() group.add_argument('--local-file-fix',action='store_true') group.add_argument('--native-retry',action='store_true',help='One versioned signed-retry upgrade; never a package retry itself') args=parser.parse_args() os.umask(0o077);m=cloud.contract require(followup.evidence('gate')['unresolved']==0,'unresolved central job') diagnosis=m.protected_json(followup.STATE/'diagnosis/checkpoint.json');require(diagnosis.get('verified') is True,'diagnostic evidence required') reports=list(followup.STATE.glob('sandbox-*.json')) if args.local_file_fix or args.native_retry: require(len(reports)==(2 if args.native_retry else 1),'exact prior successful sandbox upgrades required; never repeat migration') previous=[m.protected_json(p) for p in reports] require(all(p['completed'] is True for p in previous),'previous migration incomplete') pins=m.protected_json(followup.STATE/('provider-before-native-retry.json' if args.native_retry else 'provider-before-local-file-fix.json')) candidates=[p for p in previous if p['runtime_sha256']==pins.get('runtime_sha256')] if args.native_retry else previous require(len(candidates)==1,'reviewed prior runtime required');prior=candidates[0] old=cloud.bootstrap.rpc('linux-vm.conf',installed_metadata,dict(pins,runtime_sha256=prior['runtime_sha256'],apt_pin=prior['apt_pin'])) artifact=(followup.STATE/('helper-native-retry' if args.native_retry else 'helper-local-file-fixed')).read_bytes() folders=[cloud.ROOT/'devstate/cloud-root/cases'/name for name in ('service_setup','service_start','service_stop','apt_native_install')] folders += [followup.STATE/name for name in ('diagnosis','diagnosis-acquisition','diagnosis-hook','diagnosis-local-hook')] expected=[str(uuid.UUID(m.protected_json(p/'checkpoint.json')['result']['result']['grant_id'])) for p in folders] require(len(set(expected))==8,'eight actual completed root attempts required') else: require(not reports,'existing sandbox upgrade retained; no implicit repeat') preflights=list(followup.STATE.glob('preflight-*.json'));require(len(preflights)==1,'one reviewed installed baseline') old=m.protected_json(preflights[0]);artifact=(followup.STATE/'helper-sandbox-fixed').read_bytes() folders=[cloud.ROOT/'devstate/cloud-root/cases'/name for name in ('service_setup','service_start','service_stop','apt_native_install')]+[followup.STATE/'diagnosis'] expected=[str(uuid.UUID(m.protected_json(p/'checkpoint.json')['result']['result']['grant_id'])) for p in folders] code=(cloud.ROOT/'tools/apt-transaction/apt_transaction.py').read_bytes() import importlib.util import sys spec=importlib.util.spec_from_file_location('reviewed_apt_provider',cloud.ROOT/'tools/apt-transaction/apt_transaction.py') module=importlib.util.module_from_spec(spec);sys.modules[spec.name]=module;spec.loader.exec_module(module) payload=dict(id=str(uuid.uuid4()),agent_id=followup.identity()['agent_id'],old_files=old['files'], binary=base64.b64encode(artifact).decode(),helper_sha256=hashlib.sha256(artifact).hexdigest(), provider=base64.b64encode(code).decode(),provider_sha256=hashlib.sha256(code).hexdigest(),unit=(cloud.ROOT/'deploy/linux/rmm-apt-helper.service').read_text(), expected_grants=expected,reviewed_config_sha256=hashlib.sha256(module.apt_config()).hexdigest()) result=cloud.bootstrap.rpc('linux-vm.conf',upgrade,payload) m.write_private(followup.STATE/('sandbox-'+payload['id']+'.json'),result) print({k:result.get(k) for k in ('completed','stage','error_class','frames','preserved_state','runtime_sha256','readiness_only')}) require(result['completed'],'native probe incomplete; preserve reports, never repeat installation') if __name__=='__main__': try:main() except Exception as error: print('APT_SANDBOX_UPGRADE_INCOMPLETE:'+type(error).__name__);raise SystemExit(2) from None