mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-16 15:30:41 +09:00
Add HostManager for host records and host tokens
controller.hosts is now a HostManager wrapping the hosts datastore, the same way controller.users wraps the users datastore. It owns creating hosts (moved from HostPairingManager), revoking them (moved from ControlConnection) and signing and verifying host tokens (moved from Controller, generateHostToken is renamed signHostToken to match signUserToken). UserManager gains verifyUserToken, replacing the copies of the user token check in WsServer and routes. The WsServer copy accepted a token without iat when the user had no tokenValidAfter; the shared check always requires iat, which jsonwebtoken adds by default. Follows Cooldude2606's comment on #1161. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5d1e98dafd
commit
e6554e09c7
20 files changed
+404
-198
No files matched your search
@@ -3,7 +3,7 @@ import crypto from "node:crypto";
|
||||
import jwt from "jsonwebtoken";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { Controller, HostPairingManager, HostPairingError, HostRecord } from "@clusterio/controller";
|
||||
import { Controller, HostPairingManager, HostPairingError } from "@clusterio/controller";
|
||||
import {
|
||||
pollTimeoutMs, maxPendingPerAddress, maxNameLength,
|
||||
} from "@clusterio/controller/dist/node/src/HostPairingManager.js";
|
||||
@@ -109,36 +109,6 @@ describe("controller/src/HostPairingManager", function() {
|
||||
internal(requestId).lastPollMs -= ms;
|
||||
}
|
||||
|
||||
describe(".createHost()", function() {
|
||||
it("should create a host record with a valid token", function() {
|
||||
const before = Math.floor(Date.now() / 1000);
|
||||
const result = 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 not reuse the id of an existing host", function() {
|
||||
controller.hosts.set(new HostRecord(5, "existing", "", new Map()));
|
||||
restoreRandomInt = stubRandomInt([5, 5, 6]);
|
||||
const result = 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();
|
||||
@@ -348,10 +318,10 @@ describe("controller/src/HostPairingManager", function() {
|
||||
() => 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(controller.hosts.records.size, 0);
|
||||
assert.equal(manager.poll(requestId), null);
|
||||
manager.approve(code, undefined, "admin");
|
||||
assert.equal(controller.hosts.size, 1);
|
||||
assert.equal(controller.hosts.records.size, 1);
|
||||
});
|
||||
it("should throw NotFoundError for an unknown code", function() {
|
||||
assert.throws(
|
||||
@@ -363,7 +333,7 @@ describe("controller/src/HostPairingManager", function() {
|
||||
const { code } = manager.create("Host A", "", "10.0.0.1");
|
||||
manager.approve(code, undefined, "admin");
|
||||
assert.throws(() => manager.approve(code, undefined, "admin"), lib.NotFoundError);
|
||||
assert.equal(controller.hosts.size, 1);
|
||||
assert.equal(controller.hosts.records.size, 1);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -373,7 +343,7 @@ describe("controller/src/HostPairingManager", function() {
|
||||
manager.reject(code.toLowerCase(), "admin");
|
||||
assert.equal(manager.records.has(code), false);
|
||||
assert.deepEqual(updates, [{ code, isDeleted: false }, { code, isDeleted: true }]);
|
||||
assert.equal(controller.hosts.size, 0);
|
||||
assert.equal(controller.hosts.records.size, 0);
|
||||
});
|
||||
it("should throw NotFoundError for an unknown code", function() {
|
||||
assert.throws(
|
||||
|
||||
Reference in new issue
Block a user