Skip to content

Commit cadb36b

Browse files
[fix] Openwrt backend validation problem for property ip_rules/src #96
Fixed issue by defining a new format checker for CIDR notation. Fixes #96
1 parent 8804d76 commit cadb36b

File tree

4 files changed

+24
-26
lines changed

4 files changed

+24
-26
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ target/
5454

5555
# editors
5656
*.komodoproject
57+
.vscode
5758

5859
# other
5960
*.DS_Store*

netjsonconfig/backends/base/backend.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import gzip
2+
import ipaddress
23
import json
34
import tarfile
45
from collections import OrderedDict
@@ -122,6 +123,14 @@ def _deduplicate_files(self):
122123
files_dict[file['path']] = file
123124
self.config['files'] = list(files_dict.values())
124125

126+
@draft4_format_checker.checks('cidr', AssertionError)
127+
def _cidr_notation(value):
128+
try:
129+
ipaddress.ip_network(value)
130+
except ValueError as e:
131+
assert False, str(e)
132+
return True
133+
125134
def validate(self):
126135
try:
127136
Draft4Validator(self.schema, format_checker=draft4_format_checker).validate(

netjsonconfig/backends/openwrt/schema.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,14 @@
229229
"title": "source subnet",
230230
"description": "(CIDR notation)",
231231
"propertyOrder": 3,
232+
"format": "cidr",
232233
},
233234
"dest": {
234235
"type": "string",
235236
"title": "destination subnet",
236237
"description": "(CIDR notation)",
237238
"propertyOrder": 4,
239+
"format": "cidr",
238240
},
239241
"tos": {
240242
"type": "integer",

tests/openwrt/test_network.py

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import unittest
22

33
from netjsonconfig import OpenWrt
4+
from netjsonconfig.exceptions import ValidationError
45
from netjsonconfig.utils import _TabsMixin
56

67

@@ -217,37 +218,28 @@ def test_render_rule_wrong(self):
217218
"in": "eth0",
218219
"out": "eth1",
219220
"src": "wrong",
220-
"dest": "wrong",
221+
"dest": "wrong1",
221222
"tos": 2,
222-
"action": "blackhole"
223+
"action": "blackhole",
223224
}
224225
]
225226
}
226227
o = OpenWrt(rule)
227-
try:
228+
with self.assertRaisesRegex(ValidationError, "'wrong' is not a 'cidr'"):
228229
o.validate()
229-
except ValidationError as e:
230-
# check error message
231-
pass
232-
else:
233-
self.fail('ValidationError not raised')
234-
# fix 'src' and expect wrong 'dest' to fail
235-
rule['src'] = '192.168.1.1/24'
230+
rule['ip_rules'][0]['src'] = '192.168.1.0/24'
236231
o = OpenWrt(rule)
237-
try:
232+
with self.assertRaisesRegexp(ValidationError, "'wrong1' is not a 'cidr'"):
238233
o.validate()
239-
except ValidationError as e:
240-
# check error message
241-
pass
242-
else:
243-
self.fail('ValidationError not raised')
244234
# fix 'dest' and expect no ValidationError raised
245-
rule['src'] = '192.168.1.1/24'
235+
rule['ip_rules'][0]['dest'] = '192.168.1.0/24'
246236
o = OpenWrt(rule)
247237
o.validate()
248238

249239
def test_parse_rules_zone(self):
250-
o = OpenWrt(native="""package network
240+
with self.assertRaisesRegexp(ValidationError, "'wrong' is not a 'cidr'"):
241+
OpenWrt(
242+
native="""package network
251243
252244
config rule 'rule1'
253245
option action 'blackhole'
@@ -256,14 +248,8 @@ def test_parse_rules_zone(self):
256248
option out 'eth1'
257249
option src 'wrong'
258250
option tos '2'
259-
""")
260-
try:
261-
o.validate()
262-
except ValidationError as e:
263-
# check error message
264-
pass
265-
else:
266-
self.fail('ValidationError not raised')
251+
"""
252+
)
267253

268254
_switch_netjson = {
269255
"switch": [

0 commit comments

Comments
 (0)