From 1b3218df0466a59fd1e9a8df72d97ede6448425e Mon Sep 17 00:00:00 2001 From: Skyler Grey Date: Thu, 24 Aug 2023 14:01:22 +0000 Subject: [PATCH] Add cli option to use settings from env variables Currently [in docker it is possible to do configuration through environment variables](https://col.la/dockercodeconfigviaenv), which works using the start-collabora-online.sh start-collabora-online.pl scripts. This commit lets COOLWSD listen to the same environment variables directly Change-Id: I75762ad620132037523fa82167a3ff17075c7027 Signed-off-by: Skyler Grey --- man/coolwsd.1 | 2 ++ wsd/COOLWSD.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++++++- wsd/COOLWSD.hpp | 2 ++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/man/coolwsd.1 b/man/coolwsd.1 index 1b5fdd10f1..f47556f4d7 100644 --- a/man/coolwsd.1 +++ b/man/coolwsd.1 @@ -30,5 +30,7 @@ coolwsd OPTIONS .PP \fB\-\-nocaps\fR Use a non\-privileged forkit, with increase in security problems. .PP +\fB\-\-use\-env\-vars\fR Override coolwsd.xml config with options from the environment variables described in https://col.la/dockercodeconfigviaenv +.PP .SH "SEE ALSO" coolforkit(1), coolconvert(1), coolconfig(1), coolwsd-systemplate-setup(1), coolmount(1) diff --git a/wsd/COOLWSD.cpp b/wsd/COOLWSD.cpp index 046b18fe29..6f911f4537 100644 --- a/wsd/COOLWSD.cpp +++ b/wsd/COOLWSD.cpp @@ -901,6 +901,7 @@ bool COOLWSD::NoSeccomp = false; bool COOLWSD::AdminEnabled = true; bool COOLWSD::UnattendedRun = false; bool COOLWSD::SignalParent = false; +bool COOLWSD::UseEnvVarOptions = false; std::string COOLWSD::RouteToken; #if ENABLE_DEBUG bool COOLWSD::SingleKit = false; @@ -2232,7 +2233,9 @@ void COOLWSD::innerInitialize(Application& self) } } - // Override any settings passed on the command-line. + // Override any settings passed on the command-line or via environment variables + if (UseEnvVarOptions) + initializeEnvOptions(); AutoPtr overrideConfig(new AppConfigMap(_overrideSettings)); conf.addWriteable(overrideConfig, PRIO_APPLICATION); // Highest priority @@ -2998,6 +3001,16 @@ void COOLWSD::defineOptions(OptionSet& optionSet) .required(false) .repeatable(false)); + optionSet.addOption(Option("use-env-vars", "", + "Use the environment variables defined on " + "https://sdk.collaboraonline.com/docs/installation/" + "CODE_Docker_image.html#setting-the-application-configuration-" + "dynamically-via-environment-variables to set options. " + "'DONT_GEN_SSL_CERT' is forcibly enabled and 'extra_params' is " + "ignored even when using this option.") + .required(false) + .repeatable(false)); + #if ENABLE_DEBUG optionSet.addOption(Option("unitlib", "", "Unit testing library path.") .required(false) @@ -3066,6 +3079,8 @@ void COOLWSD::handleOption(const std::string& optionName, LoTemplate = value; else if (optionName == "signal") SignalParent = true; + else if (optionName == "use-env-vars") + UseEnvVarOptions = true; #if ENABLE_DEBUG else if (optionName == "unitlib") @@ -3096,6 +3111,45 @@ void COOLWSD::handleOption(const std::string& optionName, #endif } +void COOLWSD::initializeEnvOptions() +{ + int n = 0; + char* aliasGroup; + while ((aliasGroup = std::getenv(("aliasgroup" + std::to_string(n + 1)).c_str())) != nullptr) + { + bool first = true; + std::istringstream aliasGroupStream; + aliasGroupStream.str(aliasGroup); + for (std::string alias; std::getline(aliasGroupStream, alias, ',');) + { + if (first) + { + _overrideSettings["storage.wopi.alias_groups.group[" + std::to_string(n) + + "].host"] = alias; + first = false; + } + else + { + _overrideSettings["storage.wopi.alias_groups.group[" + std::to_string(n) + + "].alias"] = alias; + } + } + + n++; + } + if (n >= 1) + { + _overrideSettings["alias_groups[@mode]"] = "groups"; + } + + char* optionValue; + if ((optionValue = std::getenv("username")) != nullptr) _overrideSettings["admin_console.username"] = optionValue; + if ((optionValue = std::getenv("password")) != nullptr) _overrideSettings["admin_console.password"] = optionValue; + if ((optionValue = std::getenv("server_name")) != nullptr) _overrideSettings["server_name"] = optionValue; + if ((optionValue = std::getenv("dictionaries")) != nullptr) _overrideSettings["allowed_languages"] = optionValue; + if ((optionValue = std::getenv("remoteconfigurl")) != nullptr) _overrideSettings["remote_config.remote_url"] = optionValue; +} + #if !MOBILEAPP void COOLWSD::displayHelp() diff --git a/wsd/COOLWSD.hpp b/wsd/COOLWSD.hpp index ed788a5fe1..ed610b3468 100644 --- a/wsd/COOLWSD.hpp +++ b/wsd/COOLWSD.hpp @@ -249,6 +249,7 @@ public: static bool AdminEnabled; static bool UnattendedRun; //< True when run from an unattended test, not interactive. static bool SignalParent; + static bool UseEnvVarOptions; static std::string RouteToken; #if ENABLE_DEBUG static bool SingleKit; @@ -518,6 +519,7 @@ protected: void defineOptions(Poco::Util::OptionSet& options) override; void handleOption(const std::string& name, const std::string& value) override; + void initializeEnvOptions(); int main(const std::vector& args) override; /// Handle various global static destructors.