From de9efcbec784ccb54bb00acf31fd68044b14bdaf Mon Sep 17 00:00:00 2001 From: Federico Di Pierro Date: Tue, 9 Apr 2024 11:08:04 +0200 Subject: [PATCH] new(userspace/falco): allow `--support` to print expanded configuration file. Signed-off-by: Federico Di Pierro --- userspace/falco/app/actions/print_support.cpp | 2 +- userspace/falco/configuration.cpp | 27 ++++++++++--------- userspace/falco/configuration.h | 12 ++++++--- userspace/falco/yaml_helper.h | 7 +++++ 4 files changed, 31 insertions(+), 17 deletions(-) diff --git a/userspace/falco/app/actions/print_support.cpp b/userspace/falco/app/actions/print_support.cpp index 80424584..6e76a8e4 100644 --- a/userspace/falco/app/actions/print_support.cpp +++ b/userspace/falco/app/actions/print_support.cpp @@ -108,7 +108,7 @@ falco::app::run_result falco::app::actions::print_support(falco::app::state& s) support["version"] = infos.falco_version; support["engine_info"] = infos.as_json(); support["cmdline"] = s.cmdline; - support["config"] = read_file(s.options.conf_filename); + support["config"] = s.config->dump(); support["rules_files"] = nlohmann::json::array(); for(const auto& filename : s.config->m_loaded_rules_filenames) { diff --git a/userspace/falco/configuration.cpp b/userspace/falco/configuration.cpp index 50c25b07..2a533751 100644 --- a/userspace/falco/configuration.cpp +++ b/userspace/falco/configuration.cpp @@ -85,16 +85,14 @@ falco_configuration::falco_configuration(): void falco_configuration::init(const std::vector& cmdline_options) { - yaml_helper config; config.load_from_string(""); - init_cmdline_options(config, cmdline_options); - load_yaml("default", config); + init_cmdline_options(cmdline_options); + load_yaml("default"); } void falco_configuration::init(const std::string& conf_filename, std::vector& loaded_conf_files, std::vector& loaded_conf_warnings, const std::vector &cmdline_options) { - yaml_helper config; try { config.load_from_file(conf_filename, loaded_conf_files, loaded_conf_warnings); @@ -104,11 +102,16 @@ void falco_configuration::init(const std::string& conf_filename, std::vector engine_mode_lut = { @@ -177,9 +180,9 @@ void falco_configuration::load_engine_config(const std::string& config_name, con } } -void falco_configuration::load_yaml(const std::string& config_name, const yaml_helper& config) +void falco_configuration::load_yaml(const std::string& config_name) { - load_engine_config(config_name, config); + load_engine_config(config_name); m_log_level = config.get_scalar("log_level", "info"); std::list rules_files; @@ -588,15 +591,15 @@ static bool split(const std::string &str, char delim, std::pair &cmdline_options) +void falco_configuration::init_cmdline_options(const std::vector &cmdline_options) { for(const std::string &option : cmdline_options) { - set_cmdline_option(config, option); + set_cmdline_option(option); } } -void falco_configuration::set_cmdline_option(yaml_helper& config, const std::string &opt) +void falco_configuration::set_cmdline_option(const std::string &opt) { std::pair keyval; diff --git a/userspace/falco/configuration.h b/userspace/falco/configuration.h index 8185eb8e..97d5341b 100644 --- a/userspace/falco/configuration.h +++ b/userspace/falco/configuration.h @@ -89,6 +89,8 @@ public: void init(const std::string& conf_filename, std::vector& loaded_conf_files, std::vector& loaded_conf_warnings, const std::vector& cmdline_options); void init(const std::vector& cmdline_options); + std::string dump(); + static void read_rules_file_directory(const std::string& path, std::list& rules_filenames, std::list &rules_folders); // Rules list as passed by the user @@ -162,11 +164,11 @@ public: gvisor_config m_gvisor = {}; private: - void load_yaml(const std::string& config_name, const yaml_helper& config); + void load_yaml(const std::string& config_name); - void load_engine_config(const std::string& config_name, const yaml_helper& config); + void load_engine_config(const std::string& config_name); - void init_cmdline_options(yaml_helper& config, const std::vector& cmdline_options); + void init_cmdline_options(const std::vector& cmdline_options); /** * Given a = specifier, set the appropriate option @@ -174,7 +176,9 @@ private: * characters for nesting. Currently only 1- or 2- level keys * are supported and only scalar values are supported. */ - void set_cmdline_option(yaml_helper& config, const std::string& spec); + void set_cmdline_option(const std::string& spec); + + yaml_helper config; }; namespace YAML { diff --git a/userspace/falco/yaml_helper.h b/userspace/falco/yaml_helper.h index 82088896..1f245030 100644 --- a/userspace/falco/yaml_helper.h +++ b/userspace/falco/yaml_helper.h @@ -215,6 +215,13 @@ public: return node.IsDefined(); } + std::string dump() const + { + YAML::Emitter emitter; + emitter << YAML::DoubleQuoted << YAML::Flow << YAML::LowerNull << YAML::BeginSeq << m_root; + return emitter.c_str() + 1; // drop initial '[' char + } + private: YAML::Node m_root;