Skip to content

Commit

Permalink
Merge pull request #17 from fictivekin/9-service-interface
Browse files Browse the repository at this point in the history
Peer Service Interface and Config Customizability
  • Loading branch information
jnhmcknight authored Nov 6, 2023
2 parents 6607ec4 + 2fd5d6a commit c6530b0
Show file tree
Hide file tree
Showing 13 changed files with 217 additions and 116 deletions.
8 changes: 4 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ Setup a WireGuard server::
server = Server('myvpnserver.com', '192.168.24.0/24', address='192.168.24.1')

# Write out the server config to the default location: /etc/wireguard/wg0.conf
server.config().write()
server.config.write()


Create a client within the previously created server::

peer = server.peer('my-client')

# Output this peer's config for copying to the peer device
print(peer.config().local_config)
print(peer.config.local_config)

# Rewrite the server config file including the newly created peer
server.config().write()
server.config.write()


Create a standalone client::
Expand All @@ -39,7 +39,7 @@ Create a standalone client::
peer = Peer('my-client', '192.168.24.0/24', address='192.168.24.45')

# Write out the peer config to the default location: /etc/wireguard/wg0.conf
peer.config().write()
peer.config.write()


**Note**: Both the server and peer config files are named the same by default. This is because
Expand Down
10 changes: 4 additions & 6 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
patch,
)

from subnet import ip_network, IPv4Network, IPv4Address

from wireguard import (
Config,
ServerConfig,
Expand Down Expand Up @@ -112,7 +110,7 @@ def test_write_server_config_no_params():
)

with patch('builtins.open', mock_open()) as mo:
server.config().write()
server.config.write()

mo.assert_has_calls([
call('/etc/wireguard/wg0.conf', mode='w', encoding='utf-8'),
Expand All @@ -139,7 +137,7 @@ def test_write_server_config(interface, path, full_path, peers_full_path):
interface=interface
)

config = server.config()
config = server.config
assert config.full_path(path) == full_path
assert config.peers_full_path(path) == peers_full_path

Expand All @@ -162,7 +160,7 @@ def test_write_peer_config_no_params():
)

with patch('builtins.open', mock_open()) as mo:
peer.config().write()
peer.config.write()

mo.assert_has_calls([
call('/etc/wireguard/wg0.conf', mode='w', encoding='utf-8'),
Expand Down Expand Up @@ -191,7 +189,7 @@ def test_write_peer_config(interface, path, full_path):
assert config.full_path(path) == full_path

with patch('builtins.open', mock_open()) as mo:
peer.config().write(path)
peer.config.write(path)

mo.assert_has_calls([
call(full_path, mode='w', encoding='utf-8'),
Expand Down
12 changes: 1 addition & 11 deletions tests/test_config_attributes.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@

import pytest
from unittest.mock import (
call,
mock_open,
patch,
)

from subnet import ip_network, IPv4Network, IPv4Address

from wireguard import (
Config,
ServerConfig,
Peer,
Server,
)
from wireguard.utils import IPAddressSet


def test_description():
Expand Down Expand Up @@ -285,7 +275,7 @@ def test_comments():
comments=comments,
)

config = peer.config()
config = peer.config

for comment in comments:
assert f'# {comment}' in config.local_config
Expand Down
89 changes: 89 additions & 0 deletions tests/test_customizations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@

import pytest

from subnet import (
ip_address,
IPv4Address,
)

from wireguard import (
Config,
Interface,
Peer,
)


class MyCustomInterface(Interface):
pass


class MyCustomConfig(Config):
pass


def test_peer_custom_config_cls():
address = '192.168.0.2'
dns = '1.1.1.1'

peer = Peer(
'test-peer',
address=address,
dns=ip_address(dns),
config_cls=MyCustomConfig,
)

assert isinstance(peer.config, MyCustomConfig)


@pytest.mark.parametrize(
('cls',),
[
(MyCustomInterface,),
(IPv4Address,),
],
)
def test_peer_invalid_custom_config_cls(cls):
address = '192.168.0.2'
dns = '1.1.1.1'

with pytest.raises(ValueError):
peer = Peer(
'test-peer',
address=address,
dns=ip_address(dns),
config_cls=cls,
)


def test_peer_custom_service_cls():
address = '192.168.0.2'
dns = '1.1.1.1'

peer = Peer(
'test-peer',
address=address,
dns=ip_address(dns),
service_cls=MyCustomInterface,
)

assert isinstance(peer.service, MyCustomInterface)


@pytest.mark.parametrize(
('cls',),
[
(MyCustomConfig,),
(IPv4Address,),
],
)
def test_peer_invalid_custom_service_cls(cls):
address = '192.168.0.2'
dns = '1.1.1.1'

with pytest.raises(ValueError):
peer = Peer(
'test-peer',
address=address,
dns=ip_address(dns),
service_cls=cls,
)
12 changes: 0 additions & 12 deletions tests/test_json.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@

import functools
import json
import pytest

from subnet import (
ip_network,
IPv4Network,
IPv4Address,
)

from wireguard import (
INTERFACE,
PORT,
Config,
ServerConfig,
Peer,
Server,
)
from wireguard.utils import generate_key, public_key


def test_server_json_dump_ipv4():
Expand Down
8 changes: 3 additions & 5 deletions tests/test_peers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
INTERFACE,
PORT,
Config,
ServerConfig,
Peer,
Server,
)
from wireguard.utils import public_key

Expand Down Expand Up @@ -69,7 +67,7 @@ def test_basic_peer(ipv4_address, ipv6_address):
assert not peer.keepalive
assert not peer.preshared_key

config = peer.config()
config = peer.config
assert isinstance(config, Config)

wg_config = config.local_config
Expand Down Expand Up @@ -139,7 +137,7 @@ def test_peer_mtu(mtu):
assert not peer.keepalive
assert not peer.preshared_key

config = peer.config()
config = peer.config
config_lines = config.local_config.split('\n')
assert f'MTU = {mtu}' in config_lines

Expand Down Expand Up @@ -210,7 +208,7 @@ def test_peer_dns():
assert not peer.keepalive
assert not peer.preshared_key

config = peer.config()
config = peer.config
config_lines = config.local_config.split('\n')
assert f'DNS = {dns}' in config_lines

Expand Down
15 changes: 2 additions & 13 deletions tests/test_qrcode.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@

import pytest
from unittest.mock import (
call,
mock_open,
patch,
)

from subnet import ip_network, IPv4Network, IPv4Address

from wireguard import (
Config,
ServerConfig,
Peer,
Server,
)
from wireguard.utils import IPAddressSet


def test_peer_qrcode():
Expand All @@ -29,7 +18,7 @@ def test_peer_qrcode():
address=address,
)

assert peer.config().qrcode
assert peer.config.qrcode


def test_peer_qrcode_not_present():
Expand All @@ -49,6 +38,6 @@ def test_peer_qrcode_not_present():

# If qrcode is not present in the venv, test it fails appropriately.
with pytest.raises(AttributeError) as exc:
peer.config().qrcode
peer.config.qrcode

assert 'add the qrcode' in str(exc.value)
Loading

0 comments on commit c6530b0

Please sign in to comment.