diff --git a/source/extensions/filters/http/proto_api_scrubber/scrubbing_util_lib/BUILD b/source/extensions/filters/http/proto_api_scrubber/scrubbing_util_lib/BUILD new file mode 100644 index 0000000000000..53b12169e24db --- /dev/null +++ b/source/extensions/filters/http/proto_api_scrubber/scrubbing_util_lib/BUILD @@ -0,0 +1,23 @@ +load( + "//bazel:envoy_build_system.bzl", + "envoy_cc_library", + "envoy_extension_package", +) + +licenses(["notice"]) # Apache 2 + +envoy_extension_package() + +envoy_cc_library( + name = "scrubbing_util_lib", + srcs = [ + "field_checker.cc", + ], + hdrs = [ + "field_checker.h", + ], + deps = [ + "//source/common/protobuf", + "@com_google_protoprocessinglib//proto_processing_lib/proto_scrubber", + ], +) diff --git a/source/extensions/filters/http/proto_api_scrubber/scrubbing_util_lib/field_checker.cc b/source/extensions/filters/http/proto_api_scrubber/scrubbing_util_lib/field_checker.cc new file mode 100644 index 0000000000000..20e652933957c --- /dev/null +++ b/source/extensions/filters/http/proto_api_scrubber/scrubbing_util_lib/field_checker.cc @@ -0,0 +1,24 @@ +#include "source/extensions/filters/http/proto_api_scrubber/scrubbing_util_lib/field_checker.h" + +#include "source/common/protobuf/protobuf.h" + +#include "proto_processing_lib/proto_scrubber/field_checker_interface.h" + +namespace Envoy { +namespace Extensions { +namespace HttpFilters { +namespace ProtoApiScrubber { + +FieldCheckResults FieldChecker::CheckField(const std::vector&, + const Protobuf::Field*) const { + return FieldCheckResults::kInclude; +} + +FieldCheckResults FieldChecker::CheckType(const Protobuf::Type*) const { + return FieldCheckResults::kInclude; +} + +} // namespace ProtoApiScrubber +} // namespace HttpFilters +} // namespace Extensions +} // namespace Envoy diff --git a/source/extensions/filters/http/proto_api_scrubber/scrubbing_util_lib/field_checker.h b/source/extensions/filters/http/proto_api_scrubber/scrubbing_util_lib/field_checker.h new file mode 100644 index 0000000000000..c6947bf2fc95c --- /dev/null +++ b/source/extensions/filters/http/proto_api_scrubber/scrubbing_util_lib/field_checker.h @@ -0,0 +1,60 @@ +#pragma once + +#include +#include + +#include "source/common/protobuf/protobuf.h" + +#include "proto_processing_lib/proto_scrubber/field_checker_interface.h" + +namespace Envoy { +namespace Extensions { +namespace HttpFilters { +namespace ProtoApiScrubber { + +using proto_processing_lib::proto_scrubber::FieldCheckerInterface; +using proto_processing_lib::proto_scrubber::FieldCheckResults; +using proto_processing_lib::proto_scrubber::FieldFilters; + +/** + * FieldChecker class encapsulates the scrubbing logic of `ProtoApiScrubber` filter. + * This `FieldChecker` would be integrated with `proto_processing_lib::proto_scrubber` library for + * protobuf payload scrubbing. The `CheckField()` method declared in the parent class + * `FieldCheckerInterface` and defined in this class is called by the + * `proto_processing_lib::proto_scrubber` library for each field of the protobuf payload to decide + * whether to preserve, remove or traverse it further. + */ +class FieldChecker : public FieldCheckerInterface { +public: + FieldChecker() {} + + // This type is neither copyable nor movable. + FieldChecker(const FieldChecker&) = delete; + FieldChecker& operator=(const FieldChecker&) = delete; + ~FieldChecker() override {} + + /** + * Returns whether the `field` should be included (kInclude), excluded (kExclude) + * or traversed further (kPartial). + */ + FieldCheckResults CheckField(const std::vector& path, + const Protobuf::Field* field) const override; + + /** + * Returns false as it currently doesn't support `google.protobuf.Any` type. + */ + bool SupportAny() const override { return false; } + + /** + * Returns whether the `type` should be included (kInclude), excluded (kExclude) + * or traversed further (kPartial). + */ + FieldCheckResults CheckType(const Protobuf::Type* type) const override; + + FieldFilters FilterName() const override { return FieldFilters::FieldMaskFilter; } +}; + +} // namespace ProtoApiScrubber +} // namespace HttpFilters +} // namespace Extensions +} // namespace Envoy diff --git a/test/extensions/filters/http/proto_api_scrubber/scrubbing_util_lib/BUILD b/test/extensions/filters/http/proto_api_scrubber/scrubbing_util_lib/BUILD new file mode 100644 index 0000000000000..29c85e635fc62 --- /dev/null +++ b/test/extensions/filters/http/proto_api_scrubber/scrubbing_util_lib/BUILD @@ -0,0 +1,21 @@ +load( + "//bazel:envoy_build_system.bzl", + "envoy_cc_test", + "envoy_package", +) + +licenses(["notice"]) # Apache 2 + +envoy_package() + +envoy_cc_test( + name = "field_checker_test", + srcs = ["field_checker_test.cc"], + deps = [ + "//source/common/protobuf", + "//source/extensions/filters/http/proto_api_scrubber/scrubbing_util_lib", + "//test/test_common:environment_lib", + "//test/test_common:utility_lib", + "@com_google_protoprocessinglib//proto_processing_lib/proto_scrubber", + ], +) diff --git a/test/extensions/filters/http/proto_api_scrubber/scrubbing_util_lib/field_checker_test.cc b/test/extensions/filters/http/proto_api_scrubber/scrubbing_util_lib/field_checker_test.cc new file mode 100644 index 0000000000000..3e080eb88fe14 --- /dev/null +++ b/test/extensions/filters/http/proto_api_scrubber/scrubbing_util_lib/field_checker_test.cc @@ -0,0 +1,51 @@ +#include "source/common/protobuf/protobuf.h" +#include "source/extensions/filters/http/proto_api_scrubber/scrubbing_util_lib/field_checker.h" + +#include "test/test_common/utility.h" + +#include "gtest/gtest.h" +#include "proto_processing_lib/proto_scrubber/field_checker_interface.h" + +using proto_processing_lib::proto_scrubber::FieldCheckResults; +using proto_processing_lib::proto_scrubber::FieldFilters; + +namespace Envoy { +namespace Extensions { +namespace HttpFilters { +namespace ProtoApiScrubber { +namespace { + +class FieldCheckerTest : public ::testing::Test {}; + +// With the current basic implementation, all fields are included. This test +// verifies that behavior for a few different field paths. Once field mask +// logic is added, this test suite should be expanded to cover exclusion, +// wildcards, and other scenarios. +TEST_F(FieldCheckerTest, IncludesSimpleField) { + FieldChecker field_checker; + Protobuf::Field simple_field; + simple_field.set_name("name"); + EXPECT_EQ(field_checker.CheckField({"name"}, &simple_field), FieldCheckResults::kInclude); +} + +TEST_F(FieldCheckerTest, IncludesType) { + FieldChecker field_checker; + Protobuf::Type type; + EXPECT_EQ(field_checker.CheckType(&type), FieldCheckResults::kInclude); +} + +TEST_F(FieldCheckerTest, SupportAny) { + FieldChecker field_checker; + EXPECT_FALSE(field_checker.SupportAny()); +} + +TEST_F(FieldCheckerTest, FilterName) { + FieldChecker field_checker; + EXPECT_EQ(field_checker.FilterName(), FieldFilters::FieldMaskFilter); +} + +} // namespace +} // namespace ProtoApiScrubber +} // namespace HttpFilters +} // namespace Extensions +} // namespace Envoy