diff --git a/src/coulobre/dns_node.py b/src/coulobre/dns_node.py index fba689f..24951b9 100644 --- a/src/coulobre/dns_node.py +++ b/src/coulobre/dns_node.py @@ -275,12 +275,16 @@ class DnsZone: address_names = {record.name for record in self.addresses} if any(not in_zone(name, self.domain) for name in address_names): raise ValueError("Address records must belong to the zone.") - if any( - in_zone(name, self.domain) and name not in address_names + missing_nameservers = [ + name for name in nameservers - ): + if in_zone(name, self.domain) and name not in address_names + ] + if missing_nameservers: + missing = ", ".join(missing_nameservers) raise ValueError( - "Each in-zone nameserver needs an address record." + "Each in-zone nameserver needs an address record. " + f"Add A or AAAA records for {missing}." ) self.check_mail_records(address_names) self.check_extra_records(address_names) diff --git a/src/coulobre/hosting/account_setup.py b/src/coulobre/hosting/account_setup.py index a01f327..f86f59d 100644 --- a/src/coulobre/hosting/account_setup.py +++ b/src/coulobre/hosting/account_setup.py @@ -5,7 +5,7 @@ from django.core.exceptions import ValidationError from django.db import IntegrityError, transaction from django.utils import timezone -from coulobre.dns_node import AddressRecord +from coulobre.dns_node import AddressRecord, in_zone from coulobre.web_node import WebSite from .dns_defaults import zone_defaults @@ -100,7 +100,15 @@ def default_dns_zone(account, *, www=None, wildcard=None, managed_mail=False): wildcard=defaults.include_wildcard if wildcard is None else wildcard, addresses=defaults.addresses, ) - extra = normalize_record_rows(defaults.records, domain) + extra = normalize_record_rows( + [ + row + for row in defaults.records + if not row["name"].endswith(".") + or in_zone(row["name"][:-1], domain) + ], + domain, + ) mx = txt = () if managed_mail: public_key = ( diff --git a/src/coulobre/hosting/dns_defaults_views.py b/src/coulobre/hosting/dns_defaults_views.py index 2a1f9ac..d23404c 100644 --- a/src/coulobre/hosting/dns_defaults_views.py +++ b/src/coulobre/hosting/dns_defaults_views.py @@ -1,6 +1,7 @@ from ipaddress import ip_address from django import forms +from django.conf import settings from django.contrib import messages from django.contrib.auth.mixins import UserPassesTestMixin from django.urls import reverse_lazy @@ -39,8 +40,9 @@ class DnsDefaultsForm(forms.Form): widget=forms.Textarea(attrs={"rows": 4}), help_text=( "Enter one record per line, for example _verification TXT abc123. " - "Use @ for the new zone name. " - "Enter fully qualified hostnames for record targets." + "Use @ for the new zone name. To give a configured nameserver " + "an address in its own zone, enter its hostname with a trailing " + "dot, for example ns1.example.net. A 192.0.2.10." ), ) @@ -73,16 +75,32 @@ class DnsDefaultsForm(forms.Form): def clean_records(self): rows = [] + nameservers = ( + self.cleaned_data.get("nameservers") + or settings.HOSTING_NAMESERVERS + ) for line in self.cleaned_data["records"].splitlines(): parts = line.split(maxsplit=2) if not parts: continue - if len(parts) != 3 or parts[0].endswith("."): + if len(parts) != 3: raise forms.ValidationError( "Enter a relative name, a record type and a value." ) name, kind, value = parts - rows.append({"name": name, "type": kind.upper(), "value": value}) + kind = kind.upper() + if name.endswith("."): + try: + host = dns_name(name) + except ValueError as error: + raise forms.ValidationError(str(error)) from error + if kind not in {"A", "AAAA"} or host not in nameservers: + raise forms.ValidationError( + "Absolute record names are allowed for address " + "records of configured nameservers." + ) + name = f"{host}." + rows.append({"name": name, "type": kind, "value": value}) normalize_record_rows(rows, "preview.example") return rows diff --git a/src/coulobre/hosting/templates/hosting/account_form.html b/src/coulobre/hosting/templates/hosting/account_form.html index 372684d..827a64a 100644 --- a/src/coulobre/hosting/templates/hosting/account_form.html +++ b/src/coulobre/hosting/templates/hosting/account_form.html @@ -9,6 +9,9 @@
{% csrf_token %} {{ form.as_div }} + {% if form.setup_dns and request.user.is_superuser %} +

Review DNS zone template

+ {% endif %}
Cancel diff --git a/src/coulobre/hosting/test_account_setup.py b/src/coulobre/hosting/test_account_setup.py index 7935aaa..e6657f9 100644 --- a/src/coulobre/hosting/test_account_setup.py +++ b/src/coulobre/hosting/test_account_setup.py @@ -67,6 +67,60 @@ class HostingAccountSetupTests(TestCase): self.assertContains(response, "Copy website DNS records") self.assertContains(response, "existing.example.test A 192.0.2.21") + def test_nameserver_addresses_are_added_only_to_their_own_zone(self): + self.client.force_login(self.operator) + create = reverse("panel:account_create") + data = { + "name": "DNS provider", + "primary_domain": "example.test", + "setup_dns": "on", + } + missing = self.client.post(create, data) + self.assertContains( + missing, + "Add A or AAAA records for ns1.example.test, ns2.example.test.", + ) + self.assertFalse( + HostingAccount.objects.filter( + primary_domain="example.test" + ).exists() + ) + template = reverse("panel:dns_defaults") + self.assertContains( + self.client.post( + template, {"records": "other.example.test. A 192.0.2.40"} + ), + "of configured nameservers", + ) + self.assertRedirects( + self.client.post( + template, + { + "records": ( + "ns1.example.test. A 192.0.2.40\n" + "ns2.example.test. AAAA 2001:db8::40" + ), + }, + ), + template, + ) + for domain in ("example.test", "customer.example.org"): + response = self.client.post( + create, data | {"primary_domain": domain} + ) + account = HostingAccount.objects.get(primary_domain=domain) + self.assertRedirects(response, account.get_absolute_url()) + addresses = { + record.name: record.address + for record in account.dns_zones.get().spec.addresses + } + if domain == "example.test": + self.assertEqual(addresses["ns1.example.test"], "192.0.2.40") + self.assertEqual(addresses["ns2.example.test"], "2001:db8::40") + else: + self.assertNotIn("ns1.example.test", addresses) + self.assertNotIn("ns2.example.test", addresses) + def test_guided_setup_applies_template_and_preserves_existing_zone(self): self.client.force_login(self.operator) template_url = reverse("panel:dns_defaults")