From 1836c221faf344d1aedd1bb44c6b75e8f7e83023 Mon Sep 17 00:00:00 2001 From: Erik Olof Gunnar Andersson Date: Fri, 14 Sep 2018 01:15:50 -0700 Subject: [PATCH] Fixing Python 3.x issues introduced with eventlet update Closes-Bug: #1792671 Change-Id: Iecef0977a1e4c102bd85b0d9c27a8d37dcd3ee17 --- designate/agent/handler.py | 13 ++++++-- designate/backend/agent_backend/impl_bind9.py | 5 ++- .../backend/agent_backend/impl_denominator.py | 13 ++++++-- .../backend/agent_backend/impl_djbdns.py | 9 +++-- designate/backend/agent_backend/impl_gdnsd.py | 5 ++- designate/backend/agent_backend/impl_knot2.py | 9 +++-- designate/backend/agent_backend/impl_msdns.py | 9 +++-- designate/dnsutils.py | 33 +++++++++++++++---- designate/mdns/handler.py | 17 ++++++++-- designate/network_api/base.py | 6 +++- 10 files changed, 94 insertions(+), 25 deletions(-) diff --git a/designate/agent/handler.py b/designate/agent/handler.py index a9ef99694..4aa644f5d 100644 --- a/designate/agent/handler.py +++ b/designate/agent/handler.py @@ -30,6 +30,7 @@ import dns.rcode import dns.message import dns.flags import dns.opcode +import six from oslo_config import cfg from oslo_log import log as logging @@ -104,7 +105,9 @@ class RequestHandler(object): question = request.question[0] requester = request.environ['addr'][0] - zone_name = question.name.to_text().decode('utf-8') + zone_name = question.name.to_text() + if six.PY3 and isinstance(zone_name, bytes): + zone_name = zone_name.decode('utf-8') if not self._allowed(request, requester, "CREATE", zone_name): response.set_rcode(dns.rcode.from_text("REFUSED")) @@ -155,7 +158,9 @@ class RequestHandler(object): question = request.question[0] requester = request.environ['addr'][0] - zone_name = question.name.to_text().decode('utf-8') + zone_name = question.name.to_text() + if six.PY3 and isinstance(zone_name, bytes): + zone_name = zone_name.decode('utf-8') if not self._allowed(request, requester, "NOTIFY", zone_name): response.set_rcode(dns.rcode.from_text("REFUSED")) @@ -206,7 +211,9 @@ class RequestHandler(object): question = request.question[0] requester = request.environ['addr'][0] - zone_name = question.name.to_text().decode('utf-8') + zone_name = question.name.to_text() + if six.PY3 and isinstance(zone_name, bytes): + zone_name = zone_name.decode('utf-8') if not self._allowed(request, requester, "DELETE", zone_name): response.set_rcode(dns.rcode.from_text("REFUSED")) diff --git a/designate/backend/agent_backend/impl_bind9.py b/designate/backend/agent_backend/impl_bind9.py index b65c5502d..a668810cc 100644 --- a/designate/backend/agent_backend/impl_bind9.py +++ b/designate/backend/agent_backend/impl_bind9.py @@ -17,6 +17,7 @@ import os import dns import dns.resolver +import six from oslo_concurrency import lockutils from oslo_config import cfg from oslo_log import log as logging @@ -111,7 +112,9 @@ class Bind9Backend(base.AgentBackend): # NOTE: Different versions of BIND9 behave differently with a trailing # dot, so we're just going to take it off. - zone_name = zone.origin.to_text(omit_final_dot=True).decode('utf-8') + zone_name = zone.origin.to_text(omit_final_dot=True) + if six.PY3 and isinstance(zone_name, bytes): + zone_name = zone_name.decode('utf-8') # NOTE: Only one thread should be working with the Zonefile at a given # time. The sleep(1) below introduces a not insignificant risk diff --git a/designate/backend/agent_backend/impl_denominator.py b/designate/backend/agent_backend/impl_denominator.py index a69e40945..056614d3d 100644 --- a/designate/backend/agent_backend/impl_denominator.py +++ b/designate/backend/agent_backend/impl_denominator.py @@ -18,6 +18,7 @@ import itertools import dns.rdata import dns.rdatatype import dns.rdataclass +import six from oslo_config import cfg from oslo_concurrency import lockutils from oslo_log import log as logging @@ -139,7 +140,9 @@ class DenominatorBackend(base.AgentBackend): def create_zone(self, zone): LOG.debug("Creating %s", zone.origin.to_text()) - zone_name = zone.origin.to_text(omit_final_dot=True).decode('utf-8') + zone_name = zone.origin.to_text(omit_final_dot=True) + if six.PY3 and isinstance(zone_name, bytes): + zone_name = zone_name.decode('utf-8') # Use SOA TTL as zone default TTL soa_record = zone.find_rrset(zone.origin, dns.rdatatype.SOA) @@ -171,7 +174,9 @@ class DenominatorBackend(base.AgentBackend): def update_zone(self, zone): LOG.debug("Updating %s", zone.origin) - zone_name = zone.origin.to_text(omit_final_dot=True).decode('utf-8') + zone_name = zone.origin.to_text(omit_final_dot=True) + if six.PY3 and isinstance(zone_name, bytes): + zone_name = zone_name.decode('utf-8') soa_record = zone.find_rrset(zone.origin, dns.rdatatype.SOA) rname = soa_record.items[0].rname.derelativize(origin=zone.origin) @@ -239,7 +244,9 @@ class DenominatorBackend(base.AgentBackend): def _iterate_records(self, zone): for rname, ttl, rdata in zone.iterate_rdatas(): name = rname.derelativize(origin=zone.origin) - name = name.to_text(omit_final_dot=True).decode('utf-8') + name = name.to_text(omit_final_dot=True) + if six.PY3 and isinstance(name, bytes): + name = name.decode('utf-8') data = rdata.to_text(origin=zone.origin, relativize=False) yield name, ttl, dns.rdatatype.to_text(rdata.rdtype), data diff --git a/designate/backend/agent_backend/impl_djbdns.py b/designate/backend/agent_backend/impl_djbdns.py index 393ce9203..d74337d6e 100755 --- a/designate/backend/agent_backend/impl_djbdns.py +++ b/designate/backend/agent_backend/impl_djbdns.py @@ -46,6 +46,7 @@ import tempfile import dns import dns.resolver +import six from oslo_concurrency import lockutils from oslo_concurrency.processutils import ProcessExecutionError from oslo_config import cfg @@ -310,7 +311,9 @@ class DjbdnsBackend(base.AgentBackend): :type zone: raw pythondns Zone :raises: exceptions.Backend on error """ - zone_name = zone.origin.to_text(omit_final_dot=True).decode('utf-8') + zone_name = zone.origin.to_text(omit_final_dot=True) + if six.PY3 and isinstance(zone_name, bytes): + zone_name = zone_name.decode('utf-8') LOG.debug("Creating %s", zone_name) # The zone might be already in place due to a race condition between # checking if the zone is there and creating it across different @@ -329,7 +332,9 @@ class DjbdnsBackend(base.AgentBackend): :type zone: raw pythondns Zone :raises: exceptions.Backend on error """ - zone_name = zone.origin.to_text(omit_final_dot=True).decode('utf-8') + zone_name = zone.origin.to_text(omit_final_dot=True) + if six.PY3 and isinstance(zone_name, bytes): + zone_name = zone_name.decode('utf-8') LOG.debug("Triggering AXFR from MiniDNS to Djbdns for %s", zone_name) self._perform_axfr_from_minidns(zone_name) self._rebuild_data_cdb() diff --git a/designate/backend/agent_backend/impl_gdnsd.py b/designate/backend/agent_backend/impl_gdnsd.py index 4df8ab865..ef992b675 100644 --- a/designate/backend/agent_backend/impl_gdnsd.py +++ b/designate/backend/agent_backend/impl_gdnsd.py @@ -45,6 +45,7 @@ import string import dns import dns.resolver +import six from oslo_concurrency.processutils import ProcessExecutionError from oslo_config import cfg from oslo_log import log as logging @@ -185,7 +186,9 @@ class GdnsdBackend(base.AgentBackend): """Create or update a zone file atomically. The zone file is written to a unique temp file and then renamed """ - zone_name = zone.origin.to_text(omit_final_dot=True).decode('utf-8') + zone_name = zone.origin.to_text(omit_final_dot=True) + if six.PY3 and isinstance(zone_name, bytes): + zone_name = zone_name.decode('utf-8') zone_base_fname = self._generate_zone_filename(zone_name) zone_fname = os.path.join(self._zonedir_path, zone_base_fname) try: diff --git a/designate/backend/agent_backend/impl_knot2.py b/designate/backend/agent_backend/impl_knot2.py index c1d4e194b..08fa49c5d 100755 --- a/designate/backend/agent_backend/impl_knot2.py +++ b/designate/backend/agent_backend/impl_knot2.py @@ -37,6 +37,7 @@ Supported Knot versions: >= 2.1, < 3 Configured in [service:agent:knot2] """ +import six from oslo_concurrency import lockutils from oslo_concurrency.processutils import ProcessExecutionError @@ -199,7 +200,9 @@ class Knot2Backend(base.AgentBackend): :param zone: zone to be created :type zone: raw pythondns Zone """ - zone_name = zone.origin.to_text(omit_final_dot=True).decode('utf-8') + zone_name = zone.origin.to_text(omit_final_dot=True) + if six.PY3 and isinstance(zone_name, bytes): + zone_name = zone_name.decode('utf-8') LOG.debug("Creating %s", zone_name) # The zone might be already in place due to a race condition between # checking if the zone is there and creating it across different @@ -217,7 +220,9 @@ class Knot2Backend(base.AgentBackend): :param zone: zone to be created :type zone: raw pythondns Zone """ - zone_name = zone.origin.to_text(omit_final_dot=True).decode('utf-8') + zone_name = zone.origin.to_text(omit_final_dot=True) + if six.PY3 and isinstance(zone_name, bytes): + zone_name = zone_name.decode('utf-8') LOG.debug("Triggering AXFR from MiniDNS to Knot for %s", zone_name) self._start_minidns_to_knot_axfr(zone_name) diff --git a/designate/backend/agent_backend/impl_msdns.py b/designate/backend/agent_backend/impl_msdns.py index 213945ab8..e9ef8a37b 100644 --- a/designate/backend/agent_backend/impl_msdns.py +++ b/designate/backend/agent_backend/impl_msdns.py @@ -14,6 +14,7 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. +import six from oslo_config import cfg from oslo_log import log as logging @@ -79,7 +80,9 @@ class MSDNSBackend(base.AgentBackend): def create_zone(self, zone): """Create a new DNS Zone""" - zone_name = zone.origin.to_text(omit_final_dot=True).decode('utf-8') + zone_name = zone.origin.to_text(omit_final_dot=True) + if six.PY3 and isinstance(zone_name, bytes): + zone_name = zone_name.decode('utf-8') LOG.debug("Creating zone: %s", zone_name) try: self._dnsutils.zone_create( @@ -104,7 +107,9 @@ class MSDNSBackend(base.AgentBackend): def update_zone(self, zone): """Instruct MSDNS to request an AXFR from MiniDNS. """ - zone_name = zone.origin.to_text(omit_final_dot=True).decode('utf-8') + zone_name = zone.origin.to_text(omit_final_dot=True) + if six.PY3 and isinstance(zone_name, bytes): + zone_name = zone_name.decode('utf-8') LOG.debug("Updating zone: %s", zone_name) self._dnsutils.zone_update(zone_name) diff --git a/designate/dnsutils.py b/designate/dnsutils.py index 18f641feb..0fc9164a0 100644 --- a/designate/dnsutils.py +++ b/designate/dnsutils.py @@ -156,7 +156,10 @@ class TsigInfoMiddleware(DNSMiddleware): return None try: - criterion = {'name': request.keyname.to_text(True).decode('utf-8')} + name = request.keyname.to_text(True) + if six.PY3 and isinstance(name, bytes): + name = name.decode('utf-8') + criterion = {'name': name} tsigkey = self.storage.find_tsigkey( context.get_current(), criterion) @@ -182,7 +185,10 @@ class TsigKeyring(object): def get(self, key, default=None): try: - criterion = {'name': key.to_text(True).decode('utf-8')} + name = key.to_text(True) + if six.PY3 and isinstance(name, bytes): + name = name.decode('utf-8') + criterion = {'name': name} tsigkey = self.storage.find_tsigkey( context.get_current(), criterion) @@ -249,7 +255,9 @@ class LimitNotifyMiddleware(DNSMiddleware): if opcode != dns.opcode.NOTIFY: return None - zone_name = request.question[0].name.to_text().decode('utf-8') + zone_name = request.question[0].name.to_text() + if six.PY3 and isinstance(zone_name, bytes): + zone_name = zone_name.decode('utf-8') if self.locker.acquire(zone_name): time.sleep(self.delay) @@ -272,10 +280,17 @@ def from_dnspython_zone(dnspython_zone): raise exceptions.BadRequest('An SOA record is required') if soa.ttl == 0: soa.ttl = cfg.CONF['service:central'].min_ttl - email = soa[0].rname.to_text(omit_final_dot=True).decode('utf-8') + email = soa[0].rname.to_text(omit_final_dot=True) + if six.PY3 and isinstance(email, bytes): + email = email.decode('utf-8') email = email.replace('.', '@', 1) + + name = dnspython_zone.origin.to_text() + if six.PY3 and isinstance(name, bytes): + name = name.decode('utf-8') + values = { - 'name': dnspython_zone.origin.to_text().decode('utf-8'), + 'name': name, 'email': email, 'ttl': soa.ttl, 'serial': soa[0].serial, @@ -307,12 +322,16 @@ def dnspyrecords_to_recordsetlist(dnspython_records): def dnspythonrecord_to_recordset(rname, rdataset): record_type = rdatatype.to_text(rdataset.rdtype) + name = rname.to_text() + if six.PY3 and isinstance(name, bytes): + name = name.decode('utf-8') + # Create the other recordsets + values = { - 'name': rname.to_text().decode('utf-8'), + 'name': name, 'type': record_type } - if rdataset.ttl != 0: values['ttl'] = rdataset.ttl diff --git a/designate/mdns/handler.py b/designate/mdns/handler.py index 4df20fd39..0739f29b6 100644 --- a/designate/mdns/handler.py +++ b/designate/mdns/handler.py @@ -20,6 +20,7 @@ import dns.rcode import dns.rdataclass import dns.rdatatype import dns.message +import six from oslo_config import cfg from oslo_log import log as logging @@ -110,8 +111,12 @@ class RequestHandler(xfr.XFRMixin): else: question = request.question[0] + name = question.name.to_text() + if six.PY3 and isinstance(name, bytes): + name = name.decode('utf-8') + criterion = { - 'name': question.name.to_text().decode('utf-8'), + 'name': name, 'type': 'SECONDARY', 'deleted': False } @@ -223,8 +228,11 @@ class RequestHandler(xfr.XFRMixin): # TODO(vinod) once validation is separated from the api, # validate the parameters try: + name = q_rrset.name.to_text() + if six.PY3 and isinstance(name, bytes): + name = name.decode('utf-8') criterion = self._zone_criterion_from_request( - request, {'name': q_rrset.name.to_text().decode('utf-8')}) + request, {'name': name}) zone = self.storage.find_zone(context, criterion) except exceptions.ZoneNotFound: @@ -340,10 +348,13 @@ class RequestHandler(xfr.XFRMixin): try: q_rrset = request.question[0] + name = q_rrset.name.to_text() + if six.PY3 and isinstance(name, bytes): + name = name.decode('utf-8') # TODO(vinod) once validation is separated from the api, # validate the parameters criterion = { - 'name': q_rrset.name.to_text().decode('utf-8'), + 'name': name, 'type': dns.rdatatype.to_text(q_rrset.rdtype), 'zones_deleted': False } diff --git a/designate/network_api/base.py b/designate/network_api/base.py index d444b04c8..39bd3beeb 100644 --- a/designate/network_api/base.py +++ b/designate/network_api/base.py @@ -14,6 +14,7 @@ # License for the specific language governing permissions and limitations # under the License. import eventlet.patcher +import six from oslo_config import cfg from oslo_log import log as logging @@ -108,4 +109,7 @@ class NetworkAPI(DriverPlugin): """ Get the name for the address """ - return reversename.from_address(address).to_text().decode('utf-8') + name = reversename.from_address(address).to_text() + if six.PY3 and isinstance(name, bytes): + name = name.decode('utf-8') + return name