Files
factorio-clusterio/test/messages/host.test.js
T
Danielv123 87d4f5c64e 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.
2026-10-30 06:11:32 +01:00

191 lines
6.7 KiB
JavaScript

import assert from "node:assert/strict";
import * as lib from "@clusterio/lib";
import { Host } from "@clusterio/host";
import { testMatrix, testRoundTripJsonSerialisable } from "../common.js";
import "../setup_logging.js";
describe("messages/host", function() {
/** @type {Host} */
let host;
beforeEach(function() {
const hostConfig = new lib.HostConfig("host");
const hostConnector = new lib.VirtualConnector(
lib.Address.fromShorthand({ hostId: 1 }),
lib.Address.fromShorthand("controller"),
);
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);
await host.handleHostUpdateRequest(new lib.HostUpdateRequest());
});
it("rejects if updates are disabled", async function() {
host.config.set("host.allow_remote_updates", false);
await assert.rejects(
host.handleHostUpdateRequest(new lib.HostUpdateRequest()),
/Remote updates are disabled on this machine/
);
});
});
describe("HostMarkRestartRequiredRequest", function() {
it("runs", async function() {
await host.handleHostMarkRestartRequiredRequest(new lib.HostMarkRestartRequiredRequest());
assert.equal(host.config.restartRequired, true);
});
});
describe("HostConfigGetRequest", function() {
it("runs", async function() {
const config = await host.handleHostConfigGetRequest(new lib.HostConfigGetRequest());
assert.deepEqual(config, host.config.toRemote("control"));
});
});
describe("HostConfigSetFieldRequest", function() {
it("is round trip json serialisable", function() {
testRoundTripJsonSerialisable(lib.HostConfigSetFieldRequest, testMatrix(
["controller.name", "invalid"], // field
["Foo Bar", "true", "5", "{}"], // value
));
});
it("runs", async function() {
await host.handleHostConfigSetFieldRequest(new lib.HostConfigSetFieldRequest("host.name", "Foo Bar"));
assert.equal(host.config.get("host.name"), "Foo Bar");
});
it("rejects if the field does not exist", async function() {
await assert.rejects(
host.handleHostConfigSetFieldRequest(new lib.HostConfigSetFieldRequest("invalid", "Foo Bar")),
/No field named 'invalid'/
);
});
it("rejects if the field is inaccessible", async function() {
await assert.rejects(
host.handleHostConfigSetFieldRequest(new lib.HostConfigSetFieldRequest("host.version", "2.0.0")),
/Field 'host.version' is not accessible from control/
);
});
it("rejects setting 'host.id'", async function() {
await assert.rejects(
host.handleHostConfigSetFieldRequest(new lib.HostConfigSetFieldRequest("host.id", 5)),
/Setting 'host.id' while host is running is not supported/
);
});
});
describe("HostConfigSetPropRequest", function() {
it("is round trip json serialisable", function() {
testRoundTripJsonSerialisable(lib.HostConfigSetPropRequest, testMatrix(
["controller.name", "invalid"], // field
["foo", "bar"], // prop
["Foo Bar", true, 5, {}], // value
));
});
it("runs", async function() {
this.skip(); // No object fields on host
});
it("rejects if the field does not exist", async function() {
await assert.rejects(
host.handleHostConfigSetPropRequest(new lib.HostConfigSetPropRequest("invalid", "foo", "bar")),
/No field named 'invalid'/
);
});
it("rejects if the field is not an object", async function() {
await assert.rejects(
host.handleHostConfigSetPropRequest(new lib.HostConfigSetPropRequest("host.name", "foo", "bar")),
/Cannot set property on non-object field 'host\.name'/
);
});
it("rejects if the field is inaccessible", async function() {
await assert.rejects(
host.handleHostConfigSetPropRequest(new lib.HostConfigSetPropRequest("host.version", "foo", "bar")),
/Field 'host\.version' is not accessible from control/
);
});
});
describe("HostConfigSetRequest", function() {
it("is round trip json serialisable", function() {
testRoundTripJsonSerialisable(lib.HostConfigSetRequest, testMatrix(
[ // fields
{ "foo": "bar", "baz": { "cat": "dog" } },
{ "foo": "5", "baz": { "cat": "5" } },
{ "foo": "true", "baz": { "cat": "true" } },
{ "foo": "{}", "baz": { "cat": "{}" } },
],
));
});
it("runs", async function() {
await host.handleHostConfigSetRequest(new lib.HostConfigSetRequest({
"host.name": "Foo Bar",
"host.factorio_directory": "Bar Baz",
}));
assert.equal(host.config.get("host.name"), "Foo Bar");
assert.equal(host.config.get("host.factorio_directory"), "Bar Baz");
});
it("rejects if the field does not exist", async function() {
await assert.rejects(
host.handleHostConfigSetRequest(new lib.HostConfigSetRequest({
"host.name": "Foo Bar",
"invalid": "Bar Baz",
})),
/No field named 'invalid'/
);
assert.equal(host.config.get("host.name"), "New Host");
});
it("rejects if the field is not an object", async function() {
const prevDir = host.config.get("host.factorio_directory");
await assert.rejects(
host.handleHostConfigSetRequest(new lib.HostConfigSetRequest({
"host.name": "Foo Bar",
"host.factorio_directory": {
"foo": "bar",
},
})),
/Cannot set property on non-object field 'host\.factorio_directory'/
);
assert.equal(host.config.get("host.name"), "New Host");
assert.equal(host.config.get("host.factorio_directory"), prevDir);
});
it("rejects if the field is inaccessible", async function() {
const prevVersion = host.config.get("host.version");
await assert.rejects(
host.handleHostConfigSetRequest(new lib.HostConfigSetRequest({
"host.name": "Foo Bar",
"host.version": "2.0.0",
})),
/Field 'host\.version' is not accessible from control/
);
assert.equal(host.config.get("host.name"), "New Host");
assert.equal(host.config.get("host.version"), prevVersion);
});
it("rejects setting 'host.id'", async function() {
await assert.rejects(
host.handleHostConfigSetRequest(new lib.HostConfigSetRequest({
"host.name": "Foo Bar",
"host.id": 5,
})),
/Setting 'host.id' while host is running is not supported/
);
assert.equal(host.config.get("host.name"), "New Host");
});
});
});