From d35b068a73d281de8ea82c7b012586c20b770f28 Mon Sep 17 00:00:00 2001 From: KiekerJan Date: Sun, 17 Apr 2022 22:56:30 +0200 Subject: [PATCH] add dns exception handling --- management/daemon.py | 3 ++- management/dns_update.py | 21 ++++++++++++++++----- management/status_checks.py | 10 ++++++---- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/management/daemon.py b/management/daemon.py index 8ba11e7e..ae7fb351 100755 --- a/management/daemon.py +++ b/management/daemon.py @@ -274,6 +274,7 @@ def dns_update(): try: return do_dns_update(env, force=request.form.get('force', '') == '1') except Exception as e: + logging.exception('dns update exc') return (str(e), 500) @app.route('/dns/secondary-nameserver') @@ -763,7 +764,7 @@ def log_failed_login(request): # APP if __name__ == '__main__': - logging_level = logging.INFO + logging_level = logging.DEBUG if "DEBUG" in os.environ: # Turn on Flask debugging. diff --git a/management/dns_update.py b/management/dns_update.py index 9191a307..ad55868b 100755 --- a/management/dns_update.py +++ b/management/dns_update.py @@ -1065,6 +1065,7 @@ def set_custom_dns_record(qname, rtype, value, action, env): def get_secondary_dns(custom_dns, mode=None): resolver = dns.resolver.get_default_resolver() resolver.timeout = 10 + resolver.lifetime = 10 values = [] for qname, rtype, value in custom_dns: @@ -1082,10 +1083,17 @@ def get_secondary_dns(custom_dns, mode=None): # doesn't. if not hostname.startswith("xfr:"): if mode == "xfr": - response = dns.resolver.resolve(hostname+'.', "A", raise_on_no_answer=False) - values.extend(map(str, response)) - response = dns.resolver.resolve(hostname+'.', "AAAA", raise_on_no_answer=False) - values.extend(map(str, response)) + try: + response = resolver.resolve(hostname+'.', "A", raise_on_no_answer=False) + values.extend(map(str, response)) + except dns.exception.DNSException: + logging.debug("Secondary dns Alookup exception %s", hostname) + + try: + response = resolver.resolve(hostname+'.', "AAAA", raise_on_no_answer=False) + values.extend(map(str, response)) + except dns.exception.DNSException: + logging.debug("Secondary dns AAAA lookup exception %s", hostname) continue values.append(hostname) @@ -1103,6 +1111,7 @@ def set_secondary_dns(hostnames, env): # Validate that all hostnames are valid and that all zone-xfer IP addresses are valid. resolver = dns.resolver.get_default_resolver() resolver.timeout = 3 + resolver.lifetime = 3 for item in hostnames: if not item.startswith("xfr:"): # Resolve hostname. @@ -1116,7 +1125,8 @@ def set_secondary_dns(hostnames, env): raise ValueError("Could not resolve the IP address of %s." % item) except (dns.resolver.Timeout): resolver.timeout = 5 - logging.warning('Timeout on resolving ipv4 address re-trying') + resolver.lifetime = 5 + logging.debug('Timeout on resolving ipv4 address re-trying') try: response = resolver.resolve(item, "A") except (dns.resolver.NoNameservers, dns.resolver.NXDOMAIN, dns.resolver.NoAnswer): @@ -1128,6 +1138,7 @@ def set_secondary_dns(hostnames, env): except (dns.resolver.Timeout): raise ValueError("Could not resolve the IP address of %s due to timeout." % item) resolver.timeout = 3 + resolver.lifetime = 3 else: # Validate IP address. try: diff --git a/management/status_checks.py b/management/status_checks.py index 93e0320f..2bdaf538 100755 --- a/management/status_checks.py +++ b/management/status_checks.py @@ -802,6 +802,7 @@ def query_dns(qname, rtype, nxdomain='[Not Set]', at=None, as_list=False): # Set a timeout so that a non-responsive server doesn't hold us back. resolver.timeout = 3 + reaolver.lifetime = 3 # Do the query. try: @@ -809,20 +810,21 @@ def query_dns(qname, rtype, nxdomain='[Not Set]', at=None, as_list=False): except (dns.resolver.NoNameservers, dns.resolver.NXDOMAIN, dns.resolver.NoAnswer): # Host did not have an answer for this query; not sure what the # difference is between the two exceptions. - logging.info("No result for dns lookup %s, %s", qname, rtype) + logging.debug("No result for dns lookup %s, %s", qname, rtype) return nxdomain except dns.exception.Timeout: - logging.info("Timeout on dns lookup %s, %s. Retrying", qname, rtype) + logging.debug("Timeout on dns lookup %s, %s. Retrying", qname, rtype) resolver.timeout = 5 + resolver.lifetime = 5 try: response = resolver.resolve(qname, rtype, search=True) except (dns.resolver.NoNameservers, dns.resolver.NXDOMAIN, dns.resolver.NoAnswer): # Host did not have an answer for this query; not sure what the # difference is between the two exceptions. - logging.info("No result for dns lookup %s, %s (2)", qname, rtype) + logging.debug("No result for dns lookup %s, %s (2)", qname, rtype) return nxdomain except dns.exception.Timeout: - logging.info("Timeout on dns lookup %s, %s.", qname, rtype) + logging.debug("Timeout on dns lookup %s, %s.", qname, rtype) return "[timeout]" # Normalize IP addresses. IP address --- especially IPv6 addresses --- can