Files
rust-discovery/tests/apt_transaction/test_native_acceptance.py

407 lines
24 KiB
Python

"""No host package mutation: native root/child execution is mocked explicitly."""
import copy
import io
import json
import os
from pathlib import Path
import stat
import subprocess
import sys
import tarfile
import tempfile
from unittest import TestCase
from unittest.mock import patch
from test_guard import apt, transaction, ROOT
def fixture_transaction(recipe='rmm-apt-fixture-install-1.0'):
old, new = apt.FIXTURE_RECIPES[recipe]
obj = transaction(old)
obj['recipe_id'] = recipe
obj['packages'][0].update(new_version=new, sha256=apt.FIXTURE_SHA256[new],
size=len(apt.fixture_archive(new)),
archive_path=str(apt.BASE / 'archives' / (apt.FIXTURE_SHA256[new] + '.deb')))
return obj
def context(action='apt-native-acceptance', profile='dev-test'):
return apt.VerifiedHelperContext.parse({
'schema_version': 2, 'transaction_sha256': 'a' * 64,
'effect_armed_ledger_id': 'ledger-1', 'grant_id': 'grant-1',
'not_before_ms': 1000, 'start_before_ms': 61000, 'boot_id': 'boot-1',
'monotonic_start_before_ns': 10000, 'job_payload_sha256': 'b' * 64,
'action': action, 'execution_profile': profile, 'job_id': 'job-1',
'attempt_id': 'attempt-1', 'authorization_id': 'approval-1', 'plan_sha256': 'c' * 64})
def action_manifest(ctx, obj, runtime):
return {'type': 'rmm.apt.helper-action', 'schema_version': 2,
'action': ctx.action, 'execution_profile': ctx.execution_profile,
'transaction_sha256': ctx.transaction_sha256, 'resolved_apt_transaction': obj,
'transaction_bytes': list(apt.canonical(obj)),
'provider_sha256': apt.digest(b'code'), 'hook_sha256': apt.digest(apt.hook_script()),
'apt_config_sha256': apt.digest(apt.apt_config()),
'runtime_sha256': apt.digest(apt.canonical(runtime)), 'test_result_id': 'native-test-1'}
def signed_job(ctx, manifest):
tx = manifest['resolved_apt_transaction']
return {'schema_version': 1, 'tenant_id': 'tenant-1', 'asset_id': 'asset-1',
'agent_id': 'agent-1', 'enrollment_generation': 1, 'assignment_version': 1,
'job_id': ctx.job_id, 'attempt_id': ctx.attempt_id, 'execution_identity': 'root',
'not_before': '2026-11-12T12:00:00Z', 'start_before': '2026-11-12T12:01:00Z',
'max_runtime_seconds': 300, 'max_output_bytes': 4096,
'action': {'action': 'apt_native_acceptance' if ctx.action == 'apt-native-acceptance' else 'ensure_package',
'recipe_id': tx['recipe_id'], 'recipe_version': tx['recipe_version'],
'apt_manifest': manifest}}
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,
'control_syscalls': {'invalid_reboot_magic': 'EPERM',
'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):
def test_pinned_fixture_is_reproducible_and_has_no_scripts_or_dependencies(self):
for version in ('1.0', '1.1'):
first = apt.fixture_archive(version)
self.assertEqual(first, apt.fixture_archive(version))
self.assertEqual(apt.digest(first), apt.FIXTURE_SHA256[version])
# Inspect real deb control tar through dpkg-deb, never install.
with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
path = Path(temp) / 'fixture.deb'
path.write_bytes(first)
data = apt.run_read(['/usr/bin/dpkg-deb', '--ctrl-tarfile', str(path)])
with tarfile.open(fileobj=io.BytesIO(data)) as archive:
self.assertEqual(archive.getnames(), ['./control'])
control = archive.extractfile('./control').read()
self.assertNotIn(b'Depends:', control)
self.assertIn(('Version: ' + version + '\n').encode(), control)
def test_fixture_scope_only_one_exact_package_and_two_transitions(self):
for recipe in apt.FIXTURE_RECIPES:
apt.fixture_scope(apt.ResolvedAptTransaction.parse(fixture_transaction(recipe)))
for field, value in (('name', 'another-package'), ('new_version', '9.0'),
('sha256', 'd' * 64), ('size', 1), ('origin', 'catalog:external'),
('architecture', 'amd64'), ('old_version', '0.5')):
obj = fixture_transaction()
obj['packages'][0][field] = value
if field == 'sha256':
obj['packages'][0]['archive_path'] = str(apt.BASE / 'archives' / (value + '.deb'))
with self.subTest(field=field), self.assertRaises(apt.Veto):
apt.fixture_scope(apt.ResolvedAptTransaction.parse(obj))
obj = fixture_transaction()
obj['recipe_id'] = 'ordinary-catalog-recipe'
with self.assertRaisesRegex(apt.Veto, 'FIXTURE_RECIPE'):
apt.fixture_scope(apt.ResolvedAptTransaction.parse(obj))
def test_context_schema1_cannot_authorize_bootstrap(self):
obj = dict(context().__dict__)
obj['schema_version'] = 1
with self.assertRaisesRegex(apt.Veto, 'CONTEXT_SCHEMA'):
apt.VerifiedHelperContext.parse(obj)
def test_signed_nested_manifest_is_mandatory_and_hash_bound(self):
obj = fixture_transaction()
tx = apt.ResolvedAptTransaction.parse(obj)
ctx = context()
ctx = apt.VerifiedHelperContext(**{**ctx.__dict__, 'transaction_sha256': apt.digest(apt.canonical(obj))})
manifest = action_manifest(ctx, obj, {})
raw = apt.canonical(signed_job(ctx, manifest))
ctx = apt.VerifiedHelperContext(**{**ctx.__dict__, 'job_payload_sha256': apt.digest(raw)})
def read(path, *args):
return {apt.ACTIVE / 'signed-action.json': raw,
apt.ACTIVE / 'expectation.json': apt.canonical(obj),
apt.CODE: b'code', Path(apt.HOOK): apt.hook_script()}[path]
with patch.object(apt, 'sealed_read', side_effect=read):
self.assertEqual(apt.load_signed_action(tx, ctx), manifest)
wrong_ctx = apt.VerifiedHelperContext(**{**ctx.__dict__, 'job_payload_sha256': '0' * 64})
with self.assertRaisesRegex(apt.Veto, 'SIGNED_ACTION_HASH'):
apt.load_signed_action(tx, wrong_ctx)
for change in ('nested', 'action', 'code', 'profile'):
altered = copy.deepcopy(manifest)
if change == 'nested':
altered['resolved_apt_transaction']['packages'][0]['new_version'] = '9.0'
elif change == 'action':
altered['action'] = 'apt-execute'
elif change == 'code':
altered['provider_sha256'] = '0' * 64
else:
altered['execution_profile'] = 'production'
altered_raw = apt.canonical(signed_job(ctx, altered))
altered_ctx = apt.VerifiedHelperContext(**{**ctx.__dict__, 'job_payload_sha256': apt.digest(altered_raw)})
with patch.object(apt, 'sealed_read', side_effect=lambda path, *args:
altered_raw if path == apt.ACTIVE / 'signed-action.json' else read(path)):
with self.subTest(change=change), self.assertRaises(apt.Veto):
apt.load_signed_action(tx, altered_ctx)
def test_bootstrap_mode_cannot_replace_normal_or_production_action(self):
tx = apt.ResolvedAptTransaction.parse(fixture_transaction())
for mode, ctx in ((True, context('apt-execute')), (False, context()),
(True, context(profile='production')), (False, context('apt-execute', 'production'))):
with self.subTest(mode=mode, action=ctx.action), self.assertRaises(apt.Veto):
apt.check_native_gate(tx, ctx, action_manifest(ctx, fixture_transaction(), {}), {}, mode)
def test_normal_execution_requires_real_scoped_record(self):
tx = apt.ResolvedAptTransaction.parse(fixture_transaction())
ctx = context('apt-execute')
with patch.object(apt, 'sealed_read', side_effect=FileNotFoundError):
with self.assertRaises(FileNotFoundError):
apt.check_native_gate(tx, ctx, action_manifest(ctx, fixture_transaction(), {}), {}, False)
def test_runtime_changes_are_rejected_even_for_bootstrap(self):
tx = apt.ResolvedAptTransaction.parse(fixture_transaction())
ctx = context()
with self.assertRaisesRegex(apt.Veto, 'SIGNED_RUNTIME_BINDING'):
apt.check_native_gate(tx, ctx, action_manifest(ctx, fixture_transaction(), {}), {'new': True}, True)
class NativeProbeTests(TestCase):
def test_native_mount_introspection_and_checks_result_with_mocked_kernel(self):
self.inspect_kernel_scenario('valid')
def test_native_introspection_rejects_bad_caps_namespaces_mounts_and_sockets(self):
for scenario in ('capability', 'nnp', 'namespace', 'readonly', 'tmpfs', 'socket'):
with self.subTest(scenario=scenario), self.assertRaises(apt.Veto):
self.inspect_kernel_scenario(scenario)
def inspect_kernel_scenario(self, scenario):
mask = sum(1 << n for n in (0, 1, 3, 4, 6, 7, 8, 31))
if scenario == 'capability':
mask |= 1 << 22
status = 'NoNewPrivs: ' + ('0' if scenario == 'nnp' else '1') + '\nSeccomp: 2\n'
status += ''.join(key + ': ' + format(mask, 'x') + '\n' for key in ('CapEff', 'CapPrm', 'CapBnd'))
status += 'CapInh: 0\nCapAmb: 0\n'
mountinfo = []
for path in ('/sys', '/proc/sys', '/usr/lib/rmm-apt-transaction', str(apt.BASE / 'archives')):
flags = 'rw' if scenario == 'readonly' and path == '/sys' else 'ro'
mountinfo.append('1 0 0:1 / ' + path + ' ' + flags + ' - sysfs sysfs ro')
for path in ('/run', '/tmp', '/var/tmp', '/dev'):
kind = 'ext4' if scenario == 'tmpfs' and path == '/run' else 'tmpfs'
mountinfo.append('1 0 0:1 / ' + path + ' rw - ' + kind + ' tmpfs rw')
def text(path, *args, **kwargs):
return {'/proc/self/status': status, '/proc/self/mountinfo': '\n'.join(mountinfo)}[str(path)]
with patch.object(Path, 'read_text', text), \
patch.object(Path, 'read_bytes', return_value=b'/usr/bin/python3\x00-I\x00'), \
patch.object(Path, 'exists', side_effect=lambda: scenario == 'socket'), \
patch.object(Path, 'stat', return_value=type('MockStat', (), {'st_mode': stat.S_IFCHR})()), \
patch.object(apt, 'sealed_read', return_value=apt.canonical({'pid': 'host', 'mnt': 'host', 'net': 'host'})), \
patch.object(apt.os, 'readlink', return_value='host' if scenario == 'namespace' else 'child'), \
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(),host_before(context())))
def test_safe_invalid_syscall_probes_in_unprivileged_filtered_child(self):
code = '''import importlib.util,sys,json
sys.dont_write_bytecode=True
s=importlib.util.spec_from_file_location('apt_transaction',sys.argv[1])
m=importlib.util.module_from_spec(s);sys.modules[s.name]=m;s.loader.exec_module(m)
m.seccomp_filter()
print(json.dumps(m.invalid_control_syscall_probes()))
'''
output = subprocess.check_output(['/usr/bin/python3', '-I', '-c', code,
str(ROOT / 'tools/apt-transaction/apt_transaction.py')])
self.assertEqual(set(json.loads(output).values()), {'EPERM'})
def test_frontend_probe_uses_native_check_with_actual_fcntl_lock_but_no_local_apt(self):
with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
lock = Path(temp) / 'native-lock'
lock.touch()
with patch.object(apt, 'secure_path', return_value=lock), \
patch.object(apt, 'run_read', return_value=b'E: Could not get lock /var/lib/dpkg/lock-frontend') as read:
self.assertEqual(apt.frontend_lock_probe(), 'native-apt-check-conflict')
self.assertEqual(read.call_args.args[0][-1], 'check')
self.assertEqual(read.call_args.kwargs['expected_returncode'], 100)
with patch.object(apt, 'secure_path', return_value=lock), \
patch.object(apt, 'run_read', return_value=b'an unrelated error'):
with self.assertRaisesRegex(apt.Veto, 'NATIVE_APT_LOCK_CONFLICT_NOT_PROVEN'):
apt.frontend_lock_probe()
class CompletionTests(TestCase):
def setup_files(self, base, ctx):
active = base / 'active'
active.mkdir()
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,
'grant_id': ctx.grant_id, 'attempt_id': ctx.attempt_id}))
return active
def test_record_after_complete_mocked_native_pipeline_is_scoped_and_fsynced(self):
ctx = context()
with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
base = Path(temp)
active = self.setup_files(base, ctx)
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)
result = apt.complete_acceptance(tx, ctx, action_manifest(ctx, obj, {}), {},
{'reboot_required': False})
self.assertTrue(result['test_acceptance'])
self.assertTrue(result['noProductionEvidence'])
self.assertGreaterEqual(sync.call_count, 3)
proof = json.loads(apt.record_path(tx).read_bytes())
self.assertEqual(proof['job_payload_sha256'], ctx.job_payload_sha256)
self.assertEqual(proof['scope']['recipe_id'], tx.recipe_id)
self.assertEqual(proof['runtime'], {})
normal_ctx = context('apt-execute')
apt.check_native_gate(tx, normal_ctx, action_manifest(normal_ctx, obj, {}), {}, False)
with self.assertRaisesRegex(apt.Veto, 'ACCEPTANCE_ALREADY_RECORDED'):
apt.check_native_gate(tx, ctx, action_manifest(ctx, obj, {}), {}, True)
with self.assertRaises(apt.Veto):
apt.complete_acceptance(tx, ctx, action_manifest(ctx, obj, {}), {}, {'reboot_required': False})
def test_missing_partial_negative_or_wrong_prechecks_never_publish_record(self):
for variation in ('missing', 'partial', 'negative', 'binding', 'reboot', 'runtime'):
ctx = context()
with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
base = Path(temp)
active = self.setup_files(base, ctx)
checkfile = active / 'native-prechecks.json'
if variation == 'missing':
checkfile.unlink()
elif variation == 'partial':
checkfile.write_bytes(b'{')
elif variation in ('negative', 'binding'):
obj = prechecks(ctx,host_before(ctx))
if variation == 'negative':
obj['checks']['namespaces_caps_nnp_seccomp'] = False
else:
obj['transaction_sha256'] = '0' * 64
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(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)):
apt.complete_acceptance(tx, ctx, action_manifest(ctx, obj, {}), {},
{'reboot_required': variation == 'reboot'})
self.assertFalse(apt.record_path(tx).exists())
def test_staged_write_failure_never_publishes_final_record(self):
ctx = context()
with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
base = Path(temp)
active = self.setup_files(base, ctx)
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)
with self.assertRaises(OSError):
apt.complete_acceptance(tx, ctx, action_manifest(ctx, obj, {}), {}, {'reboot_required': False})
self.assertFalse(apt.record_path(tx).exists())
class DriverTests(TestCase):
def test_entire_driver_mocked_child_success_and_redelivery_no_second_launch(self):
self.run_driver_scenario('success')
def test_failed_unknown_stale_or_partial_driver_never_creates_acceptance(self):
for scenario in ('child-failed', 'no-hook', 'missing-prechecks', 'postcondition-failed', 'stale'):
with self.subTest(scenario=scenario):
self.run_driver_scenario(scenario)
def run_driver_scenario(self, scenario):
# No unshare/apt/root execution: only the process boundary and root
# preflight functions are mocked. Actual gate/one-shot/fsync/record logic
# runs on private temporary files. Such files never leave this test dir.
with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
base = Path(temp)
active = base / 'active'
active.mkdir()
for name in ('empty', 'lists', 'cache'):
(active / name).mkdir()
(active / 'apt.conf').write_bytes(apt.apt_config())
ctx = context()
native_code = 100 if scenario == 'child-failed' else 0
def read(path, *args):
return apt.hook_script() if path == Path(apt.HOOK) else path.read_bytes()
def child(argv, **kwargs):
self.assertEqual(argv[0], '/usr/bin/unshare')
self.assertIn('--pid', argv)
if scenario != 'no-hook':
(active / 'hook-armed.json').write_bytes(apt.canonical({
'transaction_sha256': ctx.transaction_sha256,
'effect_armed_ledger_id': ctx.effect_armed_ledger_id, 'grant_id': ctx.grant_id}))
(active / 'sandbox-result.json').write_bytes(apt.canonical({
'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,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), \
patch.object(apt.os, 'geteuid', return_value=0), patch.object(apt, 'secure_path', side_effect=lambda p, **k: p), \
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:
obj = fixture_transaction()
tx = apt.ResolvedAptTransaction.parse(obj)
manifest = action_manifest(ctx, obj, {})
evidence = {'reboot_required': False, 'snapshot_sha256': 'e' * 64}
with patch.object(apt, 'load_active', return_value=(tx, ctx, {})), \
patch.object(apt, 'load_signed_action', return_value=manifest), \
patch.object(apt, 'verify_after', return_value=evidence,
side_effect=apt.Veto('POSTCONDITION_OR_UNEXPECTED_CHANGE')
if scenario == 'postcondition-failed' else None):
request = {'transaction_sha256': ctx.transaction_sha256,
'effect_armed_ledger_id': ctx.effect_armed_ledger_id}
result = apt.execute(request, native_acceptance=True)
self.assertTrue(result['evidence']['test_acceptance'])
self.assertTrue(result['evidence']['noProductionEvidence'])
if scenario == 'success':
self.assertEqual(result['effect_state'], 'verified')
self.assertTrue(apt.record_path(tx).is_file())
self.assertEqual(spawn.call_count, 1)
repeated = apt.execute(request, native_acceptance=True)
self.assertEqual(repeated['effect_state'], 'unknown')
self.assertEqual(spawn.call_count, 1)
else:
self.assertNotEqual(result['effect_state'], 'verified')
self.assertFalse(apt.record_path(tx).exists())
self.assertEqual(spawn.call_count, 0 if scenario == 'stale' else 1)