[relay] Calls: a long-poll on the fetch route, TURN credentials for coturn, wakes every 10 s; the module passes the TURN secret by systemd credential

This commit is contained in:
2026-12-03 00:29:07 +01:00
parent 1e2eed8e5b
commit 776e4ce39b
15 changed files with 654 additions and 22 deletions
+28 -1
View File
@@ -64,6 +64,24 @@ in
'';
};
turn = {
secretFile = lib.mkOption {
# A string, not a path: a path would copy the secret into the world-readable Nix store.
type = lib.types.nullOr lib.types.str;
default = null;
description = ''
The secret the relay shares with the TURN server for calls (coturn `use-auth-secret`), as a file outside
the Nix store (e.g. written by Custos). Null: no TURN route (`POST /v0/mailboxes/{id}/turn` is 404).
'';
};
urls = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
example = [ "turn:turn.example.org:3478?transport=udp" "turn:turn.example.org:3478?transport=tcp" ];
description = "The TURN servers named in each credential. Required with `secretFile`.";
};
};
openFirewall = lib.mkOption {
type = lib.types.bool;
default = true;
@@ -72,6 +90,11 @@ in
};
config = lib.mkIf cfg.enable {
assertions = [{
assertion = (cfg.turn.secretFile == null) == (cfg.turn.urls == [ ]);
message = "services.tabellarius.turn: secretFile and urls come together, or neither.";
}];
systemd.services.tabellarius = {
description = "Tabellarius message relay (loopback only; Caddy in front)";
wantedBy = [ "multi-user.target" ];
@@ -84,7 +107,11 @@ in
"--max-bytes" (toString cfg.maxBytes)
"--max-mailboxes" (toString cfg.maxMailboxes)
"--max-invites" (toString cfg.maxInvites)
];
] + lib.optionalString (cfg.turn.secretFile != null) (" " + lib.escapeShellArgs (
# systemd hands the secret to the DynamicUser under its credentials directory (%d); it is never in the store.
[ "--turn-secret-file" "%d/turn-secret" ] ++ lib.concatMap (u: [ "--turn-url" u ]) cfg.turn.urls
));
LoadCredential = lib.mkIf (cfg.turn.secretFile != null) [ "turn-secret:${toString cfg.turn.secretFile}" ];
Restart = "on-failure";
DynamicUser = true;
StateDirectory = "tabellarius";
+16
View File
@@ -12,6 +12,15 @@ pkgs.testers.runNixOSTest {
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" ];
};
systemd.services.turn-test-secret = {
wantedBy = [ "tabellarius.service" ];
before = [ "tabellarius.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";
@@ -65,6 +74,10 @@ pkgs.testers.runNixOSTest {
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"
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"
@@ -78,5 +91,8 @@ pkgs.testers.runNixOSTest {
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")
# 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")
'';
}