"""Bootstrap guard regressions; no SSH, system service or actual chown.""" import base64 import hashlib from pathlib import Path import os import subprocess import sys import tempfile from types import SimpleNamespace import unittest from unittest.mock import patch sys.path.insert(0, str(Path(__file__).resolve().parents[1]/'deploy')) import remote_client_bootstrap as bootstrap class InstallationGuards(unittest.TestCase): def staged_install(self, existing=False): with tempfile.TemporaryDirectory() as folder: root = Path(folder) def mapped(path): return root/str(path).lstrip('/') for name in ('usr/local/libexec', 'etc/systemd/system', 'var/lib'): (root/name).mkdir(parents=True, exist_ok=True) state = root/'var/lib/rmm-agent-remote' if existing: state.mkdir(mode=0o700) (state/'identity.json').write_bytes(b'preserved identity') binary = b'\x7fELF\x02\x01'+b'\x00'*12+b'\x3e\x00'+b'\x00'*64 payload = dict(action='install', binary=base64.b64encode(binary).decode(), binary_sha256=hashlib.sha256(binary).hexdigest(), token='x'*64, ca=base64.b64encode(b'public CA').decode(), unit='reviewed fixture unit', environment='reviewed fixture environment') original = os.umask(0o077) try: with patch.object(bootstrap, 'Path', side_effect=mapped), \ patch.object(bootstrap.os, 'geteuid', return_value=0), \ patch('pwd.getpwnam', return_value=SimpleNamespace(pw_uid=993, pw_gid=984)), \ patch.object(bootstrap.os, 'chown'), \ patch.object(bootstrap.subprocess, 'run', return_value=SimpleNamespace(stdout=b'inactive\n')) as run: if existing: with self.assertRaisesRegex(RuntimeError, 'existing installation'): bootstrap.vm(payload) self.assertEqual((state/'identity.json').read_bytes(), b'preserved identity') self.assertFalse((root/'usr/local/libexec/rmm-agent-remote').exists()) return result = bootstrap.vm(payload) self.assertTrue(result['installed']) self.assertEqual((root/'usr/local/libexec/rmm-agent-remote').stat().st_mode & 0o777, 0o755) self.assertEqual((root/'etc/rmm-remote').stat().st_mode & 0o777, 0o755) self.assertEqual((root/'etc/rmm-remote/ca.pem').stat().st_mode & 0o777, 0o644) self.assertEqual((root/'etc/rmm-remote/agent.env').stat().st_mode & 0o777, 0o600) self.assertEqual((state/'enroll.token').stat().st_mode & 0o777, 0o600) for call in run.call_args_list: self.assertNotIn('start', call.args[0]) finally: os.umask(original) def test_private_umask_does_not_prevent_unprivileged_execution_or_public_ca_read(self): self.staged_install() def test_existing_identity_is_preserved_and_never_reenrolled(self): self.staged_install(existing=True) def test_notebook_installation_option_is_rejected_before_any_remote_action(self): result = subprocess.run([sys.executable, str(Path(bootstrap.__file__)), '--provision-local'], capture_output=True, timeout=10) self.assertEqual(result.returncode, 2) self.assertFalse(hasattr(bootstrap, 'local')) if __name__ == '__main__': unittest.main()