#!/usr/bin/env python3 """Requalify closed APT runtime after the same reboot has finished and closed. Updates measurements only, using the existing copied-dpkg ExecStartPre probe. No binary/provider replacement, package command, endpoint exposure or approval. """ import argparse import hashlib import json import os from pathlib import Path import uuid import cloud_reboot_capacity_upgrade as capacity cloud=capacity.cloud require=cloud.contract.require def requalify(payload): import contextlib import fcntl import sqlite3 import traceback os.umask(0o077) base=Path('/var/lib/rmm-apt-transaction');state=Path('/var/lib/rmm-agent-remote');root=Path('/var/lib/rmm-helper') helper=Path('/usr/local/libexec/rmm-privileged-helper');agent=Path('/usr/local/libexec/rmm-agent-remote') 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';ready_file=base/'native-readiness.json' general_config=Path('/etc/rmm-remote/helper-general.json');general_proof=Path('/etc/rmm-remote/general-helper-proof.json') dropin=Path('/etc/systemd/system/rmm-agent-remote.service.d/30-cloud-root.conf') def sha(raw):return hashlib.sha256(raw).hexdigest() def canonical(value):return json.dumps(value,sort_keys=True,separators=(',',':'),ensure_ascii=True).encode() def run(args,**kwargs):return subprocess.run(args,capture_output=True,check=True,timeout=30,**kwargs).stdout def sync(path): fd=os.open(path,os.O_RDONLY|os.O_DIRECTORY|os.O_NOFOLLOW) try:os.fsync(fd) finally:os.close(fd) 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()) sync(path.parent) def manifest(path): if not path.exists():return {'absent':True} result={} for p in [path,*sorted(path.rglob('*'))]: s=p.lstat();require(not p.is_symlink() and (p.is_file() or p.is_dir()),'regular preserved tree') result[str(p.relative_to(path))]=dict(device=s.st_dev,inode=s.st_ino,uid=s.st_uid,gid=s.st_gid,mode=s.st_mode, sha256=sha(p.read_bytes()) if p.is_file() else None) return result def guard(): require(payload['session_id']=='d015c3ef-f3aa-4522-a3c2-fcb6099df17b' and len(payload['root_rows'])==18,'one finished original reboot and all18 Root rows') require(json.loads((state/'identity.json').read_bytes())['agent_id']==payload['agent_id'] and Path('/proc/sys/kernel/random/boot_id').read_text().strip()==payload['new_boot_id'],'same permanent device/newboot') require(all(sha(p.read_bytes())==payload[k] for p,k in ((agent,'agent_sha256'),(helper,'helper_sha256'),(provider,'provider_sha256'))),'qualified binaries/provider unchanged') require(sha(general_config.read_bytes())==payload['general_config_sha256'] and sha(general_proof.read_bytes())==payload['general_pin_sha256'] and json.loads(general_config.read_bytes())['allow_reboot'] is False,'exact restored non-reboot General policy') require('RMM_APT_HELPER_' not in dropin.read_text() and run(['systemctl','show','rmm-apt-helper.service','-p','ActiveState','--value']).strip()==b'inactive','APT remains unexposed/inactive') ledger=root/'consumption.sqlite';s=ledger.stat();require([s.st_dev,s.st_ino]==payload['database_inodes'][str(ledger)],'original Root inode') for name,inode in payload['database_inodes'].items(): s=Path(name).stat();require([s.st_dev,s.st_ino]==inode,'all original database inodes') with contextlib.closing(sqlite3.connect(ledger.as_uri()+'?mode=ro',uri=True)) as db: require(db.execute('PRAGMA integrity_check').fetchone()[0]=='ok' 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,'all Root effects definitively closed') 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() actual={r[0]:sha(json.dumps([dict(sha256=sha(v),size=len(v)) if isinstance(v,bytes) else v for v in r],separators=(',',':')).encode()) for r in rows} require(actual==payload['root_rows'],'all original closed Root rows unchanged') strict_metadata={'schema_version','provider_sha256','hook_sha256','unit_sha256'} def resume_inputs(): failed=payload['failed'];previous=Path(failed['backup']) require(previous==Path('/var/lib/rmm-bootstrap-history/post-reboot-apt-readiness-dd358cdd-d60c-4e98-8f00-cea46d8347c3') and json.loads((previous/'report.json').read_bytes())==failed and not failed['completed'] and failed['stage']=='repinned_preserved', 'exact retained failed qualification report; never repeat original maintenance') require(all(Path(failed['backups'][str(p)])==previous/str(p).lstrip('/').replace('/','__') for p in (config,proof,metadata,ready_file)), 'fixed original versioned artifacts only') expected=json.loads(Path(failed['backups'][str(config)]).read_bytes()) expected['apt_provider'].update(runtime_sha256=failed['runtime_sha256'],apt_config_sha256=failed['apt_config_sha256']) require(config.read_bytes()==json.dumps(expected).encode() and proof.read_bytes()==json.dumps(failed['apt_pin']).encode(), 'correct config/proof bytes remain unchanged') current_raw=metadata.read_bytes();original=Path(failed['backups'][str(metadata)]).read_bytes() old=json.loads(original);current=json.loads(current_raw) require(sha(current_raw)==payload['invalid_metadata_sha256'] and sha(original)==payload['original_metadata_sha256'] and set(old)==strict_metadata and old['schema_version']==2 and old['provider_sha256']==payload['provider_sha256'] and set(current)==strict_metadata|{'helper_sha256','runtime_sha256','apt_config_sha256'} and all(current[k]==v for k,v in old.items()) and current['helper_sha256']==payload['helper_sha256'] and current['runtime_sha256']==failed['runtime_sha256'] and current['apt_config_sha256']==failed['apt_config_sha256'] and not ready_file.exists(),'only known extra metadata fields may be corrected; original readiness remains archived') return failed,original guard() if payload.get('resume_failed'):resume_inputs() if payload['preflight_only']:return dict(preflight_only=True,maintenance_mutations=False,root_consumed=18,root_unresolved=0,apt_endpoint_enabled=False) folder=Path('/var/lib/rmm-bootstrap-history')/('post-reboot-apt-readiness-'+str(uuid.UUID(payload['id'])));folder.mkdir(mode=0o700) report=dict(completed=False,backup=str(folder),stage='validated',readiness_only=True,native_acceptance=False,apt_endpoint_enabled=False, jobs_created=False,grants_created=False,package_effects=False,reboot_requested=False,backups={}) def save(): create(folder/'.report-next.json',json.dumps(report,indent=2).encode());os.replace(folder/'.report-next.json',folder/'report.json');sync(folder) def replace(path,raw=None,mode=0o600,gid=0): require(path.is_file() and not path.is_symlink() and path.stat().st_dev==folder.stat().st_dev,'version only known same-filesystem artifact') old=folder/str(path).lstrip('/').replace('/','__');require(not old.exists(),'versioned destination exclusive') if raw is not None:create(folder/'.artifact-next',raw,mode,gid) os.rename(path,old);sync(path.parent);sync(folder);report['backups'][str(path)]=str(old);save() if raw is not None:os.rename(folder/'.artifact-next',path);sync(path.parent) lock=None try: save();run(['systemctl','stop','rmm-agent-remote.service','rmm-helper-cloud.service']) lock=os.open(root/'executor.lock',os.O_RDWR|os.O_NOFOLLOW);fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB);guard() preserved={str(p):manifest(p) for p in (state,root,Path('/var/lib/rmm-agent'))};old_base=manifest(base) controls={} for p in (agent,helper,provider,general_config,general_proof,dropin,Path('/var/lib/dpkg/status'),Path('/usr/share/rmm-mvp-test/version')): s=p.stat();controls[str(p)]=(s.st_dev,s.st_ino,sha(p.read_bytes())) command=['/usr/bin/python3','-I',str(provider)];config_bytes=run(command+['print-config']);config_sha=sha(config_bytes) require(config_sha==payload['apt_config_sha256'] and all(v in config_bytes for v in (b'DPkg::ConfigurePending "false";',b'DPkg::TriggersPending "false";',b'dpkg::selection::remove::approved "false";')),'unchanged exact reviewed APT parser configuration') runtime=json.loads(run(command+['inspect-runtime']));runtime_sha=sha(canonical(runtime)) if payload.get('resume_failed'): failed,original_metadata=resume_inputs();previous=Path(failed['backup']) require(runtime_sha==failed['runtime_sha256'] and config_sha==failed['apt_config_sha256'],'measured runtime remains the already repinned runtime') pin=failed['apt_pin'];replace(metadata,original_metadata) report.update(resumed_failed_report_sha256=sha(canonical(failed)),corrected_metadata_sha256=sha(original_metadata),original_failed_backup=str(previous)) else: current=json.loads(config.read_bytes());current['apt_provider'].update(runtime_sha256=runtime_sha,apt_config_sha256=config_sha) replace(config,json.dumps(current).encode()) pin=json.loads(run([str(helper),'--inspect-apt-profile'],env=dict(os.environ,RMM_HELPER_CONFIG=str(config)))) require(pin['identity']==json.loads(general_proof.read_bytes())['identity'],'same APT identity') replace(proof,json.dumps(pin).encode(),0o640,984) # InstallMetadata is a strict four-field Rust struct. Runtime and # helper measurements belong in config/proof/report, never here. metadata_bytes=metadata.read_bytes();meta=json.loads(metadata_bytes) require(set(meta)==strict_metadata and meta['schema_version']==2 and meta['provider_sha256']==payload['provider_sha256'], 'strict original installation metadata') replace(metadata,metadata_bytes);replace(ready_file) def preserved_state(probe_id=None): require(all(manifest(Path(p))==v for p,v in preserved.items()),'identities/journals/consumptions bytes and inodes unchanged') current_base=manifest(base);exceptions={'install-metadata.json','native-readiness.json'} require(all(current_base.get(k)==v for k,v in old_base.items() if k not in exceptions),'all original APT archives/history/attempts/acceptances unchanged') require(all(k in old_base or k in exceptions or probe_id and (k=='probes/'+probe_id or k.startswith('probes/'+probe_id+'/')) for k in current_base),'only fixed new copied probe artifacts allowed') for p,v in controls.items(): path=Path(p);s=path.stat();require((s.st_dev,s.st_ino,sha(path.read_bytes()))==v,'original control/package file bytes and inode unchanged') preserved_state();report.update(stage='repinned_preserved',preserved_state=True,agent_sha256=payload['agent_sha256'],helper_sha256=payload['helper_sha256'], provider_sha256=payload['provider_sha256'],runtime_sha256=runtime_sha,apt_config_sha256=config_sha,apt_pin=pin,general_pin=json.loads(general_proof.read_bytes()));save() fcntl.flock(lock,fcntl.LOCK_UN);os.close(lock);lock=None run(['systemctl','start','rmm-apt-helper.service']) require(run(['systemctl','show','rmm-apt-helper.service','-p','ActiveState','--value']).strip()==b'active','native copied-database ExecStartPre passed') run(['systemctl','stop','rmm-apt-helper.service']) lock=os.open(root/'executor.lock',os.O_RDWR|os.O_NOFOLLOW);fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB) ready=json.loads(ready_file.read_bytes());probe_id=str(uuid.UUID(ready['probe_id']));workspace=base/'probes'/probe_id require(ready['runtime']==runtime and ready['readiness_only'] is True and ready['native_acceptance'] is False and ready['host_status_sha256']==sha(Path('/var/lib/dpkg/status').read_bytes()) and (workspace/'dpkg/status').read_bytes()==Path('/var/lib/dpkg/status').read_bytes(),'actual immutable copied host database and measured runtime') preserved_state(probe_id);guard() report.update(stage='native_readiness_preserved',readiness_sha256=sha(ready_file.read_bytes()),probe_id=probe_id, journals_preserved_before_restart=True,root_consumed=18,root_unresolved=0,apt_runtime_ready=True);save() fcntl.flock(lock,fcntl.LOCK_UN);os.close(lock);lock=None run(['systemctl','start','rmm-helper-cloud.service','rmm-agent-remote.service']) require(run(['systemctl','show','rmm-apt-helper.service','-p','ActiveState','--value']).strip()==b'inactive','APT remains stopped') report.update(stage='closed_requalified_runtime_started',completed=True);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 lock is not None:fcntl.flock(lock,fcntl.LOCK_UN);os.close(lock) subprocess.run(['systemctl','stop','rmm-apt-helper.service'],capture_output=True,timeout=20) return report def build_payload(preflight_only,resume_failed=False): m=cloud.contract;folder=capacity.reboot.STATE/capacity.SESSION session=m.protected_json(folder/'before.json');closed=m.protected_json(folder/'closed.json');result=m.protected_json(folder/'result.json') report=m.protected_json(folder/'capacity-runtime.json') require(closed['verified'] and result['verified'] and result['result']['state']=='succeeded' and report['completed'],'original one reboot finished and restored before APT requalification') payload=dict(id=str(uuid.uuid4()),session_id=capacity.SESSION,agent_id=session['target']['agent_id'],new_boot_id=result['native']['boot_id'], root_rows=result['native']['root_rows'],database_inodes=result['native']['database_inodes'],agent_sha256=report['agent_sha256'],helper_sha256=report['helper_sha256'], provider_sha256=report['provider_sha256'],general_config_sha256=closed['helper']['config_sha256'],general_pin_sha256=closed['helper']['pin_sha256'], apt_config_sha256=m.protected_json(cloud.ROOT/'devstate/cloud-apt-recovery/configured-reconcile-runtime.json')['apt_config_sha256'],preflight_only=preflight_only) if resume_failed: failed=m.protected_json(folder/'post-reboot-apt-readiness.json') schema=m.protected_json(cloud.ROOT/'devstate/post-apt-readiness-metadata-schema.json') require(failed['completed'] is False and failed['stage']=='repinned_preserved','one retained failed runtime qualification') require(schema['original_strict_schema'] is True and schema['original_fields_unchanged'] is True and schema['current_sha256']=='f55a784e8c29bc3fe60099bcb7bac6a96d63cf6be39276f29efc8d35ea42942a' and schema['previous_sha256']=='5566bc269e22d05176a7bd469935b9447bf6a37bf99850241ef68ada11966ad0', 'actual independently captured strict metadata mismatch required') payload.update(resume_failed=True,failed=failed, invalid_metadata_sha256='f55a784e8c29bc3fe60099bcb7bac6a96d63cf6be39276f29efc8d35ea42942a', original_metadata_sha256='5566bc269e22d05176a7bd469935b9447bf6a37bf99850241ef68ada11966ad0') return payload def main(): p=argparse.ArgumentParser(description=__doc__);p.add_argument('--preflight-only',action='store_true');p.add_argument('--resume-failed',action='store_true');args=p.parse_args();os.umask(0o077) m=cloud.contract;folder=capacity.reboot.STATE/capacity.SESSION;payload=build_payload(args.preflight_only,args.resume_failed) policy=cloud.bootstrap.rpc('server.conf',capacity.reboot.server_policy,dict(action='state'));require(policy['closed'],'all central Root scopes closed') target=m.protected_json(folder/'before.json')['target'];require(capacity.reboot.evidence(target,'gate')['unresolved']==0,'no unresolved central jobs') name='post-reboot-apt-'+('recovery-preflight' if args.preflight_only else 'recovery') if args.resume_failed else 'post-reboot-apt-'+('preflight' if args.preflight_only else 'readiness') require(not (folder/(name+'.json')).exists(),'existing qualification retained; inspect before any explicit recovery') m.write_private(folder/(name+'-intent.json'),payload) report=cloud.bootstrap.rpc('linux-vm.conf',requalify,payload);m.write_private(folder/(name+'.json'),report) require(report.get('preflight_only') if args.preflight_only else report.get('completed'),'qualification incomplete; preserve report') print('PASS post-reboot APT runtime measurements/readiness; endpoint and scopes remain closed; no package acceptance') if __name__=='__main__': try:main() except Exception as error: message=str(error);print(message if message.startswith('remote bootstrap incomplete: ') else 'POST_REBOOT_APT_READINESS_INCOMPLETE:'+type(error).__name__) raise SystemExit(2) from None