134 lines
7.5 KiB
Nix
134 lines
7.5 KiB
Nix
# `nix flake check` (Linux, KVM): two machines. `server` runs the module; `client` talks to it
|
|
# through Caddy with TLS, does everything the protocol allows and some things it refuses, fills a
|
|
# mailbox cap to its 507, relays a call's packets through coturn with credentials from the relay, and then
|
|
# the test fails if the client's address appears anywhere in the server's journal, under /var/log, in the
|
|
# relay's database or where coturn runs (.agents/features/home-relay.md, call-kinds.md).
|
|
{ self, pkgs }:
|
|
pkgs.testers.runNixOSTest {
|
|
name = "tabellarius-relay-keeps-no-address";
|
|
|
|
nodes.server = { config, lib, ... }: {
|
|
imports = [ self.nixosModules.default ];
|
|
services.tabellarius = {
|
|
enable = true;
|
|
domain = "relay.test";
|
|
maxMailboxes = 3;
|
|
# Calls (calls-relay.md): the TURN route, with a secret made at boot, never in the repository or the store.
|
|
turn.secretFile = "/run/turn-test/secret";
|
|
turn.urls = [ "turn:relay.test:3478?transport=udp" ];
|
|
# coturn with the same secret. The test network is private, so the client's range is let through here;
|
|
# every other private range stays denied.
|
|
turn.coturn.enable = true;
|
|
turn.coturn.allowedPeerIps = [ "192.168.1.0-192.168.1.255" ];
|
|
};
|
|
# The VM's first NIC is QEMU's user network, which every node shares as 10.0.2.15: coturn listens and relays on
|
|
# the test network only.
|
|
services.coturn.listening-ips = [ config.networking.primaryIPAddress ];
|
|
services.coturn.relay-ips = [ config.networking.primaryIPAddress ];
|
|
systemd.services.turn-test-secret = {
|
|
wantedBy = [ "tabellarius.service" "coturn.service" ];
|
|
before = [ "tabellarius.service" "coturn.service" ];
|
|
serviceConfig.Type = "oneshot";
|
|
script = "install -d -m 0700 /run/turn-test && head -c 32 /dev/urandom | base64 > /run/turn-test/secret";
|
|
};
|
|
# No Let's Encrypt inside a VM: Caddy's own CA instead.
|
|
services.caddy.virtualHosts."relay.test".extraConfig = lib.mkBefore "tls internal";
|
|
};
|
|
|
|
nodes.client = { pkgs, ... }: {
|
|
# coturn for its test tools: turnutils_uclient (a TURN client) and turnutils_peer (the far end).
|
|
environment.systemPackages = [ pkgs.curl pkgs.python3 pkgs.coturn ];
|
|
# The peer answers the relayed packets on 3480/udp.
|
|
networking.firewall.enable = false;
|
|
};
|
|
|
|
testScript = ''
|
|
import base64, hashlib, json, os, re
|
|
|
|
def b64(raw):
|
|
return base64.urlsafe_b64encode(raw).rstrip(b"=").decode()
|
|
|
|
start_all()
|
|
server.wait_for_unit("tabellarius.service")
|
|
server.wait_for_unit("caddy.service")
|
|
server.wait_for_unit("coturn.service")
|
|
server.wait_for_open_port(443)
|
|
# coturn listens on the test network only, not on localhost, which is where wait_for_open_port looks.
|
|
server.wait_until_succeeds("ss -Hltn 'sport = :3478' | grep -q .")
|
|
client.wait_for_unit("multi-user.target")
|
|
|
|
client_ip = client.succeed("ip -4 -o addr show dev eth1 | awk '{print $4}' | cut -d/ -f1").strip()
|
|
server_ip = server.succeed("ip -4 -o addr show dev eth1 | awk '{print $4}' | cut -d/ -f1").strip()
|
|
assert client_ip and server_ip and client_ip != server_ip, (client_ip, server_ip)
|
|
|
|
def call(method, path, token=None, body=None, json_body=False):
|
|
cmd = ["curl", "-sk", "-o", "/dev/null", "-w", "%{http_code}",
|
|
"--resolve", "relay.test:443:" + server_ip, "-X", method]
|
|
if token:
|
|
cmd += ["-H", "Authorization: Bearer " + token]
|
|
if body is not None:
|
|
client.succeed("cat > /tmp/body <<'EOF'\n" + body + "\nEOF")
|
|
client.succeed("truncate -s -1 /tmp/body")
|
|
cmd += ["--data-binary", "@/tmp/body"]
|
|
if json_body:
|
|
cmd += ["-H", "Content-Type: application/json"]
|
|
cmd.append("https://relay.test" + path)
|
|
return client.succeed(" ".join("'" + c + "'" for c in cmd)).strip()
|
|
|
|
def mailbox():
|
|
mid, post, fetch = os.urandom(16), os.urandom(32), os.urandom(32)
|
|
body = json.dumps({"id": b64(mid), "postTokenHash": b64(hashlib.sha256(post).digest()),
|
|
"fetchTokenHash": b64(hashlib.sha256(fetch).digest())})
|
|
return b64(mid), b64(post), b64(fetch), body
|
|
|
|
assert call("GET", "/version") == "200"
|
|
mid, post, fetch, body = mailbox()
|
|
assert call("POST", "/v0/mailboxes", body=body, json_body=True) == "201"
|
|
blob = "sealed-envelope-for-the-test"
|
|
assert call("POST", "/v0/mailboxes/" + mid + "/blobs", token=post, body=blob) == "201"
|
|
assert call("GET", "/v0/mailboxes/" + mid + "/blobs", token=fetch) == "200"
|
|
digest = b64(hashlib.sha256(blob.encode()).digest())
|
|
assert call("DELETE", "/v0/mailboxes/" + mid + "/blobs/" + digest, token=fetch) == "204"
|
|
# Calls: TURN credentials for the fetch token only; a long-poll with nothing arriving answers after its wait.
|
|
assert call("POST", "/v0/mailboxes/" + mid + "/turn", token=fetch) == "200"
|
|
assert call("POST", "/v0/mailboxes/" + mid + "/turn", token=post) == "404"
|
|
creds = json.loads(client.succeed(
|
|
"curl -sk --resolve relay.test:443:" + server_ip + " -X POST -H 'Authorization: Bearer " + fetch + "'"
|
|
+ " https://relay.test/v0/mailboxes/" + mid + "/turn"))
|
|
assert creds["urls"] == ["turn:relay.test:3478?transport=udp"], creds
|
|
# coturn relays a call's packets for those credentials, to a peer on the client, and refuses a wrong password.
|
|
client.succeed("turnutils_peer -L " + client_ip + " -p 3480 >/dev/null 2>&1 &")
|
|
client.succeed("sleep 1")
|
|
uclient = ("timeout 60 turnutils_uclient -n 10 -m 1 -l 120 -e " + client_ip + " -r 3480 -u '"
|
|
+ creds["username"] + "' -w '{}' " + server_ip)
|
|
out = client.succeed(uclient.format(creds["password"]) + " 2>&1")
|
|
print(out)
|
|
sent = re.search(r"tot_recv_msgs=(\d+)", out)
|
|
# The output goes into the message: Nix keeps only the last lines of a failed test's log.
|
|
assert sent and int(sent.group(1)) > 0, "no packet came back through coturn: " + out[-2500:]
|
|
refused = client.fail(uclient.format(b64(os.urandom(20))) + " 2>&1")
|
|
assert "401" in refused, "a wrong password was not refused with 401: " + refused[-2500:]
|
|
|
|
assert call("GET", "/v0/mailboxes/" + mid + "/blobs?wait=1", token=fetch) == "200"
|
|
# Refusals: a wrong token, a malformed id, a body that is not JSON.
|
|
assert call("GET", "/v0/mailboxes/" + mid + "/blobs", token=b64(os.urandom(32))) == "404"
|
|
assert call("GET", "/v0/mailboxes/nonsense/blobs", token=fetch) == "404"
|
|
assert call("POST", "/v0/mailboxes", body="not json", json_body=True) == "400"
|
|
# The cap: three mailboxes, then 507.
|
|
for _ in range(2):
|
|
assert call("POST", "/v0/mailboxes", body=mailbox()[3], json_body=True) == "201"
|
|
assert call("POST", "/v0/mailboxes", body=mailbox()[3], json_body=True) == "507"
|
|
|
|
# Nowhere on the server: not in the journal, not under /var/log, not in the relay's data.
|
|
server.succeed("journalctl --sync")
|
|
server.fail("journalctl --no-pager -o verbose | grep -F '" + client_ip + "'")
|
|
server.fail("grep -rF '" + client_ip + "' /var/log /var/lib/private/tabellarius")
|
|
# Nor where coturn runs: its config and pid in /run, any temporary file.
|
|
# -s: a missing directory must not turn a match into exit 2, which `fail` would accept.
|
|
server.fail("grep -rsF '" + client_ip + "' /run/coturn /run/turnserver /tmp /var/tmp")
|
|
# The TURN secret reached the relay through systemd's credentials, never its database.
|
|
secret = server.succeed("cat /run/turn-test/secret").strip()
|
|
server.fail("grep -rF '" + secret + "' /var/lib/private/tabellarius /var/log")
|
|
'';
|
|
}
|