From 7172afa4a25bdb80199c4fe0628e7fb5c978ac78 Mon Sep 17 00:00:00 2001 From: Eric van der Vlist Date: Sat, 18 Apr 2020 09:36:48 +0200 Subject: [PATCH] Support of Docker host names (for NSD) --- binder/backends/nsd.py | 9 ++++----- binder/helpers.py | 14 ++++++++++++++ binder/models.py | 4 +++- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/binder/backends/nsd.py b/binder/backends/nsd.py index 684c823..f7b3ef6 100644 --- a/binder/backends/nsd.py +++ b/binder/backends/nsd.py @@ -5,6 +5,7 @@ from django.conf import settings from jinja2 import Template import re import subprocess +from binder import helpers NSD_CONF_TEMPLATE = """ # nsd.conf for {{hostname}} @@ -16,9 +17,7 @@ remote-control: """ -ZONE_RE = re.compile(""" -zone\:\s+(?P\S+) -\s+state: master""") +ZONE_RE = re.compile("""zone:\s+(?P\S+)\s+state:\s+master""") class NSDServer(object): """Class to manage NSD backend server data.""" @@ -53,13 +52,13 @@ class NSDServer(object): zs_out = subprocess.check_output( ["/usr/sbin/nsd-control", "-c", self.get_config_path(), - "-s", self.hostname, + "-s", helpers.ip_address(self.hostname), "zonestatus"], stderr=subprocess.STDOUT, ).decode('utf-8') except subprocess.CalledProcessError: raise - zones = ZONE_RE.findall(zs_out, re.MULTILINE) + zones = ZONE_RE.findall(zs_out) zone_data = {} zone_data['stats'] = {} zone_data['stats']['zone_stats'] = {} diff --git a/binder/helpers.py b/binder/helpers.py index 18f0439..2d8f218 100644 --- a/binder/helpers.py +++ b/binder/helpers.py @@ -175,6 +175,20 @@ def ip_info(host_name): return info +def ip_address(host_name): + """Find an IP address for a host. + """ + try: + for s_family, s_type, s_proto, s_cannoname, s_sockaddr in socket.getaddrinfo(host_name, None): + if s_family == 2 and s_type == 1: + return s_sockaddr[0] + if s_family == 10 and s_type == 1: + return s_sockaddr[0] + except (socket.gaierror, err): + return None + + return None + def send_dns_update(dns_message, dns_server, port, key_name): """Send DNS message to server and return response. diff --git a/binder/models.py b/binder/models.py index bf7317d..8e93a1c 100644 --- a/binder/models.py +++ b/binder/models.py @@ -20,6 +20,8 @@ from binder.backends import nsd from django.db import models from django.conf import settings from django.core.validators import MinValueValidator, MaxValueValidator +from binder import helpers + TSIG_ALGORITHMS = (('HMAC-MD5.SIG-ALG.REG.INT', 'MD5'), ('hmac-sha1', 'SHA1'), @@ -198,7 +200,7 @@ class BindServer(models.Model): try: xfr = dns.query.xfr( - self.hostname, + helpers.ip_address(self.hostname), zone_name, port=self.dns_port, keyring=keyring,