Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Deleting a missing interface crashed #576

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 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 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 set_link_up(ipr: IPRoute, device_name: str):
"""Set the given interface up."""
ipr.link("set", index=ipr.link_lookup(ifname=device_name)[0], state="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=interface_index[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