From 4354bc04f95c03fc90d7b596422874e329996660 Mon Sep 17 00:00:00 2001 From: Dmitry Tantsur Date: Wed, 27 Nov 2019 19:08:07 +0100 Subject: [PATCH] Replace netaddr dependency with stdlib ipaddress netaddr is quite a big library, and all we need is covered by the built-in ipaddress module (available in python 3). Also add a safeguard for invalid 'ip route' output. Change-Id: I9d76a8d1c1b6b1585e301a9c63b37fab3b98746f --- ironic_python_agent/agent.py | 20 ++++++++++++++------ ironic_python_agent/hardware.py | 14 +++++++------- lower-constraints.txt | 1 - requirements.txt | 1 - 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/ironic_python_agent/agent.py b/ironic_python_agent/agent.py index c9fe2acc1..25a08d59d 100644 --- a/ironic_python_agent/agent.py +++ b/ironic_python_agent/agent.py @@ -13,6 +13,7 @@ # limitations under the License. import collections +import ipaddress import os import random import select @@ -23,7 +24,6 @@ from wsgiref import simple_server from ironic_lib import exception as lib_exc from ironic_lib import mdns -import netaddr from oslo_concurrency import processutils from oslo_config import cfg from oslo_log import log @@ -238,14 +238,22 @@ class IronicPythonAgent(base.ExecuteCommandMixin): try: source = out.strip().split('\n')[0].split('src')[1].split()[0] - if netaddr.IPAddress(source).is_link_local(): - LOG.info('Ignoring link-local source to %(dest)s: %(rec)s', - {'dest': dest, 'rec': out}) - return - return source except IndexError: LOG.warning('No route to host %(dest)s, route record: %(rec)s', {'dest': dest, 'rec': out}) + return + + try: + if ipaddress.ip_address(source).is_link_local: + LOG.info('Ignoring link-local source to %(dest)s: %(rec)s', + {'dest': dest, 'rec': out}) + return + except ValueError as exc: + LOG.warning('Invalid IP address %(addr)s returned as a route ' + 'to host %(dest)s: %(err)s', + {'dest': dest, 'addr': source, 'err': exc}) + + return source def set_agent_advertise_addr(self): """Set advertised IP address for the agent, if not already set. diff --git a/ironic_python_agent/hardware.py b/ironic_python_agent/hardware.py index 79f7ac8f6..fd0b3f910 100644 --- a/ironic_python_agent/hardware.py +++ b/ironic_python_agent/hardware.py @@ -15,6 +15,7 @@ import abc import binascii import functools +import ipaddress import json from multiprocessing.pool import ThreadPool import os @@ -24,7 +25,6 @@ import time from ironic_lib import disk_utils from ironic_lib import utils as il_utils -import netaddr from oslo_concurrency import processutils from oslo_config import cfg from oslo_log import log @@ -1320,9 +1320,9 @@ class GenericHardwareManager(HardwareManager): out = out.strip() try: - netaddr.IPAddress(out) - except netaddr.AddrFormatError: - LOG.warning('Invalid IP address: %s', out) + ipaddress.ip_address(out) + except ValueError as exc: + LOG.warning('Invalid IP address %s: %s', out, exc) continue # In case we get 0.0.0.0 on a valid channel, we need to keep @@ -1397,9 +1397,9 @@ class GenericHardwareManager(HardwareManager): continue try: - return str(netaddr.IPNetwork(address).ip) - except netaddr.AddrFormatError: - LOG.warning('Invalid IP address: %s', address) + return str(ipaddress.ip_interface(address).ip) + except ValueError as exc: + LOG.warning('Invalid IP address %s: %s', address, exc) continue except (processutils.ProcessExecutionError, OSError) as e: # Not error, because it's normal in virtual environment diff --git a/lower-constraints.txt b/lower-constraints.txt index ddf4e85cd..038fffdda 100644 --- a/lower-constraints.txt +++ b/lower-constraints.txt @@ -37,7 +37,6 @@ mock==2.0.0 monotonic==1.4 mox3==0.25.0 msgpack==0.5.6 -netaddr==0.7.18 netifaces==0.10.4 openstackdocstheme==1.20.0 os-client-config==1.29.0 diff --git a/requirements.txt b/requirements.txt index 77b1c1539..47e6c6b18 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,7 +3,6 @@ # process, which may cause wedges in the gate later. pbr!=2.1.0,>=2.0.0 # Apache-2.0 eventlet!=0.18.3,!=0.20.1,>=0.18.2 # MIT -netaddr>=0.7.18 # BSD netifaces>=0.10.4 # MIT oslo.config>=5.2.0 # Apache-2.0 oslo.concurrency>=3.26.0 # Apache-2.0