mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-20 23:46:14 -04:00
Added env configuration for plugins
* Added env. * Added tree config. * Added tree. * Fixed settings test. * Added test cases.
This commit is contained in:
parent
e61e8ebd9e
commit
bf7cd11b59
3 changed files with 667 additions and 470 deletions
|
@ -27,6 +27,10 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import {MapArrayType} from "../types/MapType";
|
||||||
|
import {SettingsNode, SettingsTree} from "./SettingsTree";
|
||||||
|
import {coerce} from "semver";
|
||||||
|
|
||||||
const absolutePaths = require('./AbsolutePaths');
|
const absolutePaths = require('./AbsolutePaths');
|
||||||
const deepEqual = require('fast-deep-equal/es6');
|
const deepEqual = require('fast-deep-equal/es6');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
@ -50,13 +54,15 @@ const nonSettings = [
|
||||||
|
|
||||||
// This is a function to make it easy to create a new instance. It is important to not reuse a
|
// 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. :(
|
// config object after passing it to log4js.configure() because that method mutates the object. :(
|
||||||
const defaultLogConfig = (level:string) => ({appenders: {console: {type: 'console'}},
|
const defaultLogConfig = (level: string) => ({
|
||||||
|
appenders: {console: {type: 'console'}},
|
||||||
categories: {
|
categories: {
|
||||||
default: {appenders: ['console'], level},
|
default: {appenders: ['console'], level},
|
||||||
}});
|
}
|
||||||
|
});
|
||||||
const defaultLogLevel = 'INFO';
|
const defaultLogLevel = 'INFO';
|
||||||
|
|
||||||
const initLogging = (config:any) => {
|
const initLogging = (config: any) => {
|
||||||
// log4js.configure() modifies exports.logconfig so check for equality first.
|
// log4js.configure() modifies exports.logconfig so check for equality first.
|
||||||
log4js.configure(config);
|
log4js.configure(config);
|
||||||
log4js.getLogger('console');
|
log4js.getLogger('console');
|
||||||
|
@ -487,7 +493,7 @@ exports.getGitCommit = () => {
|
||||||
version = ref;
|
version = ref;
|
||||||
}
|
}
|
||||||
version = version.substring(0, 7);
|
version = version.substring(0, 7);
|
||||||
} catch (e:any) {
|
} catch (e: any) {
|
||||||
logger.warn(`Can't get git version for server header\n${e.message}`);
|
logger.warn(`Can't get git version for server header\n${e.message}`);
|
||||||
}
|
}
|
||||||
return version;
|
return version;
|
||||||
|
@ -503,7 +509,7 @@ exports.getEpVersion = () => require('../../package.json').version;
|
||||||
* This code refactors a previous version that copied & pasted the same code for
|
* This code refactors a previous version that copied & pasted the same code for
|
||||||
* both "settings.json" and "credentials.json".
|
* both "settings.json" and "credentials.json".
|
||||||
*/
|
*/
|
||||||
const storeSettings = (settingsObj:any) => {
|
const storeSettings = (settingsObj: any) => {
|
||||||
for (const i of Object.keys(settingsObj || {})) {
|
for (const i of Object.keys(settingsObj || {})) {
|
||||||
if (nonSettings.includes(i)) {
|
if (nonSettings.includes(i)) {
|
||||||
logger.warn(`Ignoring setting: '${i}'`);
|
logger.warn(`Ignoring setting: '${i}'`);
|
||||||
|
@ -542,7 +548,7 @@ const storeSettings = (settingsObj:any) => {
|
||||||
* short syntax "${ABIWORD}", and not "${ABIWORD:null}": the latter would result
|
* short syntax "${ABIWORD}", and not "${ABIWORD:null}": the latter would result
|
||||||
* in the literal string "null", instead.
|
* in the literal string "null", instead.
|
||||||
*/
|
*/
|
||||||
const coerceValue = (stringValue:string) => {
|
const coerceValue = (stringValue: string) => {
|
||||||
// cooked from https://stackoverflow.com/questions/175739/built-in-way-in-javascript-to-check-if-a-string-is-a-valid-number
|
// cooked from https://stackoverflow.com/questions/175739/built-in-way-in-javascript-to-check-if-a-string-is-a-valid-number
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const isNumeric = !isNaN(stringValue) && !isNaN(parseFloat(stringValue) && isFinite(stringValue));
|
const isNumeric = !isNaN(stringValue) && !isNaN(parseFloat(stringValue) && isFinite(stringValue));
|
||||||
|
@ -554,11 +560,16 @@ const coerceValue = (stringValue:string) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (stringValue) {
|
switch (stringValue) {
|
||||||
case 'true': return true;
|
case 'true':
|
||||||
case 'false': return false;
|
return true;
|
||||||
case 'undefined': return undefined;
|
case 'false':
|
||||||
case 'null': return null;
|
return false;
|
||||||
default: return stringValue;
|
case 'undefined':
|
||||||
|
return undefined;
|
||||||
|
case 'null':
|
||||||
|
return null;
|
||||||
|
default:
|
||||||
|
return stringValue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -598,14 +609,16 @@ const coerceValue = (stringValue:string) => {
|
||||||
*
|
*
|
||||||
* see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter
|
* see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter
|
||||||
*/
|
*/
|
||||||
const lookupEnvironmentVariables = (obj: object) => {
|
const lookupEnvironmentVariables = (obj: MapArrayType<any>) => {
|
||||||
const stringifiedAndReplaced = JSON.stringify(obj, (key, value) => {
|
const replaceEnvs = (obj: MapArrayType<any>) => {
|
||||||
|
for (let [key, value] of Object.entries(obj)) {
|
||||||
/*
|
/*
|
||||||
* the first invocation of replacer() is with an empty key. Just go on, or
|
* the first invocation of replacer() is with an empty key. Just go on, or
|
||||||
* we would zap the entire object.
|
* we would zap the entire object.
|
||||||
*/
|
*/
|
||||||
if (key === '') {
|
if (key === '') {
|
||||||
return value;
|
obj[key] = value;
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -616,10 +629,23 @@ const lookupEnvironmentVariables = (obj: object) => {
|
||||||
* The environment variable expansion syntax "${ENV_VAR}" is just a string
|
* The environment variable expansion syntax "${ENV_VAR}" is just a string
|
||||||
* of specific form, after all.
|
* of specific form, after all.
|
||||||
*/
|
*/
|
||||||
if (typeof value !== 'string') {
|
|
||||||
return value;
|
if(key === 'undefined' || value === undefined) {
|
||||||
|
delete obj[key]
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ((typeof value !== 'string' && typeof value !== 'object') || value === null) {
|
||||||
|
obj[key] = value;
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof obj[key] === "object") {
|
||||||
|
replaceEnvs(obj[key]);
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Let's check if the string value looks like a variable expansion (e.g.:
|
* Let's check if the string value looks like a variable expansion (e.g.:
|
||||||
* "${ENV_VAR}" or "${ENV_VAR:default_value}")
|
* "${ENV_VAR}" or "${ENV_VAR:default_value}")
|
||||||
|
@ -629,8 +655,8 @@ const lookupEnvironmentVariables = (obj: object) => {
|
||||||
|
|
||||||
if (match == null) {
|
if (match == null) {
|
||||||
// no match: use the value literally, without any substitution
|
// no match: use the value literally, without any substitution
|
||||||
|
obj[key] = value;
|
||||||
return value;
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -651,14 +677,16 @@ const lookupEnvironmentVariables = (obj: object) => {
|
||||||
* We have to return null, because if we just returned undefined, the
|
* We have to return null, because if we just returned undefined, the
|
||||||
* configuration item "key" would be stripped from the returned object.
|
* configuration item "key" would be stripped from the returned object.
|
||||||
*/
|
*/
|
||||||
return null;
|
obj[key] = null;
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((envVarValue === undefined) && (defaultValue !== undefined)) {
|
if ((envVarValue === undefined) && (defaultValue !== undefined)) {
|
||||||
logger.debug(`Environment variable "${envVarName}" not found for ` +
|
logger.debug(`Environment variable "${envVarName}" not found for ` +
|
||||||
`configuration key "${key}". Falling back to default value.`);
|
`configuration key "${key}". Falling back to default value.`);
|
||||||
|
|
||||||
return coerceValue(defaultValue);
|
obj[key] = coerceValue(defaultValue);
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// envVarName contained some value.
|
// envVarName contained some value.
|
||||||
|
@ -670,14 +698,42 @@ const lookupEnvironmentVariables = (obj: object) => {
|
||||||
logger.debug(
|
logger.debug(
|
||||||
`Configuration key "${key}" will be read from environment variable "${envVarName}"`);
|
`Configuration key "${key}" will be read from environment variable "${envVarName}"`);
|
||||||
|
|
||||||
return coerceValue(envVarValue!);
|
obj[key] = coerceValue(envVarValue!);
|
||||||
});
|
}
|
||||||
|
return obj
|
||||||
|
}
|
||||||
|
|
||||||
const newSettings = JSON.parse(stringifiedAndReplaced);
|
replaceEnvs(obj);
|
||||||
|
|
||||||
return newSettings;
|
// Add plugin ENV variables
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If the key contains a double underscore, it's a plugin variable
|
||||||
|
* E.g.
|
||||||
|
*/
|
||||||
|
let treeEntries = new Map<string, string | undefined>
|
||||||
|
const root = new SettingsNode("EP")
|
||||||
|
|
||||||
|
for (let [env, envVal] of Object.entries(process.env)) {
|
||||||
|
if (!env.startsWith("EP")) continue
|
||||||
|
treeEntries.set(env, envVal)
|
||||||
|
}
|
||||||
|
treeEntries.forEach((value, key) => {
|
||||||
|
let pathToKey = key.split("__")
|
||||||
|
let currentNode = root
|
||||||
|
let depth = 0
|
||||||
|
depth++
|
||||||
|
currentNode.addChild(pathToKey, value!)
|
||||||
|
})
|
||||||
|
|
||||||
|
//console.log(root.collectFromLeafsUpwards())
|
||||||
|
const rooting = root.collectFromLeafsUpwards()
|
||||||
|
console.log("Rooting is", rooting.ADMIN)
|
||||||
|
obj = Object.assign(obj, rooting)
|
||||||
|
return obj;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* - reads the JSON configuration file settingsFilename from disk
|
* - reads the JSON configuration file settingsFilename from disk
|
||||||
* - strips the comments
|
* - strips the comments
|
||||||
|
@ -686,7 +742,7 @@ const lookupEnvironmentVariables = (obj: object) => {
|
||||||
*
|
*
|
||||||
* The isSettings variable only controls the error logging.
|
* The isSettings variable only controls the error logging.
|
||||||
*/
|
*/
|
||||||
const parseSettings = (settingsFilename:string, isSettings:boolean) => {
|
const parseSettings = (settingsFilename: string, isSettings: boolean) => {
|
||||||
let settingsStr = '';
|
let settingsStr = '';
|
||||||
|
|
||||||
let settingsType, notFoundMessage, notFoundFunction;
|
let settingsType, notFoundMessage, notFoundFunction;
|
||||||
|
@ -718,10 +774,8 @@ const parseSettings = (settingsFilename:string, isSettings:boolean) => {
|
||||||
|
|
||||||
logger.info(`${settingsType} loaded from: ${settingsFilename}`);
|
logger.info(`${settingsType} loaded from: ${settingsFilename}`);
|
||||||
|
|
||||||
const replacedSettings = lookupEnvironmentVariables(settings);
|
return lookupEnvironmentVariables(settings);
|
||||||
|
} catch (e: any) {
|
||||||
return replacedSettings;
|
|
||||||
} catch (e:any) {
|
|
||||||
logger.error(`There was an error processing your ${settingsType} ` +
|
logger.error(`There was an error processing your ${settingsType} ` +
|
||||||
`file from ${settingsFilename}: ${e.message}`);
|
`file from ${settingsFilename}: ${e.message}`);
|
||||||
|
|
||||||
|
@ -814,7 +868,8 @@ exports.reloadSettings = () => {
|
||||||
try {
|
try {
|
||||||
exports.sessionKey = fs.readFileSync(sessionkeyFilename, 'utf8');
|
exports.sessionKey = fs.readFileSync(sessionkeyFilename, 'utf8');
|
||||||
logger.info(`Session key loaded from: ${sessionkeyFilename}`);
|
logger.info(`Session key loaded from: ${sessionkeyFilename}`);
|
||||||
} catch (err) { /* ignored */ }
|
} catch (err) { /* ignored */
|
||||||
|
}
|
||||||
const keyRotationEnabled = exports.cookie.keyRotationInterval && exports.cookie.sessionLifetime;
|
const keyRotationEnabled = exports.cookie.keyRotationInterval && exports.cookie.sessionLifetime;
|
||||||
if (!exports.sessionKey && !keyRotationEnabled) {
|
if (!exports.sessionKey && !keyRotationEnabled) {
|
||||||
logger.info(
|
logger.info(
|
||||||
|
@ -870,4 +925,3 @@ exports.exportedForTestingOnly = {
|
||||||
|
|
||||||
// initially load settings
|
// initially load settings
|
||||||
exports.reloadSettings();
|
exports.reloadSettings();
|
||||||
|
|
||||||
|
|
112
src/node/utils/SettingsTree.ts
Normal file
112
src/node/utils/SettingsTree.ts
Normal file
|
@ -0,0 +1,112 @@
|
||||||
|
import {MapArrayType} from "../types/MapType";
|
||||||
|
|
||||||
|
export class SettingsTree {
|
||||||
|
private children: Map<string, SettingsNode>;
|
||||||
|
constructor() {
|
||||||
|
this.children = new Map();
|
||||||
|
}
|
||||||
|
|
||||||
|
public addChild(key: string, value: string) {
|
||||||
|
this.children.set(key, new SettingsNode(key, value));
|
||||||
|
}
|
||||||
|
|
||||||
|
public removeChild(key: string) {
|
||||||
|
this.children.delete(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public getChild(key: string) {
|
||||||
|
return this.children.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public hasChild(key: string) {
|
||||||
|
return this.children.has(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export class SettingsNode {
|
||||||
|
private readonly key: string;
|
||||||
|
private value: string | number | boolean | null | undefined;
|
||||||
|
private children: MapArrayType<SettingsNode>;
|
||||||
|
|
||||||
|
constructor(key: string, value?: string | number | boolean | null | undefined) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
this.children = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
public addChild(path: string[], value: string) {
|
||||||
|
let currentNode:SettingsNode = this;
|
||||||
|
for (let i = 0; i < path.length; i++) {
|
||||||
|
const key = path[i];
|
||||||
|
/*
|
||||||
|
Skip the current node if the key is the same as the current node's key
|
||||||
|
*/
|
||||||
|
if (key === this.key ) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
If the current node does not have a child with the key, create a new node with the key
|
||||||
|
*/
|
||||||
|
if (!currentNode.hasChild(key)) {
|
||||||
|
currentNode = currentNode.children[key] = new SettingsNode(key, this.coerceValue(value));
|
||||||
|
} else {
|
||||||
|
/*
|
||||||
|
Else move to the child node
|
||||||
|
*/
|
||||||
|
currentNode = currentNode.getChild(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public collectFromLeafsUpwards() {
|
||||||
|
let collected:MapArrayType<any> = {};
|
||||||
|
for (const key in this.children) {
|
||||||
|
const child = this.children[key];
|
||||||
|
if (child.hasChildren()) {
|
||||||
|
collected[key] = child.collectFromLeafsUpwards();
|
||||||
|
} else {
|
||||||
|
collected[key] = child.value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return collected;
|
||||||
|
}
|
||||||
|
|
||||||
|
coerceValue = (stringValue: string) => {
|
||||||
|
// cooked from https://stackoverflow.com/questions/175739/built-in-way-in-javascript-to-check-if-a-string-is-a-valid-number
|
||||||
|
// @ts-ignore
|
||||||
|
const isNumeric = !isNaN(stringValue) && !isNaN(parseFloat(stringValue) && isFinite(stringValue));
|
||||||
|
|
||||||
|
if (isNumeric) {
|
||||||
|
// detected numeric string. Coerce to a number
|
||||||
|
|
||||||
|
return +stringValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (stringValue) {
|
||||||
|
case 'true':
|
||||||
|
return true;
|
||||||
|
case 'false':
|
||||||
|
return false;
|
||||||
|
case 'undefined':
|
||||||
|
return undefined;
|
||||||
|
case 'null':
|
||||||
|
return null;
|
||||||
|
default:
|
||||||
|
return stringValue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
public hasChildren() {
|
||||||
|
return Object.keys(this.children).length > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getChild(key: string) {
|
||||||
|
return this.children[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
public hasChild(key: string) {
|
||||||
|
return this.children[key] !== undefined;
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,7 +7,7 @@ import process from 'process';
|
||||||
|
|
||||||
describe(__filename, function () {
|
describe(__filename, function () {
|
||||||
describe('parseSettings', function () {
|
describe('parseSettings', function () {
|
||||||
let settings:any;
|
let settings: any;
|
||||||
const envVarSubstTestCases = [
|
const envVarSubstTestCases = [
|
||||||
{name: 'true', val: 'true', var: 'SET_VAR_TRUE', want: true},
|
{name: 'true', val: 'true', var: 'SET_VAR_TRUE', want: true},
|
||||||
{name: 'false', val: 'false', var: 'SET_VAR_FALSE', want: false},
|
{name: 'false', val: 'false', var: 'SET_VAR_FALSE', want: false},
|
||||||
|
@ -58,4 +58,35 @@ describe(__filename, function () {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
describe("Parse plugin settings", function () {
|
||||||
|
|
||||||
|
before(async function () {
|
||||||
|
process.env["EP__ADMIN__PASSWORD"] = "test"
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should parse plugin settings', async function () {
|
||||||
|
let settings = parseSettings(path.join(__dirname, 'settings.json'), true);
|
||||||
|
assert.equal(settings.ADMIN.PASSWORD, "test");
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should bundle settings with same path', async function () {
|
||||||
|
process.env["EP__ADMIN__USERNAME"] = "test"
|
||||||
|
let settings = parseSettings(path.join(__dirname, 'settings.json'), true);
|
||||||
|
assert.deepEqual(settings.ADMIN, {PASSWORD: "test", USERNAME: "test"});
|
||||||
|
})
|
||||||
|
|
||||||
|
it("Can set the ep themes", async function () {
|
||||||
|
process.env["EP__ep_themes__default_theme"] = "hacker"
|
||||||
|
let settings = parseSettings(path.join(__dirname, 'settings.json'), true);
|
||||||
|
assert.deepEqual(settings.ep_themes, {"default_theme": "hacker"});
|
||||||
|
})
|
||||||
|
|
||||||
|
it("can set the ep_webrtc settings", async function () {
|
||||||
|
process.env["EP__ep_webrtc__enabled"] = "true"
|
||||||
|
let settings = parseSettings(path.join(__dirname, 'settings.json'), true);
|
||||||
|
assert.deepEqual(settings.ep_webrtc, {"enabled": true});
|
||||||
|
})
|
||||||
|
})
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue