104 lines
4.4 KiB
Python
104 lines
4.4 KiB
Python
"""Check that the runner VM script keeps the boundary of the production runner.
|
|
|
|
Production runs the code runner only in an LXD virtual machine with its own
|
|
kernel, no shared folder, no Docker socket, and no secret of the core. A
|
|
privileged container on the core host must never replace it. These tests
|
|
read files and run `bash -n`; they need no LXD, Docker, or network.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
import shutil
|
|
import subprocess
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
SCRIPT = ROOT / "deploy" / "labs" / "runner-vm.sh"
|
|
PRESEED = ROOT / "deploy" / "labs" / "lxd-preseed.yaml"
|
|
DEPLOY_MAKEFILE = ROOT / "deploy" / "Makefile"
|
|
|
|
# Switches that would share the host with the guest or weaken the VM.
|
|
FORBIDDEN = {
|
|
"a device added to the instance (shared folder or socket)": r"\bdevice\s+add\b",
|
|
"a privileged or nested instance": r"security\.(privileged|nesting)\s*=\s*true",
|
|
"raw LXC or QEMU settings": r"\braw\.(lxc|qemu)\b",
|
|
"a privileged container": r"--privileged",
|
|
"a mounted Docker socket": r"-v\s+\S*docker\.sock",
|
|
"sudo": r"\bsudo\b",
|
|
"the dev plaintext switch": r"RUNNER_DEV_ALLOW_PLAINTEXT_HTTP",
|
|
}
|
|
|
|
# Settings of the runner unit that carry the isolation and the budget.
|
|
UNIT_SETTINGS = (
|
|
"RootDirectory=$guest_root",
|
|
"Delegate=yes",
|
|
"ProtectSystem=strict",
|
|
"NoNewPrivileges=yes",
|
|
"IPAddressDeny=any",
|
|
"exec,nosuid,nodev",
|
|
"Environment=RUNNER_TLS_CERT_FILE=",
|
|
"Environment=RUNNER_API_TOKEN_FILE=",
|
|
"Environment=RUNNER_MAX_CONCURRENT_RUNS=1",
|
|
)
|
|
|
|
|
|
class RunnerVMScriptTest(unittest.TestCase):
|
|
text = SCRIPT.read_text(encoding="utf-8")
|
|
# Comments may name what the script never does.
|
|
code = "\n".join(line for line in text.splitlines() if not line.lstrip().startswith("#"))
|
|
|
|
@unittest.skipUnless(shutil.which("bash"), "bash is not installed")
|
|
def test_the_script_parses(self) -> None:
|
|
subprocess.run(["bash", "-n", str(SCRIPT)], check=True)
|
|
|
|
def test_the_runner_is_a_virtual_machine_with_only_its_profile(self) -> None:
|
|
init = [line for line in self.text.splitlines() if re.search(r"\blxcq init\b", line)]
|
|
self.assertEqual(len(init), 1, init)
|
|
self.assertIn("--vm", init[0])
|
|
self.assertIn('--profile "$vm_profile"', init[0])
|
|
self.assertIn("vm_profile=${RUNNER_VM_PROFILE:-lincodex-runner}", self.text)
|
|
|
|
def test_nothing_shares_the_host_or_weakens_the_vm(self) -> None:
|
|
for name, pattern in FORBIDDEN.items():
|
|
with self.subTest(switch=name):
|
|
self.assertIsNone(re.search(pattern, self.code), name)
|
|
|
|
def test_the_unit_keeps_the_sandbox_and_budget_settings(self) -> None:
|
|
for setting in UNIT_SETTINGS:
|
|
with self.subTest(setting=setting):
|
|
self.assertIn(setting, self.text)
|
|
|
|
def test_the_guest_gets_a_static_address_and_no_route(self) -> None:
|
|
self.assertIn("dhcp4: false", self.text)
|
|
self.assertNotRegex(self.text, r"\b(gateway4|routes|nameservers)\s*:")
|
|
self.assertIn("security.devlxd=false", self.text)
|
|
|
|
def test_secrets_live_outside_the_repository(self) -> None:
|
|
self.assertIn("state_dir=${RUNNER_VM_STATE_DIR:-$HOME/lincodex-runner}", self.text)
|
|
self.assertIn("umask 077", self.text)
|
|
# Only the runner's own TLS pair and token enter the guest; the CA key stays on the host.
|
|
pushed = re.findall(r"for file in ([a-z. -]+); do", self.text)
|
|
self.assertTrue(pushed)
|
|
for files in pushed:
|
|
self.assertEqual(files.split(), ["tls.crt", "tls.key", "api-token"])
|
|
|
|
def test_the_profile_keeps_the_initial_budget(self) -> None:
|
|
preseed = PRESEED.read_text(encoding="utf-8")
|
|
profile = preseed.split("- name: lincodex-runner\n", 1)[1].split("\n - name:", 1)[0]
|
|
self.assertIn('limits.cpu: "2"', profile)
|
|
self.assertIn("limits.memory: 3GiB", profile)
|
|
self.assertIn("limits.memory.enforce: hard", profile)
|
|
self.assertIn("network: lxdbr-runner", profile)
|
|
|
|
def test_make_targets_call_the_script(self) -> None:
|
|
makefile = DEPLOY_MAKEFILE.read_text(encoding="utf-8")
|
|
for command in ("up", "secrets", "rootfs", "create", "install", "a16", "check"):
|
|
with self.subTest(command=command):
|
|
self.assertRegex(makefile, rf"runner-vm-{command}:.*\n\t@bash \"\$\(RUNNER_VM_SCRIPT\)\" {command}\n")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|