Skip to content

Commit

Permalink
fix(userspace/libsinsp): allow plugin filterchecks args to be both in…
Browse files Browse the repository at this point in the history
…dex or key.

Signed-off-by: Federico Di Pierro <[email protected]>
  • Loading branch information
FedeDP committed Feb 12, 2025
1 parent ab581e2 commit 40ec458
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
15 changes: 11 additions & 4 deletions userspace/libsinsp/plugin_filtercheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@ int32_t sinsp_filter_check_plugin::parse_field_name(std::string_view val,

// parse the argument content, which can either be an index or a key
if(m_info->m_fields[m_field_id].m_flags & filtercheck_field_flags::EPF_ARG_INDEX) {
extract_arg_index(val);
// Do NOT throw any exception if we want also try to
// parse the arg as key string,
// so that we support arguments that can be both index or key.
extract_arg_index(val,
!(m_info->m_fields[m_field_id].m_flags &

Check warning on line 102 in userspace/libsinsp/plugin_filtercheck.cpp

View check run for this annotation

Codecov / codecov/patch

userspace/libsinsp/plugin_filtercheck.cpp#L101-L102

Added lines #L101 - L102 were not covered by tests
filtercheck_field_flags::EPF_ARG_KEY));
}
if(m_info->m_fields[m_field_id].m_flags & filtercheck_field_flags::EPF_ARG_KEY) {
extract_arg_key();
Expand Down Expand Up @@ -207,7 +212,7 @@ bool sinsp_filter_check_plugin::extract_nocache(sinsp_evt* evt,
return true;
}

void sinsp_filter_check_plugin::extract_arg_index(std::string_view full_field_name) {
void sinsp_filter_check_plugin::extract_arg_index(std::string_view full_field_name, bool required) {

Check warning on line 215 in userspace/libsinsp/plugin_filtercheck.cpp

View check run for this annotation

Codecov / codecov/patch

userspace/libsinsp/plugin_filtercheck.cpp#L215

Added line #L215 was not covered by tests
int length = m_argstr.length();
bool is_valid = true;
std::string message = "";
Expand Down Expand Up @@ -239,8 +244,10 @@ void sinsp_filter_check_plugin::extract_arg_index(std::string_view full_field_na
message = " has an invalid index argument not representable on 64 bit: ";
}
}
throw sinsp_exception(string("filter ") + string(full_field_name) + string(" ") +
m_field->m_name + message + m_argstr);
if(required) {
throw sinsp_exception(string("filter ") + string(full_field_name) + string(" ") +
m_field->m_name + message + m_argstr);
}
}

// extract_arg_key() extracts a valid string from the argument. If we pass
Expand Down
2 changes: 1 addition & 1 deletion userspace/libsinsp/plugin_filtercheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class sinsp_filter_check_plugin : public sinsp_filter_check {
// format is valid, otherwise it throws an exception.
// `full_field_name` has the format "field[argument]" and it is necessary
// to throw an exception.
void extract_arg_index(std::string_view full_field_name);
void extract_arg_index(std::string_view full_field_name, bool required);

// extract_arg_key() extracts a valid string from the argument. If we pass
// a numeric argument, it will be converted to string.
Expand Down

0 comments on commit 40ec458

Please sign in to comment.