From 90fecb1a85677376406fafd8ae7431f21b3246e3 Mon Sep 17 00:00:00 2001 From: Peter van Arkel Date: Thu, 3 Dec 2026 12:09:14 +0100 Subject: [PATCH] Show DNS primary and secondary roles in Connected Nodes --- docs/deployment.md | 8 ++ src/coulobre/hosting/connected_inventory.py | 59 ++++++++++ src/coulobre/hosting/operator_tools.py | 17 ++- .../templates/hosting/connected_nodes.html | 14 ++- .../hosting/test_connected_inventory.py | 110 ++++++++++++++++++ src/coulobre/hosting/update_views.py | 10 ++ 6 files changed, 212 insertions(+), 6 deletions(-) create mode 100644 src/coulobre/hosting/connected_inventory.py create mode 100644 src/coulobre/hosting/test_connected_inventory.py diff --git a/docs/deployment.md b/docs/deployment.md index ddb5e27..b8a5782 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -3728,6 +3728,14 @@ then the secondary query IP, separated by a comma. Restart the panel and select **Check replication** from a DNS zone to compare live authoritative SOA serials. +**Connected Nodes** shows separately the primary publisher and the secondary +transfer address reported from a Coulobre-managed primary. For an older +primary that does not report its catalog configuration, the panel uses the +configured replication addresses if present. It does not infer a managed +secondary from public NS records. The address report alone cannot verify +the secondary's release or service health, which are shown as unchecked. +Use **Check replication** to verify zone answers and confirm transfer health. + ## Enable local operator actions The operator action worker handles allowlisted service reloads and restarts, diff --git a/src/coulobre/hosting/connected_inventory.py b/src/coulobre/hosting/connected_inventory.py new file mode 100644 index 0000000..d3dedc2 --- /dev/null +++ b/src/coulobre/hosting/connected_inventory.py @@ -0,0 +1,59 @@ +"""Show configured service roles separately from health observations.""" + +from ipaddress import ip_address + +ROLE_LABELS = { + "dns": "DNS primary", + "dns-secondary": "DNS secondary", + "mail": "Mail", +} + + +def host_key(host): + try: + return str(ip_address(host)) + except ValueError: + return host.removesuffix(".").lower() + + +def inventory_rows(nodes, replication_addresses): + rows = {} + secondaries = [] + observed_primary = False + + def add(host, role, release=None, state="not checked", *, local=False): + key = host_key(host) + row = rows.setdefault( + key, {"host": key, "local": False, "services": {}} + ) + row["local"] |= local + row["services"].setdefault( + role, + {"role": ROLE_LABELS[role], "release": release, "state": state}, + ) + + for node in nodes: + role = node["role"] + host = node["host"] + add( + host, + role, + node.get("release"), + node.get("state") or "unavailable", + local=node.get("local", False), + ) + if role == "dns": + observed_primary = True + peer = node.get("dns_secondary") + if peer and host_key(peer) != host_key(host): + secondaries.append(peer) + if not secondaries and replication_addresses: + primary, secondary = map(str, replication_addresses) + if not observed_primary: + add(primary, "dns") + secondaries.append(secondary) + for host in secondaries: + row = rows.get(host_key(host)) + if row is None or "dns" not in row["services"]: + add(host, "dns-secondary") + return rows.values() diff --git a/src/coulobre/hosting/operator_tools.py b/src/coulobre/hosting/operator_tools.py index b99bd1d..0641dec 100644 --- a/src/coulobre/hosting/operator_tools.py +++ b/src/coulobre/hosting/operator_tools.py @@ -26,9 +26,14 @@ from coulobre.operator_node import ( from coulobre.release_updates import UpdateError from .account_administration import OperatorAuthorization +from .connected_inventory import inventory_rows from .models import OperatorOperation from .tasks import OPERATOR_OPERATION_LEASE, run_operator_operation -from .update_views import inspect_linked_nodes, request_updater +from .update_views import ( + inspect_linked_nodes, + inspect_node_inventory, + request_updater, +) ACTION_LABELS = { "package-preview": "Check for package updates", @@ -274,8 +279,16 @@ class ConnectedNodesView(OperatorAuthorization, TemplateView): template_name = "hosting/connected_nodes.html" def get_context_data(self, **kwargs): + topology = inspect_node_inventory() + nodes = inventory_rows( + topology.get("nodes", []), settings.DNS_REPLICATION_ADDRESSES + ) return super().get_context_data( - topology=inspect_linked_nodes(), + topology=topology, + nodes=nodes, + has_secondary=any( + "dns-secondary" in node["services"] for node in nodes + ), deploy_enabled=remote_deployment.SOCKET.is_socket(), peering_available=bool(settings.PASSWORD_RESET_ORIGIN), **kwargs, diff --git a/src/coulobre/hosting/templates/hosting/connected_nodes.html b/src/coulobre/hosting/templates/hosting/connected_nodes.html index d27fea0..3b0b536 100644 --- a/src/coulobre/hosting/templates/hosting/connected_nodes.html +++ b/src/coulobre/hosting/templates/hosting/connected_nodes.html @@ -1,6 +1,7 @@ {% extends "hosting/workspace_page.html" %} {% block title %}Connected Nodes ยท Coulobre Panel{% endblock %} {% block page_title %}Connected Nodes{% endblock %} +{% block page_metadata %}

DNS and mail service nodes configured for this panel.

{% endblock %} {% block page_actions %} {% if deploy_enabled %}Install a remote server{% endif %} {% if peering_available %}Enrolled panels{% endif %} @@ -8,11 +9,16 @@ {% endblock %} {% block page_content %} {% if topology.problem %}

{{ topology.problem }}

{% endif %} -
+
- - {% for node in topology.nodes %} - {% empty %}{% endfor %} + + {% for node in nodes %} + + + + + {% empty %}{% endfor %}
RoleHostReleaseState
{{ node.role|upper }}{{ node.host }}{{ node.release|default:"Unknown" }}{{ node.state|capfirst }}
No linked mail or DNS receivers reported.
HostRoleObserved releaseState
{{ node.host }}{% if node.local %}
This panel
{% endif %}
{% for service in node.services.values %}
{{ service.role }}
{% endfor %}
{% for service in node.services.values %}
{{ service.release|default:"Unknown" }}
{% endfor %}
{% for service in node.services.values %}
{{ service.state|capfirst }}
{% endfor %}
No mail or DNS service nodes reported.
+ {% if has_secondary %}

A secondary can be listed before its release or replication status has been checked. Use Check replication on a DNS zone to compare authoritative answers.

{% endif %}
{% endblock %} diff --git a/src/coulobre/hosting/test_connected_inventory.py b/src/coulobre/hosting/test_connected_inventory.py new file mode 100644 index 0000000..5f2a37d --- /dev/null +++ b/src/coulobre/hosting/test_connected_inventory.py @@ -0,0 +1,110 @@ +from unittest.mock import patch + +from django.contrib.auth import get_user_model +from django.test import SimpleTestCase, TestCase, override_settings +from django.urls import reverse +from django_otp import DEVICE_ID_SESSION_KEY +from django_otp.plugins.otp_totp.models import TOTPDevice + +from coulobre.release_updates import UpdateError + +from .connected_inventory import inventory_rows +from .update_views import inspect_node_inventory + + +class ServiceInventoryTests(SimpleTestCase): + def test_grouping_keeps_roles_and_never_uses_an_ns_record(self): + rows = list( + inventory_rows( + [ + { + "host": "2001:0db8::1", + "role": "dns", + "release": "1.2.1", + "state": "configured", + "dns_secondary": "192.0.2.53", + "nameservers": ["unmanaged.example.test"], + }, + {"host": "2001:db8::1", "role": "mail"}, + {"host": "192.0.2.53", "role": "mail"}, + ], + ("192.0.2.52", "198.51.100.53"), + ) + ) + self.assertEqual( + [row["host"] for row in rows], ["2001:db8::1", "192.0.2.53"] + ) + self.assertEqual(list(rows[0]["services"]), ["dns", "mail"]) + secondary = rows[1]["services"]["dns-secondary"] + self.assertEqual(secondary["state"], "not checked") + self.assertIsNone(secondary["release"]) + + def test_older_updater_uses_the_existing_linked_nodes_api(self): + expected = {"nodes": [{"role": "dns", "host": "192.0.2.52"}]} + with patch( + "coulobre.hosting.update_views.request_updater", + side_effect=[ + UpdateError("The update request is invalid."), + expected, + ], + ) as updater: + self.assertEqual(inspect_node_inventory(), expected) + self.assertEqual( + [call.args[0]["action"] for call in updater.call_args_list], + ["node-inventory", "linked-nodes"], + ) + + +class ConnectedNodesViewTests(TestCase): + def setUp(self): + user = get_user_model().objects.create_superuser("operator") + device = TOTPDevice.objects.create(user=user, name="test") + self.client.force_login(user) + session = self.client.session + session[DEVICE_ID_SESSION_KEY] = device.persistent_id + session.save() + + @override_settings(DNS_REPLICATION_ADDRESSES=()) + def test_local_and_remote_primaries_show_discovered_secondaries(self): + for host, local in ( + ("panel.example.test", True), + ("192.0.2.52", False), + ): + with ( + self.subTest(local=local), + patch( + "coulobre.hosting.operator_tools.inspect_node_inventory", + return_value={ + "nodes": [ + { + "host": host, + "role": "dns", + "state": "configured", + "release": "1.2.1", + "dns_secondary": "192.0.2.53", + "local": local, + } + ] + }, + ), + ): + response = self.client.get(reverse("panel:connected_nodes")) + self.assertContains(response, host) + self.assertContains(response, "DNS primary") + self.assertContains(response, "192.0.2.53") + self.assertContains(response, "DNS secondary") + self.assertContains(response, "Not checked") + if local: + self.assertContains(response, "This panel") + + @override_settings(DNS_REPLICATION_ADDRESSES=("192.0.2.52", "192.0.2.53")) + def test_configured_pair_remains_visible_without_live_status(self): + with patch( + "coulobre.hosting.operator_tools.inspect_node_inventory", + return_value={"problem": "Inventory unavailable"}, + ): + response = self.client.get(reverse("panel:connected_nodes")) + self.assertContains(response, "Inventory unavailable") + self.assertContains(response, "192.0.2.52") + self.assertContains(response, "192.0.2.53") + self.assertContains(response, "Unknown", count=2) diff --git a/src/coulobre/hosting/update_views.py b/src/coulobre/hosting/update_views.py index 3fc7a7a..79269a3 100644 --- a/src/coulobre/hosting/update_views.py +++ b/src/coulobre/hosting/update_views.py @@ -57,6 +57,16 @@ def inspect_linked_nodes(): } +def inspect_node_inventory(): + try: + return request_updater({"action": "node-inventory"}, timeout=10) + except UpdateError: + # Older updaters only implement linked-nodes. + return inspect_linked_nodes() + except (OSError, ValueError): + return {"problem": "The configured service inventory is unavailable."} + + @method_decorator(never_cache, name="dispatch") class OperatorUpdatesAccess(UserPassesTestMixin): def test_func(self):