Address review of host pairing

Revoking a host now unassigns its instances instead of being refused
while it has any, and removes its system metrics.

Approving and revoking no longer force a save. Hosts are saved by
autosave and on exit like everything else, which also removes the
approving state from pairing requests.

The approval code and link are logged as warnings so they show up in
the systemd service, which only prints warnings and above. The pairing
limits are now the controller.host_pairing_timeout and
controller.host_pairing_max_pending config values, 0 removes the limit.

Subscribers take an optional permitted function, used for the pairing
requests instead of overrides in a subclass.
This commit is contained in:
Danielv123 committed 2026-10-31 16:31:45 +01:00
1 parent e5701a8dae
commit 49a0b27d29
29 files changed
+356 -418

No files matched your search

+22 -34
View File
@@ -326,16 +326,9 @@ describe("controller/src/ControlConnection", function() {
await connection.handleHostRevokeRequest(new lib.HostRevokeRequest(hostId));
}
it("deletes and saves the host record", async function() {
let savedWithoutHost;
const originalSave = controller.hosts.save;
controller.hosts.save = async function() {
savedWithoutHost = !this.has(10);
return await originalSave.call(this);
};
it("deletes the host record", async function() {
await revoke();
assert.equal(controller.hosts.has(10), false);
assert.equal(savedWithoutHost, true, "hosts were not saved after deleting the record");
assert.deepEqual(broadcasts, [{ id: 10, isDeleted: true }]);
assert.equal(audits.length, 1);
assert.match(audits[0], /User admin revoked host host-10 \(10\)/);
@@ -356,15 +349,6 @@ describe("controller/src/ControlConnection", function() {
await revoke();
assert.equal(terminated, true);
});
it("terminates the connection of the host when saving fails", async function() {
let terminated = false;
controller.wsServer.hostConnections.set(10, { connector: { terminate() { terminated = true; } } });
controller.hosts.save = async () => { throw new Error("EPERM: operation not permitted"); };
await assert.rejects(revoke(), /EPERM/);
assert.equal(terminated, true, "revoked host kept its connection");
assert.equal(controller.hosts.has(10), false);
assert.equal(controller.notes.get("host/10"), undefined);
});
it("does not leave a host record behind when the connection closes", async function() {
const connector = new MockConnector(addr("controller"), addr({ hostId: 10 }));
connector.terminate = () => { connector.emit("close"); };
@@ -378,27 +362,31 @@ describe("controller/src/ControlConnection", function() {
assert.equal(controller.hosts.has(10), false, "host record was recreated");
assert.deepEqual(broadcasts.at(-1), { id: 10, isDeleted: true });
});
it("refuses to revoke a host with assigned instances", async function() {
controller.setNote("host", "10", "rack 3", "someone");
let terminated = false;
controller.wsServer.hostConnections.set(10, { connector: { terminate() { terminated = true; } } });
it("unassigns the instances of the host without waiting for it", async function() {
let closing = false;
controller.wsServer.hostConnections.set(10, {
connector: { terminate() { closing = true; }, get closing() { return closing; } },
send: () => new Promise(() => {}),
});
assignInstance(101, "alpha", 10);
assignInstance(102, "beta", 10);
assignInstance(201, "gamma", 20);
await assert.rejects(
revoke(),
new lib.ConflictError("Cannot revoke host host-10 while instances are assigned to it: alpha, beta"),
);
assert.equal(controller.hosts.get(10), record);
assert.equal(controller.notes.get("host/10").isDeleted, false);
assert.equal(terminated, false);
assert.deepEqual(broadcasts, []);
});
it("ignores instances assigned to other hosts", async function() {
assignInstance(201, "gamma", 20);
await revoke();
assert.equal(controller.hosts.has(10), false);
for (const id of [101, 102]) {
const instance = controller.instances.get(id);
assert.equal(instance.config.get("instance.assigned_host"), null);
assert.equal(instance.status, "unassigned");
}
assert.equal(controller.instances.get(201).config.get("instance.assigned_host"), 20);
});
it("removes the system metrics of the host", async function() {
controller.systems.set(await lib.gatherSystemInfo(10, false, false, 0));
controller.systems.set(await lib.gatherSystemInfo("controller", false, false, 0));
await revoke();
assert.equal(controller.systems.has(10), false);
assert.equal(controller.systems.has("controller"), true);
});
it("throws NotFoundError for an unknown host", async function() {
await assert.rejects(revoke(99), new lib.NotFoundError("Unknown host id (99)"));
@@ -421,7 +409,7 @@ describe("controller/src/ControlConnection", function() {
const rejected = controller.hostPairing.create("Host C", "", "10.0.0.3");
const pending = controller.hostPairing.create("Host D", "", "10.0.0.4");
controller.hostPairing._bySecret.get(expired.requestId).lastPollMs -= 60e3;
await controller.hostPairing.approve(approved.code, undefined, "admin");
controller.hostPairing.approve(approved.code, undefined, "admin");
controller.hostPairing.reject(rejected.code, "admin");
const list = await connection.handleHostPairingListRequest(new lib.HostPairingListRequest());
@@ -440,7 +428,7 @@ describe("controller/src/ControlConnection", function() {
it("rejects a request", async function() {
const { requestId, code } = controller.hostPairing.create("Host A", "", "10.0.0.1");
await connection.handleHostPairingRejectRequest(new lib.HostPairingRejectRequest(code.toLowerCase()));
assert.equal(controller.hostPairing.requests.has(code), false);
assert.equal(controller.hostPairing.records.has(code), false);
assert.throws(() => controller.hostPairing.poll(requestId), /Request was rejected/);
assert.equal(controller.hosts.size, 0);
assert(audits.some(msg => msg.startsWith(`User admin rejected host Host A with code ${code}`)));
+70 -115
View File
@@ -5,8 +5,7 @@ import jwt from "jsonwebtoken";
import * as lib from "@clusterio/lib";
import { Controller, HostPairingManager, HostPairingError, HostRecord } from "@clusterio/controller";
import {
pollTimeoutMs, maxPendingMs, maxPending, maxPendingPerAddress,
maxNameLength,
pollTimeoutMs, maxPendingPerAddress, maxNameLength,
} from "@clusterio/controller/dist/node/src/HostPairingManager.js";
import "../setup_logging.js";
@@ -31,12 +30,6 @@ function stubRandomInt(values) {
return () => { crypto.randomInt = original; };
}
function deferred() {
let resolve, reject;
const promise = new Promise((res, rej) => { resolve = res; reject = rej; });
return { promise, resolve, reject };
}
function assertPairingError(fn, status, message) {
assert.throws(fn, err => {
assert(err instanceof HostPairingError, `expected HostPairingError, got ${err}`);
@@ -95,7 +88,7 @@ describe("controller/src/HostPairingManager", function() {
controller = makeController();
manager = controller.hostPairing;
updates = [];
manager.requests.on("update", changed => {
manager.records.on("update", changed => {
updates.push(...changed.map(details => ({ code: details.code, isDeleted: details.isDeleted })));
});
restoreRandomInt = undefined;
@@ -111,15 +104,15 @@ describe("controller/src/HostPairingManager", function() {
return manager._bySecret.get(requestId);
}
// Pretend the installer last polled the request ms milliseconds earlier.
// Pretend the host last polled the request ms milliseconds earlier.
function idle(requestId, ms) {
internal(requestId).lastPollMs -= ms;
}
describe(".createHost()", function() {
it("should create a host record with a valid token", async function() {
it("should create a host record with a valid token", function() {
const before = Math.floor(Date.now() / 1000);
const result = await manager.createHost("Host A");
const result = manager.createHost("Host A");
assert.equal(result.name, "Host A");
assert(Number.isInteger(result.hostId), "hostId is not an integer");
@@ -136,31 +129,10 @@ describe("controller/src/HostPairingManager", function() {
assert.equal(payload.host, result.hostId);
assert(payload.iat >= record.tokenValidAfter, "token is not valid for the new record");
});
it("should save the hosts before returning", async function() {
const save = deferred();
let saveCalls = 0;
controller.hosts.save = () => { saveCalls += 1; return save.promise; };
let done = false;
const promise = manager.createHost("Host A").then(result => { done = true; return result; });
await new Promise(resolve => setImmediate(resolve));
assert.equal(saveCalls, 1);
assert.equal(done, false, "createHost returned before the hosts were saved");
save.resolve();
const result = await promise;
assert(controller.hosts.has(result.hostId));
});
it("should remove the record if saving fails", async function() {
restoreRandomInt = stubRandomInt([1234]);
controller.hosts.save = async () => { throw new Error("disk full"); };
await assert.rejects(manager.createHost("Host A"), /disk full/);
assert.equal(controller.hosts.has(1234), false);
assert.equal(controller.hosts.size, 0);
});
it("should not reuse the id of an existing host", async function() {
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 = await manager.createHost("Host A");
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");
@@ -177,7 +149,7 @@ describe("controller/src/HostPairingManager", function() {
assert.match(code, codePattern);
assert.notEqual(requestId, code);
const details = manager.requests.get(code);
const details = manager.records.get(code);
assert(details instanceof lib.HostPairingDetails);
assert.equal(details.code, code);
assert.equal(details.name, "Host A");
@@ -191,23 +163,23 @@ describe("controller/src/HostPairingManager", function() {
});
it("should create unique codes", function() {
const codes = new Set();
for (let i = 0; i < maxPending; i++) {
for (let i = 0; i < 50; i++) {
const { code } = manager.create(`Host ${i}`, "", `10.1.${i}.1`);
assert.match(code, codePattern);
codes.add(code);
}
assert.equal(codes.size, maxPending);
assert.equal(codes.size, 50);
});
it("should not reuse the code of a pending request", function() {
restoreRandomInt = stubRandomInt([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]);
assert.equal(manager.create("Host A", "", "10.0.0.1").code, "222-222");
assert.equal(manager.create("Host B", "", "10.0.0.2").code, "333-333");
});
it("should not reuse the code of an approved request not yet picked up", async function() {
it("should not reuse the code of an approved request not yet picked up", function() {
restoreRandomInt = stubRandomInt([0, 0, 0, 0, 0, 0, 1234, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]);
const { code } = manager.create("Host A", "", "10.0.0.1");
assert.equal(code, "222-222");
assert.equal(await manager.approve(code, undefined, "admin"), 1234);
assert.equal(manager.approve(code, undefined, "admin"), 1234);
assert.equal(manager.create("Host B", "", "10.0.0.2").code, "333-333");
});
});
@@ -227,7 +199,7 @@ describe("controller/src/HostPairingManager", function() {
});
// Other addresses are not affected
manager.create("Host", "", "10.0.0.2");
assert.equal(manager.requests.size, maxPendingPerAddress + 1);
assert.equal(manager.records.size, maxPendingPerAddress + 1);
});
it("should count IPv4 mapped addresses as the IPv4 address", function() {
manager.create("Host", "", "10.0.0.1");
@@ -245,7 +217,7 @@ describe("controller/src/HostPairingManager", function() {
);
manager.create("Host", "", "2001:db8:0:2::1");
});
it("should free the slot when a request is no longer pending", async function() {
it("should free the slot when a request is no longer pending", function() {
const first = manager.create("Host", "", "10.0.0.1");
const second = manager.create("Host", "", "10.0.0.1");
const third = manager.create("Host", "", "10.0.0.1");
@@ -256,14 +228,15 @@ describe("controller/src/HostPairingManager", function() {
assertPairingError(() => manager.create("Host", "", "10.0.0.1"), 429);
// Approved and rejected results waiting to be picked up do not count
await manager.approve(second.code, undefined, "admin");
manager.approve(second.code, undefined, "admin");
manager.reject(third.code, "admin");
manager.create("Host", "", "10.0.0.1");
manager.create("Host", "", "10.0.0.1");
assertPairingError(() => manager.create("Host", "", "10.0.0.1"), 429);
assert(manager.requests.has(fourth.code));
assert(manager.records.has(fourth.code));
});
it("should limit the total number of pending requests", function() {
const maxPending = controller.config.get("controller.host_pairing_max_pending");
assert.equal(maxPending, 50);
for (let i = 0; i < maxPending; i++) {
manager.create(`Host ${i}`, "", `10.1.${i}.1`);
@@ -275,7 +248,14 @@ describe("controller/src/HostPairingManager", function() {
assert.equal(err.message, "Too many pending requests");
return true;
});
assert.equal(manager.requests.size, maxPending);
assert.equal(manager.records.size, maxPending);
});
it("should not limit the total number of pending requests when set to 0", function() {
controller.config.set("controller.host_pairing_max_pending", 0);
for (let i = 0; i < 60; i++) {
manager.create(`Host ${i}`, "", `10.1.${i}.1`);
}
assert.equal(manager.records.size, 60);
});
});
@@ -292,9 +272,9 @@ describe("controller/src/HostPairingManager", function() {
const { code } = manager.create("Host A", "", "10.0.0.1");
assertPairingError(() => manager.poll(code), 404);
});
it("should hand over an approved result once", async function() {
it("should hand over an approved result once", function() {
const { requestId, code } = manager.create("Host A", "", "10.0.0.1");
const hostId = await manager.approve(code, undefined, "admin");
const hostId = manager.approve(code, undefined, "admin");
const result = manager.poll(requestId);
assert.deepEqual(Object.keys(result).sort(), ["hostId", "name", "token"]);
assert.equal(result.hostId, hostId);
@@ -317,7 +297,7 @@ describe("controller/src/HostPairingManager", function() {
it("should remove a pending request", function() {
const { requestId, code } = manager.create("Host A", "", "10.0.0.1");
manager.cancel(requestId);
assert.equal(manager.requests.has(code), false);
assert.equal(manager.records.has(code), false);
assertPairingError(() => manager.poll(requestId), 404);
assert.deepEqual(updates, [{ code, isDeleted: false }, { code, isDeleted: true }]);
assert.throws(() => manager.reject(code, "admin"), lib.NotFoundError);
@@ -325,9 +305,9 @@ describe("controller/src/HostPairingManager", function() {
it("should ignore unknown requests", function() {
manager.cancel("invalid");
});
it("should not throw away an approved result", async function() {
it("should not throw away an approved result", function() {
const { requestId, code } = manager.create("Host A", "", "10.0.0.1");
const hostId = await manager.approve(code, undefined, "admin");
const hostId = manager.approve(code, undefined, "admin");
manager.cancel(requestId);
assert.equal(manager.poll(requestId).hostId, hostId);
});
@@ -340,92 +320,58 @@ describe("controller/src/HostPairingManager", function() {
});
describe(".approve()", function() {
it("should create the host with the requested name", async function() {
it("should create the host with the requested name", function() {
const { code } = manager.create("Host A", "", "10.0.0.1");
const hostId = await manager.approve(code, undefined, "admin");
const hostId = manager.approve(code, undefined, "admin");
assert.equal(controller.hosts.get(hostId).name, "Host A");
assert.equal(manager.requests.has(code), false);
assert.equal(manager.records.has(code), false);
assert.deepEqual(updates, [{ code, isDeleted: false }, { code, isDeleted: true }]);
});
it("should accept codes in any case and without the dash", async function() {
it("should accept codes in any case and without the dash", function() {
const { code } = manager.create("Host A", "", "10.0.0.1");
const hostId = await manager.approve(code.toLowerCase().replace("-", ""), undefined, "admin");
const hostId = manager.approve(code.toLowerCase().replace("-", ""), undefined, "admin");
assert(controller.hosts.has(hostId));
});
it("should use and trim the name given", async function() {
it("should use and trim the name given", function() {
const { requestId, code } = manager.create("Host A", "", "10.0.0.1");
const hostId = await manager.approve(code, " Renamed ", "admin");
const hostId = manager.approve(code, " Renamed ", "admin");
assert.equal(controller.hosts.get(hostId).name, "Renamed");
assert.equal(manager.poll(requestId).name, "Renamed");
});
it("should reject an invalid name and keep the request pending", async function() {
it("should reject an invalid name and keep the request pending", function() {
const { requestId, code } = manager.create("Host A", "", "10.0.0.1");
await assert.rejects(
manager.approve(code, "", "admin"),
assert.throws(
() => manager.approve(code, "", "admin"),
new lib.ValidationError("Host name is required"),
);
await assert.rejects(
manager.approve(code, "x".repeat(maxNameLength + 1), "admin"),
assert.throws(
() => 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(manager.poll(requestId), null);
await manager.approve(code, undefined, "admin");
manager.approve(code, undefined, "admin");
assert.equal(controller.hosts.size, 1);
});
it("should throw NotFoundError for an unknown code", async function() {
await assert.rejects(
manager.approve("ZZZ-ZZZ", undefined, "admin"),
it("should throw NotFoundError for an unknown code", function() {
assert.throws(
() => manager.approve("ZZZ-ZZZ", undefined, "admin"),
new lib.NotFoundError("No pending host request with code ZZZ-ZZZ"),
);
});
it("should only approve a request once", async function() {
it("should only approve a request once", function() {
const { code } = manager.create("Host A", "", "10.0.0.1");
await manager.approve(code, undefined, "admin");
await assert.rejects(manager.approve(code, undefined, "admin"), lib.NotFoundError);
manager.approve(code, undefined, "admin");
assert.throws(() => manager.approve(code, undefined, "admin"), lib.NotFoundError);
assert.equal(controller.hosts.size, 1);
});
it("should not create two hosts when approved twice at the same time", async function() {
const save = deferred();
controller.hosts.save = () => save.promise;
const { requestId, code } = manager.create("Host A", "", "10.0.0.1");
const first = manager.approve(code, undefined, "admin");
await assert.rejects(manager.approve(code, undefined, "admin"), lib.NotFoundError);
assert.throws(() => manager.reject(code, "admin"), lib.NotFoundError);
// The request stays while approving, the installer sees it as pending
manager.cancel(requestId);
idle(requestId, pollTimeoutMs + 1);
manager.prune();
assert.equal(manager.poll(requestId), null);
assert(manager.requests.has(code), "request removed while approving");
save.resolve();
const hostId = await first;
assert.equal(controller.hosts.size, 1);
assert.equal(manager.poll(requestId).hostId, hostId);
});
it("should keep the request pending if creating the host fails", async function() {
const { requestId, code } = manager.create("Host A", "", "10.0.0.1");
const originalSave = controller.hosts.save;
controller.hosts.save = async () => { throw new Error("disk full"); };
await assert.rejects(manager.approve(code, undefined, "admin"), /disk full/);
assert.equal(controller.hosts.size, 0);
assert(manager.requests.has(code));
assert.equal(manager.poll(requestId), null);
controller.hosts.save = originalSave;
const hostId = await manager.approve(code, undefined, "admin");
assert.equal(manager.poll(requestId).hostId, hostId);
});
});
describe(".reject()", function() {
it("should remove the request from the pending list", function() {
const { code } = manager.create("Host A", "", "10.0.0.1");
manager.reject(code.toLowerCase(), "admin");
assert.equal(manager.requests.has(code), false);
assert.equal(manager.records.has(code), false);
assert.deepEqual(updates, [{ code, isDeleted: false }, { code, isDeleted: true }]);
assert.equal(controller.hosts.size, 0);
});
@@ -435,9 +381,9 @@ describe("controller/src/HostPairingManager", function() {
new lib.NotFoundError("No pending host request with code ZZZ-ZZZ"),
);
});
it("should not reject an approved request", async function() {
it("should not reject an approved request", function() {
const { requestId, code } = manager.create("Host A", "", "10.0.0.1");
const hostId = await manager.approve(code, undefined, "admin");
const hostId = manager.approve(code, undefined, "admin");
assert.throws(() => manager.reject(code, "admin"), lib.NotFoundError);
assert.equal(manager.poll(requestId).hostId, hostId);
});
@@ -455,11 +401,11 @@ describe("controller/src/HostPairingManager", function() {
lib.logger.warn = originalWarn;
});
it("should expire a request the installer stopped polling", function() {
it("should expire a request the host stopped polling", function() {
const { requestId, code } = manager.create("Host A", "", "10.0.0.1");
idle(requestId, pollTimeoutMs + 1);
manager.prune();
assert.equal(manager.requests.has(code), false);
assert.equal(manager.records.has(code), false);
assert.deepEqual(updates, [{ code, isDeleted: false }, { code, isDeleted: true }]);
assertPairingError(() => manager.poll(requestId), 404);
assert.throws(() => manager.reject(code, "admin"), lib.NotFoundError);
@@ -472,22 +418,31 @@ describe("controller/src/HostPairingManager", function() {
assert.equal(manager.poll(requestId), null);
idle(requestId, pollTimeoutMs - 1000);
manager.prune();
assert(manager.requests.has(code));
assert(manager.records.has(code));
});
it("should expire a request that has been pending for too long", function() {
assert.equal(maxPendingMs, 60 * 60e3);
const timeoutMs = controller.config.get("controller.host_pairing_timeout") * 1000;
assert.equal(timeoutMs, 60 * 60e3);
const { requestId, code } = manager.create("Host A", "", "10.0.0.1");
internal(requestId).details.createdAtMs -= maxPendingMs - 1000;
internal(requestId).details.createdAtMs -= timeoutMs - 1000;
manager.prune();
assert.equal(manager.poll(requestId), null);
internal(requestId).details.createdAtMs -= 1001;
manager.prune();
assert.equal(manager.requests.has(code), false);
assert.equal(manager.records.has(code), false);
assertPairingError(() => manager.poll(requestId), 404);
});
it("should drop and warn about an approved host never picked up", async function() {
it("should not limit how long a request is pending when the timeout is 0", function() {
controller.config.set("controller.host_pairing_timeout", 0);
const { requestId, code } = manager.create("Host A", "", "10.0.0.1");
const hostId = await manager.approve(code, undefined, "admin");
internal(requestId).details.createdAtMs -= 365 * 24 * 60 * 60e3;
manager.prune();
assert(manager.records.has(code));
assert.equal(manager.poll(requestId), null);
});
it("should drop and warn about an approved host never picked up", function() {
const { requestId, code } = manager.create("Host A", "", "10.0.0.1");
const hostId = manager.approve(code, undefined, "admin");
idle(requestId, pollTimeoutMs + 1);
manager.prune();
assertPairingError(() => manager.poll(requestId), 404);
@@ -526,7 +481,7 @@ describe("controller/src/HostPairingManager", function() {
assert(event instanceof lib.HostPairingUpdatesEvent);
assert.deepEqual(event.updates.map(details => details.code), [code]);
const lastUpdate = manager.requests.get(code).updatedAtMs;
const lastUpdate = manager.records.get(code).updatedAtMs;
assert.equal(await controller.handleHostPairingSubscription(
new lib.SubscriptionRequest(lib.HostPairingUpdatesEvent.name, true, lastUpdate)
), null);
+13 -13
View File
@@ -254,7 +254,7 @@ describe("controller/src/routes", function() {
}
function assertNothingCreated() {
assert.equal(controller.hostPairing.requests.size, 0, "a pending request was created");
assert.equal(controller.hostPairing.records.size, 0, "a pending request was created");
assert.equal(controller.hosts.size, 0, "a host was created");
}
@@ -267,7 +267,7 @@ describe("controller/src/routes", function() {
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);
const details = controller.hostPairing.records.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/);
@@ -275,12 +275,12 @@ describe("controller/src/routes", function() {
});
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);
let details = controller.hostPairing.records.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);
details = controller.hostPairing.records.get(code);
assert.equal(details.version, "");
});
it("should respond with 400 for a missing or invalid name", async function() {
@@ -316,7 +316,7 @@ describe("controller/src/routes", function() {
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);
assert.equal(controller.hostPairing.records.size, 3);
});
it("should respond with 503 in recovery mode", async function() {
controller.recoveryMode = true;
@@ -344,7 +344,7 @@ describe("controller/src/routes", function() {
assert.equal(payload.host, body.hostId);
}
assert.equal(controller.hosts.size, 3);
assert.equal(controller.hostPairing.requests.size, 0, "a pending request was created");
assert.equal(controller.hostPairing.records.size, 0, "a pending request was created");
});
it("should validate the name before approving directly", async function() {
await assertResponse(
@@ -393,7 +393,7 @@ describe("controller/src/routes", function() {
});
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 hostId = controller.hostPairing.approve(code, "Renamed", "test");
const response = await post("/poll", { requestId });
assert.equal(response.status, 200);
@@ -438,14 +438,14 @@ describe("controller/src/routes", 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);
assert.equal(controller.hostPairing.records.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");
assert(controller.hostPairing.records.has(code), "cancelled by the code");
});
});
@@ -470,7 +470,7 @@ describe("controller/src/routes", function() {
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");
const { hostId, token } = controller.hostPairing.createHost("Host A");
for (const headers of [
{ "Authorization": `Bearer ${token}` },
{ "X-Access-Token": token },
@@ -480,18 +480,18 @@ describe("controller/src/routes", function() {
assert(controller.hosts.has(hostId));
});
it("should respond with 200 for a host token from before the slave rename", async function() {
const { hostId } = await controller.hostPairing.createHost("Host A");
const { hostId } = controller.hostPairing.createHost("Host A");
const token = jwt.sign({ aud: "slave", slave: hostId }, controller.authSecret);
await assertResponse(await check({ "Authorization": `Bearer ${token}` }), 200, { valid: true });
});
it("should respond with 401 without a valid host token", async function() {
await controller.hostPairing.createHost("Host A");
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 once the host is revoked", async function() {
const { hostId, token } = await controller.hostPairing.createHost("Host A");
const { hostId, token } = 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);