{ pkgs, module }: let # Self-signed pair for the TLS case. cert = pkgs.runCommand "skyg-test-cert" { nativeBuildInputs = [ pkgs.openssl ]; } '' mkdir $out openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes -days 3650 \ -keyout $out/key.pem -out $out/cert.pem -subj '/CN=tls' \ -addext 'subjectAltName=DNS:tls,DNS:localhost,IP:127.0.0.1' ''; in pkgs.testers.runNixOSTest { name = "skyg-linux-tracker"; nodes.plain = { ... }: { imports = [ module ]; services.skyg-linux-tracker = { enable = true; port = 1984; }; }; nodes.tls = { ... }: { imports = [ module ]; services.skyg-linux-tracker = { enable = true; tls = { enable = true; certFile = "${cert}/cert.pem"; keyFile = "${cert}/key.pem"; }; }; }; testScript = '' CLI = "sudo -u skyg-linux-tracker slinuxm" JSON = "-H 'Content-Type: application/json' -H 'Accept: application/json, text/event-stream'" INIT = '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-03-26","capabilities":{},"clientInfo":{"name":"vm","version":"0"}}}' def code(m, url, extra=""): return m.succeed(f"curl -s -o /dev/null -w '%{{http_code}}' {extra} {url}").strip() start_all() with subtest("plain: healthz is public, API needs a token"): plain.wait_for_unit("skyg-linux-tracker.service") plain.wait_for_open_port(1984) assert code(plain, "http://127.0.0.1:1984/healthz") == "200" assert code(plain, "http://127.0.0.1:1984/api/v1/systemd/units") == "401" with subtest("plain: token minted by the service user via the system CLI"): token = plain.succeed(f"{CLI} token create t").strip() auth = f"-H 'Authorization: Bearer {token}'" assert code(plain, "http://127.0.0.1:1984/api/v1/systemd/units", auth) == "200" with subtest("plain: /mcp initialize and tools/list"): plain.succeed( f"curl -sf -D /tmp/h {auth} {JSON} -d '{INIT}' http://127.0.0.1:1984/mcp >/dev/null" ) sid = plain.succeed("awk -F': ' 'tolower($1)==\"mcp-session-id\"{print $2}' /tmp/h | tr -d '\\r'").strip() sess = f"-H 'Mcp-Session-Id: {sid}'" if sid else "" plain.succeed( f"curl -sf {auth} {sess} {JSON} -d '{{\"jsonrpc\":\"2.0\",\"method\":\"notifications/initialized\"}}' http://127.0.0.1:1984/mcp >/dev/null" ) out = plain.succeed( f"curl -sf {auth} {sess} {JSON} -d '{{\"jsonrpc\":\"2.0\",\"id\":2,\"method\":\"tools/list\"}}' http://127.0.0.1:1984/mcp" ) assert "tools" in out, out with subtest("plain: bad Origin on /mcp is rejected"): assert code(plain, "http://127.0.0.1:1984/mcp", f"{auth} {JSON} -H 'Origin: http://evil.example' -d '{INIT}'") == "403" with subtest("plain: revoke takes effect without a restart"): plain.succeed(f"{CLI} token revoke t") assert code(plain, "http://127.0.0.1:1984/api/v1/systemd/units", auth) == "401" with subtest("root is refused by the token CLI"): out = plain.fail("slinuxm token list 2>&1") assert "sudo -u skyg-linux-tracker slinuxm token" in out, out with subtest("tls: healthz over HTTPS, plain HTTP refused, token works"): tls.wait_for_unit("skyg-linux-tracker.service") tls.wait_for_open_port(1984) curl = "--cacert ${cert}/cert.pem" assert code(tls, "https://localhost:1984/healthz", curl) == "200" tls.fail("curl -sf http://localhost:1984/healthz") assert code(tls, "https://localhost:1984/api/v1/systemd/units", curl) == "401" t2 = tls.succeed(f"{CLI} token create t").strip() assert code(tls, "https://localhost:1984/api/v1/systemd/units", f"{curl} -H 'Authorization: Bearer {t2}'") == "200" ''; }