Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

http filter: Add overrideable function to http filter factory interface for specifying FilterDependencies #15157

Merged
merged 9 commits into from
Mar 5, 2021
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
1 change: 1 addition & 0 deletions include/envoy/server/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ envoy_cc_library(
"//source/common/common:assert_lib",
"//source/common/common:macros",
"//source/common/protobuf",
"@envoy_api//envoy/extensions/filters/common/dependency/v3:pkg_cc_proto",
],
)

Expand Down
14 changes: 14 additions & 0 deletions include/envoy/server/filter_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <functional>

#include "envoy/config/typed_config.h"
#include "envoy/extensions/filters/common/dependency/v3/dependency.pb.h"
#include "envoy/http/filter.h"
#include "envoy/init/manager.h"
#include "envoy/network/filter.h"
Expand Down Expand Up @@ -152,6 +153,9 @@ class NamedUpstreamNetworkFilterConfigFactory : public ProtocolOptionsFactory {
std::string category() const override { return "envoy.filters.upstream_network"; }
};

using FilterDependenciesPtr =
std::unique_ptr<envoy::extensions::filters::common::dependency::v3::FilterDependencies>;

/**
* Implemented by each HTTP filter and registered via Registry::registerFactory or the
* convenience class RegisterFactory.
Expand Down Expand Up @@ -201,6 +205,16 @@ class NamedHttpFilterConfigFactory : public ProtocolOptionsFactory {
* @return bool true if this filter must be the last filter in a filter chain, false otherwise.
*/
virtual bool isTerminalFilter() { return false; }

/**
* @return FilterDependenciesPtr specification of dependencies required or
* provided on the decode and encode paths. This function returns an empty
* filter dependencies specification by default, and can be overridden.
*/
virtual FilterDependenciesPtr dependencies() {
return std::make_unique<
envoy::extensions::filters::common::dependency::v3::FilterDependencies>();
}
};

} // namespace Configuration
Expand Down
26 changes: 26 additions & 0 deletions test/server/filter_config_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
#include "gtest/gtest.h"

namespace Envoy {
namespace Server {
namespace Configuration {
namespace {

using envoy::extensions::filters::common::dependency::v3::Dependency;
using envoy::extensions::filters::common::dependency::v3::FilterDependencies;

class TestHttpFilterConfigFactory : public Server::Configuration::NamedHttpFilterConfigFactory {
public:
TestHttpFilterConfigFactory() = default;
Expand All @@ -25,6 +30,14 @@ class TestHttpFilterConfigFactory : public Server::Configuration::NamedHttpFilte
ProtobufTypes::MessagePtr createEmptyConfigProto() override { return nullptr; }
ProtobufTypes::MessagePtr createEmptyProtocolOptionsProto() override { return nullptr; }

FilterDependenciesPtr dependencies() override {
FilterDependencies dependencies;
Dependency* d = dependencies.add_decode_required();
d->set_name("foobar");
d->set_type(Dependency::FILTER_STATE_KEY);
return std::make_unique<FilterDependencies>(dependencies);
}

std::string name() const override { return "envoy.test.http_filter"; }
std::string configType() override { return ""; };
};
Expand All @@ -38,5 +51,18 @@ TEST(NamedHttpFilterConfigFactoryTest, CreateFilterFactory) {
factory.createFilterFactoryFromProto(*message, stats_prefix, context);
}

TEST(NamedHttpFilterConfigFactoryTest, Dependencies) {
TestHttpFilterConfigFactory factory;
const std::string stats_prefix = "foo";
Server::Configuration::MockFactoryContext context;
ProtobufTypes::MessagePtr message{new Envoy::ProtobufWkt::Struct()};

factory.createFilterFactoryFromProto(*message, stats_prefix, context);

EXPECT_EQ(factory.dependencies()->decode_required().size(), 1);
}

} // namespace
} // namespace Configuration
} // namespace Server
} // namespace Envoy