Files
homegate/tests/test_homegate.py
DizelBadCoder b305cc1ba8
All checks were successful
Test and publish Homegate / publish (push) Successful in 12m49s
Test and publish Homegate / checks (push) Successful in 29s
Support passwordless Pi-hole authentication and UI settings
2026-10-14 18:12:10 +03:00

328 lines
14 KiB
Python

"""Behavior tests using real X.509/TLS and a simulated NPM/Pi-hole HTTP transport."""
import copy
from datetime import timedelta
from email.parser import BytesParser
from email.policy import default
import json
from pathlib import Path
import ssl
import tempfile
import unittest
from unittest.mock import patch
from urllib.parse import unquote, urlsplit
import requests
import yaml
from cryptography import x509
import homegate as h
BASE = Path(__file__).resolve().parents[1]
class Transport:
def __init__(self):
self.hosts = []
self.certs = []
self.records = ["192.168.0.9 unrelated.home"]
self.cnames = []
self.writes = []
self.fail_upload = False
self.fail_nginx = False
self.fail_dns = False
self.uploads = {}
self.pihole_session = {"valid": True, "sid": "TEST_SID", "csrf": "TEST_CSRF"}
self.requests = []
def send(self, session, request, **kwargs):
self.requests.append(request)
path = unquote(urlsplit(request.url).path.removeprefix("/api"))
method = request.method
data = json.loads(request.body) if request.body and "application/json" in request.headers.get("Content-Type", "") else None
body, status = None, 200
if method != "GET":
self.writes.append((method, path))
if path == "/tokens":
body = {"token": "TEST_TOKEN"}
elif path == "/auth":
if method == "POST":
body = {"session": self.pihole_session}
else:
status = 204
elif path.startswith("/nginx/"):
assert request.headers["Authorization"] == "Bearer TEST_TOKEN"
if path == "/nginx/proxy-hosts":
if method == "GET":
body = self.hosts
else:
assert self.uploads[data["certificate_id"]]
body = dict(data, id=len(self.hosts) + 1, meta={"nginx_online": not self.fail_nginx})
self.hosts.append(body)
elif path.startswith("/nginx/proxy-hosts/"):
index = int(path.split("/")[-1]) - 1
body = dict(data, id=index + 1, meta={"nginx_online": not self.fail_nginx})
self.hosts[index] = body
elif path == "/nginx/certificates":
if method == "GET":
body = self.certs
else:
body = dict(data, id=len(self.certs) + 1, meta={})
self.certs.append(body)
elif path.endswith("/upload"):
cert_id = int(path.split("/")[-2])
if self.fail_upload:
status = 500
else:
raw = ("Content-Type: " + request.headers["Content-Type"] + "\r\n\r\n").encode() + request.body
message = BytesParser(policy=default).parsebytes(raw)
fields = {p.get_param("name", header="content-disposition"): p.get_payload(decode=True)
for p in message.iter_parts()}
assert set(fields) == {"certificate", "certificate_key"}
x509.load_pem_x509_certificate(fields["certificate"])
self.uploads[cert_id] = fields
self.certs[cert_id - 1]["meta"] = {"certificate": True, "certificate_key": True}
body = {"certificate_key": "SECRET_NOT_TO_LOG"}
else:
raise AssertionError(path)
elif path == "/config/dns":
assert request.headers.get("X-FTL-SID") == self.pihole_session["sid"]
if self.fail_dns:
status = 503
else:
body = {"config": {"dns": {"hosts": self.records, "cnameRecords": self.cnames}}}
elif path.startswith("/config/dns/hosts/"):
assert method == "PUT"
assert request.headers.get("X-FTL-SID") == self.pihole_session["sid"]
entry = path.removeprefix("/config/dns/hosts/")
assert entry not in self.records
assert self.hosts[0]["meta"]["nginx_online"]
self.records.append(entry)
status = 201
else:
raise AssertionError((method, path))
response = requests.Response()
response.status_code = status
response._content = json.dumps(body).encode() if body is not None else b""
return response
class HomegateTests(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
self.state = Path(self.tmp.name)
self.ca = h.Authority(self.state)
self.config = h.load_config(BASE / "config/services.yaml")
self.registry = {"instance": "test-instance", "routes": {}}
self.transport = Transport()
transport = self.transport
self.patch = patch.object(requests.Session, "send", lambda session, request, **kw: transport.send(session, request, **kw))
self.patch.start()
self.addCleanup(self.patch.stop)
self.npm = h.NPM("http://npm:81", "NPM")
self.npm.login("test@example.com", "password")
self.dns = h.PiHole("http://pihole", "PIHOLE")
self.dns.login("password")
self.addCleanup(self.npm.close)
self.addCleanup(self.dns.close)
self.transport.writes.clear()
def sync(self):
h.reconcile(self.config, self.state, self.ca, self.registry, self.npm, self.dns)
def test_first_sync_and_idempotent_second_sync(self):
self.sync()
self.assertIn("192.168.0.10 pve.home", self.transport.records)
self.assertIn("192.168.0.9 unrelated.home", self.transport.records)
host = self.transport.hosts[0]
self.assertTrue(host["ssl_forced"])
self.assertTrue(host["allow_websocket_upgrade"])
self.assertIn("allow 192.168.0.0/24;\ndeny all;", host["advanced_config"])
self.assertIn("proxy_ssl_verify off;", host["advanced_config"])
self.transport.writes.clear()
self.sync()
self.assertEqual([], self.transport.writes)
def test_passwordless_pihole_publishes_dns_without_auth_headers_or_logout(self):
self.transport.pihole_session = {"valid": True, "sid": None, "validity": -1,
"message": "no password set"}
self.transport.requests.clear()
self.dns.session.cookies.set("sid", "OLD_SESSION")
self.dns.login("")
self.sync()
self.assertIn("192.168.0.10 pve.home", self.transport.records)
self.assertIn("192.168.0.9 unrelated.home", self.transport.records)
self.transport.writes.clear()
self.sync()
self.dns.close()
self.assertEqual([], self.transport.writes)
for request in self.transport.requests:
if urlsplit(request.url).hostname == "pihole":
self.assertNotIn("X-FTL-SID", request.headers)
self.assertNotIn("X-FTL-CSRF", request.headers)
self.assertNotIn("Cookie", request.headers)
def test_invalid_pihole_session_does_not_enable_passwordless_access(self):
for session in ({"valid": False, "sid": None, "validity": -1},
{"valid": False, "sid": "invalid"}, {"valid": True},
{"valid": True, "sid": None, "validity": 300}):
with self.subTest(session=session), patch.object(self.dns, "call", return_value={"session": session}):
with self.assertRaisesRegex(h.Error, "Pi-hole authentication failed"):
self.dns.login("wrong-password")
self.assertNotIn("X-FTL-SID", self.dns.session.headers)
self.assertEqual([], self.transport.writes)
def test_renewal_uploads_and_reloads_existing_host(self):
self.sync()
old = self.transport.uploads[1]["certificate"]
future = h.now() + timedelta(days=61)
self.transport.writes.clear()
with patch.object(h, "now", return_value=future):
self.sync()
self.assertNotEqual(old, self.transport.uploads[1]["certificate"])
self.assertEqual([("POST", "/nginx/certificates/1/upload"), ("PUT", "/nginx/proxy-hosts/1")], self.transport.writes)
self.assertEqual(1, len(self.transport.certs))
def test_failed_upload_retries_without_duplicate_certificate(self):
self.transport.fail_upload = True
with self.assertRaises(h.Error):
self.sync()
self.assertEqual({}, self.registry["routes"])
self.assertEqual([], self.transport.hosts)
self.transport.fail_upload = False
self.sync()
self.assertEqual(1, len(self.transport.certs))
def test_failed_reload_is_retried_and_does_not_publish_dns(self):
self.transport.fail_nginx = True
with self.assertRaises(h.Error):
self.sync()
self.assertNotIn("192.168.0.10 pve.home", self.transport.records)
self.assertEqual({}, self.registry["routes"])
self.transport.fail_nginx = False
self.sync()
self.assertIn("192.168.0.10 pve.home", self.transport.records)
def test_unmanaged_proxy_is_not_overwritten(self):
self.transport.hosts.append({"id": 1, "domain_names": ["pve.home"], "advanced_config": "# existing"})
with self.assertRaises(h.Error):
self.sync()
self.assertEqual([], self.transport.writes)
def test_conflicting_dns_is_not_overwritten(self):
self.transport.records.append("192.168.0.137 pve.home alias.home")
with self.assertRaises(h.Error):
self.sync()
self.assertEqual([], self.transport.writes)
def test_cname_conflict_is_not_overwritten(self):
self.transport.cnames.append("pve.home,other.home")
with self.assertRaises(h.Error):
self.sync()
self.assertEqual([], self.transport.writes)
def test_dns_outage_does_not_stop_certificate_renewal(self):
self.sync()
self.transport.fail_dns = True
future = h.now() + timedelta(days=61)
with patch.object(h, "now", return_value=future), self.assertRaises(h.Error):
self.sync()
self.assertEqual(1, len(self.transport.certs))
self.assertIn(("PUT", "/nginx/proxy-hosts/1"), self.transport.writes)
def test_removed_service_is_not_deleted(self):
self.sync()
self.config["services"] = []
self.transport.writes.clear()
self.sync()
self.assertEqual([], self.transport.writes)
self.assertEqual(1, len(self.transport.hosts))
def test_disabled_service_disables_proxy(self):
self.sync()
self.config["services"][0]["enabled"] = False
self.sync()
self.assertFalse(self.transport.hosts[0]["enabled"])
def test_invalid_input_cannot_inject_nginx_or_expand_access(self):
base = yaml.safe_load((BASE / "config/services.yaml").read_text())
for field, value in [("hostname", "pve.home;return 200;"), ("upstream", "http://8.8.8.8:80"),
("upstream", "https://192.168.0.137:8006/path"), ("websocket", "false"),
("upstream_ca_file", "/tmp/ca;return 200;"), ("upstream_tls_name", "pve;#")]:
c = copy.deepcopy(base)
c["services"][0][field] = value
p = self.state / "invalid.yaml"
p.write_text(yaml.safe_dump(c))
with self.subTest(field=field, value=value), self.assertRaises((h.Error, ValueError)):
h.load_config(p)
base["allowed_networks"] = ["0.0.0.0/0"]
p.write_text(yaml.safe_dump(base))
with self.assertRaises(h.Error):
h.load_config(p)
def test_ca_survives_restart_and_private_files_are_restricted(self):
original = (self.state / "root-ca.crt").read_bytes()
h.Authority(self.state)
self.assertEqual(original, (self.state / "root-ca.crt").read_bytes())
self.assertEqual(0o600, (self.state / "ca.json").stat().st_mode & 0o777)
with self.assertRaises(h.Error):
self.ca.leaf("example.com")
def test_missing_ca_does_not_silently_replace_trust(self):
(self.state / "registry.json").write_text("{}")
(self.state / "ca.json").unlink()
with self.assertRaises(h.Error):
h.Authority(self.state)
def tls_handshake(self, hostname, trust=True):
bundle = self.ca.leaf("pve.home")
certpath, keypath = self.state / "tls.pem", self.state / "tls.key"
certpath.write_text(bundle["cert"])
keypath.write_text(bundle["key"])
server_ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
server_ctx.load_cert_chain(certpath, keypath)
client_ctx = ssl.create_default_context(cafile=str(self.state / "root-ca.crt") if trust else None)
ci, co, si, so = ssl.MemoryBIO(), ssl.MemoryBIO(), ssl.MemoryBIO(), ssl.MemoryBIO()
client = client_ctx.wrap_bio(ci, co, server_hostname=hostname)
server = server_ctx.wrap_bio(si, so, server_side=True)
finished = set()
for _ in range(20):
for name, obj in (("client", client), ("server", server)):
try:
obj.do_handshake()
finished.add(name)
except ssl.SSLWantReadError:
pass
if co.pending:
si.write(co.read())
if so.pending:
ci.write(so.read())
if len(finished) == 2:
return client.getpeercert()
self.fail("TLS handshake did not complete")
def test_real_tls_accepts_correct_hostname_and_trusted_ca(self):
cert = self.tls_handshake("pve.home")
self.assertIn(("DNS", "pve.home"), cert["subjectAltName"])
def test_real_tls_rejects_wrong_hostname(self):
with self.assertRaises(ssl.SSLCertVerificationError):
self.tls_handshake("wrong.home")
def test_real_tls_rejects_untrusted_ca(self):
with self.assertRaises(ssl.SSLCertVerificationError):
self.tls_handshake("pve.home", trust=False)
def test_upstream_verification_is_default_for_other_https_services(self):
c = yaml.safe_load((BASE / "config/services.yaml").read_text())
c["services"] = [{"hostname": "nas.home.arpa", "upstream": "https://192.168.0.130"}]
p = self.state / "config.yaml"
p.write_text(yaml.safe_dump(c))
config = h.load_config(p)
result = h.payload(config, config["services"][0], 1, "# test")
self.assertIn("proxy_ssl_verify on;", result["advanced_config"])
if __name__ == "__main__":
unittest.main()