diff --git a/oslo_messaging/_drivers/impl_kafka.py b/oslo_messaging/_drivers/impl_kafka.py index c102626dc..089ea210e 100644 --- a/oslo_messaging/_drivers/impl_kafka.py +++ b/oslo_messaging/_drivers/impl_kafka.py @@ -20,6 +20,7 @@ from confluent_kafka import KafkaException from oslo_serialization import jsonutils from oslo_utils import eventletutils from oslo_utils import importutils +from oslo_utils import netutils from oslo_messaging._drivers import base from oslo_messaging._drivers import common as driver_common @@ -125,10 +126,8 @@ class Connection(object): LOG.warning("Different transport usernames detected") if host.hostname: - if ':' in host.hostname: - hostaddr = "[%s]:%s" % (host.hostname, host.port) - else: - hostaddr = "%s:%s" % (host.hostname, host.port) + hostaddr = "%s:%s" % (netutils.escape_ipv6(host.hostname), + host.port) self.hostaddrs.append(hostaddr) diff --git a/oslo_messaging/_drivers/impl_rabbit.py b/oslo_messaging/_drivers/impl_rabbit.py index 4c7e40d2a..c51b9f456 100644 --- a/oslo_messaging/_drivers/impl_rabbit.py +++ b/oslo_messaging/_drivers/impl_rabbit.py @@ -36,6 +36,7 @@ import kombu.messaging from oslo_config import cfg from oslo_log import log as logging from oslo_utils import eventletutils +from oslo_utils import netutils import oslo_messaging from oslo_messaging._drivers import amqp as rpc_amqp @@ -1001,16 +1002,10 @@ class Connection(object): transport, parse.quote(host.username or default_username), parse.quote(host.password or default_password), - self._parse_url_hostname(host.hostname) or default_hostname, + netutils.escape_ipv6(host.hostname) or default_hostname, str(host.port or 5672), url.virtual_host or '') - def _parse_url_hostname(self, hostname): - """Handles hostname returned from urlparse and checks whether it's - ipaddress. If it's ipaddress it ensures that it has brackets for IPv6. - """ - return '[%s]' % hostname if ':' in hostname else hostname - def _fetch_ssl_params(self): """Handles fetching what ssl params should be used for the connection (if any). diff --git a/oslo_messaging/tests/test_transport.py b/oslo_messaging/tests/test_transport.py index cb12c16ea..f296d43c0 100644 --- a/oslo_messaging/tests/test_transport.py +++ b/oslo_messaging/tests/test_transport.py @@ -305,7 +305,7 @@ class TestTransportUrlCustomisation(test_utils.BaseTestCase): "fake:/vhost1/localhost:5672/?l=1&l=2&l=3") self.url4 = transport_url_parse( "fake:/vhost2/localhost:5672/?d=x:1&d=y:2&d=z:3") - self.url5 = transport_url_parse("fake://noport:/?") + self.url5 = transport_url_parse("fake://noport/?") def test_hash(self): urls = {} diff --git a/oslo_messaging/transport.py b/oslo_messaging/transport.py index 337fb69aa..cb4213e41 100644 --- a/oslo_messaging/transport.py +++ b/oslo_messaging/transport.py @@ -21,6 +21,7 @@ import logging from debtcollector import removals from oslo_config import cfg +from oslo_utils import netutils from stevedore import driver from urllib import parse @@ -397,10 +398,7 @@ class TransportURL(object): # Build the network location portion of the transport URL if hostname: - if ':' in hostname: - netloc += '[%s]' % hostname - else: - netloc += hostname + netloc += netutils.escape_ipv6(hostname) if port is not None: netloc += ':%d' % port @@ -492,31 +490,7 @@ class TransportURL(object): password = parse.unquote(password) username = parse.unquote(username) - if not hostname: - hostname = None - elif hostname.startswith('['): - # Find the closing ']' and extract the hostname - host_end = hostname.find(']') - if host_end < 0: - # NOTE(Vek): Identical to what Python 2.7's - # urlparse.urlparse() raises in this case - raise ValueError('Invalid IPv6 URL') - - port_text = hostname[host_end:] - hostname = hostname[1:host_end] - - # Now we need the port; this is compliant with how urlparse - # parses the port data - port = None - if ':' in port_text: - port = port_text.split(':', 1)[1] - elif ':' in hostname: - hostname, port = hostname.split(':', 1) - - if port == "": - port = None - if port is not None: - port = int(port) + hostname, port = netutils.parse_host_port(hostname) if username is None or password is None: hosts_without_credentials.append(hostname)