Files
coulobre/src/coulobre/node_probe.py
T

66 lines
2.1 KiB
Python

import json
import re
from importlib.metadata import version
PROBE = {"version": 1, "operation": "probe"}
PROBE_BYTES = (json.dumps(PROBE) + "\n").encode()
PROBE_LIMIT = 4096
def probe_identity(role):
return PROBE | {"role": role, "release": version("coulobre-panel")}
def check_receiver(role):
if role == "dns":
from .dns_node_apply import RNDC, ZONE_DIRECTORY, run
from .node_files import require_root_directory
require_root_directory(ZONE_DIRECTORY)
configuration = run("named-checkconf", "-p", "-x")
if not re.search(r"(?m)^\s*allow-new-zones\s+yes;", configuration):
raise ValueError(
"Enable allow-new-zones in the BIND9 configuration."
)
run(*RNDC, "status")
else:
from .mail_node_apply import (
DOVECOT_USERS,
POSTFIX_DOMAINS,
POSTFIX_MAILBOXES,
check_services,
)
from .node_files import check_root_file
_, auth_group, _ = check_services()
for path in (POSTFIX_DOMAINS, POSTFIX_MAILBOXES):
check_root_file(path, mode=0o644)
check_root_file(DOVECOT_USERS, mode=0o640)
if DOVECOT_USERS.lstat().st_gid != auth_group:
raise ValueError("Check the group of the Dovecot lookup file.")
def verify_probe(body, role):
try:
if len(body) > PROBE_LIMIT:
raise ValueError
reply = json.loads(body)
if (
not isinstance(reply, dict)
or type(reply.get("version")) is not int
):
raise ValueError
expected = probe_identity(role)
if any(reply.get(field) != value for field, value in expected.items()):
raise ValueError
except (ValueError, RecursionError):
raise ValueError(
f"Expected a Coulobre {role} receiver on the same release."
) from None
if reply == expected | {"state": "configured", "problem": None}:
return
raise ValueError(
f"The {role} receiver is reachable but its services are not ready. "
"Check the destination socket service and node configuration."
)