Archived
1 parent
f5bdd7e21c
commit
affe28ad71
110 files changed
+52936
-14
No files matched your search
@@ -0,0 +1,86 @@
|
||||
from pathlib import Path
|
||||
import runpy
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def network(tmp_path, monkeypatch):
|
||||
pytest.importorskip('fcntl')
|
||||
module = runpy.run_path(str(Path(__file__).resolve().parents[1] / 'scripts/wgtools-helper'))
|
||||
gl = module['extended_forward_add'].__globals__
|
||||
configs = {
|
||||
'wg1': '[Interface]\nAddress = 10.0.1.1/24\nListenPort = 51821\nPrivateKey = key\n[Peer]\nPublicKey = client1\nAllowedIPs = 10.0.1.2/32\n',
|
||||
'wg2': '[Interface]\nAddress = 10.0.2.1/24\nListenPort = 51822\nPrivateKey = key\n[Peer]\nPublicKey = client2\nAllowedIPs = 10.0.2.2/32\n',
|
||||
}
|
||||
monkeypatch.setitem(gl, 'EXT_IP_GROUPS_PATH', tmp_path / 'groups.json')
|
||||
monkeypatch.setitem(gl, '_configured_interface_names', lambda: set(configs))
|
||||
monkeypatch.setitem(gl, '_extended_configured', lambda: set(configs))
|
||||
monkeypatch.setitem(gl, '_extended_host_ips', lambda: {'192.0.2.10': 'eth0', '192.0.2.20': 'eth0'})
|
||||
monkeypatch.setitem(gl, '_extended_listener_conflict', lambda *args: False)
|
||||
monkeypatch.setitem(gl, 'read_config_text', lambda iface: configs[iface])
|
||||
def apply(iface, payload):
|
||||
configs[iface] = payload['content']
|
||||
return {'ok': True}
|
||||
monkeypatch.setitem(gl, 'raw_config_apply', apply)
|
||||
return module, configs
|
||||
|
||||
|
||||
def test_groups_share_addresses_and_enforce_conflicts(network):
|
||||
m, configs = network
|
||||
m['extended_ip_groups_set']({'name':'shared','addresses':['192.0.2.10'],'interfaces':['wg1','wg2']})
|
||||
m['extended_ip_groups_set']({'name':'private','addresses':['192.0.2.20'],'interfaces':['wg2']})
|
||||
forward = {'public_ip':'192.0.2.10','public_port':25565,'target_ip':'10.0.1.2','target_port':25565,'protocol':'both'}
|
||||
added = m['extended_forward_add']('wg1', forward)['forward']
|
||||
assert 'DNAT --to-destination 10.0.1.2:25565' in configs['wg1']
|
||||
assert m['extended_forward_get']('wg1')['addresses'] == ['192.0.2.10']
|
||||
with pytest.raises(ValueError, match='nie jest przypisany'):
|
||||
m['extended_forward_add']('wg1', {**forward, 'public_ip':'192.0.2.20'})
|
||||
with pytest.raises(ValueError, match='już używane'):
|
||||
m['extended_forward_add']('wg2', {**forward, 'target_ip':'10.0.2.2', 'protocol':'tcp'})
|
||||
with pytest.raises(ValueError, match='odebrałaby'):
|
||||
m['extended_ip_groups_set']({'name':'shared', 'delete':True})
|
||||
m['extended_forward_delete']('wg1', {'id':added['id']})
|
||||
assert 'WGEXT-FORWARD' not in configs['wg1']
|
||||
m['extended_ip_groups_set']({'name':'shared', 'delete':True})
|
||||
|
||||
|
||||
def test_rejects_foreign_target_and_wireguard_port(network):
|
||||
m, _ = network
|
||||
m['extended_ip_groups_set']({'name':'shared','addresses':['192.0.2.10'],'interfaces':['wg1']})
|
||||
forward = {'public_ip':'192.0.2.10','public_port':1234,'target_ip':'10.0.2.2','target_port':1234,'protocol':'tcp'}
|
||||
with pytest.raises(ValueError, match='nie należy'):
|
||||
m['extended_forward_add']('wg1', forward)
|
||||
with pytest.raises(ValueError, match='używany przez WireGuard'):
|
||||
m['extended_forward_add']('wg1', {**forward,'target_ip':'10.0.1.2','public_port':51822,'protocol':'udp'})
|
||||
|
||||
|
||||
def test_wireguard_dump_accepts_disabled_keepalive(network, monkeypatch):
|
||||
m, _ = network
|
||||
gl = m['wg_dump'].__globals__
|
||||
monkeypatch.setattr(gl['shutil'], 'which', lambda name: '/usr/bin/wg')
|
||||
dump = 'private\tpublic\t51821\toff\npeer\t(none)\t(none)\t10.0.1.2/32\t0\t0\t0\toff\n'
|
||||
monkeypatch.setitem(gl, 'run', lambda *args: (0, dump, ''))
|
||||
assert m['wg_dump']('wg1')['peers']['peer']['persistent_keepalive_runtime'] == 0
|
||||
|
||||
|
||||
def test_server_editor_preserves_extended_firewall_hooks(network):
|
||||
m, _ = network
|
||||
command = 'iptables -I FORWARD -i wg1 -m comment --comment wgext-forward-wg1-123 -j ACCEPT'
|
||||
assert m['_managed_hook_kind'](command, 'wg1', {}, set()) is None
|
||||
|
||||
|
||||
def test_migration_backup_includes_address_groups(network, tmp_path, monkeypatch):
|
||||
m, _ = network
|
||||
gl = m['_build_migration_archive'].__globals__
|
||||
monkeypatch.setitem(gl, 'DATABASE_PATH', tmp_path / 'absent.db')
|
||||
monkeypatch.setitem(gl, 'CLIENT_DIR', tmp_path / 'clients')
|
||||
monkeypatch.setitem(gl, 'WG_DIR', tmp_path / 'wireguard')
|
||||
monkeypatch.setitem(gl, 'SYSCTL_FILE', tmp_path / 'sysctl')
|
||||
monkeypatch.setitem(gl, 'INTERFACES_META_PATH', tmp_path / 'interfaces.json')
|
||||
monkeypatch.setitem(gl, 'GROUPS_PATH', tmp_path / 'client-groups.json')
|
||||
(tmp_path / 'groups.json').write_text('{"test": {"addresses": [], "interfaces": []}}', encoding='utf8')
|
||||
archive = m['_build_migration_archive']('wg1', tmp_path / 'backups')
|
||||
import tarfile
|
||||
with tarfile.open(archive, 'r:gz') as tar:
|
||||
assert 'data/extended-ip-groups.json' in tar.getnames()
|
||||
Reference in new issue
Block a user