187 lines
10 KiB
Python
187 lines
10 KiB
Python
#!/usr/bin/env python3
|
|
"""Local fail-closed readiness tests; no credentials, SSH or effects."""
|
|
from pathlib import Path
|
|
import json
|
|
import sys
|
|
import unittest
|
|
import sqlite3
|
|
import tempfile
|
|
from contextlib import redirect_stdout
|
|
from unittest.mock import patch
|
|
from types import SimpleNamespace
|
|
import io
|
|
import base64
|
|
import hashlib
|
|
import subprocess
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'deploy'))
|
|
import vm_job_test as vm
|
|
import vm_job_bootstrap as bootstrap
|
|
import vm_job_live as live
|
|
import vm_root_bootstrap as root_bootstrap
|
|
|
|
|
|
class ReadinessTests(unittest.TestCase):
|
|
def test_root_kernel_readiness_rejects_systemd_preexec_race(self):
|
|
mask = format(sum(1 << n for n in (0, 1, 3, 4, 6, 7, 8, 31)), 'x')
|
|
valid = dict(Uid='0 0 0 0', Gid='984 984 984 984', NoNewPrivs='1', Seccomp='2', CapEff=mask, CapBnd=mask)
|
|
self.assertTrue(root_bootstrap.confined_runtime(valid, 984))
|
|
self.assertTrue(root_bootstrap.confined_runtime(dict(valid, CapEff=format(int(mask, 16) & ~(1 << 7), 'x')), 984))
|
|
self.assertFalse(root_bootstrap.confined_runtime(dict(valid, CapEff=format(1 << 21, 'x')), 984))
|
|
for change in (dict(NoNewPrivs='0'), dict(Seccomp='0'), dict(Gid='0 0 0 0'), dict(CapBnd='1ffffffffff')):
|
|
self.assertFalse(root_bootstrap.confined_runtime(dict(valid, **change), 984))
|
|
|
|
def test_root_readiness_config_does_not_authorize_apt_reboot_or_root_scripts(self):
|
|
binding = dict(tenant_id='test', agent_id='test-agent', asset_id='test-asset', public_key_hex='public-test-key')
|
|
config = root_bootstrap.helper_config(binding, 993, 984)
|
|
self.assertEqual(config['allowed_services'], ['rmm-mvp-test.service'])
|
|
self.assertIsNone(config['apt_provider'])
|
|
self.assertFalse(config['allow_reboot'])
|
|
self.assertFalse(config['allow_root_scripts'])
|
|
self.assertEqual(config['binding']['public_key_hex'], '')
|
|
self.assertEqual(binding['public_key_hex'], 'public-test-key')
|
|
with self.assertRaises(RuntimeError):
|
|
root_bootstrap.helper_config(binding, 0, 984)
|
|
|
|
def test_root_cleanup_requires_proven_empty_consumption_ledger(self):
|
|
self.assertTrue(root_bootstrap.helper_can_stop({'helper_started': True, 'helper_consumed_count': 0}))
|
|
for count in (None, 1, 2):
|
|
self.assertFalse(root_bootstrap.helper_can_stop({'helper_started': True, 'helper_consumed_count': count}))
|
|
self.assertFalse(root_bootstrap.helper_can_stop({'helper_started': True}))
|
|
|
|
def test_actual_generated_root_program_never_echoes_exception_canary(self):
|
|
script = root_bootstrap.program("(_ for _ in ()).throw(ValueError('SECRET-ROOT-CANARY'))")
|
|
output = io.StringIO()
|
|
with redirect_stdout(output):
|
|
exec(compile(script, '<generated-root-bootstrap>', 'exec'), {})
|
|
response = json.loads(output.getvalue())
|
|
self.assertEqual(response['error']['exception_class'], 'ValueError')
|
|
self.assertTrue(response['error']['traceback_lines'])
|
|
self.assertNotIn('SECRET-ROOT-CANARY', output.getvalue())
|
|
|
|
def test_generated_root_program_has_real_manifest_and_hash_dependencies(self):
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
path = Path(folder)
|
|
(path / 'identity').write_text('SECRET-ROOT-FILE-CANARY')
|
|
script = root_bootstrap.program('(sha(b"test"),manifest(Path(' + repr(folder) + ')))')
|
|
output = io.StringIO()
|
|
with redirect_stdout(output):
|
|
exec(compile(script, '<generated-root-bootstrap>', 'exec'), {})
|
|
response = json.loads(output.getvalue())
|
|
self.assertEqual(response['result'], [root_bootstrap.sha(b'test'), bootstrap.manifest(path)])
|
|
self.assertNotIn('SECRET-ROOT-FILE-CANARY', output.getvalue())
|
|
|
|
def test_lifecycle_never_stops_active_or_unknown_jobs(self):
|
|
self.assertTrue(live.can_stop([]))
|
|
self.assertTrue(live.can_stop(['succeeded', 'failed', 'expired', 'cancelled']))
|
|
for state in ('queued', 'accepted', 'running', 'verifying', 'unknown', 'awaiting_reboot', 'unrecognized'):
|
|
self.assertFalse(live.can_stop(['succeeded', state]))
|
|
self.assertFalse(live.can_stop(None))
|
|
|
|
def facts(self, **changes):
|
|
return dict(dict(identity_present=True, ssh_uid=1000, state_owner_uid=1000,
|
|
dedicated_account_present=True, agent_count=0,
|
|
port_occupied=False), **changes)
|
|
|
|
def test_existing_unprivileged_identity_is_only_readiness(self):
|
|
self.assertEqual(vm.readiness(self.facts()), [])
|
|
|
|
def test_root_is_not_agent_service_even_with_an_account(self):
|
|
self.assertIn('root_ssh_requires_existing_unprivileged_account_deployment',
|
|
vm.readiness(self.facts(ssh_uid=0)))
|
|
|
|
def test_root_without_account_and_root_state_blocks(self):
|
|
self.assertEqual(len(vm.readiness(self.facts(ssh_uid=0, state_owner_uid=0,
|
|
dedicated_account_present=False))), 3)
|
|
|
|
def test_missing_identity_never_requests_reenrollment(self):
|
|
self.assertEqual(vm.readiness(self.facts(identity_present=False)),
|
|
['preserved_device_identity_missing'])
|
|
|
|
def test_existing_agent_or_listener_not_stopped(self):
|
|
for change in ({'agent_count': 1}, {'port_occupied': True}):
|
|
self.assertEqual(vm.readiness(self.facts(**change)),
|
|
['existing_agent_or_tunnel_requires_lifecycle_review'])
|
|
|
|
def test_manifest_preserves_exact_bytes_and_sqlite_schema_rows(self):
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
path = Path(folder)
|
|
(path / 'device.key').write_bytes(b'PRIVATE TEST BYTES\n')
|
|
with sqlite3.connect(path / 'journal.sqlite') as db:
|
|
db.execute('CREATE TABLE safety(key TEXT PRIMARY KEY,value TEXT)')
|
|
db.execute("INSERT INTO safety VALUES('connected','1')")
|
|
before = bootstrap.manifest(path)
|
|
self.assertEqual(before, bootstrap.manifest(path))
|
|
self.assertEqual(before['databases']['journal.sqlite']['tables']['safety']['rows'], 1)
|
|
self.assertNotIn('PRIVATE', json.dumps(before))
|
|
(path / 'device.key').write_bytes(b'CHANGED')
|
|
self.assertNotEqual(before, bootstrap.manifest(path))
|
|
|
|
def test_manifest_refuses_symlinks_and_external_hardlinks(self):
|
|
import os
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
path = Path(folder)
|
|
(path / 'key').write_bytes(b'test')
|
|
(path / 'link').symlink_to(path / 'key')
|
|
with self.assertRaises(RuntimeError):
|
|
bootstrap.manifest(path)
|
|
(path / 'link').unlink()
|
|
os.link(path / 'key', path / 'link')
|
|
with self.assertRaises(RuntimeError):
|
|
bootstrap.manifest(path)
|
|
|
|
def run_generated(self, folder, fail_loader=False):
|
|
root = Path(folder)
|
|
for name in ('var/lib', 'etc/systemd/system', 'usr/local', 'original/state'):
|
|
(root / name).mkdir(parents=True, exist_ok=True)
|
|
state = root / 'original/state'
|
|
(state / 'agent.lock').touch()
|
|
(state / 'identity.json').write_text(json.dumps({'agent_id': 'test-agent', 'asset_id': 'test-asset'}))
|
|
(state / 'device.key').write_text('SECRET-CANARY-DEVICE-KEY')
|
|
with sqlite3.connect(state / 'journal.sqlite') as db:
|
|
db.execute('CREATE TABLE safety(key TEXT PRIMARY KEY,value TEXT)')
|
|
db.execute("INSERT INTO safety VALUES('connected','1')")
|
|
db.close()
|
|
elf = bytearray(64); elf[:6] = b'\x7fELF\x02\x01'; elf[18:20] = b'\x3e\x00'
|
|
payload = dict(source=str(root / 'original'), binding={'agent_id': 'test-agent', 'asset_id': 'test-asset'},
|
|
binary=base64.b64encode(elf).decode(), binary_sha256=hashlib.sha256(elf).hexdigest(),
|
|
ca='-----BEGIN CERTIFICATE-----\ntest\n', unit='test unit', previous_binary_sha256='unused')
|
|
script = bootstrap.remote_script(payload, staging_root=root)
|
|
account = SimpleNamespace(pw_uid=993, pw_gid=984, pw_shell='/usr/sbin/nologin')
|
|
def command(args, **kwargs):
|
|
if fail_loader and '--list' in args:
|
|
raise subprocess.CalledProcessError(127, ['SECRET-CANARY-ARGV'], stderr=b'SECRET-CANARY-STDERR')
|
|
return subprocess.CompletedProcess(args, 0, b'', b'')
|
|
output = io.StringIO()
|
|
with patch('pwd.getpwnam', side_effect=[KeyError(), account]), patch('grp.getgrnam', side_effect=KeyError()), \
|
|
patch('grp.getgrall', return_value=[]), patch('os.chown'), patch('os.fchown'), \
|
|
patch('subprocess.run', side_effect=command), redirect_stdout(output):
|
|
exec(compile(script, '<generated-bootstrap>', 'exec'), {})
|
|
self.assertNotIn('SECRET-CANARY', output.getvalue())
|
|
return root, json.loads(output.getvalue())
|
|
|
|
def test_real_generated_script_missing_binary_parent_reports_and_migrates(self):
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
root, response = self.run_generated(folder)
|
|
self.assertIn('result', response)
|
|
report = json.loads((root / 'etc/rmm/vm-bootstrap-report.json').read_text())
|
|
self.assertTrue(report['migration_complete'])
|
|
self.assertTrue(report['byte_and_sqlite_preservation'])
|
|
self.assertFalse((root / 'original/state').exists())
|
|
self.assertEqual(report['before'], report['after'])
|
|
self.assertEqual((root / 'usr/local/libexec').stat().st_mode & 0o777, 0o755)
|
|
|
|
def test_generated_script_exception_is_sanitized_and_partial_report_persists(self):
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
root, response = self.run_generated(folder, fail_loader=True)
|
|
self.assertEqual(response['error']['exception_class'], 'CalledProcessError')
|
|
self.assertTrue(all(isinstance(n, int) for n in response['error']['traceback_lines']))
|
|
report = json.loads((root / 'etc/rmm/vm-bootstrap-report.json').read_text())
|
|
self.assertFalse(report['migration_complete'])
|
|
self.assertEqual(report['attempts'][-1]['error']['exception_class'], 'CalledProcessError')
|
|
self.assertTrue((root / 'original/state/identity.json').is_file())
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|