Skip to content

Commit f727193

Browse files
Make firewall rule proto parameter a list
1 parent 29957e3 commit f727193

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

netjsonconfig/backends/openwrt/converters/firewall.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
"""Firewall configuration management for OpenWRT.
2+
3+
See the following resource for a detailed description of the sections and parameters of
4+
the UCI configuration for the OpenWRT firewall.
5+
6+
https://openwrt.org/docs/guide-user/firewall/firewall_configuration
7+
"""
18
from collections import OrderedDict
29

310
from ..schema import schema
@@ -76,6 +83,15 @@ def __intermediate_rules(self, rules):
7683
resultdict = OrderedDict(
7784
((".name", self.__get_auto_name_rule(rule)), (".type", "rule"))
7885
)
86+
if "proto" in rule:
87+
# If proto is a single value, then force it not to be in a list so that
88+
# the UCI uses "option" rather than "list". If proto is only "tcp"
89+
# and"udp", we can force it to the single special value of "tcpudp".
90+
proto = rule["proto"]
91+
if len(proto) == 1:
92+
rule["proto"] = proto[0]
93+
elif set(proto) == {"tcp", "udp"}:
94+
rule["proto"] = "tcpudp"
7995
resultdict.update(rule)
8096
result.append(resultdict)
8197
return result
@@ -97,7 +113,15 @@ def to_netjson_loop(self, block, result, index):
97113
return self.type_cast(result)
98114

99115
def __netjson_rule(self, rule):
116+
print(rule)
100117
if "enabled" in rule:
101118
rule["enabled"] = rule.pop("enabled") == "1"
119+
if "proto" in rule:
120+
proto = rule.pop("proto")
121+
if not isinstance(proto, list):
122+
if proto == "tcpudp":
123+
rule["proto"] = ["tcp", "udp"]
124+
else:
125+
rule["proto"] = [proto]
102126

103127
return self.type_cast(rule)

netjsonconfig/backends/openwrt/schema.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,16 +615,17 @@
615615
"propertyOrder": 5,
616616
},
617617
"proto": {
618-
"type": "string",
618+
"type": "array",
619619
"title": "proto",
620620
"description": "match incoming traffic using the given protocol. "
621621
"Can be one of tcp, udp, tcpudp, udplite, icmp, esp, "
622622
"ah, sctp, or all or it can be a numeric value, "
623623
"representing one of these protocols or a different one. "
624624
"A protocol name from /etc/protocols is also allowed. "
625625
"The number 0 is equivalent to all",
626-
"default": "tcpudp",
626+
"default": ["tcp", "udp"],
627627
"propertyOrder": 6,
628+
"items": {"title": "Protocol type", "type": "string"},
628629
},
629630
"icmp_type": {
630631
"title": "icmp_type",

tests/openwrt/test_default.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def test_render_default(self):
2626
{
2727
"name": "Allow-MLD",
2828
"src": "wan",
29-
"proto": "icmp",
29+
"proto": ["icmp"],
3030
"src_ip": "fe80::/10",
3131
"family": "ipv6",
3232
"target": "ACCEPT",
@@ -35,7 +35,7 @@ def test_render_default(self):
3535
{
3636
"name": "Rule2",
3737
"src": "wan",
38-
"proto": "icmp",
38+
"proto": ["icmp"],
3939
"src_ip": "192.168.1.1/24",
4040
"family": "ipv4",
4141
"target": "ACCEPT",
@@ -152,7 +152,7 @@ def test_parse_default(self):
152152
"family": "ipv6",
153153
"icmp_type": ["130/0", "131/0", "132/0", "143/0"],
154154
"name": "Allow-MLD",
155-
"proto": "icmp",
155+
"proto": ["icmp"],
156156
"src": "wan",
157157
"src_ip": "fe80::/10",
158158
"target": "ACCEPT",

tests/openwrt/test_firewall.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class TestFirewall(unittest.TestCase, _TabsMixin):
1515
"name": "Allow-MLD",
1616
"src": "wan",
1717
"src_ip": "fe80::/10",
18-
"proto": "icmp",
18+
"proto": ["icmp"],
1919
"icmp_type": ["130/0", "131/0", "132/0", "143/0"],
2020
"target": "ACCEPT",
2121
"family": "ipv6",
@@ -62,7 +62,7 @@ def test_parse_rule_1(self):
6262
"src_ip": "fc00::/6",
6363
"dest_ip": "fc00::/6",
6464
"dest_port": "546",
65-
"proto": "udp",
65+
"proto": ["udp"],
6666
"target": "ACCEPT",
6767
"family": "ipv6",
6868
}
@@ -103,7 +103,7 @@ def test_parse_rule_2(self):
103103
{
104104
"name": "Allow-Ping",
105105
"src": "wan",
106-
"proto": "icmp",
106+
"proto": ["icmp"],
107107
"family": "ipv4",
108108
"icmp_type": ["echo-request"],
109109
"target": "ACCEPT",

0 commit comments

Comments
 (0)