Complete public hosting operations for Coulobre 0.9

This commit is contained in:
parkel committed 2026-11-22 17:51:05 +01:00
1 parent a033d85d1f
commit 9e66510f7c
41 files changed
+2042 -44

No files matched your search

+77 -8
View File
@@ -16,7 +16,9 @@ PLATFORMS = {
DNS_LABEL = re.compile(r"[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?")
FILE_PATH = re.compile(r"/[a-zA-Z0-9_./+-]+")
USERNAME = re.compile(r"[a-zA-Z0-9@.+_-]{1,150}")
SSH_USERNAME = re.compile(r"[a-z_][a-z0-9_-]{0,31}")
EMAIL = re.compile(r"[^\s@]+@[^\s@]+\.[^\s@]+")
REMOTE_MODES = {"remote", "remote-install"}
class InstallError(Exception):
@@ -103,6 +105,22 @@ def certificate(table, label):
return chain, key
def tls_files(table, host, label, *, remote=False):
if table.get("tls", "provided") == "acme":
if "certificate" in table or "key" in table:
raise InstallError(f"Choose one certificate source for {label}.")
directory = Path("/etc/letsencrypt/live") / host
return str(directory / "fullchain.pem"), str(directory / "privkey.pem")
if table.get("tls", "provided") != "provided":
raise InstallError(f"Choose provided or acme TLS for {label}.")
if remote:
return (
file_path(table.get("certificate"), f"the {label} certificate"),
file_path(table.get("key"), f"the {label} certificate key"),
)
return certificate(table, label)
@dataclass(frozen=True)
class Role:
mode: str
@@ -111,6 +129,10 @@ class Role:
host_key: str = ""
certificate: str = ""
key: str = ""
mail_hostname: str = ""
tls: str = "provided"
ssh_user: str = ""
ssh_identity: str = ""
def role(table, name):
@@ -121,12 +143,24 @@ def role(table, name):
keys(table, {"mode"}, name)
return Role(mode)
if mode == "local":
keys(table, {"mode", "host", "certificate", "key"}, name)
keys(table, {"mode", "host", "certificate", "key", "tls"}, name)
host = hostname(table.get("host"), "mail")
chain, key = certificate(table, "mail")
return Role(mode, host=host, certificate=chain, key=key)
if mode == "remote":
keys(table, {"mode", "host", "host_key", "port"}, name)
chain, key = tls_files(table, host, "mail")
return Role(
mode,
host=host,
certificate=chain,
key=key,
mail_hostname=host,
tls=table.get("tls", "provided"),
)
if mode in REMOTE_MODES:
allowed = {"mode", "host", "host_key", "port"}
if mode == "remote-install":
allowed |= {"ssh_user", "ssh_identity"}
if name == "mail":
allowed |= {"mail_hostname", "tls", "certificate", "key"}
keys(table, allowed, name)
host = table.get("host")
try:
normalized = address(host, f"the {name} node")
@@ -144,13 +178,40 @@ def role(table, name):
raise InstallError(
f"Read the pinned {name} SSH public key."
) from None
options = {}
if mode == "remote-install":
user = table.get("ssh_user")
if not isinstance(user, str) or not SSH_USERNAME.fullmatch(user):
raise InstallError(
f"Enter a valid SSH administrator for {name}."
)
options = {
"ssh_user": user,
"ssh_identity": file_path(
table.get("ssh_identity"), f"the {name} SSH private key"
),
}
if name == "mail":
mail_host = hostname(table.get("mail_hostname"), "mail")
chain, private_key = tls_files(
table, mail_host, "mail", remote=True
)
options.update(
mail_hostname=mail_host,
tls=table.get("tls", "provided"),
certificate=chain,
key=private_key,
)
return Role(
mode,
host=normalized,
port=port(table.get("port"), f"the {name} node", 22),
host_key=host_key,
**options,
)
raise InstallError(f"Choose off, local, or remote for {name}.")
raise InstallError(
f"Choose off, local, remote, or remote-install for {name}."
)
@dataclass(frozen=True)
@@ -168,9 +229,14 @@ class Plan:
mail: Role
def description(self):
def target(role):
if role.mode in REMOTE_MODES:
return f"{role.mode} ({role.host}, SSH port {role.port})"
return role.mode
return (
f"https://{self.hostname}/ · {self.access} · "
f"DNS {self.dns.mode} · mail {self.mail.mode} · "
f"DNS {target(self.dns)} · mail {target(self.mail)} · "
f"Administrator {self.admin_username}"
)
@@ -202,6 +268,9 @@ def parse_plan(data):
if access == "tls":
keys(panel, {"hostname", "access", "certificate", "key"}, "panel")
chain, key = certificate(panel, "the panel")
elif access == "acme":
keys(panel, {"hostname", "access"}, "panel")
chain, key = tls_files({"tls": "acme"}, host, "the panel")
elif access == "proxy":
keys(
panel,
@@ -218,7 +287,7 @@ def parse_plan(data):
"Use a separate origin port for the reverse proxy."
)
else:
raise InstallError("Choose tls or proxy for panel access.")
raise InstallError("Choose tls, acme, or proxy for panel access.")
return Plan(
host,
access,