mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-13 15:14:52 +09:00
Approve standalone hosts like any other host
A standalone install no longer adds its host to the controller database by signing a token and writing hosts.json itself. The host asks to join when it first starts and is approved once with the link it prints.
This commit is contained in:
7 files changed
+16
-227
No files matched your search
+5
-42
@@ -1,11 +1,9 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { execFile } from "node:child_process";
|
||||
import crypto from "node:crypto";
|
||||
import fs from "node:fs/promises";
|
||||
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";
|
||||
|
||||
@@ -145,13 +143,10 @@ describe("create installer CLI", function() {
|
||||
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, "");
|
||||
callback(null, "", "");
|
||||
});
|
||||
return { stdout, stderr };
|
||||
};
|
||||
@@ -206,28 +201,17 @@ describe("create installer CLI", function() {
|
||||
assert((await commands()).includes(hostSet("host.controller_url", controllerUrl)));
|
||||
});
|
||||
|
||||
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,
|
||||
}));
|
||||
it("leaves joining the cluster to the host of a standalone install", async function() {
|
||||
await fs.writeFile(path.join(directory, "config-controller.json"), "{}");
|
||||
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");
|
||||
assert.match(stdout, /Start the host with run-host/);
|
||||
|
||||
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`,
|
||||
@@ -239,28 +223,7 @@ describe("create installer CLI", function() {
|
||||
]) {
|
||||
assert(recorded.includes(expected), `missing ${expected}`);
|
||||
}
|
||||
});
|
||||
|
||||
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);
|
||||
assert(!recorded.some(command => command.includes("host.controller_token")));
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,98 +0,0 @@
|
||||
import assert from "node:assert/strict";
|
||||
import crypto from "node:crypto";
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import jwt from "jsonwebtoken";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
import HostRecord from "@clusterio/controller/dist/node/src/HostRecord.js";
|
||||
import { createLocalHost } from "../../packages/create/local_host.js";
|
||||
|
||||
describe("create/local_host createLocalHost()", function() {
|
||||
let directory;
|
||||
let secret;
|
||||
beforeEach(async function() {
|
||||
directory = await fs.mkdtemp(path.join(os.tmpdir(), "clusterio-local-host-"));
|
||||
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/);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user