diff --git a/src/aleph/vm/network/interfaces.py b/src/aleph/vm/network/interfaces.py index 1a79892cc..6ad75acc5 100644 --- a/src/aleph/vm/network/interfaces.py +++ b/src/aleph/vm/network/interfaces.py @@ -13,6 +13,12 @@ logger = logging.getLogger(__name__) +class MissingInterfaceError(Exception): + """The interface is missing.""" + + pass + + class InterfaceBusyError(Exception): """The interface is busy.""" @@ -41,8 +47,11 @@ def create_tap_interface(ipr: IPRoute, device_name: str): def add_ip_address(ipr: IPRoute, device_name: str, ip: Union[IPv4Interface, IPv6Interface]): """Add an IP address to the given interface. If the address already exists, a warning is logged and the function returns without error.""" + interface_index: list[int] = ipr.link_lookup(ifname=device_name) + if not interface_index: + raise MissingInterfaceError(f"Interface {device_name} does not exist, can't add address {ip} to it.") try: - ipr.addr("add", index=ipr.link_lookup(ifname=device_name)[0], address=str(ip.ip), mask=ip.network.prefixlen) + ipr.addr("add", index=interface_index[0], address=str(ip.ip), mask=ip.network.prefixlen) except NetlinkError as e: if e.code == 17: logger.warning(f"Address {ip} already exists") @@ -52,11 +61,18 @@ def add_ip_address(ipr: IPRoute, device_name: str, ip: Union[IPv4Interface, IPv6 def set_link_up(ipr: IPRoute, device_name: str): """Set the given interface up.""" + interface_index: list[int] = ipr.link_lookup(ifname=device_name) + if not interface_index: + raise MissingInterfaceError(f"Interface {device_name} does not exist, can't set it up.") ipr.link("set", index=ipr.link_lookup(ifname=device_name)[0], state="up") def delete_tap_interface(ipr: IPRoute, device_name: str): - ipr.link("del", index=ipr.link_lookup(ifname=device_name)[0]) + interface_index: list[int] = ipr.link_lookup(ifname=device_name) + if not interface_index: + logger.debug(f"Interface {device_name} does not exist, won't be deleted.") + return + ipr.link("del", index=interface_index[0]) class TapInterface: diff --git a/tests/supervisor/test_interfaces.py b/tests/supervisor/test_interfaces.py index a309e8796..79868b85e 100644 --- a/tests/supervisor/test_interfaces.py +++ b/tests/supervisor/test_interfaces.py @@ -5,6 +5,7 @@ from pyroute2 import IPRoute from aleph.vm.network.interfaces import ( + MissingInterfaceError, add_ip_address, create_tap_interface, delete_tap_interface, @@ -45,7 +46,7 @@ def test_add_ip_address(): run(["ip", "tuntap", "del", test_device_name, "mode", "tap"], check=False) # Without an interface, the function should raise an error - with pytest.raises(IndexError): + with pytest.raises(MissingInterfaceError): add_ip_address(IPRoute(), test_device_name, test_ipv4)