Skip to content
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
Original file line number Diff line number Diff line change
@@ -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",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This dependency is added for the classes FieldCheckerInterface, FieldCheckResults, etc.

],
)
Original file line number Diff line number Diff line change
@@ -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<std::string>&,
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#pragma once

#include <string>
#include <vector>

#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<std::string>& 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
Original file line number Diff line number Diff line change
@@ -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",
],
)
Original file line number Diff line number Diff line change
@@ -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