Skip to content

Commit

Permalink
update(config): split init_from_content from init_from_file
Browse files Browse the repository at this point in the history
Signed-off-by: Luca Guerra <[email protected]>
  • Loading branch information
LucaGuerra authored and poiana committed May 14, 2024
1 parent f6ae8c8 commit abf82f6
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 28 deletions.
24 changes: 12 additions & 12 deletions unit_tests/falco/test_configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ TEST(Configuration, configuration_config_files_secondary_fail)
std::vector<std::string> cmdline_config_options;
std::vector<std::string> loaded_conf_files;
falco_configuration falco_config;
ASSERT_ANY_THROW(falco_config.init("main.yaml", loaded_conf_files, cmdline_config_options));
ASSERT_ANY_THROW(falco_config.init_from_file("main.yaml", loaded_conf_files, cmdline_config_options));

std::filesystem::remove("main.yaml");
std::filesystem::remove("conf_2.yaml");
Expand Down Expand Up @@ -187,7 +187,7 @@ TEST(Configuration, configuration_config_files_ok)
std::vector<std::string> cmdline_config_options;
std::vector<std::string> loaded_conf_files;
falco_configuration falco_config;
ASSERT_NO_THROW(falco_config.init("main.yaml", loaded_conf_files, cmdline_config_options));
ASSERT_NO_THROW(falco_config.init_from_file("main.yaml", loaded_conf_files, cmdline_config_options));

// main + conf_2 + conf_3
ASSERT_EQ(loaded_conf_files.size(), 3);
Expand Down Expand Up @@ -260,7 +260,7 @@ TEST(Configuration, configuration_config_files_relative_main)
std::vector<std::string> cmdline_config_options;
std::vector<std::string> loaded_conf_files;
falco_configuration falco_config;
ASSERT_NO_THROW(falco_config.init(temp_main.string(), loaded_conf_files, cmdline_config_options));
ASSERT_NO_THROW(falco_config.init_from_file(temp_main.string(), loaded_conf_files, cmdline_config_options));

// main + conf_2 + conf_3
ASSERT_EQ(loaded_conf_files.size(), 3);
Expand Down Expand Up @@ -317,7 +317,7 @@ TEST(Configuration, configuration_config_files_override)
std::vector<std::string> cmdline_config_options;
std::vector<std::string> loaded_conf_files;
falco_configuration falco_config;
ASSERT_NO_THROW(falco_config.init("main.yaml", loaded_conf_files, cmdline_config_options));
ASSERT_NO_THROW(falco_config.init_from_file("main.yaml", loaded_conf_files, cmdline_config_options));

// main + conf_2 + conf_3
ASSERT_EQ(loaded_conf_files.size(), 3);
Expand Down Expand Up @@ -355,7 +355,7 @@ TEST(Configuration, configuration_config_files_unexistent)
std::vector<std::string> cmdline_config_options;
std::vector<std::string> loaded_conf_files;
falco_configuration falco_config;
ASSERT_NO_THROW(falco_config.init("main.yaml", loaded_conf_files, cmdline_config_options));
ASSERT_NO_THROW(falco_config.init_from_file("main.yaml", loaded_conf_files, cmdline_config_options));

// main
ASSERT_EQ(loaded_conf_files.size(), 1);
Expand Down Expand Up @@ -393,7 +393,7 @@ TEST(Configuration, configuration_config_files_scalar_configs_files)
std::vector<std::string> cmdline_config_options;
std::vector<std::string> loaded_conf_files;
falco_configuration falco_config;
ASSERT_NO_THROW(falco_config.init("main.yaml", loaded_conf_files, cmdline_config_options));
ASSERT_NO_THROW(falco_config.init_from_file("main.yaml", loaded_conf_files, cmdline_config_options));

// main + conf_2
ASSERT_EQ(loaded_conf_files.size(), 2);
Expand Down Expand Up @@ -430,7 +430,7 @@ TEST(Configuration, configuration_config_files_empty_configs_files)
std::vector<std::string> cmdline_config_options;
std::vector<std::string> loaded_conf_files;
falco_configuration falco_config;
ASSERT_NO_THROW(falco_config.init("main.yaml", loaded_conf_files, cmdline_config_options));
ASSERT_NO_THROW(falco_config.init_from_file("main.yaml", loaded_conf_files, cmdline_config_options));

// main
ASSERT_EQ(loaded_conf_files.size(), 1);
Expand Down Expand Up @@ -462,7 +462,7 @@ TEST(Configuration, configuration_config_files_self)
std::vector<std::string> cmdline_config_options;
std::vector<std::string> loaded_conf_files;
falco_configuration falco_config;
ASSERT_ANY_THROW(falco_config.init("main.yaml", loaded_conf_files, cmdline_config_options));
ASSERT_ANY_THROW(falco_config.init_from_file("main.yaml", loaded_conf_files, cmdline_config_options));

std::filesystem::remove("main.yaml");
}
Expand Down Expand Up @@ -516,7 +516,7 @@ TEST(Configuration, configuration_config_files_directory)
std::vector<std::string> cmdline_config_options;
std::vector<std::string> loaded_conf_files;
falco_configuration falco_config;
ASSERT_NO_THROW(falco_config.init("main.yaml", loaded_conf_files, cmdline_config_options));
ASSERT_NO_THROW(falco_config.init_from_file("main.yaml", loaded_conf_files, cmdline_config_options));

// main + conf_2 + conf_3.
// test/foo is not parsed.
Expand Down Expand Up @@ -567,7 +567,7 @@ TEST(Configuration, configuration_config_files_cmdline)

std::vector<std::string> loaded_conf_files;
falco_configuration falco_config;
ASSERT_NO_THROW(falco_config.init("main.yaml", loaded_conf_files, cmdline_config_options));
ASSERT_NO_THROW(falco_config.init_from_file("main.yaml", loaded_conf_files, cmdline_config_options));

// main + conf_2
ASSERT_EQ(loaded_conf_files.size(), 2);
Expand Down Expand Up @@ -799,7 +799,7 @@ TEST(Configuration, configuration_webserver_ip)
std::vector<std::string> cmdline_config_options;
cmdline_config_options.push_back(option);

EXPECT_NO_THROW(falco_config.init(cmdline_config_options));
EXPECT_NO_THROW(falco_config.init_from_content("", cmdline_config_options));

ASSERT_EQ(falco_config.m_webserver_config.m_listen_address, address);
}
Expand Down Expand Up @@ -836,6 +836,6 @@ TEST(Configuration, configuration_webserver_ip)
std::vector<std::string> cmdline_config_options;
cmdline_config_options.push_back(option);

EXPECT_ANY_THROW(falco_config.init(cmdline_config_options));
EXPECT_ANY_THROW(falco_config.init_from_content("", cmdline_config_options));
}
}
2 changes: 1 addition & 1 deletion unit_tests/falco/test_configuration_rule_selection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ TEST(ConfigurationRuleSelection, parse_yaml)
TEST(ConfigurationRuleSelection, cli_options)
{
falco_configuration falco_config;
EXPECT_NO_THROW(falco_config.init(std::vector<std::string>{"rules[].disable.tag=maturity_incubating", "rules[].enable.rule=Adding ssh keys to authorized_keys"}));
EXPECT_NO_THROW(falco_config.init_from_content("", std::vector<std::string>{"rules[].disable.tag=maturity_incubating", "rules[].enable.rule=Adding ssh keys to authorized_keys"}));

ASSERT_EQ(falco_config.m_rules_selection.size(), 2);

Expand Down
4 changes: 2 additions & 2 deletions userspace/falco/app/actions/load_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ falco::app::run_result falco::app::actions::load_config(const falco::app::state&
{
if (!s.options.conf_filename.empty())
{
s.config->init(s.options.conf_filename, loaded_conf_files, s.options.cmdline_config_options);
s.config->init_from_file(s.options.conf_filename, loaded_conf_files, s.options.cmdline_config_options);
}
else
{
// Is possible to have an empty config file when we want to use some command line
// options like `--help`, `--version`, ...
// The configs used in `load_yaml` will be initialized to the default values.
s.config->init(s.options.cmdline_config_options);
s.config->init_from_content("", s.options.cmdline_config_options);
}
}
catch (std::exception& e)
Expand Down
13 changes: 3 additions & 10 deletions userspace/falco/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,14 @@ falco_configuration::falco_configuration():
{
}

void falco_configuration::init(const std::vector<std::string>& cmdline_options)
{
config.load_from_string("");
init_cmdline_options(cmdline_options);
load_yaml("default");
}

void falco_configuration::init_from_content(const std::string& config_content, const std::vector<std::string>& cmdline_options)
void falco_configuration::init_from_content(const std::string& config_content, const std::vector<std::string>& cmdline_options, const std::string& filename)
{
config.load_from_string(config_content);
init_cmdline_options(cmdline_options);
load_yaml("default");
load_yaml(filename);
}

void falco_configuration::init(const std::string& conf_filename, std::vector<std::string>& loaded_conf_files,
void falco_configuration::init_from_file(const std::string& conf_filename, std::vector<std::string>& loaded_conf_files,
const std::vector<std::string> &cmdline_options)
{
loaded_conf_files.clear();
Expand Down
5 changes: 2 additions & 3 deletions userspace/falco/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,8 @@ class falco_configuration
falco_configuration();
virtual ~falco_configuration() = default;

void init(const std::string& conf_filename, std::vector<std::string>& loaded_conf_files, const std::vector<std::string>& cmdline_options);
void init(const std::vector<std::string>& cmdline_options);
void init_from_content(const std::string& config_content, const std::vector<std::string>& cmdline_options);
void init_from_file(const std::string& conf_filename, std::vector<std::string>& loaded_conf_files, const std::vector<std::string>& cmdline_options);
void init_from_content(const std::string& config_content, const std::vector<std::string>& cmdline_options, const std::string& filename="default");

std::string dump();

Expand Down

0 comments on commit abf82f6

Please sign in to comment.