Skip to content

Commit 006b196

Browse files
[openwrt] Make name parameter required for firewall objects
1 parent 47e5530 commit 006b196

2 files changed

Lines changed: 82 additions & 43 deletions

File tree

netjsonconfig/backends/openwrt/converters/firewall.py

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,14 @@ def __intermediate_forwardings(self, forwardings):
4545
for forwarding in forwardings:
4646
resultdict = OrderedDict(
4747
(
48-
(".name", self.__get_auto_name_forwarding(forwarding)),
48+
(".name", self._get_uci_name(forwarding["name"])),
4949
(".type", "forwarding"),
5050
)
5151
)
5252
resultdict.update(forwarding)
5353
result.append(resultdict)
5454
return result
5555

56-
def __get_auto_name_forwarding(self, forwarding):
57-
if "family" in forwarding.keys():
58-
uci_name = self._get_uci_name(
59-
"_".join([forwarding["src"], forwarding["dest"], forwarding["family"]])
60-
)
61-
else:
62-
uci_name = self._get_uci_name(
63-
"_".join([forwarding["src"], forwarding["dest"]])
64-
)
65-
return "forwarding_{0}".format(uci_name)
66-
6756
def __intermediate_zones(self, zones):
6857
"""
6958
converts NetJSON zone to
@@ -72,7 +61,7 @@ def __intermediate_zones(self, zones):
7261
result = []
7362
for zone in zones:
7463
resultdict = OrderedDict(
75-
((".name", self.__get_auto_name_zone(zone)), (".type", "zone"))
64+
((".name", self._get_uci_name(zone["name"])), (".type", "zone"))
7665
)
7766
# If network contains only a single value, force the use of a UCI "option"
7867
# rather than "list"".
@@ -83,9 +72,6 @@ def __intermediate_zones(self, zones):
8372
result.append(resultdict)
8473
return result
8574

86-
def __get_auto_name_zone(self, zone):
87-
return "zone_{0}".format(self._get_uci_name(zone["name"]))
88-
8975
def __intermediate_rules(self, rules):
9076
"""
9177
converts NetJSON rule to
@@ -96,7 +82,7 @@ def __intermediate_rules(self, rules):
9682
if "config_name" in rule:
9783
del rule["config_name"]
9884
resultdict = OrderedDict(
99-
((".name", self.__get_auto_name_rule(rule)), (".type", "rule"))
85+
((".name", self._get_uci_name(rule["name"])), (".type", "rule"))
10086
)
10187
if "proto" in rule:
10288
# If proto is a single value, then force it not to be in a list so that
@@ -111,9 +97,6 @@ def __intermediate_rules(self, rules):
11197
result.append(resultdict)
11298
return result
11399

114-
def __get_auto_name_rule(self, rule):
115-
return "rule_{0}".format(self._get_uci_name(rule["name"]))
116-
117100
def __intermediate_redirects(self, redirects):
118101
"""
119102
converts NetJSON redirect to
@@ -125,7 +108,7 @@ def __intermediate_redirects(self, redirects):
125108
del redirect["config_name"]
126109
resultdict = OrderedDict(
127110
(
128-
(".name", self.__get_auto_name_redirect(redirect)),
111+
(".name", self._get_uci_name(redirect["name"])),
129112
(".type", "redirect"),
130113
)
131114
)
@@ -144,9 +127,6 @@ def __intermediate_redirects(self, redirects):
144127

145128
return result
146129

147-
def __get_auto_name_redirect(self, redirect):
148-
return "redirect_{0}".format(self._get_uci_name(redirect["name"]))
149-
150130
def to_netjson_loop(self, block, result, index):
151131
result.setdefault("firewall", {})
152132

@@ -233,6 +213,10 @@ def __netjson_zone(self, zone):
233213
return self.type_cast(zone)
234214

235215
def __netjson_forwarding(self, forwarding):
216+
if "enabled" in forwarding:
217+
forwarding["enabled"] = self.__netjson_generic_boolean(
218+
forwarding["enabled"]
219+
)
236220
return self.type_cast(forwarding)
237221

238222
def __netjson_redirect(self, redirect):

tests/openwrt/test_firewall.py

Lines changed: 74 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def test_parse_defaults_2(self):
115115
116116
config defaults 'defaults'
117117
118-
config rule 'rule_Allow_MLD'
118+
config rule 'Allow_MLD'
119119
option name 'Allow-MLD'
120120
option src 'wan'
121121
option src_ip 'fe80::/10'
@@ -161,7 +161,7 @@ def test_parse_rule_1(self):
161161
162162
config defaults 'defaults'
163163
164-
config rule 'rule_Allow_DHCPv6'
164+
config rule 'Allow_DHCPv6'
165165
option name 'Allow-DHCPv6'
166166
option src 'wan'
167167
option src_ip 'fc00::/6'
@@ -204,7 +204,7 @@ def test_parse_rule_2(self):
204204
205205
config defaults 'defaults'
206206
207-
config rule 'rule_Allow_Ping'
207+
config rule 'Allow_Ping'
208208
option name 'Allow-Ping'
209209
option src 'wan'
210210
option proto 'icmp'
@@ -244,7 +244,7 @@ def test_parse_rule_3(self):
244244
245245
config defaults 'defaults'
246246
247-
config rule 'rule_Allow_Isolated_DHCP'
247+
config rule 'Allow_Isolated_DHCP'
248248
option name 'Allow-Isolated-DHCP'
249249
option src 'isolated'
250250
option proto 'udp'
@@ -284,7 +284,7 @@ def test_parse_rule_4(self):
284284
285285
config defaults 'defaults'
286286
287-
config rule 'rule_Allow_Isolated_DHCP'
287+
config rule 'Allow_Isolated_DHCP'
288288
option name 'Allow-Isolated-DHCP'
289289
option src_ip '10.10.10.10'
290290
option src_mac 'fc:aa:14:18:12:98'
@@ -341,7 +341,7 @@ def test_parse_rule_5(self):
341341
342342
config defaults 'defaults'
343343
344-
config rule 'rule_Allow_Isolated_DHCP'
344+
config rule 'Allow_Isolated_DHCP'
345345
option name 'Allow-Isolated-DHCP'
346346
option src_ip '10.10.10.10'
347347
option src_mac 'fc:aa:14:18:12:98'
@@ -399,7 +399,7 @@ def test_parse_rule_6(self):
399399
400400
config defaults 'defaults'
401401
402-
config zone 'zone_lan'
402+
config zone 'lan'
403403
option name 'lan'
404404
option input 'ACCEPT'
405405
option output 'ACCEPT'
@@ -440,7 +440,7 @@ def test_parse_zone_1(self):
440440
441441
config defaults 'defaults'
442442
443-
config zone 'zone_wan'
443+
config zone 'wan'
444444
option name 'wan'
445445
option input 'DROP'
446446
option output 'ACCEPT'
@@ -460,7 +460,7 @@ def test_parse_zone_1(self):
460460
461461
config defaults 'defaults'
462462
463-
config zone 'zone_wan'
463+
config zone 'wan'
464464
option name 'wan'
465465
option input 'DROP'
466466
option output 'ACCEPT'
@@ -485,7 +485,9 @@ def test_parse_zone_3(self):
485485
self.assertEqual(o.config, self._zone_2_netjson)
486486

487487
_forwarding_1_netjson = {
488-
"firewall": {"forwardings": [{"src": "isolated", "dest": "wan"}]}
488+
"firewall": {
489+
"forwardings": [{"name": "isolated-wan", "src": "isolated", "dest": "wan"}]
490+
}
489491
}
490492

491493
_forwarding_1_uci = textwrap.dedent(
@@ -494,7 +496,8 @@ def test_parse_zone_3(self):
494496
495497
config defaults 'defaults'
496498
497-
config forwarding 'forwarding_isolated_wan'
499+
config forwarding 'isolated_wan'
500+
option name 'isolated-wan'
498501
option src 'isolated'
499502
option dest 'wan'
500503
"""
@@ -511,7 +514,14 @@ def test_parse_forwarding_1(self):
511514

512515
_forwarding_2_netjson = {
513516
"firewall": {
514-
"forwardings": [{"src": "isolated", "dest": "wan", "family": "ipv4"}]
517+
"forwardings": [
518+
{
519+
"name": "isolated-wan-ipv4",
520+
"src": "isolated",
521+
"dest": "wan",
522+
"family": "ipv4",
523+
}
524+
]
515525
}
516526
}
517527

@@ -521,7 +531,8 @@ def test_parse_forwarding_1(self):
521531
522532
config defaults 'defaults'
523533
524-
config forwarding 'forwarding_isolated_wan_ipv4'
534+
config forwarding 'isolated_wan_ipv4'
535+
option name 'isolated-wan-ipv4'
525536
option src 'isolated'
526537
option dest 'wan'
527538
option family 'ipv4'
@@ -538,7 +549,11 @@ def test_parse_forwarding_2(self):
538549
self.assertEqual(o.config, self._forwarding_2_netjson)
539550

540551
_forwarding_3_netjson = {
541-
"firewall": {"forwardings": [{"src": "lan", "dest": "wan", "family": "any"}]}
552+
"firewall": {
553+
"forwardings": [
554+
{"name": "lan-wan-any", "src": "lan", "dest": "wan", "family": "any"}
555+
]
556+
}
542557
}
543558

544559
_forwarding_3_uci = textwrap.dedent(
@@ -547,7 +562,8 @@ def test_parse_forwarding_2(self):
547562
548563
config defaults 'defaults'
549564
550-
config forwarding 'forwarding_lan_wan_any'
565+
config forwarding 'lan_wan_any'
566+
option name 'lan-wan-any'
551567
option src 'lan'
552568
option dest 'wan'
553569
option family 'any'
@@ -563,6 +579,45 @@ def test_parse_forwarding_3(self):
563579
o = OpenWrt(native=self._forwarding_3_uci)
564580
self.assertEqual(o.config, self._forwarding_3_netjson)
565581

582+
_forwarding_4_netjson = {
583+
"firewall": {
584+
"forwardings": [
585+
{
586+
"name": "forward_name",
587+
"src": "lan",
588+
"dest": "wan",
589+
"family": "any",
590+
"enabled": False,
591+
}
592+
]
593+
}
594+
}
595+
596+
_forwarding_4_uci = textwrap.dedent(
597+
"""\
598+
package firewall
599+
600+
config defaults 'defaults'
601+
602+
config forwarding 'forward_name'
603+
option name 'forward_name'
604+
option src 'lan'
605+
option dest 'wan'
606+
option family 'any'
607+
option enabled '0'
608+
"""
609+
)
610+
611+
def test_render_forwarding_4(self):
612+
o = OpenWrt(self._forwarding_4_netjson)
613+
expected = self._tabs(self._forwarding_4_uci)
614+
self.assertEqual(o.render(), expected)
615+
616+
def test_parse_forwarding_4(self):
617+
o = OpenWrt(native=self._forwarding_4_uci)
618+
print(o.config)
619+
self.assertEqual(o.config, self._forwarding_4_netjson)
620+
566621
def test_forwarding_validation_error(self):
567622
o = OpenWrt(
568623
{
@@ -595,7 +650,7 @@ def test_forwarding_validation_error(self):
595650
596651
config defaults 'defaults'
597652
598-
config redirect 'redirect_Adblock DNS, port 53'
653+
config redirect 'Adblock DNS, port 53'
599654
option name 'Adblock DNS, port 53'
600655
option src 'lan'
601656
option proto 'tcpudp'
@@ -638,7 +693,7 @@ def test_parse_redirect_1(self):
638693
639694
config defaults 'defaults'
640695
641-
config redirect 'redirect_Adblock DNS, port 53'
696+
config redirect 'Adblock DNS, port 53'
642697
option name 'Adblock DNS, port 53'
643698
option src 'lan'
644699
option proto 'tcpudp'
@@ -691,7 +746,7 @@ def test_redirect_monthdays_validation_error_2(self):
691746
692747
config defaults 'defaults'
693748
694-
config redirect 'redirect_Adblock DNS, port 53'
749+
config redirect 'Adblock DNS, port 53'
695750
option name 'Adblock DNS, port 53'
696751
option src 'lan'
697752
option proto 'tcpudp'
@@ -757,7 +812,7 @@ def test_parse_redirect_3(self):
757812
758813
config defaults 'defaults'
759814
760-
config redirect 'redirect_Adblock DNS, port 53'
815+
config redirect 'Adblock DNS, port 53'
761816
option name 'Adblock DNS, port 53'
762817
option src 'lan'
763818
option proto 'tcpudp'

0 commit comments

Comments
 (0)