diff --git a/unit_tests/falco/test_configuration.cpp b/unit_tests/falco/test_configuration.cpp index 2774487af14..cf07d72e3c3 100644 --- a/unit_tests/falco/test_configuration.cpp +++ b/unit_tests/falco/test_configuration.cpp @@ -138,7 +138,7 @@ TEST(Configuration, configuration_config_files_secondary_fail) std::vector cmdline_config_options; std::vector 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"); @@ -187,7 +187,7 @@ TEST(Configuration, configuration_config_files_ok) std::vector cmdline_config_options; std::vector 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); @@ -260,7 +260,7 @@ TEST(Configuration, configuration_config_files_relative_main) std::vector cmdline_config_options; std::vector 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); @@ -317,7 +317,7 @@ TEST(Configuration, configuration_config_files_override) std::vector cmdline_config_options; std::vector 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); @@ -355,7 +355,7 @@ TEST(Configuration, configuration_config_files_unexistent) std::vector cmdline_config_options; std::vector 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); @@ -393,7 +393,7 @@ TEST(Configuration, configuration_config_files_scalar_configs_files) std::vector cmdline_config_options; std::vector 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); @@ -430,7 +430,7 @@ TEST(Configuration, configuration_config_files_empty_configs_files) std::vector cmdline_config_options; std::vector 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); @@ -462,7 +462,7 @@ TEST(Configuration, configuration_config_files_self) std::vector cmdline_config_options; std::vector 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"); } @@ -516,7 +516,7 @@ TEST(Configuration, configuration_config_files_directory) std::vector cmdline_config_options; std::vector 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. @@ -567,7 +567,7 @@ TEST(Configuration, configuration_config_files_cmdline) std::vector 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); @@ -799,7 +799,7 @@ TEST(Configuration, configuration_webserver_ip) std::vector 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); } @@ -836,6 +836,6 @@ TEST(Configuration, configuration_webserver_ip) std::vector 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)); } } diff --git a/unit_tests/falco/test_configuration_rule_selection.cpp b/unit_tests/falco/test_configuration_rule_selection.cpp index 467d8ebd3fb..725793fdf96 100644 --- a/unit_tests/falco/test_configuration_rule_selection.cpp +++ b/unit_tests/falco/test_configuration_rule_selection.cpp @@ -48,7 +48,7 @@ TEST(ConfigurationRuleSelection, parse_yaml) TEST(ConfigurationRuleSelection, cli_options) { falco_configuration falco_config; - EXPECT_NO_THROW(falco_config.init(std::vector{"rules[].disable.tag=maturity_incubating", "rules[].enable.rule=Adding ssh keys to authorized_keys"})); + EXPECT_NO_THROW(falco_config.init_from_content("", std::vector{"rules[].disable.tag=maturity_incubating", "rules[].enable.rule=Adding ssh keys to authorized_keys"})); ASSERT_EQ(falco_config.m_rules_selection.size(), 2); diff --git a/userspace/falco/app/actions/load_config.cpp b/userspace/falco/app/actions/load_config.cpp index 9f6efd05aad..20bbf22bd6b 100644 --- a/userspace/falco/app/actions/load_config.cpp +++ b/userspace/falco/app/actions/load_config.cpp @@ -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) diff --git a/userspace/falco/configuration.cpp b/userspace/falco/configuration.cpp index 128c2326ad7..9b6c5887dbd 100644 --- a/userspace/falco/configuration.cpp +++ b/userspace/falco/configuration.cpp @@ -78,21 +78,14 @@ falco_configuration::falco_configuration(): { } -void falco_configuration::init(const std::vector& 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& cmdline_options) +void falco_configuration::init_from_content(const std::string& config_content, const std::vector& 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& loaded_conf_files, +void falco_configuration::init_from_file(const std::string& conf_filename, std::vector& loaded_conf_files, const std::vector &cmdline_options) { loaded_conf_files.clear(); diff --git a/userspace/falco/configuration.h b/userspace/falco/configuration.h index 175da09552e..7468590f41d 100644 --- a/userspace/falco/configuration.h +++ b/userspace/falco/configuration.h @@ -107,9 +107,8 @@ class falco_configuration falco_configuration(); virtual ~falco_configuration() = default; - void init(const std::string& conf_filename, std::vector& loaded_conf_files, const std::vector& cmdline_options); - void init(const std::vector& cmdline_options); - void init_from_content(const std::string& config_content, const std::vector& cmdline_options); + void init_from_file(const std::string& conf_filename, std::vector& loaded_conf_files, const std::vector& cmdline_options); + void init_from_content(const std::string& config_content, const std::vector& cmdline_options, const std::string& filename="default"); std::string dump();