Replace host tokens with host pairing

Hosts join the cluster by running the installer, which shows a code and
waits until the request is approved under Hosts > Add host in the web
interface or with clusterioctl host approve. An admin can pass their
user token to the installer to approve the host right away. The token
is only used for that and is not saved.

The controller only accepts hosts that have a record, and only
approving a host creates one. Revoking a host deletes its record, so
its token can no longer be used to reconnect. Hosts that have connected
before keep working.

Removes host generate-token, revoke-token and create-config from
clusterioctl, bootstrap generate-host-token, the host token messages and
the core.host.generate_token, revoke_token and create_config
permissions, which are also stripped from stored roles. Adds
clusterioctl host pending, approve, reject and revoke, and the
core.host.approve and core.host.revoke permissions.

Standalone installs add the host to the controller database directly,
since the controller is not running during the install.
This commit is contained in:
Danielv123 committed 2026-10-30 06:11:32 +01:00
1 parent fa2883d74e
commit 87d4f5c64e
49 files changed
+4855 -473

No files matched your search

+213 -1
View File
@@ -1,8 +1,11 @@
import assert from "node:assert/strict";
import events from "node:events";
import * as lib from "@clusterio/lib";
import { Controller, ControlConnection, ControllerHooks, InstanceManager } from "@clusterio/controller";
import {
Controller, ControlConnection, ControllerHooks, HostConnection, HostRecord, InstanceManager, InstanceRecord,
} from "@clusterio/controller";
import { MockConnector, MockController, MockLogger } from "../mock.js";
import "../setup_logging.js";
const addr = lib.Address.fromShorthand;
@@ -272,4 +275,213 @@ describe("controller/src/ControlConnection", function() {
assert.equal(player.connector.terminated, true);
});
});
describe("host management", function() {
/** @type {Controller} */
let controller;
/** @type {ControlConnection} */
let connection;
let audits;
let originalAudit;
beforeEach(function() {
const config = new lib.ControllerConfig("controller");
config.set("controller.auth_secret", Buffer.from("TestSecretDoNotUse").toString("base64"));
controller = new Controller(lib.logger, [], config);
const connector = new lib.VirtualConnector(addr("controller"), addr({ controlId: 1 }));
const user = controller.users.getOrCreateUser("admin");
connection = new ControlConnection({ version: "2.0.0" }, connector, controller, user, 1);
audits = [];
originalAudit = lib.logger.audit;
lib.logger.audit = msg => { audits.push(msg); };
});
afterEach(function() {
lib.logger.audit = originalAudit;
});
it("registers the host pairing and revoke handlers", function() {
for (const Request of [
lib.HostRevokeRequest,
lib.HostPairingListRequest,
lib.HostPairingApproveRequest,
lib.HostPairingRejectRequest,
]) {
assert(connection._requestHandlers.has(Request), `${Request.name} is not handled`);
}
});
describe(".handleHostRevokeRequest()", function() {
let record;
let broadcasts;
beforeEach(function() {
record = new HostRecord(10, "host-10", "", new Map());
controller.hosts.set(record);
broadcasts = [];
controller.subscriptions.broadcast = event => {
if (event instanceof lib.HostUpdatesEvent) {
broadcasts.push(...event.updates.map(host => ({ id: host.id, isDeleted: host.isDeleted })));
}
};
});
function assignInstance(id, name, hostId) {
const instanceConfig = new lib.InstanceConfig("controller");
instanceConfig.set("instance.id", id);
instanceConfig.set("instance.name", name);
instanceConfig.set("instance.assigned_host", hostId);
controller.instances.records.set(new InstanceRecord(instanceConfig, "stopped"));
}
async function revoke(hostId = 10) {
await connection.handleHostRevokeRequest(new lib.HostRevokeRequest(hostId));
}
it("deletes and saves the host record", async function() {
let savedWithoutHost;
const originalSave = controller.hosts.save;
controller.hosts.save = async function() {
savedWithoutHost = !this.has(10);
return await originalSave.call(this);
};
await revoke();
assert.equal(controller.hosts.has(10), false);
assert.equal(savedWithoutHost, true, "hosts were not saved after deleting the record");
assert.deepEqual(broadcasts, [{ id: 10, isDeleted: true }]);
assert.equal(audits.length, 1);
assert.match(audits[0], /User admin revoked host host-10 \(10\)/);
});
it("deletes the note of the host", async function() {
controller.setNote("host", "10", "rack 3", "someone");
controller.setNote("host", "11", "rack 4", "someone");
await revoke();
const note = controller.notes.get("host/10");
assert.equal(note.isDeleted, true);
assert.equal(note.content, "");
assert.equal(note.updatedBy, "admin");
assert.equal(controller.notes.get("host/11").isDeleted, false);
});
it("terminates the connection of the host", async function() {
let terminated = false;
controller.wsServer.hostConnections.set(10, { connector: { terminate() { terminated = true; } } });
await revoke();
assert.equal(terminated, true);
});
it("terminates the connection of the host when saving fails", async function() {
let terminated = false;
controller.wsServer.hostConnections.set(10, { connector: { terminate() { terminated = true; } } });
controller.hosts.save = async () => { throw new Error("EPERM: operation not permitted"); };
await assert.rejects(revoke(), /EPERM/);
assert.equal(terminated, true, "revoked host kept its connection");
assert.equal(controller.hosts.has(10), false);
assert.equal(controller.notes.get("host/10"), undefined);
});
it("does not leave a host record behind when the connection closes", async function() {
const connector = new MockConnector(addr("controller"), addr({ hostId: 10 }));
connector.terminate = () => { connector.emit("close"); };
const hostConnection = new HostConnection(
new lib.RegisterHostData("", "2.0.0", 10, {}), connector, controller, "10.0.0.1"
);
controller.wsServer.hostConnections.set(10, hostConnection);
assert.equal(controller.hosts.get(10).connected, true);
await revoke();
assert.equal(controller.hosts.has(10), false, "host record was recreated");
assert.deepEqual(broadcasts.at(-1), { id: 10, isDeleted: true });
});
it("refuses to revoke a host with assigned instances", async function() {
controller.setNote("host", "10", "rack 3", "someone");
let terminated = false;
controller.wsServer.hostConnections.set(10, { connector: { terminate() { terminated = true; } } });
assignInstance(101, "alpha", 10);
assignInstance(102, "beta", 10);
assignInstance(201, "gamma", 20);
await assert.rejects(
revoke(),
new lib.ConflictError("Cannot revoke host host-10 while instances are assigned to it: alpha, beta"),
);
assert.equal(controller.hosts.get(10), record);
assert.equal(controller.notes.get("host/10").isDeleted, false);
assert.equal(terminated, false);
assert.deepEqual(broadcasts, []);
});
it("ignores instances assigned to other hosts", async function() {
assignInstance(201, "gamma", 20);
await revoke();
assert.equal(controller.hosts.has(10), false);
});
it("throws NotFoundError for an unknown host", async function() {
await assert.rejects(revoke(99), new lib.NotFoundError("Unknown host id (99)"));
assert.equal(controller.hosts.has(10), true);
});
});
describe("host pairing requests", function() {
it("lists pending requests", async function() {
const first = controller.hostPairing.create("Host A", "2.0.0", "10.0.0.1");
const second = controller.hostPairing.create("Host B", "2.0.0", "10.0.0.2");
const list = await connection.handleHostPairingListRequest(new lib.HostPairingListRequest());
assert(list.every(details => details instanceof lib.HostPairingDetails));
assert.deepEqual(list.map(details => details.code).sort(), [first.code, second.code].sort());
assert.equal(list.find(details => details.code === first.code).name, "Host A");
});
it("leaves out expired, approved and rejected requests when listing", async function() {
const expired = controller.hostPairing.create("Host A", "", "10.0.0.1");
const approved = controller.hostPairing.create("Host B", "", "10.0.0.2");
const rejected = controller.hostPairing.create("Host C", "", "10.0.0.3");
const pending = controller.hostPairing.create("Host D", "", "10.0.0.4");
controller.hostPairing._bySecret.get(expired.requestId).lastPollMs -= 60e3;
await controller.hostPairing.approve(approved.code, undefined, "admin");
controller.hostPairing.reject(rejected.code, "admin");
const list = await connection.handleHostPairingListRequest(new lib.HostPairingListRequest());
assert.deepEqual(list.map(details => details.code), [pending.code]);
});
it("approves a request and returns the new host id", async function() {
const { requestId, code } = controller.hostPairing.create("Host A", "", "10.0.0.1");
const hostId = await connection.handleHostPairingApproveRequest(
new lib.HostPairingApproveRequest(code.toLowerCase().replace("-", ""))
);
assert.equal(typeof hostId, "number");
assert.equal(controller.hosts.get(hostId).name, "Host A");
assert.equal(controller.hostPairing.poll(requestId).hostId, hostId);
assert(audits.some(msg => msg.startsWith(`User admin approved host Host A (${hostId})`)));
});
it("approves a request with a different name", async function() {
const { requestId, code } = controller.hostPairing.create("Host A", "", "10.0.0.1");
const hostId = await connection.handleHostPairingApproveRequest(
new lib.HostPairingApproveRequest(code, "Renamed")
);
assert.equal(controller.hosts.get(hostId).name, "Renamed");
assert.equal(controller.hostPairing.poll(requestId).name, "Renamed");
});
it("rejects approving an unknown code or with an invalid name", async function() {
await assert.rejects(
connection.handleHostPairingApproveRequest(new lib.HostPairingApproveRequest("ZZZ-ZZZ")),
new lib.NotFoundError("No pending host request with code ZZZ-ZZZ"),
);
const { code } = controller.hostPairing.create("Host A", "", "10.0.0.1");
await assert.rejects(
connection.handleHostPairingApproveRequest(new lib.HostPairingApproveRequest(code, " ")),
new lib.ValidationError("Host name is required"),
);
assert.equal(controller.hosts.size, 0);
});
it("rejects a request", async function() {
const { requestId, code } = controller.hostPairing.create("Host A", "", "10.0.0.1");
await connection.handleHostPairingRejectRequest(new lib.HostPairingRejectRequest(code.toLowerCase()));
assert.equal(controller.hostPairing.requests.has(code), false);
assert.throws(() => controller.hostPairing.poll(requestId), /Request was rejected/);
assert.equal(controller.hosts.size, 0);
assert(audits.some(msg => msg.startsWith(`User admin rejected host Host A with code ${code}`)));
});
it("throws NotFoundError when rejecting an unknown code", async function() {
await assert.rejects(
connection.handleHostPairingRejectRequest(new lib.HostPairingRejectRequest("ZZZ-ZZZ")),
new lib.NotFoundError("No pending host request with code ZZZ-ZZZ"),
);
});
});
});
});
+97 -1
View File
@@ -1,6 +1,9 @@
import assert from "node:assert/strict";
import * as lib from "@clusterio/lib";
import { HostConnection } from "@clusterio/controller";
import { Controller, HostConnection, HostRecord } from "@clusterio/controller";
import { MockConnector } from "../mock.js";
const addr = lib.Address.fromShorthand;
function makeSave(instanceId, name) {
return new lib.SaveDetails(instanceId, "file", name, 0, 0, false, false, 0, false);
@@ -40,6 +43,99 @@ function makeInstances(assignments, names = new Map()) {
}
describe("controller/src/HostConnection", function() {
describe("host record", function() {
/** @type {Controller} */
let controller;
let record;
let connector;
let updates;
beforeEach(function() {
controller = new Controller(lib.logger, [], new lib.ControllerConfig("controller"));
record = new HostRecord(10, "host-10", "1.0.0", new Map(), false, "", "203.0.113.1", 123);
controller.hosts.set(record);
connector = new MockConnector(addr("controller"), addr({ hostId: 10 }));
updates = [];
controller.hosts.on("update", changed => {
updates.push(...changed.map(host => ({ id: host.id, name: host.name, isDeleted: host.isDeleted })));
});
});
function connect(id = 10) {
return new HostConnection(
new lib.RegisterHostData("", "2.0.0", id, {}), connector, controller, "10.0.0.1"
);
}
function revoke() {
controller.hosts.delete(record);
updates.length = 0;
}
it("should refuse a host without a record", function() {
assert.throws(() => connect(11), /Host 11 is not registered/);
assert.equal(controller.hosts.has(11), false);
assert.deepEqual(updates, []);
});
it("should update the existing record on connect", function() {
const connection = connect();
assert.equal(controller.hosts.get(10), record, "host record was replaced");
assert.equal(connection.id, 10);
assert.equal(record.connected, true);
assert.equal(record.version, "2.0.0");
assert.equal(record.remoteAddress, "10.0.0.1");
assert.equal(record.name, "host-10");
assert.equal(record.publicAddress, "203.0.113.1");
assert.equal(record.tokenValidAfter, 123);
assert.deepEqual(updates, [{ id: 10, name: "host-10", isDeleted: false }]);
});
it("should mark the host disconnected when the connection closes", function() {
connect();
updates.length = 0;
connector.emit("close");
assert.equal(controller.hosts.get(10), record);
assert.equal(record.connected, false);
assert.deepEqual(updates, [{ id: 10, name: "host-10", isDeleted: false }]);
});
it("should save info updates from the host", async function() {
const connection = connect();
updates.length = 0;
await connection.handleHostInfoUpdateEvent(
new lib.HostInfoUpdateEvent(new lib.HostInfoUpdate("renamed", "198.51.100.1"))
);
assert.equal(controller.hosts.get(10).name, "renamed");
assert.equal(controller.hosts.get(10).publicAddress, "198.51.100.1");
assert.deepEqual(updates, [{ id: 10, name: "renamed", isDeleted: false }]);
});
it("should not recreate the record of a revoked host when the connection closes", function() {
connect();
revoke();
connector.emit("close");
assert.equal(controller.hosts.has(10), false, "host record was recreated");
assert.deepEqual(updates, []);
});
it("should not recreate the record of a revoked host on info updates", async function() {
const connection = connect();
revoke();
await connection.handleHostInfoUpdateEvent(
new lib.HostInfoUpdateEvent(new lib.HostInfoUpdate("renamed", "198.51.100.1"))
);
assert.equal(controller.hosts.has(10), false, "host record was recreated");
assert.deepEqual(updates, []);
});
it("should not overwrite a newer record with the same id", function() {
connect();
revoke();
const newer = new HostRecord(10, "newer", "", new Map());
controller.hosts.set(newer);
updates.length = 0;
connector.emit("close");
assert.equal(controller.hosts.get(10), newer);
assert.equal(newer.name, "newer");
assert.deepEqual(updates, []);
});
});
describe(".handleInstanceSaveDetailsUpdatesEvent()", function() {
// Instance 1 is on host 10, instance 2 is on host 20.
const assignments = new Map([[1, 10], [2, 20]]);
+648
View File
@@ -0,0 +1,648 @@
import assert from "node:assert/strict";
import crypto from "node:crypto";
import jwt from "jsonwebtoken";
import * as lib from "@clusterio/lib";
import { Controller, HostPairingManager, HostPairingError, HostRecord } from "@clusterio/controller";
import {
addressKey, normaliseCode, pollTimeoutMs, maxPendingMs, maxPending, maxPendingPerAddress,
maxCreatesPerMinute, maxNameLength,
} from "@clusterio/controller/dist/node/src/HostPairingManager.js";
import "../setup_logging.js";
const codePattern = /^[2-9A-HJKMNP-Z]{3}-[2-9A-HJKMNP-Z]{3}$/;
function makeController() {
const config = new lib.ControllerConfig("controller");
config.set("controller.auth_secret", Buffer.from("TestSecretDoNotUse").toString("base64"));
return new Controller(lib.logger, [], config);
}
/**
* Make crypto.randomInt return the given values before falling back to random ones
*
* @param {number[]} values - Values to return, in order.
* @returns {() => void} function restoring crypto.randomInt.
*/
function stubRandomInt(values) {
const original = crypto.randomInt;
crypto.randomInt = (...args) => (values.length ? values.shift() : original(...args));
return () => { crypto.randomInt = original; };
}
function deferred() {
let resolve, reject;
const promise = new Promise((res, rej) => { resolve = res; reject = rej; });
return { promise, resolve, reject };
}
function assertPairingError(fn, status, message) {
assert.throws(fn, err => {
assert(err instanceof HostPairingError, `expected HostPairingError, got ${err}`);
assert.equal(err.status, status);
if (message) {
assert.equal(err.message, message);
}
return true;
});
}
describe("controller/src/HostPairingManager", function() {
describe("addressKey()", function() {
it("should return IPv4 addresses unchanged", function() {
assert.equal(addressKey("10.0.0.1"), "10.0.0.1");
});
it("should strip the IPv4 mapped prefix", function() {
assert.equal(addressKey("::ffff:10.0.0.1"), "10.0.0.1");
});
it("should group IPv6 addresses by their /64", function() {
assert.equal(addressKey("2001:db8:0:1::5"), "2001:db8:0:1::/64");
assert.equal(addressKey("2001:db8::1:ffff:0:0:3"), "2001:db8:0:1::/64");
assert.equal(addressKey("2001:0DB8:0000:0001:aaaa:bbbb:cccc:dddd"), "2001:db8:0:1::/64");
assert.equal(addressKey("2001:db8:0:2::5"), "2001:db8:0:2::/64");
});
it("should expand compressed groups at the start and end", function() {
assert.equal(addressKey("::1"), "0:0:0:0::/64");
assert.equal(addressKey("::"), "0:0:0:0::/64");
assert.equal(addressKey("fe80::1"), "fe80:0:0:0::/64");
assert.equal(addressKey("2001:db8::"), "2001:db8:0:0::/64");
});
it("should return other values unchanged", function() {
assert.equal(addressKey(""), "");
assert.equal(addressKey("not an address"), "not an address");
assert.equal(addressKey("::ffff:not-ipv4"), "::ffff:not-ipv4");
});
});
describe("normaliseCode()", function() {
it("should format codes as shown by the installer", function() {
assert.equal(normaliseCode("ABC-DEF"), "ABC-DEF");
assert.equal(normaliseCode("abcdef"), "ABC-DEF");
assert.equal(normaliseCode("abc-def"), "ABC-DEF");
assert.equal(normaliseCode(" abc def "), "ABC-DEF");
assert.equal(normaliseCode("a.b_c-d e/f"), "ABC-DEF");
});
});
describe("HostPairingManager.validateName()", function() {
it("should require a non-empty string", function() {
for (const name of [undefined, null, 5, {}, "", " "]) {
assertPairingError(() => HostPairingManager.validateName(name), 400, "Host name is required");
}
});
it("should trim the name", function() {
assert.equal(HostPairingManager.validateName(" Host A \t"), "Host A");
});
it("should limit the length of the name", function() {
const longest = "x".repeat(maxNameLength);
assert.equal(maxNameLength, 100);
assert.equal(HostPairingManager.validateName(longest), longest);
assert.equal(HostPairingManager.validateName(` ${longest} `), longest);
assertPairingError(
() => HostPairingManager.validateName(`${longest}x`),
400, "Host name must be at most 100 characters",
);
});
it("should refuse control and bidirectional override characters", function() {
for (const name of ["x\u001b]0;title\u0007", "line\nfaked", "a\rb", "del\u007f", "c1\u009b", "x\u202ey"]) {
assertPairingError(
() => HostPairingManager.validateName(name), 400, "Host name may not contain control characters",
);
}
});
it("should allow other unicode characters", function() {
for (const name of ["Fabrikk p\u00e5 \u00d8stlandet", "\u5de5\u5382 1", "box \u{1F44D}\u200d"]) {
assert.equal(HostPairingManager.validateName(name), name);
}
});
});
describe("class HostPairingManager", function() {
/** @type {Controller} */
let controller;
/** @type {HostPairingManager} */
let manager;
let updates;
let restoreRandomInt;
beforeEach(function() {
controller = makeController();
manager = controller.hostPairing;
updates = [];
manager.requests.on("update", changed => {
updates.push(...changed.map(details => ({ code: details.code, isDeleted: details.isDeleted })));
});
restoreRandomInt = undefined;
});
afterEach(function() {
manager.stop();
if (restoreRandomInt) {
restoreRandomInt();
}
});
function internal(requestId) {
return manager._bySecret.get(requestId);
}
// Pretend the installer last polled the request ms milliseconds earlier.
function idle(requestId, ms) {
internal(requestId).lastPollMs -= ms;
}
describe(".createHost()", function() {
it("should create a host record with a valid token", async function() {
const before = Math.floor(Date.now() / 1000);
const result = await manager.createHost("Host A");
assert.equal(result.name, "Host A");
assert(Number.isInteger(result.hostId), "hostId is not an integer");
assert(result.hostId >= 1 && result.hostId < 2 ** 31, `hostId ${result.hostId} out of range`);
const record = controller.hosts.get(result.hostId);
assert(record instanceof HostRecord, "host record was not created");
assert.equal(record.name, "Host A");
assert.equal(record.connected, false);
assert(record.tokenValidAfter >= before, "tokenValidAfter predates the call");
assert(record.tokenValidAfter <= Math.floor(Date.now() / 1000), "tokenValidAfter is in the future");
const payload = jwt.verify(result.token, controller.authSecret, { audience: "host" });
assert.equal(payload.host, result.hostId);
assert(payload.iat >= record.tokenValidAfter, "token is not valid for the new record");
});
it("should save the hosts before returning", async function() {
const save = deferred();
let saveCalls = 0;
controller.hosts.save = () => { saveCalls += 1; return save.promise; };
let done = false;
const promise = manager.createHost("Host A").then(result => { done = true; return result; });
await new Promise(resolve => setImmediate(resolve));
assert.equal(saveCalls, 1);
assert.equal(done, false, "createHost returned before the hosts were saved");
save.resolve();
const result = await promise;
assert(controller.hosts.has(result.hostId));
});
it("should remove the record if saving fails", async function() {
restoreRandomInt = stubRandomInt([1234]);
controller.hosts.save = async () => { throw new Error("disk full"); };
await assert.rejects(manager.createHost("Host A"), /disk full/);
assert.equal(controller.hosts.has(1234), false);
assert.equal(controller.hosts.size, 0);
});
it("should not reuse the id of an existing host", async function() {
controller.hosts.set(new HostRecord(5, "existing", "", new Map()));
restoreRandomInt = stubRandomInt([5, 5, 6]);
const result = await manager.createHost("Host A");
assert.equal(result.hostId, 6);
assert.equal(controller.hosts.get(5).name, "existing");
assert.equal(controller.hosts.get(6).name, "Host A");
});
});
describe(".create()", function() {
it("should create a pending request", function() {
const before = Date.now();
const { requestId, code } = manager.create("Host A", "2.0.0", "10.0.0.1");
assert.equal(typeof requestId, "string");
assert(requestId.length >= 32, "requestId is too short to be a secret");
assert.match(code, codePattern);
assert.notEqual(requestId, code);
const details = manager.requests.get(code);
assert(details instanceof lib.HostPairingDetails);
assert.equal(details.code, code);
assert.equal(details.name, "Host A");
assert.equal(details.version, "2.0.0");
assert.equal(details.remoteAddress, "10.0.0.1");
assert(details.createdAtMs >= before && details.createdAtMs <= Date.now());
assert(details.updatedAtMs > 0);
assert.equal(details.isDeleted, false);
assert(!JSON.stringify(details).includes(requestId), "requestId is exposed to users");
assert.deepEqual(updates, [{ code, isDeleted: false }]);
});
it("should create unique codes", function() {
const codes = new Set();
for (let i = 0; i < maxPending; i++) {
const { code } = manager.create(`Host ${i}`, "", `10.1.${i}.1`);
assert.match(code, codePattern);
codes.add(code);
}
assert.equal(codes.size, maxPending);
});
it("should not reuse the code of a pending request", function() {
restoreRandomInt = stubRandomInt([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]);
assert.equal(manager.create("Host A", "", "10.0.0.1").code, "222-222");
assert.equal(manager.create("Host B", "", "10.0.0.2").code, "333-333");
});
it("should not reuse the code of an approved request not yet picked up", async function() {
restoreRandomInt = stubRandomInt([0, 0, 0, 0, 0, 0, 1234, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]);
const { code } = manager.create("Host A", "", "10.0.0.1");
assert.equal(code, "222-222");
assert.equal(await manager.approve(code, undefined, "admin"), 1234);
assert.equal(manager.create("Host B", "", "10.0.0.2").code, "333-333");
});
it("should use distinct request ids", function() {
const first = manager.create("Host A", "", "10.0.0.1");
const second = manager.create("Host A", "", "10.0.0.1");
assert.notEqual(first.requestId, second.requestId);
assert.notEqual(first.code, second.code);
});
});
describe("limits", function() {
it("should limit pending requests per address", function() {
assert.equal(maxPendingPerAddress, 3);
for (let i = 0; i < maxPendingPerAddress; i++) {
manager.create("Host", "", "10.0.0.1");
}
assert.throws(() => manager.create("Host", "", "10.0.0.1"), err => {
assert(err instanceof HostPairingError);
assert.equal(err.status, 429);
assert.equal(err.retryAfter, 30);
assert.equal(err.message, "Too many pending requests from this address");
return true;
});
// Other addresses are not affected
manager.create("Host", "", "10.0.0.2");
assert.equal(manager.requests.size, maxPendingPerAddress + 1);
});
it("should count IPv4 mapped addresses as the IPv4 address", function() {
manager.create("Host", "", "10.0.0.1");
manager.create("Host", "", "::ffff:10.0.0.1");
manager.create("Host", "", "10.0.0.1");
assertPairingError(() => manager.create("Host", "", "::ffff:10.0.0.1"), 429);
});
it("should group IPv6 addresses by /64", function() {
manager.create("Host", "", "2001:db8:0:1::1");
manager.create("Host", "", "2001:db8:0:1::2");
manager.create("Host", "", "2001:db8::1:ffff:0:0:3");
assertPairingError(
() => manager.create("Host", "", "2001:0db8:0000:0001:0000:0000:0000:0004"),
429, "Too many pending requests from this address",
);
manager.create("Host", "", "2001:db8:0:2::1");
});
it("should free the slot when a request is no longer pending", async function() {
const first = manager.create("Host", "", "10.0.0.1");
const second = manager.create("Host", "", "10.0.0.1");
const third = manager.create("Host", "", "10.0.0.1");
assertPairingError(() => manager.create("Host", "", "10.0.0.1"), 429);
manager.cancel(first.requestId);
const fourth = manager.create("Host", "", "10.0.0.1");
assertPairingError(() => manager.create("Host", "", "10.0.0.1"), 429);
// Approved and rejected results waiting to be picked up do not count
await manager.approve(second.code, undefined, "admin");
manager.reject(third.code, "admin");
manager.create("Host", "", "10.0.0.1");
manager.create("Host", "", "10.0.0.1");
assertPairingError(() => manager.create("Host", "", "10.0.0.1"), 429);
assert(manager.requests.has(fourth.code));
});
it("should limit the total number of pending requests", function() {
assert.equal(maxPending, 50);
for (let i = 0; i < maxPending; i++) {
manager.create(`Host ${i}`, "", `10.1.${i}.1`);
}
assert.throws(() => manager.create("Host", "", "10.2.0.1"), err => {
assert(err instanceof HostPairingError);
assert.equal(err.status, 429);
assert.equal(err.retryAfter, 30);
assert.equal(err.message, "Too many pending requests");
return true;
});
assert.equal(manager.requests.size, maxPending);
});
it("should limit the number of requests created per minute from an address", function() {
assert.equal(maxCreatesPerMinute, 10);
for (let i = 0; i < maxCreatesPerMinute; i++) {
manager.cancel(manager.create("Host", "", "10.0.0.1").requestId);
}
assert.throws(() => manager.create("Host", "", "10.0.0.1"), err => {
assert(err instanceof HostPairingError);
assert.equal(err.status, 429);
assert.equal(err.message, "Too many requests from this address");
assert(err.retryAfter >= 1 && err.retryAfter <= 60, `bad retryAfter ${err.retryAfter}`);
return true;
});
// IPv6 addresses in the same /64 share the limit
for (let i = 0; i < maxCreatesPerMinute; i++) {
manager.cancel(manager.create("Host", "", `2001:db8:0:1::${i + 1}`).requestId);
}
assertPairingError(
() => manager.create("Host", "", "2001:db8:0:1:ffff::1"), 429, "Too many requests from this address"
);
manager.create("Host", "", "10.0.0.2");
});
it("should allow creating requests again after a minute", function() {
for (let i = 0; i < maxCreatesPerMinute; i++) {
manager.cancel(manager.create("Host", "", "10.0.0.1").requestId);
}
assertPairingError(() => manager.create("Host", "", "10.0.0.1"), 429);
for (const times of manager._recentCreates.values()) {
times.forEach((ms, index) => { times[index] = ms - 60e3 - 1; });
}
manager.create("Host", "", "10.0.0.1");
});
it("should not count refused requests towards the rate limit", function() {
const first = manager.create("Host", "", "10.0.0.1");
manager.create("Host", "", "10.0.0.1");
manager.create("Host", "", "10.0.0.1");
const createRefused = () => manager.create("Host", "", "10.0.0.1");
for (let i = 0; i < maxCreatesPerMinute; i++) {
assertPairingError(createRefused, 429);
}
manager.cancel(first.requestId);
manager.create("Host", "", "10.0.0.1");
});
});
describe(".poll()", function() {
it("should return null while pending", function() {
const { requestId } = manager.create("Host A", "", "10.0.0.1");
assert.equal(manager.poll(requestId), null);
assert.equal(manager.poll(requestId), null);
});
it("should throw 404 for an unknown request", function() {
assertPairingError(() => manager.poll("invalid"), 404, "Request not found");
});
it("should not accept the code in place of the request id", function() {
const { code } = manager.create("Host A", "", "10.0.0.1");
assertPairingError(() => manager.poll(code), 404);
});
it("should hand over an approved result once", async function() {
const { requestId, code } = manager.create("Host A", "", "10.0.0.1");
const hostId = await manager.approve(code, undefined, "admin");
const result = manager.poll(requestId);
assert.deepEqual(Object.keys(result).sort(), ["hostId", "name", "token"]);
assert.equal(result.hostId, hostId);
assert.equal(result.name, "Host A");
const payload = jwt.verify(result.token, controller.authSecret, { audience: "host" });
assert.equal(payload.host, hostId);
assertPairingError(() => manager.poll(requestId), 404, "Request not found");
assert.equal(manager._byCode.has(code), false);
});
it("should report a rejection once", function() {
const { requestId, code } = manager.create("Host A", "", "10.0.0.1");
manager.reject(code, "admin");
assertPairingError(() => manager.poll(requestId), 403, "Request was rejected");
assertPairingError(() => manager.poll(requestId), 404, "Request not found");
});
});
describe(".cancel()", function() {
it("should remove a pending request", function() {
const { requestId, code } = manager.create("Host A", "", "10.0.0.1");
manager.cancel(requestId);
assert.equal(manager.requests.has(code), false);
assertPairingError(() => manager.poll(requestId), 404);
assert.deepEqual(updates, [{ code, isDeleted: false }, { code, isDeleted: true }]);
assert.throws(() => manager.reject(code, "admin"), lib.NotFoundError);
});
it("should ignore unknown requests", function() {
manager.cancel("invalid");
});
it("should not throw away an approved result", async function() {
const { requestId, code } = manager.create("Host A", "", "10.0.0.1");
const hostId = await manager.approve(code, undefined, "admin");
manager.cancel(requestId);
assert.equal(manager.poll(requestId).hostId, hostId);
});
it("should not throw away a rejection", function() {
const { requestId, code } = manager.create("Host A", "", "10.0.0.1");
manager.reject(code, "admin");
manager.cancel(requestId);
assertPairingError(() => manager.poll(requestId), 403);
});
});
describe(".approve()", function() {
it("should create the host with the requested name", async function() {
const { code } = manager.create("Host A", "", "10.0.0.1");
const hostId = await manager.approve(code, undefined, "admin");
assert.equal(controller.hosts.get(hostId).name, "Host A");
assert.equal(manager.requests.has(code), false);
assert.deepEqual(updates, [{ code, isDeleted: false }, { code, isDeleted: true }]);
});
it("should accept codes in any case and without the dash", async function() {
const { code } = manager.create("Host A", "", "10.0.0.1");
const hostId = await manager.approve(code.toLowerCase().replace("-", ""), undefined, "admin");
assert(controller.hosts.has(hostId));
});
it("should use and trim the name given", async function() {
const { requestId, code } = manager.create("Host A", "", "10.0.0.1");
const hostId = await manager.approve(code, " Renamed ", "admin");
assert.equal(controller.hosts.get(hostId).name, "Renamed");
assert.equal(manager.poll(requestId).name, "Renamed");
});
it("should reject an invalid name and keep the request pending", async function() {
const { requestId, code } = manager.create("Host A", "", "10.0.0.1");
await assert.rejects(
manager.approve(code, "", "admin"),
new lib.ValidationError("Host name is required"),
);
await assert.rejects(
manager.approve(code, "x".repeat(maxNameLength + 1), "admin"),
new lib.ValidationError("Host name must be at most 100 characters"),
);
assert.equal(controller.hosts.size, 0);
assert.equal(manager.poll(requestId), null);
await manager.approve(code, undefined, "admin");
assert.equal(controller.hosts.size, 1);
});
it("should throw NotFoundError for an unknown code", async function() {
await assert.rejects(
manager.approve("ZZZ-ZZZ", undefined, "admin"),
new lib.NotFoundError("No pending host request with code ZZZ-ZZZ"),
);
});
it("should only approve a request once", async function() {
const { code } = manager.create("Host A", "", "10.0.0.1");
await manager.approve(code, undefined, "admin");
await assert.rejects(manager.approve(code, undefined, "admin"), lib.NotFoundError);
assert.equal(controller.hosts.size, 1);
});
it("should not create two hosts when approved twice at the same time", async function() {
const save = deferred();
controller.hosts.save = () => save.promise;
const { requestId, code } = manager.create("Host A", "", "10.0.0.1");
const first = manager.approve(code, undefined, "admin");
await assert.rejects(manager.approve(code, undefined, "admin"), lib.NotFoundError);
assert.throws(() => manager.reject(code, "admin"), lib.NotFoundError);
// The request stays while approving, the installer sees it as pending
manager.cancel(requestId);
idle(requestId, pollTimeoutMs + 1);
manager.prune();
assert.equal(manager.poll(requestId), null);
assert(manager.requests.has(code), "request removed while approving");
save.resolve();
const hostId = await first;
assert.equal(controller.hosts.size, 1);
assert.equal(manager.poll(requestId).hostId, hostId);
});
it("should keep the request pending if creating the host fails", async function() {
const { requestId, code } = manager.create("Host A", "", "10.0.0.1");
const originalSave = controller.hosts.save;
controller.hosts.save = async () => { throw new Error("disk full"); };
await assert.rejects(manager.approve(code, undefined, "admin"), /disk full/);
assert.equal(controller.hosts.size, 0);
assert(manager.requests.has(code));
assert.equal(manager.poll(requestId), null);
controller.hosts.save = originalSave;
const hostId = await manager.approve(code, undefined, "admin");
assert.equal(manager.poll(requestId).hostId, hostId);
});
});
describe(".reject()", function() {
it("should remove the request from the pending list", function() {
const { code } = manager.create("Host A", "", "10.0.0.1");
manager.reject(code.toLowerCase(), "admin");
assert.equal(manager.requests.has(code), false);
assert.deepEqual(updates, [{ code, isDeleted: false }, { code, isDeleted: true }]);
assert.equal(controller.hosts.size, 0);
});
it("should throw NotFoundError for an unknown code", function() {
assert.throws(
() => manager.reject("ZZZ-ZZZ", "admin"),
new lib.NotFoundError("No pending host request with code ZZZ-ZZZ"),
);
});
it("should not reject an approved request", async function() {
const { requestId, code } = manager.create("Host A", "", "10.0.0.1");
const hostId = await manager.approve(code, undefined, "admin");
assert.throws(() => manager.reject(code, "admin"), lib.NotFoundError);
assert.equal(manager.poll(requestId).hostId, hostId);
});
});
describe(".prune()", function() {
let warnings;
let originalWarn;
beforeEach(function() {
warnings = [];
originalWarn = lib.logger.warn;
lib.logger.warn = msg => { warnings.push(msg); };
});
afterEach(function() {
lib.logger.warn = originalWarn;
});
it("should expire a request the installer stopped polling", function() {
const { requestId, code } = manager.create("Host A", "", "10.0.0.1");
idle(requestId, pollTimeoutMs + 1);
manager.prune();
assert.equal(manager.requests.has(code), false);
assert.deepEqual(updates, [{ code, isDeleted: false }, { code, isDeleted: true }]);
assertPairingError(() => manager.poll(requestId), 404);
assert.throws(() => manager.reject(code, "admin"), lib.NotFoundError);
});
it("should keep a request that is still polled", function() {
assert.equal(pollTimeoutMs, 30e3);
const { requestId, code } = manager.create("Host A", "", "10.0.0.1");
idle(requestId, pollTimeoutMs - 1000);
manager.prune();
assert.equal(manager.poll(requestId), null);
idle(requestId, pollTimeoutMs - 1000);
manager.prune();
assert(manager.requests.has(code));
});
it("should expire a request that has been pending for too long", function() {
assert.equal(maxPendingMs, 60 * 60e3);
const { requestId, code } = manager.create("Host A", "", "10.0.0.1");
internal(requestId).details.createdAtMs -= maxPendingMs - 1000;
manager.prune();
assert.equal(manager.poll(requestId), null);
internal(requestId).details.createdAtMs -= 1001;
manager.prune();
assert.equal(manager.requests.has(code), false);
assertPairingError(() => manager.poll(requestId), 404);
});
it("should expire requests when creating and polling", function() {
const first = manager.create("Host A", "", "10.0.0.1");
idle(first.requestId, pollTimeoutMs + 1);
const second = manager.create("Host B", "", "10.0.0.1");
assert.equal(manager.requests.has(first.code), false);
idle(second.requestId, pollTimeoutMs + 1);
assertPairingError(() => manager.poll(second.requestId), 404);
});
it("should drop and warn about an approved host never picked up", async function() {
const { requestId, code } = manager.create("Host A", "", "10.0.0.1");
const hostId = await manager.approve(code, undefined, "admin");
idle(requestId, pollTimeoutMs + 1);
manager.prune();
assertPairingError(() => manager.poll(requestId), 404);
assert.equal(warnings.length, 1);
assert.match(warnings[0], new RegExp(`Approved host Host A \\(${hostId}\\) was never picked up`));
// The host stays until revoked
assert(controller.hosts.has(hostId));
});
it("should drop a rejection never picked up", function() {
const { requestId, code } = manager.create("Host A", "", "10.0.0.1");
manager.reject(code, "admin");
idle(requestId, pollTimeoutMs + 1);
manager.prune();
assertPairingError(() => manager.poll(requestId), 404);
assert.equal(warnings.length, 0);
});
it("should forget creation times older than a minute", function() {
manager.create("Host A", "", "10.0.0.1");
assert.equal(manager._recentCreates.size, 1);
manager.prune();
assert.equal(manager._recentCreates.size, 1);
for (const times of manager._recentCreates.values()) {
times.forEach((ms, index) => { times[index] = ms - 60e3 - 1; });
}
manager.prune();
assert.equal(manager._recentCreates.size, 0);
});
});
describe(".start()/.stop()", function() {
it("should run an unreferenced prune interval while started", function() {
manager.start();
assert(manager._pruneInterval, "prune interval not started");
assert.equal(manager._pruneInterval.hasRef(), false);
manager.stop();
assert.equal(manager._pruneInterval, undefined);
});
});
describe("Controller integration", function() {
it("should broadcast changes to pending requests", function() {
const broadcasts = [];
controller.subscriptions.broadcast = event => {
if (event instanceof lib.HostPairingUpdatesEvent) {
broadcasts.push(event.updates.map(details => [details.code, details.isDeleted]));
}
};
const { code } = manager.create("Host A", "", "10.0.0.1");
manager.reject(code, "admin");
assert.deepEqual(broadcasts, [[[code, false]], [[code, true]]]);
});
it("should send pending requests to new subscribers", async function() {
const { code } = manager.create("Host A", "", "10.0.0.1");
const event = await controller.handleHostPairingSubscription(
new lib.SubscriptionRequest(lib.HostPairingUpdatesEvent.name, true, 0)
);
assert(event instanceof lib.HostPairingUpdatesEvent);
assert.deepEqual(event.updates.map(details => details.code), [code]);
const lastUpdate = manager.requests.get(code).updatedAtMs;
assert.equal(await controller.handleHostPairingSubscription(
new lib.SubscriptionRequest(lib.HostPairingUpdatesEvent.name, true, lastUpdate)
), null);
});
});
});
});
+142 -1
View File
@@ -1,7 +1,32 @@
import assert from "node:assert/strict";
import events from "node:events";
import { Controller, WsServer } from "@clusterio/controller";
import jwt from "jsonwebtoken";
import * as lib from "@clusterio/lib";
import { Controller, HostRecord, WsServer } from "@clusterio/controller";
import { ControllerConfig } from "@clusterio/lib";
import packageJson from "@clusterio/controller/package.json" with { type: "json" };
import "../setup_logging.js";
class MockWebSocket extends events.EventEmitter {
constructor() {
super();
this.sent = [];
this.closed = null;
}
send(message) {
this.sent.push(JSON.parse(message));
}
close(code, reason) {
this.closed = { code, reason };
}
terminate() {
this.emit("close", 1006, "");
}
}
describe("controller/src/WsServer", function() {
describe("class WsServer", function() {
@@ -88,4 +113,120 @@ describe("controller/src/WsServer", function() {
});
});
});
describe(".registerHost()", function() {
const secret = Buffer.from("TestSecretDoNotUse");
const req = { socket: { remoteAddress: "10.0.0.1", remoteFamily: "IPv4" }, headers: {} };
/** @type {Controller} */
let controller;
/** @type {WsServer} */
let wsServer;
let socket;
beforeEach(function() {
const config = new ControllerConfig("controller");
config.set("controller.auth_secret", secret.toString("base64"));
controller = new Controller(lib.logger, [], config);
wsServer = controller.wsServer;
socket = new MockWebSocket();
});
afterEach(function() {
for (const connection of wsServer.hostConnections.values()) {
connection.connector.terminate();
}
});
function addHost(id, tokenValidAfter = 0) {
const record = new HostRecord(id, `host-${id}`, "", new Map(), false, "", "203.0.113.1", tokenValidAfter);
controller.hosts.set(record);
return record;
}
async function register(token, id = 5) {
await wsServer.registerHost(new lib.RegisterHostData(token, packageJson.version, id, {}), socket, req);
}
function assertRejected(reason) {
assert.deepEqual(socket.closed, { code: lib.ConnectionClosed.Unauthorized, reason });
assert.equal(socket.closed.code, 3000);
assert.equal(wsServer.hostConnections.size, 0);
assert(!socket.sent.some(message => message.type === "ready"), "ready was sent");
}
it("should reject a validly signed token for a host that is not registered", async function() {
await register(controller.generateHostToken(5));
assertRejected(
"Authentication failed: host is not registered, run the installer again in the host directory"
);
assert.equal(controller.hosts.has(5), false, "host record was created");
});
it("should accept a registered host", async function() {
const record = addHost(5);
await register(controller.generateHostToken(5));
assert.equal(socket.closed, null);
assert(socket.sent.some(message => message.type === "ready"), "ready was not sent");
assert(wsServer.hostConnections.has(5));
assert.equal(controller.hosts.size, 1);
assert.equal(controller.hosts.get(5), record, "host record was replaced");
assert.equal(record.connected, true);
assert.equal(record.version, packageJson.version);
assert.equal(record.remoteAddress, "10.0.0.1");
assert.equal(record.name, "host-5");
assert.equal(record.publicAddress, "203.0.113.1");
wsServer.hostConnections.get(5).connector.terminate();
assert.equal(wsServer.hostConnections.has(5), false);
assert.equal(controller.hosts.get(5).connected, false);
});
it("should reject a token issued before tokenValidAfter", async function() {
const token = controller.generateHostToken(5);
addHost(5, Math.floor(Date.now() / 1000) + 60);
await register(token);
assertRejected("Authentication failed: invalid token");
});
it("should accept a token issued at tokenValidAfter", async function() {
const now = Math.floor(Date.now() / 1000);
addHost(5, now);
await register(jwt.sign({ aud: "host", host: 5, iat: now }, secret));
assert.equal(socket.closed, null);
assert(wsServer.hostConnections.has(5));
});
it("should reject a token without an issue time", async function() {
addHost(5);
await register(jwt.sign({ aud: "host", host: 5 }, secret, { noTimestamp: true }));
assertRejected("Authentication failed: invalid token");
});
it("should reject a token for another host", async function() {
addHost(5);
addHost(6);
await register(controller.generateHostToken(6), 5);
assertRejected("Authentication failed: missmatched host id");
});
it("should reject a token signed with another secret", async function() {
addHost(5);
await register(jwt.sign({ aud: "host", host: 5 }, Buffer.from("OtherSecret")));
assertRejected("Authentication failed: invalid signature");
});
it("should reject a host revoked while its previous connection closes", async function() {
const record = addHost(5);
wsServer.hostConnections.set(5, {
remoteAddress: "10.0.0.1",
async disconnect() {
controller.hosts.delete(record);
wsServer.hostConnections.delete(5);
},
});
await register(controller.generateHostToken(5));
assertRejected("Authentication failed: host was revoked");
assert.equal(controller.hosts.has(5), false, "host record was recreated");
});
it("should reject hosts in recovery mode", async function() {
addHost(5);
controller.recoveryMode = true;
await register(controller.generateHostToken(5));
assert.deepEqual(socket.closed, {
code: lib.ConnectionClosed.RecoveryMode,
reason: "Connection Refused: Controller in recovery mode",
});
});
});
});
+324 -1
View File
@@ -3,7 +3,10 @@ import events from "node:events";
import FormData from "form-data";
import http from "node:http";
import { wait, expositionContentType } from "@clusterio/lib";
import jwt from "jsonwebtoken";
import { SubscribableDatastore, wait, expositionContentType } from "@clusterio/lib";
import { Controller, HostPairingManager, HostRecord } from "@clusterio/controller";
import * as routes from "@clusterio/controller/dist/node/src/routes.js";
import * as mock from "../mock.js";
@@ -211,6 +214,326 @@ describe("controller/src/routes", function() {
assert.deepEqual(data, { name: "Test Cluster" });
});
});
describe("/api/host-pairing", function() {
let endpoint, adminToken, playerToken;
beforeEach(function() {
endpoint = `http://localhost:${port}/api/host-pairing`;
// The mock controller lacks what pairing needs, use the real implementations.
controller.hosts = new SubscribableDatastore();
controller.hostPairing = new HostPairingManager(controller);
controller.generateHostToken = Controller.prototype.generateHostToken;
controller.wsServer.remoteAddr = req => req.socket.remoteAddress;
adminToken = controller.users.signUserToken(controller.users.getByName("test"));
playerToken = controller.users.signUserToken(controller.users.getByName("player"));
});
function post(path, body, headers = {}) {
return fetch(`${endpoint}${path}`, {
method: "POST",
headers: { "Content-Type": "application/json", ...headers },
body: typeof body === "string" || body === undefined ? body : JSON.stringify(body),
});
}
async function assertResponse(response, status, body) {
assert.equal(response.status, status);
assert.equal(response.headers.get("cache-control"), "no-store");
if (body !== undefined) {
assert.deepEqual(await response.json(), body);
}
}
async function createRequest(name = "Host A") {
const response = await post("", { name });
assert.equal(response.status, 202);
return await response.json();
}
function check(headers) {
return fetch(`${endpoint}/check`, { headers });
}
function assertNothingCreated() {
assert.equal(controller.hostPairing.requests.size, 0, "a pending request was created");
assert.equal(controller.hosts.size, 0, "a host was created");
}
describe("POST /api/host-pairing", function() {
it("should create a pending request", async function() {
const response = await post("", { name: "Host A", version: "2.0.0" });
assert.equal(response.status, 202);
assert.equal(response.headers.get("cache-control"), "no-store");
const body = await response.json();
assert.deepEqual(Object.keys(body).sort(), ["code", "requestId"]);
assert.match(body.code, /^[2-9A-HJKMNP-Z]{3}-[2-9A-HJKMNP-Z]{3}$/);
const details = controller.hostPairing.requests.get(body.code);
assert.equal(details.name, "Host A");
assert.equal(details.version, "2.0.0");
assert.match(details.remoteAddress, /127\.0\.0\.1|::1/);
assert.equal(controller.hosts.size, 0);
});
it("should trim the name and limit the version", async function() {
let { code } = await (await post("", { name: " Host A ", version: "x".repeat(100) })).json();
let details = controller.hostPairing.requests.get(code);
assert.equal(details.name, "Host A");
assert.equal(details.version, "x".repeat(64));
({ code } = await (await post("", { name: "Host B", version: 5 })).json());
details = controller.hostPairing.requests.get(code);
assert.equal(details.version, "");
});
it("should respond with 400 for a missing or invalid name", async function() {
const required = { error: "Host name is required" };
await assertResponse(await post("", {}), 400, required);
await assertResponse(await post("", { name: "" }), 400, required);
await assertResponse(await post("", { name: " " }), 400, required);
await assertResponse(await post("", { name: 5 }), 400, required);
await assertResponse(await post("", undefined), 400, required);
await assertResponse(await post("", "name=Host", { "Content-Type": "text/plain" }), 400, required);
await assertResponse(
await post("", { name: "x".repeat(101) }),
400, { error: "Host name must be at most 100 characters" },
);
assertNothingCreated();
});
it("should respond with 400 for malformed JSON", async function() {
controller.app.set("env", "test"); // Silence the default error handler
const response = await post("", "{\"name\":");
assert.equal(response.status, 400);
assertNothingCreated();
});
it("should respond with 413 for a body over 4kb", async function() {
controller.app.set("env", "test"); // Silence the default error handler
const response = await post("", { name: "x".repeat(5000) });
assert.equal(response.status, 413);
assertNothingCreated();
});
it("should respond with 429 and Retry-After when limited", async function() {
for (let i = 0; i < 3; i++) {
await createRequest();
}
const response = await post("", { name: "Host A" });
assert.equal(response.headers.get("retry-after"), "30");
await assertResponse(response, 429, { error: "Too many pending requests from this address" });
assert.equal(controller.hostPairing.requests.size, 3);
});
it("should respond with 503 in recovery mode", async function() {
controller.recoveryMode = true;
const recovery = { error: "Controller is in recovery mode" };
await assertResponse(await post("", { name: "Host A" }), 503, recovery);
await assertResponse(
await post("", { name: "Host A" }, { "Authorization": `Bearer ${adminToken}` }), 503, recovery
);
assertNothingCreated();
});
it("should approve directly with the token of a user allowed to approve hosts", async function() {
for (const headers of [
{ "Authorization": `Bearer ${adminToken}` },
{ "Authorization": `bearer ${adminToken}` },
{ "X-Access-Token": adminToken },
]) {
const response = await post("", { name: "Host A" }, headers);
assert.equal(response.status, 200);
assert.equal(response.headers.get("cache-control"), "no-store");
const body = await response.json();
assert.deepEqual(Object.keys(body).sort(), ["hostId", "name", "token"]);
assert.equal(body.name, "Host A");
assert.equal(controller.hosts.get(body.hostId).name, "Host A");
const payload = jwt.verify(body.token, controller.authSecret, { audience: "host" });
assert.equal(payload.host, body.hostId);
}
assert.equal(controller.hosts.size, 3);
assert.equal(controller.hostPairing.requests.size, 0, "a pending request was created");
});
it("should validate the name before approving directly", async function() {
await assertResponse(
await post("", {}, { "Authorization": `Bearer ${adminToken}` }),
400, { error: "Host name is required" },
);
assertNothingCreated();
});
it("should respond with 403 for a user not allowed to approve hosts", async function() {
await assertResponse(
await post("", { name: "Host A" }, { "Authorization": `Bearer ${playerToken}` }),
403, { error: "User player is not allowed to approve hosts" },
);
assertNothingCreated();
});
it("should respond with 401 for an invalid token without creating a request", async function() {
// The admin user's tokens are revoked after signing this one
const revokedToken = adminToken;
const user = controller.users.getOrCreateUser("test");
user.tokenValidAfter = Math.floor((Date.now() + 60e3) / 1000);
user.saveRecord();
controller.hosts.set(new HostRecord(5, "five", "", new Map()));
const hostToken = controller.generateHostToken(5);
controller.hosts.delete(controller.hosts.get(5));
for (const headers of [
{ "Authorization": "Bearer invalid" },
{ "X-Access-Token": "invalid" },
{ "Authorization": `Bearer ${controller.users.signUserToken({ id: "nobody" })}` },
{ "Authorization": `Bearer ${revokedToken}` },
{ "Authorization": `Bearer ${hostToken}` },
]) {
await assertResponse(
await post("", { name: "Host A" }, headers), 401, { error: "Invalid user token" }
);
}
assertNothingCreated();
});
});
describe("POST /api/host-pairing/poll", function() {
it("should respond with 202 while pending", async function() {
const { requestId } = await createRequest();
await assertResponse(await post("/poll", { requestId }), 202, { status: "pending" });
await assertResponse(await post("/poll", { requestId }), 202, { status: "pending" });
});
it("should hand over the result of an approved request once", async function() {
const { requestId, code } = await createRequest();
const hostId = await controller.hostPairing.approve(code, "Renamed", "test");
const response = await post("/poll", { requestId });
assert.equal(response.status, 200);
assert.equal(response.headers.get("cache-control"), "no-store");
const body = await response.json();
assert.deepEqual(Object.keys(body).sort(), ["hostId", "name", "token"]);
assert.equal(body.hostId, hostId);
assert.equal(body.name, "Renamed");
assert.equal(jwt.verify(body.token, controller.authSecret, { audience: "host" }).host, hostId);
await assertResponse(await post("/poll", { requestId }), 404, { error: "Request not found" });
});
it("should respond with 403 once for a rejected request", async function() {
const { requestId, code } = await createRequest();
controller.hostPairing.reject(code, "test");
await assertResponse(await post("/poll", { requestId }), 403, { error: "Request was rejected" });
await assertResponse(await post("/poll", { requestId }), 404, { error: "Request not found" });
});
it("should respond with 404 for an unknown request", async function() {
const { code } = await createRequest();
const notFound = { error: "Request not found" };
await assertResponse(await post("/poll", { requestId: "invalid" }), 404, notFound);
await assertResponse(await post("/poll", { requestId: code }), 404, notFound);
});
it("should respond with 400 without a requestId in the body", async function() {
const { requestId } = await createRequest();
const required = { error: "requestId is required" };
await assertResponse(await post("/poll", {}), 400, required);
await assertResponse(await post("/poll", { requestId: 5 }), 400, required);
await assertResponse(await post(`/poll?requestId=${encodeURIComponent(requestId)}`, {}), 400, required);
});
it("should respond with 503 in recovery mode", async function() {
const { requestId } = await createRequest();
controller.recoveryMode = true;
await assertResponse(
await post("/poll", { requestId }), 503, { error: "Controller is in recovery mode" }
);
});
});
describe("POST /api/host-pairing/cancel", function() {
it("should cancel a pending request", async function() {
const { requestId, code } = await createRequest();
await assertResponse(await post("/cancel", { requestId }), 204);
assert.equal(controller.hostPairing.requests.has(code), false);
await assertResponse(await post("/poll", { requestId }), 404, { error: "Request not found" });
});
it("should respond with 204 for an unknown request", async function() {
const { code } = await createRequest();
await assertResponse(await post("/cancel", { requestId: "invalid" }), 204);
await assertResponse(await post("/cancel", { requestId: code }), 204);
assert(controller.hostPairing.requests.has(code), "cancelled by the code");
});
it("should respond with 400 without a requestId in the body", async function() {
const { requestId, code } = await createRequest();
await assertResponse(await post("/cancel", {}), 400, { error: "requestId is required" });
await assertResponse(
await post(`/cancel?requestId=${encodeURIComponent(requestId)}`, {}),
400, { error: "requestId is required" },
);
assert(controller.hostPairing.requests.has(code));
});
it("should respond with 503 in recovery mode", async function() {
const { requestId, code } = await createRequest();
controller.recoveryMode = true;
await assertResponse(
await post("/cancel", { requestId }), 503, { error: "Controller is in recovery mode" }
);
assert(controller.hostPairing.requests.has(code));
});
});
describe("request ids", function() {
it("should not be accepted in the URL", async function() {
const { requestId } = await createRequest();
const id = encodeURIComponent(requestId);
for (const [method, path] of [
["GET", `/poll?requestId=${id}`],
["GET", `/poll/${id}`],
["POST", `/poll/${id}`],
["POST", `/${id}`],
["GET", `/${id}`],
["POST", `/cancel/${id}`],
]) {
const response = await fetch(`${endpoint}${path}`, { method });
assert.equal(response.status, 404, `${method} ${path}`);
}
await assertResponse(await post("/poll", { requestId }), 202, { status: "pending" });
});
});
describe("GET /api/host-pairing/check", function() {
it("should respond with 200 for a valid host token", async function() {
const { hostId, token } = await controller.hostPairing.createHost("Host A");
for (const headers of [
{ "Authorization": `Bearer ${token}` },
{ "X-Access-Token": token },
]) {
await assertResponse(await check(headers), 200, { valid: true });
}
assert(controller.hosts.has(hostId));
});
it("should respond with 401 without a valid host token", async function() {
await controller.hostPairing.createHost("Host A");
assert.equal((await check({})).status, 401);
assert.equal((await check({ "Authorization": "Bearer invalid" })).status, 401);
assert.equal((await check({ "Authorization": `Bearer ${adminToken}` })).status, 401);
});
it("should respond with 401 for a host that is not registered", async function() {
const token = controller.generateHostToken(99);
assert.equal((await check({ "Authorization": `Bearer ${token}` })).status, 401);
});
it("should respond with 401 once the host is revoked", async function() {
const { hostId, token } = await controller.hostPairing.createHost("Host A");
await assertResponse(await check({ "Authorization": `Bearer ${token}` }), 200, { valid: true });
controller.hosts.delete(controller.hosts.get(hostId));
assert.equal((await check({ "Authorization": `Bearer ${token}` })).status, 401);
});
it("should respond with 401 for a token issued before tokenValidAfter", async function() {
const { hostId, token } = await controller.hostPairing.createHost("Host A");
const record = controller.hosts.getMutable(hostId);
record.tokenValidAfter = Math.floor((Date.now() + 60e3) / 1000);
controller.hosts.set(record);
assert.equal((await check({ "Authorization": `Bearer ${token}` })).status, 401);
});
});
describe("pairing flow", function() {
it("should pair a host approved while the installer polls", async function() {
const { requestId, code } = await createRequest("Host A");
await assertResponse(await post("/poll", { requestId }), 202, { status: "pending" });
const hostId = await controller.hostPairing.approve(code.toLowerCase(), undefined, "test");
const result = await (await post("/poll", { requestId })).json();
assert.deepEqual(result, { hostId, token: result.token, name: "Host A" });
await assertResponse(
await check({ "Authorization": `Bearer ${result.token}` }), 200, { valid: true }
);
});
});
});
describe("/api/upload-save", function() {
let endpoint, testToken;
beforeEach(function() {
+331
View File
@@ -1,9 +1,15 @@
import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import crypto from "node:crypto";
import events from "node:events";
import fs from "node:fs/promises";
import http from "node:http";
import os from "node:os";
import path from "node:path";
import { promisify } from "node:util";
import jwt from "jsonwebtoken";
import { escapeArg } from "../packages/create/escape_arg.js";
const execFileAsync = promisify(execFile);
const installer = path.resolve(import.meta.dirname, "../packages/create/create.js");
@@ -123,4 +129,329 @@ describe("create installer CLI", function() {
return true;
});
});
describe("host setup", function() {
const scriptExt = process.platform === "win32" ? ".cmd" : "";
const hostBin = path.join("node_modules", ".bin", `clusteriohost${scriptExt}`);
const controllerBin = path.join("node_modules", ".bin", `clusteriocontroller${scriptExt}`);
const hostSet = (field, value) => `${hostBin} config set ${field} ${escapeArg(String(value))}`;
const approved = { hostId: 1234, token: "host-token", name: "Approved" };
let server;
let controllerUrl;
let requests;
let handler;
beforeEach(async function() {
requests = [];
handler = () => [500, { error: "No handler" }];
server = http.createServer(async (req, res) => {
let body = "";
for await (const chunk of req) {
body += chunk;
}
const entry = { url: req.url, headers: req.headers, body: body ? JSON.parse(body) : undefined };
requests.push(entry);
const [status, json] = handler(entry);
res.writeHead(status, { "Content-Type": "application/json" });
res.end(json === undefined ? undefined : JSON.stringify(json));
});
server.listen(0, "localhost");
await events.once(server, "listening");
controllerUrl = `http://localhost:${server.address().port}/`;
});
afterEach(function() {
server.closeAllConnections();
server.close();
});
// Stub of the controller pairing endpoints, approving after the given number of pending polls.
function pairingHandler(pendingPolls = 0, result = approved) {
let polls = 0;
return ({ url }) => {
if (url === "/api/host-pairing") {
return [202, { requestId: "secret", code: "ABC-DEF" }];
}
if (url === "/api/host-pairing/poll") {
polls += 1;
return polls > pendingPolls ? [200, result] : [202, { status: "pending" }];
}
if (url === "/api/host-pairing/cancel") {
return [204, undefined];
}
return [404, { error: "Not found" }];
};
}
function run(args, env = {}) {
// Record the commands for npm and the clusterio CLIs instead of running them.
const mockExec = `
import childProcess from "node:child_process";
import fs from "node:fs";
import { PassThrough } from "node:stream";
childProcess.execFile = (command, options, callback) => {
fs.appendFileSync(process.env.CREATE_TEST_COMMANDS, JSON.stringify(command) + "\\n");
const stdout = new PassThrough();
const stderr = new PassThrough();
// Lets tests give the host id a config show prints.
const output = process.env.CREATE_TEST_HOST_ID && command.includes("config show host.id")
? process.env.CREATE_TEST_HOST_ID + "\\n" : "";
queueMicrotask(() => {
stdout.end();
stderr.end();
callback(null, output, "");
});
return { stdout, stderr };
};
if (process.env.CREATE_TEST_SIGINT) {
// Stands in for Ctrl-C, which cannot be sent to a child process on Windows.
// Sent twice like when npm init forwards the one it gets to the installer.
const write = process.stdout.write.bind(process.stdout);
process.stdout.write = (chunk, ...rest) => {
if (String(chunk).includes("Waiting for approval")) {
setTimeout(() => {
process.emit("SIGINT", "SIGINT");
process.emit("SIGINT", "SIGINT");
}, 50);
}
return write(chunk, ...rest);
};
}
`;
return execFileAsync(process.execPath, [
"--import", `data:text/javascript,${encodeURIComponent(mockExec)}`,
installer,
...(process.platform === "linux" ? ["--allow-install-as-root"] : []),
...args,
], {
cwd: directory,
env: { ...process.env, CREATE_TEST_COMMANDS: path.join(directory, "commands.jsonl"), ...env },
});
}
// Commands run by the installer, without the getent lookups made when writing systemd units.
async function commands() {
let content;
try {
content = await fs.readFile(path.join(directory, "commands.jsonl"), "utf8");
} catch (err) {
if (err.code === "ENOENT") {
return [];
}
throw err;
}
return content.split("\n").filter(Boolean).map(line => JSON.parse(line))
.filter(command => !command.startsWith("getent "));
}
function hostArgs(...extra) {
return [
"--mode", "host", "--host-name", "Test Host", "--controller-url", controllerUrl,
"--public-address", "host.example.com", "--factorio-dir", "factorio", "--no-remote-npm",
...extra, "--plugins",
];
}
async function writeHostConfig(token) {
await fs.writeFile(path.join(directory, "config-host.json"), JSON.stringify({
"host.name": "Test Host",
"host.controller_url": controllerUrl,
"host.controller_token": token,
}));
}
it("pairs a new host once approved and saves its id and token", async function() {
handler = pairingHandler(0);
const { stdout } = await run(hostArgs());
assert(stdout.includes(
`Waiting for approval. Approve "Test Host" with code ABC-DEF on the Hosts page of ${controllerUrl}, ` +
"or run: npx clusterioctl host approve ABC-DEF"
), stdout);
assert.match(stdout, /Successfully installed host/);
const { version } = JSON.parse(await fs.readFile(path.join(path.dirname(installer), "package.json")));
assert.deepEqual(requests[0].body, { name: "Test Host", version });
assert.equal(requests[0].headers.authorization, undefined);
const recorded = await commands();
assert.match(recorded[0], /^npm(?:\.cmd)? install @clusterio\/host$/);
assert.deepEqual(recorded.slice(1), [
`${hostBin} config create`,
hostSet("host.public_address", "host.example.com"),
hostSet("host.factorio_directory", "factorio"),
hostSet("host.allow_remote_updates", false),
hostSet("host.allow_plugin_updates", false),
hostSet("host.allow_plugin_install", false),
hostSet("host.name", "Test Host"),
hostSet("host.controller_url", controllerUrl),
hostSet("host.id", 1234),
hostSet("host.name", "Approved"),
hostSet("host.controller_token", "host-token"),
]);
await fs.access(path.join(directory, `run-host${process.platform === "win32" ? ".cmd" : ".sh"}`));
});
it("uses --controller-token to approve the host without saving it", async function() {
handler = ({ headers }) => (headers.authorization === "Bearer user-token"
? [200, { hostId: 99, token: "host-token", name: "Test Host" }]
: [401, { error: "Invalid user token" }]
);
const { stdout } = await run(hostArgs("--controller-token", "user-token"));
assert.doesNotMatch(stdout, /Waiting for approval/);
const recorded = await commands();
assert(!recorded.some(command => command.includes("user-token")), "user token must not be saved");
assert.deepEqual(recorded.slice(-2), [
hostSet("host.id", 99),
hostSet("host.controller_token", "host-token"),
]);
});
it("cancels the request on Ctrl-C", async function() {
handler = pairingHandler(Infinity);
await assert.rejects(run(hostArgs(), { CREATE_TEST_SIGINT: "1" }), err => {
assert.equal(err.code, 1);
assert.match(err.stdout + err.stderr, /Pairing cancelled/);
return true;
});
assert.deepEqual(requests.find(r => r.url === "/api/host-pairing/cancel")?.body, { requestId: "secret" });
assert(!(await commands()).some(command => command.includes("host.controller_token")));
});
it("installs without contacting the controller with --no-pair", async function() {
const { stdout } = await run([
"--mode", "host", "--no-pair", "--public-address", "localhost", "--factorio-dir", "factorio",
"--remote-npm", "--plugins",
]);
assert.match(stdout, /Successfully installed host/);
assert.equal(requests.length, 0);
const recorded = await commands();
assert(recorded.includes(hostSet("host.allow_plugin_install", true)));
assert(!recorded.some(command => command.includes("host.controller_token")));
});
it("exits when run again on a host the controller accepts", async function() {
await writeHostConfig("valid-token");
handler = ({ url, headers }) => {
const valid = url === "/api/host-pairing/check" && headers.authorization === "Bearer valid-token";
return valid ? [200, { valid: true }] : [401, undefined];
};
const { stdout } = await run(["--mode", "host"]);
assert.match(stdout, /Host is already set up/);
assert.deepEqual(requests.map(r => r.url), ["/api/host-pairing/check"]);
assert.deepEqual(await commands(), []);
});
it("pairs again when run again on a revoked host", async function() {
await writeHostConfig("revoked-token");
const pairing = pairingHandler(0, { hostId: 5, token: "new-token", name: "Renamed" });
handler = entry => (entry.url === "/api/host-pairing/check" ? [401, undefined] : pairing(entry));
const { stdout } = await run([
"--mode", "host", "--host-name", "Renamed", "--controller-url", controllerUrl,
]);
assert.match(stdout, /Successfully set up host/);
assert.equal(requests[0].headers.authorization, "Bearer revoked-token");
assert.equal(requests[1].body.name, "Renamed");
assert.deepEqual(await commands(), [
hostSet("host.name", "Renamed"),
hostSet("host.controller_url", controllerUrl),
hostSet("host.id", 5),
hostSet("host.controller_token", "new-token"),
]);
});
it("does not send the saved token to another controller", async function() {
await writeHostConfig("old-token");
// Same stub server under its IP address, which the installer sees as another controller.
const { address, port } = server.address();
const otherUrl = `http://${address.includes(":") ? `[${address}]` : address}:${port}/`;
handler = pairingHandler(0, { hostId: 6, token: "other-token", name: "Moved" });
const { stdout } = await run(["--mode", "host", "--host-name", "Moved", "--controller-url", otherUrl]);
assert.match(stdout, /Successfully set up host/);
assert(!requests.some(r => r.url === "/api/host-pairing/check"), "token checked with other controller");
assert(!requests.some(r => String(r.headers.authorization).includes("old-token")));
assert.deepEqual(await commands(), [
hostSet("host.name", "Moved"),
hostSet("host.controller_url", otherUrl),
hostSet("host.id", 6),
hostSet("host.controller_token", "other-token"),
]);
});
it("adds the trailing slash the host requires to the controller URL", async function() {
handler = pairingHandler(0);
const args = hostArgs();
args[args.indexOf(controllerUrl)] = controllerUrl.slice(0, -1);
await run(args);
assert((await commands()).includes(hostSet("host.controller_url", controllerUrl)));
});
it("writes the certificate into the run scripts when run again with one", async function() {
await writeHostConfig("valid-token");
handler = ({ url }) => (url === "/api/host-pairing/check" ? [200, { valid: true }] : [404, undefined]);
const cert = path.resolve(import.meta.dirname, "file", "tls", "cert.pem");
const { stdout } = await run(["--mode", "host", "--self-signed-cert", cert]);
assert.match(stdout, /Host is already set up/);
const script = process.platform === "win32" ? "run-host.cmd" : "run-host.sh";
const content = await fs.readFile(path.join(directory, script), "utf8");
assert(content.includes(`NODE_EXTRA_CA_CERTS=${cert}`), content);
});
it("adds the host of a standalone install to the controller database", async function() {
const secret = crypto.randomBytes(256).toString("base64");
await fs.writeFile(path.join(directory, "config-controller.json"), JSON.stringify({
"controller.auth_secret": secret,
}));
const { stdout } = await run([
"--mode", "standalone", "--admin", "admin", "--http-port", "8123", "--public-address", "localhost",
"--factorio-dir", "factorio", "--remote-npm", "--plugins",
]);
assert.match(stdout, /Successfully installed standalone/);
const hosts = JSON.parse(await fs.readFile(path.join(directory, "database", "hosts.json")));
assert.equal(hosts.length, 1);
assert.equal(hosts[0].name, "local");
const recorded = await commands();
const tokenSet = `${hostBin} config set host.controller_token `;
const token = recorded.find(command => command.startsWith(tokenSet)).slice(tokenSet.length);
const payload = jwt.verify(token, Buffer.from(secret, "base64"), { audience: "host" });
assert.equal(payload.host, hosts[0].id);
for (const expected of [
hostSet("host.id", hosts[0].id),
hostSet("host.name", "local"),
hostSet("host.controller_url", "http://localhost:8123/"),
`${controllerBin} config set controller.allow_remote_updates true`,
`${controllerBin} config set controller.allow_plugin_updates true`,
`${controllerBin} config set controller.allow_plugin_install true`,
hostSet("host.allow_remote_updates", true),
hostSet("host.allow_plugin_updates", true),
hostSet("host.allow_plugin_install", true),
]) {
assert(recorded.includes(expected), `missing ${expected}`);
}
assert(!recorded.some(command => command.includes("generate-host-token")));
});
it("keeps the host of a standalone install when run again", async function() {
const secret = crypto.randomBytes(256).toString("base64");
await fs.writeFile(path.join(directory, "config-controller.json"), JSON.stringify({
"controller.auth_secret": secret,
}));
await fs.mkdir(path.join(directory, "database"));
const existing = [{ id: 1234, name: "local", version: "2.0.3", plugins: {}, token_valid_after: 1 }];
await fs.writeFile(path.join(directory, "database", "hosts.json"), JSON.stringify(existing));
const { stdout } = await run([
"--mode", "standalone", "--admin", "admin", "--http-port", "8123", "--public-address", "localhost",
"--factorio-dir", "factorio", "--remote-npm", "--plugins",
], { CREATE_TEST_HOST_ID: "1234" });
assert.match(stdout, /Successfully installed standalone/);
assert.deepEqual(JSON.parse(await fs.readFile(path.join(directory, "database", "hosts.json"))), existing);
const recorded = await commands();
assert(recorded.includes(hostSet("host.id", 1234)), "host id was changed");
const tokenSet = `${hostBin} config set host.controller_token `;
const token = recorded.find(command => command.startsWith(tokenSet)).slice(tokenSet.length);
assert.equal(jwt.verify(token, Buffer.from(secret, "base64"), { audience: "host" }).host, 1234);
});
});
});
+343
View File
@@ -0,0 +1,343 @@
import assert from "node:assert/strict";
import crypto from "node:crypto";
import events from "node:events";
import fs from "node:fs/promises";
import http from "node:http";
import https from "node:https";
import os from "node:os";
import path from "node:path";
import tls from "node:tls";
import jwt from "jsonwebtoken";
import * as lib from "@clusterio/lib";
import HostRecord from "@clusterio/controller/dist/node/src/HostRecord.js";
import {
PairingError, checkHostToken, createLocalHost, pairHost,
} from "../../packages/create/pair.js";
import "../setup_logging.js";
const tlsDir = path.join(import.meta.dirname, "..", "file", "tls");
// Stub controller recording requests, responding with what the test's handler returns.
async function startStub(createServer = http.createServer) {
const stub = {
requests: [],
handler: () => [500, { error: "No handler" }],
};
stub.server = createServer(async (req, res) => {
const chunks = [];
for await (const chunk of req) {
chunks.push(chunk);
}
const entry = {
method: req.method,
url: req.url,
headers: req.headers,
body: chunks.length ? JSON.parse(Buffer.concat(chunks)) : undefined,
};
stub.requests.push(entry);
const [status, json, headers = {}] = await stub.handler(entry);
res.writeHead(status, { "Content-Type": "application/json", ...headers });
res.end(json === undefined ? undefined : JSON.stringify(json));
});
stub.server.listen(0, "localhost");
await events.once(stub.server, "listening");
stub.url = `${stub.server instanceof https.Server ? "https" : "http"}://localhost:${stub.server.address().port}/`;
stub.close = async () => {
stub.server.closeAllConnections();
stub.server.close();
await events.once(stub.server, "close");
};
return stub;
}
// Handler for a request that is approved after the given number of pending polls.
function approveAfter(polls, result = { hostId: 1234, token: "host-token", name: "Approved" }) {
let pending = 0;
return ({ url }) => {
if (url === "/api/host-pairing") {
return [202, { requestId: "secret", code: "ABC-DEF" }];
}
if (url === "/api/host-pairing/poll") {
pending += 1;
return pending > polls ? [200, result] : [202, { status: "pending" }];
}
return [404, { error: "Not found" }];
};
}
describe("create/pair", function() {
let stub;
beforeEach(async function() {
stub = await startStub();
});
afterEach(async function() {
await stub.close();
});
describe("pairHost()", function() {
it("returns the host when approved with a user token", async function() {
stub.handler = () => [200, { hostId: 5, token: "host-token", name: "Host" }];
let pendingCalls = 0;
const result = await pairHost({
controllerUrl: stub.url, name: "Host", version: "2.0.3", token: "user-token",
onPending: () => { pendingCalls += 1; },
});
assert.deepEqual(result, { hostId: 5, token: "host-token", name: "Host" });
assert.equal(pendingCalls, 0);
assert.equal(stub.requests.length, 1);
const [created] = stub.requests;
assert.equal(created.method, "POST");
assert.equal(created.url, "/api/host-pairing");
assert.equal(created.headers.authorization, "Bearer user-token");
assert.deepEqual(created.body, { name: "Host", version: "2.0.3" });
});
it("polls until the request is approved", async function() {
stub.handler = approveAfter(2);
const codes = [];
const result = await pairHost({
controllerUrl: stub.url, name: "Host", pollIntervalMs: 10,
onPending: ({ code }) => { codes.push(code); },
});
assert.deepEqual(result, { hostId: 1234, token: "host-token", name: "Approved" });
assert.deepEqual(codes, ["ABC-DEF"]);
assert.deepEqual(stub.requests.map(r => r.url), [
"/api/host-pairing", "/api/host-pairing/poll", "/api/host-pairing/poll", "/api/host-pairing/poll",
]);
assert.equal(stub.requests[0].headers.authorization, undefined);
assert.deepEqual(stub.requests[0].body, { name: "Host", version: "" });
for (const poll of stub.requests.slice(1)) {
assert.deepEqual(poll.body, { requestId: "secret" });
}
});
["sub", "sub/"].forEach(controllerPath => {
it(`resolves routes under a controller URL ending in ${controllerPath}`, async function() {
stub.handler = ({ url }) => (url === "/sub/api/host-pairing"
? [200, { hostId: 5, token: "host-token", name: "Host" }]
: [404, {}]
);
const result = await pairHost({ controllerUrl: `${stub.url}${controllerPath}`, name: "Host" });
assert.equal(result.hostId, 5);
});
});
it("throws the error from the controller when the request is rejected", async function() {
const handler = approveAfter(1);
stub.handler = entry => {
if (entry.url === "/api/host-pairing/poll" && stub.requests.length > 2) {
return [403, { error: "Request was rejected" }];
}
return handler(entry);
};
await assert.rejects(
pairHost({ controllerUrl: stub.url, name: "Host", pollIntervalMs: 10 }),
err => err instanceof PairingError && err.status === 403 && err.message === "Request was rejected",
);
});
it("throws if the controller does not support pairing", async function() {
stub.handler = () => [404, undefined];
await assert.rejects(
pairHost({ controllerUrl: stub.url, name: "Host" }),
err => err instanceof PairingError && err.status === 404 && /must be updated/.test(err.message),
);
});
it("throws on an invalid user token", async function() {
stub.handler = () => [401, { error: "Invalid user token" }];
await assert.rejects(
pairHost({ controllerUrl: stub.url, name: "Host", token: "bad", onPending: () => assert.fail() }),
err => err instanceof PairingError && err.status === 401 && err.message === "Invalid user token",
);
assert.equal(stub.requests.length, 1);
});
it("includes the retry time when rate limited", async function() {
stub.handler = () => [429, { error: "Too many requests from this address" }, { "Retry-After": "42" }];
await assert.rejects(
pairHost({ controllerUrl: stub.url, name: "Host" }),
err => err instanceof PairingError && err.status === 429 && /try again in 42 seconds/.test(err.message),
);
});
it("throws if the request expired", async function() {
const handler = approveAfter(0);
stub.handler = entry => (entry.url === "/api/host-pairing/poll"
? [404, { error: "Request not found" }]
: handler(entry)
);
await assert.rejects(
pairHost({ controllerUrl: stub.url, name: "Host", pollIntervalMs: 10 }),
err => err instanceof PairingError && err.status === 404 && err.message === "Request not found",
);
});
it("cancels the request when aborted", async function() {
const controller = new AbortController();
stub.handler = entry => {
if (entry.url === "/api/host-pairing/poll") {
controller.abort();
}
if (entry.url === "/api/host-pairing/cancel") {
return [204, undefined];
}
return approveAfter(Infinity)(entry);
};
await assert.rejects(
pairHost({ controllerUrl: stub.url, name: "Host", pollIntervalMs: 10, signal: controller.signal }),
{ name: "AbortError" },
);
const cancel = stub.requests.find(r => r.url === "/api/host-pairing/cancel");
assert.deepEqual(cancel?.body, { requestId: "secret" });
});
it("does not send anything if already aborted", async function() {
await assert.rejects(
pairHost({ controllerUrl: stub.url, name: "Host", signal: AbortSignal.abort() }),
{ name: "AbortError" },
);
assert.equal(stub.requests.length, 0);
});
it("throws on invalid controller URLs", async function() {
for (const controllerUrl of ["localhost:8080", "ftp://localhost/", "not a url"]) {
await assert.rejects(pairHost({ controllerUrl, name: "Host" }), PairingError);
}
});
it("trusts the certificate passed as ca", async function() {
const [key, cert] = await Promise.all([
fs.readFile(path.join(tlsDir, "key.pem")),
fs.readFile(path.join(tlsDir, "cert.pem")),
]);
const httpsStub = await startStub(handler => https.createServer({ key, cert }, handler));
try {
httpsStub.handler = () => [200, { hostId: 5, token: "host-token", name: "Host" }];
const result = await pairHost({ controllerUrl: httpsStub.url, name: "Host", ca: cert });
assert.equal(result.hostId, 5);
// Passing a different certificate replaces the trusted ones, proving ca is used.
await assert.rejects(
pairHost({ controllerUrl: httpsStub.url, name: "Host", ca: tls.rootCertificates[0] }),
err => typeof err.code === "string" && !(err instanceof PairingError),
);
} finally {
await httpsStub.close();
}
});
});
describe("checkHostToken()", function() {
it("returns true if the controller accepts the token", async function() {
stub.handler = () => [200, { valid: true }];
assert.equal(await checkHostToken({ controllerUrl: stub.url, token: "host-token" }), true);
assert.equal(stub.requests[0].method, "GET");
assert.equal(stub.requests[0].url, "/api/host-pairing/check");
assert.equal(stub.requests[0].headers.authorization, "Bearer host-token");
});
it("returns false if the controller does not accept the token", async function() {
stub.handler = () => [401, undefined];
assert.equal(await checkHostToken({ controllerUrl: stub.url, token: "host-token" }), false);
});
it("throws on other responses", async function() {
stub.handler = () => [503, { error: "Controller is in recovery mode" }];
await assert.rejects(
checkHostToken({ controllerUrl: stub.url, token: "host-token" }),
err => err instanceof PairingError && err.status === 503,
);
});
});
});
describe("create/pair createLocalHost()", function() {
let directory;
let secret;
beforeEach(async function() {
directory = await fs.mkdtemp(path.join(os.tmpdir(), "clusterio-pair-"));
secret = crypto.randomBytes(256).toString("base64");
});
afterEach(async function() {
await fs.rm(directory, { recursive: true, force: true, maxRetries: 10 });
});
// Written with the controller's own config class to match its file format.
async function writeControllerConfig(fields = {}) {
const configPath = path.join(directory, "config-controller.json");
const config = new lib.ControllerConfig("controller", undefined, configPath);
config.set("controller.auth_secret", secret);
for (const [name, value] of Object.entries(fields)) {
config.set(name, value);
}
await config.save(true);
}
async function loadHosts(databaseDirectory = "database") {
const provider = new lib.JsonIdDatastoreProvider(
path.join(directory, databaseDirectory, "hosts.json"), HostRecord.fromJSON.bind(HostRecord),
);
return await provider.load();
}
it("adds a host the controller loads with a token it accepts", async function() {
await writeControllerConfig();
const before = Math.floor(Date.now() / 1000);
const result = await createLocalHost({ directory, name: "local" });
assert.equal(result.name, "local");
assert(Number.isInteger(result.hostId) && result.hostId >= 1 && result.hostId < 2 ** 31);
const hosts = await loadHosts();
assert.deepEqual([...hosts.keys()], [result.hostId]);
const record = hosts.get(result.hostId);
assert.equal(record.name, "local");
assert.equal(record.version, "");
assert.deepEqual(record.plugins, new Map());
assert(record.tokenValidAfter >= before);
const payload = jwt.verify(result.token, Buffer.from(secret, "base64"), { audience: "host" });
assert.equal(payload.host, result.hostId);
assert(payload.iat >= record.tokenValidAfter, "token must not be issued before token_valid_after");
});
it("appends to an existing database in the configured directory", async function() {
await writeControllerConfig({ "controller.database_directory": "db" });
await fs.mkdir(path.join(directory, "db"));
await fs.writeFile(path.join(directory, "db", "hosts.json"), JSON.stringify([
{ id: 1, name: "existing", version: "2.0.0", plugins: {} },
]));
const result = await createLocalHost({ directory, name: "local", id: 7, version: "2.0.3" });
assert.equal(result.hostId, 7);
const hosts = await loadHosts("db");
assert.deepEqual([...hosts.keys()], [1, 7]);
assert.equal(hosts.get(1).name, "existing");
assert.equal(hosts.get(7).version, "2.0.3");
await assert.rejects(fs.access(path.join(directory, "database")), { code: "ENOENT" });
});
it("signs a new token for an existing host with the same id", async function() {
await writeControllerConfig();
const first = await createLocalHost({ directory, name: "first", id: 7 });
const before = await fs.readFile(path.join(directory, "database", "hosts.json"), "utf8");
const second = await createLocalHost({ directory, name: "second", id: 7 });
assert.equal(second.hostId, 7);
assert.equal(second.name, "first", "the name of the existing host should be kept");
assert.equal(await fs.readFile(path.join(directory, "database", "hosts.json"), "utf8"), before);
const record = (await loadHosts()).get(7);
for (const token of [first.token, second.token]) {
const payload = jwt.verify(token, Buffer.from(secret, "base64"), { audience: "host" });
assert.equal(payload.host, 7);
assert(payload.iat >= record.tokenValidAfter);
}
});
it("throws if the controller config has no auth secret", async function() {
await fs.writeFile(path.join(directory, "config-controller.json"), "{}");
await assert.rejects(createLocalHost({ directory, name: "local" }), /controller\.auth_secret/);
});
});
+145
View File
@@ -0,0 +1,145 @@
import assert from "node:assert/strict";
import * as lib from "@clusterio/lib";
import { hostCommands } from "@clusterio/ctl/dist/node/src/commands_host.js";
/** Stand-in for Ctl that answers requests from a map of handlers and records what was sent */
class FakeCtl {
constructor(handlers) {
this.handlers = new Map(handlers);
this.sent = [];
}
async send(request) {
return await this.sendTo("controller", request);
}
async sendTo(dst, request) {
this.sent.push(request);
const handler = this.handlers.get(request.constructor);
if (!handler) {
throw new Error(`Unexpected ${request.constructor.name}`);
}
return handler(request);
}
}
async function run(command, args, ctl) {
const lines = [];
const originalLog = console.log; // eslint-disable-line no-console
console.log = (...content) => lines.push(content.join(" ")); // eslint-disable-line no-console
try {
await hostCommands.get(command).run(args, ctl);
} finally {
console.log = originalLog; // eslint-disable-line no-console
}
return lines;
}
describe("ctl/commands_host", function() {
// Created when the request is handled, the ages printed depend on the current time.
const pending = () => [
new lib.HostPairingDetails("ABC-DEF", "new-box", "192.0.2.10", "2.0.3", Date.now() - 65500),
new lib.HostPairingDetails("XYZ-234", "other-box", "2001:db8::1", "", Date.now() - 3720500),
];
const hosts = [{ id: 7, name: "basement-box" }, { id: 12, name: "attic-box" }];
it("no longer has the token based commands", function() {
for (const name of ["generate-token", "revoke-token", "create-config"]) {
assert.equal(hostCommands.get(name), undefined, `host ${name} still exists`);
}
});
describe("host pending", function() {
it("should list pending requests with their age", async function() {
const ctl = new FakeCtl([[lib.HostPairingListRequest, pending]]);
const lines = await run("pending", {}, ctl);
const output = lines.join("\n");
assert.match(output, /code\s*\|\s*name\s*\|\s*address\s*\|\s*age/);
assert.match(output, /ABC-DEF\s*\|\s*new-box\s*\|\s*192\.0\.2\.10\s*\|\s*1m 5s/);
assert.match(output, /XYZ-234\s*\|\s*other-box\s*\|\s*2001:db8::1\s*\|\s*1h 2m/);
});
it("should say so when nothing is pending", async function() {
const ctl = new FakeCtl([[lib.HostPairingListRequest, () => []]]);
assert.deepEqual(await run("pending", {}, ctl), ["No pending host requests"]);
});
});
describe("host approve", function() {
it("should approve with the requested name and print it", async function() {
const ctl = new FakeCtl([
[lib.HostPairingListRequest, pending],
[lib.HostPairingApproveRequest, () => 42],
]);
const lines = await run("approve", { code: "abcdef" }, ctl);
const approve = ctl.sent.find(request => request instanceof lib.HostPairingApproveRequest);
assert.equal(approve.code, "abcdef");
assert.equal(approve.name, undefined);
assert.deepEqual(lines, ["Approved host new-box with id 42"]);
});
it("should pass --name and print it without listing requests", async function() {
const ctl = new FakeCtl([[lib.HostPairingApproveRequest, () => 43]]);
const lines = await run("approve", { code: "ABC-DEF", name: " renamed " }, ctl);
assert.equal(ctl.sent.length, 1);
assert.equal(ctl.sent[0].code, "ABC-DEF");
assert.equal(ctl.sent[0].name, " renamed ");
assert.deepEqual(lines, ["Approved host renamed with id 43"]);
});
it("should pass on errors from the controller", async function() {
const ctl = new FakeCtl([
[lib.HostPairingListRequest, () => []],
[lib.HostPairingApproveRequest, () => {
throw new lib.NotFoundError("No pending host request with code AAA-AAA");
}],
]);
await assert.rejects(
run("approve", { code: "AAA-AAA" }, ctl),
new lib.NotFoundError("No pending host request with code AAA-AAA"),
);
});
});
describe("host reject", function() {
it("should send the code to the controller", async function() {
const ctl = new FakeCtl([[lib.HostPairingRejectRequest, () => undefined]]);
assert.deepEqual(await run("reject", { code: "abc-def" }, ctl), []);
assert.equal(ctl.sent.length, 1);
assert(ctl.sent[0] instanceof lib.HostPairingRejectRequest);
assert.equal(ctl.sent[0].code, "abc-def");
});
});
describe("host revoke", function() {
function revokeCtl() {
return new FakeCtl([
[lib.HostListRequest, () => hosts],
[lib.HostRevokeRequest, () => undefined],
]);
}
it("should revoke a host given by name", async function() {
const ctl = revokeCtl();
await run("revoke", { host: "basement-box" }, ctl);
const revoke = ctl.sent.at(-1);
assert(revoke instanceof lib.HostRevokeRequest);
assert.equal(revoke.hostId, 7);
});
it("should revoke a host given by id", async function() {
const ctl = revokeCtl();
await run("revoke", { host: "12" }, ctl);
assert.equal(ctl.sent.at(-1).hostId, 12);
});
it("should fail for an unknown host", async function() {
const ctl = revokeCtl();
await assert.rejects(
run("revoke", { host: "missing" }, ctl),
new lib.CommandError("No host named missing"),
);
assert(!ctl.sent.some(request => request instanceof lib.HostRevokeRequest));
});
});
});
+278 -64
View File
@@ -10,11 +10,12 @@ import * as libBuildMod from "@clusterio/lib/build_mod.js";
import * as testStrings from "../lib/factorio/test_strings.js";
import {
TestControl, TestControlConnector, url, controlToken,
TestControl, TestControlConnector, TestHostConnector, url, controlToken, hostId,
execCtl, execCtlProcess, execController, execHost, sendRcon, getControl,
spawnNode, instancesDir, factorioDir, databaseDir, controllerConfigPath,
requiresFactorio, hasFactorio,
requiresFactorio, hasFactorio, pairTestHost, writeHostConfig,
} from "./index.js";
import { pairHost, checkHostToken, PairingError } from "../../packages/create/pair.js";
import { externalTest, slowTest } from "../common.js";
import { getTestRconPort } from "./rcon_port.js";
import { ControllerEcho, HostEchoReceived, InstanceEcho } from "../file/test_plugin/messages.js";
@@ -31,6 +32,18 @@ async function checkInstanceStatus(id, status) {
assert.equal(instances.get(44).status, status, "incorrect instance status");
}
// Factorio directory relative to temp/test where the hosts are run from
const hostFactorioDir = path.isAbsolute(factorioDir) ? factorioDir : path.join("..", "..", factorioDir);
/** Id, token and name of the alt-host, paired the first time it starts */
const altHost = { hostId: undefined, token: undefined, name: undefined };
async function pairAltHost() {
if (altHost.hostId === undefined) {
Object.assign(altHost, await pairTestHost("alt-host"));
}
}
async function spawnAltHost(config = "alt-host-config.json") {
return await spawnNode("alt-host:", `../../packages/host run --config ${config}`, /Started host/);
}
@@ -41,10 +54,10 @@ async function startAltHost() {
await fs.rm(configPath, { force: true });
await fs.rm(path.join("temp", "test", "alt-instances"), { force: true, recursive: true, maxRetries: 10 });
await fs.rm(path.join("temp", "test", "alt-mods"), { force: true, recursive: true, maxRetries: 10 });
await execCtl(`host create-config --id 5 --name alt-host --generate-token --output ${config}`);
await pairAltHost();
await writeHostConfig(configPath, altHost);
await execHost(`--config ${config} config set host.instances_directory alt-instances`);
const dir = path.isAbsolute(factorioDir) ? factorioDir : path.join("..", "..", factorioDir);
await execHost(`--config ${config} config set host.factorio_directory ${dir}`);
await execHost(`--config ${config} config set host.factorio_directory ${hostFactorioDir}`);
await execHost(`--config ${config} config set host.mods_directory alt-mods`);
return await spawnAltHost(config);
}
@@ -57,7 +70,7 @@ async function stopAltHost(hostProcess) {
"timeout"
).catch(() => {});
try {
await execCtl("host stop 5");
await execCtl(`host stop ${altHost.hostId}`);
} catch (err) {
// Some tests cause the host to stop
}
@@ -105,6 +118,28 @@ function jsonArg(value) {
return `"${JSON.stringify(value).replace(/"/g, process.platform === "win32" ? '""' : '\\"')}"`;
}
/** Run a ctl command in this process and return what it printed */
async function execCtlOutput(command) {
const lines = [];
const originalLog = console.log; // eslint-disable-line no-console
console.log = (...args) => lines.push(args.join(" ")); // eslint-disable-line no-console
try {
await execCtl(command);
} finally {
console.log = originalLog; // eslint-disable-line no-console
}
return lines.join("\n");
}
/** Validator for assert.rejects expecting a PairingError with the given HTTP status */
function pairingError(status) {
return err => {
assert(err instanceof PairingError, `Expected a PairingError, got ${err.stack}`);
assert.equal(err.status, status, err.message);
return true;
};
}
describe("Integration of Clusterio", function() {
describe("clusteriocontroller", function() {
describe("bootstrap generate-user-token", function() {
@@ -281,11 +316,15 @@ describe("Integration of Clusterio", function() {
describe("hostUpdateEventHandler()", function() {
it("should trigger when a new host is added", async function() {
slowTest(this);
// Pairing sends an update for the new host before it connects, which is not what is tested here.
// The round trip makes sure that update has been received before clearing the updates.
await pairAltHost();
await getControl().send(new lib.HostListRequest());
getControl().hostUpdates = [];
await runWithAltHost(async () => {
await execCtl("instance create alt-test --id 99");
await execCtl("instance assign alt-test 5");
await execCtl(`instance assign alt-test ${altHost.hostId}`);
});
let sawUpdate = false;
@@ -311,7 +350,7 @@ describe("Integration of Clusterio", function() {
let result = await getControl().send(new lib.HostListRequest());
let hosts = new Map(result.map(instance => [instance.id, instance]));
assert(hosts.has(5), "Host list was not updated");
assert(hosts.has(altHost.hostId), "Host list was not updated");
});
});
it("should download mods from controller", async function() {
@@ -325,7 +364,7 @@ describe("Integration of Clusterio", function() {
}
assert(!await checkModDownloaded(), "mod was downloaded before the test");
await execCtl("instance create alt-mod --id 98");
await execCtl("instance assign alt-mod 5");
await execCtl(`instance assign alt-mod ${altHost.hostId}`);
await execCtl("instance save create alt-mod");
assert(await checkModDownloaded(), "mod was not downloaded by the test");
});
@@ -340,7 +379,7 @@ describe("Integration of Clusterio", function() {
hostProcess = await startAltHost();
await execCtl("instance create alt-start --id 97");
await execCtl(`instance config set alt-start factorio.rcon_port ${await getTestRconPort()}`);
await execCtl("instance assign alt-start 5");
await execCtl(`instance assign alt-start ${altHost.hostId}`);
await execCtl("instance config set alt-start instance.auto_start true");
const visibility = jsonArg({ lan: false, public: false });
await execCtlProcess(`instance config set-prop alt-start factorio.settings visibility ${visibility}`);
@@ -448,7 +487,7 @@ describe("Integration of Clusterio", function() {
});
it("should load the plugin on the host", async function() {
const details = await pluginDetails({ hostId: 4 });
const details = await pluginDetails({ hostId });
assert(details.enabled, "test_plugin is not enabled on the host");
assert(details.loaded, "test_plugin is not loaded on the host");
});
@@ -470,7 +509,7 @@ describe("Integration of Clusterio", function() {
});
it("should route a plugin request from control to a host", async function() {
const received = await getControl().sendTo({ hostId: 4 }, new HostEchoReceived("never-broadcast"));
const received = await getControl().sendTo({ hostId }, new HostEchoReceived("never-broadcast"));
assert.equal(received, false);
});
@@ -480,7 +519,7 @@ describe("Integration of Clusterio", function() {
// The broadcast is sent without waiting for the hosts to receive it.
let received = false;
for (let attempt = 0; attempt < 100 && !received; attempt++) {
received = await getControl().sendTo({ hostId: 4 }, new HostEchoReceived("to-hosts"));
received = await getControl().sendTo({ hostId }, new HostEchoReceived("to-hosts"));
if (!received) {
await lib.wait(20);
}
@@ -593,31 +632,31 @@ describe("Integration of Clusterio", function() {
describe("host config", function() {
it("changes host config", async function() {
await execCtl("host config set 4 host.name My-Host");
const result = await execCtlProcess("host config list 4");
await execCtl(`host config set ${hostId} host.name My-Host`);
const result = await execCtlProcess(`host config list ${hostId}`);
assert(/My-Host/.test(result.stdout), "New host name not in config output");
});
it("should not allow setting host.controller_token", async function() {
await assert.rejects(
execCtl("host config set 4 host.controller_token xyz"),
execCtl(`host config set ${hostId} host.controller_token xyz`),
/Field 'host\.controller_token' is not accessible/,
);
});
it("should not allow setting host.id", async function() {
await assert.rejects(
execCtl("host config set 4 host.id 200"),
execCtl(`host config set ${hostId} host.id 200`),
/Setting 'host\.id' while host is running is not supported/,
);
});
it("should not leak host.controller_token", async function() {
let result = await getControl().sendTo({ hostId: 4 }, new lib.HostConfigGetRequest());
let result = await getControl().sendTo({ hostId }, new lib.HostConfigGetRequest());
assert.equal(Object.prototype.hasOwnProperty.call(result, "host.controller_token"), false);
});
});
describe("host plugin list", function() {
it("runs", async function() {
await execCtl("host plugin list 4");
await execCtl(`host plugin list ${hostId}`);
});
});
@@ -625,7 +664,7 @@ describe("Integration of Clusterio", function() {
it("runs", async function() {
// In dev plugins have no npm package, so best we can do is get an error from the host
await assert.rejects(
execCtl("host plugin update 4 foo"),
execCtl(`host plugin update ${hostId} foo`),
/Plugin foo is not installed on this machine/
);
});
@@ -637,7 +676,7 @@ describe("Integration of Clusterio", function() {
// Default is to disallow updates, changing this value would require a restart
// Additionally, it can not be changed via ctl, so best we can do is get an error from the host
await assert.rejects(
execCtl("host plugin install 4 foo"),
execCtl(`host plugin install ${hostId} foo`),
/Plugin installs are disabled on this machine/
);
});
@@ -646,15 +685,15 @@ describe("Integration of Clusterio", function() {
describe("host update", function() {
it("runs", async function() {
await execCtl("host update 4");
await execCtl(`host update ${hostId}`);
});
it("accepts --all", async function() {
await execCtl("host update 4 --all");
await execCtl(`host update ${hostId} --all`);
});
it("accepts --restart", async function() {
// We cannot restart the host, so we check for host error instead
await assert.rejects(
execCtl("host update 4 --restart"),
execCtl(`host update ${hostId} --restart`),
/Cannot restart, host does not have a process monitor to restart it./
);
});
@@ -664,7 +703,7 @@ describe("Integration of Clusterio", function() {
it("runs", async function() {
// We cannot restart the host, so we check for host error instead
await assert.rejects(
execCtl("host restart 4"),
execCtl(`host restart ${hostId}`),
/Cannot restart, host does not have a process monitor to restart it./
);
});
@@ -672,41 +711,216 @@ describe("Integration of Clusterio", function() {
describe("host mark-restart-required", function() {
it("runs", async function() {
await execCtl("host mark-restart-required 4");
await execCtl(`host mark-restart-required ${hostId}`);
});
});
describe("host generate-token", function() {
it("runs", async function() {
await execCtl("host generate-token --id 42");
describe("host pending, approve and reject", function() {
let pairings = [];
afterEach(async function() {
// Stop installers left waiting by a failed test
for (const pairing of pairings) {
pairing.abort.abort();
}
await Promise.allSettled(pairings.map(pairing => pairing.result));
pairings = [];
});
it("runs without an id", async function() {
await execCtl("host generate-token");
});
});
describe("host revoke-token", async function() {
it("should disconnect existing host", async function() {
slowTest(this);
let sawDisconnected;
await runWithAltHost(async () => {
const control = getControl();
control.hostUpdates = [];
await wait(50); // Allow for host to update instances
await execCtl("host revoke-token 5");
await wait(50); // Allow controller to notify control
for (let update of control.hostUpdates) {
if (update.name !== "alt-host") {
continue;
}
if (!update.connected) {
sawDisconnected = true;
}
}
// Start pairing like an installer without a token, returns once the request is pending.
async function requestPairing(name) {
const abort = new AbortController();
let onPending;
const pending = new Promise(resolve => { onPending = resolve; });
const result = pairHost({
controllerUrl: url, name, pollIntervalMs: 100, signal: abort.signal, onPending,
});
const pairing = { abort, result, code: undefined };
pairings.push(pairing);
const ended = result.then(() => {
throw new Error("Pairing finished before the request was pending");
});
ended.catch(() => {}); // Only of interest while racing the pending request
({ code: pairing.code } = await Promise.race([pending, ended]));
return pairing;
}
assert(sawDisconnected, "No host update with status disconnected was sent after revoking token");
it("should list a waiting request and hand the host over when approved", async function() {
const control = getControl();
control.hostPairingUpdates = [];
const pairing = await requestPairing("pending-host");
assert.match(pairing.code, /^[2-9A-HJKMNP-Z]{3}-[2-9A-HJKMNP-Z]{3}$/);
const pendingOutput = await execCtlOutput("host pending");
const pendingLines = pendingOutput.split("\n");
assert(
pendingLines.some(line => line.includes(pairing.code) && line.includes("pending-host")),
`Request missing from host pending output:\n${pendingOutput}`,
);
assert(
control.hostPairingUpdates.some(update => update.code === pairing.code && !update.isDeleted),
"No pairing update was sent for the new request",
);
// Codes are matched ignoring case and separators
const typedCode = pairing.code.replace("-", "").toLowerCase();
const approveOutput = await execCtlOutput(`host approve ${typedCode} --name approved-host`);
const approved = await pairing.result;
assert.equal(approved.name, "approved-host");
assert(approveOutput.includes(String(approved.hostId)), `Host id missing from: ${approveOutput}`);
assert(
control.hostPairingUpdates.some(update => update.code === pairing.code && update.isDeleted),
"No pairing update was sent for the approved request",
);
const requests = await control.send(new lib.HostPairingListRequest());
assert(!requests.some(request => request.code === pairing.code), "Approved request is still pending");
const hosts = await control.send(new lib.HostListRequest());
assert.equal(hosts.find(host => host.id === approved.hostId)?.name, "approved-host");
assert.equal(await checkHostToken({ controllerUrl: url, token: approved.token }), true);
await execCtl(`host revoke ${approved.hostId}`);
});
it("should make the installer fail when the request is rejected", async function() {
const pairing = await requestPairing("rejected-host");
await execCtl(`host reject ${pairing.code}`);
await assert.rejects(pairing.result, pairingError(403));
const requests = await getControl().send(new lib.HostPairingListRequest());
assert(!requests.some(request => request.code === pairing.code), "Rejected request is still pending");
const hosts = await getControl().send(new lib.HostListRequest());
assert(!hosts.some(host => host.name === "rejected-host"), "Rejected host was added");
});
it("should remove the request when the installer stops waiting", async function() {
const pairing = await requestPairing("cancelled-host");
pairing.abort.abort();
await assert.rejects(pairing.result, err => err.name === "AbortError");
const requests = await getControl().send(new lib.HostPairingListRequest());
assert(!requests.some(request => request.code === pairing.code), "Cancelled request is still pending");
});
it("should fail for a code that is not pending", async function() {
await assert.rejects(execCtl("host approve 222-222"), /No pending host request with code 222-222/);
await assert.rejects(execCtl("host reject 222-222"), /No pending host request with code 222-222/);
});
it("should refuse an invalid user token instead of waiting for approval", async function() {
await assert.rejects(
pairHost({ controllerUrl: url, name: "invalid-token-host", token: "invalid" }), pairingError(401)
);
const requests = await getControl().send(new lib.HostPairingListRequest());
assert(!requests.some(request => request.name === "invalid-token-host"), "Request was created");
});
it("should refuse a user token without permission to approve hosts", async function() {
await getControl().send(new lib.UserCreateRequest("pairing-player"));
try {
const token = jwt.sign(
{ aud: "user", user: "pairing-player" }, Buffer.from("TestSecretDoNotUse", "base64")
);
await assert.rejects(
pairHost({ controllerUrl: url, name: "unapproved-host", token }), pairingError(403)
);
} finally {
await getControl().send(new lib.UserDeleteRequest("pairing-player"));
}
const requests = await getControl().send(new lib.HostPairingListRequest());
assert(!requests.some(request => request.name === "unapproved-host"), "Request was created");
const hosts = await getControl().send(new lib.HostListRequest());
assert(!hosts.some(host => host.name === "unapproved-host"), "Host was added");
});
});
describe("host revoke", function() {
it("should remove the host and invalidate its token", async function() {
const pairing = await pairTestHost("revokee-host");
assert.equal(await checkHostToken({ controllerUrl: url, token: pairing.token }), true);
await execCtl(`note set host ${pairing.hostId} short-lived`);
const control = getControl();
control.hostUpdates = [];
await execCtl(`host revoke ${pairing.hostId}`);
assert(
control.hostUpdates.some(update => update.id === pairing.hostId && update.isDeleted),
"No host update with isDeleted was sent",
);
const hosts = await control.send(new lib.HostListRequest());
assert(!hosts.some(host => host.id === pairing.hostId), "Host was not removed");
assert.equal(await checkHostToken({ controllerUrl: url, token: pairing.token }), false);
const notes = await control.send(new lib.NoteListRequest());
assert(!notes.some(note => note.id === `host/${pairing.hostId}`), "Note of the host was not removed");
});
it("should refuse while instances are assigned to the host", async function() {
const pairing = await pairTestHost("assigned-host");
// Instances can only be assigned to connected hosts, a link that accepts the assignment is enough.
const connector = new TestHostConnector(url, 0.2);
connector.token = pairing.token;
connector.hostId = pairing.hostId;
const link = new lib.Link(connector);
link.handle(lib.InstanceAssignInternalRequest, async () => {});
await connector.connect();
await execCtl("instance create revoke-assigned --id 45");
try {
await execCtl(`instance assign revoke-assigned ${pairing.hostId}`);
await assert.rejects(
execCtl(`host revoke ${pairing.hostId}`),
/Cannot revoke host assigned-host while instances are assigned to it: revoke-assigned/,
);
} finally {
await connector.disconnect();
await execCtl("instance assign revoke-assigned");
await execCtl("instance delete revoke-assigned");
}
await execCtl(`host revoke ${pairing.hostId}`);
const hosts = await getControl().send(new lib.HostListRequest());
assert(!hosts.some(host => host.id === pairing.hostId), "Host was not removed");
});
it("should stop a running host and prevent it from starting again", async function() {
slowTest(this);
this.timeout(30000);
const config = "revoke-host-config.json";
const pairing = await pairTestHost("running-revokee");
await writeHostConfig(path.join("temp", "test", config), pairing, {
"host.factorio_directory": hostFactorioDir,
"host.instances_directory": "revoke-instances",
"host.mods_directory": "revoke-mods",
});
const hostProcess = await spawnNode(
"revoke-host:", `../../packages/host run --config ${config}`, /Started host/
);
try {
const exited = events.once(hostProcess, "exit");
await execCtl(`host revoke ${pairing.hostId}`);
assert.notEqual(
await lib.timeout(exited, 10e3, "timeout"), "timeout", "Host did not stop after being revoked"
);
} finally {
if (hostProcess.exitCode === null && hostProcess.signalCode === null) {
hostProcess.kill("SIGINT");
}
}
// Exit code 8 tells the run scripts not to restart the host
await assert.rejects(execHost(`--config ${config} run`), err => {
const output = `${err.stdout}${err.stderr}`;
assert.equal(err.code, 8, `Unexpected exit code ${err.code}:\n${output}`);
assert.match(output, /host is not registered/);
return true;
});
});
});
describe("removed host token commands", function() {
it("should no longer exist", async function() {
slowTest(this);
for (const command of ["generate-token", "revoke-token", "create-config"]) {
await assert.rejects(execCtlProcess(`host ${command}`), new RegExp(`Unknown argument: ${command}`));
}
});
});
@@ -751,7 +965,7 @@ describe("Integration of Clusterio", function() {
describe("instance assign", function() {
it("creates the instance files", async function() {
await execCtl("instance assign test 4");
await execCtl(`instance assign test ${hostId}`);
await assert.doesNotReject(
fs.access(path.join(instancesDir, "test")),
"Instance directory was not created"
@@ -1046,14 +1260,14 @@ describe("Integration of Clusterio", function() {
assert.equal(controllerEntry.host_id, undefined);
assert.equal(controllerEntry.instance_id, undefined);
const hostEntry = await findLog({ hostIds: [4] }, "test_plugin host echo log-tagging");
const hostEntry = await findLog({ hostIds: [hostId] }, "test_plugin host echo log-tagging");
assert.equal(hostEntry.plugin, "test_plugin");
assert.equal(hostEntry.host_id, 4);
assert.equal(hostEntry.host_id, hostId);
assert.equal(hostEntry.instance_id, undefined);
const instanceEntry = await findLog({ instanceIds: [44] }, "test_plugin instance echo log-tagging");
assert.equal(instanceEntry.plugin, "test_plugin");
assert.equal(instanceEntry.host_id, 4);
assert.equal(instanceEntry.host_id, hostId);
assert.equal(instanceEntry.instance_id, 44);
});
});
@@ -1523,7 +1737,7 @@ describe("Integration of Clusterio", function() {
before(async function() {
slowTest(this);
await execCtlProcess("instance create spam --id 66");
await execCtlProcess("instance assign spam 4");
await execCtlProcess(`instance assign spam ${hostId}`);
await execCtlProcess("instance create unassign --id 77");
});
after(async function() {
@@ -1550,7 +1764,7 @@ describe("Integration of Clusterio", function() {
hostProcess = await startAltHost();
await execCtlProcess("instance create save-test --id 88");
await execCtlProcess("instance assign save-test 5");
await execCtlProcess(`instance assign save-test ${altHost.hostId}`);
});
after(async function() {
await stopAltHost(hostProcess);
@@ -1978,9 +2192,9 @@ describe("Integration of Clusterio", function() {
describe("note set", function() {
it("should set the note of the given resource", async function() {
await execCtl("note set host 4 basement-box");
await execCtl(`note set host ${hostId} basement-box`);
const notes = await getControl().send(new lib.NoteListRequest());
const note = notes.find(n => n.id === "host/4");
const note = notes.find(n => n.id === `host/${hostId}`);
assert.equal(note.content, "basement-box");
assert.equal(note.updatedBy, "test");
});
@@ -2001,7 +2215,7 @@ describe("Integration of Clusterio", function() {
const originalLog = console.log; // eslint-disable-line no-console
console.log = (...args) => lines.push(args.join(" ")); // eslint-disable-line no-console
try {
await execCtl("note get host 4");
await execCtl(`note get host ${hostId}`);
} finally {
console.log = originalLog; // eslint-disable-line no-console
}
@@ -2017,9 +2231,9 @@ describe("Integration of Clusterio", function() {
describe("note delete", function() {
it("should remove the note of the given resource", async function() {
await execCtl("note delete host 4");
await execCtl(`note delete host ${hostId}`);
const notes = await getControl().send(new lib.NoteListRequest());
assert(!notes.find(n => n.id === "host/4"), "Note was not deleted");
assert(!notes.find(n => n.id === `host/${hostId}`), "Note was not deleted");
});
});
+44 -1
View File
@@ -12,6 +12,7 @@ import { LineSplitter, logger } from "@clusterio/lib";
import { selectTargetCommand, initialize as initializeCtl } from "@clusterio/ctl";
import { _listFactorioVersions } from "@clusterio/host/dist/node/src/server.js";
import { pairHost } from "../../packages/create/pair.js";
import "../setup_logging.js";
@@ -26,6 +27,7 @@ export class TestControl extends lib.Link {
constructor(connector, subscribe = true) {
super(connector);
this.hostUpdates = [];
this.hostPairingUpdates = [];
this.instanceUpdates = [];
this.saveUpdates = [];
this.modUpdates = [];
@@ -39,6 +41,9 @@ export class TestControl extends lib.Link {
this.send(
new lib.SubscriptionRequest(lib.HostUpdatesEvent.name, true)
).catch(err => logger.error(`Error setting host subscriptions:\n${err.stack}`));
this.send(
new lib.SubscriptionRequest(lib.HostPairingUpdatesEvent.name, true)
).catch(err => logger.error(`Error setting host pairing subscriptions:\n${err.stack}`));
this.send(
new lib.SubscriptionRequest(lib.InstanceDetailsUpdatesEvent.name, true)
).catch(err => logger.error(`Error setting instance subscriptions:\n${err.stack}`));
@@ -58,6 +63,7 @@ export class TestControl extends lib.Link {
this.handle(lib.AccountUpdateEvent);
this.handle(lib.HostUpdatesEvent, this.handleHostUpdatesEvent.bind(this));
this.handle(lib.HostPairingUpdatesEvent, this.handleHostPairingUpdatesEvent.bind(this));
this.handle(lib.InstanceDetailsUpdatesEvent, this.handleInstanceDetailsUpdatesEvent.bind(this));
this.handle(lib.InstanceSaveDetailsUpdatesEvent, this.handleInstanceSaveDetailsUpdatesEvent.bind(this));
this.handle(lib.ModUpdatesEvent, this.handleModUpdatesEvent.bind(this));
@@ -69,6 +75,10 @@ export class TestControl extends lib.Link {
this.hostUpdates.push(...event.updates);
}
async handleHostPairingUpdatesEvent(event) {
this.hostPairingUpdates.push(...event.updates);
}
async handleInstanceDetailsUpdatesEvent(event) {
this.instanceUpdates.push(...event.updates);
}
@@ -181,6 +191,8 @@ const baseHostConfig = loadJSON("config-host.json");
export let url = "https://localhost:4443/";
// Signed in the root before hook, as the user has to exist before the token is issued
export let controlToken;
// Id of the test host, assigned by the controller when it is paired in the root before hook
export let hostId;
export let instancesDir = path.join("temp", "test", "instances");
export let modsDir = path.join("temp", "test", "mods");
export let databaseDir = path.join("temp", "test", "database");
@@ -253,6 +265,35 @@ export function getControl() {
return control;
}
/**
* Pair a new host with the test controller, approved right away with the admin token
*
* @param {string} name - Name to give the host.
* @returns {Promise<{ hostId: number, token: string, name: string }>} the paired host.
*/
export async function pairTestHost(name) {
return await pairHost({ controllerUrl: url, name, token: controlToken });
}
/**
* Write a new host config for a paired host, with the fields the installer sets
*
* @param {string} configPath - Path to write the config to.
* @param {{ hostId: number, token: string, name: string }} pairing - Host returned by pairing.
* @param {Record<string, unknown>} fields - Other fields to set in the config.
*/
export async function writeHostConfig(configPath, pairing, fields = {}) {
const hostConfig = new lib.HostConfig("host");
hostConfig.set("host.id", pairing.hostId);
hostConfig.set("host.name", pairing.name);
hostConfig.set("host.controller_url", url);
hostConfig.set("host.controller_token", pairing.token);
for (const [name, value] of Object.entries(fields)) {
hostConfig.set(name, value);
}
await fs.writeFile(configPath, JSON.stringify(hostConfig, null, "\t"), { mode: 0o600 });
}
/** @returns {Promise<child_process.ChildProcess>} */
export function spawn(name, cmd, waitFor, options = {}) {
@@ -366,7 +407,9 @@ before(async function() {
haveFactorioInstall = factorioVersions.size > 0;
const relativeFactorioDir = path.isAbsolute(factorioDir) ? factorioDir : path.join("..", "..", factorioDir);
await execCtlProcess("host create-config --id 4 --name host --generate-token");
const pairing = await pairTestHost("host");
hostId = pairing.hostId;
await writeHostConfig(hostConfigPath, pairing);
await execHost("config create");
await execHost(`config set host.factorio_directory ${relativeFactorioDir}`);
+6 -6
View File
@@ -8,7 +8,7 @@ import fs from "node:fs/promises";
import { slowTest, testMatrix } from "../common.js";
import {
execCtl, execCtlProcess, sendRcon, getControl,
requiresFactorio, hasFactorio, getFactorioVersion, instancesDir,
requiresFactorio, hasFactorio, getFactorioVersion, instancesDir, hostId,
} from "./index.js";
import { getTestRconPort } from "./rcon_port.js";
@@ -96,7 +96,7 @@ describe("Clusterio Instance", function() {
await execCtl(`instance config set ${instName} factorio.enable_whitelist true`);
await execCtlProcess(`instance config set-prop ${instName} factorio.settings visibility "${visibility}"`);
await execCtl(`instance config set-prop ${instName} factorio.settings require_user_verification false`);
await execCtl(`instance assign ${instName} 4`);
await execCtl(`instance assign ${instName} ${hostId}`);
// Exclude main test instance from start-all to prevent interference
await execCtl(`instance config set ${instName} instance.exclude_from_start_all true`);
@@ -112,7 +112,7 @@ describe("Clusterio Instance", function() {
await execCtl(`instance config set ${instAltName} factorio.enable_whitelist true`);
await execCtlProcess(`instance config set-prop ${instAltName} factorio.settings visibility "${visibility}"`);
await execCtl(`instance config set-prop ${instAltName} factorio.settings require_user_verification false`);
await execCtl(`instance assign ${instAltName} 4`);
await execCtl(`instance assign ${instAltName} ${hostId}`);
// Exclude alt test instance from start-all to prevent interference
await execCtl(`instance config set ${instAltName} instance.exclude_from_start_all true`);
await execCtl(`instance start ${instAltName}`);
@@ -340,9 +340,9 @@ describe("start-all and stop-all commands", function() {
await execCtl(`instance create ${testInstName3} --id ${testInstId3}`);
// Configure instances
await execCtl(`instance assign ${testInstName1} 4`);
await execCtl(`instance assign ${testInstName2} 4`);
await execCtl(`instance assign ${testInstName3} 4`);
await execCtl(`instance assign ${testInstName1} ${hostId}`);
await execCtl(`instance assign ${testInstName2} ${hostId}`);
await execCtl(`instance assign ${testInstName3} ${hostId}`);
const visibility = JSON.stringify({ lan: false, public: false }).replace(
/"/g, process.platform === "win32" ? '""' : '\\"'
);
+29 -7
View File
@@ -3,25 +3,36 @@ import events from "node:events";
import jwt from "jsonwebtoken";
import {
TestControlConnector, TestHostConnector, TestControl, url, controlToken, getControl,
TestControlConnector, TestHostConnector, TestControl, url, controlToken, getControl, hostId, pairTestHost,
} from "./index.js";
import * as lib from "@clusterio/lib";
import { ConnectionClosed, ProtocolError, PolicyViolation, AuthenticationFailed } from "@clusterio/lib";
import { slowTest } from "../common.js";
let tokenHost = jwt.sign({ aud: "host", host: 0 }, Buffer.from("TestSecretDoNotUse", "base64"));
// Valid token for a host id that was never approved
let tokenUnregistered = jwt.sign({ aud: "host", host: 0 }, Buffer.from("TestSecretDoNotUse", "base64"));
describe("Integration of lib/link", function() {
let control;
let controlConnector;
let hostConnector;
// Hosts can only connect once approved, so the host connections use a paired host
let linkHost;
before(async function() {
linkHost = await pairTestHost("link-test");
});
after(async function() {
if (linkHost) {
await getControl().send(new lib.HostRevokeRequest(linkHost.hostId));
}
});
beforeEach(async function() {
controlConnector = new TestControlConnector(url, 0.2);
controlConnector.token = controlToken;
control = new TestControl(controlConnector, false);
hostConnector = new TestHostConnector(url, 0.2);
hostConnector.token = tokenHost;
hostConnector.hostId = 0;
hostConnector.token = linkHost.token;
hostConnector.hostId = linkHost.hostId;
});
afterEach(async function() {
await controlConnector.disconnect();
@@ -138,13 +149,24 @@ describe("Integration of lib/link", function() {
});
it("should refuse connection with mismatched ids", async function() {
hostConnector.hostId = 1;
hostConnector.hostId = linkHost.hostId + 1;
await assert.rejects(
hostConnector.connect(),
new Error("Authentication failed: missmatched host id")
);
});
it("should refuse connection for a host that is not registered", async function() {
hostConnector.token = tokenUnregistered;
hostConnector.hostId = 0;
await assert.rejects(
hostConnector.connect(),
new Error(
"Authentication failed: host is not registered, run the installer again in the host directory"
)
);
});
it("should close old connections from the same address", async function() {
slowTest(this);
const hostConnectorFirst = new TestHostConnector(url, 0.2);
@@ -163,8 +185,8 @@ describe("Integration of lib/link", function() {
it("should refuse duplicate host connections", async function() {
this.skip(); // Can't be checked because the remote address for both connections is the same
hostConnector.token = jwt.sign({ aud: "host", host: 4 }, Buffer.from("TestSecretDoNotUse", "base64"));
hostConnector.hostId = 4; // Same as already running host
hostConnector.token = jwt.sign({ aud: "host", host: hostId }, Buffer.from("TestSecretDoNotUse", "base64"));
hostConnector.hostId = hostId; // Same as already running host
await assert.rejects(
hostConnector.connect(),
new Error("Authentication failed: missmatched host id")
+3 -1
View File
@@ -2,7 +2,7 @@ import assert from "node:assert/strict";
import * as lib from "@clusterio/lib";
import { Host, Instance, InstanceConnection } from "@clusterio/host";
import { ControlConnection, Controller, HostConnection, InstanceRecord } from "@clusterio/controller";
import { ControlConnection, Controller, HostConnection, HostRecord, InstanceRecord } from "@clusterio/controller";
import { MockServer } from "../mock.js";
// Although there are no imports, the file has side-effects such as "before" which can effect tests
@@ -19,6 +19,8 @@ class Control extends lib.Link { }
function connectHost(controller, hostId, plugins) {
const [controllerSide, hostSide] = lib.VirtualConnector.makePair(addr("controller"), addr({ hostId }));
const registerData = new lib.RegisterHostData("", "0.0.0", hostId, plugins);
// Hosts can only connect once approved, which creates their record.
controller.hosts.set(new HostRecord(hostId, `host-${hostId}`, "", new Map()));
const hostConnection = new HostConnection(registerData, controllerSide, controller, "host-a.test");
controller.wsServer.hostConnections.set(hostId, hostConnection);
const hostConfig = new lib.HostConfig("host");
+85
View File
@@ -0,0 +1,85 @@
import assert from "node:assert/strict";
import { HostPairingDetails } from "@clusterio/lib";
import { testMatrix, testRoundTripJsonSerialisable } from "../../common.js";
describe("lib/data/HostPairingDetails", function () {
it("should be round trip serialisable", function () {
testRoundTripJsonSerialisable(HostPairingDetails, testMatrix(
["ABC-DEF"], // code (required)
["New Host", ""], // name (required)
["10.0.0.1", "2001:db8::1"], // remoteAddress (required)
[undefined, "2.0.0"], // version
[undefined, 123456], // createdAtMs
[undefined, 999999], // updatedAtMs
[undefined, true], // isDeleted
));
});
describe("constructor", function () {
it("should set defaults correctly", function () {
const details = new HostPairingDetails("ABC-DEF", "New Host", "10.0.0.1");
assert.equal(details.code, "ABC-DEF");
assert.equal(details.name, "New Host");
assert.equal(details.remoteAddress, "10.0.0.1");
assert.equal(details.version, "");
assert.equal(details.createdAtMs, 0);
assert.equal(details.updatedAtMs, 0);
assert.equal(details.isDeleted, false);
});
it("should use the code as the id", function () {
const details = new HostPairingDetails("ABC-DEF", "New Host", "10.0.0.1");
assert.equal(details.id, "ABC-DEF");
});
});
describe(".toJSON()", function () {
it("should only include required fields when optional fields are defaults", function () {
const details = new HostPairingDetails("ABC-DEF", "New Host", "10.0.0.1", "", 0, 0, false);
assert.deepEqual(details.toJSON(), {
code: "ABC-DEF",
name: "New Host",
remoteAddress: "10.0.0.1",
});
});
it("should include all optional fields when set", function () {
const details = new HostPairingDetails("ABC-DEF", "New Host", "10.0.0.1", "2.0.0", 100, 200, true);
assert.deepEqual(details.toJSON(), {
code: "ABC-DEF",
name: "New Host",
remoteAddress: "10.0.0.1",
version: "2.0.0",
createdAtMs: 100,
updatedAtMs: 200,
isDeleted: true,
});
});
it("should not include the id getter", function () {
const details = new HostPairingDetails("ABC-DEF", "New Host", "10.0.0.1");
assert.equal(Object.hasOwn(JSON.parse(JSON.stringify(details)), "id"), false);
});
});
describe("static fromJSON()", function () {
it("should construct minimal object", function () {
const details = HostPairingDetails.fromJSON({
code: "ABC-DEF",
name: "New Host",
remoteAddress: "10.0.0.1",
});
assert.equal(details.code, "ABC-DEF");
assert.equal(details.version, "");
assert.equal(details.createdAtMs, 0);
assert.equal(details.updatedAtMs, 0);
assert.equal(details.isDeleted, false);
});
});
});
+77
View File
@@ -186,6 +186,83 @@ describe("messages/controller", function() {
});
});
describe("Host pairing", function() {
const details = [
new lib.HostPairingDetails("ABC-DEF", "Host A", "10.0.0.1", "2.0.0", 100, 200),
new lib.HostPairingDetails("GHJ-KMN", "Host B", "2001:db8::1", "", 0, 300, true),
];
it("defines the pairing messages for control links approving hosts", function() {
for (const Message of [
lib.HostPairingListRequest,
lib.HostPairingApproveRequest,
lib.HostPairingRejectRequest,
]) {
assert.equal(Message.type, "request", Message.name);
assert.equal(Message.src, "control", Message.name);
assert.equal(Message.dst, "controller", Message.name);
assert.equal(Message.permission, "core.host.approve", Message.name);
assert(lib.Link._requestsByClass.has(Message), `${Message.name} is not registered`);
}
assert.equal(lib.HostPairingUpdatesEvent.type, "event");
assert.equal(lib.HostPairingUpdatesEvent.src, "controller");
assert.equal(lib.HostPairingUpdatesEvent.dst, "control");
assert.equal(lib.HostPairingUpdatesEvent.permission, "core.host.approve");
assert(lib.Link._eventsByClass.has(lib.HostPairingUpdatesEvent));
});
it("removes the host token messages", function() {
for (const name of ["HostGenerateTokenRequest", "HostConfigCreateRequest", "HostRevokeTokensRequest"]) {
assert.equal(lib[name], undefined, `${name} is still exported`);
assert(!lib.Link._requestsByName.has(name), `${name} is still registered`);
}
});
it("defines the host permissions", function() {
for (const name of ["core.host.approve", "core.host.revoke"]) {
assert(lib.permissions.has(name), `${name} is not defined`);
assert.equal(lib.permissions.get(name).grantByDefault, false, `${name} is granted by default`);
}
for (const name of ["core.host.generate_token", "core.host.revoke_token", "core.host.create_config"]) {
assert.equal(lib.permissions.has(name), false, `${name} is still defined`);
}
});
it("round trips HostPairingListRequest responses", function() {
const validate = lib.compile(lib.HostPairingListRequest.Response.jsonSchema);
const json = JSON.parse(JSON.stringify(details));
assert(validate(json), JSON.stringify(validate.errors));
assert.deepEqual(lib.HostPairingListRequest.Response.fromJSON(json), details);
});
it("round trips HostPairingUpdatesEvent", function() {
testRoundTripJsonSerialisable(lib.HostPairingUpdatesEvent, [[[]], [details]]);
});
it("round trips HostPairingApproveRequest", function() {
testRoundTripJsonSerialisable(lib.HostPairingApproveRequest, testMatrix(
["ABC-DEF", "abcdef"], // code
[undefined, "Renamed"], // name
));
const validate = lib.compile(lib.HostPairingApproveRequest.Response.jsonSchema);
assert(validate(1234));
});
it("round trips HostPairingRejectRequest", function() {
testRoundTripJsonSerialisable(lib.HostPairingRejectRequest, [["ABC-DEF"], ["abcdef"]]);
});
it("approves a pending request", async function() {
controller.config.set("controller.auth_secret", Buffer.from("TestSecretDoNotUse").toString("base64"));
const { requestId, code } = controller.hostPairing.create("Host A", "", "10.0.0.1");
const list = await controlConnection.handleHostPairingListRequest(new lib.HostPairingListRequest());
assert.deepEqual(list.map(entry => entry.code), [code]);
const hostId = await controlConnection.handleHostPairingApproveRequest(
new lib.HostPairingApproveRequest(code)
);
assert.equal(controller.hosts.get(hostId).name, "Host A");
assert.equal(controller.hostPairing.poll(requestId).hostId, hostId);
});
it("rejects a pending request", async function() {
const { requestId, code } = controller.hostPairing.create("Host A", "", "10.0.0.1");
await controlConnection.handleHostPairingRejectRequest(new lib.HostPairingRejectRequest(code));
assert.throws(() => controller.hostPairing.poll(requestId), /Request was rejected/);
});
});
describe("InstanceConfigGetRequest", function() {
it("runs", async function() {
const config = await controlConnection.handleInstanceConfigGetRequest(
+13
View File
@@ -18,6 +18,19 @@ describe("messages/host", function() {
host = new Host(hostConnector, hostConfig, []);
});
describe("HostRevokeRequest", function() {
it("is round trip json serialisable", function() {
testRoundTripJsonSerialisable(lib.HostRevokeRequest, [[1], [2 ** 31 - 1]]);
});
it("is a control request requiring core.host.revoke", function() {
assert.equal(lib.HostRevokeRequest.type, "request");
assert.equal(lib.HostRevokeRequest.src, "control");
assert.equal(lib.HostRevokeRequest.dst, "controller");
assert.equal(lib.HostRevokeRequest.permission, "core.host.revoke");
assert(lib.Link._requestsByClass.has(lib.HostRevokeRequest));
});
});
describe("HostUpdateRequest", function() {
it("runs", async function() {
host.config.set("host.allow_remote_updates", true);
+142
View File
@@ -0,0 +1,142 @@
import assert from "node:assert/strict";
import events from "node:events";
import * as lib from "@clusterio/lib";
import { MockConnector, MockControl } from "../mock.js";
import "../setup_logging.js";
import { HostPairingSubscriber } from "../../packages/web_ui/src/util/host_pairing.ts";
const addr = lib.Address.fromShorthand;
describe("web_ui/util/host_pairing", function() {
describe("class HostPairingSubscriber", function() {
let mockControl;
let permitted;
let subscriber;
beforeEach(function() {
mockControl = new MockControl(new MockConnector(addr({ controlId: 0 }), addr("controller")));
permitted = true;
subscriber = new HostPairingSubscriber(mockControl, () => permitted);
});
function pairing(code, updatedAtMs) {
return new lib.HostPairingDetails(code, `Host ${code}`, "192.0.2.1", "", updatedAtMs, updatedAtMs);
}
async function settle() {
await new Promise(resolve => setImmediate(resolve));
}
function sentRequests() {
return mockControl.connector.sentMessages.filter(message => message.name === "SubscriptionRequest");
}
it("should not send requests on connect or account updates without permission", async function() {
permitted = false;
mockControl.connector.emit("connect");
subscriber.handleConnectionEvent("resume");
subscriber.handleAccountEvent();
await settle();
assert.deepEqual(sentRequests(), []);
});
it("should resubscribe on connect with permission", async function() {
subscriber.subscribe(() => {});
await events.once(mockControl.connector, "send");
mockControl.connector.sentMessages.length = 0;
mockControl.connector.emit("connect");
await settle();
assert.equal(sentRequests().length, 1);
assert.deepEqual(sentRequests()[0].data, new lib.SubscriptionRequest(
lib.HostPairingUpdatesEvent.name, "replace", -1, lib.SubscriptionFilters.all()
));
});
it("should check the permission after the connect handlers have run", async function() {
subscriber.subscribe(() => {});
await events.once(mockControl.connector, "send");
mockControl.connector.sentMessages.length = 0;
permitted = false;
mockControl.connector.emit("connect");
permitted = true;
await settle();
assert.equal(sentRequests().length, 1);
});
it("should resubscribe on account updates with permission", async function() {
subscriber.subscribe(() => {});
await events.once(mockControl.connector, "send");
mockControl.connector.sentMessages.length = 0;
permitted = false;
subscriber.handleAccountEvent();
await settle();
assert.deepEqual(sentRequests(), []);
permitted = true;
subscriber.handleAccountEvent();
await settle();
assert.equal(sentRequests().length, 1);
});
it("should clear requests when a new session starts", async function() {
await subscriber._handleEvent(new lib.HostPairingUpdatesEvent([pairing("ABC-DEF", 10)]));
assert.equal(subscriber.values.size, 1);
const [before] = subscriber.getSnapshot();
mockControl.connector.emit("connect");
assert.equal(subscriber.values.size, 0);
assert.equal(subscriber.lastUpdatedMs, -1);
const [after] = subscriber.getSnapshot();
assert.notEqual(after, before);
assert.equal(after.size, 0);
});
it("should keep requests when a session is resumed", async function() {
await subscriber._handleEvent(new lib.HostPairingUpdatesEvent([pairing("ABC-DEF", 10)]));
subscriber.handleConnectionEvent("drop");
subscriber.handleConnectionEvent("resume");
assert.equal(subscriber.values.size, 1);
assert.equal(subscriber.lastUpdatedMs, 10);
});
it("should clear requests when subscribing again after the last unsubscribe", async function() {
const unsubscribe = subscriber.subscribe(() => {});
await events.once(mockControl.connector, "send");
await subscriber._handleEvent(new lib.HostPairingUpdatesEvent([pairing("ABC-DEF", 10)]));
unsubscribe();
await new Promise(resolve => setTimeout(resolve));
mockControl.connector.sentMessages.length = 0;
subscriber.subscribe(() => {});
assert.equal(subscriber.values.size, 0);
await settle();
assert.equal(sentRequests().length, 1);
assert.equal(sentRequests()[0].data.lastRequestTimeMs, -1);
});
it("should keep requests when a page that subscribes replaces another", async function() {
const unsubscribe = subscriber.subscribe(() => {});
await events.once(mockControl.connector, "send");
await subscriber._handleEvent(new lib.HostPairingUpdatesEvent([pairing("ABC-DEF", 10)]));
unsubscribe();
subscriber.subscribe(() => {});
assert.equal(subscriber.values.size, 1);
assert.equal(subscriber.lastUpdatedMs, 10);
});
it("should set synced false on drop and close", async function() {
subscriber.synced = true;
subscriber.handleConnectionEvent("drop");
assert.equal(subscriber.synced, false);
subscriber.synced = true;
subscriber.handleConnectionEvent("close");
assert.equal(subscriber.synced, false);
});
});
});