155 lines
5.8 KiB
Python
155 lines
5.8 KiB
Python
import stat
|
|||
|
|
import tempfile
|
||
|
|
import unittest
|
||
|
|
from pathlib import Path
|
||
|
|
from types import SimpleNamespace
|
||
|
|
from unittest.mock import Mock, patch
|
||
|
|
|
||
|
|
from deploy.panel_installer.config import (
|
||
|
|
InstallError,
|
||
|
|
parse_plan,
|
||
|
|
read_password,
|
||
|
|
read_plan,
|
||
|
|
)
|
||
|
|
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):
|
||
|
|
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)
|
||
|
|
|
||
|
|
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)
|