Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Almost-dynamic zone update #15

Merged
merged 7 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
33 changes: 20 additions & 13 deletions dnserver/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from dnslib.proxy import ProxyResolver as LibProxyResolver
from dnslib.server import BaseResolver as LibBaseResolver, DNSServer as LibDNSServer

from .load_records import Zone, load_records
from .load_records import Records, Zone, load_records

__all__ = 'DNSServer', 'logger'

Expand Down Expand Up @@ -107,10 +107,8 @@ def resolve(request, handler, records):


class BaseResolver(LibBaseResolver):
def __init__(self, zones_file: str | Path):
records = load_records(zones_file)
def __init__(self, records: Records):
self.records = [Record(zone) for zone in records.zones]
logger.info('loaded %d zone record from %s, without proxy DNS server', len(self.records), zones_file)
super().__init__()

def resolve(self, request, handler):
Expand All @@ -124,12 +122,8 @@ def resolve(self, request, handler):


class ProxyResolver(LibProxyResolver):
def __init__(self, zones_file: str | Path, upstream: str):
records = load_records(zones_file)
def __init__(self, records: Records, upstream: str):
self.records = [Record(zone) for zone in records.zones]
logger.info(
'loaded %d zone record from %s, with %s as a proxy DNS server', len(self.records), zones_file, upstream
)
super().__init__(address=upstream, port=53, timeout=5)

def resolve(self, request, handler):
Expand All @@ -146,18 +140,26 @@ class DNSServer:
def __init__(
self, zones_file: str | Path, *, port: int | str | None = DEFAULT_PORT, upstream: str | None = DEFAULT_UPSTREAM
):
self.zones_file = zones_file
self.port: int = DEFAULT_PORT if port is None else int(port)
self.upstream: str | None = upstream
self.udp_server: LibDNSServer | None = None
self.tcp_server: LibDNSServer | None = None

self.records: Records = load_records(zones_file)
azmeuk marked this conversation as resolved.
Show resolved Hide resolved
logger.info(
'loaded %d zone record from %s, with %s as a proxy DNS server',
len(self.records.zones),
zones_file,
upstream,
)

def start(self):
logger.info('starting DNS server on port %d, upstream DNS server "%s"', self.port, self.upstream)
if self.upstream:
resolver = ProxyResolver(self.zones_file, self.upstream)
logger.info('starting DNS server on port %d, upstream DNS server "%s"', self.port, self.upstream)
resolver = ProxyResolver(self.records, self.upstream)
else:
resolver = BaseResolver(self.zones_file)
logger.info('starting DNS server on port %d, without upstream DNS server', self.port)
resolver = BaseResolver(self.records)

self.udp_server = LibDNSServer(resolver, port=self.port)
self.tcp_server = LibDNSServer(resolver, port=self.port, tcp=True)
Expand All @@ -173,3 +175,8 @@ def stop(self):
@property
def is_running(self):
return (self.udp_server and self.udp_server.isAlive()) or (self.tcp_server and self.tcp_server.isAlive())

def add_record(self, **kwargs):
zone = Zone(**kwargs)
azmeuk marked this conversation as resolved.
Show resolved Hide resolved
self.records.zones.append(zone)
Copy link
Owner

Choose a reason for hiding this comment

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

humm, have you actually tried this? I think because of self.records = [Record(zone) for zone in records.zones] this won't actually add another record to the dns server.

Best option would be to store Records directly on ProxyResolver and BaseResolver, with that, extending self.records should actually affect the dns server.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

humm, have you actually tried this? I think because of self.records = [Record(zone) for zone in records.zones] this won't actually add another record to the dns server.

Well, there is a unit test for that. Do you think this is not tested enough?

Best option would be to store Records directly on ProxyResolver and BaseResolver, with that, extending self.records should actually affect the dns server.

I just pushed that. Indeed, doing this way actually not requires the server to be restarted when a zone is added.

return zone
azmeuk marked this conversation as resolved.
Show resolved Hide resolved
41 changes: 36 additions & 5 deletions tests/test_dnsserver.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Any, Callable, Dict, List

import dns
import pytest
from dirty_equals import IsIP, IsPositive
from dns.resolver import NoAnswer, Resolver as RawResolver
Expand Down Expand Up @@ -33,26 +34,28 @@ def convert_answer(answer) -> Dict[str, Any]:


@pytest.fixture(scope='session')
def dns_resolver():
def dns_server():
port = 5053

server = DNSServer('example_zones.toml', port=port)
server.start()

assert server.is_running
yield server
server.stop


@pytest.fixture(scope='session')
def dns_resolver(dns_server: DNSServer):
resolver = RawResolver()
resolver.nameservers = ['127.0.0.1']
resolver.port = port
resolver.port = dns_server.port

def resolve(name: str, type_: str) -> List[Dict[str, Any]]:
answers = resolver.resolve(name, type_)
return [convert_answer(answer) for answer in answers]

yield resolve

server.stop()


def test_a_record(dns_resolver: Resolver):
assert dns_resolver('example.com', 'A') == [
Expand Down Expand Up @@ -155,3 +158,31 @@ def resolve(name: str, type_: str) -> List[Dict[str, Any]]:
resolve('python.org', 'A')
finally:
server.stop()


def test_dynamic_zone_update(dns_server: DNSServer, dns_resolver: Resolver):
assert dns_resolver('example.com', 'A') == [
{
'type': 'A',
'value': '1.2.3.4',
},
]
with pytest.raises(dns.resolver.NXDOMAIN):
dns_resolver('another-example.org', 'A')

dns_server.stop()
dns_server.add_record(host='another-example.com', type='A', answer='2.3.4.5')
dns_server.start()

assert dns_resolver('example.com', 'A') == [
{
'type': 'A',
'value': '1.2.3.4',
},
]
assert dns_resolver('another-example.com', 'A') == [
{
'type': 'A',
'value': '2.3.4.5',
},
]