Skip to content

Commit

Permalink
[fix] Openwrt backend validation problem for property ip_rules/src #96
Browse files Browse the repository at this point in the history
Fixed issue by adding a pattern to match the valid src and dest
notation.

Fixes #96
  • Loading branch information
NoumbissiValere committed Jul 11, 2020
1 parent 8804d76 commit 7d6aa0a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
18 changes: 18 additions & 0 deletions netjsonconfig/backends/openwrt/schema.py
Expand Up @@ -8,6 +8,22 @@

default_radio_driver = "mac80211"

# pattern to match ipv4 and ipv6 in CIDR notation
src_pattern = (
"^(s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|"
"((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|"
"(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]d|1dd|[1-9]?d)"
"(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|"
"((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|"
"(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:"
"((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}"
"(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)"
"(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|"
"((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|"
"(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)"
"(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:)))(%.+)?s*(\\/(12[0-8]|1[0-1][0-9]|[1-9][0-9]|[0-9])))|"
"(((^|\\.)((25[0-5])|(2[0-4]\\d)|(1\\d\\d)|([1-9]?\\d))){4}\\/(?:\\d|[12]\\d|3[01]))$"
)

schema = merge_config(
default_schema,
Expand Down Expand Up @@ -229,12 +245,14 @@
"title": "source subnet",
"description": "(CIDR notation)",
"propertyOrder": 3,
"pattern": src_pattern,
},
"dest": {
"type": "string",
"title": "destination subnet",
"description": "(CIDR notation)",
"propertyOrder": 4,
"pattern": src_pattern,
},
"tos": {
"type": "integer",
Expand Down
38 changes: 12 additions & 26 deletions tests/openwrt/test_network.py
@@ -1,6 +1,7 @@
import unittest

from netjsonconfig import OpenWrt
from netjsonconfig.exceptions import ValidationError
from netjsonconfig.utils import _TabsMixin


Expand Down Expand Up @@ -217,37 +218,28 @@ def test_render_rule_wrong(self):
"in": "eth0",
"out": "eth1",
"src": "wrong",
"dest": "wrong",
"dest": "wrong1",
"tos": 2,
"action": "blackhole"
"action": "blackhole",
}
]
}
o = OpenWrt(rule)
try:
with self.assertRaisesRegexp(ValidationError, "'wrong' does not match"):
o.validate()
except ValidationError as e:
# check error message
pass
else:
self.fail('ValidationError not raised')
# fix 'src' and expect wrong 'dest' to fail
rule['src'] = '192.168.1.1/24'
rule['ip_rules'][0]['src'] = '192.168.1.1/24'
o = OpenWrt(rule)
try:
with self.assertRaisesRegexp(ValidationError, "'wrong1' does not match"):
o.validate()
except ValidationError as e:
# check error message
pass
else:
self.fail('ValidationError not raised')
# fix 'dest' and expect no ValidationError raised
rule['src'] = '192.168.1.1/24'
rule['ip_rules'][0]['dest'] = '192.168.1.1/24'
o = OpenWrt(rule)
o.validate()

def test_parse_rules_zone(self):
o = OpenWrt(native="""package network
with self.assertRaisesRegexp(ValidationError, "'wrong' does not match"):
OpenWrt(
native="""package network
config rule 'rule1'
option action 'blackhole'
Expand All @@ -256,14 +248,8 @@ def test_parse_rules_zone(self):
option out 'eth1'
option src 'wrong'
option tos '2'
""")
try:
o.validate()
except ValidationError as e:
# check error message
pass
else:
self.fail('ValidationError not raised')
"""
)

_switch_netjson = {
"switch": [
Expand Down

0 comments on commit 7d6aa0a

Please sign in to comment.