From f5985720f1330042197a2ebdc8fda5d7efff3e27 Mon Sep 17 00:00:00 2001 From: Jason Dellaluce Date: Thu, 2 Nov 2023 17:37:19 +0000 Subject: [PATCH] fix(userspace/engine): cache latest rules compilation output Signed-off-by: Jason Dellaluce --- userspace/engine/falco_engine.cpp | 30 +++++++++++++----------------- userspace/engine/falco_engine.h | 3 +++ 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/userspace/engine/falco_engine.cpp b/userspace/engine/falco_engine.cpp index 2c5e5df7873..34374b98a05 100644 --- a/userspace/engine/falco_engine.cpp +++ b/userspace/engine/falco_engine.cpp @@ -197,8 +197,8 @@ std::unique_ptr falco_engine::load_rules(const std::string &rules_c if (reader.read(cfg, m_rule_collector)) { // compile the definitions (resolve macro/list refs, exceptions, ...) - rule_loader::compiler::compile_output out; - rule_loader::compiler().compile(cfg, m_rule_collector, out); + m_last_compile_output = std::make_unique(); + rule_loader::compiler().compile(cfg, m_rule_collector, *m_last_compile_output.get()); // clear the rules known by the engine and each ruleset m_rules.clear(); @@ -208,7 +208,7 @@ std::unique_ptr falco_engine::load_rules(const std::string &rules_c } // add rules to the engine and the rulesets - for (const auto& rule : out.rules) + for (const auto& rule : m_last_compile_output->rules) { // skip the rule if below the minimum priority if (rule.priority > m_min_priority) @@ -517,6 +517,13 @@ template inline Json::Value sequence_to_json_array(const T& seq) void falco_engine::describe_rule(std::string *rule, const std::vector>& plugins, bool json) const { + // use previously-loaded collector definitions and the compiled + // output of rules, macros, and lists. + if (m_last_compile_output == nullptr) + { + throw falco_exception("rules most be loaded before describing them"); + } + if(!json) { static const char *rule_fmt = "%-50s %s\n"; @@ -544,17 +551,6 @@ void falco_engine::describe_rule(std::string *rule, const std::vectorrules) { auto info = m_rule_collector.rules().at(r.name); Json::Value rule; @@ -604,7 +600,7 @@ void falco_engine::describe_rule(std::string *rule, const std::vectormacros) { auto info = m_rule_collector.macros().at(m.name); Json::Value macro; @@ -615,7 +611,7 @@ void falco_engine::describe_rule(std::string *rule, const std::vectorlists) { auto info = m_rule_collector.lists().at(l.name); Json::Value list; diff --git a/userspace/engine/falco_engine.h b/userspace/engine/falco_engine.h index c24eb75cb23..69eece862e7 100644 --- a/userspace/engine/falco_engine.h +++ b/userspace/engine/falco_engine.h @@ -40,6 +40,7 @@ limitations under the License. #include "falco_load_result.h" #include "filter_details_resolver.h" #include "rule_loader_reader.h" +#include "rule_loader_compiler.h" // // This class acts as the primary interface between a program and the @@ -347,6 +348,8 @@ class falco_engine std::map m_known_rulesets; falco_common::priority_type m_min_priority; + std::unique_ptr m_last_compile_output; + // // Here's how the sampling ratio and multiplier influence // whether or not an event is dropped in