Skip to content

Commit

Permalink
[fix] Fixed Wireguard backend auto_client method
Browse files Browse the repository at this point in the history
- Fixed issues in configuration that prevented Wireguard from establishing tunnels
  • Loading branch information
pandafy committed Apr 30, 2021
1 parent 490ba55 commit b479003
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 18 deletions.
7 changes: 3 additions & 4 deletions netjsonconfig/backends/openwrt/openwrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def wireguard_auto_client(cls, **kwargs):
config = {
'interfaces': [
{
'name': data['client']['name'],
'name': data['interface_name'],
'type': 'wireguard',
'private_key': data['client']['private_key'],
'port': data['client']['port'],
Expand All @@ -70,21 +70,20 @@ def wireguard_auto_client(cls, **kwargs):
'fwmark': '',
'ip6prefix': [],
'addresses': [],
'disabled': True,
'network': '',
}
],
'wireguard_peers': [
{
'interface': data['server']['name'],
'interface': data['interface_name'],
'public_key': data['server']['public_key'],
'allowed_ips': data['server']['allowed_ips'],
'endpoint_host': data['server']['endpoint_host'],
'endpoint_port': data['server']['endpoint_port'],
# Default values for Wireguard Peers
'preshared_key': '',
'persistent_keepalive': 0,
'route_allowed_ips': False,
'route_allowed_ips': True,
}
],
}
Expand Down
4 changes: 1 addition & 3 deletions netjsonconfig/backends/wireguard/wireguard.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@ def auto_client(cls, host='', pub_key='', server={}, port=51820, **kwargs):
:param pub_key: publick key of the Wireguard server
:returns: dictionary representing a Wireguard server and client properties
"""
server_name = server.get('name', '')
return {
'interface_name': server.get('name', ''),
'client': {
'port': port,
'private_key': '{{private_key}}',
'name': f'{server_name}_client',
'ip_address': kwargs.get('ip_address'),
},
'server': {
'name': server_name,
'public_key': pub_key,
'endpoint_host': host,
'endpoint_port': server.get('port', 51820),
Expand Down
13 changes: 6 additions & 7 deletions tests/openwrt/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,11 +427,10 @@ def _get_wireguard_empty_configuration(self):
'interfaces': [
{
'addresses': [],
'disabled': True,
'fwmark': '',
'ip6prefix': [],
'mtu': 1420,
'name': '_client',
'name': '',
'network': '',
'nohostroute': False,
'port': 51820,
Expand All @@ -448,7 +447,7 @@ def _get_wireguard_empty_configuration(self):
'persistent_keepalive': 0,
'preshared_key': '',
'public_key': '',
'route_allowed_ips': False,
'route_allowed_ips': True,
}
],
}
Expand All @@ -464,7 +463,7 @@ def _get_vxlan_wireguard_empty_configuration(self):
'port': 4789,
'rxcsum': True,
'ttl': 64,
'tunlink': '_client',
'tunlink': '',
'txcsum': True,
'type': 'vxlan',
'vni': 0,
Expand All @@ -481,7 +480,7 @@ def test_wireguard_auto_client(self):
expected = self._get_wireguard_empty_configuration()
expected['interfaces'][0].update(
{
'name': 'wg_client',
'name': 'wg',
'private_key': '{{private_key}}',
'addresses': [
{
Expand Down Expand Up @@ -519,7 +518,7 @@ def test_vxlan_wireguard_auto_client(self):
with self.subTest('Required arguments provided'):
expected = self._get_vxlan_wireguard_empty_configuration()
expected['interfaces'][0].update(
{'name': 'wg_client', 'private_key': '{{private_key}}'}
{'name': 'wg', 'private_key': '{{private_key}}'}
)
expected['wireguard_peers'][0].update(
{
Expand All @@ -530,7 +529,7 @@ def test_vxlan_wireguard_auto_client(self):
}
)
expected['interfaces'][1].update(
{'tunlink': 'wg_client', 'vni': 1, 'vtep': '10.0.0.1'}
{'tunlink': 'wg', 'vni': 1, 'vtep': '10.0.0.1'}
)
self.assertDictEqual(
OpenWrt.vxlan_wireguard_auto_client(
Expand Down
6 changes: 2 additions & 4 deletions tests/wireguard/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,13 @@ def test_generate(self):
def test_auto_client(self):
with self.subTest('No arguments are provided'):
expected = {
'interface_name': '',
'client': {
'port': 51820,
'private_key': '{{private_key}}',
'name': '_client',
'ip_address': None,
},
'server': {
'name': '',
'public_key': '',
'endpoint_host': '',
'endpoint_port': 51820,
Expand All @@ -154,14 +153,13 @@ def test_auto_client(self):
self.assertDictEqual(Wireguard.auto_client(), expected)
with self.subTest('Required arguments are provided'):
expected = {
'interface_name': 'wg',
'client': {
'port': 51820,
'private_key': '{{private_key}}',
'name': 'wg_client',
'ip_address': '10.0.0.2',
},
'server': {
'name': 'wg',
'public_key': 'server_public_key',
'endpoint_host': '0.0.0.0',
'endpoint_port': 51820,
Expand Down

0 comments on commit b479003

Please sign in to comment.