fix: reconcile configured APT effect without retroactive acceptance and retain host reboot evidence
Rust development slice / linux (push) Failing after 6s

This commit is contained in:
2026-11-13 20:00:20 +01:00
parent 34d695a778
commit b08734065a
26 changed files with 2006 additions and 36 deletions
@@ -0,0 +1,81 @@
"""Bound HOST-before/after logic; no native package, namespace or reboot calls."""
import copy
from pathlib import Path
import tempfile
from unittest import TestCase
from unittest.mock import patch
from test_guard import apt
import test_native_acceptance as fixture
class AmbientRebootTests(TestCase):
def scenario(self, before, after, private=False, missing=False, changed_boot=False):
ctx=fixture.context()
with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
base=Path(temp);active=fixture.CompletionTests().setup_files(base,ctx)
record=fixture.host_before(ctx,before)
(active/'host-reboot-before.json').write_bytes(apt.canonical(record))
(active/'native-prechecks.json').write_bytes(apt.canonical(fixture.prechecks(ctx,record)))
(active/'sandbox-result.json').write_bytes(apt.canonical({'transaction_sha256':ctx.transaction_sha256,'apt_exit_code':0,'reboot_required':private}))
if missing:(active/'host-reboot-before.json').unlink()
original_exists=Path.exists
with patch.object(apt,'BASE',base),patch.object(apt,'ACTIVE',active),patch.object(apt,'secure_path'), \
patch.object(apt,'sealed_read',side_effect=lambda path,*a,**k:path.read_bytes()), \
patch.object(apt,'runtime_identity',return_value={}), \
patch.object(Path,'exists',lambda path:after if str(path)=='/run/reboot-required' else original_exists(path)), \
patch.object(Path,'read_text',return_value='other-boot' if changed_boot else ctx.boot_id), \
patch.object(apt.subprocess,'Popen',side_effect=AssertionError('No effects in acceptance publication')):
obj=fixture.fixture_transaction();tx=apt.ResolvedAptTransaction.parse(obj)
evidence={'reboot_required':after}
if before!=after or private or missing or changed_boot:
with self.assertRaises((apt.Veto,OSError)):
apt.complete_acceptance(tx,ctx,fixture.action_manifest(ctx,obj,{}),{},evidence)
self.assertFalse(apt.record_path(tx).exists())
else:
apt.complete_acceptance(tx,ctx,fixture.action_manifest(ctx,obj,{}),{},evidence)
self.assertTrue(apt.record_path(tx).exists())
self.assertIs(evidence['reboot_required'],before)
self.assertIs(evidence['host_reboot_required_before'],before)
self.assertIs(evidence['host_reboot_required_after'],after)
self.assertFalse(evidence['reboot_required_introduced'])
self.assertEqual(evidence['host_reboot_before_sha256'],apt.digest(apt.canonical(record)))
proof=apt.json_bytes(apt.record_path(tx).read_bytes())
self.assertEqual(proof['schema_version'],3)
self.assertEqual(proof['native_prechecks_schema_version'],2)
apt.validate_acceptance_record(proof,apt.fixture_scope(tx),{})
for key,value in (('schema_version',2),('native_prechecks_schema_version',1),
('host_reboot_required_after',not before),('host_reboot_required_before',int(before)),
('reboot_required_introduced',True)):
bad=copy.deepcopy(proof);bad[key]=value
with self.subTest(field=key),self.assertRaises(apt.Veto):apt.validate_acceptance_record(bad,apt.fixture_scope(tx),{})
def test_ambient_false_false_and_true_true_are_honest_success(self):
for pending in (False,True):
with self.subTest(pending=pending):self.scenario(pending,pending)
def test_host_changes_private_reboot_missing_before_and_boot_change_never_accept(self):
for before,after in ((False,True),(True,False)):
with self.subTest(before=before,after=after):self.scenario(before,after)
self.scenario(True,True,private=True)
self.scenario(False,False,missing=True)
self.scenario(False,False,changed_boot=True)
def test_parent_capture_is_once_and_child_prechecks_only_bind_that_sample(self):
ctx=fixture.context()
with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
active=Path(temp)
with patch.object(apt,'ACTIVE',active),patch.object(apt,'secure_path'), \
patch.object(apt,'sealed_read',side_effect=lambda path,*a,**k:path.read_bytes()), \
patch.object(apt.VerifiedHelperContext,'check_deadline'),patch.object(apt.time,'time_ns',return_value=2000000000), \
patch.object(Path,'exists',return_value=True):
apt.capture_host_reboot_before(ctx)
before,sha=apt.load_host_reboot_before(ctx)
self.assertTrue(before['host_reboot_required'])
with self.assertRaisesRegex(apt.Veto,'ALREADY_HANDED_OFF'):apt.capture_host_reboot_before(ctx)
with patch.object(Path,'exists',return_value=False) as child_private_flag, \
patch.object(apt,'sandbox_checks',return_value=fixture.prechecks(ctx)['checks']):
checks=apt.native_prechecks(ctx)
self.assertTrue(checks['host_reboot_required_before'])
self.assertEqual(checks['host_reboot_before_sha256'],sha)
child_private_flag.assert_not_called()
@@ -0,0 +1,83 @@
"""Historical configure-only proof is FAILED, never retrospective acceptance."""
import copy
from pathlib import Path
from unittest import TestCase
from unittest.mock import patch
from test_guard import apt
import test_native_acceptance as fixture
from test_native_recovery import recovery_files
class ConfiguredReconciliationTests(TestCase):
def scenario(self,variation=None):
with recovery_files() as (base,tx,ctx,action,partial,previous):
active=apt.ACTIVE
# This deliberately models the historical runtime with NO HOST-before
# observation, not a current schema2-prechecks execution.
(active/'host-reboot-before.json').unlink()
protocol=apt.digest(apt.canonical([apt.Action('rmm-mvp-test','1.0','all','none','=','1.0','all','none','**CONFIGURE**').__dict__]))
records={'launch.json':{'transaction_sha256':ctx.transaction_sha256,'effect_armed_ledger_id':ctx.effect_armed_ledger_id},
'native-prechecks.json':fixture.prechecks(ctx),
'sandbox-ready.json':{'transaction_sha256':ctx.transaction_sha256,'sandbox_version':1},
'hook-armed.json':{'armed_at_ms':2000,'effect_armed_ledger_id':ctx.effect_armed_ledger_id,'grant_id':ctx.grant_id,'transaction_sha256':ctx.transaction_sha256,'protocol_sha256':protocol}}
for name,obj in records.items():(active/name).write_bytes(apt.canonical(obj))
path,binding=apt.bootstrap_binding(tx,ctx,action);apt.durable_once(path,binding)
pins=dict(grant_id=ctx.grant_id,previous_grant_id=partial['grant_id'],partial_proof_sha256=action['native_recovery']['partial_proof_sha256'],provider_sha256=action['provider_sha256'])
fields='transaction_sha256 job_payload_sha256 effect_armed_ledger_id grant_id job_id attempt_id authorization_id plan_sha256'.split()
request={k:getattr(ctx,k) for k in fields}
observed=copy.deepcopy(partial['snapshot']);observed['rmm-mvp-test:all']['status']='install ok installed'
if variation=='different-package':observed['other:amd64']={'version':'1','status':'install ok installed'}
elif variation=='unpacked':observed['rmm-mvp-test:all']['status']='install ok unpacked'
elif variation=='wrong-context':request['job_payload_sha256']='0'*64
elif variation=='wrong-grant':pins['grant_id']='different'
elif variation=='missing-hook':(active/'hook-armed.json').unlink()
elif variation in ('apt-failed','child-reboot'):
obj={'transaction_sha256':ctx.transaction_sha256,'apt_exit_code':100 if variation=='apt-failed' else 0,'reboot_required':variation=='child-reboot'}
(active/'sandbox-result.json').write_bytes(apt.canonical(obj))
elif variation=='fake-before':(active/'host-reboot-before.json').write_bytes(apt.canonical(fixture.host_before(ctx,True)))
elif variation=='new-prechecks':(active/'native-prechecks.json').write_bytes(apt.canonical(fixture.prechecks(ctx,fixture.host_before(ctx,True))))
elif variation=='partial-proof':(active/'partial-proof.json').write_bytes(b'{}')
elif variation=='record-created':(base/('recovery-'+partial['grant_id']+'.json')).write_bytes(b'{}')
elif variation=='history':(previous/'sandbox-result.json').write_bytes(b'{}')
elif variation=='protocol':
records['hook-armed.json']['protocol_sha256']='0'*64
(active/'hook-armed.json').write_bytes(apt.canonical(records['hook-armed.json']))
before={str(p.relative_to(base)):p.read_bytes() for p in base.rglob('*') if p.is_file()}
old_read=apt.sealed_read;original_exists=Path.exists
def read(path,*a,**k):
if str(path)=='/run/reboot-required':return b'Current HOST restart required\n'
if str(path)=='/usr/share/rmm-mvp-test/version' and variation=='payload':return b'1.1\n'
return old_read(path,*a,**k)
with patch.object(apt,'HISTORICAL_CONFIGURED_RECOVERY',pins),patch.object(apt.os,'geteuid',return_value=0), \
patch.object(apt,'sealed_read',side_effect=read),patch.object(Path,'read_text',return_value='other' if variation=='boot' else ctx.boot_id), \
patch.object(Path,'exists',lambda p:variation!='host-false' if str(p)=='/run/reboot-required' else original_exists(p)), \
patch.object(apt,'observe_host_frontend_lock',return_value=variation=='foreign-lock'), \
patch.object(apt,'status_fingerprint',return_value='a'*64,side_effect=['a'*64,'b'*64] if variation=='drift' else None), \
patch.object(apt,'installed_snapshot',return_value=observed),patch.object(apt,'verify_archive'), \
patch.object(apt,'run_read',return_value=b'not clean' if variation=='audit' else b''), \
patch.object(apt.subprocess,'Popen',side_effect=AssertionError('No package effects allowed')):
if variation is None:
result=apt.verify_configured_recovery(request)
self.assertEqual(result['effect_state'],'known_failed')
self.assertEqual(result['code'],'APT_RECOVERY_CONFIGURED_ACCEPTANCE_REBOOT_FLAG_VERIFIED')
proof=result['proof'];self.assertEqual(len(proof),24)
self.assertTrue(proof['effect_complete']);self.assertFalse(proof['postconditions_passed'])
self.assertFalse(proof['partial_effect']);self.assertTrue(proof['host_reboot_required']);self.assertTrue(proof['reboot_required'])
self.assertEqual(proof['snapshot'],observed)
self.assertNotIn('host_reboot_required_before',proof)
else:
with self.assertRaises((apt.Veto,OSError,ValueError)):apt.verify_configured_recovery(request)
after={str(p.relative_to(base)):p.read_bytes() for p in base.rglob('*') if p.is_file()}
self.assertEqual(before,after,'readonly classification must preserve EVERY witness and marker')
def test_complete_effect_with_missing_historical_before_is_failed_without_writes(self):
self.scenario()
def test_binding_history_native_result_or_retroactive_before_mismatch_never_closes(self):
for variation in ('wrong-context','wrong-grant','missing-hook','apt-failed','child-reboot','fake-before','new-prechecks','partial-proof','record-created','history','protocol'):
with self.subTest(variation=variation):self.scenario(variation)
def test_current_state_must_remain_exact_configured_fixture_and_host_flag(self):
for variation in ('different-package','unpacked','payload','boot','host-false','foreign-lock','drift','audit'):
with self.subTest(variation=variation):self.scenario(variation)
@@ -57,8 +57,14 @@ def signed_job(ctx, manifest):
'apt_manifest': manifest}}
def prechecks(ctx):
return {'schema_version': 1, 'transaction_sha256': ctx.transaction_sha256,
def host_before(ctx, pending=False):
return {'schema_version':1,'transaction_sha256':ctx.transaction_sha256,'job_payload_sha256':ctx.job_payload_sha256,
'effect_armed_ledger_id':ctx.effect_armed_ledger_id,'grant_id':ctx.grant_id,'boot_id':ctx.boot_id,
'host_reboot_required':pending,'observed_at_ms':2000}
def prechecks(ctx, before=None):
result = {'schema_version': 1, 'transaction_sha256': ctx.transaction_sha256,
'job_payload_sha256': ctx.job_payload_sha256, 'boot_id': ctx.boot_id,
'checks': {'namespaces_caps_nnp_seccomp': True, 'readonly_controlpaths': True,
'private_tmpfs_sockets': True,
@@ -66,6 +72,10 @@ def prechecks(ctx):
'invalid_kexec_load_flags': 'EPERM',
'invalid_kexec_file_fds_flags': 'EPERM'},
'frontend_lock': 'native-apt-check-conflict'}}
if before is not None:
result.update(schema_version=2,host_reboot_before_sha256=apt.digest(apt.canonical(before)),
host_reboot_required_before=before['host_reboot_required'])
return result
class BindingTests(TestCase):
@@ -201,8 +211,9 @@ class NativeProbeTests(TestCase):
patch.object(apt, 'invalid_control_syscall_probes', return_value=prechecks(context())['checks']['control_syscalls']), \
patch.object(apt, 'frontend_lock_probe', return_value='native-apt-check-conflict'), \
patch.object(apt.socket, 'socket', side_effect=OSError(apt.errno.EAFNOSUPPORT, 'mocked kernel denial')), \
patch.object(apt,'load_host_reboot_before',return_value=(host_before(context()),apt.digest(apt.canonical(host_before(context()))))), \
patch.object(apt.VerifiedHelperContext, 'check_deadline'):
self.assertEqual(apt.native_prechecks(context()), prechecks(context()))
self.assertEqual(apt.native_prechecks(context()), prechecks(context(),host_before(context())))
def test_safe_invalid_syscall_probes_in_unprivileged_filtered_child(self):
code = '''import importlib.util,sys,json
@@ -235,7 +246,10 @@ class CompletionTests(TestCase):
def setup_files(self, base, ctx):
active = base / 'active'
active.mkdir()
(active / 'native-prechecks.json').write_bytes(apt.canonical(prechecks(ctx)))
before=host_before(ctx)
(active / 'host-reboot-before.json').write_bytes(apt.canonical(before))
(active / 'native-prechecks.json').write_bytes(apt.canonical(prechecks(ctx,before)))
(active / 'sandbox-result.json').write_bytes(apt.canonical({'transaction_sha256':ctx.transaction_sha256,'apt_exit_code':0,'reboot_required':False}))
(active / 'hook-armed.json').write_bytes(apt.canonical({'grant_id': ctx.grant_id}))
(base / 'bootstrap-attempt-rmm-apt-fixture-install-1.0.json').write_bytes(apt.canonical({
'transaction_sha256': ctx.transaction_sha256, 'job_payload_sha256': ctx.job_payload_sha256,
@@ -250,6 +264,7 @@ class CompletionTests(TestCase):
with patch.object(apt, 'BASE', base), patch.object(apt, 'ACTIVE', active), \
patch.object(apt, 'secure_path'), patch.object(apt, 'sealed_read', side_effect=lambda p, *a: p.read_bytes()), \
patch.object(apt, 'runtime_identity', return_value={}), \
patch.object(Path,'read_text',return_value=ctx.boot_id), \
patch.object(apt.os, 'fsync', wraps=os.fsync) as sync:
obj = fixture_transaction()
tx = apt.ResolvedAptTransaction.parse(obj)
@@ -281,7 +296,7 @@ class CompletionTests(TestCase):
elif variation == 'partial':
checkfile.write_bytes(b'{')
elif variation in ('negative', 'binding'):
obj = prechecks(ctx)
obj = prechecks(ctx,host_before(ctx))
if variation == 'negative':
obj['checks']['namespaces_caps_nnp_seccomp'] = False
else:
@@ -289,7 +304,8 @@ class CompletionTests(TestCase):
checkfile.write_bytes(apt.canonical(obj))
with patch.object(apt, 'BASE', base), patch.object(apt, 'ACTIVE', active), \
patch.object(apt, 'secure_path'), patch.object(apt, 'sealed_read', side_effect=lambda p, *a: p.read_bytes()), \
patch.object(apt, 'runtime_identity', return_value={'changed': True} if variation == 'runtime' else {}):
patch.object(apt, 'runtime_identity', return_value={'changed': True} if variation == 'runtime' else {}), \
patch.object(Path,'read_text',return_value=ctx.boot_id):
obj = fixture_transaction()
tx = apt.ResolvedAptTransaction.parse(obj)
with self.subTest(variation=variation), self.assertRaises((apt.Veto, OSError)):
@@ -305,6 +321,7 @@ class CompletionTests(TestCase):
with patch.object(apt, 'BASE', base), patch.object(apt, 'ACTIVE', active), \
patch.object(apt, 'sealed_read', side_effect=lambda p, *a: p.read_bytes()), \
patch.object(apt, 'runtime_identity', return_value={}), \
patch.object(Path,'read_text',return_value=ctx.boot_id), \
patch.object(apt, 'durable_once', side_effect=OSError('fsync failure')):
obj = fixture_transaction()
tx = apt.ResolvedAptTransaction.parse(obj)
@@ -350,7 +367,7 @@ class DriverTests(TestCase):
'transaction_sha256': ctx.transaction_sha256, 'apt_exit_code': native_code,
'reboot_required': False}))
if scenario != 'missing-prechecks':
(active / 'native-prechecks.json').write_bytes(apt.canonical(prechecks(ctx)))
(active / 'native-prechecks.json').write_bytes(apt.canonical(prechecks(ctx,json.loads((active/'host-reboot-before.json').read_bytes()))))
return type('MockNativeChild', (), {'wait': lambda _: native_code})()
with patch.object(apt, 'BASE', base), patch.object(apt, 'ACTIVE', active), \
@@ -358,6 +375,7 @@ class DriverTests(TestCase):
patch.object(apt, 'sealed_read', side_effect=read), patch.object(apt, 'runtime_identity', return_value={}), \
patch.object(apt, 'check_current_baseline'), patch.object(apt, 'check_preconditions'), patch.object(apt, 'verify_archive'), \
patch.object(apt, 'run_read', return_value=b''), patch.object(apt, 'installed_snapshot', return_value={}), \
patch.object(Path,'read_text',return_value=ctx.boot_id), patch.object(apt.time,'time_ns',return_value=2000000000), \
patch.object(apt.VerifiedHelperContext, 'check_deadline',
side_effect=apt.Veto('START_DEADLINE') if scenario == 'stale' else None), \
patch.object(apt.subprocess, 'Popen', side_effect=child) as spawn:
@@ -9,7 +9,7 @@ from unittest import TestCase
from unittest.mock import patch
from test_guard import apt
from test_native_acceptance import context, fixture_transaction, action_manifest, signed_job, prechecks
from test_native_acceptance import context, fixture_transaction, action_manifest, signed_job, prechecks, host_before
import test_native_retry as retry_fixture
@@ -80,7 +80,9 @@ def recovery_files():
for name, data in {'context.json': apt.canonical(new.__dict__), 'signed-action.json': new_job,
'expectation.json': apt.canonical(obj), 'baseline.json': apt.canonical(snapshot),
'partial-proof.json': apt.canonical(proof), 'apt.conf': apt.apt_config(),
'native-prechecks.json': apt.canonical(prechecks(new)),
'host-reboot-before.json': apt.canonical(host_before(new)),
'native-prechecks.json': apt.canonical(prechecks(new,host_before(new))),
'sandbox-result.json': apt.canonical({'transaction_sha256':new.transaction_sha256,'apt_exit_code':0,'reboot_required':False}),
'hook-armed.json': b'{}'}.items():
(active/name).write_bytes(data)
yield base, tx, new, action, proof, previous
@@ -114,7 +116,7 @@ class NativeRecoveryTests(TestCase):
self.assertEqual(path.name,'recovery-attempt-'+proof['grant_id']+'.json')
apt.durable_once(path,binding)
with self.assertRaisesRegex(apt.Veto,'ALREADY_HANDED_OFF'): apt.durable_once(path,binding)
with patch.object(apt,'runtime_identity',return_value={}):
with patch.object(apt,'runtime_identity',return_value={}),patch.object(Path,'read_text',return_value=ctx.boot_id):
evidence = apt.complete_acceptance(tx,ctx,action,{}, {'reboot_required':False})
self.assertTrue(evidence['native_recovery'])
self.assertNotIn('native_record_sha256',evidence)
@@ -61,6 +61,7 @@ class PlanningTests(TestCase):
patch.object(apt, 'runtime_identity', return_value=runtime), \
patch.object(apt.os, 'fsync', wraps=os.fsync) as sync:
first = apt.planning_status()
self.assertIs(type(first['host_reboot_required']),bool)
self.assertGreaterEqual(sync.call_count, 2)
self.assertNotIn('private-business-package', json.dumps(first))
sealed = base / 'baselines' / (first['baseline_sha256'] + '.json')