mirror of
https://github.com/eduardogsilva/wireguard_webadmin.git
synced 2026-11-27 07:06:03 +01:00
Allow issuing a new self-signed certificate
This commit is contained in:
@@ -190,6 +190,9 @@ def build_tls_data(worker=None):
|
|||||||
|
|
||||||
if worker is not None:
|
if worker is not None:
|
||||||
if application.pk in served_application_ids:
|
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
|
hosts[hostname] = host_entry
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|||||||
@@ -332,6 +332,27 @@ class CaddyTlsStateTests(TestCase):
|
|||||||
self.assertFalse(os.path.isdir(fallback_dir))
|
self.assertFalse(os.path.isdir(fallback_dir))
|
||||||
self.assertTrue(self.apply({'certificate_type': 'dns_challenge', 'certificate_file': 'b' * 64}))
|
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):
|
def test_only_acme_certificates_are_exported(self):
|
||||||
tls_data = {'hosts': {
|
tls_data = {'hosts': {
|
||||||
'exported.example.test': {'certificate_type': 'dns_challenge', 'export_certificate': True},
|
'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']
|
warnings = [message.message for message in get_messages(response.wsgi_request) if message.level_tag == 'warning']
|
||||||
self.assertEqual(len(warnings), 1)
|
self.assertEqual(len(warnings), 1)
|
||||||
self.assertIn('1 hosts', warnings[0])
|
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])
|
||||||
|
|||||||
@@ -609,10 +609,6 @@ def view_retry_application_host_certificate(request):
|
|||||||
application_host = get_object_or_404(ApplicationHost, uuid=request.GET.get('uuid'))
|
application_host = get_object_or_404(ApplicationHost, uuid=request.GET.get('uuid'))
|
||||||
redirect_url = reverse('view_application') + f'?uuid={application_host.application.uuid}#hosts'
|
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:
|
if not settings.CADDY_ENABLED:
|
||||||
messages.error(request, _('This action is not available because Caddy is not enabled.'))
|
messages.error(request, _('This action is not available because Caddy is not enabled.'))
|
||||||
return redirect(redirect_url)
|
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)
|
ApplicationHost.objects.filter(pk=application_host.pk).update(certificate_generation=F('certificate_generation') + 1)
|
||||||
apply_caddy_config()
|
apply_caddy_config()
|
||||||
|
|
||||||
messages.success(request, _(
|
if application_host.certificate_type == 'self_signed':
|
||||||
'A new certificate will be requested for %(hostname)s. The current configuration was exported.'
|
message = _('A new self-signed certificate will be issued for %(hostname)s. The current configuration was exported.')
|
||||||
) % {'hostname': application_host.hostname})
|
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)
|
return redirect(redirect_url)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -416,6 +416,14 @@ def has_acme_certificate(host):
|
|||||||
return False
|
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():
|
def sync_tls_state():
|
||||||
"""
|
"""
|
||||||
Compare the certificate settings of the last generated Caddyfile with the ones currently applied.
|
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)
|
os.path.join(CERTIFICATES_DIR, INTERNAL_ISSUER_DIR_NAME, host)
|
||||||
):
|
):
|
||||||
continue
|
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:
|
if CLUSTER_NODE:
|
||||||
# Certificates received from the primary server replace cached ones only after a restart
|
# Certificates received from the primary server replace cached ones only after a restart
|
||||||
if host_state[0] == "certificate_file":
|
if host_state[0] == "certificate_file":
|
||||||
|
|||||||
Reference in New Issue
Block a user