fix(vpn): make the WireGuard peer key's prefix URL-safe

get_vpn_tunnels keyed a peer as wireguard-<iface>-<pubkey[:8]>. Base64
uses "/" and "+", and the key is used as a URL path segment; a "/"
breaks the route even percent-encoded. The prefix now maps "+" to "-"
and "/" to "_", as napalm-opnsense does (NAPALM/napalm-opnsense#32).
Affected peers change their key once.

Closes #43
This commit is contained in:
Christian Manivong committed 2026-11-01 21:45:42 +01:00
1 parent eaa08ae7e3
commit df4f4b0e4d
3 files changed
+33 -3

No files matched your search

+5
View File
@@ -52,6 +52,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
`uninstall_package()` already does (#4). It used to search the output for words like `uninstall_package()` already does (#4). It used to search the output for words like
"failed": an install whose postinst merely mentioned a failure read as failed, and a "failed": an install whose postinst merely mentioned a failure read as failed, and a
failure that used none of the words read as success. failure that used none of the words read as success.
- `get_vpn_tunnels()` makes the public-key prefix in a WireGuard peer's key URL-safe
(`+` becomes `-`, `/` becomes `_`), as napalm-opnsense does (#43). A key with `/`
could not be used as a URL path segment, so such a tunnel could not be edited in
netOrk. The key of a peer whose first eight key characters hold `+` or `/` changes
once; `public_key` is reported unchanged.
## [0.15.1] - 2026-10-23 ## [0.15.1] - 2026-10-23
+3 -3
View File
@@ -2112,8 +2112,8 @@ class LinuxDriver(
Falls back to interface-level data from ``ip link`` + ``/proc/net/dev`` Falls back to interface-level data from ``ip link`` + ``/proc/net/dev``
when root access is unavailable. when root access is unavailable.
Full data keyed by ``wireguard-<iface>-<pubkey[:8]>`` (one entry per peer). Full data keyed by ``wireguard-<iface>-<pubkey[:8]>``, one per peer, URL-safe
Fallback keyed by ``wireguard-<iface>`` (one entry per WireGuard interface). (``+`` → ``-``, ``/`` → ``_``); the fallback by ``wireguard-<iface>``, one per interface.
""" """
import time as _time import time as _time
@@ -2190,7 +2190,7 @@ class LinuxDriver(
local_port = listen_ports.get(iface, "") local_port = listen_ports.get(iface, "")
local_ep = f":{local_port}" if local_port and local_port != "0" else "" local_ep = f":{local_port}" if local_port and local_port != "0" else ""
key = f"wireguard-{iface}-{pubkey[:8]}" key = f"wireguard-{iface}-{pubkey[:8].replace('+', '-').replace('/', '_')}"
tunnels[key] = { tunnels[key] = {
"type": "WireGuard", "type": "WireGuard",
"local_endpoint": local_ep, "local_endpoint": local_ep,
+25
View File
@@ -2040,3 +2040,28 @@ class TestHardensItsSshServer:
with patch.object(driver, "run_command", return_value=CommandResult(out, "", 0)) as run: with patch.object(driver, "run_command", return_value=CommandResult(out, "", 0)) as run:
assert driver.get_ssh_server_algorithms()["mac"] == ["d"] assert driver.get_ssh_server_algorithms()["mac"] == ["d"]
assert run.call_args.kwargs["privileged"] is True assert run.call_args.kwargs["privileged"] is True
class TestWireGuardTunnelKeys:
"""A tunnel's key is a URL path segment for its consumer (#43): the public
key's base64 prefix is made URL-safe, as napalm-opnsense does."""
_DUMP = (
"wg0\tPRIVKEY\tSERVERPUBKEY=\t51820\toff\n"
"wg0\tab/c+d9EfGhIjKlMnOpQrStUvWxYz0123456789AB=\t(none)\t"
"198.51.100.7:51820\t10.8.0.2/32\t0\t1024\t2048\toff\n"
)
def test_a_slash_or_plus_in_the_key_prefix_is_url_safe(self, driver):
_mock_send(driver, self._DUMP)
tunnels = driver.get_vpn_tunnels()
assert list(tunnels) == ["wireguard-wg0-ab_c-d9E"]
def test_the_public_key_itself_is_reported_unchanged(self, driver):
_mock_send(driver, self._DUMP)
(tunnel,) = driver.get_vpn_tunnels().values()
assert tunnel["public_key"] == "ab/c+d9EfGhIjKlMnOpQrStUvWxYz0123456789AB="