Skip to content

Commit

Permalink
netmap: add list-devices (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
gsanchietti authored Feb 20, 2024
1 parent ffb0142 commit 139ecea
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/nethsec/firewall/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1820,3 +1820,24 @@ def delete_netmap_rule(uci, id: str) -> str:
uci.delete('netmap', id)
uci.save('netmap')
return id

def list_netmap_devices(uci) -> list:
"""
Get all network devices from ip command
Args:
uci: EUci pointer
Returns:
a list of all network devices
"""
devices = []
try:
output = subprocess.run(["ip", "-j", "link", "show"], capture_output=True, text=True)
data = json.loads(output.stdout)
for device in data:
if device.get('ifname') != 'lo' and not device.get('ifname').startswith('ifb-'):
devices.append({"device": device['ifname'], "interface": utils.get_interface_from_device(uci, device['ifname'])})
except:
pass
return devices
19 changes: 19 additions & 0 deletions tests/test_firewall.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from nethsec import firewall
from pytest_mock import MockFixture
from unittest.mock import MagicMock, patch

firewall_db = """
config zone lan1
Expand Down Expand Up @@ -361,6 +362,11 @@
1704874398 ac:57:26:00:24:8c 192.168.1.219 test2 01:dc:57:26:00:25:8c
"""

# Setup fake ip command output
ip_json='[{"ifindex":9,"ifname":"vnet3","flags":["BROADCAST","MULTICAST","UP","LOWER_UP"],"mtu":1500,"qdisc":"noqueue","master":"virbr2","operstate":"UNKNOWN","group":"default","txqlen":1000,"link_type":"ether","address":"fe:62:31:19:0b:29","broadcast":"ff:ff:ff:ff:ff:ff","addr_info":[{"family":"inet6","local":"fe80::fc62:31ff:fe19:b29","prefixlen":64,"scope":"link","valid_life_time":4294967295,"preferred_life_time":4294967295}]},{"ifindex":2,"ifname":"eth0","flags":["BROADCAST","MULTICAST","UP","LOWER_UP"],"mtu":1500,"qdisc":"fq_codel","master":"br-lan","operstate":"UP","linkmode":"DEFAULT","group":"default","txqlen":1000,"link_type":"ether","address":"52:54:00:6a:50:bf","broadcast":"ff:ff:ff:ff:ff:ff"},{"ifindex":3,"ifname":"eth1","flags":["BROADCAST","MULTICAST","UP","LOWER_UP"],"mtu":1500,"qdisc":"fq_codel","operstate":"UP","linkmode":"DEFAULT","group":"default","txqlen":1000,"link_type":"ether","address":"52:54:00:20:82:a6","broadcast":"ff:ff:ff:ff:ff:ff"},{"ifindex":4,"ifname":"eth2","flags":["BROADCAST","MULTICAST"],"mtu":1500,"qdisc":"noop","operstate":"DOWN","linkmode":"DEFAULT","group":"default","txqlen":1000,"link_type":"ether","address":"52:54:00:75:1c:c1","broadcast":"ff:ff:ff:ff:ff:ff"},{"ifindex":5,"ifname":"eth3","flags":["BROADCAST","MULTICAST"],"mtu":1500,"qdisc":"noop","operstate":"DOWN","linkmode":"DEFAULT","group":"default","txqlen":1000,"link_type":"ether","address":"52:54:00:ad:6f:63","broadcast":"ff:ff:ff:ff:ff:ff"},{"ifindex":6,"ifname":"ifb-dns","flags":["BROADCAST","NOARP","UP","LOWER_UP"],"mtu":1500,"qdisc":"fq_codel","operstate":"UNKNOWN","linkmode":"DEFAULT","group":"default","txqlen":32,"link_type":"ether","address":"72:79:65:12:07:07","broadcast":"ff:ff:ff:ff:ff:ff"},{"ifindex":7,"ifname":"br-lan","flags":["BROADCAST","MULTICAST","UP","LOWER_UP"],"mtu":1500,"qdisc":"noqueue","operstate":"UP","linkmode":"DEFAULT","group":"default","txqlen":1000,"link_type":"ether","address":"52:54:00:6a:50:bf","broadcast":"ff:ff:ff:ff:ff:ff"},{"ifindex":9,"ifname":"bond-bond1","flags":["BROADCAST","MULTICAST","MASTER","UP","LOWER_UP"],"mtu":1500,"qdisc":"noqueue","operstate":"UP","linkmode":"DEFAULT","group":"default","txqlen":1000,"link_type":"ether","address":"52:54:00:ad:6f:63","broadcast":"ff:ff:ff:ff:ff:ff"},{"ifindex":8,"ifname":"tuntunsubnet","flags":["POINTOPOINT","MULTICAST","NOARP","UP","LOWER_UP"],"mtu":1500,"qdisc":"fq_codel","operstate":"UNKNOWN","linkmode":"DEFAULT","group":"default","txqlen":500,"link_type":"none"}, {"ifindex":69,"ifname":"pppoe-w1","flags":["POINTOPOINT","MULTICAST","NOARP","UP","LOWER_UP"],"mtu":1492,"qdisc":"fq_codel","operstate":"UNKNOWN","linkmode":"DEFAULT","group":"default","txqlen":3,"link_type":"ppp"},{"ifindex":20,"link":"eth1","ifname":"eth1.4","flags":["BROADCAST","MULTICAST","UP","LOWER_UP"],"mtu":1500,"qdisc":"noqueue","master":"br-lan","operstate":"UP","linkmode":"DEFAULT","group":"default","txqlen":1000,"link_type":"ether","address":"52:54:00:20:82:a6","broadcast":"ff:ff:ff:ff:ff:ff"}]'
mock_ip_stdout = MagicMock()
mock_ip_stdout.configure_mock(**{"stdout": ip_json})

def _setup_db(tmp_path):
# setup fake dbs
with tmp_path.joinpath('firewall').open('w') as fp:
Expand Down Expand Up @@ -990,3 +996,16 @@ def test_delete_netmap_rule(tmp_path):
firewall.delete_netmap_rule(u, "notpresent")
id = firewall.add_netmap_rule(u, "myrule3b", "", "10.50.51.0/24", None, None, "10.10.11.0/24", "192.168.2.0/24")
assert firewall.delete_netmap_rule(u, id) == id

@patch("nethsec.utils.subprocess.run")
def test_list_netmap_devices(mock_run, tmp_path):
# setup mock
u = _setup_db(tmp_path)
mock_run.return_value = mock_ip_stdout
devices = firewall.list_netmap_devices(u)
device_names = [d.get("device") for d in devices]
assert 'lo' not in device_names
assert 'br-lan' in device_names
assert 'eth1.4' in device_names
assert {'device': 'br-lan', 'interface': 'lan'} in devices

0 comments on commit 139ecea

Please sign in to comment.