Commit 77772997 authored by Romain Courteaud's avatar Romain Courteaud

report MX conf issue (ie, unexpected open port 25)

parent 19d1bbb3
This diff is collapsed.
......@@ -104,12 +104,16 @@ def buildResolver(resolver_ip, timeout):
def queryDNS(db, status_id, resolver_ip, domain_text, rdtype, timeout=TIMEOUT):
# only A (and AAAA) has address property
assert rdtype == "A"
assert rdtype in ["A", "MX"], rdtype
resolver = buildResolver(resolver_ip, timeout)
try:
answer_list = [
x.address
(
x.address
if (rdtype == "A")
else x.exchange.derelativize(domain_text).to_text()[:-1]
)
for x in resolver.query(
domain_text, rdtype, raise_on_no_answer=False
)
......@@ -121,6 +125,7 @@ def queryDNS(db, status_id, resolver_ip, domain_text, rdtype, timeout=TIMEOUT):
dns_resolver.NoNameservers,
):
answer_list = []
# how to differentiate no answer from empty answer
logDnsQuery(db, status_id, resolver_ip, domain_text, rdtype, answer_list)
return answer_list
......
This diff is collapsed.
......@@ -32,13 +32,19 @@ from surykatka.dns import (
)
from surykatka.status import logStatus
import mock
from dns import name as dns_name
class MockAnswer(object):
class MockAnswerA(object):
def __init__(self, address):
self.address = address
class MockAnswerMX(object):
def __init__(self, label):
self.exchange = dns_name.Name(dns_name.from_text(label))
class SurykatkaDNSTestCase(unittest.TestCase):
def setUp(self):
self.db = LogDB(":memory:")
......@@ -253,7 +259,7 @@ class SurykatkaDNSTestCase(unittest.TestCase):
################################################
# queryDNS
################################################
def test_queryDNS_default(self):
def test_queryDNS_A(self):
resolver_ip = "127.0.0.1"
domain = "example.org"
rdtype = "A"
......@@ -263,8 +269,8 @@ class SurykatkaDNSTestCase(unittest.TestCase):
"surykatka.dns.dns_resolver.Resolver.query"
) as mock_query:
mock_query.return_value = [
MockAnswer("4.3.2.1"),
MockAnswer("1.2.3.4"),
MockAnswerA("4.3.2.1"),
MockAnswerA("1.2.3.4"),
]
result = queryDNS(self.db, status_id, resolver_ip, domain, rdtype)
......@@ -281,6 +287,37 @@ class SurykatkaDNSTestCase(unittest.TestCase):
assert self.db.DnsChange.get().status_id == status_id
assert result == ["1.2.3.4", "4.3.2.1"]
def test_queryDNS_MX(self):
resolver_ip = "127.0.0.1"
domain = "example.org"
rdtype = "MX"
status_id = logStatus(self.db, "foo")
with mock.patch(
"surykatka.dns.dns_resolver.Resolver.query"
) as mock_query:
mock_query.return_value = [
MockAnswerMX("mail1.example.org"),
MockAnswerMX("mail2.example.org"),
]
result = queryDNS(self.db, status_id, resolver_ip, domain, rdtype)
assert mock_query.call_count == 1
mock_query.assert_called_with(
domain, rdtype, raise_on_no_answer=False
)
assert self.db.DnsChange.select().count() == 1
assert self.db.DnsChange.get().resolver_ip == resolver_ip
assert self.db.DnsChange.get().domain == domain
assert self.db.DnsChange.get().rdtype == rdtype
assert (
self.db.DnsChange.get().response
== "mail1.example.org, mail2.example.org"
)
assert self.db.DnsChange.get().status_id == status_id
assert result == ["mail1.example.org", "mail2.example.org"]
def test_queryDNS_rejectRdtype(self):
resolver_ip = "127.0.0.1"
domain = "example.org"
......@@ -423,8 +460,8 @@ class SurykatkaDNSTestCase(unittest.TestCase):
"surykatka.dns.dns_resolver.Resolver.query"
) as mock_query:
mock_query.return_value = [
MockAnswer("4.3.2.1"),
MockAnswer("1.2.3.4"),
MockAnswerA("4.3.2.1"),
MockAnswerA("1.2.3.4"),
]
result = getReachableResolverList(
self.db, status_id, [resolver_ip]
......@@ -502,8 +539,8 @@ class SurykatkaDNSTestCase(unittest.TestCase):
"surykatka.dns.dns_resolver.Resolver.query"
) as mock_query:
mock_query.return_value = [
MockAnswer("4.3.2.1"),
MockAnswer("1.2.3.4"),
MockAnswerA("4.3.2.1"),
MockAnswerA("1.2.3.4"),
]
result = getDomainIpDict(
self.db, status_id, resolver_ip_list, domain_list, rdtype
......@@ -531,8 +568,8 @@ class SurykatkaDNSTestCase(unittest.TestCase):
"surykatka.dns.dns_resolver.Resolver.query"
) as mock_query:
mock_query.side_effect = [
[MockAnswer("4.3.2.1"), MockAnswer("1.2.3.4")],
[MockAnswer("4.3.2.1"), MockAnswer("1.2.3.5")],
[MockAnswerA("4.3.2.1"), MockAnswerA("1.2.3.4")],
[MockAnswerA("4.3.2.1"), MockAnswerA("1.2.3.5")],
]
result = getDomainIpDict(
self.db, status_id, resolver_ip_list, domain_list, rdtype
......@@ -564,8 +601,8 @@ class SurykatkaDNSTestCase(unittest.TestCase):
"surykatka.dns.dns_resolver.Resolver.query"
) as mock_query:
mock_query.side_effect = [
[MockAnswer("4.3.2.1"), MockAnswer("1.2.3.4")],
[MockAnswer("4.3.2.1"), MockAnswer("1.2.3.5")],
[MockAnswerA("4.3.2.1"), MockAnswerA("1.2.3.4")],
[MockAnswerA("4.3.2.1"), MockAnswerA("1.2.3.5")],
]
result = getDomainIpDict(
self.db, status_id, resolver_ip_list, domain_list, rdtype
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment