Skip to content

Commit 0c70308

Browse files
Fix redirect weekdays and monthdays handling
Treat these parameters as an array, and drop support for negating the values with a "!" in JSON as it is unneeded.
1 parent a9cb284 commit 0c70308

File tree

3 files changed

+104
-12
lines changed

3 files changed

+104
-12
lines changed

netjsonconfig/backends/openwrt/converters/firewall.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,10 @@ def __intermediate_redirects(self, redirects):
131131
redirect["proto"] = proto[0]
132132
elif set(proto) == {"tcp", "udp"}:
133133
redirect["proto"] = "tcpudp"
134+
134135
resultdict.update(redirect)
135136
result.append(resultdict)
137+
136138
return result
137139

138140
def __get_auto_name_redirect(self, redirect):
@@ -203,5 +205,7 @@ def __netjson_redirect(self, redirect):
203205
redirect["proto"] = ["tcp", "udp"]
204206
else:
205207
redirect["proto"] = [proto]
208+
if "monthdays" in redirect:
209+
redirect["monthdays"] = [int(x) for x in redirect["monthdays"]]
206210

207211
return self.type_cast(redirect)

netjsonconfig/backends/openwrt/schema.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -873,27 +873,44 @@
873873
"pattern": time_regex,
874874
"propertyOrder": 17,
875875
},
876-
# FIXME: regex needed. Also, should this be an array?
876+
# Note: here we don't support negation of values like
877+
# the UCI syntax does, as it's not necessary.
877878
"weekdays": {
878-
"type": "string",
879+
"type": "array",
879880
"title": "weekdays",
880881
"description": "Only match traffic during the given week days, "
881-
'e.g. "sun mon thu fri" to only match on Sundays, Mondays, Thursdays and '
882-
"Fridays. The list can be inverted by prefixing it with an exclamation "
883-
'mark, e.g. "! sat sun" to always match but not on Saturdays and '
884-
"Sundays.",
882+
'e.g. ["sun", "mon", "thu", "fri"] to only match on Sundays, '
883+
"Mondays, Thursdays and Fridays.",
885884
"propertyOrder": 18,
885+
"items": {
886+
"type": "string",
887+
"title": "weekday",
888+
"enum": [
889+
"mon",
890+
"tue",
891+
"wed",
892+
"thur",
893+
"fri",
894+
"sat",
895+
"sun",
896+
],
897+
},
886898
},
887-
# FIXME: regex needed. Also, should this be an array?
899+
# Note: here we don't support negation of values like
900+
# the UCI syntax does, as it's not necessary.
888901
"monthdays": {
889-
"type": "string",
902+
"type": "array",
890903
"title": "monthdays",
891904
"description": "Only match traffic during the given days of the "
892-
'month, e.g. "2 5 30" to only match on every 2nd, 5th and 30th '
893-
"day of the month. The list can be inverted by prefixing it with "
894-
'an exclamation mark, e.g. "! 31" to always match but on the '
895-
"31st of the month.",
905+
"month, e.g. [2, 5, 30] to only match on every 2nd, 5th and 30th "
906+
"day of the month.",
896907
"propertyOrder": 19,
908+
"items": {
909+
"type": "integer",
910+
"title": "day of month",
911+
"minimum": 1,
912+
"maximum": 31,
913+
},
897914
},
898915
"utc_time": {
899916
"type": "boolean",

tests/openwrt/test_firewall.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,3 +413,74 @@ def test_render_redirect_1(self):
413413
def test_parse_redirect_1(self):
414414
o = OpenWrt(native=self._redirect_1_uci)
415415
self.assertEqual(o.config, self._redirect_1_netjson)
416+
417+
_redirect_2_netjson = {
418+
"firewall": {
419+
"redirects": [
420+
{
421+
"name": "Adblock DNS, port 53",
422+
"src": "lan",
423+
"proto": ["tcp", "udp"],
424+
"src_dport": "53",
425+
"dest_port": "53",
426+
"target": "DNAT",
427+
# Contrived, unrealistic example for testing
428+
"weekdays": ["mon", "tue", "wed"],
429+
"monthdays": [1, 2, 3, 29, 30],
430+
}
431+
]
432+
}
433+
}
434+
435+
_redirect_2_uci = textwrap.dedent(
436+
"""\
437+
package firewall
438+
439+
config defaults 'defaults'
440+
441+
config redirect 'redirect_Adblock DNS, port 53'
442+
option name 'Adblock DNS, port 53'
443+
option src 'lan'
444+
option proto 'tcpudp'
445+
option src_dport '53'
446+
option dest_port '53'
447+
option target 'DNAT'
448+
list weekdays 'mon'
449+
list weekdays 'tue'
450+
list weekdays 'wed'
451+
list monthdays '1'
452+
list monthdays '2'
453+
list monthdays '3'
454+
list monthdays '29'
455+
list monthdays '30'
456+
"""
457+
)
458+
459+
def test_render_redirect_2(self):
460+
o = OpenWrt(self._redirect_2_netjson)
461+
expected = self._tabs(self._redirect_2_uci)
462+
self.assertEqual(o.render(), expected)
463+
464+
def test_parse_redirect_2(self):
465+
o = OpenWrt(native=self._redirect_2_uci)
466+
self.assertEqual(o.config, self._redirect_2_netjson)
467+
468+
def test_redirect_weekdays_validation_error_1(self):
469+
o = OpenWrt({"firewall": {"redirects": [{"weekdays": ["mon", "xxx"]}]}})
470+
with self.assertRaises(ValidationError):
471+
o.validate()
472+
473+
def test_redirect_weekdays_validation_error_2(self):
474+
o = OpenWrt({"firewall": {"redirects": [{"weekdays": ["mon", 1]}]}})
475+
with self.assertRaises(ValidationError):
476+
o.validate()
477+
478+
def test_redirect_monthdays_validation_error_1(self):
479+
o = OpenWrt({"firewall": {"redirects": [{"monthdays": [2, 8, 32]}]}})
480+
with self.assertRaises(ValidationError):
481+
o.validate()
482+
483+
def test_redirect_monthdays_validation_error_2(self):
484+
o = OpenWrt({"firewall": {"redirects": [{"monthdays": [0, 2, 8]}]}})
485+
with self.assertRaises(ValidationError):
486+
o.validate()

0 commit comments

Comments
 (0)