Add certificate retry, optional fallback and DNS propagation fix to App Gateway hosts

This commit is contained in:
Eduardo Silva
2026-11-18 17:33:44 -03:00
parent be7965512b
commit 77e046dc1f
11 changed files with 260 additions and 33 deletions
+5 -1
View File
@@ -134,7 +134,11 @@ def build_tls_data():
hosts = {}
dns_credentials = {}
for application_host in ApplicationHost.objects.select_related('dns_credential'):
host_entry = {'certificate_type': application_host.certificate_type}
host_entry = {
'certificate_type': application_host.certificate_type,
'certificate_fallback': application_host.certificate_fallback,
'certificate_generation': application_host.certificate_generation,
}
dns_credential = application_host.dns_credential
if application_host.certificate_type == 'dns_challenge' and dns_credential:
host_entry['dns_credential'] = dns_credential.name
+16 -6
View File
@@ -77,11 +77,12 @@ class ApplicationForm(forms.ModelForm):
class ApplicationHostForm(forms.ModelForm):
class Meta:
model = ApplicationHost
fields = ['hostname', 'certificate_type', 'dns_credential']
fields = ['hostname', 'certificate_type', 'dns_credential', 'certificate_fallback']
labels = {
'hostname': _('Hostname'),
'certificate_type': _('Certificate'),
'dns_credential': _('DNS Credential'),
'certificate_fallback': _('Use a self-signed certificate if the trusted certificate cannot be issued'),
}
def clean(self):
@@ -103,9 +104,13 @@ class ApplicationHostForm(forms.ModelForm):
if certificate_type == 'dns_challenge':
if not dns_credential:
self.add_error('dns_credential', _('Select a DNS credential to use the DNS challenge.'))
else:
cleaned_data['dns_credential'] = None
self.add_error('dns_credential', _('Select a DNS credential to use the DNS validation.'))
elif dns_credential:
self.add_error('dns_credential', _('A DNS credential can only be used with the DNS validation.'))
# The fallback keeps WireGuard WebAdmin reachable when the ACME certificate cannot be issued
if self.is_reserved or certificate_type == 'self_signed':
cleaned_data['certificate_fallback'] = True
return cleaned_data
@@ -116,6 +121,7 @@ class ApplicationHostForm(forms.ModelForm):
self.fields['dns_credential'].required = False
if self.is_reserved:
self.fields['hostname'].disabled = True
self.fields['certificate_fallback'].disabled = True
self.helper = FormHelper()
self.helper.layout = Layout(
@@ -124,8 +130,12 @@ class ApplicationHostForm(forms.ModelForm):
css_class='row'
),
Div(
Div('certificate_type', css_class='col-xl-6'),
Div('dns_credential', css_class='col-xl-6'),
Div('certificate_type', css_class='col-md-12'),
Div('dns_credential', css_class='col-md-12'),
css_class='row'
),
Div(
Div('certificate_fallback', css_class='col-md-12'),
css_class='row'
),
Div(
@@ -0,0 +1,23 @@
# Generated by Django 5.2.19 on 2026-11-18 20:17
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('app_gateway', '0010_set_self_signed_for_ip_hosts'),
]
operations = [
migrations.AddField(
model_name='applicationhost',
name='certificate_fallback',
field=models.BooleanField(default=True),
),
migrations.AddField(
model_name='applicationhost',
name='certificate_generation',
field=models.PositiveIntegerField(default=0),
),
]
@@ -0,0 +1,18 @@
# Generated by Django 5.2.19 on 2026-11-18 20:27
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('app_gateway', '0011_host_certificate_fallback_and_generation'),
]
operations = [
migrations.AlterField(
model_name='applicationhost',
name='certificate_type',
field=models.CharField(choices=[('http_challenge', 'Trusted certificate (HTTP validation)'), ('dns_challenge', 'Trusted certificate (DNS validation)'), ('self_signed', 'Self-signed certificate')], default='http_challenge', max_length=16),
),
]
+6 -3
View File
@@ -9,9 +9,9 @@ from gatekeeper.models import GatekeeperGroup, AuthMethod, _unique_slug
RESERVED_APP_NAME = 'wireguard_webadmin'
CERTIFICATE_TYPE_CHOICES = (
('self_signed', _('Self-signed')),
('http_challenge', _('HTTP challenge')),
('dns_challenge', _('DNS challenge')),
('http_challenge', _('Trusted certificate (HTTP validation)')),
('dns_challenge', _('Trusted certificate (DNS validation)')),
('self_signed', _('Self-signed certificate')),
)
@@ -70,6 +70,9 @@ class ApplicationHost(models.Model):
dns_credential = models.ForeignKey(
DnsChallengeCredential, on_delete=models.PROTECT, null=True, blank=True, related_name='hosts'
)
certificate_fallback = models.BooleanField(default=True)
# Incremented to request a new ACME attempt; exported so every Caddy instance applies it
certificate_generation = models.PositiveIntegerField(default=0)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
+1
View File
@@ -14,6 +14,7 @@ urlpatterns = [
# Application Hosts
path('host/manage/', views.view_manage_application_host, name='manage_application_host'),
path('host/delete/', views.view_delete_application_host, name='delete_application_host'),
path('host/retry_certificate/', views.view_retry_application_host_certificate, name='retry_application_host_certificate'),
# Access Policies
path('policy/select-type/', views.view_select_policy_type, name='select_policy_type'),
+34 -5
View File
@@ -3,7 +3,7 @@ import os
from django.conf import settings
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.db.models import ProtectedError
from django.db.models import F, ProtectedError
from django.shortcuts import render, get_object_or_404, redirect
from django.urls import reverse
from django.utils.translation import gettext as _
@@ -183,11 +183,12 @@ def view_manage_application_host(request):
<h5>Certificate</h5>
<p>Defines how the TLS certificate for this hostname is issued.</p>
<ul>
<li><strong>Self-signed</strong>: A certificate issued by the internal Caddy CA. Browsers will show a security warning.</li>
<li><strong>HTTP challenge</strong>: A trusted ACME certificate validated through HTTP. The hostname must point to this server and ports 80/443 must be reachable from the internet.</li>
<li><strong>DNS challenge</strong>: A trusted ACME certificate validated through a DNS record created using the selected DNS credential. The server does not need to be reachable from the internet.</li>
<li><strong>Trusted certificate (HTTP validation)</strong>: Issued through ACME (e.g. Let's Encrypt) and validated over HTTP. The hostname must point to this server and ports 80/443 must be reachable from the internet.</li>
<li><strong>Trusted certificate (DNS validation)</strong>: Issued through ACME and validated with a DNS record created using the selected DNS credential. The server does not need to be reachable from the internet.</li>
<li><strong>Self-signed certificate</strong>: Issued by the internal Caddy CA. Browsers will show a security warning.</li>
</ul>
<p>If an ACME certificate cannot be issued, a self-signed certificate is used until the next successful attempt.</p>
<p>When the fallback option is enabled and a trusted certificate cannot be issued, a self-signed certificate is used until the next successful attempt. Without the fallback, the connection fails until the certificate is issued.</p>
<p>Use <strong>Retry certificate</strong> in the host list to request a new attempt right away, for example after fixing the DNS or firewall.</p>
''')
if is_reserved:
form_description_content += _('''
@@ -471,6 +472,34 @@ def view_delete_application_route(request):
return render(request, 'generic_delete_confirmation.html', context)
@login_required
@require_POST
def view_retry_application_host_certificate(request):
if not UserAcl.objects.filter(user=request.user).filter(user_level__gte=50).exists():
return render(request, 'access_denied.html', {'page_title': _('Access Denied')})
application_host = get_object_or_404(ApplicationHost, uuid=request.GET.get('uuid'))
redirect_url = reverse('view_application') + f'?uuid={application_host.application.uuid}#hosts'
if application_host.certificate_type == 'self_signed':
messages.error(request, _('This host uses a self-signed certificate.'))
return redirect(redirect_url)
if not settings.CADDY_ENABLED:
messages.error(request, _('This action is not available because Caddy is not enabled.'))
return redirect(redirect_url)
ApplicationHost.objects.filter(pk=application_host.pk).update(certificate_generation=F('certificate_generation') + 1)
export_caddy_config('/caddy_json_export/')
if settings.DEBUG:
export_caddy_config(os.path.join(settings.BASE_DIR, 'containers', 'caddy', 'config_files'))
messages.success(request, _(
'A new certificate will be requested for %(hostname)s. The current configuration was exported.'
) % {'hostname': application_host.hostname})
return redirect(redirect_url)
@login_required
def view_manage_dns_credential(request):
if not UserAcl.objects.filter(user=request.user).filter(user_level__gte=50).exists():