Warn about unexported App Gateway changes and restart Caddy when switching hosts to self-signed

This commit is contained in:
Eduardo Silva
2026-11-18 18:00:06 -03:00
parent 8b18d683ab
commit 467bc48cce
8 changed files with 68 additions and 12 deletions
+28 -4
View File
@@ -1,6 +1,8 @@
import json
import os
from django.conf import settings
from app_gateway.models import (
AccessPolicy, Application, ApplicationHost, ApplicationPolicy, RESERVED_APP_NAME
)
@@ -154,18 +156,40 @@ def build_tls_data():
}
def export_caddy_config(output_dir):
os.makedirs(output_dir, exist_ok=True)
CADDY_EXPORT_DIR = '/caddy_json_export/'
file_map = {
EXPORT_FILES = {
'applications.json': build_applications_data,
'auth_policies.json': build_auth_policies_data,
'routes.json': build_routes_data,
'tls.json': build_tls_data,
}
for filename, builder in file_map.items():
def export_caddy_config(output_dir):
os.makedirs(output_dir, exist_ok=True)
for filename, builder in EXPORT_FILES.items():
filepath = os.path.join(output_dir, filename)
with open(filepath, 'w', encoding='utf-8') as output_file:
json.dump(builder(), output_file, indent=2)
output_file.write('\n')
def has_pending_export(output_dir):
"""Return True when the current configuration differs from the files last exported to output_dir."""
for filename, builder in EXPORT_FILES.items():
filepath = os.path.join(output_dir, filename)
try:
with open(filepath, 'r', encoding='utf-8') as exported_file:
exported_data = json.load(exported_file)
except (OSError, ValueError):
return True
# Round-trip through JSON so the comparison matches what export_caddy_config writes
if json.loads(json.dumps(builder())) != exported_data:
return True
return False
def caddy_export_pending():
return settings.CADDY_ENABLED and has_pending_export(CADDY_EXPORT_DIR)
+9 -3
View File
@@ -8,11 +8,12 @@ from django.http import JsonResponse
from django.shortcuts import render, get_object_or_404, redirect
from django.urls import reverse
from django.utils.formats import date_format
from django.utils.http import url_has_allowed_host_and_scheme
from django.utils.timezone import localtime
from django.utils.translation import gettext as _
from django.views.decorators.http import require_POST
from app_gateway.caddy_config_export import export_caddy_config
from app_gateway.caddy_config_export import CADDY_EXPORT_DIR, caddy_export_pending, export_caddy_config
from app_gateway.certificate_status import inspect_certificate
from app_gateway.forms import (
ApplicationForm, ApplicationHostForm, AccessPolicyForm,
@@ -48,6 +49,7 @@ def view_app_gateway_list(request):
'dns_credentials': dns_credentials,
'active_tab': tab,
'caddy_enabled': settings.CADDY_ENABLED,
'pending_export': caddy_export_pending(),
}
return render(request, 'app_gateway/app_gateway_list.html', context)
@@ -71,6 +73,7 @@ def view_application_details(request):
'routes': routes,
'is_reserved': is_reserved,
'caddy_enabled': settings.CADDY_ENABLED,
'pending_export': caddy_export_pending(),
'page_title': _('Application Details'),
}
return render(request, 'app_gateway/application_details.html', context)
@@ -531,7 +534,7 @@ def view_retry_application_host_certificate(request):
return redirect(redirect_url)
ApplicationHost.objects.filter(pk=application_host.pk).update(certificate_generation=F('certificate_generation') + 1)
export_caddy_config('/caddy_json_export/')
export_caddy_config(CADDY_EXPORT_DIR)
if settings.DEBUG:
export_caddy_config(os.path.join(settings.BASE_DIR, 'containers', 'caddy', 'config_files'))
@@ -604,6 +607,9 @@ def view_export_caddy_config(request):
return render(request, 'access_denied.html', {'page_title': _('Access Denied')})
redirect_url = reverse('app_gateway_list') + '?tab=applications'
next_url = request.POST.get('next', '')
if next_url and url_has_allowed_host_and_scheme(next_url, allowed_hosts={request.get_host()}):
redirect_url = next_url
if not settings.CADDY_ENABLED:
messages.error(request, _(
@@ -612,7 +618,7 @@ def view_export_caddy_config(request):
))
return redirect(redirect_url)
export_caddy_config('/caddy_json_export/')
export_caddy_config(CADDY_EXPORT_DIR)
if settings.DEBUG:
export_caddy_config(os.path.join(settings.BASE_DIR, 'containers', 'caddy', 'config_files'))
+6 -1
View File
@@ -363,10 +363,15 @@ def sync_tls_state():
restart_required = False
if applied_state is not None:
for host, host_state in pending_state.items():
if host_state[0] not in ACME_CERTIFICATE_TYPES or applied_state.get(host) in (None, host_state):
if applied_state.get(host) in (None, host_state):
continue
if "/" in host or host in ("", ".", ".."):
continue
if host_state[0] not in ACME_CERTIFICATE_TYPES:
# A cached ACME certificate would keep being served instead of the self-signed one
if has_acme_certificate(host):
restart_required = True
continue
fallback_removed = False
fallback_certificate_dir = os.path.join(CERTIFICATES_DIR, INTERNAL_ISSUER_DIR_NAME, host)
if os.path.isdir(fallback_certificate_dir):
+2
View File
@@ -10,6 +10,7 @@ from django.shortcuts import render, get_object_or_404, redirect
from django.urls import reverse
from django.utils.translation import gettext as _
from app_gateway.caddy_config_export import caddy_export_pending
from app_gateway.setup_defaults import create_default_entries
from gatekeeper.forms import GatekeeperUserForm, GatekeeperGroupForm, AuthMethodForm, AuthMethodAllowedDomainForm, \
AuthMethodAllowedEmailForm, GatekeeperIPAddressForm
@@ -42,6 +43,7 @@ def view_gatekeeper_list(request):
'auth_ips': auth_ips,
'active_tab': active_tab,
'caddy_enabled': settings.CADDY_ENABLED,
'pending_export': caddy_export_pending(),
}
return render(request, 'gatekeeper/gatekeeper_list.html', context)
@@ -3,6 +3,7 @@
{% block content %}
{% include 'app_gateway/caddy_disabled_alert.html' %}
{% include 'app_gateway/pending_export_alert.html' %}
<div class="card card-primary card-outline">
<div class="card-body">
@@ -2,6 +2,7 @@
{% load i18n %}
{% block content %}
{% include 'app_gateway/pending_export_alert.html' %}
<div class="card card-primary card-outline">
<div class="card-header">
<h3 class="card-title">
@@ -0,0 +1,16 @@
{% load i18n %}
{% if pending_export %}
<div class="alert alert-warning d-flex justify-content-between align-items-center">
<span>
<i class="icon fas fa-exclamation-triangle"></i>
{% trans 'There are changes that have not been exported to Caddy yet.' %}
</span>
<form method="post" action="{% url 'export_caddy_config' %}" class="d-inline mb-0">
{% csrf_token %}
<input type="hidden" name="next" value="{{ request.get_full_path }}">
<button type="submit" class="btn btn-sm btn-light">
<i class="fas fa-file-export"></i> {% trans 'Export now' %}
</button>
</form>
</div>
{% endif %}
@@ -3,6 +3,7 @@
{% block content %}
{% include 'app_gateway/caddy_disabled_alert.html' %}
{% include 'app_gateway/pending_export_alert.html' %}
<div class="card card-primary card-outline">
<div class="card-body">