Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions cmdeploy/src/cmdeploy/remote/rdns.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,22 @@ def get_dkim_entry(mail_domain, pre_command, dkim_selector):


def get_authoritative_ns(domain):
ns_replies = [
x.split()
for x in shell(
f"dig -r -q {domain} -t NS +noall +authority +answer", print=log_progress
).split("\n")
]
filtered_replies = [a for a in ns_replies if len(a) >= 5 and a[3] == "NS"]
if not filtered_replies:
return
return filtered_replies[0][4]
"""Find the closest authoritative nameserver for a domain."""
labels = domain.rstrip(".").split(".")
for index in range(len(labels)):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for index in range(len(labels)):
for index in range(len(labels) - 1):

The walk runs one label too far, up to the registry (e.g., .org), and if that is not recursing, there will be another lookup failure, so stop one before (at example.org)

candidate = ".".join(labels[index:])
ns_replies = [
x.split()
for x in shell(
f"dig -r -q {candidate} -t NS +noall +authority +answer",
print=log_progress,
).split("\n")
]
filtered_replies = [
reply for reply in ns_replies if len(reply) >= 5 and reply[3] == "NS"
]
if filtered_replies:
return filtered_replies[0][4]


def query_dns(typ, domain):
Expand Down
22 changes: 22 additions & 0 deletions cmdeploy/src/cmdeploy/tests/test_dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,28 @@ def test_get_authoritative_ns(domain, ns, mockdns):
assert get_authoritative_ns(domain) == ns


def test_get_authoritative_ns_walks_to_zone(monkeypatch):
replies = {
"www.some.domain": (
"some.domain. 1800 IN SOA ns1.some.domain. hostmaster.some.domain. "
"1 10000 2400 604800 1800\n"
"www.some.domain. 1800 IN NSEC \\000.www.some.domain. A AAAA"
),
"some.domain": "some.domain. 3600 IN NS ns1.some.domain.",
}
queried_domains = []

def shell(command, print):
chunks = command.split()
queried_domains.append(chunks[3])
return replies[chunks[3]]

monkeypatch.setattr(remote.rdns, "shell", shell)

assert get_authoritative_ns("www.some.domain") == "ns1.some.domain."
assert queried_domains == ["www.some.domain", "some.domain"]


def test_parse_zone_records():
text = """
; This is a comment
Expand Down