| 1 | #!/usr/bin/env python3 |
| 2 | from importlib import import_module |
| 3 | from contextlib import redirect_stdout |
| 4 | import io |
| 5 | import json |
| 6 | import os |
| 7 | import sqlite3 |
| 8 | from pathlib import Path |
| 9 | import struct |
| 10 | import subprocess |
| 11 | import tempfile |
| 12 | import unittest |
| 13 | from unittest.mock import Mock, patch |
| 14 | |
| 15 | |
| 16 | stage = import_module("dashboard-stage") |
| 17 | boundary = import_module("dashboard-host") |
| 18 | |
| 19 | |
| 20 | class PreviewBoundary(unittest.TestCase): |
| 21 | def call(self, request): |
| 22 | payload = json.dumps(request).encode() + b"\n" |
| 23 | connection = Mock() |
| 24 | connection.__enter__ = Mock(return_value=connection) |
| 25 | connection.__exit__ = Mock(return_value=False) |
| 26 | connection.getsockopt.return_value = struct.pack("3i", 1, os.getuid(), 1) |
| 27 | connection.recv.side_effect = [payload, payload] |
| 28 | host = Mock() |
| 29 | host.handle.return_value = {"observed": True} |
| 30 | with patch.object(boundary.socket, "SO_PEERCRED", 17, create=True), patch.object(boundary, "stream_console") as stream, patch.object(boundary, "stream_upload") as upload: |
| 31 | boundary.serve_connection(connection, host, os.getuid(), stage.OPERATIONS) |
| 32 | stream.assert_not_called() |
| 33 | upload.assert_not_called() |
| 34 | sent = b"".join(call.args[0] for call in connection.sendall.call_args_list) |
| 35 | return json.loads(sent[4:]), host |
| 36 | |
| 37 | def test_denies_host_mutations_and_vm_streams_before_dispatch(self): |
| 38 | for operation in ["vm.create", "vm.start", "vm.console", "vm.serial", "vm.guest", "vm.upload", "vm.computer", "vm.owner", "vm.access", "storage.destroy", "deploy.start", "deploy.secret.get", "shale.write", "copyparty.manage", []]: |
| 39 | with self.subTest(operation=operation): |
| 40 | response, host = self.call({"operation": operation}) |
| 41 | self.assertEqual(response["status"], 403) |
| 42 | host.handle.assert_not_called() |
| 43 | |
| 44 | def test_read_uses_the_real_host_handler(self): |
| 45 | for operation in ["host.sample", "vm.node", "vm.domains", "vm.computers", "vm.library"]: |
| 46 | with self.subTest(operation=operation): |
| 47 | request = {"operation": operation} |
| 48 | response, host = self.call(request) |
| 49 | self.assertEqual(response, {"value": {"observed": True}}) |
| 50 | host.handle.assert_called_once_with(request) |
| 51 | |
| 52 | def test_stage_identity_rejects_paths_and_production(self): |
| 53 | for identity in ["dashboard", "knife-preview-12345678", "dashboard-preview-../private", "dashboard-preview-12345678\n"]: |
| 54 | with self.assertRaisesRegex(ValueError, "invalid dashboard stage"): |
| 55 | stage.metadata(identity) |
| 56 | |
| 57 | def test_attached_computer_cannot_target_another_vm(self): |
| 58 | host = stage.PreviewHost({"clone": "globe/staging/fixture", "computer": "mac-fixture"}) |
| 59 | with patch.object(boundary.Host, "handle", return_value={"ok": True}) as call: |
| 60 | for operation in ["vm.computer", "vm.owner", "vm.access", "vm.console", "vm.guest"]: |
| 61 | with self.assertRaises(boundary.dashboard_runs.Error): |
| 62 | host.handle({"operation": operation, "payload": {"name": "personal"}}) |
| 63 | call.assert_not_called() |
| 64 | self.assertEqual(host.handle({"operation": operation, "payload": {"name": "mac-fixture"}}), {"ok": True}) |
| 65 | call.reset_mock() |
| 66 | |
| 67 | def test_file_operations_cannot_name_production_or_account_dataset(self): |
| 68 | dataset = "globe/staging/dashboard-preview-12345678" |
| 69 | host = stage.PreviewHost({"clone": dataset}) |
| 70 | for operation in ["files.snapshot", "files.snapshots", "files.discard"]: |
| 71 | for target in ["globe/prod/knife", dataset, dataset + "/files/child", dataset + "/files/../data"]: |
| 72 | request = {"operation": operation, "dataset": target} if operation == "files.discard" else {"operation": operation, "datasets": [target]} |
| 73 | with self.subTest(request=request), patch.object(boundary.Host, "handle") as handler: |
| 74 | with self.assertRaises(boundary.dashboard_runs.Error) as error: |
| 75 | host.handle(request) |
| 76 | self.assertEqual(error.exception.status, 403) |
| 77 | handler.assert_not_called() |
| 78 | with patch.object(boundary.Host, "handle", return_value="real snapshot") as handler: |
| 79 | request = {"operation": "files.snapshot", "datasets": [dataset + "/files"]} |
| 80 | self.assertEqual(host.handle(request), "real snapshot") |
| 81 | handler.assert_called_once_with(request) |
| 82 | |
| 83 | def test_busy_observation_keeps_preview_vm_access_checks(self): |
| 84 | host = stage.PreviewHost({'clone': 'globe/staging/fixture', 'computer': 'fixture'}) |
| 85 | request = {'operation': 'vm.computer', 'payload': {'name': 'fixture', 'request': {'action': 'status'}}} |
| 86 | active = {'id': 'operation:' + 'a' * 32, 'action': 'exec'} |
| 87 | with patch.object(boundary.Host, 'handle', return_value={'readiness': 'busy'}) as call: |
| 88 | self.assertEqual(host.handle(request, active_snapshot=active), {'readiness': 'busy'}) |
| 89 | call.assert_called_once_with(request, active_snapshot=active) |
| 90 | call.reset_mock() |
| 91 | with self.assertRaises(boundary.dashboard_runs.Error): |
| 92 | host.handle({**request, 'payload': {**request['payload'], 'name': 'personal'}}, active_snapshot=active) |
| 93 | call.assert_not_called() |
| 94 | |
| 95 | def test_preview_vm_creation_and_ownership_stay_in_its_account(self): |
| 96 | with tempfile.TemporaryDirectory() as directory: |
| 97 | root = Path(directory) |
| 98 | (root / "data").mkdir() |
| 99 | with sqlite3.connect(root / "data/accounts.sqlite") as accounts: |
| 100 | accounts.execute("CREATE TABLE users (id TEXT, username TEXT)") |
| 101 | accounts.execute("INSERT INTO users VALUES ('preview-id', 'preview')") |
| 102 | host = stage.PreviewHost({"clone": "globe/staging/fixture", "mount": directory, "computer": "mac-fixture,linux-fixture"}) |
| 103 | with patch.object(boundary.Host, "handle", return_value={"owner": "preview-id", "ownerOnly": True}) as call: |
| 104 | for name in ["mac-fixture", "linux-fixture", "ai-new"]: |
| 105 | self.assertEqual(host.handle({"operation": "vm.owner", "payload": {"name": name}})["owner"], "preview-id") |
| 106 | for owner in ["foreign-id", None]: |
| 107 | call.reset_mock() |
| 108 | with self.assertRaises(boundary.dashboard_runs.Error): |
| 109 | host.handle({"operation": "vm.ai", "payload": {"action": "create", "owner": owner}}) |
| 110 | call.assert_not_called() |
| 111 | request = {"operation": "vm.ai", "payload": {"action": "create", "owner": "preview-id"}} |
| 112 | host.handle(request) |
| 113 | call.assert_called_once_with(request) |
| 114 | with patch.object(boundary.Host, "handle", return_value={"owner": "foreign-id", "ownerOnly": True}): |
| 115 | for operation in ["vm.computer", "vm.access"]: |
| 116 | with self.subTest(operation=operation), self.assertRaises(boundary.dashboard_runs.Error): |
| 117 | host.handle({"operation": operation, "payload": {"name": "ai-foreign"}}) |
| 118 | |
| 119 | def test_failed_child_creation_and_repeated_token_revoke_still_clean_parent(self): |
| 120 | identity = "dashboard-preview-12345678" |
| 121 | dataset = "globe/staging/" + identity |
| 122 | saved = {"clone": dataset, "mount": "/srv/staging/" + identity, "tokenAccessor": "already-revoked", |
| 123 | "external": {"files": {"clone": dataset + "/files"}}} |
| 124 | missing_token = subprocess.CompletedProcess([], 1, stderr="Error deleting token: Unexpected response code: 400 (Cannot delete nonexistent tokens: already-revoked)") |
| 125 | okay = subprocess.CompletedProcess([], 0, stderr="") |
| 126 | def process(argv, **kwargs): |
| 127 | return missing_token if argv[:4] == ["nomad", "acl", "token", "delete"] else okay |
| 128 | with patch.object(stage, "metadata", return_value=saved), patch.object(stage.snowglobe, "token"), patch.object(stage.subprocess, "run", side_effect=process), patch.object(stage.snowglobe, "command", return_value=subprocess.CompletedProcess([], 0, stdout=dataset + "\n")) as commands, patch.object(stage.snowglobe, "destroy_stage") as destroy, patch.object(stage.shutil, "rmtree"): |
| 129 | stage.destroy(identity) |
| 130 | self.assertEqual(saved["external"], {}) |
| 131 | self.assertEqual(saved["clone"], dataset) |
| 132 | destroy.assert_called_once_with(identity, saved, {}) |
| 133 | self.assertFalse(any(call.args[:2] == ("zfs", "destroy") for call in commands.call_args_list)) |
| 134 | |
| 135 | |
| 136 | class PreviewProof(unittest.TestCase): |
| 137 | def test_generated_proof_is_private_mounted_and_retained_on_restage(self): |
| 138 | with tempfile.TemporaryDirectory() as temporary: |
| 139 | root = Path(temporary) |
| 140 | state = root / "state" |
| 141 | (root / "srv/staging").mkdir(parents=True) |
| 142 | site = {"root": str(root / "srv"), "domain": "fixture.test", "pool": "fixture", "nodeName": "fixture", "tlsInternal": True} |
| 143 | jobs = [] |
| 144 | units = [] |
| 145 | builds = [] |
| 146 | image_stream = Mock() |
| 147 | image_stream.__enter__ = Mock(return_value=image_stream) |
| 148 | image_stream.__exit__ = Mock(return_value=False) |
| 149 | image_stream.wait.return_value = 0 |
| 150 | |
| 151 | def command(*args, **kwargs): |
| 152 | output = "" |
| 153 | if args[0] == "pkl": |
| 154 | output = json.dumps(site) |
| 155 | elif args[:3] == ("systemctl", "show", "snowglobe-host"): |
| 156 | output = "PATH=/bin" |
| 157 | elif args[:4] == ("nomad", "acl", "token", "create"): |
| 158 | output = json.dumps({"AccessorID": "fixture-accessor", "SecretID": "fixture-nomad-token"}) |
| 159 | elif args[:2] == ("podman", "run"): |
| 160 | output = json.dumps({"username": "preview", "password": "disposable-fixture-password"}) |
| 161 | elif args[:3] == ("nomad", "job", "run"): |
| 162 | jobs.append(json.loads(kwargs["input"])) |
| 163 | elif args[0] == "systemd-run": |
| 164 | units.append(args) |
| 165 | return subprocess.CompletedProcess(args, 0, stdout=output) |
| 166 | |
| 167 | def run(args, **kwargs): |
| 168 | if args[:3] == ["podman", "image", "exists"]: |
| 169 | return subprocess.CompletedProcess(args, 1) |
| 170 | if args[:2] == ["podman", "load"]: |
| 171 | self.assertIs(kwargs["stdin"], image_stream.stdout) |
| 172 | self.assertTrue(Path(kwargs["env"]["TMPDIR"]).is_relative_to(root / "srv/staging")) |
| 173 | if args[0] == "nix": |
| 174 | if "build" in args: |
| 175 | self.assertTrue(Path(args[args.index("build-dir") + 1]).is_relative_to(root / "srv/staging")) |
| 176 | builds.append(args) |
| 177 | return subprocess.CompletedProcess(args, 0, stdout="fixture-image" if args[0] == "nix" else "") |
| 178 | |
| 179 | original_exists = Path.exists |
| 180 | def exists(path): |
| 181 | return (str(path).startswith("/run/snowglobe-preview/") and path.name == "host.sock" |
| 182 | or str(path).startswith("/run/snowglobe-preview-screen-") and path.name == "screen.sock" |
| 183 | or original_exists(path)) |
| 184 | |
| 185 | with patch.object(stage.snowglobe, "STATE", state), patch.object(stage.snowglobe, "REPO", root / "releases/1234567890abcdef"), patch.object(stage.snowglobe, "token"), patch.object(stage.snowglobe, "identity", return_value=os.getuid()), patch.object(stage.snowglobe, "check_staging_pool"), patch.object(stage.snowglobe, "check_http") as check_http, patch.object(stage.snowglobe, "command", side_effect=command), patch.object(stage.subprocess, "run", side_effect=run), patch.object(stage.subprocess, "Popen", return_value=image_stream), patch.object(stage.os, "chown") as chown, patch.object(Path, "exists", exists), redirect_stdout(io.StringIO()): |
| 186 | stage.stage() |
| 187 | self.assertTrue(any(target.endswith("#dashboard-image.stream") for build in builds for target in build)) |
| 188 | check_http.assert_called_once_with(jobs[-1]["ID"] + ".fixture.test", "/sign-in", True) |
| 189 | task = jobs[-1]["TaskGroups"][0]["Tasks"][0] |
| 190 | check = jobs[-1]["TaskGroups"][0]["Services"][0]["Checks"][0] |
| 191 | self.assertEqual(check["Type"], "tcp") |
| 192 | self.assertNotIn("Path", check) |
| 193 | self.assertNotIn("Header", check) |
| 194 | directory = root / "srv/staging" / jobs[-1]["ID"] |
| 195 | proof = directory / "proxy.token" |
| 196 | value = proof.read_text() |
| 197 | self.assertRegex(value.strip(), r"^[0-9a-f]{64}$") |
| 198 | self.assertEqual(proof.stat().st_mode & 0o777, 0o400) |
| 199 | chown.assert_any_call(proof, os.getuid(), os.getuid()) |
| 200 | self.assertEqual(task["Env"]["SNOWGLOBE_PROXY_TOKEN_FILE"], "/run/secrets/proxy.token") |
| 201 | self.assertIn(str(proof) + ":/run/secrets/proxy.token:ro", task["Config"]["volumes"]) |
| 202 | self.assertNotIn(value.strip(), json.dumps(jobs[-1])) |
| 203 | stage.stage() |
| 204 | self.assertEqual(proof.read_text(), value) |
| 205 | self.assertEqual(jobs[-1]["ID"], jobs[-2]["ID"]) |
| 206 | stage.stage("vm-fixture") |
| 207 | task = jobs[-1]["TaskGroups"][0]["Tasks"][0] |
| 208 | screen_runtime = "/run/snowglobe-preview-screen-" + jobs[-1]["ID"] |
| 209 | self.assertIn(screen_runtime + ":/run/snowglobe-vm-screen:ro", task["Config"]["volumes"]) |
| 210 | screen = next(unit for unit in units if "--unit=snowglobe-preview-screen-" + jobs[-1]["ID"] in unit) |
| 211 | self.assertIn("--setenv=SNOWGLOBE_HOST_SOCKET=/run/snowglobe-preview/" + jobs[-1]["ID"] + "/host.sock", screen) |
| 212 | |
| 213 | |
| 214 | class NativePreview(unittest.TestCase): |
| 215 | def test_empty_proof_requires_loopback_and_fresh_account_stays_private(self): |
| 216 | binary = Path(os.environ.get("DASHBOARD_BINARY", "dashboard/target/debug/home-dashboard")).resolve() |
| 217 | if not binary.exists(): |
| 218 | self.skipTest("build home-dashboard to exercise its native auth CLI") |
| 219 | with tempfile.TemporaryDirectory() as temporary: |
| 220 | env = {**os.environ, "SNOWGLOBE_LISTEN_ADDRESS": "0.0.0.0", "SNOWGLOBE_PROXY_TOKEN_FILE": "", "SNOWGLOBE_DATA_DIR": temporary, |
| 221 | "SNOWGLOBE_DOMAIN": "preview.test", "SNOWGLOBE_PUBLIC_ORIGIN": "https://dashboard.preview.test", "SNOWGLOBE_FILE_ORIGIN": "https://files.preview.test", |
| 222 | "SNOWGLOBE_AUTH_RP_ID": "dashboard.preview.test"} |
| 223 | rejected = subprocess.run([str(binary), "--preview-account"], env=env, capture_output=True, text=True) |
| 224 | self.assertNotEqual(rejected.returncode, 0) |
| 225 | self.assertIn("before listening on a non-loopback address", rejected.stderr) |
| 226 | self.assertFalse(Path(temporary, "accounts.sqlite").exists()) |
| 227 | env["SNOWGLOBE_LISTEN_ADDRESS"] = "127.0.0.1" |
| 228 | created = subprocess.run([str(binary), "--preview-account"], env=env, capture_output=True, text=True, check=True) |
| 229 | credentials = json.loads(created.stdout) |
| 230 | self.assertEqual(credentials["username"], "preview") |
| 231 | self.assertGreaterEqual(len(credentials["password"]), 32) |
| 232 | self.assertEqual(Path(temporary, "accounts.sqlite").stat().st_mode & 0o777, 0o600) |
| 233 | again = subprocess.run([str(binary), "--preview-account"], env=env, capture_output=True, text=True) |
| 234 | self.assertNotEqual(again.returncode, 0) |
| 235 | self.assertIn("empty store", again.stderr) |
| 236 | self.assertNotIn(credentials["password"], again.stdout + again.stderr) |
| 237 | |
| 238 | |
| 239 | if __name__ == "__main__": |
| 240 | unittest.main() |