change app dns bind behavior
This commit is contained in:
1 parent
dd50a0d05e
commit
40a8ed23cd
17 files changed
+1440
-104
No files matched your search
+102
-19
@@ -2,7 +2,9 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import ipaddress
|
||||
import os
|
||||
import re
|
||||
import tomllib
|
||||
from dataclasses import dataclass, replace
|
||||
from pathlib import Path
|
||||
@@ -18,7 +20,7 @@ DEFAULT_CONFIG_PATHS = (
|
||||
|
||||
WEBSERVER_KINDS = ("nginx", "caddy", "traefik")
|
||||
WEBSERVER_MODES = ("local", "ssh")
|
||||
TLS_MODES = ("none", "files", "certbot")
|
||||
TLS_MODES = ("none", "files", "certbot", "letsencrypt")
|
||||
IP_PREFERENCES = ("auto", "ipv4", "ipv6")
|
||||
|
||||
# Web server defaults live here so `config.example.toml` and the docs stay honest.
|
||||
@@ -52,6 +54,23 @@ class GeneralConfig:
|
||||
default_port: int = 80
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class DnsConfig:
|
||||
"""What the public DNS record points at.
|
||||
|
||||
guest - the guest's own address (useful when guests hold public IPs)
|
||||
host - the Proxmox host's public address, which runs the reverse proxy
|
||||
fixed - the literal address in dns.address
|
||||
"""
|
||||
|
||||
target: str = "host" # guest | host | fixed
|
||||
address: str = ""
|
||||
hostname: str = ""
|
||||
detect_url_v4: str = "https://ipv4.icanhazip.com"
|
||||
detect_url_v6: str = "https://ipv6.icanhazip.com"
|
||||
detect_timeout: float = 5.0
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ProxmoxConfig:
|
||||
api_url: str
|
||||
@@ -70,13 +89,11 @@ class ProxmoxConfig:
|
||||
|
||||
def resolve_secret(self) -> str:
|
||||
if self.token_secret_env:
|
||||
value = os.environ.get(self.token_secret_env, "")
|
||||
if not value:
|
||||
raise ConfigError(
|
||||
f"environment variable {self.token_secret_env} "
|
||||
"(proxmox.token_secret_env) is empty or unset"
|
||||
)
|
||||
return value
|
||||
return _env_secret(
|
||||
self.token_secret_env,
|
||||
setting="proxmox.token_secret_env",
|
||||
alternative="proxmox.token_secret",
|
||||
)
|
||||
if self.token_secret_file:
|
||||
path = Path(self.token_secret_file).expanduser()
|
||||
try:
|
||||
@@ -108,13 +125,11 @@ class CloudflareConfig:
|
||||
|
||||
def resolve_token(self) -> str:
|
||||
if self.api_token_env:
|
||||
value = os.environ.get(self.api_token_env, "")
|
||||
if not value:
|
||||
raise ConfigError(
|
||||
f"environment variable {self.api_token_env} "
|
||||
"(cloudflare.api_token_env) is empty or unset"
|
||||
)
|
||||
return value
|
||||
return _env_secret(
|
||||
self.api_token_env,
|
||||
setting="cloudflare.api_token_env",
|
||||
alternative="cloudflare.api_token",
|
||||
)
|
||||
if self.api_token_file:
|
||||
path = Path(self.api_token_file).expanduser()
|
||||
try:
|
||||
@@ -136,13 +151,17 @@ class CloudflareConfig:
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class TlsConfig:
|
||||
mode: str = "none" # none | files | certbot
|
||||
mode: str = "none" # none | files | certbot | letsencrypt
|
||||
cert_dir: str = "/etc/letsencrypt/live"
|
||||
webroot: str = "/var/www/certbot"
|
||||
certbot_command: tuple[str, ...] = (
|
||||
"certbot", "certonly", "--webroot", "-w", "/var/www/certbot", "-n", "--agree-tos",
|
||||
)
|
||||
email: str = ""
|
||||
# Alternative ACME directory (staging, Pebble, ...). Empty = Let's Encrypt production.
|
||||
acme_ca: str = ""
|
||||
# Command run after each renewal; empty = the web server's reload command.
|
||||
renew_hook: str = ""
|
||||
extra_args: tuple[str, ...] = ()
|
||||
|
||||
|
||||
@@ -195,6 +214,7 @@ class Config:
|
||||
proxmox: ProxmoxConfig
|
||||
cloudflare: CloudflareConfig
|
||||
webserver: WebServerConfig
|
||||
dns: DnsConfig = DnsConfig()
|
||||
watch: WatchConfig = WatchConfig()
|
||||
state: StateConfig = StateConfig()
|
||||
source: Path | None = None
|
||||
@@ -245,6 +265,7 @@ def parse_config(raw: Mapping[str, Any], *, source: Path | None = None) -> Confi
|
||||
watch_raw = _table(raw, "watch")
|
||||
state_raw = _table(raw, "state")
|
||||
tls_raw = _table(webserver_raw, "tls", prefix="webserver")
|
||||
dns_raw = _table(raw, "dns")
|
||||
|
||||
general = GeneralConfig(
|
||||
base_domain=_str(general_raw, "base_domain", required=True, prefix="general"),
|
||||
@@ -320,10 +341,16 @@ def parse_config(raw: Mapping[str, Any], *, source: Path | None = None) -> Confi
|
||||
certbot_command=_str_tuple(tls_raw, "certbot_command", prefix="webserver.tls")
|
||||
or TlsConfig.certbot_command,
|
||||
email=_str(tls_raw, "email", "", prefix="webserver.tls"),
|
||||
acme_ca=_str(tls_raw, "acme_ca", "", prefix="webserver.tls"),
|
||||
renew_hook=_str(tls_raw, "renew_hook", "", prefix="webserver.tls"),
|
||||
extra_args=_str_tuple(tls_raw, "extra_args", prefix="webserver.tls"),
|
||||
)
|
||||
if tls.mode == "certbot" and not tls.email:
|
||||
raise ConfigError("webserver.tls.email is required when tls.mode = 'certbot'")
|
||||
if tls.mode in ("certbot", "letsencrypt") and not tls.email:
|
||||
raise ConfigError(
|
||||
"webserver.tls.email is required when tls.mode is 'certbot' or 'letsencrypt'"
|
||||
)
|
||||
if tls.acme_ca and not tls.acme_ca.startswith(("http://", "https://")):
|
||||
raise ConfigError("webserver.tls.acme_ca must start with https://")
|
||||
|
||||
webserver = WebServerConfig(
|
||||
kind=kind,
|
||||
@@ -346,19 +373,45 @@ def parse_config(raw: Mapping[str, Any], *, source: Path | None = None) -> Confi
|
||||
if kind != "nginx" and tls.mode == "certbot":
|
||||
raise ConfigError(
|
||||
"webserver.tls.mode = 'certbot' is only supported with kind = 'nginx'; "
|
||||
"use 'none' (Caddy manages certificates itself) or 'files'"
|
||||
"use 'letsencrypt' (automatic for Caddy/Traefik) or 'files'"
|
||||
)
|
||||
if kind == "traefik" and tls.mode == "files":
|
||||
raise ConfigError(
|
||||
"webserver.tls.mode = 'files' is not supported with kind = 'traefik'; "
|
||||
"set webserver.cert_resolver instead"
|
||||
)
|
||||
if kind == "traefik" and tls.mode == "letsencrypt" and not webserver.cert_resolver:
|
||||
raise ConfigError(
|
||||
"webserver.cert_resolver is required when kind = 'traefik' and "
|
||||
"tls.mode = 'letsencrypt'"
|
||||
)
|
||||
|
||||
state = StateConfig(
|
||||
path=_str(state_raw, "path", StateConfig.path, prefix="state"),
|
||||
audit_path=_str(state_raw, "audit_path", "", prefix="state"),
|
||||
lock_timeout=_float(state_raw, "lock_timeout", 10.0, prefix="state"),
|
||||
)
|
||||
|
||||
dns = DnsConfig(
|
||||
target=_choice(dns_raw, "target", "host", ("guest", "host", "fixed"), prefix="dns"),
|
||||
address=_str(dns_raw, "address", "", prefix="dns"),
|
||||
hostname=_str(dns_raw, "hostname", "", prefix="dns"),
|
||||
detect_url_v4=_str(dns_raw, "detect_url_v4", DnsConfig.detect_url_v4, prefix="dns"),
|
||||
detect_url_v6=_str(dns_raw, "detect_url_v6", DnsConfig.detect_url_v6, prefix="dns"),
|
||||
detect_timeout=_float(dns_raw, "detect_timeout", 5.0, prefix="dns"),
|
||||
)
|
||||
if dns.address:
|
||||
try:
|
||||
ipaddress.ip_address(dns.address)
|
||||
except ValueError as exc:
|
||||
raise ConfigError(f"dns.address is not a valid IP address: {dns.address!r}") from exc
|
||||
if dns.target == "fixed" and not dns.address:
|
||||
raise ConfigError("dns.address is required when dns.target = 'fixed'")
|
||||
if dns.detect_timeout <= 0:
|
||||
raise ConfigError("dns.detect_timeout must be greater than zero")
|
||||
for key, value in (("detect_url_v4", dns.detect_url_v4), ("detect_url_v6", dns.detect_url_v6)):
|
||||
if value and not value.startswith(("http://", "https://")):
|
||||
raise ConfigError(f"dns.{key} must start with http:// or https://")
|
||||
watch = WatchConfig(
|
||||
enabled=_bool(watch_raw, "enabled", False, prefix="watch"),
|
||||
interval=max(5, _int(watch_raw, "interval", 60, prefix="watch")),
|
||||
@@ -372,6 +425,7 @@ def parse_config(raw: Mapping[str, Any], *, source: Path | None = None) -> Confi
|
||||
proxmox=proxmox,
|
||||
cloudflare=cloudflare,
|
||||
webserver=webserver,
|
||||
dns=dns,
|
||||
watch=watch,
|
||||
state=state,
|
||||
source=source,
|
||||
@@ -386,6 +440,35 @@ def with_overrides(config: Config, **overrides: Any) -> Config:
|
||||
# ---------------------------------------------------------------------------
|
||||
# helpers
|
||||
# ---------------------------------------------------------------------------
|
||||
_ENV_NAME_RE = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*$")
|
||||
_UUID_RE = re.compile(
|
||||
r"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
|
||||
)
|
||||
|
||||
|
||||
def _env_secret(name: str, *, setting: str, alternative: str) -> str:
|
||||
"""Read a secret from the environment variable named by a config setting."""
|
||||
name = name.strip()
|
||||
if not _ENV_NAME_RE.match(name):
|
||||
hint = ""
|
||||
if _UUID_RE.match(name):
|
||||
hint = (
|
||||
"; this looks like the token value itself — put the secret in "
|
||||
f"{alternative} (or a *_file setting) and set {setting} to the "
|
||||
"variable NAME, for example PROXDOMAIN_PVE_TOKEN"
|
||||
)
|
||||
raise ConfigError(
|
||||
f"{setting} must be the name of an environment variable, not {name!r}{hint}"
|
||||
)
|
||||
value = os.environ.get(name, "")
|
||||
if not value:
|
||||
raise ConfigError(
|
||||
f"{setting} points at environment variable {name}, which is empty or "
|
||||
f"unset; export {name} before running proxdomain"
|
||||
)
|
||||
return value
|
||||
|
||||
|
||||
def _table(raw: Mapping[str, Any], key: str, *, prefix: str = "") -> Mapping[str, Any]:
|
||||
value = raw.get(key, {})
|
||||
if not isinstance(value, Mapping):
|
||||
|
||||
Reference in new issue
Block a user