From d1020da3eacff914259b3411f30ecb564d9f047c Mon Sep 17 00:00:00 2001 From: Ihar Hrachyshka Date: Fri, 26 Apr 2024 23:05:47 -0400 Subject: [PATCH] Catch all exceptions in convert_ip_to_canonical_format Recent netaddr started to raise AttributeError for subnetpools values like [{'start': ..., 'end': ...}]. The converter is meant to be graceful towards inputs that are not proper ip addresses. Instead of adding another exception type to the list and hope that the list of expected types covers everything netaddr may raise, this patch makes the converter catch all exceptions. Change-Id: I967032c1dea690cd9da95ad3a8e382671186d971 --- neutron_lib/api/converters.py | 7 ++++++- neutron_lib/tests/unit/api/test_converters.py | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/neutron_lib/api/converters.py b/neutron_lib/api/converters.py index e8fd00d45..18d4ab835 100644 --- a/neutron_lib/api/converters.py +++ b/neutron_lib/api/converters.py @@ -196,7 +196,12 @@ def convert_ip_to_canonical_format(value): ip = netaddr.IPAddress(value) if ip.version == constants.IP_VERSION_6: return str(ip.format(dialect=netaddr.ipv6_compact)) - except (netaddr.core.AddrFormatError, ValueError): + except Exception: # nosec B110 + # netaddr may raise all kinds of exceptions (ValueError, + # AttributeError...) if the input is not a valid IP address. Instead of + # catching them one by one, just catch all exceptions at once. + # Obviously, it would be better if netaddr raised a particular + # exception specific to the library. But we don't control it. pass return value diff --git a/neutron_lib/tests/unit/api/test_converters.py b/neutron_lib/tests/unit/api/test_converters.py index 469ff4a8b..0ab61c5d3 100644 --- a/neutron_lib/tests/unit/api/test_converters.py +++ b/neutron_lib/tests/unit/api/test_converters.py @@ -209,6 +209,11 @@ class TestConvertIPv6AddrCanonicalFormat(base.BaseTestCase): '2001:db8:0:1:1:1:1:1/128') self.assertEqual('2001:db8:0:1:1:1:1:1/128', result) + def test_convert_subnetpools(self): + pools = [{'start': '1.1.1.1', 'end': '1.1.1.100'}] + result = converters.convert_ip_to_canonical_format(pools) + self.assertEqual(pools, result) + class TestConvertIPv6CIDRCanonicalFormat(base.BaseTestCase):