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 51eaa9d
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/aleph/vm/network/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,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 ValueError(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 +55,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 ValueError(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:
Expand Down

0 comments on commit 51eaa9d

Please sign in to comment.