From ffd05e759bdccec75c2fb5b47114b335dcd56310 Mon Sep 17 00:00:00 2001 From: Peter van Arkel Date: Tue, 1 Dec 2026 10:03:38 +0100 Subject: [PATCH] Offer configured hosting addresses in DNS records --- docs/deployment.md | 4 +++ src/coulobre/hosting/dns_defaults.py | 16 ++++++++++ .../static/hosting/dns-record-editor.js | 29 +++++++++++++++++ src/coulobre/hosting/static/hosting/panel.css | 13 +++++++- .../templates/hosting/dns_record_editor.html | 11 ++++++- .../templates/hosting/dns_zone_form.html | 4 +-- src/coulobre/hosting/test_dns_zones.py | 31 ++++++++++++++++++- src/coulobre/hosting/views.py | 7 +++-- 8 files changed, 108 insertions(+), 7 deletions(-) diff --git a/docs/deployment.md b/docs/deployment.md index 34122a9..6a8d800 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -2305,6 +2305,10 @@ Each request captures the reviewed SOA serial and records. You can edit the zone while it publishes. Publish the newer revision after the current request finishes. Use **Retry publishing** if the current revision failed. +In the DNS editor, **Use hosting IP** fills an A or AAAA record with a +matching address from the effective DNS zone template. Choose an address +when more than one is available. You can edit the value before saving. + To let the customer edit a zone's records, enable **Delegate record editing** on that zone. They can also publish the zone when Coulobre publishing is enabled. The operator controls the authoritative diff --git a/src/coulobre/hosting/dns_defaults.py b/src/coulobre/hosting/dns_defaults.py index af20e69..e336228 100644 --- a/src/coulobre/hosting/dns_defaults.py +++ b/src/coulobre/hosting/dns_defaults.py @@ -1,3 +1,5 @@ +from ipaddress import ip_address + from django.conf import settings from .models import DnsZoneDefaults @@ -8,3 +10,17 @@ def zone_defaults(): saved.nameservers = saved.nameservers or settings.HOSTING_NAMESERVERS saved.addresses = saved.addresses or settings.HOSTING_PUBLIC_ADDRESSES return saved + + +def hosting_address_choices(): + choices = {"A": [], "AAAA": []} + for configured in zone_defaults().addresses: + try: + address = ip_address(configured) + except ValueError: + continue + addresses = choices["A" if address.version == 4 else "AAAA"] + value = str(address) + if value not in addresses: + addresses.append(value) + return choices diff --git a/src/coulobre/hosting/static/hosting/dns-record-editor.js b/src/coulobre/hosting/static/hosting/dns-record-editor.js index 67c137d..edd8ce8 100644 --- a/src/coulobre/hosting/static/hosting/dns-record-editor.js +++ b/src/coulobre/hosting/static/hosting/dns-record-editor.js @@ -8,6 +8,7 @@ const valueLabels = { MX: "Mail server hostname", TXT: "Text", SRV: "Priority, weight, port and target", CAA: "Flags, tag and value", }; +const hostingAddresses = JSON.parse(document.getElementById("dns-hosting-addresses").textContent); function numberRows() { Array.from(rows.children).forEach((row, index) => { @@ -23,6 +24,11 @@ function appendRecord(record) { const priority = row.querySelector("[data-record-priority]"); const priorityLabel = row.querySelector("[data-priority-label]"); const valueLabel = row.querySelector("[data-record-value-label]"); + const hostingAddress = row.querySelector("[data-hosting-address]"); + const onlyAddress = row.querySelector("[data-only-hosting-address]"); + const addressSelect = row.querySelector("[data-hosting-address-select]"); + const useAddress = row.querySelector("[data-use-hosting-address]"); + const missingAddress = row.querySelector("[data-missing-hosting-address]"); name.value = record.name; type.value = record.type; value.value = record.value; @@ -42,8 +48,31 @@ function appendRecord(record) { value.rows = type.value === "TXT" ? 4 : 1; value.classList.toggle("dns-txt-value", type.value === "TXT"); valueLabel.textContent = valueLabels[type.value]; + + const addresses = hostingAddresses[type.value]; + hostingAddress.hidden = !addresses; + if (!addresses) return; + addressSelect.replaceChildren(...addresses.map((address) => new Option(address, address))); + if (addresses.length > 1) { + addressSelect.add(new Option("Choose a hosting IP", ""), 0); + addressSelect.selectedIndex = 0; + } + onlyAddress.hidden = addresses.length !== 1; + onlyAddress.textContent = addresses.length === 1 ? addresses[0] : ""; + addressSelect.hidden = addresses.length < 2; + useAddress.hidden = addresses.length === 0; + useAddress.disabled = !addressSelect.value; + missingAddress.hidden = addresses.length > 0; + missingAddress.textContent = `No hosting ${valueLabels[type.value]} is configured.`; }; type.addEventListener("change", updateFields); + addressSelect.addEventListener("change", () => { + useAddress.disabled = !addressSelect.value; + }); + useAddress.addEventListener("click", () => { + value.value = addressSelect.value; + value.focus(); + }); row.querySelector("[data-remove-record]").addEventListener("click", () => { const nextFocus = row.nextElementSibling?.querySelector("input") || add; row.remove(); diff --git a/src/coulobre/hosting/static/hosting/panel.css b/src/coulobre/hosting/static/hosting/panel.css index 4738634..6611eb6 100644 --- a/src/coulobre/hosting/static/hosting/panel.css +++ b/src/coulobre/hosting/static/hosting/panel.css @@ -1061,8 +1061,19 @@ dd { display: flex; gap: 0.5rem; } -.dns-edit-value > label { flex: 1; } +.dns-record-value-field { flex: 1; min-width: 0; } .dns-edit-value > [data-priority-label] { flex: 0 0 5rem; } +.dns-hosting-address { + display: flex; + align-items: center; + flex-wrap: wrap; + gap: 0.4rem; + margin-top: 0.4rem; +} +.dns-hosting-address[hidden], .dns-hosting-address > [hidden] { display: none; } +.dns-hosting-address > select { width: auto; max-width: 100%; } +.dns-hosting-address > code { overflow-wrap: anywhere; } +.dns-hosting-address > .heading-meta { margin: 0; } .dns-edit-row textarea { display: block; min-height: 2.45rem; diff --git a/src/coulobre/hosting/templates/hosting/dns_record_editor.html b/src/coulobre/hosting/templates/hosting/dns_record_editor.html index f134600..0d0b20e 100644 --- a/src/coulobre/hosting/templates/hosting/dns_record_editor.html +++ b/src/coulobre/hosting/templates/hosting/dns_record_editor.html @@ -1,4 +1,5 @@ {{ form.editor_rows|json_script:"dns-record-data" }} +{{ hosting_addresses|json_script:"dns-hosting-addresses" }} {% if not form.is_bound or form.record_rows.value %} {% include "hosting/dns_record_editor.html" %} - + {% endif %}
diff --git a/src/coulobre/hosting/test_dns_zones.py b/src/coulobre/hosting/test_dns_zones.py index 8ed36c7..e2dffc9 100644 --- a/src/coulobre/hosting/test_dns_zones.py +++ b/src/coulobre/hosting/test_dns_zones.py @@ -7,7 +7,7 @@ from django.urls import reverse from coulobre.dns_node import MAX_SOA_SERIAL, DnsZone -from .models import HostingAccount, HostingDnsZone, Reseller +from .models import DnsZoneDefaults, HostingAccount, HostingDnsZone, Reseller class DnsZonePanelTests(TestCase): @@ -191,6 +191,35 @@ class DnsZonePanelTests(TestCase): self.client.get(self.account.get_absolute_url()), "2001:db8::21" ) + def test_address_suggestions_use_effective_dns_defaults_by_family(self): + zone = self.create_zone() + edit = reverse("panel:dns_zone_edit", args=[zone.pk]) + with self.settings(HOSTING_PUBLIC_ADDRESSES=()): + self.assertEqual( + self.client.get(edit).context["hosting_addresses"], + {"A": [], "AAAA": []}, + ) + with self.settings(HOSTING_PUBLIC_ADDRESSES=("192.0.2.90",)): + self.assertEqual( + self.client.get(self.add_url).context["hosting_addresses"], + {"A": ["192.0.2.90"], "AAAA": []}, + ) + DnsZoneDefaults.objects.create( + pk=1, + addresses=[ + "192.0.2.40", + "2001:db8:0::10", + "2001:db8::10", + "192.0.2.50", + ], + ) + response = self.client.get(edit) + self.assertContains(response, 'id="dns-hosting-addresses"') + self.assertEqual( + response.context["hosting_addresses"], + {"A": ["192.0.2.40", "192.0.2.50"], "AAAA": ["2001:db8::10"]}, + ) + def test_invalid_mail_records_leave_existing_zone_unchanged(self): zone = self.create_zone() edit = reverse("panel:dns_zone_edit", args=[zone.pk]) diff --git a/src/coulobre/hosting/views.py b/src/coulobre/hosting/views.py index e57abcc..14f22e6 100644 --- a/src/coulobre/hosting/views.py +++ b/src/coulobre/hosting/views.py @@ -72,7 +72,7 @@ from .backup_restores import ( retry_remote_mail_restore, ) from .backup_retention import change_backup_retention, protect_backup -from .dns_defaults import zone_defaults +from .dns_defaults import hosting_address_choices, zone_defaults from .dns_forms import ( DnsZoneEditForm, DnsZoneForm, @@ -2179,7 +2179,10 @@ class DnsZoneEditor(PermissionRequiredMixin, FormView): def get_context_data(self, **kwargs): return super().get_context_data( - account=self.account, domain=self.domain, **kwargs + account=self.account, + domain=self.domain, + hosting_addresses=hosting_address_choices(), + **kwargs, ) def get_success_url(self):