1#!/usr/bin/env python3
2from importlib import import_module
3from contextlib import redirect_stdout
4import io
5import json
6import os
7import sqlite3
8from pathlib import Path
9import struct
10import subprocess
11import tempfile
12import unittest
13from unittest.mock import Mock, patch
14
15
16stage = import_module("dashboard-stage")
17boundary = import_module("dashboard-host")
18
19
20class 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_preview_vm_creation_and_ownership_stay_in_its_account(self):
84 with tempfile.TemporaryDirectory() as directory:
85 root = Path(directory)
86 (root / "data").mkdir()
87 with sqlite3.connect(root / "data/accounts.sqlite") as accounts:
88 accounts.execute("CREATE TABLE users (id TEXT, username TEXT)")
89 accounts.execute("INSERT INTO users VALUES ('preview-id', 'preview')")
90 host = stage.PreviewHost({"clone": "globe/staging/fixture", "mount": directory, "computer": "mac-fixture,linux-fixture"})
91 with patch.object(boundary.Host, "handle", return_value={"owner": "preview-id", "ownerOnly": True}) as call:
92 for name in ["mac-fixture", "linux-fixture", "ai-new"]:
93 self.assertEqual(host.handle({"operation": "vm.owner", "payload": {"name": name}})["owner"], "preview-id")
94 for owner in ["foreign-id", None]:
95 call.reset_mock()
96 with self.assertRaises(boundary.dashboard_runs.Error):
97 host.handle({"operation": "vm.ai", "payload": {"action": "create", "owner": owner}})
98 call.assert_not_called()
99 request = {"operation": "vm.ai", "payload": {"action": "create", "owner": "preview-id"}}
100 host.handle(request)
101 call.assert_called_once_with(request)
102 with patch.object(boundary.Host, "handle", return_value={"owner": "foreign-id", "ownerOnly": True}):
103 for operation in ["vm.computer", "vm.access"]:
104 with self.subTest(operation=operation), self.assertRaises(boundary.dashboard_runs.Error):
105 host.handle({"operation": operation, "payload": {"name": "ai-foreign"}})
106
107 def test_failed_child_creation_and_repeated_token_revoke_still_clean_parent(self):
108 identity = "dashboard-preview-12345678"
109 dataset = "globe/staging/" + identity
110 saved = {"clone": dataset, "mount": "/srv/staging/" + identity, "tokenAccessor": "already-revoked",
111 "external": {"files": {"clone": dataset + "/files"}}}
112 missing_token = subprocess.CompletedProcess([], 1, stderr="Error deleting token: Unexpected response code: 400 (Cannot delete nonexistent tokens: already-revoked)")
113 okay = subprocess.CompletedProcess([], 0, stderr="")
114 def process(argv, **kwargs):
115 return missing_token if argv[:4] == ["nomad", "acl", "token", "delete"] else okay
116 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"):
117 stage.destroy(identity)
118 self.assertEqual(saved["external"], {})
119 self.assertEqual(saved["clone"], dataset)
120 destroy.assert_called_once_with(identity, saved, {})
121 self.assertFalse(any(call.args[:2] == ("zfs", "destroy") for call in commands.call_args_list))
122
123
124class PreviewProof(unittest.TestCase):
125 def test_generated_proof_is_private_mounted_and_retained_on_restage(self):
126 with tempfile.TemporaryDirectory() as temporary:
127 root = Path(temporary)
128 state = root / "state"
129 (root / "srv/staging").mkdir(parents=True)
130 site = {"root": str(root / "srv"), "domain": "fixture.test", "pool": "fixture", "nodeName": "fixture", "tlsInternal": True}
131 jobs = []
132 units = []
133 builds = []
134 image_stream = Mock()
135 image_stream.__enter__ = Mock(return_value=image_stream)
136 image_stream.__exit__ = Mock(return_value=False)
137 image_stream.wait.return_value = 0
138
139 def command(*args, **kwargs):
140 output = ""
141 if args[0] == "pkl":
142 output = json.dumps(site)
143 elif args[:3] == ("systemctl", "show", "snowglobe-host"):
144 output = "PATH=/bin"
145 elif args[:4] == ("nomad", "acl", "token", "create"):
146 output = json.dumps({"AccessorID": "fixture-accessor", "SecretID": "fixture-nomad-token"})
147 elif args[:2] == ("podman", "run"):
148 output = json.dumps({"username": "preview", "password": "disposable-fixture-password"})
149 elif args[:3] == ("nomad", "job", "run"):
150 jobs.append(json.loads(kwargs["input"]))
151 elif args[0] == "systemd-run":
152 units.append(args)
153 return subprocess.CompletedProcess(args, 0, stdout=output)
154
155 def run(args, **kwargs):
156 if args[:3] == ["podman", "image", "exists"]:
157 return subprocess.CompletedProcess(args, 1)
158 if args[:2] == ["podman", "load"]:
159 self.assertIs(kwargs["stdin"], image_stream.stdout)
160 self.assertTrue(Path(kwargs["env"]["TMPDIR"]).is_relative_to(root / "srv/staging"))
161 if args[0] == "nix":
162 if "build" in args:
163 self.assertTrue(Path(args[args.index("build-dir") + 1]).is_relative_to(root / "srv/staging"))
164 builds.append(args)
165 return subprocess.CompletedProcess(args, 0, stdout="fixture-image" if args[0] == "nix" else "")
166
167 original_exists = Path.exists
168 def exists(path):
169 return (str(path).startswith("/run/snowglobe-preview/") and path.name == "host.sock"
170 or str(path).startswith("/run/snowglobe-preview-screen-") and path.name == "screen.sock"
171 or original_exists(path))
172
173 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()):
174 stage.stage()
175 self.assertTrue(any(target.endswith("#dashboard-image.stream") for build in builds for target in build))
176 check_http.assert_called_once_with(jobs[-1]["ID"] + ".fixture.test", "/sign-in", True)
177 task = jobs[-1]["TaskGroups"][0]["Tasks"][0]
178 check = jobs[-1]["TaskGroups"][0]["Services"][0]["Checks"][0]
179 self.assertEqual(check["Type"], "tcp")
180 self.assertNotIn("Path", check)
181 self.assertNotIn("Header", check)
182 directory = root / "srv/staging" / jobs[-1]["ID"]
183 proof = directory / "proxy.token"
184 value = proof.read_text()
185 self.assertRegex(value.strip(), r"^[0-9a-f]{64}$")
186 self.assertEqual(proof.stat().st_mode & 0o777, 0o400)
187 chown.assert_any_call(proof, os.getuid(), os.getuid())
188 self.assertEqual(task["Env"]["SNOWGLOBE_PROXY_TOKEN_FILE"], "/run/secrets/proxy.token")
189 self.assertIn(str(proof) + ":/run/secrets/proxy.token:ro", task["Config"]["volumes"])
190 self.assertNotIn(value.strip(), json.dumps(jobs[-1]))
191 stage.stage()
192 self.assertEqual(proof.read_text(), value)
193 self.assertEqual(jobs[-1]["ID"], jobs[-2]["ID"])
194 stage.stage("vm-fixture")
195 task = jobs[-1]["TaskGroups"][0]["Tasks"][0]
196 screen_runtime = "/run/snowglobe-preview-screen-" + jobs[-1]["ID"]
197 self.assertIn(screen_runtime + ":/run/snowglobe-vm-screen:ro", task["Config"]["volumes"])
198 screen = next(unit for unit in units if "--unit=snowglobe-preview-screen-" + jobs[-1]["ID"] in unit)
199 self.assertIn("--setenv=SNOWGLOBE_HOST_SOCKET=/run/snowglobe-preview/" + jobs[-1]["ID"] + "/host.sock", screen)
200
201
202class NativePreview(unittest.TestCase):
203 def test_empty_proof_requires_loopback_and_fresh_account_stays_private(self):
204 binary = Path(os.environ.get("DASHBOARD_BINARY", "dashboard/target/debug/home-dashboard")).resolve()
205 if not binary.exists():
206 self.skipTest("build home-dashboard to exercise its native auth CLI")
207 with tempfile.TemporaryDirectory() as temporary:
208 env = {**os.environ, "SNOWGLOBE_LISTEN_ADDRESS": "0.0.0.0", "SNOWGLOBE_PROXY_TOKEN_FILE": "", "SNOWGLOBE_DATA_DIR": temporary,
209 "SNOWGLOBE_DOMAIN": "preview.test", "SNOWGLOBE_PUBLIC_ORIGIN": "https://dashboard.preview.test", "SNOWGLOBE_FILE_ORIGIN": "https://files.preview.test",
210 "SNOWGLOBE_AUTH_RP_ID": "dashboard.preview.test"}
211 rejected = subprocess.run([str(binary), "--preview-account"], env=env, capture_output=True, text=True)
212 self.assertNotEqual(rejected.returncode, 0)
213 self.assertIn("before listening on a non-loopback address", rejected.stderr)
214 self.assertFalse(Path(temporary, "accounts.sqlite").exists())
215 env["SNOWGLOBE_LISTEN_ADDRESS"] = "127.0.0.1"
216 created = subprocess.run([str(binary), "--preview-account"], env=env, capture_output=True, text=True, check=True)
217 credentials = json.loads(created.stdout)
218 self.assertEqual(credentials["username"], "preview")
219 self.assertGreaterEqual(len(credentials["password"]), 32)
220 self.assertEqual(Path(temporary, "accounts.sqlite").stat().st_mode & 0o777, 0o600)
221 again = subprocess.run([str(binary), "--preview-account"], env=env, capture_output=True, text=True)
222 self.assertNotEqual(again.returncode, 0)
223 self.assertIn("empty store", again.stderr)
224 self.assertNotIn(credentials["password"], again.stdout + again.stderr)
225
226
227if __name__ == "__main__":
228 unittest.main()