Keep the Proxmox host responsible only for container creation. Serve the private backend on port 5187 behind the operator's HTTPS proxy and install a self-updating in-container command. Preserve existing identity and history while migrating the old installer-owned proxy with listener rollback. Refs #21
This commit is contained in:
+136
-43
@@ -83,10 +83,10 @@ class HostTests(unittest.TestCase):
|
||||
host.run(["pct", "exec", 201, "--", "true"])
|
||||
self.assertEqual(["pct", "exec", "201", "--keep-env", "0", "--", "true"], runner.call_args.args[0])
|
||||
|
||||
def test_update_cannot_silently_change_identity(self):
|
||||
args = host.parser().parse_args(["update", "--ctid", "201", "--email", "[email protected]"])
|
||||
with self.assertRaises(ValueError):
|
||||
host.validate(args)
|
||||
def test_host_refuses_update_before_any_proxmox_command(self):
|
||||
with patch.object(host, "run", side_effect=AssertionError("No host update")), contextlib.redirect_stderr(io.StringIO()):
|
||||
with self.assertRaises(SystemExit):
|
||||
host.main(["update", "--ctid", "201"])
|
||||
|
||||
def test_snapshot_includes_uncommitted_source_but_excludes_outputs_and_hidden_files(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
@@ -101,7 +101,8 @@ class HostTests(unittest.TestCase):
|
||||
(path / "server.json").write_text('{"synthetic":"local runtime config"}')
|
||||
(path / "operator.env").write_text("SYNTHETIC_LOCAL_CONFIGURATION=placeholder")
|
||||
(path / "private.key").write_text("synthetic local key fixture")
|
||||
for name in ("Directory.Build.props", "Directory.Packages.props", "global.json", "LICENSE"):
|
||||
for name in ("Directory.Build.props", "Directory.Packages.props", "global.json", "LICENSE", "tools/lxc_update.py"):
|
||||
(root / name).parent.mkdir(parents=True, exist_ok=True)
|
||||
(root / name).write_text("fixture")
|
||||
archive = Path(tmp) / "source.tgz"
|
||||
digest = host.snapshot(root, archive)
|
||||
@@ -125,45 +126,13 @@ class HostTests(unittest.TestCase):
|
||||
project = root / "src/FinalToken.Core"
|
||||
project.mkdir(parents=True)
|
||||
(project / "outside.txt").write_text("must not read")
|
||||
for name in ("Directory.Build.props", "Directory.Packages.props", "global.json", "LICENSE"):
|
||||
for name in ("Directory.Build.props", "Directory.Packages.props", "global.json", "LICENSE", "tools/lxc_update.py"):
|
||||
(root / name).parent.mkdir(parents=True, exist_ok=True)
|
||||
(root / name).write_text("fixture")
|
||||
original = Path.is_symlink
|
||||
with patch.object(Path, "is_symlink", lambda path: path == project or original(path)), self.assertRaises(ValueError):
|
||||
host.snapshot(root, Path(tmp) / "source.tgz")
|
||||
|
||||
def test_foreign_or_stopped_ct_is_not_updated(self):
|
||||
args = host.parser().parse_args(["update", "--ctid", "201"])
|
||||
host.validate(args)
|
||||
for status in ("status: stopped", "status: running"):
|
||||
calls = []
|
||||
def fake(command, **kwargs):
|
||||
calls.append(command)
|
||||
return subprocess.CompletedProcess(command, 0, status if command[1] == "status" else '{"installer":"foreign"}')
|
||||
with patch.object(host, "run", side_effect=fake), self.assertRaises(ValueError):
|
||||
host.deploy(args, Path("unused"), "0" * 64)
|
||||
self.assertFalse(any("apt-get" in c or "start" in c or "create" in c for c in calls))
|
||||
|
||||
def test_update_uploads_new_source_without_install_identity_or_ct_creation(self):
|
||||
args = host.parser().parse_args(["update", "--ctid", "201"])
|
||||
host.validate(args)
|
||||
calls, manifests = [], []
|
||||
def fake(command, **kwargs):
|
||||
command = list(map(str, command))
|
||||
calls.append(command)
|
||||
text = "status: running" if command[:2] == ["pct", "status"] else ""
|
||||
if command[-1] == "/etc/finaltoken/install.json":
|
||||
text = json.dumps({"installer": "finaltoken-proxmox-v1", "ctid": 201, "role": "server"})
|
||||
if command[:2] == ["pct", "push"] and command[3].endswith("manifest.json"):
|
||||
manifests.append(json.loads(Path(command[3]).read_text()))
|
||||
return subprocess.CompletedProcess(command, 0, text)
|
||||
with tempfile.TemporaryDirectory() as tmp, patch.object(host, "run", side_effect=fake), contextlib.redirect_stdout(io.StringIO()):
|
||||
archive = Path(tmp) / "source.tgz"
|
||||
archive.write_bytes(b"updated source fixture")
|
||||
host.deploy(args, archive, "a" * 64)
|
||||
self.assertEqual([{"installer": "finaltoken-proxmox-v1", "ctid": 201, "action": "update", "sourceSha256": "a" * 64}], manifests)
|
||||
self.assertFalse(any("create" in c or "start" in c or "destroy" in c for c in calls))
|
||||
self.assertTrue(any("python3" in c and "--manifest" in c for c in calls))
|
||||
|
||||
def test_install_orders_create_start_push_and_guest_without_shell_or_destroy(self):
|
||||
calls = []
|
||||
def fake(command, **kwargs):
|
||||
@@ -209,6 +178,127 @@ class HostTests(unittest.TestCase):
|
||||
|
||||
|
||||
class GuestTests(unittest.TestCase):
|
||||
def test_server_packages_do_not_install_a_reverse_proxy(self):
|
||||
with patch.object(guest, "run") as runner, patch.object(Path, "exists", return_value=True):
|
||||
guest.packages("server", [])
|
||||
commands = [list(map(str, call.args[0])) for call in runner.call_args_list]
|
||||
self.assertFalse(any("caddy" in command for command in commands))
|
||||
|
||||
def test_fresh_server_exposes_backend_without_proxy_configuration(self):
|
||||
with tempfile.TemporaryDirectory() as tmp, contextlib.ExitStack() as stack:
|
||||
root = Path(tmp)
|
||||
for name, relative in (("ETC", "etc"), ("SERVER_HOME", "server"), ("SYSTEMD", "units")):
|
||||
stack.enter_context(patch.object(guest, name, root / relative))
|
||||
users = stack.enter_context(patch.object(guest, "user"))
|
||||
runner = stack.enter_context(patch.object(guest, "run"))
|
||||
stack.enter_context(patch.object(guest.pwd, "getpwnam", return_value=types.SimpleNamespace(pw_gid=1000)))
|
||||
stack.enter_context(patch.object(guest.os, "chown", create=True))
|
||||
guest.configure({"role": "server", "providers": [], "emails": {}, "ctid": 201,
|
||||
"machineId": "lxc_201", "timezone": "UTC", "domain": "usage.example.test", "tls": "acme"})
|
||||
environment = (guest.ETC / "server.env").read_text()
|
||||
self.assertTrue("ASPNETCORE_URLS=http://0.0.0.0:5187" in environment)
|
||||
self.assertIn("FINALTOKEN_PUBLIC_ORIGIN=https://usage.example.test", environment)
|
||||
self.assertFalse((guest.ETC / "Caddyfile").exists())
|
||||
self.assertFalse((guest.SYSTEMD / "finaltoken-proxy.service").exists())
|
||||
self.assertEqual(["finaltoken-server"], [call.args[0] for call in users.call_args_list])
|
||||
self.assertFalse(any("caddy.service" in call.args[0] for call in runner.call_args_list))
|
||||
|
||||
def test_only_application_services_are_managed(self):
|
||||
self.assertEqual(["finaltoken-server.service"], guest.units("server"))
|
||||
|
||||
def test_legacy_proxy_migration_preserves_keys_and_stops_only_owned_proxy(self):
|
||||
for newline in (b"\n", b"\r\n"):
|
||||
with self.subTest(newline=newline), tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp)
|
||||
(root / "units").mkdir()
|
||||
(root / "units/finaltoken-proxy.service").write_text("owned fixture")
|
||||
original = newline.join([b"FINALTOKEN_DASHBOARD_KEY=synthetic-fixture-only", b"ASPNETCORE_URLS=http://127.0.0.1:5187", b"FINALTOKEN_PUBLIC_ORIGIN=https://usage.example.test", b""])
|
||||
(root / "server.env").write_bytes(original)
|
||||
(root / "server.json").write_bytes(b"operator config")
|
||||
calls = []
|
||||
def fake(command, **kwargs):
|
||||
calls.append(command)
|
||||
return subprocess.CompletedProcess(command, 0)
|
||||
with patch.object(guest, "ETC", root), patch.object(guest, "SYSTEMD", root / "units"), \
|
||||
patch.object(guest, "run", side_effect=fake), patch.object(guest, "healthy", return_value=True):
|
||||
updated = guest.migrate_external_proxy({"role": "server", "domain": "usage.example.test"})
|
||||
self.assertEqual("external", updated["network"])
|
||||
self.assertTrue((root / "server.env").read_bytes() == original.replace(b"127.0.0.1:5187", b"0.0.0.0:5187"))
|
||||
self.assertTrue((root / "server.env.pre-external-proxy").read_bytes() == original)
|
||||
self.assertEqual(b"operator config", (root / "server.json").read_bytes())
|
||||
self.assertIn(["systemctl", "disable", "--now", "finaltoken-proxy.service"], calls)
|
||||
self.assertFalse(any("caddy.service" in command or "purge" in command or "remove" in command for command in calls))
|
||||
|
||||
def test_failed_proxy_migration_restores_listener_and_running_services(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp)
|
||||
(root / "units").mkdir()
|
||||
(root / "units/finaltoken-proxy.service").write_text("owned")
|
||||
original = "ASPNETCORE_URLS=http://127.0.0.1:5187\n"
|
||||
(root / "server.env").write_bytes(original.encode())
|
||||
with patch.object(guest, "ETC", root), patch.object(guest, "SYSTEMD", root / "units"), \
|
||||
patch.object(guest, "run", return_value=subprocess.CompletedProcess([], 0)) as runner, \
|
||||
patch.object(guest, "healthy", return_value=False), self.assertRaises(ValueError):
|
||||
guest.migrate_external_proxy({"role": "server"})
|
||||
self.assertEqual(original.encode(), (root / "server.env").read_bytes())
|
||||
commands = [call.args[0] for call in runner.call_args_list]
|
||||
self.assertIn(["systemctl", "restart", "finaltoken-proxy.service"], commands)
|
||||
self.assertNotIn(["systemctl", "disable", "--now", "finaltoken-proxy.service"], commands)
|
||||
|
||||
def test_custom_listener_is_not_overwritten_by_migration(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp)
|
||||
original = b"ASPNETCORE_URLS=http://192.0.2.10:6000\n"
|
||||
(root / "server.env").write_bytes(original)
|
||||
with patch.object(guest, "ETC", root), patch.object(guest, "run") as runner, self.assertRaises(ValueError):
|
||||
guest.migrate_external_proxy({"role": "server"})
|
||||
runner.assert_not_called()
|
||||
self.assertEqual(original, (root / "server.env").read_bytes())
|
||||
|
||||
def test_proxy_stop_failure_rolls_back_listener_and_proxy_state(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp)
|
||||
(root / "units").mkdir()
|
||||
(root / "units/finaltoken-proxy.service").write_text("owned")
|
||||
original = b"ASPNETCORE_URLS=http://127.0.0.1:5187\n"
|
||||
(root / "server.env").write_bytes(original)
|
||||
calls = []
|
||||
def fake(command, **kwargs):
|
||||
calls.append(command)
|
||||
if command[1:3] == ["disable", "--now"]:
|
||||
raise subprocess.CalledProcessError(1, command)
|
||||
return subprocess.CompletedProcess(command, 0)
|
||||
with patch.object(guest, "ETC", root), patch.object(guest, "SYSTEMD", root / "units"), \
|
||||
patch.object(guest, "run", side_effect=fake), patch.object(guest, "healthy", return_value=True), \
|
||||
self.assertRaises(subprocess.CalledProcessError):
|
||||
guest.migrate_external_proxy({"role": "server"})
|
||||
self.assertEqual(original, (root / "server.env").read_bytes())
|
||||
self.assertIn(["systemctl", "enable", "finaltoken-proxy.service"], calls)
|
||||
self.assertIn(["systemctl", "restart", "finaltoken-proxy.service"], calls)
|
||||
|
||||
def test_migration_keeps_intentionally_stopped_backend_stopped(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp)
|
||||
(root / "server.env").write_bytes(b"ASPNETCORE_URLS=http://127.0.0.1:5187\n")
|
||||
with patch.object(guest, "ETC", root), patch.object(guest, "SYSTEMD", root / "units"), \
|
||||
patch.object(guest, "run", return_value=subprocess.CompletedProcess([], 3)) as runner, \
|
||||
patch.object(guest, "healthy") as healthy:
|
||||
guest.migrate_external_proxy({"role": "server"})
|
||||
healthy.assert_not_called()
|
||||
self.assertFalse(any("restart" in call.args[0] or "start" in call.args[0] for call in runner.call_args_list))
|
||||
|
||||
def test_worker_migration_does_not_access_server_configuration(self):
|
||||
with patch.object(Path, "read_bytes", side_effect=AssertionError("Worker has no server environment")):
|
||||
self.assertEqual("external", guest.migrate_external_proxy({"role": "worker"})["network"])
|
||||
|
||||
def test_updater_registration_does_not_overwrite_an_operator_file(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
path = Path(tmp) / "finaltoken-update"
|
||||
path.write_bytes(b"operator file")
|
||||
with patch.object(guest, "UPDATER", path), self.assertRaises(ValueError):
|
||||
guest.install_updater()
|
||||
self.assertEqual(b"operator file", path.read_bytes())
|
||||
|
||||
def test_codex_npm_install_creates_the_configured_launcher(self):
|
||||
node, npm = shutil.which("node"), shutil.which("npm")
|
||||
if not node or not npm:
|
||||
@@ -269,7 +359,7 @@ class GuestTests(unittest.TestCase):
|
||||
archive.write_bytes(b"updated server fixture")
|
||||
digest = hashlib.sha256(archive.read_bytes()).hexdigest()
|
||||
identity = {"installer": guest.INSTALLER, "ctid": 201, "installed": True,
|
||||
"role": "server", "providers": [], "domain": "usage.example.test",
|
||||
"role": "server", "network": "external", "providers": [], "domain": "usage.example.test",
|
||||
"sourceSha256": "0" * 64, "release": "previous"}
|
||||
(etc / "install.json").write_text(json.dumps(identity))
|
||||
config, history = etc / "server.json", root / "history.json"
|
||||
@@ -284,7 +374,7 @@ class GuestTests(unittest.TestCase):
|
||||
patch.object(Path, "read_text", read), patch.object(guest, "packages") as packages, \
|
||||
patch.object(guest, "configure") as configure, patch.object(guest, "official_clis") as clis, \
|
||||
patch.object(guest, "build", return_value=root / "release-v2") as build, \
|
||||
patch.object(guest, "activate") as activate, contextlib.redirect_stdout(io.StringIO()):
|
||||
patch.object(guest, "install_updater"), patch.object(guest, "activate") as activate, contextlib.redirect_stdout(io.StringIO()):
|
||||
guest.provision(manifest, archive)
|
||||
packages.assert_called_once_with("server", [])
|
||||
configure.assert_not_called()
|
||||
@@ -307,6 +397,9 @@ class GuestTests(unittest.TestCase):
|
||||
item = tarfile.TarInfo("LICENSE")
|
||||
item.size = len(license_text)
|
||||
stream.addfile(item, io.BytesIO(license_text))
|
||||
item = tarfile.TarInfo("tools/lxc_update.py")
|
||||
item.size = 1
|
||||
stream.addfile(item, io.BytesIO(b"#"))
|
||||
home = root / "build"
|
||||
home.mkdir()
|
||||
def fake_publish(name, home, command, **kwargs):
|
||||
@@ -345,7 +438,7 @@ class GuestTests(unittest.TestCase):
|
||||
def test_config_generates_bound_profiles_and_preserves_keys_and_settings(self):
|
||||
with tempfile.TemporaryDirectory() as tmp, contextlib.ExitStack() as stack:
|
||||
root = Path(tmp)
|
||||
for name, relative in (("ETC", "etc"), ("WORKER_HOME", "worker"), ("SERVER_HOME", "server"), ("PROXY_HOME", "proxy"), ("SYSTEMD", "units")):
|
||||
for name, relative in (("ETC", "etc"), ("WORKER_HOME", "worker"), ("SERVER_HOME", "server"), ("SYSTEMD", "units")):
|
||||
stack.enter_context(patch.object(guest, name, root / relative))
|
||||
stack.enter_context(patch.object(guest, "run"))
|
||||
stack.enter_context(patch.object(guest, "user"))
|
||||
@@ -365,7 +458,7 @@ class GuestTests(unittest.TestCase):
|
||||
guest.configure(meta)
|
||||
self.assertEqual(keys, (guest.ETC / "transport-keys.json").read_bytes())
|
||||
self.assertEqual({"operator": "custom"}, json.loads((guest.ETC / "worker.json").read_text()))
|
||||
self.assertIn("tls internal", (guest.ETC / "Caddyfile").read_text())
|
||||
self.assertFalse((guest.ETC / "Caddyfile").exists())
|
||||
|
||||
def test_units_use_profile_owner_and_kill_child_processes(self):
|
||||
worker = guest.service_unit("worker")
|
||||
|
||||
Reference in New Issue
Block a user