From dd004fea27c900ac2dd73aa032eb6263eb8e9da4 Mon Sep 17 00:00:00 2001 From: Mark Stemm Date: Wed, 6 Sep 2023 17:31:02 -0700 Subject: [PATCH] Use new load_rules() methods to load all rules at once This speeds up rules loading a bit because rules are only compiled once instead of for each rules file. This doesn't change rules validation yet. Validation needs some additional work to handle splitting the (single) load result back into individual results for the json/text based output. Signed-off-by: Mark Stemm --- .../falco/app/actions/load_rules_files.cpp | 49 ++++++++++++------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/userspace/falco/app/actions/load_rules_files.cpp b/userspace/falco/app/actions/load_rules_files.cpp index 49f1b0b7..2a59e288 100644 --- a/userspace/falco/app/actions/load_rules_files.cpp +++ b/userspace/falco/app/actions/load_rules_files.cpp @@ -50,11 +50,25 @@ falco::app::run_result falco::app::actions::load_rules_files(falco::app::state& } std::vector rules_contents; + std::vector rules_filenames; falco::load_result::rules_contents_t rc; + std::string filenames; + + for(auto &filename : s.config->m_loaded_rules_filenames) + { + if(!filenames.empty()) + { + filenames += ", "; + } + + filenames += filename; + + rules_filenames.push_back(filename); + } try { - read_files(s.config->m_loaded_rules_filenames.begin(), - s.config->m_loaded_rules_filenames.end(), + read_files(rules_filenames.begin(), + rules_filenames.end(), rules_contents, rc); } @@ -64,25 +78,22 @@ falco::app::run_result falco::app::actions::load_rules_files(falco::app::state& } std::string err = ""; - for(auto &filename : s.config->m_loaded_rules_filenames) + + falco_logger::log(LOG_INFO, "Loading rules from file(s): " + filenames); + std::unique_ptr res; + + res = s.engine->load_rules(rules_contents, rules_filenames); + + if(!res->successful()) { - falco_logger::log(LOG_INFO, "Loading rules from file " + filename + "\n"); - std::unique_ptr res; + // Return the summary version as the error + err = res->as_string(true, rc); + } - res = s.engine->load_rules(rc.at(filename), filename); - - if(!res->successful()) - { - // Return the summary version as the error - err = res->as_string(true, rc); - break; - } - - // If verbose is true, also print any warnings - if(s.options.verbose && res->has_warnings()) - { - fprintf(stderr, "%s\n", res->as_string(true, rc).c_str()); - } + // If verbose is true, also print any warnings + if(s.options.verbose && res->has_warnings()) + { + fprintf(stderr, "%s\n", res->as_string(true, rc).c_str()); } // note: we have an egg-and-chicken problem here. We would like to check