2026-03-14 22:56:47 -03:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
"""
|
2026-03-16 09:49:17 -03:00
|
|
|
Reads JSON config files exported by Django and generates /etc/caddy/Caddyfile.
|
2026-03-14 22:56:47 -03:00
|
|
|
|
|
|
|
|
Expected input files in /caddy_json_export/:
|
|
|
|
|
- wireguard_webadmin.json (required, generated on container startup)
|
|
|
|
|
- applications.json (optional, exported from Django)
|
|
|
|
|
- auth_policies.json (optional, exported from Django)
|
|
|
|
|
- routes.json (optional, exported from Django)
|
2026-11-17 15:59:40 -03:00
|
|
|
- tls.json (optional, exported from Django)
|
2026-03-14 22:56:47 -03:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
import os
|
2026-03-16 20:23:18 -03:00
|
|
|
import re
|
2026-03-16 09:49:17 -03:00
|
|
|
from urllib.parse import urlparse
|
2026-03-14 22:56:47 -03:00
|
|
|
|
|
|
|
|
JSON_DIR = os.environ.get("JSON_DIR", "/caddy_json_export")
|
|
|
|
|
CADDYFILE_PATH = os.environ.get("CADDYFILE_PATH", "/etc/caddy/Caddyfile")
|
2026-03-16 10:34:10 -03:00
|
|
|
AUTH_GATEWAY_INTERNAL_URL = os.environ.get("AUTH_GATEWAY_INTERNAL_URL", "http://wireguard-webadmin-auth-gateway:9091")
|
|
|
|
|
AUTH_GATEWAY_PORTAL_PATH = os.environ.get("AUTH_GATEWAY_EXTERNAL_PATH", "/auth-gateway")
|
2026-03-16 09:49:17 -03:00
|
|
|
AUTH_GATEWAY_CHECK_URI = "/auth/check"
|
2026-11-17 15:59:40 -03:00
|
|
|
CERTIFICATE_TYPES = ("self_signed", "http_challenge", "dns_challenge")
|
|
|
|
|
DEFAULT_CERTIFICATE_TYPE = "http_challenge"
|
|
|
|
|
ACME_DNS_RESOLVERS = "1.1.1.1 1.0.0.1"
|
|
|
|
|
# Set by entrypoint.sh when the generated Caddyfile fails validation
|
|
|
|
|
DISABLE_DNS_CHALLENGE = os.environ.get("DISABLE_DNS_CHALLENGE", "") == "1"
|
2026-03-16 20:23:18 -03:00
|
|
|
AUTH_IDENTITY_HEADERS = (
|
|
|
|
|
"X-Auth-User",
|
|
|
|
|
"X-Auth-Email",
|
|
|
|
|
"X-Auth-Groups",
|
|
|
|
|
"X-Auth-Factors",
|
|
|
|
|
"X-Auth-Policy",
|
|
|
|
|
)
|
2026-03-14 22:56:47 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_json(filename):
|
|
|
|
|
filepath = os.path.join(JSON_DIR, filename)
|
|
|
|
|
if not os.path.exists(filepath):
|
|
|
|
|
return None
|
|
|
|
|
with open(filepath, "r", encoding="utf-8") as json_file:
|
|
|
|
|
return json.load(json_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def collect_all_applications():
|
|
|
|
|
apps = []
|
|
|
|
|
|
|
|
|
|
webadmin_data = load_json("wireguard_webadmin.json")
|
|
|
|
|
if webadmin_data:
|
|
|
|
|
apps.extend(webadmin_data.get("entries", []))
|
|
|
|
|
|
|
|
|
|
applications_data = load_json("applications.json")
|
|
|
|
|
if applications_data:
|
|
|
|
|
apps.extend(applications_data.get("entries", []))
|
|
|
|
|
|
|
|
|
|
return apps
|
|
|
|
|
|
|
|
|
|
|
2026-03-16 09:49:17 -03:00
|
|
|
def split_upstream(upstream):
|
|
|
|
|
"""Return (base_url, upstream_path) where base_url has no path component.
|
|
|
|
|
Upstreams without an explicit http(s):// scheme are returned as-is with no path."""
|
|
|
|
|
if not upstream.startswith("http://") and not upstream.startswith("https://"):
|
|
|
|
|
return upstream, ""
|
|
|
|
|
parsed = urlparse(upstream)
|
|
|
|
|
base = f"{parsed.scheme}://{parsed.netloc}"
|
|
|
|
|
path = parsed.path.rstrip("/")
|
|
|
|
|
return base, path
|
|
|
|
|
|
|
|
|
|
|
2026-03-16 20:48:44 -03:00
|
|
|
def _safe_value(val: str) -> str:
|
|
|
|
|
"""Strip characters that would break or inject into a Caddyfile directive."""
|
|
|
|
|
return re.sub(r"[\r\n\x00]", "", val)
|
|
|
|
|
|
|
|
|
|
|
2026-11-17 15:59:40 -03:00
|
|
|
def _quote_value(val) -> str:
|
|
|
|
|
escaped = _safe_value(str(val)).replace("\\", "\\\\").replace('"', '\\"')
|
|
|
|
|
return f'"{escaped}"'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _safe_identifier(val: str) -> str:
|
|
|
|
|
return re.sub(r"[^A-Za-z0-9_]", "", str(val))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_host_tls(host, tls_data):
|
|
|
|
|
"""Return (certificate_type, dns_credential_name) for a host. Hosts not listed use the HTTP challenge."""
|
|
|
|
|
host_tls = tls_data.get("hosts", {}).get(host.lower(), {})
|
|
|
|
|
certificate_type = host_tls.get("certificate_type", DEFAULT_CERTIFICATE_TYPE)
|
|
|
|
|
if certificate_type not in CERTIFICATE_TYPES:
|
|
|
|
|
certificate_type = DEFAULT_CERTIFICATE_TYPE
|
|
|
|
|
|
|
|
|
|
dns_credential = None
|
|
|
|
|
if certificate_type == "dns_challenge":
|
|
|
|
|
dns_credential = host_tls.get("dns_credential")
|
|
|
|
|
if DISABLE_DNS_CHALLENGE or dns_credential not in tls_data.get("dns_credentials", {}):
|
|
|
|
|
certificate_type = DEFAULT_CERTIFICATE_TYPE
|
|
|
|
|
dns_credential = None
|
|
|
|
|
return certificate_type, dns_credential
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def group_hosts_by_tls(hosts, tls_data):
|
|
|
|
|
"""Hosts sharing the same certificate settings are served by the same site block."""
|
|
|
|
|
groups = {}
|
|
|
|
|
for host in hosts:
|
|
|
|
|
groups.setdefault(get_host_tls(host, tls_data), []).append(host)
|
|
|
|
|
return groups.items()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def build_caddyfile(apps, auth_policies, routes, tls_data=None):
|
|
|
|
|
tls_data = tls_data or {}
|
2026-03-16 09:49:17 -03:00
|
|
|
policies = auth_policies.get("policies", {}) if auth_policies else {}
|
|
|
|
|
route_entries = routes.get("entries", {}) if routes else {}
|
2026-03-14 22:56:47 -03:00
|
|
|
lines = []
|
2026-03-15 11:37:25 -03:00
|
|
|
|
|
|
|
|
def get_policy_type(policy_name):
|
|
|
|
|
if policy_name and policy_name in policies:
|
|
|
|
|
return policies[policy_name].get("policy_type", "bypass")
|
2026-03-16 09:49:17 -03:00
|
|
|
return "deny"
|
2026-03-14 22:56:47 -03:00
|
|
|
|
2026-03-16 09:49:17 -03:00
|
|
|
def emit_auth_portal():
|
|
|
|
|
lines.append(f" handle_path {AUTH_GATEWAY_PORTAL_PATH}/* {{")
|
|
|
|
|
lines.append(f" reverse_proxy {AUTH_GATEWAY_INTERNAL_URL}")
|
|
|
|
|
lines.append(" }")
|
|
|
|
|
lines.append("")
|
|
|
|
|
|
2026-11-17 15:59:40 -03:00
|
|
|
def emit_tls(certificate_type, dns_credential):
|
|
|
|
|
if certificate_type == "self_signed":
|
|
|
|
|
lines.append(" tls internal")
|
|
|
|
|
lines.append("")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
lines.append(" tls {")
|
|
|
|
|
if certificate_type == "dns_challenge":
|
|
|
|
|
dns_credential_data = tls_data["dns_credentials"][dns_credential]
|
|
|
|
|
provider = _safe_identifier(dns_credential_data.get("provider", ""))
|
|
|
|
|
lines.append(" issuer acme {")
|
|
|
|
|
lines.append(f" dns {provider} {{")
|
|
|
|
|
for field_name, field_value in dns_credential_data.get("credentials", {}).items():
|
|
|
|
|
lines.append(f" {_safe_identifier(field_name)} {_quote_value(field_value)}")
|
|
|
|
|
lines.append(" }")
|
|
|
|
|
lines.append(f" resolvers {ACME_DNS_RESOLVERS}")
|
|
|
|
|
lines.append(" }")
|
|
|
|
|
else:
|
|
|
|
|
lines.append(" issuer acme")
|
|
|
|
|
# Fall back to the internal CA when the ACME certificate cannot be issued
|
|
|
|
|
lines.append(" issuer internal")
|
|
|
|
|
lines.append(" }")
|
|
|
|
|
lines.append("")
|
|
|
|
|
|
2026-03-16 20:23:18 -03:00
|
|
|
def emit_identity_header_sanitization(indent=" "):
|
|
|
|
|
for header_name in AUTH_IDENTITY_HEADERS:
|
|
|
|
|
lines.append(f"{indent}request_header -{header_name}")
|
|
|
|
|
|
2026-03-16 20:36:49 -03:00
|
|
|
def emit_encoded_slash_block():
|
2026-03-16 20:48:44 -03:00
|
|
|
# Reject paths containing encoded slashes in any depth of percent-encoding:
|
|
|
|
|
# %2f = /
|
|
|
|
|
# %252f = %2f (double-encoded, decodes to / on second pass)
|
|
|
|
|
# %25252f = triple-encoded, etc.
|
|
|
|
|
# Caddy's path matcher does not decode percent-encoding, so /%2fadmin would
|
|
|
|
|
# NOT match path /admin and would fall through to the default (potentially
|
|
|
|
|
# bypass) handler even though some upstreams decode it to /admin.
|
2026-03-16 20:36:49 -03:00
|
|
|
lines.append(" @encoded_slash {")
|
2026-03-16 20:48:44 -03:00
|
|
|
lines.append(" path_regexp (?i)%(?:25)*2f")
|
2026-03-16 20:36:49 -03:00
|
|
|
lines.append(" }")
|
|
|
|
|
lines.append(" handle @encoded_slash {")
|
|
|
|
|
lines.append(" respond 400")
|
|
|
|
|
lines.append(" }")
|
|
|
|
|
lines.append("")
|
|
|
|
|
|
2026-03-16 20:23:18 -03:00
|
|
|
def emit_route_matcher(matcher_name, path_prefix):
|
|
|
|
|
matcher_name = re.sub(r"[^A-Za-z0-9_]", "_", matcher_name)
|
|
|
|
|
normalized_prefix = path_prefix.strip().rstrip("/") or "/"
|
|
|
|
|
if not normalized_prefix.startswith("/"):
|
|
|
|
|
normalized_prefix = f"/{normalized_prefix}"
|
|
|
|
|
if normalized_prefix == "/":
|
|
|
|
|
return None
|
|
|
|
|
lines.append(f" @{matcher_name} {{")
|
|
|
|
|
lines.append(f" path {normalized_prefix} {normalized_prefix}/*")
|
|
|
|
|
lines.append(" }")
|
|
|
|
|
lines.append("")
|
|
|
|
|
return f"@{matcher_name}"
|
|
|
|
|
|
|
|
|
|
def handle_open(matcher=None):
|
|
|
|
|
if not matcher:
|
2026-03-16 09:49:17 -03:00
|
|
|
return " handle {"
|
|
|
|
|
return f" handle {matcher} {{"
|
|
|
|
|
|
2026-03-16 14:27:03 -03:00
|
|
|
def emit_reverse_proxy(base, upstream_path, indent=" ", allow_invalid_cert=False):
|
2026-03-16 09:49:17 -03:00
|
|
|
if upstream_path:
|
|
|
|
|
lines.append(f"{indent}rewrite * {upstream_path}{{uri}}")
|
2026-03-16 14:27:03 -03:00
|
|
|
if allow_invalid_cert:
|
|
|
|
|
lines.append(f"{indent}reverse_proxy {base} {{")
|
|
|
|
|
lines.append(f"{indent} transport http {{")
|
|
|
|
|
lines.append(f"{indent} tls_insecure_skip_verify")
|
|
|
|
|
lines.append(f"{indent} }}")
|
|
|
|
|
lines.append(f"{indent}}}")
|
|
|
|
|
else:
|
|
|
|
|
lines.append(f"{indent}reverse_proxy {base}")
|
2026-03-16 09:49:17 -03:00
|
|
|
|
2026-03-16 14:27:03 -03:00
|
|
|
def emit_protected_handle(path_matcher, base, upstream_path, allow_invalid_cert=False):
|
2026-03-16 09:49:17 -03:00
|
|
|
lines.append(handle_open(path_matcher))
|
|
|
|
|
lines.append(f" forward_auth {AUTH_GATEWAY_INTERNAL_URL} {{")
|
|
|
|
|
lines.append(f" uri {AUTH_GATEWAY_CHECK_URI}")
|
|
|
|
|
lines.append(" copy_headers X-Auth-User X-Auth-Email X-Auth-Groups X-Auth-Factors X-Auth-Policy")
|
|
|
|
|
lines.append(" }")
|
2026-03-16 14:27:03 -03:00
|
|
|
emit_reverse_proxy(base, upstream_path, allow_invalid_cert=allow_invalid_cert)
|
2026-03-16 09:49:17 -03:00
|
|
|
lines.append(" }")
|
2026-03-15 16:30:28 -03:00
|
|
|
lines.append("")
|
|
|
|
|
|
2026-11-17 15:59:40 -03:00
|
|
|
def emit_site_block(app, hosts, certificate_type, dns_credential):
|
2026-03-14 22:56:47 -03:00
|
|
|
upstream = app.get("upstream", "")
|
2026-11-16 21:13:50 -03:00
|
|
|
# tls_insecure_skip_verify is only valid for https upstreams
|
|
|
|
|
allow_invalid_cert = app.get("allow_invalid_cert", False) and upstream.startswith("https://")
|
2026-03-14 22:56:47 -03:00
|
|
|
static_routes = app.get("static_routes", [])
|
2026-03-16 09:49:17 -03:00
|
|
|
app_id = app.get("id", "")
|
2026-03-14 22:56:47 -03:00
|
|
|
|
2026-03-16 09:49:17 -03:00
|
|
|
base, upstream_path = split_upstream(upstream)
|
2026-03-14 22:56:47 -03:00
|
|
|
|
2026-03-16 20:48:44 -03:00
|
|
|
safe_hosts = [_safe_value(h) for h in hosts]
|
|
|
|
|
lines.append(f"{', '.join(safe_hosts)} {{")
|
2026-11-17 15:59:40 -03:00
|
|
|
emit_tls(certificate_type, dns_credential)
|
2026-03-16 19:35:24 -03:00
|
|
|
lines.append(" # Security: overwrite client-supplied forwarding headers with verified values")
|
2026-03-16 20:23:18 -03:00
|
|
|
lines.append(" request_header X-Forwarded-For {http.request.remote.host}")
|
2026-03-16 19:35:24 -03:00
|
|
|
lines.append(" request_header -X-Forwarded-Host")
|
2026-03-16 20:23:18 -03:00
|
|
|
emit_identity_header_sanitization()
|
2026-03-16 19:35:24 -03:00
|
|
|
lines.append("")
|
2026-03-16 20:36:49 -03:00
|
|
|
emit_encoded_slash_block()
|
2026-03-16 09:49:17 -03:00
|
|
|
emit_auth_portal()
|
2026-03-15 10:16:26 -03:00
|
|
|
|
2026-03-14 22:56:47 -03:00
|
|
|
for static_route in static_routes:
|
2026-03-16 20:48:44 -03:00
|
|
|
path_prefix = _safe_value(static_route.get("path_prefix", ""))
|
|
|
|
|
root_dir = _safe_value(static_route.get("root", ""))
|
|
|
|
|
cache_control = _safe_value(static_route.get("cache_control", ""))
|
2026-03-14 22:56:47 -03:00
|
|
|
lines.append(f" handle_path {path_prefix}/* {{")
|
|
|
|
|
lines.append(f" root * {root_dir}")
|
2026-03-16 09:49:17 -03:00
|
|
|
lines.append(" file_server")
|
2026-03-14 22:56:47 -03:00
|
|
|
if cache_control:
|
|
|
|
|
lines.append(f" header Cache-Control \"{cache_control}\"")
|
2026-03-16 09:49:17 -03:00
|
|
|
lines.append(" }")
|
2026-03-14 22:56:47 -03:00
|
|
|
lines.append("")
|
|
|
|
|
|
2026-03-16 09:49:17 -03:00
|
|
|
app_route_data = route_entries.get(app_id)
|
|
|
|
|
if app_route_data is None:
|
2026-03-16 14:27:03 -03:00
|
|
|
emit_reverse_proxy(base, upstream_path, indent=" ", allow_invalid_cert=allow_invalid_cert)
|
2026-03-16 09:49:17 -03:00
|
|
|
lines.append("}")
|
2026-03-15 11:37:25 -03:00
|
|
|
lines.append("")
|
2026-11-17 15:59:40 -03:00
|
|
|
return
|
2026-03-15 11:37:25 -03:00
|
|
|
|
2026-03-16 09:49:17 -03:00
|
|
|
route_list = sorted(app_route_data.get("routes", []), key=lambda route: len(route.get("path_prefix", "")), reverse=True)
|
2026-03-16 20:23:18 -03:00
|
|
|
for route_index, route in enumerate(route_list):
|
2026-03-16 09:49:17 -03:00
|
|
|
path_prefix = route.get("path_prefix", "/")
|
|
|
|
|
policy_type = get_policy_type(route.get("policy"))
|
2026-03-16 20:23:18 -03:00
|
|
|
matcher = emit_route_matcher(f"route_{app_id}_{route_index}", path_prefix)
|
2026-03-16 09:49:17 -03:00
|
|
|
if policy_type == "bypass":
|
|
|
|
|
lines.append(handle_open(matcher))
|
2026-03-16 14:27:03 -03:00
|
|
|
emit_reverse_proxy(base, upstream_path, allow_invalid_cert=allow_invalid_cert)
|
2026-03-16 09:49:17 -03:00
|
|
|
lines.append(" }")
|
2026-03-15 11:37:25 -03:00
|
|
|
lines.append("")
|
2026-03-16 09:49:17 -03:00
|
|
|
elif policy_type == "deny":
|
|
|
|
|
lines.append(handle_open(matcher))
|
|
|
|
|
lines.append(" respond 403")
|
|
|
|
|
lines.append(" }")
|
|
|
|
|
lines.append("")
|
|
|
|
|
else:
|
2026-03-16 14:27:03 -03:00
|
|
|
emit_protected_handle(matcher, base, upstream_path, allow_invalid_cert=allow_invalid_cert)
|
2026-03-15 11:37:25 -03:00
|
|
|
|
2026-03-16 09:49:17 -03:00
|
|
|
default_policy_type = get_policy_type(app_route_data.get("default_policy"))
|
|
|
|
|
if default_policy_type == "bypass":
|
2026-03-16 14:27:03 -03:00
|
|
|
emit_reverse_proxy(base, upstream_path, indent=" ", allow_invalid_cert=allow_invalid_cert)
|
2026-03-16 09:49:17 -03:00
|
|
|
elif default_policy_type == "deny":
|
|
|
|
|
lines.append(" respond 403")
|
|
|
|
|
else:
|
2026-03-16 20:23:18 -03:00
|
|
|
emit_protected_handle(None, base, upstream_path, allow_invalid_cert=allow_invalid_cert)
|
2026-03-16 09:49:17 -03:00
|
|
|
lines.append("}")
|
2026-03-14 22:56:47 -03:00
|
|
|
lines.append("")
|
|
|
|
|
|
2026-11-17 15:59:40 -03:00
|
|
|
for app in apps:
|
|
|
|
|
hosts = app.get("hosts", [])
|
|
|
|
|
if not hosts or not app.get("upstream", ""):
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
for (certificate_type, dns_credential), group_hosts in group_hosts_by_tls(hosts, tls_data):
|
|
|
|
|
emit_site_block(app, group_hosts, certificate_type, dns_credential)
|
|
|
|
|
|
2026-03-14 22:56:47 -03:00
|
|
|
return "\n".join(lines)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
apps = collect_all_applications()
|
|
|
|
|
auth_policies = load_json("auth_policies.json")
|
|
|
|
|
routes = load_json("routes.json")
|
2026-11-17 15:59:40 -03:00
|
|
|
tls_data = load_json("tls.json")
|
2026-03-14 22:56:47 -03:00
|
|
|
|
2026-11-17 15:59:40 -03:00
|
|
|
caddyfile_content = build_caddyfile(apps, auth_policies, routes, tls_data)
|
2026-03-14 22:56:47 -03:00
|
|
|
os.makedirs(os.path.dirname(CADDYFILE_PATH), exist_ok=True)
|
|
|
|
|
with open(CADDYFILE_PATH, "w", encoding="utf-8") as caddyfile:
|
|
|
|
|
caddyfile.write(caddyfile_content)
|
|
|
|
|
print(f"Caddyfile written to {CADDYFILE_PATH}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|