Skip to content

Commit 68681a1

Browse files
authored
[feature] Added properties to wireguard schema #280
Closes #280
1 parent 586e4bc commit 68681a1

File tree

3 files changed

+132
-4
lines changed

3 files changed

+132
-4
lines changed

netjsonconfig/backends/wireguard/converters.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ class Wireguard(BaseConverter):
66
netjson_key = 'wireguard'
77
intermediate_key = 'wireguard'
88
_schema = schema
9+
_forward_property_map = {
10+
'port': 'ListenPort',
11+
'private_key': 'PrivateKey',
12+
'address': 'Address',
13+
'dns': 'DNS',
14+
'mtu': 'MTU',
15+
'save_config': 'SaveConfig',
16+
'table': 'Table',
17+
'pre_up': 'PreUp',
18+
'post_up': 'PostUp',
19+
'pre_down': 'PreDown',
20+
'post_down': 'PostDown',
21+
}
922

1023
def to_intermediate_loop(self, block, result, index=None):
1124
vpn = self.__intermediate_vpn(block)
@@ -14,9 +27,27 @@ def to_intermediate_loop(self, block, result, index=None):
1427
return result
1528

1629
def __intermediate_vpn(self, config, remove=None):
17-
config['ListenPort'] = config.pop('port')
18-
config['PrivateKey'] = config.pop('private_key')
19-
config['Address'] = config.pop('address')
30+
# Required properties
31+
for option in ['port', 'private_key', 'address']:
32+
config[self._forward_property_map[option]] = config.pop(option)
33+
# Optional properties
34+
for option in self._forward_property_map.keys():
35+
if option in ['port', 'private_key', 'address']:
36+
# These options have been already handled
37+
continue
38+
if config.get(option, None) not in ['', None, []]:
39+
if option == 'dns':
40+
config[option] = ','.join(config[option])
41+
elif option == 'save_config':
42+
config[option] = 'true' if config[option] else 'false'
43+
config[self._forward_property_map[option]] = config.pop(option)
44+
else:
45+
config.pop(option, None)
46+
# Remove default options
47+
if config.get('Table') == 'auto':
48+
config.pop('Table')
49+
if config.get('MTU') == 1280:
50+
config.pop('MTU')
2051
config['peers'] = self.__intermediate_peers(config.get('peers', []))
2152
return self.sorted_dict(config)
2253

netjsonconfig/backends/wireguard/schema.py

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,91 @@
4646
"pattern": "^[^\\s]*$",
4747
"propertyOrder": 3,
4848
},
49+
"dns": {
50+
"title": "DNS",
51+
"type": "array",
52+
"uniqueItems": True,
53+
"propertyOrder": 4,
54+
"items": {"title": "DNS", "type": "string"},
55+
"propertyOrder": 4,
56+
},
57+
"mtu": {
58+
"type": "integer",
59+
"title": "MTU",
60+
"minimum": 68,
61+
"propertyOrder": 5,
62+
"default": 1280,
63+
},
64+
"table": {
65+
"title": "Table",
66+
"type": "string",
67+
"default": "auto",
68+
"description": (
69+
"Controls the routing table to which routes are added."
70+
"There are two special values:"
71+
" 'off' (disables the creation of routes altogether)"
72+
" and 'auto' (adds routes to the default table and enables"
73+
" special handling of default routes)."
74+
),
75+
"propertyOrder": 6,
76+
},
77+
"pre_up": {
78+
"title": "PreUp",
79+
"type": "string",
80+
"description": (
81+
"Script snippet which will be executed before setting up the interface."
82+
" The special string '%i' is expanded to INTERFACE."
83+
),
84+
"format": "textarea",
85+
"propertyOrder": 7,
86+
},
87+
"post_up": {
88+
"title": "PostUp",
89+
"type": "string",
90+
"description": (
91+
"Script snippet which will be executed after setting up the interface."
92+
" The special string '%i' is expanded to INTERFACE."
93+
),
94+
"format": "textarea",
95+
"propertyOrder": 8,
96+
},
97+
"pre_down": {
98+
"title": "PreDown",
99+
"type": "string",
100+
"description": (
101+
"Script snippet which will be executed before tearing down the interface."
102+
" The special string '%i' is expanded to INTERFACE."
103+
),
104+
"format": "textarea",
105+
"propertyOrder": 9,
106+
},
107+
"post_down": {
108+
"title": "PostDown",
109+
"type": "string",
110+
"description": (
111+
"Script snippet which will be executed after tearing down the interface."
112+
" The special string '%i' is expanded to INTERFACE."
113+
),
114+
"format": "textarea",
115+
"propertyOrder": 10,
116+
},
117+
"save_config": {
118+
"type": "boolean",
119+
"title": "save config",
120+
"default": False,
121+
"format": "checkbox",
122+
"description": (
123+
"If set to `true', the configuration is saved from the current"
124+
" state of the interface upon shutdown. "
125+
),
126+
"propertyOrder": 11,
127+
},
49128
"peers": {
50129
"type": "array",
51130
"title": "Peers",
52131
"uniqueItems": True,
53132
"additionalItems": True,
54-
"propertyOrder": 11,
133+
"propertyOrder": 12,
55134
"items": {
56135
"type": "object",
57136
"title": "Peer",

tests/wireguard/test_backend.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,23 @@ def test_confs(self):
2828
"private_key": "QFdbnuYr7rrF4eONCAs7FhZwP7BXX/jD/jq2LXCpaXI=",
2929
"port": 40842,
3030
"address": "10.0.0.1/24",
31+
"dns": ["10.0.0.1"],
32+
"table": "auto",
33+
"mtu": 1500,
34+
"save_config": True,
35+
"pre_up": "",
36+
"post_up": "ip rule add ipproto tcp dport 22 table 1234",
37+
"pre_down": "ip rule delete ipproto tcp dport 22 table 1234",
38+
"post_down": "",
3139
},
3240
{
3341
"name": "test2",
3442
"private_key": "AFdbnuYr7rrF4eONCAs7FhZwP7BXX/jD/jq2LXCpaXI=",
3543
"port": 40843,
44+
"mtu": 1280,
3645
"address": "10.0.1.1/24",
46+
"dns": ["10.0.1.1", "10.0.0.1"],
47+
"table": "1234",
3748
},
3849
]
3950
}
@@ -42,15 +53,22 @@ def test_confs(self):
4253
4354
[Interface]
4455
Address = 10.0.0.1/24
56+
DNS = 10.0.0.1
4557
ListenPort = 40842
58+
MTU = 1500
59+
PostUp = ip rule add ipproto tcp dport 22 table 1234
60+
PreDown = ip rule delete ipproto tcp dport 22 table 1234
4661
PrivateKey = QFdbnuYr7rrF4eONCAs7FhZwP7BXX/jD/jq2LXCpaXI=
62+
SaveConfig = true
4763
4864
# wireguard config: test2
4965
5066
[Interface]
5167
Address = 10.0.1.1/24
68+
DNS = 10.0.1.1,10.0.0.1
5269
ListenPort = 40843
5370
PrivateKey = AFdbnuYr7rrF4eONCAs7FhZwP7BXX/jD/jq2LXCpaXI=
71+
Table = 1234
5472
"""
5573
self.assertEqual(c.render(), expected)
5674

0 commit comments

Comments
 (0)