diff --git a/include/envoy/server/BUILD b/include/envoy/server/BUILD index 8f0d6691103d..6ffc35a3ffe2 100644 --- a/include/envoy/server/BUILD +++ b/include/envoy/server/BUILD @@ -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", ], ) diff --git a/include/envoy/server/filter_config.h b/include/envoy/server/filter_config.h index 97b2b3fe51b0..5260902f9753 100644 --- a/include/envoy/server/filter_config.h +++ b/include/envoy/server/filter_config.h @@ -3,6 +3,7 @@ #include #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" @@ -152,6 +153,9 @@ class NamedUpstreamNetworkFilterConfigFactory : public ProtocolOptionsFactory { std::string category() const override { return "envoy.filters.upstream_network"; } }; +using FilterDependenciesPtr = + std::unique_ptr; + /** * Implemented by each HTTP filter and registered via Registry::registerFactory or the * convenience class RegisterFactory. @@ -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 diff --git a/test/server/filter_config_test.cc b/test/server/filter_config_test.cc index b8ddc9e063bf..15e89c590020 100644 --- a/test/server/filter_config_test.cc +++ b/test/server/filter_config_test.cc @@ -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; @@ -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(dependencies); + } + std::string name() const override { return "envoy.test.http_filter"; } std::string configType() override { return ""; }; }; @@ -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