feat(mars): pass-broker approval gateway for every host
A Telegram bot has a single getUpdates consumer, so a broker per host would split approval taps between them. mars now runs the gateway: the bot's only poller, relaying prompts and taps for terra's and titan's brokers over the tailnet (:8097) without ever seeing an item. Each broker authenticates with its own pass_broker_gateway_token; mars holds only the sha256, and the prompt is labelled with the host that token belongs to. The bot token and chat id move to mars and are dropped from the other hosts' sops files (terra.yaml also loses its leftover LibreChat keys and gains protonvpn_wg_private_key, which a later commit uses). Covered by checks.pass-broker-gateway (two nodes: labels, cancel, expiry, per-host token isolation, gateway down); the fakes are shared with checks.pass-broker. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,168 @@
|
||||
# VM test for pass-broker-gateway.nix with a broker in gateway mode. Run:
|
||||
# nix build .#checks.x86_64-linux.pass-broker-gateway -L
|
||||
#
|
||||
# Two nodes: mars runs the gateway and the fake Telegram API, terra a broker
|
||||
# whose prompts go through mars. A third token, titan's, is used only via
|
||||
# curl, to check that hosts can't see or cancel each other's prompts.
|
||||
{ pkgs }:
|
||||
let
|
||||
fakes = import ./pass-broker-fakes.nix { inherit pkgs; };
|
||||
sha256 = builtins.hashString "sha256";
|
||||
in
|
||||
pkgs.testers.runNixOSTest {
|
||||
name = "pass-broker-gateway";
|
||||
|
||||
nodes.mars = {
|
||||
imports = [ ./pass-broker-gateway.nix ];
|
||||
|
||||
environment.systemPackages = [ pkgs.curl pkgs.jq ];
|
||||
networking.firewall.allowedTCPPorts = [ 8097 ];
|
||||
|
||||
environment.etc = {
|
||||
"pb/token" = { text = "123:test\n"; mode = "0400"; user = "pass-broker-gateway"; };
|
||||
"pb/chat" = { text = "4242\n"; mode = "0400"; user = "pass-broker-gateway"; };
|
||||
};
|
||||
|
||||
homelab.passBrokerGateway = {
|
||||
enable = true;
|
||||
telegram = {
|
||||
apiBase = "http://127.0.0.1:8081";
|
||||
tokenFile = "/etc/pb/token";
|
||||
chatIdFile = "/etc/pb/chat";
|
||||
};
|
||||
hosts = {
|
||||
terra.tokenSha256 = sha256 "terra-token";
|
||||
titan.tokenSha256 = sha256 "titan-token";
|
||||
};
|
||||
};
|
||||
|
||||
systemd.services.fake-telegram = {
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
before = [ "pass-broker-gateway.service" ];
|
||||
serviceConfig.ExecStart = "${fakes.telegram}/bin/fake-telegram";
|
||||
};
|
||||
};
|
||||
|
||||
nodes.terra = {
|
||||
imports = [ ./pass-broker.nix ];
|
||||
|
||||
users.users.darman.isNormalUser = true;
|
||||
environment.systemPackages = [ pkgs.curl pkgs.jq fakes.tryGet ];
|
||||
systemd.tmpfiles.rules = [ "d /tmp/r 1777 root root -" ];
|
||||
|
||||
environment.etc = {
|
||||
"pb/pat" = { text = "pst_test::key\n"; mode = "0400"; user = "pass-broker"; };
|
||||
"pb/gateway" = { text = "terra-token\n"; mode = "0400"; user = "pass-broker"; };
|
||||
};
|
||||
|
||||
homelab.passBroker = {
|
||||
enable = true;
|
||||
passCli = fakes.passCli;
|
||||
patFile = "/etc/pb/pat";
|
||||
approvalTimeout = 6;
|
||||
gateway = {
|
||||
url = "http://mars:8097";
|
||||
tokenFile = "/etc/pb/gateway";
|
||||
};
|
||||
rules = [
|
||||
{ item = "darman@*"; }
|
||||
{ item = "root@*"; grantMinutes = 0; }
|
||||
];
|
||||
};
|
||||
};
|
||||
|
||||
testScript = /* python */ ''
|
||||
import json
|
||||
|
||||
def tg():
|
||||
return json.loads(mars.succeed("curl -s http://127.0.0.1:8081/"))
|
||||
|
||||
def wait_messages(n):
|
||||
mars.wait_until_succeeds(f"curl -s http://127.0.0.1:8081/ | jq -e '.messages | length == {n}'")
|
||||
return tg()["messages"][-1]
|
||||
|
||||
def button(msg, i):
|
||||
return msg["reply_markup"]["inline_keyboard"][0][i]["callback_data"]
|
||||
|
||||
def press(data):
|
||||
mars.succeed(f"curl -s -X POST 'http://127.0.0.1:8081/test/press?data={data}&from=4242'")
|
||||
|
||||
def start(tag, item):
|
||||
terra.succeed(f"su darman -c 'try-get {tag} {item}' >/dev/null 2>&1 &")
|
||||
|
||||
def result(tag):
|
||||
terra.wait_for_file(f"/tmp/r/{tag}.rc", timeout=60)
|
||||
rc = int(terra.succeed(f"cat /tmp/r/{tag}.rc"))
|
||||
return rc, terra.succeed(f"cat /tmp/r/{tag}.out").strip(), terra.succeed(f"cat /tmp/r/{tag}.err")
|
||||
|
||||
def api(method, path, token, body=None):
|
||||
data = f"-H 'Content-Type: application/json' -d '{json.dumps(body)}'" if body else ""
|
||||
out = terra.succeed(
|
||||
f"curl -s -o /tmp/api.out -w '%{{http_code}}' -X {method} {data} "
|
||||
f"-H 'Authorization: Bearer {token}' http://mars:8097{path}"
|
||||
)
|
||||
return int(out), terra.succeed("cat /tmp/api.out")
|
||||
|
||||
start_all()
|
||||
mars.wait_for_unit("pass-broker-gateway.service")
|
||||
mars.wait_for_open_port(8097)
|
||||
terra.wait_for_unit("pass-broker.socket")
|
||||
|
||||
with subtest("a broker's prompt arrives through the gateway, labelled by its token"):
|
||||
start("a", "darman@jupiter")
|
||||
msg = wait_messages(1)
|
||||
assert msg["text"].startswith("🔑 terra: HomeLab / darman@jupiter (password)"), msg
|
||||
assert "vm test a" in msg["text"], msg
|
||||
press(button(msg, 0))
|
||||
assert result("a")[:2] == (0, "hunter2"), result("a")
|
||||
assert tg()["edits"][-1]["text"].endswith("approved")
|
||||
|
||||
with subtest("deny refuses"):
|
||||
start("b", "root@jupiter")
|
||||
press(button(wait_messages(2), 1))
|
||||
rc, out, err = result("b")
|
||||
assert rc == 1 and "denied" in err, (rc, out, err)
|
||||
|
||||
with subtest("a client that hangs up withdraws its prompt on the gateway"):
|
||||
start("c", "root@jupiter")
|
||||
wait_messages(3)
|
||||
terra.wait_until_succeeds("grep -q 'waiting for approval' /tmp/r/c.err")
|
||||
terra.succeed("pkill -f 'vm test c'")
|
||||
mars.wait_until_succeeds(
|
||||
"curl -s http://127.0.0.1:8081/ | jq -e '.edits[-1].text | endswith(\"went away\")'"
|
||||
)
|
||||
|
||||
with subtest("no answer expires on the gateway's clock"):
|
||||
start("d", "root@jupiter")
|
||||
wait_messages(4)
|
||||
rc, out, err = result("d")
|
||||
assert rc == 1 and "no answer" in err, (rc, out, err)
|
||||
assert tg()["edits"][-1]["text"].endswith("expired")
|
||||
|
||||
with subtest("tokens: unknown is refused, hosts can't see or cancel each other's prompts"):
|
||||
code, _ = api("POST", "/ask", "wrong-token", {"text": "x", "timeout_secs": 30})
|
||||
assert code == 401, code
|
||||
code, out = api("POST", "/ask", "titan-token", {"text": "terra: trust me", "timeout_secs": 300})
|
||||
assert code == 200, (code, out)
|
||||
titan_id = json.loads(out)["id"]
|
||||
assert wait_messages(5)["text"] == "🔑 titan: terra: trust me"
|
||||
assert api("GET", f"/ask/{titan_id}", "terra-token")[0] == 404
|
||||
assert api("DELETE", f"/ask/{titan_id}", "terra-token")[0] == 404
|
||||
assert json.loads(api("GET", f"/ask/{titan_id}", "titan-token")[1]) == {"status": "pending"}
|
||||
|
||||
with subtest("a tap resolves only its own prompt"):
|
||||
start("e", "root@jupiter")
|
||||
terra_msg = wait_messages(6)
|
||||
press(button(terra_msg, 0))
|
||||
assert result("e")[:2] == (0, "rootpw")
|
||||
assert json.loads(api("GET", f"/ask/{titan_id}", "titan-token")[1]) == {"status": "pending"}
|
||||
assert api("DELETE", f"/ask/{titan_id}", "titan-token")[0] == 204
|
||||
assert json.loads(api("GET", f"/ask/{titan_id}", "titan-token")[1]) == {"status": "cancelled"}
|
||||
|
||||
with subtest("gateway down refuses instead of hanging"):
|
||||
mars.succeed("systemctl stop pass-broker-gateway.service")
|
||||
start("f", "root@jupiter")
|
||||
rc, out, err = result("f")
|
||||
assert rc == 1 and "gateway" in err, (rc, out, err)
|
||||
'';
|
||||
}
|
||||
Reference in New Issue
Block a user