Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(userspace/engine): improve rule loading validation results #2344

Merged
merged 2 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions userspace/engine/rule_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -87,9 +92,10 @@ rule_loader::context::context(const libsinsp::filter::ast::pos_info& pos,
// parser line/columns are 1-indexed while yaml marks are
// 0-indexed, though.
position condpos;
condpos.pos = pos.idx;
condpos.line = pos.line-1;
condpos.column = pos.col-1;
auto& lastpos = parent.m_locs.back();
condpos.pos = pos.idx + lastpos.pos.pos;
condpos.line = pos.line + lastpos.pos.line;
condpos.column = pos.col + lastpos.pos.column;

init(name, condpos, rule_loader::context::CONDITION_EXPRESSION, item_name, parent);
}
Expand Down
3 changes: 3 additions & 0 deletions userspace/engine/rule_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 30 additions & 5 deletions userspace/engine/rule_loader_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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;
}
}
}

Expand Down