diff --git a/app_gateway/caddy_config_export.py b/app_gateway/caddy_config_export.py index 10f5177..e557840 100644 --- a/app_gateway/caddy_config_export.py +++ b/app_gateway/caddy_config_export.py @@ -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(): } +CADDY_EXPORT_DIR = '/caddy_json_export/' + +EXPORT_FILES = { + 'applications.json': build_applications_data, + 'auth_policies.json': build_auth_policies_data, + 'routes.json': build_routes_data, + 'tls.json': build_tls_data, +} + + def export_caddy_config(output_dir): os.makedirs(output_dir, exist_ok=True) - file_map = { - '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(): + 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) diff --git a/app_gateway/views.py b/app_gateway/views.py index bfcfd80..9e58820 100644 --- a/app_gateway/views.py +++ b/app_gateway/views.py @@ -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')) diff --git a/containers/caddy/process_config.py b/containers/caddy/process_config.py index 7cb03e1..855864f 100644 --- a/containers/caddy/process_config.py +++ b/containers/caddy/process_config.py @@ -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): diff --git a/gatekeeper/views.py b/gatekeeper/views.py index 83dd04f..2ec8d68 100644 --- a/gatekeeper/views.py +++ b/gatekeeper/views.py @@ -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) diff --git a/templates/app_gateway/app_gateway_list.html b/templates/app_gateway/app_gateway_list.html index be7e85e..6b5c125 100644 --- a/templates/app_gateway/app_gateway_list.html +++ b/templates/app_gateway/app_gateway_list.html @@ -3,6 +3,7 @@ {% block content %} {% include 'app_gateway/caddy_disabled_alert.html' %} +{% include 'app_gateway/pending_export_alert.html' %}
diff --git a/templates/app_gateway/application_details.html b/templates/app_gateway/application_details.html index e78c432..5add9fb 100644 --- a/templates/app_gateway/application_details.html +++ b/templates/app_gateway/application_details.html @@ -2,6 +2,7 @@ {% load i18n %} {% block content %} +{% include 'app_gateway/pending_export_alert.html' %}

diff --git a/templates/app_gateway/pending_export_alert.html b/templates/app_gateway/pending_export_alert.html new file mode 100644 index 0000000..45b74c8 --- /dev/null +++ b/templates/app_gateway/pending_export_alert.html @@ -0,0 +1,16 @@ +{% load i18n %} +{% if pending_export %} +
+ + + {% trans 'There are changes that have not been exported to Caddy yet.' %} + +
+ {% csrf_token %} + + +
+
+{% endif %} diff --git a/templates/gatekeeper/gatekeeper_list.html b/templates/gatekeeper/gatekeeper_list.html index 8891990..5984644 100644 --- a/templates/gatekeeper/gatekeeper_list.html +++ b/templates/gatekeeper/gatekeeper_list.html @@ -3,6 +3,7 @@ {% block content %} {% include 'app_gateway/caddy_disabled_alert.html' %} +{% include 'app_gateway/pending_export_alert.html' %}