Files
homelab/services/identity/pass-broker-fakes.nix
T
darmanandClaude Opus 5.5 e95726d69c 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>
2026-10-21 11:34:19 +02:00

107 lines
4.4 KiB
Nix

# Fakes shared by pass-broker-test.nix and pass-broker-gateway-test.nix: a
# pass-cli that only logs in with the expected PAT (and refuses one on its
# command line), and a Telegram API on 127.0.0.1:8081 whose button presses
# the test script injects via /test/press.
{ pkgs }:
{
passCli = pkgs.writeShellScriptBin "pass-cli" ''
set -eu
case "$*" in *pst_*) echo "PAT on the command line" >&2; exit 99 ;; esac
s="$PROTON_PASS_SESSION_DIR"; mkdir -p "$s"
case "$1" in
info) [ -f "$s/ok" ] ;;
logout) rm -f "$s/ok" ;;
login) [ "''${PROTON_PASS_PERSONAL_ACCESS_TOKEN:-}" = "pst_test::key" ] && touch "$s/ok" ;;
item)
[ -f "$s/ok" ] || { echo "not logged in" >&2; exit 1; }
case "$2" in
list) printf '%s\n' \
'- [id1]: darman@jupiter (state=Active)' \
'- [id2]: root@jupiter (state=Active)' \
'- [id4]: secret-note (state=Active)' \
'- [id5]: auto-item (state=Active)' ;;
view)
[ -n "''${PROTON_PASS_AGENT_REASON:-}" ] || { echo "reason required" >&2; exit 1; }
echo "$PROTON_PASS_AGENT_REASON" >> "$HOME/reasons"
case "$*" in
*"--item-id id1 "*) echo hunter2 ;;
*"--item-id id2 "*) echo rootpw ;;
*"--item-id id5 "*) echo autopw ;;
*) exit 1 ;;
esac ;;
esac ;;
*) exit 1 ;;
esac
'';
telegram = pkgs.writers.writePython3Bin "fake-telegram" { flakeIgnore = [ "E501" "E701" "E704" ]; } ''
import json
import threading
import time
from http.server import ThreadingHTTPServer, BaseHTTPRequestHandler
from urllib.parse import urlparse, parse_qs
PREFIX = "/bot123:test/"
cv = threading.Condition()
state = {"messages": [], "edits": [], "answers": []}
updates = []
class H(BaseHTTPRequestHandler):
def reply(self, obj, code=200):
b = json.dumps(obj).encode()
self.send_response(code)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(b)))
self.end_headers()
self.wfile.write(b)
def do_GET(self):
with cv: self.reply(state)
def do_POST(self):
n = int(self.headers.get("Content-Length", 0))
body = json.loads(self.rfile.read(n) or b"{}")
u = urlparse(self.path)
if u.path == "/test/press":
q = parse_qs(u.query)
with cv:
uid = len(updates) + 1
updates.append({"update_id": uid, "callback_query": {
"id": "cq%d" % uid, "from": {"id": int(q["from"][0])}, "data": q["data"][0]}})
cv.notify_all()
return self.reply({"ok": True})
if not u.path.startswith(PREFIX):
return self.reply({"ok": False, "description": "Unauthorized"}, 401)
method = u.path[len(PREFIX):]
with cv:
if method == "sendMessage":
body["message_id"] = len(state["messages"]) + 1
state["messages"].append(body)
return self.reply({"ok": True, "result": {"message_id": body["message_id"]}})
if method in ("editMessageText", "answerCallbackQuery"):
state["edits" if method == "editMessageText" else "answers"].append(body)
return self.reply({"ok": True, "result": True})
if method == "getUpdates":
deadline = time.time() + min(body.get("timeout", 0), 5)
while True:
pending = [x for x in updates if x["update_id"] >= body.get("offset", 0)]
if pending or time.time() >= deadline:
return self.reply({"ok": True, "result": pending})
cv.wait(deadline - time.time())
self.reply({"ok": False, "description": "unknown method"}, 404)
def log_message(self, *a): pass
ThreadingHTTPServer(("127.0.0.1", 8081), H).serve_forever()
'';
# try-get <tag> <item>: result lands in /tmp/r/<tag>.{out,err,rc}. The tag
# is in the reason so pkill can pick out one client.
tryGet = pkgs.writeShellScriptBin "try-get" ''
pass-broker get --item "$2" --reason "vm test $1" >/tmp/r/$1.out 2>/tmp/r/$1.err
echo $? >/tmp/r/$1.rc
'';
}