Skip to content

Commit

Permalink
Fix: Deleting a missing interface crashed
Browse files Browse the repository at this point in the history
Calling `delete_tap_interface` on an interface that is not present raised an error. This logs a debug message instead.

When the interface is not found for adding an IP address or setting a link up, a more explicit error is raised.
  • Loading branch information
hoh committed Mar 14, 2024
1 parent 1c0761d commit 3ef5692
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
20 changes: 18 additions & 2 deletions src/aleph/vm/network/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
logger = logging.getLogger(__name__)


class MissingInterfaceError(Exception):
"""The interface is missing."""

pass


class InterfaceBusyError(Exception):
"""The interface is busy."""

Expand Down Expand Up @@ -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")
Expand All @@ -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.")

Check warning on line 66 in src/aleph/vm/network/interfaces.py

View check run for this annotation

Codecov / codecov/patch

src/aleph/vm/network/interfaces.py#L66

Added line #L66 was not covered by tests
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

Check warning on line 74 in src/aleph/vm/network/interfaces.py

View check run for this annotation

Codecov / codecov/patch

src/aleph/vm/network/interfaces.py#L73-L74

Added lines #L73 - L74 were not covered by tests
ipr.link("del", index=interface_index[0])


class TapInterface:
Expand Down
3 changes: 2 additions & 1 deletion tests/supervisor/test_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pyroute2 import IPRoute

from aleph.vm.network.interfaces import (
MissingInterfaceError,
add_ip_address,
create_tap_interface,
delete_tap_interface,
Expand Down Expand Up @@ -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)


Expand Down

0 comments on commit 3ef5692

Please sign in to comment.