2026-11-22 22:27:08 +01:00
|
|
|
import json
|
2026-11-22 23:04:17 +01:00
|
|
|
import shlex
|
2026-11-22 16:39:27 +01:00
|
|
|
import stat
|
|
|
|
|
import tempfile
|
|
|
|
|
import unittest
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from types import SimpleNamespace
|
|
|
|
|
from unittest.mock import Mock, patch
|
|
|
|
|
|
2026-11-22 21:27:29 +01:00
|
|
|
from deploy.panel_installer.cli import save_interactive_plan
|
2026-11-22 16:39:27 +01:00
|
|
|
from deploy.panel_installer.config import (
|
|
|
|
|
InstallError,
|
|
|
|
|
parse_plan,
|
|
|
|
|
read_password,
|
|
|
|
|
read_plan,
|
|
|
|
|
)
|
2026-11-23 23:41:31 +01:00
|
|
|
from deploy.panel_installer.engine import Installer, install_worker
|
2026-11-22 16:39:27 +01:00
|
|
|
from deploy.panel_installer.web import Wizard, WizardHandler, form_plan
|
|
|
|
|
|
|
|
|
|
PROXY = {
|
|
|
|
|
"hostname": "Panel.Example.NET",
|
|
|
|
|
"access": "proxy",
|
|
|
|
|
"origin_ip": "127.0.0.20",
|
|
|
|
|
"proxy_ip": "127.0.0.10",
|
|
|
|
|
}
|
|
|
|
|
ADMIN = {"username": "operator", "email": "ops@example.net"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class InstallerParametersTests(unittest.TestCase):
|
2026-11-23 23:41:31 +01:00
|
|
|
def test_worker_can_join_its_existing_private_group(self):
|
|
|
|
|
name = "coulobre-operator"
|
|
|
|
|
service = f"{name}-worker.service"
|
|
|
|
|
account = SimpleNamespace(
|
|
|
|
|
pw_gid=983,
|
|
|
|
|
pw_uid=984,
|
|
|
|
|
pw_dir="/nonexistent",
|
|
|
|
|
pw_shell="/usr/sbin/nologin",
|
|
|
|
|
)
|
|
|
|
|
with (
|
|
|
|
|
patch(
|
|
|
|
|
"deploy.panel_installer.engine.pwd.getpwnam",
|
|
|
|
|
side_effect=[KeyError, account],
|
|
|
|
|
),
|
|
|
|
|
patch(
|
|
|
|
|
"deploy.panel_installer.engine.grp.getgrnam",
|
|
|
|
|
return_value=SimpleNamespace(gr_gid=983),
|
|
|
|
|
),
|
|
|
|
|
patch(
|
|
|
|
|
"deploy.panel_installer.engine.os.getgrouplist",
|
|
|
|
|
return_value=[983],
|
|
|
|
|
),
|
|
|
|
|
patch("deploy.panel_installer.engine.run") as run,
|
|
|
|
|
patch("deploy.panel_installer.engine.install_unit"),
|
|
|
|
|
):
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
install_worker(name, name, f"{name}-apply", service),
|
|
|
|
|
service,
|
|
|
|
|
)
|
|
|
|
|
run.assert_called_once_with(
|
|
|
|
|
"useradd",
|
|
|
|
|
"--system",
|
|
|
|
|
"--gid",
|
|
|
|
|
"983",
|
|
|
|
|
"--no-create-home",
|
|
|
|
|
"--home-dir",
|
|
|
|
|
"/nonexistent",
|
|
|
|
|
"--shell",
|
|
|
|
|
"/usr/sbin/nologin",
|
|
|
|
|
name,
|
|
|
|
|
label=f"Create service user {name}",
|
|
|
|
|
)
|
|
|
|
|
|
2026-11-22 22:27:08 +01:00
|
|
|
def test_default_health_targets_follow_locally_installed_roles(self):
|
|
|
|
|
cases = (
|
|
|
|
|
({}, None, []),
|
|
|
|
|
(
|
|
|
|
|
{
|
|
|
|
|
"dns": {"mode": "local"},
|
|
|
|
|
"mail": {
|
|
|
|
|
"mode": "local",
|
|
|
|
|
"host": "panel.example.net",
|
|
|
|
|
"tls": "acme",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
"8.5",
|
|
|
|
|
["php-fpm", "bind9", "postfix", "dovecot", "rspamd", "redis"],
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
for roles, php, extras in cases:
|
|
|
|
|
with (
|
|
|
|
|
self.subTest(roles=roles),
|
|
|
|
|
patch(
|
|
|
|
|
"deploy.panel_installer.engine.platform_settings",
|
|
|
|
|
return_value=(php, True),
|
|
|
|
|
),
|
|
|
|
|
):
|
|
|
|
|
plan = parse_plan({"panel": PROXY, "admin": ADMIN, **roles})
|
|
|
|
|
installer = Installer(plan, "test-only", Mock())
|
|
|
|
|
installer.state = dict.fromkeys(
|
|
|
|
|
("secret_key", "database_password", "broker_password"),
|
|
|
|
|
"test-only",
|
|
|
|
|
)
|
|
|
|
|
env = dict(
|
|
|
|
|
line.split("=", 1)
|
|
|
|
|
for line in installer.environment().splitlines()
|
|
|
|
|
)
|
|
|
|
|
base = [
|
|
|
|
|
"panel",
|
|
|
|
|
"worker",
|
|
|
|
|
"scheduler",
|
|
|
|
|
"apache",
|
|
|
|
|
"mariadb",
|
|
|
|
|
"rabbitmq",
|
|
|
|
|
]
|
|
|
|
|
self.assertEqual(
|
2026-11-22 23:04:17 +01:00
|
|
|
json.loads(shlex.split(env["COULOBRE_SERVICE_HEALTH"])[0]),
|
2026-11-22 22:27:08 +01:00
|
|
|
{"panel": {"local": base + extras}},
|
|
|
|
|
)
|
|
|
|
|
|
2026-11-22 21:27:29 +01:00
|
|
|
def test_interactive_export_reuses_the_same_plan(self):
|
|
|
|
|
plan = parse_plan(
|
|
|
|
|
{
|
|
|
|
|
"panel": {"hostname": "panel.example.net", "access": "acme"},
|
|
|
|
|
"admin": ADMIN,
|
|
|
|
|
"dns": {"mode": "local"},
|
|
|
|
|
"mail": {
|
|
|
|
|
"mode": "local",
|
|
|
|
|
"host": "panel.example.net",
|
|
|
|
|
"tls": "acme",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
|
|
|
path = Path(folder) / "panel.toml"
|
|
|
|
|
with patch(
|
|
|
|
|
"deploy.panel_installer.cli.prompt", return_value=str(path)
|
|
|
|
|
):
|
|
|
|
|
save_interactive_plan(plan, None)
|
|
|
|
|
self.assertEqual(stat.S_IMODE(path.stat().st_mode), 0o600)
|
|
|
|
|
self.assertEqual(parse_plan(read_plan(path)), plan)
|
|
|
|
|
|
2026-11-22 16:39:27 +01:00
|
|
|
def test_browser_and_parameter_file_share_the_same_plan(self):
|
|
|
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
|
|
|
params = Path(folder) / "panel.toml"
|
|
|
|
|
params.write_text(
|
|
|
|
|
'[panel]\nhostname = "panel.example.net"\naccess = "proxy"\n'
|
|
|
|
|
'origin_ip = "127.0.0.20"\nproxy_ip = "127.0.0.10"\n'
|
|
|
|
|
'[admin]\nemail = "ops@example.net"\n[dns]\nmode = "local"\n'
|
|
|
|
|
)
|
|
|
|
|
file_plan = parse_plan(read_plan(params))
|
|
|
|
|
browser_plan = form_plan(
|
|
|
|
|
PROXY | ADMIN | {"origin_port": "8080", "dns_mode": "local"}
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(browser_plan, file_plan)
|
|
|
|
|
self.assertEqual(file_plan.origin_port, 8080)
|
|
|
|
|
self.assertEqual(file_plan.mail.mode, "off")
|
|
|
|
|
|
|
|
|
|
def test_rejects_wrong_mode_fields_and_untrusted_network_values(self):
|
|
|
|
|
for field, value in (
|
|
|
|
|
("hostname", "panel.example.net\nInclude /tmp/file"),
|
|
|
|
|
("proxy_ip", "8.8.8.8"),
|
|
|
|
|
("origin_port", True),
|
|
|
|
|
("origin_port", 443),
|
|
|
|
|
("certificate", "/etc/ssl/cert.pem"),
|
|
|
|
|
):
|
|
|
|
|
with self.subTest(field=field), self.assertRaises(InstallError):
|
|
|
|
|
parse_plan({"panel": PROXY | {field: value}, "admin": ADMIN})
|
|
|
|
|
with self.assertRaises(InstallError):
|
|
|
|
|
parse_plan(
|
|
|
|
|
{"panel": PROXY, "admin": ADMIN | {"password": "secret"}}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_remote_roles_use_pinned_public_key_files(self):
|
|
|
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
|
|
|
path = Path(folder) / "receiver.pub"
|
|
|
|
|
path.write_text("ssh-ed25519 fixture-public-host-key\n")
|
|
|
|
|
plan = parse_plan(
|
|
|
|
|
{
|
|
|
|
|
"panel": PROXY,
|
|
|
|
|
"admin": ADMIN,
|
|
|
|
|
"mail": {
|
|
|
|
|
"mode": "remote",
|
|
|
|
|
"host": "192.0.2.41",
|
|
|
|
|
"port": 2222,
|
|
|
|
|
"host_key": str(path),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(plan.mail.host, "192.0.2.41")
|
|
|
|
|
self.assertEqual(plan.mail.host_key, str(path))
|
|
|
|
|
self.assertEqual(plan.mail.port, 2222)
|
|
|
|
|
|
2026-11-22 17:51:05 +01:00
|
|
|
def test_remote_bootstrap_shares_a_host_between_roles(self):
|
|
|
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
|
|
|
key = Path(folder) / "host.pub"
|
|
|
|
|
key.write_text("ssh-ed25519 fixture-key\n")
|
|
|
|
|
connection = {
|
|
|
|
|
"mode": "remote-install",
|
|
|
|
|
"host": "192.0.2.41",
|
|
|
|
|
"host_key": str(key),
|
|
|
|
|
"ssh_user": "operator",
|
|
|
|
|
"ssh_identity": "/root/node-install-key",
|
|
|
|
|
}
|
|
|
|
|
plan = parse_plan(
|
|
|
|
|
{
|
|
|
|
|
"panel": {
|
|
|
|
|
"hostname": "panel.example.net",
|
|
|
|
|
"access": "acme",
|
|
|
|
|
},
|
|
|
|
|
"admin": ADMIN,
|
|
|
|
|
"dns": connection,
|
|
|
|
|
"mail": connection
|
|
|
|
|
| {
|
|
|
|
|
"mail_hostname": "mx.example.net",
|
|
|
|
|
"tls": "acme",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(plan.dns.host, plan.mail.host)
|
|
|
|
|
self.assertEqual(plan.dns.ssh_user, "operator")
|
|
|
|
|
self.assertEqual(plan.mail.host, "192.0.2.41")
|
|
|
|
|
self.assertEqual(plan.mail.mail_hostname, "mx.example.net")
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
plan.mail.key,
|
|
|
|
|
"/etc/letsencrypt/live/mx.example.net/privkey.pem",
|
|
|
|
|
)
|
|
|
|
|
self.assertIn("192.0.2.41", plan.description())
|
|
|
|
|
self.assertNotIn("/root/node-install-key", plan.description())
|
|
|
|
|
|
2026-11-22 16:39:27 +01:00
|
|
|
def test_password_file_must_be_private_and_regular(self):
|
|
|
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
|
|
|
path = Path(folder) / "password"
|
|
|
|
|
path.write_text("fixture-password-only\n")
|
|
|
|
|
private = SimpleNamespace(
|
|
|
|
|
st_mode=stat.S_IFREG | 0o600, st_uid=0, st_size=22
|
|
|
|
|
)
|
|
|
|
|
with patch(
|
|
|
|
|
"deploy.panel_installer.config.os.fstat", return_value=private
|
|
|
|
|
):
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
read_password(str(path)), "fixture-password-only"
|
|
|
|
|
)
|
|
|
|
|
for mode in (0o644, 0o640):
|
|
|
|
|
with (
|
|
|
|
|
patch(
|
|
|
|
|
"deploy.panel_installer.config.os.fstat",
|
|
|
|
|
return_value=SimpleNamespace(
|
|
|
|
|
st_mode=stat.S_IFREG | mode,
|
|
|
|
|
st_uid=0,
|
|
|
|
|
st_size=22,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
self.assertRaises(InstallError),
|
|
|
|
|
):
|
|
|
|
|
read_password(str(path))
|
|
|
|
|
indirect = Path(folder) / "password-link"
|
|
|
|
|
indirect.symlink_to(path)
|
|
|
|
|
with self.assertRaises(InstallError):
|
|
|
|
|
read_password(str(indirect))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SetupWizardTests(unittest.TestCase):
|
|
|
|
|
def handler(self):
|
|
|
|
|
handler = WizardHandler.__new__(WizardHandler)
|
|
|
|
|
handler.server = SimpleNamespace(
|
|
|
|
|
authority="127.0.0.1:8765",
|
|
|
|
|
wizard=Wizard("fixture-private-password"),
|
|
|
|
|
)
|
|
|
|
|
handler.headers = {
|
|
|
|
|
"Host": "127.0.0.1:8765",
|
|
|
|
|
"Origin": "http://127.0.0.1:8765",
|
|
|
|
|
}
|
|
|
|
|
handler.path = "/authorize"
|
|
|
|
|
handler.fields = Mock(
|
|
|
|
|
return_value={"code": handler.server.wizard.code}
|
|
|
|
|
)
|
|
|
|
|
handler.page = Mock()
|
|
|
|
|
handler.redirect = Mock()
|
|
|
|
|
return handler
|
|
|
|
|
|
|
|
|
|
def test_checks_origin_and_uses_a_single_use_setup_code(self):
|
|
|
|
|
handler = self.handler()
|
|
|
|
|
handler.headers["Origin"] = "https://other.example.net"
|
|
|
|
|
handler.do_POST()
|
|
|
|
|
self.assertEqual(handler.page.call_args.kwargs["status"], 403)
|
|
|
|
|
handler.fields.assert_not_called()
|
|
|
|
|
handler.headers["Origin"] = "http://127.0.0.1:8765"
|
|
|
|
|
handler.do_POST()
|
|
|
|
|
state = handler.server.wizard
|
|
|
|
|
self.assertEqual(state.code, "")
|
|
|
|
|
self.assertIn(
|
|
|
|
|
"HttpOnly; SameSite=Strict",
|
|
|
|
|
handler.redirect.call_args.kwargs["cookie"],
|
|
|
|
|
)
|
|
|
|
|
handler.do_POST()
|
|
|
|
|
self.assertEqual(handler.page.call_args.kwargs["status"], 403)
|
|
|
|
|
handler.redirect.assert_called_once()
|
|
|
|
|
|
|
|
|
|
def test_retry_form_requires_authorization_and_a_reviewed_plan(self):
|
|
|
|
|
handler = self.handler()
|
|
|
|
|
handler.path = "/install"
|
|
|
|
|
handler.do_POST()
|
|
|
|
|
self.assertEqual(handler.page.call_args.kwargs["status"], 403)
|
|
|
|
|
handler.server.wizard.session = "authenticated-session"
|
|
|
|
|
handler.headers["Cookie"] = "coulobre_installer=authenticated-session"
|
|
|
|
|
handler.do_POST()
|
|
|
|
|
self.assertEqual(handler.page.call_args.kwargs["status"], 400)
|
|
|
|
|
self.assertIsNone(handler.server.wizard.thread)
|