mirror of
https://github.com/chatmail/relay.git
synced 2026-11-27 07:19:37 +00:00
refactor(dns): remove ssh calls from cmdeploy.dns
The zone file and the report on it need no relay to talk to, so that other tools like "cmrelay" can import them without cmdeploy's ssh machinery. It's also independently a good idea to have clean cuts between "functional" and "IO" code, reflecting in tests that don't need to use mocks anymore.
This commit is contained in:
@@ -103,7 +103,10 @@ def run_cmd(args, out):
|
||||
if args.config.ipv4_relay:
|
||||
args.dns_check_disabled = True
|
||||
if not args.dns_check_disabled:
|
||||
remote_data = dns.get_initial_remote_data(sshexec, args.config.mail_domain)
|
||||
remote_data = sshexec.logged(
|
||||
call=remote.rdns.perform_initial_checks,
|
||||
kwargs=dict(mail_domain=args.config.mail_domain),
|
||||
)
|
||||
if not dns.check_initial_remote_data(remote_data, strict_tls=strict_tls, print=out.red):
|
||||
return 1
|
||||
|
||||
@@ -162,7 +165,10 @@ def dns_cmd(args, out):
|
||||
sshexec = get_sshexec(ssh_host, verbose=args.verbose)
|
||||
tls_cert_mode = args.config.tls_cert_mode
|
||||
strict_tls = tls_cert_mode == "acme"
|
||||
remote_data = dns.get_initial_remote_data(sshexec, args.config.mail_domain)
|
||||
remote_data = sshexec.logged(
|
||||
call=remote.rdns.perform_initial_checks,
|
||||
kwargs=dict(mail_domain=args.config.mail_domain),
|
||||
)
|
||||
if not dns.check_initial_remote_data(remote_data, strict_tls=strict_tls):
|
||||
return 1
|
||||
|
||||
@@ -182,10 +188,10 @@ def dns_cmd(args, out):
|
||||
out.green(f"DNS records successfully written to: {args.zonefile}")
|
||||
return 0
|
||||
|
||||
retcode = dns.check_full_zone(
|
||||
sshexec, remote_data=remote_data, zonefile=zonefile, out=out
|
||||
required_diff, recommended_diff = sshexec.logged(
|
||||
remote.rdns.check_zonefile, kwargs=dict(zonefile=zonefile, verbose=False)
|
||||
)
|
||||
return retcode
|
||||
return dns.report_zone_diff(remote_data, required_diff, recommended_diff, out)
|
||||
|
||||
|
||||
def status_cmd_options(parser):
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import datetime
|
||||
|
||||
from . import remote
|
||||
|
||||
|
||||
def parse_zone_records(text):
|
||||
"""Yield ``(name, ttl, rtype, rdata)`` from standard BIND-format text."""
|
||||
@@ -17,12 +15,6 @@ def parse_zone_records(text):
|
||||
yield name, ttl, rtype.upper(), rdata
|
||||
|
||||
|
||||
def get_initial_remote_data(sshexec, mail_domain):
|
||||
return sshexec.logged(
|
||||
call=remote.rdns.perform_initial_checks, kwargs=dict(mail_domain=mail_domain)
|
||||
)
|
||||
|
||||
|
||||
def check_initial_remote_data(remote_data, *, strict_tls=True, print=print):
|
||||
mail_domain = remote_data["mail_domain"]
|
||||
if not remote_data["A"] and not remote_data["AAAA"]:
|
||||
@@ -76,15 +68,8 @@ def get_filled_zone_file(remote_data):
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def check_full_zone(sshexec, remote_data, out, zonefile) -> int:
|
||||
"""Check existing DNS records, optionally write them to zone file
|
||||
and return (exitcode, remote_data) tuple."""
|
||||
|
||||
required_diff, recommended_diff = sshexec.logged(
|
||||
remote.rdns.check_zonefile,
|
||||
kwargs=dict(zonefile=zonefile, verbose=False),
|
||||
)
|
||||
|
||||
def report_zone_diff(remote_data, required_diff, recommended_diff, out) -> int:
|
||||
"""Print what rdns.check_zonefile found missing, return the exit code."""
|
||||
returncode = 0
|
||||
if required_diff:
|
||||
out.red("Please set required DNS entries at your DNS provider:\n")
|
||||
|
||||
@@ -3,7 +3,7 @@ from copy import deepcopy
|
||||
import pytest
|
||||
|
||||
from cmdeploy import remote
|
||||
from cmdeploy.dns import check_full_zone, check_initial_remote_data, parse_zone_records
|
||||
from cmdeploy.dns import check_initial_remote_data, parse_zone_records, report_zone_diff
|
||||
from cmdeploy.remote.rdns import get_authoritative_ns
|
||||
|
||||
|
||||
@@ -186,14 +186,6 @@ def parse_zonefile_into_dict(zonefile, mockdns_base, only_required=False):
|
||||
mockdns_base.setdefault(rtype, {})[name] = rdata
|
||||
|
||||
|
||||
class MockSSHExec:
|
||||
def logged(self, func, kwargs):
|
||||
return func(**kwargs)
|
||||
|
||||
def call(self, func, kwargs):
|
||||
return func(**kwargs)
|
||||
|
||||
|
||||
class TestZonefileChecks:
|
||||
def test_check_zonefile_all_ok(self, cm_data, mockdns_base):
|
||||
zonefile = cm_data.get("zftest.zone")
|
||||
@@ -213,9 +205,9 @@ class TestZonefileChecks:
|
||||
zonefile = cm_data.get("zftest.zone")
|
||||
zonefile_mocked = zonefile.split("; Recommended")[0]
|
||||
parse_zonefile_into_dict(zonefile_mocked, mockdns_base, only_required=True)
|
||||
mssh = MockSSHExec()
|
||||
mockdns_base["mail_domain"] = "some.domain"
|
||||
res = check_full_zone(mssh, mockdns_base, out=mockout, zonefile=zonefile)
|
||||
diffs = remote.rdns.check_zonefile(zonefile, verbose=False)
|
||||
res = report_zone_diff(mockdns_base, *diffs, out=mockout)
|
||||
assert res == 0
|
||||
assert "WARNING" in mockout.captured_plain[0]
|
||||
assert len(mockout.captured_plain) == 8
|
||||
@@ -223,9 +215,9 @@ class TestZonefileChecks:
|
||||
def test_check_zonefile_output_full(self, cm_data, mockdns_base, mockout):
|
||||
zonefile = cm_data.get("zftest.zone")
|
||||
parse_zonefile_into_dict(zonefile, mockdns_base)
|
||||
mssh = MockSSHExec()
|
||||
mockdns_base["mail_domain"] = "some.domain"
|
||||
res = check_full_zone(mssh, mockdns_base, out=mockout, zonefile=zonefile)
|
||||
diffs = remote.rdns.check_zonefile(zonefile, verbose=False)
|
||||
res = report_zone_diff(mockdns_base, *diffs, out=mockout)
|
||||
assert res == 0
|
||||
assert not mockout.captured_red
|
||||
assert "correct" in mockout.captured_green[0]
|
||||
|
||||
Reference in New Issue
Block a user