fix(userspace/engine): catch YAML parsing and validation errors with right context

Signed-off-by: Jason Dellaluce <jasondellaluce@gmail.com>
This commit is contained in:
Jason Dellaluce 2023-01-09 17:30:49 +00:00 committed by poiana
parent 1b2c7ef7d9
commit d79d7112a0
3 changed files with 38 additions and 5 deletions

View File

@ -64,6 +64,11 @@ rule_loader::context::context(const YAML::Node &item,
init(parent.name(), position(item.Mark()), item_type, item_name, parent);
}
rule_loader::context::context(const YAML::Mark &mark, const context& parent)
{
init(parent.name(), position(mark), item_type::VALUE_FOR, "", parent);
}
rule_loader::context::context(const libsinsp::filter::ast::pos_info& pos,
const std::string& condition,
const context& parent)

View File

@ -113,6 +113,9 @@ namespace rule_loader
item_type item_type,
const std::string& item_name,
const context& parent);
context(
const YAML::Mark &mark,
const context& parent);
// Build a context from a condition expression +
// parser position. This does not use the original

View File

@ -437,23 +437,32 @@ static void read_item(
bool rule_loader::reader::read(rule_loader::configuration& cfg, collector& collector)
{
std::vector<YAML::Node> docs;
rule_loader::context ctx(cfg.name);
try
{
docs = YAML::LoadAll(cfg.content);
}
catch(const exception& e)
catch (YAML::ParserException& e)
{
rule_loader::context ictx(e.mark, ctx);
cfg.res->add_error(falco::load_result::LOAD_ERR_YAML_PARSE, e.what(), ictx);
return false;
}
catch (std::exception& e)
{
rule_loader::context ctx(cfg.name);
cfg.res->add_error(falco::load_result::LOAD_ERR_YAML_PARSE, e.what(), ctx);
return false;
}
catch (...)
{
cfg.res->add_error(falco::load_result::LOAD_ERR_YAML_PARSE, "unknown YAML parsing error", ctx);
return false;
}
for (auto doc = docs.begin(); doc != docs.end(); doc++)
{
if (doc->IsDefined() && !doc->IsNull())
{
rule_loader::context ctx(cfg.name);
try {
THROW(!doc->IsMap() && !doc->IsSequence(),
"Rules content is not yaml",
@ -479,7 +488,23 @@ bool rule_loader::reader::read(rule_loader::configuration& cfg, collector& colle
// as it's effectively a new rules file, for
// consistency we stop at the first error.
return false;
};
}
catch (YAML::ParserException& e)
{
rule_loader::context ictx(e.mark, ctx);
cfg.res->add_error(falco::load_result::LOAD_ERR_YAML_VALIDATE, e.what(), ictx);
return false;
}
catch (std::exception& e)
{
cfg.res->add_error(falco::load_result::LOAD_ERR_VALIDATE, e.what(), ctx);
return false;
}
catch (...)
{
cfg.res->add_error(falco::load_result::LOAD_ERR_VALIDATE, "unknown validation error", ctx);
return false;
}
}
}