mirror of
https://github.com/eduardogsilva/wireguard_webadmin.git
synced 2026-11-21 04:23:15 +01:00
Warn about unexported App Gateway changes and restart Caddy when switching hosts to self-signed
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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'))
|
||||
|
||||
Reference in New Issue
Block a user