Allow catalog updates on managed DNS primaries
Coulobre Panel CI / Checks (Python 3.12) (pull_request) Successful in 1m43s
Coulobre Panel CI / Checks (Python 3.14) (pull_request) Failing after 1m54s

This commit is contained in:
parkel committed 2026-12-03 01:09:42 +01:00
1 parent 9fd6fda782
commit 797f737619
3 files changed
+217 -5

No files matched your search

+22 -3
View File
@@ -3,6 +3,7 @@ import grp
import json import json
import os import os
import subprocess import subprocess
from contextlib import suppress
from pathlib import Path from pathlib import Path
from . import engine, roles from . import engine, roles
@@ -90,6 +91,8 @@ def setup(role, peer):
def install_configuration(role, peer): def install_configuration(role, peer):
from coulobre.dns_catalog import PATH
root = BIND / "named.conf" root = BIND / "named.conf"
engine.root_file(root, 0o644) engine.root_file(root, 0o644)
current = root.read_text() current = root.read_text()
@@ -113,11 +116,23 @@ def install_configuration(role, peer):
raise InstallError( raise InstallError(
"Use a separate DNS host without managed primary zones." "Use a separate DNS host without managed primary zones."
) )
engine.put_file(root, wanted, group=root.stat().st_gid, update=True) group = root.stat().st_gid
engine.put_file(root, wanted, group=group, update=True)
new_override = None
try: try:
engine.run( engine.run(
"named-checkconf", label="Validate the DNS catalog configuration" "named-checkconf", label="Validate the DNS catalog configuration"
) )
if role == "primary":
directory = engine.SYSTEMD / "coulobre-dns-apply@.service.d"
engine.root_directory(directory)
override = directory / "catalog.conf"
if not os.path.lexists(override):
new_override = override
engine.put_file(
override, f"[Service]\nReadWritePaths={PATH.parent}\n"
)
engine.systemctl("daemon-reload")
engine.run( engine.run(
"rndc", "rndc",
"-s", "-s",
@@ -125,8 +140,12 @@ def install_configuration(role, peer):
"reconfig", "reconfig",
label="Load the DNS catalog configuration", label="Load the DNS catalog configuration",
) )
except InstallError: except (OSError, InstallError):
engine.put_file(root, current, group=root.stat().st_gid, update=True) if new_override is not None and new_override.exists():
new_override.unlink()
with suppress(InstallError):
engine.systemctl("daemon-reload")
engine.put_file(root, current, group=group, update=True)
raise raise
+11 -2
View File
@@ -997,8 +997,11 @@ the provisioning socket is stopped or paused.
3. Stop the receiver socket and let instances of the corresponding apply 3. Stop the receiver socket and let instances of the corresponding apply
service finish. Stage the inventory, update the receiver to the target, service finish. Stage the inventory, update the receiver to the target,
then start its socket again. Replace `dns` with `mail` for a mail receiver. then start its socket again. For a managed DNS catalog primary, rerun
The staged receiver answers status probes and rejects apply requests. [primary setup](#set-up-a-managed-secondary-dns-server) with its existing
peer address before starting the socket. Replace `dns` with `mail` for a
mail receiver. The staged receiver answers status probes and rejects
apply requests.
```sh ```sh
sudo systemctl stop coulobre-dns-apply.socket sudo systemctl stop coulobre-dns-apply.socket
@@ -3687,6 +3690,12 @@ retirement update the catalog. The secondary uses authenticated BIND
transfers and persists its copy of each zone. Add both nameservers to transfers and persists its copy of each zone. Add both nameservers to
zone delegation and verify replies directly on both hosts. zone delegation and verify replies directly on both hosts.
On an existing catalog primary, rerun the primary command from the current
release using the same peer address. This grants the DNS apply service
write access to the catalog directory while retaining its filesystem
sandbox. It preserves the catalog, managed zones, and transfer key. The
secondary does not need this permission.
On the panel, set `COULOBRE_DNS_REPLICATION_ADDRESSES` with the primary On the panel, set `COULOBRE_DNS_REPLICATION_ADDRESSES` with the primary
then the secondary query IP, separated by a comma. Restart the panel then the secondary query IP, separated by a comma. Restart the panel
and select **Check replication** from a DNS zone to compare live and select **Check replication** from a DNS zone to compare live
+184
View File
@@ -0,0 +1,184 @@
import os
import tempfile
import unittest
from functools import partial
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import call, patch
from coulobre import dns_catalog, dns_node_apply
from deploy.panel_installer import engine, roles, secondary
from deploy.panel_installer.config import InstallError
class DNSCatalogSandboxTests(unittest.TestCase):
peer = "192.0.2.53"
def setUp(self):
directory = Path(self.enterContext(tempfile.TemporaryDirectory()))
bind = directory / "bind"
bind.mkdir()
systemd = directory / "systemd"
systemd.mkdir()
self.seed = bind / "coulobre-catalog" / "zone"
self.zones = bind / "coulobre"
self.zones.mkdir()
self.key = bind / "coulobre-transfer.key"
self.key.touch(mode=0o640)
self.config = bind / "named.conf"
self.config.write_text(roles.BIND_CONFIG)
self.override = (
systemd / "coulobre-dns-apply@.service.d" / "catalog.conf"
)
self.override_text = f"[Service]\nReadWritePaths={self.seed.parent}\n"
self.enterContext(
patch.multiple(secondary, BIND=bind, KEY_PATH=self.key)
)
self.enterContext(patch.object(dns_catalog, "PATH", self.seed))
self.enterContext(
patch.object(dns_node_apply, "ZONE_DIRECTORY", self.zones)
)
self.enterContext(patch.object(engine, "SYSTEMD", systemd))
self.enterContext(
patch.object(
secondary.grp,
"getgrnam",
return_value=SimpleNamespace(gr_gid=os.getgid()),
)
)
# Use the real file writer while running without root.
def directory_exists(path, mode=0o755):
path.mkdir(mode=mode, exist_ok=True)
self.enterContext(
patch.object(
engine, "root_directory", side_effect=directory_exists
)
)
self.enterContext(patch.object(engine, "root_file"))
self.enterContext(patch.object(engine.os, "fchown"))
self.enterContext(
patch.object(
engine, "put_file", partial(engine.put_file, group=os.getgid())
)
)
self.enterContext(patch("coulobre.node_files.locked_root_directory"))
self.run_command = self.enterContext(patch.object(engine, "run"))
def test_primary_can_write_only_the_catalog_directory(self):
secondary.setup("primary", self.peer)
self.assertEqual(self.override.read_text(), self.override_text)
self.assertEqual(self.override.stat().st_mode & 0o777, 0o644)
self.assertTrue(self.seed.exists())
self.assertEqual(
[args[0] for args, _ in self.run_command.call_args_list],
["named-checkconf", "systemctl", "rndc"],
)
self.assertIn(
call(
"systemctl",
"daemon-reload",
label="systemctl daemon-reload",
timeout=180,
),
self.run_command.call_args_list,
)
def test_existing_primary_without_override_gets_one(self):
self.config.write_text(secondary.configuration("primary", self.peer))
self.seed.parent.mkdir()
catalog = dns_catalog.render_catalog(17, {"site.test"})
self.seed.write_text(catalog)
secondary.setup("primary", self.peer)
self.assertEqual(self.override.read_text(), self.override_text)
self.assertEqual(self.seed.read_text(), catalog)
def test_existing_primary_keeps_zones_key_and_manual_override(self):
self.config.write_text(secondary.configuration("primary", self.peer))
self.seed.parent.mkdir()
self.seed.write_text(dns_catalog.render_catalog(17, {"site.test"}))
self.override.parent.mkdir()
self.override.write_text(self.override_text)
original_files = {
path: (path.read_bytes(), path.stat().st_ino)
for path in (self.config, self.key, self.seed, self.override)
}
secondary.setup("primary", self.peer)
secondary.setup("primary", self.peer)
for path, original in original_files.items():
self.assertEqual((path.read_bytes(), path.stat().st_ino), original)
def test_secondary_does_not_get_a_write_exception(self):
secondary.setup("secondary", self.peer)
self.assertFalse(self.override.parent.exists())
self.assertFalse(self.seed.exists())
self.assertEqual(
[args[0] for args, _ in self.run_command.call_args_list],
["named-checkconf", "rndc"],
)
def test_failure_removes_only_new_catalog_and_override(self):
def fail_load(command, *_args, **_kwargs):
if command == "rndc":
raise InstallError("BIND configuration rejected")
self.run_command.side_effect = fail_load
with self.assertRaisesRegex(
InstallError, "BIND configuration rejected"
):
secondary.setup("primary", self.peer)
self.assertEqual(self.config.read_text(), roles.BIND_CONFIG)
self.assertFalse(self.seed.exists())
self.assertFalse(self.override.exists())
self.assertEqual(
[args[0] for args, _ in self.run_command.call_args_list],
["named-checkconf", "systemctl", "rndc", "systemctl"],
)
def test_reload_failure_keeps_an_existing_catalog_and_override(self):
config = secondary.configuration("primary", self.peer)
self.config.write_text(config)
self.seed.parent.mkdir()
catalog = dns_catalog.render_catalog(17, {"site.test"})
self.seed.write_text(catalog)
self.override.parent.mkdir()
self.override.write_text(self.override_text)
def fail_reload(command, *_args, **_kwargs):
if command == "systemctl":
raise InstallError("Reload failed")
self.run_command.side_effect = fail_reload
with self.assertRaisesRegex(InstallError, "Reload failed"):
secondary.setup("primary", self.peer)
self.assertEqual(self.config.read_text(), config)
self.assertEqual(self.seed.read_text(), catalog)
self.assertEqual(self.override.read_text(), self.override_text)
self.assertEqual(
[args[0] for args, _ in self.run_command.call_args_list],
["named-checkconf", "systemctl"],
)
def test_conflicting_override_is_not_replaced(self):
self.override.parent.mkdir()
self.override.write_text("[Service]\nReadWritePaths=/custom\n")
with self.assertRaisesRegex(
InstallError, "Existing configuration differs"
):
secondary.setup("primary", self.peer)
self.assertEqual(
self.override.read_text(), "[Service]\nReadWritePaths=/custom\n"
)
self.assertEqual(self.config.read_text(), roles.BIND_CONFIG)
self.assertFalse(self.seed.exists())