From 8682dc8ce66ce4c7bd3ce4ed24f7be124913f2a7 Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Sun, 22 Nov 2026 12:13:16 -0300 Subject: [PATCH] Allow issuing a new self-signed certificate --- app_gateway/caddy_config_export.py | 3 ++ app_gateway/tests.py | 63 ++++++++++++++++++++++++++++++ app_gateway/views.py | 12 +++--- containers/caddy/process_config.py | 16 ++++++++ 4 files changed, 87 insertions(+), 7 deletions(-) diff --git a/app_gateway/caddy_config_export.py b/app_gateway/caddy_config_export.py index ae79c5c..8f788f6 100644 --- a/app_gateway/caddy_config_export.py +++ b/app_gateway/caddy_config_export.py @@ -190,6 +190,9 @@ def build_tls_data(worker=None): if worker is not None: if application.pk in served_application_ids: + # Workers issue their own self-signed certificates, so they also follow the renewal requests + if application_host.certificate_type == 'self_signed': + host_entry['certificate_generation'] = application_host.certificate_generation hosts[hostname] = host_entry continue diff --git a/app_gateway/tests.py b/app_gateway/tests.py index ddf9af3..653c119 100644 --- a/app_gateway/tests.py +++ b/app_gateway/tests.py @@ -332,6 +332,27 @@ class CaddyTlsStateTests(TestCase): self.assertFalse(os.path.isdir(fallback_dir)) self.assertTrue(self.apply({'certificate_type': 'dns_challenge', 'certificate_file': 'b' * 64})) + def test_self_signed_renewal_issues_a_new_certificate(self): + self.apply({'certificate_type': 'self_signed'}) + internal_dir = self.store_certificate('local', 'app.example.test') + self.assertFalse(self.apply({'certificate_type': 'self_signed'})) + self.assertTrue(os.path.isdir(internal_dir)) + self.assertTrue(self.apply({'certificate_type': 'self_signed', 'certificate_generation': 1})) + self.assertFalse(os.path.isdir(internal_dir)) + + def test_switching_to_self_signed_keeps_the_internal_certificate(self): + self.apply({'certificate_type': 'http_challenge'}) + internal_dir = self.store_certificate('local', 'app.example.test') + self.assertFalse(self.apply({'certificate_type': 'self_signed', 'certificate_generation': 1})) + self.assertTrue(os.path.isdir(internal_dir)) + + def test_cluster_node_renews_its_self_signed_certificate(self): + with patch.object(process_config, 'CLUSTER_NODE', True): + self.apply({'certificate_type': 'self_signed'}) + internal_dir = self.store_certificate('local', 'app.example.test') + self.assertTrue(self.apply({'certificate_type': 'self_signed', 'certificate_generation': 1})) + self.assertFalse(os.path.isdir(internal_dir)) + def test_only_acme_certificates_are_exported(self): tls_data = {'hosts': { 'exported.example.test': {'certificate_type': 'dns_challenge', 'export_certificate': True}, @@ -605,3 +626,45 @@ class DnsDelegationViewTests(DnsDelegationTestMixin, TestCase): warnings = [message.message for message in get_messages(response.wsgi_request) if message.level_tag == 'warning'] self.assertEqual(len(warnings), 1) self.assertIn('1 hosts', warnings[0]) + + +@override_settings(ALLOWED_HOSTS=FIXTURE_ALLOWED_HOSTS + ['testserver'], CADDY_ENABLED=True, AUTO_APPLY=False) +class SelfSignedRenewalTests(TestCase): + def setUp(self): + temp_dir = tempfile.TemporaryDirectory() + self.addCleanup(temp_dir.cleanup) + patcher = patch('app_gateway.caddy_config_export.CADDY_EXPORT_DIR', temp_dir.name) + patcher.start() + self.addCleanup(patcher.stop) + ClusterSettings.objects.update_or_create(name='cluster_settings', defaults={'enabled': True}) + self.worker = Worker.objects.create(name='node-a') + self.application = Application.objects.create( + display_name='Wiki', upstream='http://10.0.0.30:8080', all_workers=True + ) + self.credential = DnsChallengeCredential.objects.create( + display_name='Cloudflare', provider='cloudflare', credentials={'api_token': CLOUDFLARE_TOKEN} + ) + self.self_signed = ApplicationHost.objects.create( + application=self.application, hostname='internal.example.test', certificate_type='self_signed', + certificate_generation=3, + ) + ApplicationHost.objects.create( + application=self.application, hostname='dns.example.test', certificate_type='dns_challenge', + dns_credential=self.credential, certificate_generation=5, + ) + user = User.objects.create_user(username='gateway-admin', password='unused-test-password') + UserAcl.objects.create(user=user, user_level=50) + self.client.force_login(user) + + def test_workers_follow_renewals_of_self_signed_certificates_only(self): + hosts = build_tls_data(self.worker)['hosts'] + self.assertEqual(hosts['internal.example.test']['certificate_generation'], 3) + self.assertNotIn('certificate_generation', hosts['dns.example.test']) + + def test_retry_requests_a_new_self_signed_certificate(self): + response = self.client.post(f'/app_gateway/host/retry_certificate/?uuid={self.self_signed.uuid}') + self.self_signed.refresh_from_db() + self.assertEqual(self.self_signed.certificate_generation, 4) + success = [message.message for message in get_messages(response.wsgi_request) if message.level_tag == 'success'] + self.assertEqual(len(success), 1) + self.assertIn('self-signed', success[0]) diff --git a/app_gateway/views.py b/app_gateway/views.py index da3a8b1..79d0d35 100644 --- a/app_gateway/views.py +++ b/app_gateway/views.py @@ -609,10 +609,6 @@ def view_retry_application_host_certificate(request): 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) @@ -620,9 +616,11 @@ def view_retry_application_host_certificate(request): ApplicationHost.objects.filter(pk=application_host.pk).update(certificate_generation=F('certificate_generation') + 1) apply_caddy_config() - messages.success(request, _( - 'A new certificate will be requested for %(hostname)s. The current configuration was exported.' - ) % {'hostname': application_host.hostname}) + if application_host.certificate_type == 'self_signed': + message = _('A new self-signed certificate will be issued for %(hostname)s. The current configuration was exported.') + else: + message = _('A new certificate will be requested for %(hostname)s. The current configuration was exported.') + messages.success(request, message % {'hostname': application_host.hostname}) return redirect(redirect_url) diff --git a/containers/caddy/process_config.py b/containers/caddy/process_config.py index 6eb6bd2..37c583e 100644 --- a/containers/caddy/process_config.py +++ b/containers/caddy/process_config.py @@ -416,6 +416,14 @@ def has_acme_certificate(host): return False +def is_self_signed_renewal(previous_host_state, host_state): + """A new self-signed certificate was requested for a host that keeps using the internal CA.""" + return ( + previous_host_state is not None and previous_host_state[0] == "self_signed" == host_state[0] + and previous_host_state[4] != host_state[4] + ) + + def sync_tls_state(): """ Compare the certificate settings of the last generated Caddyfile with the ones currently applied. @@ -442,6 +450,14 @@ def sync_tls_state(): os.path.join(CERTIFICATES_DIR, INTERNAL_ISSUER_DIR_NAME, host) ): continue + if is_self_signed_renewal(previous_host_state, host_state): + # The internal CA only issues a new certificate when the cached one is gone + internal_certificate_dir = os.path.join(CERTIFICATES_DIR, INTERNAL_ISSUER_DIR_NAME, host) + if os.path.isdir(internal_certificate_dir): + shutil.rmtree(internal_certificate_dir) + print(f"Removed self-signed certificate for {host}", file=sys.stderr) + restart_required = True + continue if CLUSTER_NODE: # Certificates received from the primary server replace cached ones only after a restart if host_state[0] == "certificate_file":