From e2489aaa1c5fec25c8c74f44e5ec41cd7b0f9cb6 Mon Sep 17 00:00:00 2001 From: Peter van Arkel Date: Tue, 24 Nov 2026 13:43:58 +0100 Subject: [PATCH] Use the public service hostname for existing mail nodes --- deploy/panel-install.toml.example | 2 + deploy/panel_installer/cli.py | 18 +++++--- deploy/panel_installer/config.py | 12 +++-- deploy/panel_installer/engine.py | 7 +-- deploy/panel_installer/guidance.py | 3 +- deploy/panel_installer/web.py | 2 + docs/deployment.md | 4 ++ tests/test_panel_installer.py | 72 +++++++++++++++++++++++++++++- 8 files changed, 107 insertions(+), 13 deletions(-) diff --git a/deploy/panel-install.toml.example b/deploy/panel-install.toml.example index 844b156..b17a6c7 100644 --- a/deploy/panel-install.toml.example +++ b/deploy/panel-install.toml.example @@ -21,6 +21,8 @@ mode = "off" # A local mail role requires host, certificate, and key. # A remote DNS or mail role requires mode = "remote", host, and host_key. # Use port = 22 or the separate node's configured SSH port. +# For an existing mail node, mail_hostname can specify the public service +# name from its mail certificate. The host field is used for SSH. # For Let's Encrypt on the panel, use access = "acme" in [panel] and omit # certificate and key. For new local mail, add tls = "acme" to request diff --git a/deploy/panel_installer/cli.py b/deploy/panel_installer/cli.py index 35374a2..47ccfdf 100644 --- a/deploy/panel_installer/cli.py +++ b/deploy/panel_installer/cli.py @@ -138,6 +138,13 @@ def interactive_plan(previous=None): "ssh_identity", hint=guidance.SSH_IDENTITY, ) + if name == "mail" and mode == "remote": + target["mail_hostname"] = ask( + "Public mail hostname (Enter to skip)", + name, + "mail_hostname", + hint=guidance.MAIL_HOSTNAME, + ) if name == "mail" and mode in {"local", "remote-install"}: field = "host" if mode == "local" else "mail_hostname" target[field] = ask( @@ -165,10 +172,10 @@ def interactive_plan(previous=None): for name, label, hint in guidance.HOSTING_FIELDS: if name == "nameservers" and data["dns"]["mode"] == "off": continue - if name == "mail_sending_addresses" and data["mail"]["mode"] not in { - "local", - "remote-install", - }: + if name == "mail_sending_addresses" and not ( + data["mail"].get("mail_hostname") + or data["mail"]["mode"] == "local" + ): continue previous_values = defaults.get("hosting", {}).get(name, []) value = prompt( @@ -225,9 +232,10 @@ def export_parameters(plan, password_path): fields.update( ssh_user=role.ssh_user, ssh_identity=role.ssh_identity ) - if name == "mail" and role.mode in {"local", "remote-install"}: + if name == "mail" and role.mail_hostname: host_key = "host" if role.mode == "local" else "mail_hostname" fields[host_key] = role.mail_hostname + if name == "mail" and role.mode in {"local", "remote-install"}: fields["tls"] = role.tls if role.tls == "provided": fields.update(certificate=role.certificate, key=role.key) diff --git a/deploy/panel_installer/config.py b/deploy/panel_installer/config.py index 6d84303..2d826d4 100644 --- a/deploy/panel_installer/config.py +++ b/deploy/panel_installer/config.py @@ -156,10 +156,12 @@ def role(table, name): ) if mode in REMOTE_MODES: allowed = {"mode", "host", "host_key", "port"} + if name == "mail": + allowed.add("mail_hostname") if mode == "remote-install": allowed |= {"ssh_user", "ssh_identity"} if name == "mail": - allowed |= {"mail_hostname", "tls", "certificate", "key"} + allowed |= {"tls", "certificate", "key"} keys(table, allowed, name) host = table.get("host") try: @@ -179,6 +181,10 @@ def role(table, name): f"Read the pinned {name} SSH public key." ) from None options = {} + if mode == "remote" and name == "mail": + mail_host = table.get("mail_hostname", "") + if mail_host != "": + options["mail_hostname"] = hostname(mail_host, "mail") if mode == "remote-install": user = table.get("ssh_user") if not isinstance(user, str) or not SSH_USERNAME.fullmatch(user): @@ -337,8 +343,8 @@ def parse_plan(data): defaults = hosting_defaults(data.get("hosting", {})) if defaults.mail_sending_addresses and not mail.mail_hostname: raise InstallError( - "Provide a local or installer-managed mail hostname when setting " - "mail sending addresses." + "Provide the public mail hostname when setting mail sending " + "addresses." ) return Plan( host, diff --git a/deploy/panel_installer/engine.py b/deploy/panel_installer/engine.py index 952d416..a99975d 100644 --- a/deploy/panel_installer/engine.py +++ b/deploy/panel_installer/engine.py @@ -626,9 +626,10 @@ class Installer: options["COULOBRE_LOCAL_MAILBOX_PROVISIONING"] = "1" if self.plan.mail.mode in {"local", "remote-install"}: options["COULOBRE_MAIL_NODE_DKIM"] = "1" - options["COULOBRE_MAIL_IMAP_HOST"] = self.plan.mail.mail_hostname - options["COULOBRE_MAIL_SUBMISSION_HOST"] = ( - self.plan.mail.mail_hostname + if self.plan.mail.mail_hostname: + options.update( + COULOBRE_MAIL_IMAP_HOST=self.plan.mail.mail_hostname, + COULOBRE_MAIL_SUBMISSION_HOST=self.plan.mail.mail_hostname, ) if self.plan.mail.mode == "local": options.update( diff --git a/deploy/panel_installer/guidance.py b/deploy/panel_installer/guidance.py index 00cd352..80f3ad1 100644 --- a/deploy/panel_installer/guidance.py +++ b/deploy/panel_installer/guidance.py @@ -31,7 +31,8 @@ MAIL_ROLE = ( ) MAIL_HOSTNAME = ( "The public name mail clients and other mail servers use to reach " - "this mail service." + "this mail service. For an existing remote node, enter the name on " + "its mail certificate or leave it unset." ) MAIL_TLS = ( "Use acme for Let's Encrypt or provided for certificate and key files " diff --git a/deploy/panel_installer/web.py b/deploy/panel_installer/web.py index 068e30d..9b1f22d 100644 --- a/deploy/panel_installer/web.py +++ b/deploy/panel_installer/web.py @@ -166,6 +166,8 @@ def form_plan(fields): ssh_user=fields.get(f"{name}_ssh_user", ""), ssh_identity=fields.get(f"{name}_ssh_identity", ""), ) + if name == "mail" and mode == "remote": + target["mail_hostname"] = fields.get("mail_local_host", "") if name == "mail" and mode in {"local", "remote-install"}: field = "host" if mode == "local" else "mail_hostname" target[field] = fields.get("mail_local_host") or ( diff --git a/docs/deployment.md b/docs/deployment.md index aabbdef..5781c46 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -2284,6 +2284,10 @@ CNAME from **Check mail DNS**. Use the configured service hostname until public DNS resolves and the mail service presents a certificate valid for the alias. Welcome emails list the configured IMAP and submission hostnames as the defaults. +When connecting an existing remote mail node in the installer, set +`mail_hostname` to its public service name. The SSH host may be private. +An already installed panel uses the client-facing names configured in +`COULOBRE_MAIL_IMAP_HOST` and `COULOBRE_MAIL_SUBMISSION_HOST`. ### Set up signing on a Coulobre mail node diff --git a/tests/test_panel_installer.py b/tests/test_panel_installer.py index 635433f..3c29926 100644 --- a/tests/test_panel_installer.py +++ b/tests/test_panel_installer.py @@ -9,7 +9,11 @@ from pathlib import Path from types import SimpleNamespace from unittest.mock import Mock, patch -from deploy.panel_installer.cli import interactive_plan, save_interactive_plan +from deploy.panel_installer.cli import ( + export_parameters, + interactive_plan, + save_interactive_plan, +) from deploy.panel_installer.config import ( InstallError, parse_plan, @@ -229,6 +233,72 @@ class InstallerParametersTests(unittest.TestCase): self.assertEqual(plan.mail.host, "192.0.2.41") self.assertEqual(plan.mail.host_key, str(path)) self.assertEqual(plan.mail.port, 2222) + self.assertEqual(plan.mail.mail_hostname, "") + + def test_existing_mail_node_has_a_separate_public_hostname(self): + with tempfile.TemporaryDirectory() as folder: + path = Path(folder) / "receiver.pub" + path.write_text("ssh-ed25519 fixture-public-host-key\n") + web = form_plan( + PROXY + | ADMIN + | { + "origin_port": "8080", + "mail_mode": "remote", + "mail_host": "192.0.2.41", + "mail_host_key": str(path), + "mail_local_host": "mail.example.net", + "mail_sending_addresses": "93.184.215.14", + } + ) + inputs = ( + "custom", + "panel.example.net", + "acme", + ADMIN["username"], + ADMIN["email"], + "off", + "remote", + "192.0.2.41", + "", + str(path), + "mail.example.net", + "", + "93.184.215.14", + ) + with ( + patch("builtins.input", side_effect=inputs), + redirect_stdout(StringIO()), + ): + cli = interactive_plan() + for plan in (cli, web): + self.assertEqual(plan.mail.host, "192.0.2.41") + self.assertEqual(plan.mail.mail_hostname, "mail.example.net") + self.assertEqual( + parse_plan(export_parameters(plan, None)), plan + ) + with patch( + "deploy.panel_installer.engine.platform_settings", + return_value=(None, True), + ): + 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() + ) + self.assertEqual( + env["COULOBRE_MAIL_IMAP_HOST"], "mail.example.net" + ) + self.assertEqual( + env["COULOBRE_MAIL_SUBMISSION_HOST"], "mail.example.net" + ) + self.assertEqual( + env["COULOBRE_MAIL_MX_HOST"], "mail.example.net" + ) def test_remote_bootstrap_shares_a_host_between_roles(self): with tempfile.TemporaryDirectory() as folder: