web: allow direct IP dashboard and worker access (#26)
Support direct HTTP IP access on port 5187 without hostname registration or a required proxy. Preserve dashboard/machine authentication and CSRF checks. Update Proxmox setup, D14, tests and documentation for 1.2.3. Closes #25
This commit was merged in pull request #26.
This commit is contained in:
+14
-3
@@ -1,6 +1,7 @@
|
||||
"""Private guest provisioner, invoked by host.py inside the selected LXC. Not a host entry point."""
|
||||
import argparse
|
||||
import hashlib
|
||||
import ipaddress
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path, PurePosixPath
|
||||
@@ -178,7 +179,7 @@ def configure(meta, enrollment_key=None):
|
||||
"profiles": [{"id": p, "accountId": p} for p in providers]}]
|
||||
json_file(ETC / "server.json", {"accounts": accounts, "machines": machines}, once=True)
|
||||
atomic(ETC / "server.env", f"FINALTOKEN_SERVER_CONFIG={ETC}/server.json\nFINALTOKEN_SERVER_DATA={SERVER_HOME}/data\n"
|
||||
f"FINALTOKEN_DASHBOARD_KEY={keys['dashboard']}\nFINALTOKEN_PUBLIC_ORIGIN=https://{meta['domain']}\n"
|
||||
f"FINALTOKEN_DASHBOARD_KEY={keys['dashboard']}\n"
|
||||
f"ASPNETCORE_URLS=http://0.0.0.0:5187\nFINALTOKEN_MACHINE_{meta['machineId']}={keys['worker']}\n", once=True)
|
||||
atomic(ETC / "dashboard-key", keys["dashboard"] + "\n", once=True)
|
||||
atomic(ETC / "worker-key", keys["worker"] + "\n", once=True)
|
||||
@@ -315,12 +316,22 @@ def migrate_external_proxy(meta):
|
||||
return updated
|
||||
|
||||
|
||||
def dashboard_urls():
|
||||
try:
|
||||
result = run(["hostname", "-I"], capture=True, check=False)
|
||||
addresses = [ipaddress.ip_address(value) for value in result.stdout.split()] if result.returncode == 0 else []
|
||||
return [f"http://{'[' + str(ip) + ']' if ip.version == 6 else ip}:5187"
|
||||
for ip in addresses if not ip.is_loopback and not ip.is_link_local] or ["http://<container-ip>:5187"]
|
||||
except (OSError, ValueError):
|
||||
return ["http://<container-ip>:5187"]
|
||||
|
||||
|
||||
def healthy(services, meta):
|
||||
for _ in range(20):
|
||||
live = all(run(["systemctl", "is-active", "--quiet", s], check=False, capture=True).returncode == 0 for s in services)
|
||||
if live and "finaltoken-server.service" in services:
|
||||
try:
|
||||
request = urllib.request.Request("http://127.0.0.1:5187/", headers={"Host": meta["domain"]})
|
||||
request = urllib.request.Request("http://127.0.0.1:5187/")
|
||||
with urllib.request.urlopen(request, timeout=2) as response:
|
||||
live = response.status == 200
|
||||
except OSError:
|
||||
@@ -399,7 +410,7 @@ def provision(manifest, archive):
|
||||
json_file(marker, meta)
|
||||
install_updater()
|
||||
if meta["role"] != "worker":
|
||||
print("Dashboard: https://" + meta["domain"])
|
||||
print("Dashboard: " + ", ".join(dashboard_urls()))
|
||||
print("Dashboard key is root-readable at /etc/finaltoken/dashboard-key (not printed).")
|
||||
print("Official CLI login still requires your browser authorization. No provider credentials were copied.")
|
||||
|
||||
|
||||
@@ -43,8 +43,8 @@ def domain_name(value):
|
||||
|
||||
def server_url(value):
|
||||
url = urlsplit(value)
|
||||
if url.scheme != "https" or not url.hostname or url.username or url.password or url.path not in ("", "/") or url.query or url.fragment:
|
||||
raise ValueError("Remote server URL must be an HTTPS origin")
|
||||
if url.scheme not in ("http", "https") or not url.hostname or url.username is not None or url.password is not None or url.path not in ("", "/") or url.query or url.fragment:
|
||||
raise ValueError("Server URL must be an HTTP or HTTPS origin")
|
||||
_ = url.port
|
||||
return value.rstrip("/")
|
||||
|
||||
@@ -65,7 +65,7 @@ def parser():
|
||||
p.add_argument("--cores", type=int, default=2)
|
||||
p.add_argument("--disk", type=int, default=20, help="GiB")
|
||||
p.add_argument("--role", choices=("both", "server", "worker"), help="Install default: both")
|
||||
p.add_argument("--domain", help="Dashboard DNS name; required for server/both")
|
||||
p.add_argument("--domain", help="Legacy optional DNS hint; no longer used to restrict access")
|
||||
p.add_argument("--email", help="Expected provider email for both selected providers")
|
||||
p.add_argument("--claude-email")
|
||||
p.add_argument("--codex-email")
|
||||
@@ -104,9 +104,8 @@ def validate(args):
|
||||
if args.template and not TEMPLATE.fullmatch(args.template):
|
||||
raise ValueError("Template must be a Debian 13 standard amd64 template filename")
|
||||
if args.role != "worker":
|
||||
if not args.domain:
|
||||
raise ValueError("Server/both requires the public hostname configured on your existing HTTPS reverse proxy")
|
||||
args.domain = domain_name(args.domain)
|
||||
if args.domain:
|
||||
args.domain = domain_name(args.domain)
|
||||
else:
|
||||
if not args.server_url or not args.worker_key_file:
|
||||
raise ValueError("Worker-only install requires --server-url and --worker-key-file")
|
||||
@@ -221,7 +220,7 @@ def main(argv=None):
|
||||
print("Unprivileged Debian 13 amd64; build NativeAOT inside CT; preserve config, keys, history and CLI profiles.")
|
||||
if args.action == "install":
|
||||
print(f"Root storage={args.storage}; template storage={args.template_storage}; bridge={args.bridge}; IP={args.ip}")
|
||||
print(f"Dashboard={('https://' + args.domain) if args.domain else args.server_url}; backend=HTTP :5187 behind your existing reverse proxy")
|
||||
print("Server=" + args.server_url if role == "worker" else "Dashboard=http://<container-ip>:5187; no hostname or proxy required")
|
||||
return 0
|
||||
execute(args)
|
||||
return 0
|
||||
|
||||
@@ -33,6 +33,14 @@ def install_args(*extra):
|
||||
|
||||
|
||||
class HostTests(unittest.TestCase):
|
||||
def test_server_install_requires_no_hostname(self):
|
||||
args = host.parser().parse_args(["install", "--role", "server", "--ctid", "201", "--storage", "local-lvm"])
|
||||
host.validate(args)
|
||||
self.assertIsNone(args.domain)
|
||||
|
||||
def test_worker_can_use_a_direct_http_ip_address(self):
|
||||
self.assertEqual("http://10.0.30.150:5187", host.server_url("http://10.0.30.150:5187/"))
|
||||
|
||||
def test_server_only_requires_no_provider_login_or_email(self):
|
||||
args = host.parser().parse_args(["install", "--role", "server", "--ctid", "201",
|
||||
"--storage", "local-lvm", "--domain", "usage.example.test"])
|
||||
@@ -61,14 +69,14 @@ class HostTests(unittest.TestCase):
|
||||
with self.subTest(flags=flags), self.assertRaises(ValueError):
|
||||
install_args(*flags)
|
||||
|
||||
def test_worker_only_requires_https_and_enrollment_file(self):
|
||||
def test_worker_only_requires_http_origin_and_enrollment_file(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
key = Path(tmp) / "enrollment"
|
||||
key.write_text("a" * 64)
|
||||
args = install_args("--role", "worker", "--server-url", "https://usage.example.test", "--worker-key-file", str(key))
|
||||
self.assertEqual("worker", args.role)
|
||||
with self.assertRaises(ValueError):
|
||||
install_args("--role", "worker", "--server-url", "http://usage.example.test", "--worker-key-file", str(key))
|
||||
install_args("--role", "worker", "--server-url", "ftp://usage.example.test", "--worker-key-file", str(key))
|
||||
key.write_text("short")
|
||||
with self.assertRaises(ValueError):
|
||||
install_args("--role", "worker", "--server-url", "https://usage.example.test", "--worker-key-file", str(key))
|
||||
@@ -178,6 +186,22 @@ class HostTests(unittest.TestCase):
|
||||
|
||||
|
||||
class GuestTests(unittest.TestCase):
|
||||
def test_dashboard_urls_use_container_ips_and_bracket_ipv6(self):
|
||||
with patch.object(guest, "run", return_value=subprocess.CompletedProcess([], 0, "10.0.30.150 2001:db8::5 fe80::1 127.0.0.1")):
|
||||
self.assertEqual(["http://10.0.30.150:5187", "http://[2001:db8::5]:5187"], guest.dashboard_urls())
|
||||
|
||||
def test_missing_address_command_does_not_fail_an_update(self):
|
||||
with patch.object(guest, "run", side_effect=FileNotFoundError):
|
||||
self.assertEqual(["http://<container-ip>:5187"], guest.dashboard_urls())
|
||||
|
||||
def test_health_check_uses_no_configured_hostname(self):
|
||||
with patch.object(guest, "run", return_value=subprocess.CompletedProcess([], 0)), patch.object(guest.time, "sleep"), \
|
||||
patch.object(guest.urllib.request, "urlopen") as opener:
|
||||
opener.return_value.__enter__.return_value.status = 200
|
||||
self.assertTrue(guest.healthy(["finaltoken-server.service"], {"role": "server"}))
|
||||
self.assertEqual("http://127.0.0.1:5187/", opener.call_args.args[0].full_url)
|
||||
self.assertFalse(opener.call_args.args[0].has_header("Host"))
|
||||
|
||||
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", [])
|
||||
@@ -194,10 +218,10 @@ class GuestTests(unittest.TestCase):
|
||||
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"})
|
||||
"machineId": "lxc_201", "timezone": "UTC"})
|
||||
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.assertNotIn("FINALTOKEN_PUBLIC_ORIGIN", 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])
|
||||
@@ -405,7 +429,8 @@ 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, "install_updater"), patch.object(guest, "activate") as activate, contextlib.redirect_stdout(io.StringIO()):
|
||||
patch.object(guest, "install_updater"), patch.object(guest, "dashboard_urls", return_value=["http://10.0.30.150:5187"]), \
|
||||
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()
|
||||
|
||||
Reference in New Issue
Block a user