From 2a7bc75929c6bb96f2e25a0b3e3f4ff8f2927f8b Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Wed, 7 Oct 2020 15:49:35 +0200 Subject: [PATCH] Move generation of auth secret to master startup Move the code generating the master authentication secret from the inital value function of the master.auth_secret config entry to the master startup. Having the authentication secret generated in the config library poses issues with using the config library in browser. --- packages/lib/config/definitions.js | 10 +--------- packages/master/master.js | 11 +++++++++-- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/packages/lib/config/definitions.js b/packages/lib/config/definitions.js index 7e4b25d6..45e86672 100644 --- a/packages/lib/config/definitions.js +++ b/packages/lib/config/definitions.js @@ -1,9 +1,6 @@ // Core definitions for the configuration system "use strict"; -const util = require("util"); -const crypto = require("crypto"); -const path = require("path"); const fs = require("fs-extra"); const classes = require("./classes"); @@ -76,12 +73,7 @@ MasterGroup.define({ +" Should be a long string of random letters and numbers." +" Do not share this.", type: "string", - initial_value: async function() { - console.log("Generating new master authentication secret"); - let asyncRandomBytes = util.promisify(crypto.randomBytes); - let bytes = await asyncRandomBytes(256); - return bytes.toString("base64"); - }, + optional: true, }); MasterGroup.define({ name: "heartbeat_interval", diff --git a/packages/master/master.js b/packages/master/master.js index e3832fba..70179711 100755 --- a/packages/master/master.js +++ b/packages/master/master.js @@ -29,6 +29,7 @@ const yargs = require("yargs"); const WebSocket = require("ws"); const JSZip = require("jszip"); const version = require("./package").version; +const util = require("util"); // ugly globals let masterConfig; @@ -1595,6 +1596,14 @@ async function startServer() { } } + if (!masterConfig.get("master.auth_secret")) { + console.log("Generating new master authentication secret"); + let asyncRandomBytes = util.promisify(crypto.randomBytes); + let bytes = await asyncRandomBytes(256); + masterConfig.set("master.auth_secret", bytes.toString("base64")); + await fs.outputFile(masterConfigPath, JSON.stringify(masterConfig.serialize(), null, 4)); + } + let command = args._[0]; if (command === "config") { await config.handleConfigCommand(args, masterConfig, masterConfigPath); @@ -1642,8 +1651,6 @@ async function startServer() { } } - // Save config in case the auth_secret was generated during this invocation. - await fs.outputFile(masterConfigPath, JSON.stringify(masterConfig.serialize(), null, 4)); return; }