Publish local BIND9 zones from the panel
This commit is contained in:
1 parent
6e075c25f4
commit
70cb4de81b
14 files changed
+450
-19
No files matched your search
@@ -0,0 +1,152 @@
|
||||
import json
|
||||
from io import BytesIO
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.models import Permission
|
||||
from django.test import SimpleTestCase, TestCase, override_settings
|
||||
from django.urls import reverse
|
||||
|
||||
from coulobre.dns_node import DnsZone
|
||||
|
||||
from .models import Customer, DnsPublication, HostingAccount, HostingDnsZone
|
||||
from .node_client import publish_local_zone
|
||||
from .tasks import dispatch_dns_publications, publish_dns_zone
|
||||
|
||||
|
||||
@override_settings(LOCAL_DNS_PUBLISHING=True)
|
||||
class DnsPublicationTests(TestCase):
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
users = get_user_model().objects
|
||||
cls.operator = users.create_superuser("operator")
|
||||
cls.customer = users.create_user("customer")
|
||||
cls.customer.user_permissions.add(
|
||||
*Permission.objects.filter(content_type__app_label="hosting")
|
||||
)
|
||||
cls.account = HostingAccount.objects.create(
|
||||
name="DNS example",
|
||||
primary_domain="site.example",
|
||||
customer=Customer.objects.create(user=cls.customer),
|
||||
)
|
||||
cls.zone = HostingDnsZone.objects.create(
|
||||
account=cls.account,
|
||||
domain=cls.account.primary_domain,
|
||||
nameservers=["ns.provider.example"],
|
||||
responsible_name="hostmaster.site.example",
|
||||
addresses=[{"name": "site.example", "address": "192.0.2.1"}],
|
||||
)
|
||||
|
||||
def setUp(self):
|
||||
self.client.force_login(self.operator)
|
||||
self.url = reverse("panel:dns_zone_publish", args=[self.zone.pk])
|
||||
self.detail = self.account.get_absolute_url()
|
||||
|
||||
def test_queued_snapshot_survives_edits_and_the_new_revision_can_publish_next(self):
|
||||
self.assertContains(self.client.get(self.detail), "Publish zone")
|
||||
self.assertEqual(self.client.get(self.url).status_code, 405)
|
||||
for _ in range(2):
|
||||
self.assertRedirects(self.client.post(self.url, {"serial": 1}), self.detail)
|
||||
publication = DnsPublication.objects.get(zone=self.zone)
|
||||
self.assertContains(self.client.get(self.detail), "Queued serial 1")
|
||||
with patch.object(publish_dns_zone, "apply_async") as send:
|
||||
dispatch_dns_publications.run()
|
||||
dispatch_dns_publications.run()
|
||||
send.assert_called_once_with(
|
||||
args=[publication.pk], queue="coulobre-dns-publishing", retry=False
|
||||
)
|
||||
|
||||
edit = reverse("panel:dns_zone_edit", args=[self.zone.pk])
|
||||
self.assertRedirects(
|
||||
self.client.post(
|
||||
edit,
|
||||
{
|
||||
"serial": 1,
|
||||
"nameservers": "ns.provider.example",
|
||||
"responsible_name": "hostmaster.site.example",
|
||||
"addresses": "site.example 192.0.2.2",
|
||||
},
|
||||
),
|
||||
self.detail,
|
||||
)
|
||||
self.zone.refresh_from_db()
|
||||
self.assertEqual(self.zone.serial, 2)
|
||||
self.assertEqual(self.client.post(self.url, {"serial": 1}).status_code, 409)
|
||||
self.client.post(self.url, {"serial": 2})
|
||||
publication.refresh_from_db()
|
||||
self.assertEqual(publication.serial, 1)
|
||||
|
||||
with patch("coulobre.hosting.tasks.publish_local_zone") as apply:
|
||||
publish_dns_zone.run(publication.pk)
|
||||
published = apply.call_args.args[0]
|
||||
self.assertEqual(published.serial, 1)
|
||||
self.assertEqual(published.addresses[0].address, "192.0.2.1")
|
||||
detail = self.client.get(self.detail)
|
||||
self.assertContains(detail, "Published SOA serial 1")
|
||||
self.assertContains(detail, "Publish zone")
|
||||
|
||||
self.assertRedirects(self.client.post(self.url, {"serial": 2}), self.detail)
|
||||
publication.refresh_from_db()
|
||||
self.assertEqual(publication.published_serial, 1)
|
||||
self.assertEqual(publication.serial, 2)
|
||||
with patch(
|
||||
"coulobre.hosting.tasks.publish_local_zone",
|
||||
side_effect=ValueError("BIND9 rejected the update."),
|
||||
):
|
||||
publish_dns_zone.run(publication.pk)
|
||||
detail = self.client.get(self.detail)
|
||||
self.assertContains(detail, "Published SOA serial 1")
|
||||
self.assertContains(detail, "Publishing failed for serial 2")
|
||||
self.assertContains(detail, "Retry publishing")
|
||||
self.assertRedirects(self.client.post(self.url, {"serial": 2}), self.detail)
|
||||
with patch("coulobre.hosting.tasks.publish_local_zone") as apply:
|
||||
publish_dns_zone.run(publication.pk)
|
||||
apply.assert_called_once_with(self.zone.spec)
|
||||
self.assertContains(self.client.get(self.detail), "Published SOA serial 2")
|
||||
|
||||
def test_customer_and_operator_without_publish_permission_cannot_queue(self):
|
||||
viewer = get_user_model().objects.create_user("viewer")
|
||||
viewer.user_permissions.add(
|
||||
*Permission.objects.filter(
|
||||
codename__in=("view_hostingaccount", "view_hostingdnszone")
|
||||
)
|
||||
)
|
||||
for user in (self.customer, viewer):
|
||||
with self.subTest(user=user):
|
||||
self.client.force_login(user)
|
||||
self.assertNotContains(self.client.get(self.detail), "Publish zone")
|
||||
self.assertEqual(
|
||||
self.client.post(self.url, {"serial": 1}).status_code, 403
|
||||
)
|
||||
self.assertFalse(DnsPublication.objects.exists())
|
||||
|
||||
@override_settings(LOCAL_DNS_PUBLISHING=False)
|
||||
def test_disabled_publishing_keeps_manifest_downloads_available(self):
|
||||
self.assertNotContains(self.client.get(self.detail), "Publish zone")
|
||||
self.assertEqual(self.client.post(self.url, {"serial": 1}).status_code, 404)
|
||||
manifest_url = reverse("panel:dns_zone_manifest", args=[self.zone.pk])
|
||||
self.assertEqual(
|
||||
self.client.get(manifest_url).json(), self.zone.spec.manifest()
|
||||
)
|
||||
|
||||
|
||||
class DnsPublicationSocketTests(SimpleTestCase):
|
||||
def test_receipt_must_identify_the_exact_soa_serial(self):
|
||||
zone = DnsZone(
|
||||
"site.example", 1, ("ns.provider.example",), "hostmaster.site.example"
|
||||
)
|
||||
reply = zone.manifest_identity() | {"state": "configured", "problem": None}
|
||||
with patch("coulobre.hosting.node_client.socket.socket") as socket:
|
||||
connection = socket.return_value.__enter__.return_value
|
||||
read = connection.makefile.return_value.__enter__
|
||||
read.side_effect = (
|
||||
BytesIO(json.dumps(reply).encode()),
|
||||
BytesIO(json.dumps(reply | {"serial": True}).encode()),
|
||||
)
|
||||
publish_local_zone(zone)
|
||||
connection.connect.assert_called_once_with("/run/coulobre-dns-apply.sock")
|
||||
self.assertEqual(
|
||||
json.loads(connection.sendall.call_args.args[0]), zone.manifest()
|
||||
)
|
||||
with self.assertRaisesMessage(ValueError, "invalid response for this zone"):
|
||||
publish_local_zone(zone)
|
||||
Reference in new issue
Block a user