#!/usr/bin/env python3 """Read/audit ONLY the existing 337 configured attempt; never execute/unlock.""" import hashlib import json import os from pathlib import Path import uuid import cloud_mvp_acceptance as cloud GRANT='33775478-15b0-46ec-b059-44f500dbb0b6' PRIOR='31ef5e5e-5dbd-4428-a9a1-cbd15de2f5a0' PARTIAL='dc3bf3870ea77c12f9291bac712c53ae72194eaec980a8646155a7fb5c0dbe21' CODE='APT_RECOVERY_CONFIGURED_ACCEPTANCE_REBOOT_FLAG_VERIFIED' STATE=cloud.ROOT/'devstate/cloud-apt-recovery/capacity-recovery' def stored(payload): import sqlite3 grant='33775478-15b0-46ec-b059-44f500dbb0b6' code='APT_RECOVERY_CONFIGURED_ACCEPTANCE_REBOOT_FLAG_VERIFIED' path=Path('/var/lib/rmm-helper/consumption.sqlite') with sqlite3.connect(path.as_uri()+'?mode=ro',uri=True) as db: require(db.execute('PRAGMA integrity_check').fetchone()[0]=='ok','root ledger integrity') row=db.execute('SELECT c.job,c.attempt,c.result,e.start,e.outcome FROM consumed c JOIN execution_evidence e USING(grant_id) WHERE c.grant_id=?',(grant,)).fetchone() require(row is not None,'one exact retained configured attempt') job,attempt,result,start,outcome=row require(job==payload['job_id'] and attempt==payload['attempt_id'],'original job/attempt') names=[r[1] for r in db.execute('PRAGMA table_info(apt_effect_reconciliations)')] proofrow=db.execute('SELECT * FROM apt_effect_reconciliations WHERE grant_id=?',(grant,)).fetchone() if names else None record=dict(zip(names,proofrow)) if proofrow else None original=json.loads(record['previous_outcome']) if record else None proof=json.loads(record['proof']) if record else None request=json.loads(start)['request'] # Match the original Request struct codec, without reading any key. request_raw=json.dumps({k:request[k] for k in ('job_bytes','permit_bytes','signature','plan_hash','authorization_id')},separators=(',',':')).encode() binding=bool(record and hashlib.sha256(request_raw).hexdigest()==record['original_request_sha256'] and hashlib.sha256(record['previous_outcome']).hexdigest()==record['previous_outcome_sha256'] and hashlib.sha256(record['proof']).hexdigest()==record['proof_sha256'] and original['state']=='unknown' and original['error_code']=='FIXTURE_UNEXPECTED_REBOOT_REQUIRED' and proof['grant_id']==grant and proof['job_id']==job and proof['attempt_id']==attempt and proof['job_payload_sha256']==hashlib.sha256(bytes(request['job_bytes'])).hexdigest() and proof['effect_complete'] is True and proof['postconditions_passed'] is False and proof['reboot_required'] is True and proof['host_reboot_required'] is True) classified=json.loads(outcome) if outcome else None;finished=json.loads(result) if result else None closed=bool(binding and finished==classified==json.loads(record['classified_outcome']) and finished['state']=='failed' and finished['error_code']==code and finished['evidence']['effect_complete'] is True and finished['evidence']['postconditions_passed'] is False and finished['reboot_required'] is True and finished['evidence']['apt']['host_reboot_required_before'] is None) partials=[dict(grant_id=g,proof_sha256=s) for g,s in db.execute('SELECT grant_id,proof_sha256 FROM apt_partial_reconciliations')] stats=dict(consumed=db.execute('SELECT count(*) FROM consumed').fetchone()[0],unresolved=db.execute('SELECT count(*) FROM consumed WHERE result IS NULL').fetchone()[0],faults=db.execute('SELECT count(*) FROM helper_sync_meta WHERE fault IS NOT NULL').fetchone()[0]) result=dict(ledger=stats,classified=classified,original_unknown=original,proof=proof,proof_sha256=record['proof_sha256'] if record else None, immutable_binding_verified=binding,closed_by_normal_sync=closed,result_bytes_equal_outcome=result==outcome,partial_rows=partials, effect_reconciliation_rows=db.execute('SELECT count(*) FROM apt_effect_reconciliations').fetchone()[0] if names else 0) base=Path('/var/lib/rmm-apt-transaction') result['forbidden_acceptance_records']={name:(base/name).exists() for name in ('acceptance-rmm-apt-fixture-install-1.0.json','pending-acceptance-rmm-apt-fixture-install-1.0.json', 'recovery-31ef5e5e-5dbd-4428-a9a1-cbd15de2f5a0.json','pending-recovery-31ef5e5e-5dbd-4428-a9a1-cbd15de2f5a0.json')} return result def main(): os.umask(0o077) checkpoint=cloud.contract.protected_json(STATE/'cases/apt_configure_recovery/checkpoint.json') job=checkpoint['plan']['jobs'][0] report=cloud.bootstrap.rpc('linux-vm.conf',stored,dict(job_id=job['job_id'],attempt_id=job['payload']['attempt_id'])) identity=cloud.contract.protected_json(cloud.ROOT/'devstate/remote-vm.json')['identity'] counts=cloud.bootstrap.rpc('server.conf',cloud.server,dict(kind='gate',tenant_id=cloud.TENANT,agent_id=identity['agent_id'])) result=cloud.Backend().call('jobs.get',dict(tenant_id=cloud.TENANT,job_id=job['job_id'])) cloud.contract.write_private(STATE/('configured-closure-'+uuid.uuid4().hex+'.json'),dict(native=report,counts=counts,central=result)) cloud.contract.require(report['immutable_binding_verified'] and report['closed_by_normal_sync'] and report['result_bytes_equal_outcome'] and report['ledger']==dict(consumed=11,unresolved=0,faults=0) and report['partial_rows']==[dict(grant_id=PRIOR,proof_sha256=PARTIAL)] and report['effect_reconciliation_rows']==1 and not any(report['forbidden_acceptance_records'].values()) and counts==dict(jobs=20,outbox=20,permits=18,unresolved=0) and result['state']=='failed' and result['result']['error_code']==CODE, 'same-attempt classified failure and normal signed closure not yet proven') print('PASS exact337 failed acceptance/effect complete, truthful host rebootrequired, immutable oldunknown/partial proof, normal Sync closed Root11/0/fault0 central20/20/18/0; no success/retry') if __name__=='__main__': try:main() except Exception as error: print('CONFIGURED_CLOSURE_INCOMPLETE:'+(str(error) if isinstance(error,cloud.contract.GuardError) else type(error).__name__)) raise SystemExit(2) from None