mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-17 23:33:12 +09:00
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:
49 files changed
+4855
-473
No files matched your search
+324
-1
@@ -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() {
|
||||
|
||||
Reference in new issue
Block a user