cleanup: use macros for default error messages

Signed-off-by: Andrea Terzolo <andreaterzolo3@gmail.com>
This commit is contained in:
Andrea Terzolo
2024-01-10 15:17:14 +01:00
committed by poiana
parent 91d1511285
commit 8ebdbe3e6f
5 changed files with 27 additions and 26 deletions

View File

@@ -3,6 +3,7 @@
#include "falco_engine.h" #include "falco_engine.h"
#include "rule_loader_reader.h" #include "rule_loader_reader.h"
#include "rule_loader_compiler.h" #include "rule_loader_compiler.h"
#include "rule_loading_messages.h"
class engine_loader_test : public ::testing::Test { class engine_loader_test : public ::testing::Test {
protected: protected:
@@ -370,7 +371,7 @@ TEST_F(engine_loader_test, macro_override_append_before_macro_definition)
// We cannot define a macro override before the macro definition. // We cannot define a macro override before the macro definition.
ASSERT_FALSE(load_rules(rules_content, "rules.yaml")); ASSERT_FALSE(load_rules(rules_content, "rules.yaml"));
ASSERT_TRUE(check_error_message("Macro has 'append' key but no macro by that name already exists")); ASSERT_TRUE(check_error_message(ERROR_NO_PREVIOUS_MACRO));
} }
TEST_F(engine_loader_test, macro_append_before_macro_definition) TEST_F(engine_loader_test, macro_append_before_macro_definition)
@@ -394,7 +395,7 @@ TEST_F(engine_loader_test, macro_append_before_macro_definition)
// We cannot define a macro override before the macro definition. // We cannot define a macro override before the macro definition.
ASSERT_FALSE(load_rules(rules_content, "rules.yaml")); ASSERT_FALSE(load_rules(rules_content, "rules.yaml"));
ASSERT_TRUE(check_error_message("Macro has 'append' key but no macro by that name already exists")); ASSERT_TRUE(check_error_message(ERROR_NO_PREVIOUS_MACRO));
} }
TEST_F(engine_loader_test, macro_override_append_after_macro_definition) TEST_F(engine_loader_test, macro_override_append_after_macro_definition)
@@ -469,7 +470,7 @@ TEST_F(engine_loader_test, rule_override_append_before_rule_definition)
)END"; )END";
ASSERT_FALSE(load_rules(rules_content, "rules.yaml")); ASSERT_FALSE(load_rules(rules_content, "rules.yaml"));
ASSERT_TRUE(check_error_message("Rule has 'append' key but no rule by that name already exists")); ASSERT_TRUE(check_error_message(ERROR_NO_PREVIOUS_RULE));
} }
TEST_F(engine_loader_test, rule_append_before_rule_definition) TEST_F(engine_loader_test, rule_append_before_rule_definition)
@@ -488,7 +489,7 @@ TEST_F(engine_loader_test, rule_append_before_rule_definition)
)END"; )END";
ASSERT_FALSE(load_rules(rules_content, "rules.yaml")); ASSERT_FALSE(load_rules(rules_content, "rules.yaml"));
ASSERT_TRUE(check_error_message("Rule has 'append' key but no rule by that name already exists")); ASSERT_TRUE(check_error_message(ERROR_NO_PREVIOUS_RULE));
} }
TEST_F(engine_loader_test, rule_override_append_after_rule_definition) TEST_F(engine_loader_test, rule_override_append_after_rule_definition)
@@ -590,7 +591,7 @@ TEST_F(engine_loader_test, list_override_append_before_list_definition)
// We cannot define a list override before the list definition. // We cannot define a list override before the list definition.
ASSERT_FALSE(load_rules(rules_content, "rules.yaml")); ASSERT_FALSE(load_rules(rules_content, "rules.yaml"));
ASSERT_TRUE(check_error_message("List has 'append' key but no list by that name already exists")); ASSERT_TRUE(check_error_message(ERROR_NO_PREVIOUS_LIST));
} }
TEST_F(engine_loader_test, list_append_before_list_definition) TEST_F(engine_loader_test, list_append_before_list_definition)
@@ -613,7 +614,7 @@ TEST_F(engine_loader_test, list_append_before_list_definition)
// We cannot define a list append before the list definition. // We cannot define a list append before the list definition.
ASSERT_FALSE(load_rules(rules_content, "rules.yaml")); ASSERT_FALSE(load_rules(rules_content, "rules.yaml"));
ASSERT_TRUE(check_error_message("List has 'append' key but no list by that name already exists")); ASSERT_TRUE(check_error_message(ERROR_NO_PREVIOUS_LIST));
} }
TEST_F(engine_loader_test, list_override_append_after_list_definition) TEST_F(engine_loader_test, list_override_append_after_list_definition)

View File

@@ -190,10 +190,7 @@ void rule_loader::collector::define(configuration& cfg, list_info& info)
void rule_loader::collector::append(configuration& cfg, list_info& info) void rule_loader::collector::append(configuration& cfg, list_info& info)
{ {
auto prev = m_list_infos.at(info.name); auto prev = m_list_infos.at(info.name);
THROW(!prev, THROW(!prev, ERROR_NO_PREVIOUS_LIST, info.ctx);
// "List has 'append' key or an append override but no list by that name already exists", // TODO update this error and update testing
"List has 'append' key but no list by that name already exists",
info.ctx);
prev->items.insert(prev->items.end(), info.items.begin(), info.items.end()); prev->items.insert(prev->items.end(), info.items.begin(), info.items.end());
append_info(prev, info, m_cur_index++); append_info(prev, info, m_cur_index++);
} }
@@ -206,9 +203,7 @@ void rule_loader::collector::define(configuration& cfg, macro_info& info)
void rule_loader::collector::append(configuration& cfg, macro_info& info) void rule_loader::collector::append(configuration& cfg, macro_info& info)
{ {
auto prev = m_macro_infos.at(info.name); auto prev = m_macro_infos.at(info.name);
THROW(!prev, THROW(!prev, ERROR_NO_PREVIOUS_MACRO, info.ctx);
"Macro has 'append' key but no macro by that name already exists",
info.ctx);
prev->cond += " "; prev->cond += " ";
prev->cond += info.cond; prev->cond += info.cond;
append_info(prev, info, m_cur_index++); append_info(prev, info, m_cur_index++);
@@ -244,10 +239,7 @@ void rule_loader::collector::append(configuration& cfg, rule_update_info& info)
{ {
auto prev = m_rule_infos.at(info.name); auto prev = m_rule_infos.at(info.name);
THROW(!prev, THROW(!prev, ERROR_NO_PREVIOUS_RULE, info.ctx);
// "Rule has 'append' key or an append override but no rule by that name already exists", // TODO replace with this and update testing
"Rule has 'append' key but no rule by that name already exists",
info.ctx);
THROW(!info.has_any_value(), THROW(!info.has_any_value(),
"Appended rule must have exceptions or condition property", "Appended rule must have exceptions or condition property",
// "Appended rule must have at least one field that can be appended to", // TODO replace with this and update testing // "Appended rule must have at least one field that can be appended to", // TODO replace with this and update testing

View File

@@ -22,6 +22,7 @@ limitations under the License.
#include "rule_loader_reader.h" #include "rule_loader_reader.h"
#include "falco_engine_version.h" #include "falco_engine_version.h"
#include "rule_loading_messages.h"
#define THROW(cond, err, ctx) { if ((cond)) { throw rule_loader::rule_load_exception(falco::load_result::LOAD_ERR_YAML_VALIDATE, (err), (ctx)); } } #define THROW(cond, err, ctx) { if ((cond)) { throw rule_loader::rule_load_exception(falco::load_result::LOAD_ERR_YAML_VALIDATE, (err), (ctx)); } }

View File

@@ -23,15 +23,6 @@ limitations under the License.
#include "version.h" #include "version.h"
#include "falco_engine_version.h" #include "falco_engine_version.h"
// Error message used when both 'override' and 'append' are specified.
#define OVERRIDE_APPEND_ERROR_MESSAGE "Keys 'override' and 'append: true' cannot be used together. Add an 'append' entry (e.g. 'condition: append') under 'override' instead."
// Warning message used when `append` is used.
#define WARNING_APPEND_MESSAGE "'append' key is deprecated. Add an 'append' entry (e.g. 'condition: append') under 'override' instead."
// Warning message used when `enabled` is used without override.
#define WARNING_ENABLED_MESSAGE "The standalone 'enabled' key usage is deprecated. The correct approach requires also a 'replace' entry under the 'override' key (i.e. 'enabled: replace')."
namespace rule_loader namespace rule_loader
{ {

View File

@@ -0,0 +1,16 @@
#pragma once
// Error message used when both 'override' and 'append' keys are specified.
#define OVERRIDE_APPEND_ERROR_MESSAGE "Keys 'override' and 'append: true' cannot be used together. Add an 'append' entry (e.g. 'condition: append') under 'override' instead."
// Warning message used when 'append' key is used.
#define WARNING_APPEND_MESSAGE "'append' key is deprecated. Add an 'append' entry (e.g. 'condition: append') under 'override' instead."
// Warning message used when 'enabled' is used without 'override' key.
#define WARNING_ENABLED_MESSAGE "The standalone 'enabled' key usage is deprecated. The correct approach requires also a 'replace' entry under the 'override' key (i.e. 'enabled: replace')."
#define ERROR_NO_PREVIOUS_MACRO "Macro uses 'append' or 'override.condition: append' but no macro by that name already exists"
#define ERROR_NO_PREVIOUS_LIST "List uses 'append' or 'override.items: append' but no list by that name already exists"
#define ERROR_NO_PREVIOUS_RULE "Rule uses 'append' or 'override.<key>: append' but no rule by that name already exists"