From b0e28a160310613401315f23c7faac9e01403f4b Mon Sep 17 00:00:00 2001 From: Takashi Kajinami Date: Tue, 20 Feb 2024 19:01:32 +0900 Subject: [PATCH] kafka: Fix invalid hostaddr format for IPv6 address When IPv6 address is used for host, the hostaddr should be formatted in [
]: format instead of
: format. This ensures the correct format is used. Closes-Bug: 1907702 Change-Id: I6f4a453a69e942d5b2d66ffeca6960b85c8bc721 --- oslo_messaging/_drivers/impl_kafka.py | 7 ++++++- oslo_messaging/tests/drivers/test_impl_kafka.py | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/oslo_messaging/_drivers/impl_kafka.py b/oslo_messaging/_drivers/impl_kafka.py index 09abfc550..c102626dc 100644 --- a/oslo_messaging/_drivers/impl_kafka.py +++ b/oslo_messaging/_drivers/impl_kafka.py @@ -125,7 +125,12 @@ class Connection(object): LOG.warning("Different transport usernames detected") if host.hostname: - self.hostaddrs.append("%s:%s" % (host.hostname, host.port)) + if ':' in host.hostname: + hostaddr = "[%s]:%s" % (host.hostname, host.port) + else: + hostaddr = "%s:%s" % (host.hostname, host.port) + + self.hostaddrs.append(hostaddr) def reset(self): """Reset a connection so it can be used again.""" diff --git a/oslo_messaging/tests/drivers/test_impl_kafka.py b/oslo_messaging/tests/drivers/test_impl_kafka.py index 5e78369a1..4e5c84e20 100644 --- a/oslo_messaging/tests/drivers/test_impl_kafka.py +++ b/oslo_messaging/tests/drivers/test_impl_kafka.py @@ -66,6 +66,16 @@ class TestKafkaTransportURL(test_utils.BaseTestCase): username='stack', password='stacksecret', vhost='my_host'))), + ('ipv4', dict(url='kafka://127.0.0.1:1234', + expected=dict(hostaddrs=['127.0.0.1:1234'], + username=None, + password=None, + vhost=None))), + ('ipv6', dict(url='kafka://[::1]:1234', + expected=dict(hostaddrs=['[::1]:1234'], + username=None, + password=None, + vhost=None))), ] def setUp(self):