Report managed DNS secondary addresses for service inventory
This commit is contained in:
1 parent
62c1e9bf54
commit
805305f24b
7 files changed
+210
-5
No files matched your search
@@ -1,17 +1,51 @@
|
||||
import hashlib
|
||||
import os
|
||||
import re
|
||||
from ipaddress import ip_address
|
||||
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"
|
||||
PATH = Path("/etc/bind/coulobre-catalog/zone")
|
||||
KEY = "coulobre-transfer"
|
||||
BIND_CONFIG = Path("/etc/bind/named.conf")
|
||||
HEADER = re.compile(r"; Coulobre catalog (\d+)")
|
||||
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):
|
||||
records = [
|
||||
f"; Coulobre catalog {serial}",
|
||||
|
||||
@@ -36,7 +36,7 @@ def read_probe(role, payload):
|
||||
return stream.read(PROBE_LIMIT + 1)
|
||||
|
||||
|
||||
def receiver_status(role, host):
|
||||
def receiver_status(role, host, *, inventory=False, local=False):
|
||||
status = {
|
||||
"role": role,
|
||||
"host": host,
|
||||
@@ -46,7 +46,8 @@ def receiver_status(role, host):
|
||||
"upgrade_target": None,
|
||||
}
|
||||
try:
|
||||
relay_is_installed(role)
|
||||
if not local:
|
||||
relay_is_installed(role)
|
||||
body = read_probe(role, STATUS_PROBE_BYTES)
|
||||
try:
|
||||
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)
|
||||
except (ValueError, OSError):
|
||||
return status
|
||||
if not inventory:
|
||||
report.pop("dns_secondary", None)
|
||||
return status | report
|
||||
|
||||
|
||||
@@ -80,6 +83,22 @@ def linked_roles(plan):
|
||||
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):
|
||||
return (
|
||||
node["protocol"] == 2
|
||||
|
||||
@@ -15,7 +15,12 @@ from pathlib import Path
|
||||
from pwd import getpwnam
|
||||
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 (
|
||||
SHA256,
|
||||
Release,
|
||||
@@ -468,7 +473,7 @@ def read_request(peer):
|
||||
raise UpdateError("Only the Coulobre panel can request updates.")
|
||||
with peer.makefile("rb") as stream:
|
||||
request = json.loads(stream.readline(2048))
|
||||
if request == {"action": "linked-nodes"}:
|
||||
if request in ({"action": "linked-nodes"}, {"action": "node-inventory"}):
|
||||
return request
|
||||
if not isinstance(request, dict):
|
||||
raise UpdateError("The update request is invalid.")
|
||||
@@ -602,6 +607,19 @@ def handle_apt_update(peer, request):
|
||||
|
||||
def handle_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":
|
||||
try:
|
||||
nodes = linked_status(installation_plan())
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import json
|
||||
import re
|
||||
from contextlib import suppress
|
||||
from importlib.metadata import version
|
||||
from ipaddress import ip_address
|
||||
|
||||
PROBE = {"version": 1, "operation": "probe"}
|
||||
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:
|
||||
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
|
||||
|
||||
|
||||
|
||||
@@ -51,6 +51,12 @@ def serve_apply_request(
|
||||
with authorized_request(probe_role):
|
||||
check_receiver(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":
|
||||
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):
|
||||
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 staged(body):
|
||||
return linked_updates.staged_for_release(
|
||||
|
||||
Reference in new issue
Block a user