Skip to content

Commit 1a41f71

Browse files
authored
[ProtoApiScrubber] Add directory skeleton and basic logic for field checker (#41713)
Signed-off-by: Sumit Kumar <[email protected]>
1 parent 37efb74 commit 1a41f71

File tree

5 files changed

+179
-0
lines changed

5 files changed

+179
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
load(
2+
"//bazel:envoy_build_system.bzl",
3+
"envoy_cc_library",
4+
"envoy_extension_package",
5+
)
6+
7+
licenses(["notice"]) # Apache 2
8+
9+
envoy_extension_package()
10+
11+
envoy_cc_library(
12+
name = "scrubbing_util_lib",
13+
srcs = [
14+
"field_checker.cc",
15+
],
16+
hdrs = [
17+
"field_checker.h",
18+
],
19+
deps = [
20+
"//source/common/protobuf",
21+
"@com_google_protoprocessinglib//proto_processing_lib/proto_scrubber",
22+
],
23+
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "source/extensions/filters/http/proto_api_scrubber/scrubbing_util_lib/field_checker.h"
2+
3+
#include "source/common/protobuf/protobuf.h"
4+
5+
#include "proto_processing_lib/proto_scrubber/field_checker_interface.h"
6+
7+
namespace Envoy {
8+
namespace Extensions {
9+
namespace HttpFilters {
10+
namespace ProtoApiScrubber {
11+
12+
FieldCheckResults FieldChecker::CheckField(const std::vector<std::string>&,
13+
const Protobuf::Field*) const {
14+
return FieldCheckResults::kInclude;
15+
}
16+
17+
FieldCheckResults FieldChecker::CheckType(const Protobuf::Type*) const {
18+
return FieldCheckResults::kInclude;
19+
}
20+
21+
} // namespace ProtoApiScrubber
22+
} // namespace HttpFilters
23+
} // namespace Extensions
24+
} // namespace Envoy
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <vector>
5+
6+
#include "source/common/protobuf/protobuf.h"
7+
8+
#include "proto_processing_lib/proto_scrubber/field_checker_interface.h"
9+
10+
namespace Envoy {
11+
namespace Extensions {
12+
namespace HttpFilters {
13+
namespace ProtoApiScrubber {
14+
15+
using proto_processing_lib::proto_scrubber::FieldCheckerInterface;
16+
using proto_processing_lib::proto_scrubber::FieldCheckResults;
17+
using proto_processing_lib::proto_scrubber::FieldFilters;
18+
19+
/**
20+
* FieldChecker class encapsulates the scrubbing logic of `ProtoApiScrubber` filter.
21+
* This `FieldChecker` would be integrated with `proto_processing_lib::proto_scrubber` library for
22+
* protobuf payload scrubbing. The `CheckField()` method declared in the parent class
23+
* `FieldCheckerInterface` and defined in this class is called by the
24+
* `proto_processing_lib::proto_scrubber` library for each field of the protobuf payload to decide
25+
* whether to preserve, remove or traverse it further.
26+
*/
27+
class FieldChecker : public FieldCheckerInterface {
28+
public:
29+
FieldChecker() {}
30+
31+
// This type is neither copyable nor movable.
32+
FieldChecker(const FieldChecker&) = delete;
33+
FieldChecker& operator=(const FieldChecker&) = delete;
34+
~FieldChecker() override {}
35+
36+
/**
37+
* Returns whether the `field` should be included (kInclude), excluded (kExclude)
38+
* or traversed further (kPartial).
39+
*/
40+
FieldCheckResults CheckField(const std::vector<std::string>& path,
41+
const Protobuf::Field* field) const override;
42+
43+
/**
44+
* Returns false as it currently doesn't support `google.protobuf.Any` type.
45+
*/
46+
bool SupportAny() const override { return false; }
47+
48+
/**
49+
* Returns whether the `type` should be included (kInclude), excluded (kExclude)
50+
* or traversed further (kPartial).
51+
*/
52+
FieldCheckResults CheckType(const Protobuf::Type* type) const override;
53+
54+
FieldFilters FilterName() const override { return FieldFilters::FieldMaskFilter; }
55+
};
56+
57+
} // namespace ProtoApiScrubber
58+
} // namespace HttpFilters
59+
} // namespace Extensions
60+
} // namespace Envoy
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
load(
2+
"//bazel:envoy_build_system.bzl",
3+
"envoy_cc_test",
4+
"envoy_package",
5+
)
6+
7+
licenses(["notice"]) # Apache 2
8+
9+
envoy_package()
10+
11+
envoy_cc_test(
12+
name = "field_checker_test",
13+
srcs = ["field_checker_test.cc"],
14+
deps = [
15+
"//source/common/protobuf",
16+
"//source/extensions/filters/http/proto_api_scrubber/scrubbing_util_lib",
17+
"//test/test_common:environment_lib",
18+
"//test/test_common:utility_lib",
19+
"@com_google_protoprocessinglib//proto_processing_lib/proto_scrubber",
20+
],
21+
)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include "source/common/protobuf/protobuf.h"
2+
#include "source/extensions/filters/http/proto_api_scrubber/scrubbing_util_lib/field_checker.h"
3+
4+
#include "test/test_common/utility.h"
5+
6+
#include "gtest/gtest.h"
7+
#include "proto_processing_lib/proto_scrubber/field_checker_interface.h"
8+
9+
using proto_processing_lib::proto_scrubber::FieldCheckResults;
10+
using proto_processing_lib::proto_scrubber::FieldFilters;
11+
12+
namespace Envoy {
13+
namespace Extensions {
14+
namespace HttpFilters {
15+
namespace ProtoApiScrubber {
16+
namespace {
17+
18+
class FieldCheckerTest : public ::testing::Test {};
19+
20+
// With the current basic implementation, all fields are included. This test
21+
// verifies that behavior for a few different field paths. Once field mask
22+
// logic is added, this test suite should be expanded to cover exclusion,
23+
// wildcards, and other scenarios.
24+
TEST_F(FieldCheckerTest, IncludesSimpleField) {
25+
FieldChecker field_checker;
26+
Protobuf::Field simple_field;
27+
simple_field.set_name("name");
28+
EXPECT_EQ(field_checker.CheckField({"name"}, &simple_field), FieldCheckResults::kInclude);
29+
}
30+
31+
TEST_F(FieldCheckerTest, IncludesType) {
32+
FieldChecker field_checker;
33+
Protobuf::Type type;
34+
EXPECT_EQ(field_checker.CheckType(&type), FieldCheckResults::kInclude);
35+
}
36+
37+
TEST_F(FieldCheckerTest, SupportAny) {
38+
FieldChecker field_checker;
39+
EXPECT_FALSE(field_checker.SupportAny());
40+
}
41+
42+
TEST_F(FieldCheckerTest, FilterName) {
43+
FieldChecker field_checker;
44+
EXPECT_EQ(field_checker.FilterName(), FieldFilters::FieldMaskFilter);
45+
}
46+
47+
} // namespace
48+
} // namespace ProtoApiScrubber
49+
} // namespace HttpFilters
50+
} // namespace Extensions
51+
} // namespace Envoy

0 commit comments

Comments
 (0)