From c7bb18c6dafe0eba0c14ed77ab2b9371bc4c1c3e Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Sun, 6 Jun 2021 02:14:50 -0400 Subject: [PATCH] Settings: Support null and undefined env var substitutions --- CHANGELOG.md | 13 +++++++++++++ settings.json.docker | 23 +++++++++++++++++++++++ settings.json.template | 23 +++++++++++++++++++++++ src/node/utils/Settings.js | 16 ++++++---------- 4 files changed, 65 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7931773fe..5c50a3f63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,19 @@ connections to the MySQL/MariaDB server (by default) instead of 1. This might cause Etherpad to crash with a "ER_CON_COUNT_ERROR: Too many connections" error if your server is configured with a low connection limit. +* Changes to environment variable substitution in `settings.json` (see the + documentation comments in `settings.json.template` for details): + * An environment variable set to the string "null" now becomes `null` instead + of the string "null". Similarly, if the environment variable is unset and + the default value is "null" (e.g., `"${UNSET_VAR:null}"`), the value now + becomes `null` instead of the string "null". It is no longer possible to + produce the string "null" via environment variable substitution. + * An environment variable set to the string "undefined" now causes the setting + to be removed instead of set to the string "undefined". Similarly, if the + environment variable is unset and the default value is "undefined" (e.g., + `"${UNSET_VAR:undefined}"`), the setting is now removed instead of set to + the string "undefined". It is no longer possible to produce the string + "undefined" via environment variable substitution. ### Notable enhancements diff --git a/settings.json.docker b/settings.json.docker index 030ab88f1..9c99975cd 100644 --- a/settings.json.docker +++ b/settings.json.docker @@ -24,6 +24,29 @@ * * This is useful, for example, when running in a Docker container. * + * DETAILED RULES: + * - If the environment variable is set to the string "true" or "false", the + * value becomes Boolean true or false. + * - If the environment variable is set to the string "null", the value + * becomes null. + * - If the environment variable is set to the string "undefined", the setting + * is removed entirely, except when used as the member of an array in which + * case it becomes null. + * - If the environment variable is set to a string representation of a finite + * number, the string is converted to that number. + * - If the environment variable is set to any other string, including the + * empty string, the value is that string. + * - If the environment variable is unset and a default value is provided, the + * value is as if the environment variable was set to the provided default: + * - "${UNSET_VAR:}" becomes the empty string. + * - "${UNSET_VAR:foo}" becomes the string "foo". + * - "${UNSET_VAR:true}" and "${UNSET_VAR:false}" become true and false. + * - "${UNSET_VAR:null}" becomes null. + * - "${UNSET_VAR:undefined}" causes the setting to be removed (or be set + * to null, if used as a member of an array). + * - If the environment variable is unset and no default value is provided, + * the value becomes null. + * * EXAMPLE: * "port": "${PORT:9001}" * "minify": "${MINIFY}" diff --git a/settings.json.template b/settings.json.template index 90a89970c..3469cc7a4 100644 --- a/settings.json.template +++ b/settings.json.template @@ -15,6 +15,29 @@ * * This is useful, for example, when running in a Docker container. * + * DETAILED RULES: + * - If the environment variable is set to the string "true" or "false", the + * value becomes Boolean true or false. + * - If the environment variable is set to the string "null", the value + * becomes null. + * - If the environment variable is set to the string "undefined", the setting + * is removed entirely, except when used as the member of an array in which + * case it becomes null. + * - If the environment variable is set to a string representation of a finite + * number, the string is converted to that number. + * - If the environment variable is set to any other string, including the + * empty string, the value is that string. + * - If the environment variable is unset and a default value is provided, the + * value is as if the environment variable was set to the provided default: + * - "${UNSET_VAR:}" becomes the empty string. + * - "${UNSET_VAR:foo}" becomes the string "foo". + * - "${UNSET_VAR:true}" and "${UNSET_VAR:false}" become true and false. + * - "${UNSET_VAR:null}" becomes null. + * - "${UNSET_VAR:undefined}" causes the setting to be removed (or be set + * to null, if used as a member of an array). + * - If the environment variable is unset and no default value is provided, + * the value becomes null. + * * EXAMPLE: * "port": "${PORT:9001}" * "minify": "${MINIFY}" diff --git a/src/node/utils/Settings.js b/src/node/utils/Settings.js index e5b1636db..27630a046 100644 --- a/src/node/utils/Settings.js +++ b/src/node/utils/Settings.js @@ -509,17 +509,13 @@ const coerceValue = (stringValue) => { return +stringValue; } - // the boolean literal case is easy. - if (stringValue === 'true') { - return true; + switch (stringValue) { + case 'true': return true; + case 'false': return false; + case 'undefined': return undefined; + case 'null': return null; + default: return stringValue; } - - if (stringValue === 'false') { - return false; - } - - // otherwise, return this value as-is - return stringValue; }; /**