mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-19 15: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
@@ -0,0 +1,72 @@
|
||||
import * as lib from "@clusterio/lib";
|
||||
|
||||
/**
|
||||
* Subscriber for the requests from host installers waiting to be approved
|
||||
*
|
||||
* Only accounts that may approve hosts can subscribe to the requests, so
|
||||
* this does not resubscribe on connect or account changes for other
|
||||
* accounts, which would fail with a permission error. The requests only
|
||||
* live in the controller's memory and it does not tell a new session about
|
||||
* requests removed before it started, so the requests are cleared when a
|
||||
* new session starts and fetched again. The same applies to requests removed
|
||||
* while nothing was subscribed, so they are also cleared when subscribing
|
||||
* again after the last handler unsubscribed.
|
||||
*/
|
||||
export class HostPairingSubscriber extends lib.MapSubscriber<lib.HostPairingUpdatesEvent> {
|
||||
private _permitted: () => boolean;
|
||||
private _handlers = 0;
|
||||
|
||||
/**
|
||||
* @param control - Link to the controller.
|
||||
* @param permitted - Returns true if the account may approve hosts.
|
||||
*/
|
||||
constructor(control: lib.Link, permitted: () => boolean) {
|
||||
super(lib.HostPairingUpdatesEvent, control);
|
||||
this._permitted = permitted;
|
||||
}
|
||||
|
||||
override handleConnectionEvent(event: "connect" | "drop" | "resume" | "close") {
|
||||
if (event === "connect") {
|
||||
this.values.clear();
|
||||
this.lastUpdatedMs = -1;
|
||||
}
|
||||
if (event === "connect" || event === "resume") {
|
||||
// The account of a new session is reported after the connect event.
|
||||
queueMicrotask(() => {
|
||||
if (this._permitted()) {
|
||||
super.handleConnectionEvent(event);
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
super.handleConnectionEvent(event);
|
||||
}
|
||||
|
||||
override handleAccountEvent() {
|
||||
if (this._permitted()) {
|
||||
super.handleAccountEvent();
|
||||
}
|
||||
}
|
||||
|
||||
override subscribe(handler: lib.EventSubscriberCallback<lib.HostPairingUpdatesEvent>) {
|
||||
if (this._handlers === 0) {
|
||||
this.values.clear();
|
||||
this.lastUpdatedMs = -1;
|
||||
}
|
||||
this._handlers += 1;
|
||||
const unsubscribe = super.subscribe(handler);
|
||||
let unsubscribed = false;
|
||||
return () => {
|
||||
if (unsubscribed) {
|
||||
return;
|
||||
}
|
||||
unsubscribed = true;
|
||||
unsubscribe();
|
||||
// Deferred like the unsubscribe in EventSubscriber, so moving between pages
|
||||
// that both subscribe keeps the requests.
|
||||
setTimeout(() => {
|
||||
this._handlers -= 1;
|
||||
});
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,16 @@
|
||||
import * as lib from "@clusterio/lib";
|
||||
import packageJson from "../../package.json";
|
||||
import * as WebPlugin from "../BaseWebPlugin";
|
||||
import { HostPairingSubscriber } from "./host_pairing";
|
||||
|
||||
const { logFilter, logger } = lib;
|
||||
|
||||
function rolesHavePermission(roles: lib.AccountRole[] | null, permission: lib.PermissionName) {
|
||||
return (roles ?? []).some(
|
||||
role => role.permissions.includes("core.admin") || role.permissions.includes(permission)
|
||||
);
|
||||
}
|
||||
|
||||
type accountHandler = (account: lib.AccountDetails) => void;
|
||||
type hostHandler = (hostDetails: lib.HostDetails) => void;
|
||||
type instanceHandler = (instanceDetails: lib.InstanceDetails) => void;
|
||||
@@ -67,6 +74,7 @@ export class Control extends lib.Link {
|
||||
/** Updates handled by the subscription service */
|
||||
systems = new lib.MapSubscriber(lib.SystemInfoUpdateEvent, this);
|
||||
hosts = new lib.MapSubscriber(lib.HostUpdatesEvent, this);
|
||||
hostPairings = new HostPairingSubscriber(this, () => rolesHavePermission(this.accountRoles, "core.host.approve"));
|
||||
instances = new lib.MapSubscriber(lib.InstanceDetailsUpdatesEvent, this);
|
||||
saves = new lib.MapSubscriber(lib.InstanceSaveDetailsUpdatesEvent, this);
|
||||
modPacks = new lib.MapSubscriber(lib.ModPackUpdatesEvent, this);
|
||||
|
||||
Reference in new issue
Block a user