Skip to content

Commit

Permalink
http filter: Add overrideable function to http filter factory interfa…
Browse files Browse the repository at this point in the history
…ce for specifying FilterDependencies (#15157)

Signed-off-by: Auni Ahsan <[email protected]>
  • Loading branch information
auni53 authored Mar 5, 2021
1 parent aaaee77 commit 2bb2038
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
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

0 comments on commit 2bb2038

Please sign in to comment.