From 6835640c515191b2b0a697fc4d065e575bc1e0c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Harald=20Jens=C3=A5s?= Date: Mon, 18 Sep 2023 20:56:29 +0200 Subject: [PATCH] redfish_address - wrap_ipv6 address When parsing redfish driver info wrap IPv6 address in brackets before appending default scheme/authority. Updated common.utils.wrap_ipv6() to ignore ValueError, e.g simply return the string if ip is not an ipv6 address string. Related: RHBZ#2239356 Closes-Bug: #2036454 Change-Id: Icefd96d6873474b4cfb7fbf3d8337cd42fd63ca6 (cherry picked from commit 72037b596af140f4930f2c2f17c397be01e89f0f) --- ironic/common/utils.py | 8 ++++++-- ironic/drivers/modules/redfish/utils.py | 3 ++- ironic/tests/unit/common/test_utils.py | 6 ++++++ ironic/tests/unit/drivers/modules/redfish/test_utils.py | 8 ++++++++ 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/ironic/common/utils.py b/ironic/common/utils.py index 80865a90ba..7504ddea5a 100644 --- a/ironic/common/utils.py +++ b/ironic/common/utils.py @@ -572,8 +572,12 @@ def pop_node_nested_field(node, collection, field, default=None): def wrap_ipv6(ip): """Wrap the address in square brackets if it's an IPv6 address.""" - if ipaddress.ip_address(ip).version == 6: - return "[%s]" % ip + try: + if ipaddress.ip_address(ip).version == 6: + return "[%s]" % ip + except ValueError: + pass + return ip diff --git a/ironic/drivers/modules/redfish/utils.py b/ironic/drivers/modules/redfish/utils.py index 1dffe1a1ac..078c6ef807 100644 --- a/ironic/drivers/modules/redfish/utils.py +++ b/ironic/drivers/modules/redfish/utils.py @@ -27,6 +27,7 @@ import tenacity from ironic.common import exception from ironic.common.i18n import _ +from ironic.common import utils from ironic.conf import CONF sushy = importutils.try_import('sushy') @@ -96,7 +97,7 @@ def parse_driver_info(node): 'info': missing_info}) # Validate the Redfish address - address = driver_info['redfish_address'] + address = utils.wrap_ipv6(driver_info['redfish_address']) try: parsed = rfc3986.uri_reference(address) except TypeError: diff --git a/ironic/tests/unit/common/test_utils.py b/ironic/tests/unit/common/test_utils.py index d5d8b42dff..90ec13f60b 100644 --- a/ironic/tests/unit/common/test_utils.py +++ b/ironic/tests/unit/common/test_utils.py @@ -321,6 +321,12 @@ class GenericUtilsTestCase(base.TestCase): self.assertFalse(utils.is_fips_enabled()) m.assert_called_once_with('/proc/sys/crypto/fips_enabled', 'r') + def test_wrap_ipv6(self): + self.assertEqual('[2001:DB8::1]', utils.wrap_ipv6('2001:DB8::1')) + self.assertEqual('example.com', utils.wrap_ipv6('example.com')) + self.assertEqual('192.168.24.1', utils.wrap_ipv6('192.168.24.1')) + self.assertEqual('[2001:DB8::1]', utils.wrap_ipv6('[2001:DB8::1]')) + class TempFilesTestCase(base.TestCase): diff --git a/ironic/tests/unit/drivers/modules/redfish/test_utils.py b/ironic/tests/unit/drivers/modules/redfish/test_utils.py index ca8aba9da8..5734f00e43 100644 --- a/ironic/tests/unit/drivers/modules/redfish/test_utils.py +++ b/ironic/tests/unit/drivers/modules/redfish/test_utils.py @@ -168,6 +168,14 @@ class RedfishUtilsTestCase(db_base.DbTestCase): response = redfish_utils.parse_driver_info(self.node) self.assertEqual(self.parsed_driver_info, response) + def test_parse_driver_info_default_scheme_ipv6_brackets_added(self): + test_redfish_address = '2001:DB8::1' + self.node.driver_info['redfish_address'] = test_redfish_address + response = redfish_utils.parse_driver_info(self.node) + self.parsed_driver_info['address'] = ("https://[%s]" + % test_redfish_address) + self.assertEqual(self.parsed_driver_info, response) + def test_get_task_monitor(self): redfish_utils._get_connection = mock.Mock() fake_monitor = mock.Mock()