Skip to content

Commit dae2fb9

Browse files
Enable negation when parsing monthdays and weekdays
This change enables parsing of the "!" character for monthdays and weekdays paraemters of a firewall redirect object when present in UCI configuration. This change does not add support for negation in NetJSON config.
1 parent b9cf322 commit dae2fb9

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

netjsonconfig/backends/openwrt/converters/firewall.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,31 @@ def __netjson_redirect(self, redirect):
205205
redirect["proto"] = ["tcp", "udp"]
206206
else:
207207
redirect["proto"] = [proto]
208+
209+
if "weekdays" in redirect:
210+
weekdays = redirect["weekdays"]
211+
if not isinstance(weekdays, list):
212+
weekdays = weekdays.split()
213+
# UCI allows the first entry to be "!" which means negate the remaining
214+
# entries
215+
if weekdays[0] == "!":
216+
all_days = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"]
217+
wd = set([x for x in weekdays[1:]])
218+
redirect["weekdays"] = list(set(all_days) - wd)
219+
# Sort the days for predictability when testing
220+
redirect["weekdays"].sort(key=lambda v: all_days.index(v))
221+
208222
if "monthdays" in redirect:
209-
redirect["monthdays"] = [int(x) for x in redirect["monthdays"]]
223+
monthdays = redirect["monthdays"]
224+
if not isinstance(monthdays, list):
225+
monthdays = monthdays.split()
226+
# UCI allows the first entry to be "!" which means negate the remaining
227+
# entries
228+
if monthdays[0] == "!":
229+
all_days = set(range(1,32))
230+
md = set([int(x) for x in monthdays[1:]])
231+
redirect["monthdays"] = list(all_days - md)
232+
else:
233+
redirect["monthdays"] = [int(x) for x in monthdays]
210234

211235
return self.type_cast(redirect)

tests/openwrt/test_firewall.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,3 +484,43 @@ def test_redirect_monthdays_validation_error_2(self):
484484
o = OpenWrt({"firewall": {"redirects": [{"monthdays": [0, 2, 8]}]}})
485485
with self.assertRaises(ValidationError):
486486
o.validate()
487+
488+
_redirect_3_uci = textwrap.dedent(
489+
"""\
490+
package firewall
491+
492+
config defaults 'defaults'
493+
494+
config redirect 'redirect_Adblock DNS, port 53'
495+
option name 'Adblock DNS, port 53'
496+
option src 'lan'
497+
option proto 'tcpudp'
498+
option src_dport '53'
499+
option dest_port '53'
500+
option target 'DNAT'
501+
option weekdays '! mon tue wed'
502+
option monthdays '! 1 2 3 4 5'
503+
"""
504+
)
505+
506+
_redirect_3_netjson = {
507+
"firewall": {
508+
"redirects": [
509+
{
510+
"name": "Adblock DNS, port 53",
511+
"src": "lan",
512+
"proto": ["tcp", "udp"],
513+
"src_dport": "53",
514+
"dest_port": "53",
515+
"target": "DNAT",
516+
"weekdays": ["sun", "thu", "fri", "sat"],
517+
"monthdays": [6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31],
518+
}
519+
]
520+
}
521+
}
522+
523+
def test_parse_redirect_3(self):
524+
o = OpenWrt(native=self._redirect_3_uci)
525+
print(o.config)
526+
self.assertEqual(o.config, self._redirect_3_netjson)

0 commit comments

Comments
 (0)