From: John Mulligan Date: Thu, 27 Jul 2023 18:17:36 +0000 (-0400) Subject: python-common: fix valid_addr on python 3.11 X-Git-Tag: v18.2.5~71^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=d46c898fc5deacc7007e2a9e2fc5286b9e84707b;p=ceph.git python-common: fix valid_addr on python 3.11 The behavior on python 3.11 regarding IPv4 addresses in bracket has changed: ``` $ python3.8 -c 'from urllib.parse import urlparse; urlparse("http://[192.168.0.1]")' [john@edfu ~]$ python3.11 -c 'from urllib.parse import urlparse; urlparse("http://[192.168.0.1]")' Traceback (most recent call last): File "", line 1, in File "/usr/lib64/python3.11/urllib/parse.py", line 395, in urlparse splitresult = urlsplit(url, scheme, allow_fragments) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib64/python3.11/urllib/parse.py", line 500, in urlsplit _check_bracketed_host(bracketed_host) File "/usr/lib64/python3.11/urllib/parse.py", line 448, in _check_bracketed_host raise ValueError(f"An IPv4 address cannot be in brackets") ValueError: An IPv4 address cannot be in brackets ``` This breaks the test in test_valid_addr that asserts that function valid_addr returns the string "IPv4 address wrapped in brackets is invalid". Move the step that checks for brackets and dots above the urllib check so that the function continues returning the expected string. Signed-off-by: John Mulligan (cherry picked from commit ef72af7d720f8811368f25cb6f613ccaf5dcd55d) --- diff --git a/src/python-common/ceph/deployment/utils.py b/src/python-common/ceph/deployment/utils.py index 6aad15b75b6b..f800e3738970 100644 --- a/src/python-common/ceph/deployment/utils.py +++ b/src/python-common/ceph/deployment/utils.py @@ -70,6 +70,9 @@ def valid_addr(addr: str) -> Tuple[bool, str]: colons = addr.count(':') addr_as_url = f'http://{addr}' + if addr.startswith('[') and dots: + return False, "IPv4 address wrapped in brackets is invalid" + try: res = urlparse(addr_as_url) except ValueError as e: @@ -89,9 +92,6 @@ def valid_addr(addr: str) -> Tuple[bool, str]: elif ']:' in addr: return False, 'Port must be numeric' - if addr.startswith('[') and dots: - return False, "IPv4 address wrapped in brackets is invalid" - # catch partial address like 10.8 which would be valid IPaddress schemes # but are classed as invalid here since they're not usable if dots and addr[0].isdigit() and dots != 3: