Skip to content

Commit

Permalink
make env variables ignore the env variables of the respective other c…
Browse files Browse the repository at this point in the history
…onfig type
  • Loading branch information
Taepper committed Nov 28, 2024
1 parent 9673d42 commit 0baa238
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
8 changes: 6 additions & 2 deletions include/config/config_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ concept Config = requires(C c, const C cc, const VerifiedConfigSource& config_so
/// pass to exit(): 0 if the user gave --help, 1 in case of erroneous
/// usage (the error is already printed in that case).
template <Config C>
std::variant<C, int32_t> getConfig(std::span<const std::string> cmd) {
std::variant<C, int32_t> getConfig(
std::span<const std::string> cmd,
const std::vector<std::string>& allow_list_for_env_vars
) {
const auto config_specification = C::getConfigSpecification();
try {
auto cmd_source = CommandLineArguments{cmd}.verify(config_specification);
Expand All @@ -74,7 +77,8 @@ std::variant<C, int32_t> getConfig(std::span<const std::string> cmd) {
}

auto env_source =
EnvironmentVariables::decodeEnvironmentVariables().verify(config_specification);
EnvironmentVariables::newWithAllowListAndEnv(allow_list_for_env_vars, environ)
.verify(config_specification);
// Restart from scratch, because the values from cmd_source need
// to shadow the ones from env_source.
config = {};
Expand Down
14 changes: 11 additions & 3 deletions include/config/source/environment_variables.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,25 @@ namespace silo::config {
class EnvironmentVariables : public ConfigSource {
/* EnvironmentVariables base, */
std::vector<std::pair<std::string, std::string>> alist;
std::vector<std::string> allow_list;

explicit EnvironmentVariables(std::vector<std::pair<std::string, std::string>>&& alist_)
: alist(std::move(alist_)){};
explicit EnvironmentVariables(
std::vector<std::pair<std::string, std::string>>&& alist_,
std::vector<std::string> allow_list_
)
: alist(std::move(alist_)),
allow_list(std::move(allow_list_)){};

explicit EnvironmentVariables(){};

public:
[[nodiscard]] VerifiedConfigSource verify(const ConfigSpecification& config_specification
) const override;

static EnvironmentVariables decodeEnvironmentVariables(const char* const* envp = environ);
static EnvironmentVariables newWithAllowListAndEnv(
const std::vector<std::string>& allow_list,
const char* const* envp
);

[[nodiscard]] constexpr std::string_view errorContext() const {
return "environment variables";
Expand Down
20 changes: 13 additions & 7 deletions src/config/source/environment_variables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ std::string toLowerCase(std::string input) {

namespace silo::config {

EnvironmentVariables EnvironmentVariables::decodeEnvironmentVariables(const char* const* envp) {
EnvironmentVariables EnvironmentVariables::newWithAllowListAndEnv(
const std::vector<std::string>& allow_list,
const char* const* envp
) {
std::vector<std::pair<std::string, std::string>> alist;
for (const char* const* current_envp = envp; *current_envp != nullptr; current_envp++) {
const char* env = *current_envp;
Expand All @@ -35,7 +38,7 @@ EnvironmentVariables EnvironmentVariables::decodeEnvironmentVariables(const char
}
}
}
return EnvironmentVariables{std::move(alist)};
return EnvironmentVariables{std::move(alist), allow_list};
}

[[nodiscard]] std::string EnvironmentVariables::configKeyPathToString(
Expand Down Expand Up @@ -100,9 +103,11 @@ AmbiguousConfigKeyPath EnvironmentVariables::stringToConfigKeyPath(
const ConfigValue value = value_specification.getValueFromString(value_string);
config_values.emplace(value_specification.key, value);
} else {
if (key_string == "SILO_PANIC") {
SPDLOG_TRACE(
"allowing env variable {} which is independent of the config system", key_string
if (std::find(allow_list.begin(), allow_list.end(), key_string) != allow_list.end()) {
SPDLOG_INFO(
"Given env variable '{}' is not a valid key for '{}'. (but explicitly allowed)",
key_string,
config_specification.program_name
);
} else {
invalid_config_keys.push_back(key_string);
Expand All @@ -114,10 +119,11 @@ AmbiguousConfigKeyPath EnvironmentVariables::stringToConfigKeyPath(
const std::string_view keys_or_options =
(invalid_config_keys.size() >= 2) ? "variables" : "variable";
throw silo::config::ConfigException(fmt::format(
"in {}: unknown {} {}",
"in {}: unknown {} {} for '{}'",
errorContext(),
keys_or_options,
boost::join(invalid_config_keys, ", ")
boost::join(invalid_config_keys, ", "),
config_specification.program_name
));
}

Expand Down
19 changes: 16 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#include <iostream>
#include <variant>

#include <boost/algorithm/string/join.hpp>
#include <spdlog/spdlog.h>
#include <boost/algorithm/string/join.hpp>

#include "silo/common/overloaded.h"
#include "silo/config/preprocessing_config.h"
Expand Down Expand Up @@ -96,6 +96,19 @@ int mainWhichMayThrowExceptions(int argc, char** argv) {
return 1;
}

std::vector<std::string> env_allow_list;
env_allow_list.emplace_back("SILO_PANIC");
for (auto& field : silo::config::PreprocessingConfig::getConfigSpecification().fields) {
env_allow_list.emplace_back(
silo::config::EnvironmentVariables::configKeyPathToString(field.key)
);
}
for (auto& field : silo::config::RuntimeConfig::getConfigSpecification().fields) {
env_allow_list.emplace_back(
silo::config::EnvironmentVariables::configKeyPathToString(field.key)
);
}

switch (mode) {
case ExecutionMode::PREPROCESSING:
return std::visit(
Expand All @@ -106,7 +119,7 @@ int mainWhichMayThrowExceptions(int argc, char** argv) {
},
[&](int32_t exit_code) { return exit_code; }
},
silo::config::getConfig<silo::config::PreprocessingConfig>(args)
silo::config::getConfig<silo::config::PreprocessingConfig>(args, env_allow_list)
);
case ExecutionMode::API:
return std::visit(
Expand All @@ -117,7 +130,7 @@ int mainWhichMayThrowExceptions(int argc, char** argv) {
},
[&](int32_t exit_code) { return exit_code; }
},
silo::config::getConfig<silo::config::RuntimeConfig>(args)
silo::config::getConfig<silo::config::RuntimeConfig>(args, env_allow_list)
);
}
}
Expand Down

0 comments on commit 0baa238

Please sign in to comment.