"use strict"; // End-to-end test through the REAL nodes.js express app (module.exports = app): // oneTimeCommand() mints an enrollment into the module-internal store, and the // bootstrap routes registered on the app must serve that exact enrollment — // script once, config once, replays rejected — with the node's credential only // ever appearing in the config response, never in the command or the script. // // The DB layer is stubbed (nodes.js only needs findNodeByIdOrName to resolve // the enrollment's node), so no MySQL is required. const test = require("node:test"); const assert = require("node:assert"); const http = require("node:http"); const Module = require("node:module"); const SENTINEL_TOKEN = "integration-sentinel-token-5f4e3d2c1b"; const SENTINEL_NODE_ID = "node-int-uuid-1"; let currentToken = SENTINEL_TOKEN; process.env.PANEL_HMAC_SECRET = "integration-test-hmac-master-secret"; process.env.NODES_PORT = "0"; process.env.PANEL_PUBLIC_URL = "https://panel.example.com"; const query = async (sql) => { if (/FROM nodes/i.test(String(sql))) { return [{ id: SENTINEL_NODE_ID, uuid: SENTINEL_NODE_ID, name: "integration-node", address: "203.0.113.10", ram_mb: 0, disk_gb: 0, ports: "{}", token_id: "tid-int-1", token: currentToken, token_status: "ok", created_at: Date.now(), api_port: 8080, sftp_port: 2022, port_ok: 1, last_seen: Date.now(), last_check: Date.now(), online: 1, build_config: "{}", max_upload_mb: 10240, }]; } return []; }; const nodesPath = require.resolve("../nodes.js"); const nodesApp = (() => { const load = Module._load; Module._load = function (request, parent, isMain) { if (request === "./db" && parent?.filename === nodesPath) { return { query, pool: { execute: async () => [[]], query: async () => [[]] } }; } return load.call(this, request, parent, isMain); }; try { return require(nodesPath); } finally { Module._load = load; } })(); test("integration: full secret-free bootstrap through the real nodes.js app", async (t) => { const server = http.createServer(nodesApp); await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); const base = `http://127.0.0.1:${server.address().port}`; t.after(() => new Promise(resolve => { server.close(resolve); server.closeAllConnections(); })); // 1. The revealed setup command. const fakeReq = { get: () => "panel.example.com", protocol: "https", secure: true }; const issueCommand = async () => nodesApp.oneTimeCommand(await nodesApp.findNodeByIdOrName(SENTINEL_NODE_ID), fakeReq); const fixtureUrl = (url) => url.replace("https://panel.example.com", base); const scriptUrlFor = (command) => fixtureUrl(/^curl -fsSL '([^']+)' \| bash$/.exec(command)[1]); const command = await issueCommand(); assert.match( command, /^curl -fsSL 'https:\/\/panel\.example\.com\/api\/node-bootstrap\/[A-Za-z0-9_-]{43}' \| bash$/, "command must be the secret-free one-liner" ); assert.ok(!command.includes(SENTINEL_TOKEN), "command must not contain the node token"); assert.ok(!command.includes("hmac_secret"), "command must not contain the hmac secret"); // 2. Download the script exactly as a node admin would. const scriptUrl = scriptUrlFor(command); assert.equal((await fetch(scriptUrl)).status, 403, "plaintext cannot consume the script enrollment"); nodesApp.setNodeBootstrapTransport({ secure: (req) => req.get("x-fixture-transport") === "https", trustedHost: () => "" }); const secureFixture = { headers: { "x-fixture-transport": "https" } }; const scriptRes = await fetch(scriptUrl, secureFixture); assert.equal(scriptRes.status, 200); const script = await scriptRes.text(); assert.ok(script.includes("#!/bin/sh")); assert.ok(!script.includes(SENTINEL_TOKEN), "script must not contain the node token"); assert.ok(!command.includes("token_id"), "command must not contain token ids"); // 3. The script's single-use config link is the only secret channel. const cfgMatch = /CONFIG_URL='([^']+)'/.exec(script); assert.ok(cfgMatch, "script must embed its single-use config URL"); const cfgUrl = fixtureUrl(cfgMatch[1]); assert.equal((await fetch(cfgUrl)).status, 403, "plaintext cannot consume the config enrollment"); const cfgRes = await fetch(cfgUrl, secureFixture); assert.equal(cfgRes.status, 200); const yml = await cfgRes.text(); assert.ok(yml.includes(`token: ${SENTINEL_TOKEN}`), "config must deliver the bearer token"); assert.ok(yml.includes("hmac_secret: \""), "config must deliver the derived hmac secret"); // 4. Every link is dead after first use. assert.equal((await fetch(scriptUrl, secureFixture)).status, 410, "script replay must be rejected"); assert.equal((await fetch(cfgUrl, secureFixture)).status, 410, "config replay must be rejected"); const unopenedScript = scriptUrlFor(await issueCommand()); const outstandingScript = await fetch(scriptUrlFor(await issueCommand()), secureFixture); assert.equal(outstandingScript.status, 200); const outstandingConfig = fixtureUrl(/CONFIG_URL='([^']+)'/.exec(await outstandingScript.text())[1]); currentToken = "integration-replacement-token"; assert.equal((await fetch(unopenedScript, secureFixture)).status, 410, "rotation invalidates unopened setup links"); assert.equal((await fetch(outstandingConfig, secureFixture)).status, 410, "rotation invalidates outstanding config links"); const freshScript = await fetch(scriptUrlFor(await issueCommand()), secureFixture); assert.equal(freshScript.status, 200); const freshConfig = fixtureUrl(/CONFIG_URL='([^']+)'/.exec(await freshScript.text())[1]); const freshResponse = await fetch(freshConfig, secureFixture); assert.equal(freshResponse.status, 200); assert.ok((await freshResponse.text()).includes(`token: ${currentToken}`)); });