Merge pull request 'Show managed DNS secondaries in Connected Nodes' (#253) from feature/dns-secondary-inventory into main
Reviewed-on: #253
This commit was merged in pull request #253.
This commit is contained in:
commit
16199b9d1a
13 files changed
+422
-11
No files matched your search
@@ -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
|
and select **Check replication** from a DNS zone to compare live
|
||||||
authoritative SOA serials.
|
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
|
## Enable local operator actions
|
||||||
|
|
||||||
The operator action worker handles allowlisted service reloads and restarts,
|
The operator action worker handles allowlisted service reloads and restarts,
|
||||||
|
|||||||
@@ -1,17 +1,51 @@
|
|||||||
import hashlib
|
import hashlib
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
from ipaddress import ip_address
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from .node_files import read_root_file, replace_configuration
|
from .node_files import (
|
||||||
|
read_root_file,
|
||||||
|
replace_configuration,
|
||||||
|
require_root_directory,
|
||||||
|
)
|
||||||
|
|
||||||
CATALOG = "catalog.coulobre.invalid"
|
CATALOG = "catalog.coulobre.invalid"
|
||||||
PATH = Path("/etc/bind/coulobre-catalog/zone")
|
PATH = Path("/etc/bind/coulobre-catalog/zone")
|
||||||
KEY = "coulobre-transfer"
|
KEY = "coulobre-transfer"
|
||||||
|
BIND_CONFIG = Path("/etc/bind/named.conf")
|
||||||
HEADER = re.compile(r"; Coulobre catalog (\d+)")
|
HEADER = re.compile(r"; Coulobre catalog (\d+)")
|
||||||
MEMBER = re.compile(r"[a-f0-9]{32}\.zones 0 IN PTR ([a-z0-9.-]+)\.")
|
MEMBER = re.compile(r"[a-f0-9]{32}\.zones 0 IN PTR ([a-z0-9.-]+)\.")
|
||||||
|
|
||||||
|
|
||||||
|
def managed_secondary_address():
|
||||||
|
"""Read the notify address from a managed DNS primary."""
|
||||||
|
try:
|
||||||
|
require_root_directory(BIND_CONFIG.parent)
|
||||||
|
if BIND_CONFIG.stat().st_size > 64 * 1024:
|
||||||
|
return None
|
||||||
|
configuration = read_root_file(BIND_CONFIG, mode=0o644)
|
||||||
|
primary = (
|
||||||
|
f'zone "{CATALOG}" {{\n'
|
||||||
|
f' type primary; file "{PATH}";\n'
|
||||||
|
f' allow-query {{ key "{KEY}"; }};\n'
|
||||||
|
"};\n"
|
||||||
|
)
|
||||||
|
if not configuration.startswith(
|
||||||
|
f'include "/etc/bind/{KEY}.key";\n'
|
||||||
|
) or not configuration.endswith(primary):
|
||||||
|
return None
|
||||||
|
candidates = re.findall(
|
||||||
|
rf'(?m)^ also-notify \{{ ([0-9a-fA-F:.]+) key "{KEY}"; \}};$',
|
||||||
|
configuration,
|
||||||
|
)
|
||||||
|
if len(candidates) != 1:
|
||||||
|
return None
|
||||||
|
return str(ip_address(candidates[0]))
|
||||||
|
except (OSError, ValueError):
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def render_catalog(serial, members):
|
def render_catalog(serial, members):
|
||||||
records = [
|
records = [
|
||||||
f"; Coulobre catalog {serial}",
|
f"; Coulobre catalog {serial}",
|
||||||
|
|||||||
@@ -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()
|
||||||
@@ -26,9 +26,14 @@ from coulobre.operator_node import (
|
|||||||
from coulobre.release_updates import UpdateError
|
from coulobre.release_updates import UpdateError
|
||||||
|
|
||||||
from .account_administration import OperatorAuthorization
|
from .account_administration import OperatorAuthorization
|
||||||
|
from .connected_inventory import inventory_rows
|
||||||
from .models import OperatorOperation
|
from .models import OperatorOperation
|
||||||
from .tasks import OPERATOR_OPERATION_LEASE, run_operator_operation
|
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 = {
|
ACTION_LABELS = {
|
||||||
"package-preview": "Check for package updates",
|
"package-preview": "Check for package updates",
|
||||||
@@ -274,8 +279,16 @@ class ConnectedNodesView(OperatorAuthorization, TemplateView):
|
|||||||
template_name = "hosting/connected_nodes.html"
|
template_name = "hosting/connected_nodes.html"
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
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(
|
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(),
|
deploy_enabled=remote_deployment.SOCKET.is_socket(),
|
||||||
peering_available=bool(settings.PASSWORD_RESET_ORIGIN),
|
peering_available=bool(settings.PASSWORD_RESET_ORIGIN),
|
||||||
**kwargs,
|
**kwargs,
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
{% extends "hosting/workspace_page.html" %}
|
{% extends "hosting/workspace_page.html" %}
|
||||||
{% block title %}Connected Nodes · Coulobre Panel{% endblock %}
|
{% block title %}Connected Nodes · Coulobre Panel{% endblock %}
|
||||||
{% block page_title %}Connected Nodes{% endblock %}
|
{% block page_title %}Connected Nodes{% endblock %}
|
||||||
|
{% block page_metadata %}<p class="heading-meta">DNS and mail service nodes configured for this panel.</p>{% endblock %}
|
||||||
{% block page_actions %}
|
{% block page_actions %}
|
||||||
{% if deploy_enabled %}<a class="button primary" href="{% url 'panel:remote_deploy' %}">Install a remote server</a>{% endif %}
|
{% if deploy_enabled %}<a class="button primary" href="{% url 'panel:remote_deploy' %}">Install a remote server</a>{% endif %}
|
||||||
{% if peering_available %}<a class="button secondary" href="{% url 'panel:panel_peers' %}">Enrolled panels</a>{% endif %}
|
{% if peering_available %}<a class="button secondary" href="{% url 'panel:panel_peers' %}">Enrolled panels</a>{% endif %}
|
||||||
@@ -8,11 +9,16 @@
|
|||||||
{% endblock %}
|
{% endblock %}
|
||||||
{% block page_content %}
|
{% block page_content %}
|
||||||
{% if topology.problem %}<p class="notice">{{ topology.problem }}</p>{% endif %}
|
{% if topology.problem %}<p class="notice">{{ topology.problem }}</p>{% endif %}
|
||||||
<section class="surface" aria-label="Mail and DNS receivers">
|
<section class="surface" aria-label="DNS and mail service nodes">
|
||||||
<div class="table-scroll"><table>
|
<div class="table-scroll"><table>
|
||||||
<thead><tr><th scope="col">Role</th><th scope="col">Host</th><th scope="col">Release</th><th scope="col">State</th></tr></thead>
|
<thead><tr><th scope="col">Host</th><th scope="col">Role</th><th scope="col">Observed release</th><th scope="col">State</th></tr></thead>
|
||||||
<tbody>{% for node in topology.nodes %}<tr><th scope="row">{{ node.role|upper }}</th><td>{{ node.host }}</td><td>{{ node.release|default:"Unknown" }}</td><td>{{ node.state|capfirst }}</td></tr>
|
<tbody>{% for node in nodes %}<tr>
|
||||||
{% empty %}<tr><td colspan="4">No linked mail or DNS receivers reported.</td></tr>{% endfor %}</tbody>
|
<th scope="row"><code>{{ node.host }}</code>{% if node.local %}<div class="muted">This panel</div>{% endif %}</th>
|
||||||
|
<td>{% for service in node.services.values %}<div>{{ service.role }}</div>{% endfor %}</td>
|
||||||
|
<td>{% for service in node.services.values %}<div>{{ service.release|default:"Unknown" }}</div>{% endfor %}</td>
|
||||||
|
<td>{% for service in node.services.values %}<div>{{ service.state|capfirst }}</div>{% endfor %}</td>
|
||||||
|
</tr>{% empty %}<tr><td colspan="4">No mail or DNS service nodes reported.</td></tr>{% endfor %}</tbody>
|
||||||
</table></div>
|
</table></div>
|
||||||
|
{% if has_secondary %}<p class="section-text muted">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.</p>{% endif %}
|
||||||
</section>
|
</section>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
@@ -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)
|
||||||
@@ -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")
|
@method_decorator(never_cache, name="dispatch")
|
||||||
class OperatorUpdatesAccess(UserPassesTestMixin):
|
class OperatorUpdatesAccess(UserPassesTestMixin):
|
||||||
def test_func(self):
|
def test_func(self):
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ def read_probe(role, payload):
|
|||||||
return stream.read(PROBE_LIMIT + 1)
|
return stream.read(PROBE_LIMIT + 1)
|
||||||
|
|
||||||
|
|
||||||
def receiver_status(role, host):
|
def receiver_status(role, host, *, inventory=False, local=False):
|
||||||
status = {
|
status = {
|
||||||
"role": role,
|
"role": role,
|
||||||
"host": host,
|
"host": host,
|
||||||
@@ -46,7 +46,8 @@ def receiver_status(role, host):
|
|||||||
"upgrade_target": None,
|
"upgrade_target": None,
|
||||||
}
|
}
|
||||||
try:
|
try:
|
||||||
relay_is_installed(role)
|
if not local:
|
||||||
|
relay_is_installed(role)
|
||||||
body = read_probe(role, STATUS_PROBE_BYTES)
|
body = read_probe(role, STATUS_PROBE_BYTES)
|
||||||
try:
|
try:
|
||||||
report = parse_status_probe(body, role)
|
report = parse_status_probe(body, role)
|
||||||
@@ -54,6 +55,8 @@ def receiver_status(role, host):
|
|||||||
report = parse_status_probe(read_probe(role, PROBE_BYTES), role)
|
report = parse_status_probe(read_probe(role, PROBE_BYTES), role)
|
||||||
except (ValueError, OSError):
|
except (ValueError, OSError):
|
||||||
return status
|
return status
|
||||||
|
if not inventory:
|
||||||
|
report.pop("dns_secondary", None)
|
||||||
return status | report
|
return status | report
|
||||||
|
|
||||||
|
|
||||||
@@ -80,6 +83,22 @@ def linked_roles(plan):
|
|||||||
return roles
|
return roles
|
||||||
|
|
||||||
|
|
||||||
|
def node_inventory(plan):
|
||||||
|
nodes = [
|
||||||
|
receiver_status(role, host, inventory=True)
|
||||||
|
for role, host in linked_roles(plan)
|
||||||
|
]
|
||||||
|
if plan["dns"]["mode"] == "local":
|
||||||
|
nodes.insert(
|
||||||
|
0,
|
||||||
|
receiver_status(
|
||||||
|
"dns", plan["hostname"], inventory=True, local=True
|
||||||
|
)
|
||||||
|
| {"local": True},
|
||||||
|
)
|
||||||
|
return nodes
|
||||||
|
|
||||||
|
|
||||||
def staged_for_release(node, target):
|
def staged_for_release(node, target):
|
||||||
return (
|
return (
|
||||||
node["protocol"] == 2
|
node["protocol"] == 2
|
||||||
|
|||||||
@@ -15,7 +15,12 @@ from pathlib import Path
|
|||||||
from pwd import getpwnam
|
from pwd import getpwnam
|
||||||
from tempfile import TemporaryDirectory
|
from tempfile import TemporaryDirectory
|
||||||
|
|
||||||
from .linked_updates import linked_roles, receiver_status, staged_for_release
|
from .linked_updates import (
|
||||||
|
linked_roles,
|
||||||
|
node_inventory,
|
||||||
|
receiver_status,
|
||||||
|
staged_for_release,
|
||||||
|
)
|
||||||
from .release_updates import (
|
from .release_updates import (
|
||||||
SHA256,
|
SHA256,
|
||||||
Release,
|
Release,
|
||||||
@@ -468,7 +473,7 @@ def read_request(peer):
|
|||||||
raise UpdateError("Only the Coulobre panel can request updates.")
|
raise UpdateError("Only the Coulobre panel can request updates.")
|
||||||
with peer.makefile("rb") as stream:
|
with peer.makefile("rb") as stream:
|
||||||
request = json.loads(stream.readline(2048))
|
request = json.loads(stream.readline(2048))
|
||||||
if request == {"action": "linked-nodes"}:
|
if request in ({"action": "linked-nodes"}, {"action": "node-inventory"}):
|
||||||
return request
|
return request
|
||||||
if not isinstance(request, dict):
|
if not isinstance(request, dict):
|
||||||
raise UpdateError("The update request is invalid.")
|
raise UpdateError("The update request is invalid.")
|
||||||
@@ -602,6 +607,19 @@ def handle_apt_update(peer, request):
|
|||||||
|
|
||||||
def handle_request(peer):
|
def handle_request(peer):
|
||||||
request = read_request(peer)
|
request = read_request(peer)
|
||||||
|
if request.get("action") == "node-inventory":
|
||||||
|
try:
|
||||||
|
nodes = node_inventory(installation_plan())
|
||||||
|
except (UpdateError, OSError, TypeError, KeyError, ValueError):
|
||||||
|
reply(
|
||||||
|
peer,
|
||||||
|
accepted=True,
|
||||||
|
nodes=[],
|
||||||
|
problem="The configured service inventory is unavailable.",
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
reply(peer, accepted=True, nodes=nodes)
|
||||||
|
return
|
||||||
if request.get("action") == "linked-nodes":
|
if request.get("action") == "linked-nodes":
|
||||||
try:
|
try:
|
||||||
nodes = linked_status(installation_plan())
|
nodes = linked_status(installation_plan())
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
|
from contextlib import suppress
|
||||||
from importlib.metadata import version
|
from importlib.metadata import version
|
||||||
|
from ipaddress import ip_address
|
||||||
|
|
||||||
PROBE = {"version": 1, "operation": "probe"}
|
PROBE = {"version": 1, "operation": "probe"}
|
||||||
PROBE_BYTES = (json.dumps(PROBE) + "\n").encode()
|
PROBE_BYTES = (json.dumps(PROBE) + "\n").encode()
|
||||||
@@ -51,6 +53,16 @@ def parse_status_probe(body, role):
|
|||||||
}
|
}
|
||||||
if role == "mail" and reply.get("scoped_webmail") is True:
|
if role == "mail" and reply.get("scoped_webmail") is True:
|
||||||
result["scoped_webmail"] = True
|
result["scoped_webmail"] = True
|
||||||
|
address = reply.get("dns_secondary")
|
||||||
|
if (
|
||||||
|
role == "dns"
|
||||||
|
and reply["version"] == 2
|
||||||
|
and isinstance(address, str)
|
||||||
|
and len(address) <= 45
|
||||||
|
and "%" not in address
|
||||||
|
):
|
||||||
|
with suppress(ValueError):
|
||||||
|
result["dns_secondary"] = str(ip_address(address))
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -51,6 +51,12 @@ def serve_apply_request(
|
|||||||
with authorized_request(probe_role):
|
with authorized_request(probe_role):
|
||||||
check_receiver(probe_role)
|
check_receiver(probe_role)
|
||||||
target = rollout_target(probe_role)
|
target = rollout_target(probe_role)
|
||||||
|
if payload == STATUS_PROBE and probe_role == "dns":
|
||||||
|
from .dns_catalog import managed_secondary_address
|
||||||
|
|
||||||
|
secondary = managed_secondary_address()
|
||||||
|
if secondary:
|
||||||
|
report["dns_secondary"] = secondary
|
||||||
if payload == STATUS_PROBE and probe_role == "mail":
|
if payload == STATUS_PROBE and probe_role == "mail":
|
||||||
from .receiver_scope import LOCAL_WORKERS, socket_peer
|
from .receiver_scope import LOCAL_WORKERS, socket_peer
|
||||||
|
|
||||||
|
|||||||
@@ -26,6 +26,95 @@ def receiver(role, release, *, protocol=2, state="maintenance", target=TARGET):
|
|||||||
|
|
||||||
|
|
||||||
class LinkedUpdateTests(TestCase):
|
class LinkedUpdateTests(TestCase):
|
||||||
|
def test_inventory_extension_does_not_change_direct_upgrade_receivers(
|
||||||
|
self,
|
||||||
|
):
|
||||||
|
body = json.dumps(
|
||||||
|
receiver("dns", TARGET) | {"dns_secondary": "2001:0db8::53"}
|
||||||
|
).encode()
|
||||||
|
with (
|
||||||
|
patch.object(linked_updates, "relay_is_installed"),
|
||||||
|
patch.object(linked_updates, "read_probe", return_value=body),
|
||||||
|
):
|
||||||
|
direct = linked_updates.receiver_status("dns", "192.0.2.52")
|
||||||
|
inventory = linked_updates.receiver_status(
|
||||||
|
"dns", "192.0.2.52", inventory=True
|
||||||
|
)
|
||||||
|
self.assertNotIn("dns_secondary", direct)
|
||||||
|
self.assertEqual(inventory["dns_secondary"], "2001:db8::53")
|
||||||
|
self.assertTrue(linked_updates.staged_for_release(direct, TARGET))
|
||||||
|
|
||||||
|
def test_inventory_probe_can_check_local_primary_without_a_relay(self):
|
||||||
|
plan = {
|
||||||
|
"hostname": "panel.example.test",
|
||||||
|
"dns": {"mode": "local"},
|
||||||
|
"mail": {"mode": "off"},
|
||||||
|
}
|
||||||
|
response = receiver("dns", TARGET, state="configured")
|
||||||
|
response["dns_secondary"] = "192.0.2.53"
|
||||||
|
with (
|
||||||
|
patch.object(
|
||||||
|
linked_updates,
|
||||||
|
"read_probe",
|
||||||
|
return_value=json.dumps(response).encode(),
|
||||||
|
),
|
||||||
|
patch.object(linked_updates, "relay_is_installed") as relay,
|
||||||
|
patch.object(linked_updates, "linked_roles", return_value=[]),
|
||||||
|
):
|
||||||
|
nodes = linked_updates.node_inventory(plan)
|
||||||
|
relay.assert_not_called()
|
||||||
|
self.assertEqual(len(nodes), 1)
|
||||||
|
self.assertTrue(nodes[0]["local"])
|
||||||
|
self.assertEqual(nodes[0]["host"], "panel.example.test")
|
||||||
|
self.assertEqual(nodes[0]["dns_secondary"], "192.0.2.53")
|
||||||
|
|
||||||
|
def test_status_ignores_invalid_secondary_and_legacy_extensions(self):
|
||||||
|
for protocol, address in (
|
||||||
|
(1, "192.0.2.53"),
|
||||||
|
(2, "dns.example.test"),
|
||||||
|
(2, "2001:db8::1%eth0"),
|
||||||
|
):
|
||||||
|
with self.subTest(protocol=protocol, address=address):
|
||||||
|
result = parse_status_probe(
|
||||||
|
json.dumps(
|
||||||
|
receiver(
|
||||||
|
"dns",
|
||||||
|
TARGET,
|
||||||
|
state="configured",
|
||||||
|
protocol=protocol,
|
||||||
|
)
|
||||||
|
| {"dns_secondary": address}
|
||||||
|
).encode(),
|
||||||
|
"dns",
|
||||||
|
)
|
||||||
|
self.assertNotIn("dns_secondary", result)
|
||||||
|
|
||||||
|
def test_inventory_is_only_in_the_status_probe(self):
|
||||||
|
for payload in (PROBE, STATUS_PROBE):
|
||||||
|
with (
|
||||||
|
patch(
|
||||||
|
"coulobre.node_requests.sys.stdin",
|
||||||
|
buffer=io.BytesIO(json.dumps(payload).encode()),
|
||||||
|
),
|
||||||
|
patch("sys.stdout", new_callable=io.StringIO) as output,
|
||||||
|
patch("coulobre.node_probe.check_receiver"),
|
||||||
|
patch(
|
||||||
|
"coulobre.node_maintenance.rollout_target",
|
||||||
|
return_value=None,
|
||||||
|
),
|
||||||
|
patch(
|
||||||
|
"coulobre.dns_catalog.managed_secondary_address",
|
||||||
|
return_value="192.0.2.53",
|
||||||
|
) as read_topology,
|
||||||
|
):
|
||||||
|
code = serve_apply_request(1, Mock(), Mock(), probe_role="dns")
|
||||||
|
self.assertEqual(code, 0)
|
||||||
|
self.assertEqual(read_topology.called, payload == STATUS_PROBE)
|
||||||
|
self.assertEqual(
|
||||||
|
"dns_secondary" in json.loads(output.getvalue()),
|
||||||
|
payload == STATUS_PROBE,
|
||||||
|
)
|
||||||
|
|
||||||
def test_only_target_version_in_maintenance_enables_update(self):
|
def test_only_target_version_in_maintenance_enables_update(self):
|
||||||
def staged(body):
|
def staged(body):
|
||||||
return linked_updates.staged_for_release(
|
return linked_updates.staged_for_release(
|
||||||
|
|||||||
@@ -66,6 +66,33 @@ class DNSCatalogSandboxTests(unittest.TestCase):
|
|||||||
self.enterContext(patch("coulobre.node_files.locked_root_directory"))
|
self.enterContext(patch("coulobre.node_files.locked_root_directory"))
|
||||||
self.run_command = self.enterContext(patch.object(engine, "run"))
|
self.run_command = self.enterContext(patch.object(engine, "run"))
|
||||||
|
|
||||||
|
def test_inventory_reads_only_the_managed_primary_and_notify_address(self):
|
||||||
|
primary = secondary.configuration("primary", "2001:0db8::53")
|
||||||
|
primary = primary.replace(
|
||||||
|
str(self.key), "/etc/bind/coulobre-transfer.key"
|
||||||
|
)
|
||||||
|
replica = secondary.configuration("secondary", self.peer)
|
||||||
|
replica = replica.replace(
|
||||||
|
str(self.key), "/etc/bind/coulobre-transfer.key"
|
||||||
|
)
|
||||||
|
for content, expected in (
|
||||||
|
(primary, "2001:db8::53"),
|
||||||
|
(replica, None),
|
||||||
|
(roles.BIND_CONFIG, None),
|
||||||
|
(primary.replace("2001:0db8::53", "bad.example"), None),
|
||||||
|
):
|
||||||
|
with (
|
||||||
|
self.subTest(expected=expected),
|
||||||
|
patch.object(dns_catalog, "BIND_CONFIG", self.config),
|
||||||
|
patch.object(dns_catalog, "require_root_directory"),
|
||||||
|
patch.object(
|
||||||
|
dns_catalog, "read_root_file", return_value=content
|
||||||
|
),
|
||||||
|
):
|
||||||
|
self.assertEqual(
|
||||||
|
dns_catalog.managed_secondary_address(), expected
|
||||||
|
)
|
||||||
|
|
||||||
def test_primary_can_write_only_the_catalog_directory(self):
|
def test_primary_can_write_only_the_catalog_directory(self):
|
||||||
secondary.setup("primary", self.peer)
|
secondary.setup("primary", self.peer)
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user