Skip to content

refactor(libsinp): refactor filter transformers to use interfaces #2224

New issue

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

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

Already on GitHub? # to your account

Merged
merged 6 commits into from
Jan 9, 2025
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
8 changes: 7 additions & 1 deletion userspace/libsinsp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ add_library(
dumper.cpp
fdinfo.cpp
filter.cpp
sinsp_filter_transformer.cpp
sinsp_filter_transformers/sinsp_filter_transformer.cpp
sinsp_filter_transformers/sinsp_filter_transformer_base64.cpp
sinsp_filter_transformers/sinsp_filter_transformer_basename.cpp
sinsp_filter_transformers/sinsp_filter_transformer_len.cpp
sinsp_filter_transformers/sinsp_filter_transformer_storage.cpp
sinsp_filter_transformers/sinsp_filter_transformer_tolower.cpp
sinsp_filter_transformers/sinsp_filter_transformer_toupper.cpp
sinsp_filtercheck.cpp
sinsp_filtercheck_container.cpp
sinsp_filtercheck_event.cpp
Expand Down
262 changes: 0 additions & 262 deletions userspace/libsinsp/sinsp_filter_transformer.cpp

This file was deleted.

52 changes: 52 additions & 0 deletions userspace/libsinsp/sinsp_filter_transformers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright (C) 2024 The Falco Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#pragma once

#include <libsinsp/sinsp_filter_transformers/sinsp_filter_transformer.h>
#include <libsinsp/sinsp_filter_transformers/sinsp_filter_transformer_base64.h>
#include <libsinsp/sinsp_filter_transformers/sinsp_filter_transformer_basename.h>
#include <libsinsp/sinsp_filter_transformers/sinsp_filter_transformer_len.h>
#include <libsinsp/sinsp_filter_transformers/sinsp_filter_transformer_storage.h>
#include <libsinsp/sinsp_filter_transformers/sinsp_filter_transformer_tolower.h>
#include <libsinsp/sinsp_filter_transformers/sinsp_filter_transformer_toupper.h>

namespace sinsp_filter_transformer_factory {
inline std::unique_ptr<sinsp_filter_transformer> create_transformer(
filter_transformer_type trtype) {
switch(trtype) {
case FTR_TOUPPER: {
return std::make_unique<sinsp_filter_transformer_toupper>();
}
case FTR_TOLOWER: {
return std::make_unique<sinsp_filter_transformer_tolower>();
}
case FTR_BASE64: {
return std::make_unique<sinsp_filter_transformer_base64>();
}
case FTR_STORAGE: {
return std::make_unique<sinsp_filter_transformer_storage>();
}
case FTR_BASENAME: {
return std::make_unique<sinsp_filter_transformer_basename>();
}
case FTR_LEN: {
return std::make_unique<sinsp_filter_transformer_len>();
}
default:

Check warning on line 47 in userspace/libsinsp/sinsp_filter_transformers.h

View check run for this annotation

Codecov / codecov/patch

userspace/libsinsp/sinsp_filter_transformers.h#L47

Added line #L47 was not covered by tests
throw sinsp_exception("transformer '" + std::to_string(trtype) + "' is not supported");
return nullptr;
}
}
}; // namespace sinsp_filter_transformer_factory
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright (C) 2025 The Falco Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include <libsinsp/sinsp_filter_transformers/sinsp_filter_transformer.h>

sinsp_filter_transformer::~sinsp_filter_transformer() {}

bool sinsp_filter_transformer::string_transformer(std::vector<extract_value_t>& vec,
ppm_param_type t,
str_transformer_func_t f) {
m_storage_values.resize(vec.size());
for(std::size_t i = 0; i < vec.size(); i++) {
storage_t& buf = m_storage_values[i];

buf.clear();
if(vec[i].ptr == nullptr) {
continue;

Check warning on line 28 in userspace/libsinsp/sinsp_filter_transformers/sinsp_filter_transformer.cpp

View check run for this annotation

Codecov / codecov/patch

userspace/libsinsp/sinsp_filter_transformers/sinsp_filter_transformer.cpp#L28

Added line #L28 was not covered by tests
}

// we don't know whether this will come as a string or a byte buf,
// so we sanitize by skipping all terminator characters
size_t in_len = vec[i].len;
while(in_len > 0 && vec[i].ptr[in_len - 1] == '\0') {
in_len--;
}

// each function can assume that the input size does NOT include
// the terminator character, and should not assume that the string
// is null-terminated
std::string_view in{(const char*)vec[i].ptr, in_len};
if(!f(in, buf)) {
return false;
}

// we insert a null terminator in case we miss one, just to stay safe
if(buf.size() == 0 || buf[buf.size() - 1] != '\0') {
buf.push_back('\0');
}

vec[i].ptr = (uint8_t*)&buf[0];
vec[i].len = buf.size();
}
return true;
}
Loading
Loading