Support nameserver addresses in new zone templates
Coulobre Panel CI / Checks (Python 3.12) (pull_request) Canceled after 0s
Coulobre Panel CI / Checks (Python 3.14) (pull_request) Canceled after 0s

This commit is contained in:
parkel committed 2026-11-24 13:34:43 +01:00
1 parent 5293e9497d
commit c0dd02e7a6
5 files changed
+97 -10

No files matched your search

+8 -4
View File
@@ -275,12 +275,16 @@ class DnsZone:
address_names = {record.name for record in self.addresses} address_names = {record.name for record in self.addresses}
if any(not in_zone(name, self.domain) for name in address_names): if any(not in_zone(name, self.domain) for name in address_names):
raise ValueError("Address records must belong to the zone.") raise ValueError("Address records must belong to the zone.")
if any( missing_nameservers = [
in_zone(name, self.domain) and name not in address_names name
for name in nameservers 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( 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_mail_records(address_names)
self.check_extra_records(address_names) self.check_extra_records(address_names)
+10 -2
View File
@@ -5,7 +5,7 @@ from django.core.exceptions import ValidationError
from django.db import IntegrityError, transaction from django.db import IntegrityError, transaction
from django.utils import timezone 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 coulobre.web_node import WebSite
from .dns_defaults import zone_defaults 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, wildcard=defaults.include_wildcard if wildcard is None else wildcard,
addresses=defaults.addresses, 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 = () mx = txt = ()
if managed_mail: if managed_mail:
public_key = ( public_key = (
+22 -4
View File
@@ -1,6 +1,7 @@
from ipaddress import ip_address from ipaddress import ip_address
from django import forms from django import forms
from django.conf import settings
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.mixins import UserPassesTestMixin from django.contrib.auth.mixins import UserPassesTestMixin
from django.urls import reverse_lazy from django.urls import reverse_lazy
@@ -39,8 +40,9 @@ class DnsDefaultsForm(forms.Form):
widget=forms.Textarea(attrs={"rows": 4}), widget=forms.Textarea(attrs={"rows": 4}),
help_text=( help_text=(
"Enter one record per line, for example _verification TXT abc123. " "Enter one record per line, for example _verification TXT abc123. "
"Use @ for the new zone name. " "Use @ for the new zone name. To give a configured nameserver "
"Enter fully qualified hostnames for record targets." "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): def clean_records(self):
rows = [] rows = []
nameservers = (
self.cleaned_data.get("nameservers")
or settings.HOSTING_NAMESERVERS
)
for line in self.cleaned_data["records"].splitlines(): for line in self.cleaned_data["records"].splitlines():
parts = line.split(maxsplit=2) parts = line.split(maxsplit=2)
if not parts: if not parts:
continue continue
if len(parts) != 3 or parts[0].endswith("."): if len(parts) != 3:
raise forms.ValidationError( raise forms.ValidationError(
"Enter a relative name, a record type and a value." "Enter a relative name, a record type and a value."
) )
name, kind, value = parts 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") normalize_record_rows(rows, "preview.example")
return rows return rows
@@ -9,6 +9,9 @@
<form method="post" class="editor"> <form method="post" class="editor">
{% csrf_token %} {% csrf_token %}
{{ form.as_div }} {{ form.as_div }}
{% if form.setup_dns and request.user.is_superuser %}
<p class="heading-meta"><a href="{% url 'panel:dns_defaults' %}">Review DNS zone template</a></p>
{% endif %}
<div class="form-actions"> <div class="form-actions">
<button class="button primary" type="submit">{{ submit_label }}</button> <button class="button primary" type="submit">{{ submit_label }}</button>
<a class="button secondary" href="{% if account %}{{ account.get_absolute_url }}{% else %}{% url 'panel:accounts' %}{% endif %}">Cancel</a> <a class="button secondary" href="{% if account %}{{ account.get_absolute_url }}{% else %}{% url 'panel:accounts' %}{% endif %}">Cancel</a>
@@ -67,6 +67,60 @@ class HostingAccountSetupTests(TestCase):
self.assertContains(response, "Copy website DNS records") self.assertContains(response, "Copy website DNS records")
self.assertContains(response, "existing.example.test A 192.0.2.21") 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): def test_guided_setup_applies_template_and_preserves_existing_zone(self):
self.client.force_login(self.operator) self.client.force_login(self.operator)
template_url = reverse("panel:dns_defaults") template_url = reverse("panel:dns_defaults")