From 19e9c2f114a4b3b62595baf4f0730189f5685daa Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Wed, 10 Nov 2021 18:41:53 -0500 Subject: [PATCH] adminsettings: Pay attention to `--settings` --- src/node/hooks/express/adminsettings.js | 6 +++--- src/node/utils/Settings.js | 27 ++++++++++++++----------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/node/hooks/express/adminsettings.js b/src/node/hooks/express/adminsettings.js index 7445158d8..792801dc7 100644 --- a/src/node/hooks/express/adminsettings.js +++ b/src/node/hooks/express/adminsettings.js @@ -24,7 +24,7 @@ exports.socketio = (hookName, {io}) => { socket.on('load', async (query) => { let data; try { - data = await fsp.readFile('settings.json', 'utf8'); + data = await fsp.readFile(settings.settingsFilename, 'utf8'); } catch (err) { return console.log(err); } @@ -36,8 +36,8 @@ exports.socketio = (hookName, {io}) => { } }); - socket.on('saveSettings', async (settings) => { - await fsp.writeFile('settings.json', settings); + socket.on('saveSettings', async (newSettings) => { + await fsp.writeFile(settings.settingsFilename, newSettings); socket.emit('saveprogress', 'saved'); }); diff --git a/src/node/utils/Settings.js b/src/node/utils/Settings.js index 1a8bb000e..814601f89 100644 --- a/src/node/utils/Settings.js +++ b/src/node/utils/Settings.js @@ -42,6 +42,12 @@ const _ = require('underscore'); const logger = log4js.getLogger('settings'); +// Exported values that settings.json and credentials.json cannot override. +const nonSettings = [ + 'credentialsFilename', + 'settingsFilename', +]; + // This is a function to make it easy to create a new instance. It is important to not reuse a // config object after passing it to log4js.configure() because that method mutates the object. :( const defaultLogConfig = () => ({appenders: [{type: 'console'}]}); @@ -65,6 +71,8 @@ initLogging(defaultLogLevel, defaultLogConfig()); exports.root = absolutePaths.findEtherpadRoot(); logger.info('All relative paths will be interpreted relative to the identified ' + `Etherpad base dir: ${exports.root}`); +exports.settingsFilename = absolutePaths.makeAbsolute(argv.settings || 'settings.json'); +exports.credentialsFilename = absolutePaths.makeAbsolute(argv.credentials || 'credentials.json'); /** * The app title, visible e.g. in the browser window @@ -489,6 +497,11 @@ exports.getEpVersion = () => require('../../package.json').version; */ const storeSettings = (settingsObj) => { for (const i of Object.keys(settingsObj || {})) { + if (nonSettings.includes(i)) { + logger.warn(`Ignoring setting: '${i}'`); + continue; + } + // test if the setting starts with a lowercase character if (i.charAt(0).search('[a-z]') !== 0) { logger.warn(`Settings should start with a lowercase character: '${i}'`); @@ -708,18 +721,8 @@ const parseSettings = (settingsFilename, isSettings) => { }; exports.reloadSettings = () => { - // Discover where the settings file lives - const settingsFilename = absolutePaths.makeAbsolute(argv.settings || 'settings.json'); - - // Discover if a credential file exists - const credentialsFilename = absolutePaths.makeAbsolute(argv.credentials || 'credentials.json'); - - // try to parse the settings - const settings = parseSettings(settingsFilename, true); - - // try to parse the credentials - const credentials = parseSettings(credentialsFilename, false); - + const settings = parseSettings(exports.settingsFilename, true); + const credentials = parseSettings(exports.credentialsFilename, false); storeSettings(settings); storeSettings(credentials);