change app dns bind behavior

This commit is contained in:
leo committed 2026-11-22 22:44:47 +02:00
1 parent dd50a0d05e
commit 40a8ed23cd
17 files changed
+1440 -104

No files matched your search

+88 -4
View File
@@ -20,6 +20,12 @@ FQDN_RE = re.compile(
_EXTENSIONS = {"nginx": ".conf", "caddy": ".caddy", "traefik": ".yml"}
# Let's Encrypt production directory; overridable through webserver.tls.acme_ca.
LETSENCRYPT_DIRECTORY = "https://acme-v02.api.letsencrypt.org/directory"
# Modes where proxdomain itself obtains a certificate (nginx + certbot).
CERTBOT_MODES = ("certbot", "letsencrypt")
@dataclass(frozen=True)
class SiteSpec:
@@ -34,6 +40,9 @@ class SiteSpec:
cert_dir: str = "/etc/letsencrypt/live"
webroot: str = "/var/www/certbot"
cert_resolver: str = ""
tls_email: str = ""
acme_ca: str = ""
renew_hook: str = ""
upstream_scheme: str = "http"
@property
@@ -63,6 +72,41 @@ class WebServerManager:
self.config = config
self.runner = runner
# -- TLS -------------------------------------------------------------
@property
def tls_mode(self) -> str:
return self.config.tls.mode
@property
def issues_certificates(self) -> bool:
"""True when proxdomain runs certbot itself (nginx only)."""
return self.config.kind == "nginx" and self.tls_mode in CERTBOT_MODES
@property
def renew_hook(self) -> str:
"""Command certbot runs after a renewal, so the server picks up the new cert."""
return self.config.tls.renew_hook or " ".join(self.config.reload_command)
def certificate_present(self, spec: SiteSpec) -> bool:
return self.runner.exists(spec.certificate)
def certificate_expiry(self, spec: SiteSpec) -> str:
"""Return the notAfter date of an existing certificate, or ''."""
if not self.certificate_present(spec):
return ""
try:
result = self.runner.run(
["openssl", "x509", "-noout", "-enddate", "-in", spec.certificate]
)
except ProxdomainError:
return ""
if not result.ok:
return ""
for line in result.stdout.splitlines():
if line.startswith("notAfter="):
return line.split("=", 1)[1].strip()
return ""
# -- rendering ------------------------------------------------------
def render(self, spec: SiteSpec) -> str:
if spec.webserver == "nginx":
@@ -102,11 +146,12 @@ class WebServerManager:
original = self.runner.read_text(path)
bootstrapped = False
if spec.tls_mode == "certbot" and not self.runner.exists(spec.certificate):
if self.issues_certificates and not self.certificate_present(spec):
try:
self._serve_challenge(spec, path, result)
self._request_certificate(spec, result)
except WebServerError:
except ProxdomainError:
# Do not leave the temporary HTTP vhost behind.
if original is None:
self.runner.remove(path)
else:
@@ -187,6 +232,7 @@ class WebServerManager:
def _serve_challenge(self, spec: SiteSpec, path: str, result: ApplyResult) -> None:
"""Activate an HTTP-only vhost so certbot's webroot plugin can answer."""
self.runner.mkdir(self.config.tls.webroot)
self.runner.write_text(path, render_nginx_challenge(spec))
try:
self.validate()
@@ -200,14 +246,26 @@ class WebServerManager:
def _request_certificate(self, spec: SiteSpec, result: ApplyResult) -> None:
command = list(self.config.tls.certbot_command) + [
"--keep-until-expiring",
"-d",
spec.fqdn,
"--email",
self.config.tls.email,
]
if self.config.tls.acme_ca:
command += ["--server", self.config.tls.acme_ca]
if self.renew_hook:
command += ["--deploy-hook", self.renew_hook]
command.extend(self.config.tls.extra_args)
result.messages.append(f"requesting a certificate for {spec.fqdn}")
completed = self.runner.run(command)
try:
completed = self.runner.run(command)
except ProxdomainError as exc:
raise WebServerError(
f"cannot run certbot for {spec.fqdn}: {exc}\n"
"install it (apt install certbot) or set "
"webserver.tls.certbot_command to its full path"
) from exc
if not completed.ok:
raise WebServerError(
f"certbot failed for {spec.fqdn}:\n{completed.output()}\n"
@@ -244,13 +302,29 @@ class WebServerManager:
)
)
if self.config.tls.mode == "certbot":
if self.issues_certificates:
webroot = self.config.tls.webroot
if dry_run:
steps.append(("planned", f"create ACME webroot {webroot}"))
else:
self.runner.mkdir(webroot)
steps.append(("ok", f"ACME webroot {webroot} is ready"))
if self.tls_mode == "letsencrypt" and self.config.kind in ("caddy", "traefik"):
steps.append(
(
"ok",
f"{self.config.kind} obtains and renews Let's Encrypt certificates "
"for every managed site",
)
)
if self.tls_mode == "letsencrypt" and self.config.kind == "nginx":
steps.append(
(
"next",
"certbot's systemd timer renews certificates; verify with "
"'systemctl list-timers | grep certbot'",
)
)
return steps
def _bootstrap_nginx(self, manage_dir: str, dry_run: bool) -> list[tuple[str, str]]:
@@ -411,6 +485,16 @@ def render_caddy(spec: SiteSpec) -> str:
]
if spec.tls_mode == "files":
lines.append(f"\ttls {spec.certificate} {spec.private_key}")
elif spec.tls_mode == "letsencrypt":
# Pin the issuer: without this Caddy may fall back to ZeroSSL.
lines.append("\ttls {")
lines.append("\t\tissuer acme {")
if spec.tls_email:
lines.append(f"\t\t\temail {spec.tls_email}")
if spec.acme_ca:
lines.append(f"\t\t\tdir {spec.acme_ca}")
lines.append("\t\t}")
lines.append("\t}")
lines.append(f"\treverse_proxy {spec.target}")
lines.append("}")
return "\n".join(lines) + "\n"