109 lines
3.5 KiB
Python
109 lines
3.5 KiB
Python
import json
|
|
import re
|
|
from importlib.metadata import version
|
|
|
|
PROBE = {"version": 1, "operation": "probe"}
|
|
PROBE_BYTES = (json.dumps(PROBE) + "\n").encode()
|
|
STATUS_PROBE = PROBE | {"version": 2}
|
|
STATUS_PROBE_BYTES = (json.dumps(STATUS_PROBE) + "\n").encode()
|
|
PROBE_LIMIT = 4096
|
|
|
|
|
|
def probe_identity(role):
|
|
return PROBE | {"role": role, "release": version("coulobre-panel")}
|
|
|
|
|
|
def parse_status_probe(body, role):
|
|
from .release_updates import UpdateError, version_key
|
|
|
|
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
|
|
or reply["version"] not in {1, 2}
|
|
or reply.get("operation") != "probe"
|
|
or reply.get("role") != role
|
|
or reply.get("state")
|
|
not in {"configured", "maintenance", "failed"}
|
|
):
|
|
raise ValueError
|
|
version_key(reply.get("release"))
|
|
target = reply.get("upgrade_target")
|
|
if reply["state"] == "maintenance":
|
|
if reply["version"] != 2:
|
|
raise ValueError
|
|
version_key(target)
|
|
else:
|
|
target = None
|
|
except (ValueError, TypeError, UpdateError, RecursionError) as error:
|
|
raise ValueError(
|
|
"The receiver returned no trusted release status."
|
|
) from error
|
|
result = {
|
|
"role": role,
|
|
"protocol": reply["version"],
|
|
"release": reply["release"],
|
|
"state": reply["state"],
|
|
"upgrade_target": target,
|
|
}
|
|
if role == "mail" and reply.get("scoped_webmail") is True:
|
|
result["scoped_webmail"] = True
|
|
return result
|
|
|
|
|
|
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."
|
|
)
|