openwisp / netjsonconfig

Network configuration management library based on NetJSON DeviceConfiguration
https://netjsonconfig.openwisp.org/
Other
359 stars 72 forks source link

OpenWRT Parse/Native - Error parsing route with no Gateway assigned #290

Closed shbatm closed 5 months ago

shbatm commented 11 months ago

Received an error trying to parse my OpenWRT config (open native) because there was a route that does not have a gateway assigned (wan exit route to reach the modem).

>>> router = OpenWrt(native=open('./backup-sc-router-2023-11-30.tar.gz'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/mnt/c/Users/timjb/src/router/venv/lib/python3.11/site-packages/netjsonconfig/backends/openwrt/openwrt.py", line 51, in __init__
    super().__init__(config, native, templates, context)
  File "/mnt/c/Users/timjb/src/router/venv/lib/python3.11/site-packages/netjsonconfig/backends/base/backend.py", line 51, in __init__
    self.parse(native)
  File "/mnt/c/Users/timjb/src/router/venv/lib/python3.11/site-packages/netjsonconfig/backends/base/backend.py", line 329, in parse
    self.to_netjson()
  File "/mnt/c/Users/timjb/src/router/venv/lib/python3.11/site-packages/netjsonconfig/backends/base/backend.py", line 342, in to_netjson
    value = converter.to_netjson()
            ^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/c/Users/timjb/src/router/venv/lib/python3.11/site-packages/netjsonconfig/backends/base/converter.py", line 116, in to_netjson
    result = self.to_netjson_loop(block, result, index + 1)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/c/Users/timjb/src/router/venv/lib/python3.11/site-packages/netjsonconfig/backends/openwrt/converters/routes.py", line 39, in to_netjson_loop
    rule = self.__netjson_route(block, index)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/c/Users/timjb/src/router/venv/lib/python3.11/site-packages/netjsonconfig/backends/openwrt/converters/routes.py", line 57, in __netjson_route
    "next": route.pop('gateway'),
            ^^^^^^^^^^^^^^^^^^^^
KeyError: 'gateway'

Valid config from OpenWRT:

config route
    option interface 'wan'
    option target '192.168.100.1'
    option netmask '255.255.255.255'
nemesifier commented 5 months ago

Thanks for reporting @shbatm. Pull request #290 should fix this.