rabbit: fix ipv6 support

Url constructed for kombu cannot contain ipv6 address without
brackets. The url.hostname returns hosts without brackets so
we have to readd them.

Change-Id: Ifb1d358a67655af99a84b77ca813fc2dd87d87dc
This commit is contained in:
Lukas Bezdicka 2015-03-19 15:09:52 +01:00
parent a2a87eebbf
commit 9f137948ae
3 changed files with 29 additions and 2 deletions

View File

@ -623,7 +623,8 @@ class Connection(object):
transport,
parse.quote(host.username or ''),
parse.quote(host.password or ''),
host.hostname or '', str(host.port or 5672),
self._parse_url_hostname(host.hostname) or '',
str(host.port or 5672),
virtual_host)
elif url.transport.startswith('kombu+'):
# NOTE(sileht): url have a + but no hosts
@ -638,7 +639,7 @@ class Connection(object):
";" if self._url else '',
parse.quote(self.driver_conf.rabbit_userid),
parse.quote(self.driver_conf.rabbit_password),
hostname, port,
self._parse_url_hostname(hostname), port,
virtual_host)
self._initial_pid = os.getpid()
@ -726,6 +727,12 @@ class Connection(object):
except KeyError:
raise RuntimeError(_("Invalid SSL version : %s") % version)
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).

View File

@ -228,6 +228,13 @@ class TestRabbitTransportURL(test_utils.BaseTestCase):
expected=["amqp://user:password@host:10/virtual_host",
"amqp://user2:password2@host2:12/virtual_host"]
)),
('rabbit_ipv6',
dict(url='rabbit://u:p@[fd00:beef:dead:55::133]:10/vhost',
skip_py26='python 2.6 has broken urlparse for ipv6',
expected=['amqp://u:p@[fd00:beef:dead:55::133]:10/vhost'])),
('rabbit_ipv4',
dict(url='rabbit://user:password@10.20.30.40:10/vhost',
expected=['amqp://user:password@10.20.30.40:10/vhost'])),
]
def setUp(self):
@ -239,6 +246,9 @@ class TestRabbitTransportURL(test_utils.BaseTestCase):
@mock.patch('oslo_messaging._drivers.impl_rabbit.Connection.ensure')
@mock.patch('oslo_messaging._drivers.impl_rabbit.Connection.reset')
def test_transport_url(self, fake_reset, fake_ensure):
if hasattr(self, 'skip_py26') and sys.version_info < (2, 7):
self.skipTest(self.skip_py26)
transport = oslo_messaging.get_transport(self.conf, self.url)
self.addCleanup(transport.cleanup)
driver = transport._driver

View File

@ -141,6 +141,13 @@ class TestRabbitTransportURL(test_utils.BaseTestCase):
('rabbit',
dict(url='kombu+rabbit://user:password@host:10/virtual_host',
expected=['amqp://user:password@host:10/virtual_host'])),
('rabbit_ipv6',
dict(url='kombu+rabbit://u:p@[fd00:beef:dead:55::133]:10/vhost',
skip_py26='python 2.6 has broken urlparse for ipv6',
expected=['amqp://u:p@[fd00:beef:dead:55::133]:10/vhost'])),
('rabbit_ipv4',
dict(url='kombu+rabbit://user:password@10.20.30.40:10/vhost',
expected=['amqp://user:password@10.20.30.40:10/vhost'])),
]
def setUp(self):
@ -152,6 +159,9 @@ class TestRabbitTransportURL(test_utils.BaseTestCase):
@mock.patch('oslo_messaging._drivers.impl_rabbit.Connection.ensure')
@mock.patch('oslo_messaging._drivers.impl_rabbit.Connection.reset')
def test_transport_url(self, fake_ensure_connection, fake_reset):
if hasattr(self, 'skip_py26') and sys.version_info < (2, 7):
self.skipTest(self.skip_py26)
transport = messaging.get_transport(self.conf, self.url)
self.addCleanup(transport.cleanup)
driver = transport._driver