Add user and permission system

Add users, roles and permissions and rework the authentication to use
separate user and slave tokens for control and slave connections
respectively.

Add commands for listing permissions, roles and users, and creating,
editing and deleting roles and users.
This commit is contained in:
Hornwitser committed 2020-06-15 16:47:15 +02:00
1 parent 2acf57d23d
commit cf6f2cb70a
20 files changed
+1511 -153

No files matched your search

+75 -1
View File
@@ -4,7 +4,9 @@ const path = require("path");
const validateHTML = require('html5-validator');
const parallel = require('mocha.parallel');
const { slowTest, get, exec, sendRcon, controlConfigPath, instancesDir } = require("./index");
const link = require("lib/link");
const { slowTest, get, exec, sendRcon, getControl, controlConfigPath, instancesDir } = require("./index");
describe("Integration of Clusterio", function() {
@@ -182,5 +184,77 @@ describe("Integration of Clusterio", function() {
assert(!await fs.exists(path.join(instancesDir, "test")), "Instance was not deleted");
});
});
describe("list-permissions", function() {
it("runs", async function() {
await exec(`node clusterctl --config ${controlConfigPath} list-permissions`);
});
});
describe("list-roles", function() {
it("runs", async function() {
await exec(`node clusterctl --config ${controlConfigPath} list-roles`);
});
});
describe("create-role", function() {
it("should create the given role", async function() {
await exec(`node clusterctl --config ${controlConfigPath} create-role --name temp --description "A temp role" --permissions core.control.connect`);
let result = await link.messages.listRoles.send(getControl());
let role = result.list.find(role => role.name === "temp");
assert.deepEqual(role, { id: 5, name: "temp", description: "A temp role", permissions: ["core.control.connect"]});
});
});
describe("edit-role", function() {
it("should modify the given role", async function() {
await exec(`node clusterctl --config ${controlConfigPath} edit-role --role temp --name new --description "A new role" --permissions`);
let result = await link.messages.listRoles.send(getControl());
let role = result.list.find(role => role.name === "new");
assert.deepEqual(role, { id: 5, name: "new", description: "A new role", permissions: []});
});
});
describe("delete-role", function() {
it("should modify the given role", async function() {
await exec(`node clusterctl --config ${controlConfigPath} delete-role --role new`);
let result = await link.messages.listRoles.send(getControl());
let role = result.list.find(role => role.name === "new");
assert(!role, "Role was not deleted");
});
});
describe("list-users", function() {
it("runs", async function() {
await exec(`node clusterctl --config ${controlConfigPath} list-users`);
});
});
describe("create-user", function() {
it("should create the given user", async function() {
await exec(`node clusterctl --config ${controlConfigPath} create-user --name temp`);
let result = await link.messages.listUsers.send(getControl());
let user = result.list.find(user => user.name === "temp");
assert(user, "user was not created");
});
});
describe("edit-user-roles", function() {
it("should set the roles on the user", async function() {
await exec(`node clusterctl --config ${controlConfigPath} edit-user-roles --name temp --roles Admin`);
let result = await link.messages.listUsers.send(getControl());
let user = result.list.find(user => user.name === "temp");
assert.deepEqual(user.roles, [0]);
});
});
describe("delete-user", function() {
it("should delete the user", async function() {
await exec(`node clusterctl --config ${controlConfigPath} delete-user --name temp`);
let result = await link.messages.listUsers.send(getControl());
let user = result.list.find(user => user.name === "temp");
assert(!user, "user was note deleted");
});
});
});
});
+13 -12
View File
@@ -54,7 +54,7 @@ let slaveProcess;
let control;
let url = "https://localhost:4443/";
let token = jwt.sign({ id: "api" }, "TestSecretDoNotUse");
let controlToken = jwt.sign({ aud: "user", user: "test" }, "TestSecretDoNotUse");
let instancesDir = path.join("temp", "test", "instances");
let databaseDir = path.join("temp", "test", "databse");
let masterConfigPath = path.join("temp", "test", "master-integration.json");
@@ -71,6 +71,10 @@ async function sendRcon(instanceId, command) {
return response.result;
}
function getControl() {
return control;
}
function spawn(name, cmd, waitFor) {
return new Promise((resolve, reject) => {
console.log(cmd);
@@ -111,20 +115,18 @@ before(async function() {
await exec(`node master --config ${masterConfigPath} config set master.heartbeat_interval 0.25`);
await exec(`node master --config ${masterConfigPath} config set master.connector_shutdown_timeout 2`);
await exec(`node slave --config ${slaveConfigPath} config set slave.id 4`);
await exec(`node slave --config ${slaveConfigPath} config set slave.name slave`);
await exec(`node slave --config ${slaveConfigPath} config set slave.instances_directory ${instancesDir}`);
await exec(`node slave --config ${slaveConfigPath} config set slave.master_url "${url}"`);
await exec(`node slave --config ${slaveConfigPath} config set slave.master_token "${token}"`);
await exec(`node clusterctl --config ${controlConfigPath} control-config set control.master_url "https://localhost:4443/"`);
await exec(`node clusterctl --config ${controlConfigPath} control-config set control.master_token "${token}"`);
await exec(`node master --config ${masterConfigPath} bootstrap create-admin test`);
await exec(`node master --config ${masterConfigPath} bootstrap create-ctl-config test --output ${controlConfigPath}`);
masterProcess = await spawn("master:", `node master --config ${masterConfigPath} run`, "All plugins loaded");
await exec(`node clusterctl --config ${controlConfigPath} create-slave-config --id 4 --name slave --generate-token --output ${slaveConfigPath}`);
await exec(`node slave --config ${slaveConfigPath} config set slave.instances_directory ${instancesDir}`);
slaveProcess = await spawn("slave:", `node slave --config ${slaveConfigPath} run`, "SOCKET | received ready from master");
let controlConnector = new TestControlConnector(url, 2);
controlConnector.token = token;
controlConnector.token = controlToken;
control = new TestControl(controlConnector);
await controlConnector.connect();
});
@@ -157,10 +159,9 @@ module.exports = {
get,
exec,
sendRcon,
control,
getControl,
url,
token,
instancesDir,
databaseDir,
masterConfigPath,
+3 -1
View File
@@ -1,8 +1,10 @@
const assert = require("assert").strict;
const events = require("events");
const jwt = require("jsonwebtoken");
const { TestControlConnector, TestControl, get, exec, url, token } = require("./index");
const { TestControlConnector, TestControl, get, exec, url } = require("./index");
let token = jwt.sign({ aud: "user", user: "test" }, "TestSecretDoNotUse");
describe("Integration of lib/link", function() {
let control;