Pair hosts when they start instead of in the installer

The installer only writes the controller URL and host name. Running the
host requests to join the cluster if it has not been approved yet, or if
the controller no longer accepts its token, and waits until an admin
approves it. Once approved it saves its id and token and starts as
normal. A rejected host exits with code 8.

clusteriohost run --user-token approves the host right away with the
token of a user allowed to approve hosts. The token is not saved.

This moves the pairing client into the host package, where it uses the
same certificate setup as the host's connection to the controller, and
removes --no-pair and the rerun handling from the installer.
This commit is contained in:
Danielv123 committed 2026-10-30 21:13:14 +01:00
1 parent 37e79d9c41
commit 4a49a198f0
25 files changed
+948 -1131

No files matched your search

+38 -17
View File
@@ -15,7 +15,7 @@ import {
spawnNode, instancesDir, factorioDir, databaseDir, controllerConfigPath,
requiresFactorio, hasFactorio, pairTestHost, writeHostConfig,
} from "./index.js";
import { pairHost, checkHostToken, PairingError } from "../../packages/create/pair.js";
import { pairHost, checkHostToken, PairingError } from "@clusterio/host";
import { externalTest, slowTest } from "../common.js";
import { getTestRconPort } from "./rcon_port.js";
import { ControllerEcho, HostEchoReceived, InstanceEcho } from "../file/test_plugin/messages.js";
@@ -718,7 +718,7 @@ describe("Integration of Clusterio", function() {
describe("host pending, approve and reject", function() {
let pairings = [];
afterEach(async function() {
// Stop installers left waiting by a failed test
// Stop hosts left waiting by a failed test
for (const pairing of pairings) {
pairing.abort.abort();
}
@@ -726,7 +726,7 @@ describe("Integration of Clusterio", function() {
pairings = [];
});
// Start pairing like an installer without a token, returns once the request is pending.
// Start pairing like a host without a user token, returns once the request is pending.
async function requestPairing(name) {
const abort = new AbortController();
let onPending;
@@ -740,7 +740,7 @@ describe("Integration of Clusterio", function() {
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]));
pairing.code = await Promise.race([pending, ended]);
return pairing;
}
@@ -776,12 +776,12 @@ describe("Integration of Clusterio", function() {
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);
assert.equal(await checkHostToken(url, approved.token), true);
await execCtl(`host revoke ${approved.hostId}`);
});
it("should make the installer fail when the request is rejected", async function() {
it("should make the host 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));
@@ -792,7 +792,7 @@ describe("Integration of Clusterio", function() {
assert(!hosts.some(host => host.name === "rejected-host"), "Rejected host was added");
});
it("should remove the request when the installer stops waiting", async function() {
it("should remove the request when the host stops waiting", async function() {
const pairing = await requestPairing("cancelled-host");
pairing.abort.abort();
await assert.rejects(pairing.result, err => err.name === "AbortError");
@@ -836,7 +836,7 @@ describe("Integration of Clusterio", function() {
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);
assert.equal(await checkHostToken(url, pairing.token), true);
await execCtl(`note set host ${pairing.hostId} short-lived`);
const control = getControl();
@@ -848,7 +848,7 @@ describe("Integration of Clusterio", function() {
);
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);
assert.equal(await checkHostToken(url, 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");
});
@@ -880,7 +880,7 @@ describe("Integration of Clusterio", function() {
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() {
it("should stop a running host and have it ask to join again when started", async function() {
slowTest(this);
this.timeout(30000);
const config = "revoke-host-config.json";
@@ -905,13 +905,34 @@ describe("Integration of Clusterio", function() {
}
}
// 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;
});
// Starting it again asks to join the cluster again, and it starts up once approved
const rejoining = await spawnNode(
"revoke-host:", `../../packages/host run --config ${config}`, /Waiting for approval/
);
const exited = events.once(rejoining, "exit");
try {
const started = new Promise(resolve => {
rejoining.stdout.on("data", chunk => {
if (String(chunk).includes("Started host")) {
resolve();
}
});
});
const requests = await getControl().send(new lib.HostPairingListRequest());
const request = requests.find(entry => entry.name === "running-revokee");
assert(request, "Revoked host did not ask to join again");
const newHostId = await getControl().send(new lib.HostPairingApproveRequest(request.code));
assert.notEqual(newHostId, pairing.hostId);
assert.notEqual(
await lib.timeout(started, 10e3, "timeout"), "timeout", "Host did not start once approved"
);
await execCtl(`host revoke ${newHostId}`);
await lib.timeout(exited, 10e3, "timeout");
} finally {
if (rejoining.exitCode === null && rejoining.signalCode === null) {
rejoining.kill("SIGINT");
}
}
});
});
+2 -2
View File
@@ -12,7 +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 { pairHost } from "@clusterio/host";
import "../setup_logging.js";
@@ -276,7 +276,7 @@ export async function pairTestHost(name) {
}
/**
* Write a new host config for a paired host, with the fields the installer sets
* Write a new host config for a paired host, with the fields the installer and pairing set
*
* @param {string} configPath - Path to write the config to.
* @param {{ hostId: number, token: string, name: string }} pairing - Host returned by pairing.
+1 -1
View File
@@ -162,7 +162,7 @@ describe("Integration of lib/link", function() {
await assert.rejects(
hostConnector.connect(),
new Error(
"Authentication failed: host is not registered, run the installer again in the host directory"
"Authentication failed: host is not registered, start the host again to request to join the cluster"
)
);
});