Line data Source code
1 : #pragma once 2 : 3 : #include <functional> 4 : #include <string> 5 : 6 : #include "envoy/config/typed_config.h" 7 : #include "envoy/extensions/filters/common/dependency/v3/dependency.pb.h" 8 : #include "envoy/http/filter.h" 9 : #include "envoy/init/manager.h" 10 : #include "envoy/network/filter.h" 11 : #include "envoy/server/drain_manager.h" 12 : #include "envoy/server/factory_context.h" 13 : 14 : #include "source/common/common/assert.h" 15 : #include "source/common/common/macros.h" 16 : #include "source/common/protobuf/protobuf.h" 17 : 18 : namespace Envoy { 19 : namespace Server { 20 : namespace Configuration { 21 : 22 : /** 23 : * Common interface for listener filters and UDP listener filters 24 : */ 25 : class ListenerFilterConfigFactoryBase : public Config::TypedFactory { 26 : public: 27 0 : ~ListenerFilterConfigFactoryBase() override = default; 28 : }; 29 : 30 : /** 31 : * Implemented by each listener filter and registered via Registry::registerFactory() 32 : * or the convenience class RegisterFactory. 33 : */ 34 : class NamedListenerFilterConfigFactory : public ListenerFilterConfigFactoryBase { 35 : public: 36 0 : ~NamedListenerFilterConfigFactory() override = default; 37 : 38 : /** 39 : * Create a particular listener filter factory implementation. If the implementation is unable to 40 : * produce a factory with the provided parameters, it should throw an EnvoyException in the case 41 : * of general error or a Json::Exception if the json configuration is erroneous. The returned 42 : * callback should always be initialized. 43 : * @param config supplies the general protobuf configuration for the filter. 44 : * @param listener_filter_matcher supplies the matcher to decide when filter is enabled. 45 : * @param context supplies the filter's context. 46 : * @return Network::ListenerFilterFactoryCb the factory creation function. 47 : */ 48 : virtual Network::ListenerFilterFactoryCb createListenerFilterFactoryFromProto( 49 : const Protobuf::Message& config, 50 : const Network::ListenerFilterMatcherSharedPtr& listener_filter_matcher, 51 : ListenerFactoryContext& context) PURE; 52 : 53 14 : std::string category() const override { return "envoy.filters.listener"; } 54 : }; 55 : 56 : /** 57 : * Implemented by each UDP listener filter and registered via Registry::registerFactory() 58 : * or the convenience class RegisterFactory. 59 : */ 60 : class NamedUdpListenerFilterConfigFactory : public ListenerFilterConfigFactoryBase { 61 : public: 62 0 : ~NamedUdpListenerFilterConfigFactory() override = default; 63 : 64 : /** 65 : * Create a particular UDP listener filter factory implementation. If the implementation is unable 66 : * to produce a factory with the provided parameters, it should throw an EnvoyException. 67 : * The returned callback should always be initialized. 68 : * @param config supplies the general protobuf configuration for the filter 69 : * @param context supplies the filter's context. 70 : * @return Network::UdpListenerFilterFactoryCb the factory creation function. 71 : */ 72 : virtual Network::UdpListenerFilterFactoryCb 73 : createFilterFactoryFromProto(const Protobuf::Message& config, 74 : ListenerFactoryContext& context) PURE; 75 : 76 6 : std::string category() const override { return "envoy.filters.udp_listener"; } 77 : }; 78 : 79 : /** 80 : * Implemented by each QUIC listener filter and registered via Registry::registerFactory() 81 : * or the convenience class RegisterFactory. 82 : */ 83 : class NamedQuicListenerFilterConfigFactory : public ListenerFilterConfigFactoryBase { 84 : public: 85 : ~NamedQuicListenerFilterConfigFactory() override = default; 86 : 87 : /** 88 : * Create a particular listener filter factory implementation. If the implementation is unable to 89 : * produce a factory with the provided parameters, it should throw an EnvoyException in the case 90 : * of general error or a Json::Exception if the json configuration is erroneous. The returned 91 : * callback should always be initialized. 92 : * @param config supplies the general protobuf configuration for the filter. 93 : * @param listener_filter_matcher supplies the matcher to decide when filter is enabled. 94 : * @param context supplies the filter's context. 95 : * @return Network::QuicListenerFilterFactoryCb the factory creation function. 96 : */ 97 : virtual Network::QuicListenerFilterFactoryCb createListenerFilterFactoryFromProto( 98 : const Protobuf::Message& config, 99 : const Network::ListenerFilterMatcherSharedPtr& listener_filter_matcher, 100 : ListenerFactoryContext& context) PURE; 101 : 102 0 : std::string category() const override { return "envoy.filters.quic_listener"; } 103 : }; 104 : 105 : /** 106 : * Implemented by filter factories that require more options to process the protocol used by the 107 : * upstream cluster. 108 : */ 109 : class ProtocolOptionsFactory : public Config::TypedFactory { 110 : public: 111 0 : ~ProtocolOptionsFactory() override = default; 112 : 113 : /** 114 : * Create a particular filter's protocol specific options implementation. If the factory 115 : * implementation is unable to produce a factory with the provided parameters, it should throw an 116 : * EnvoyException. 117 : * @param config supplies the protobuf configuration for the filter 118 : * @param validation_visitor message validation visitor instance. 119 : * @return Upstream::ProtocolOptionsConfigConstSharedPtr the protocol options 120 : */ 121 : virtual Upstream::ProtocolOptionsConfigConstSharedPtr 122 : createProtocolOptionsConfig(const Protobuf::Message& config, 123 0 : ProtocolOptionsFactoryContext& factory_context) { 124 0 : UNREFERENCED_PARAMETER(config); 125 0 : UNREFERENCED_PARAMETER(factory_context); 126 0 : return nullptr; 127 0 : } 128 : 129 : /** 130 : * @return ProtobufTypes::MessagePtr a newly created empty protocol specific options message or 131 : * nullptr if protocol specific options are not available. 132 : */ 133 0 : virtual ProtobufTypes::MessagePtr createEmptyProtocolOptionsProto() { return nullptr; } 134 : }; 135 : 136 : /** 137 : * Implemented by each network filter and registered via Registry::registerFactory() 138 : * or the convenience class RegisterFactory. 139 : */ 140 : class NamedNetworkFilterConfigFactory : public ProtocolOptionsFactory { 141 : public: 142 0 : ~NamedNetworkFilterConfigFactory() override = default; 143 : 144 : /** 145 : * Create a particular network filter factory implementation. If the implementation is unable to 146 : * produce a factory with the provided parameters, it should throw an EnvoyException. The returned 147 : * callback should always be initialized. 148 : * @param config supplies the general json configuration for the filter 149 : * @param filter_chain_factory_context supplies the filter's context. 150 : * @return Network::FilterFactoryCb the factory creation function. 151 : */ 152 : virtual Network::FilterFactoryCb 153 : createFilterFactoryFromProto(const Protobuf::Message& config, 154 : FactoryContext& filter_chain_factory_context) PURE; 155 : 156 152 : std::string category() const override { return "envoy.filters.network"; } 157 : 158 : /** 159 : * @return bool true if this filter must be the last filter in a filter chain, false otherwise. 160 : */ 161 0 : virtual bool isTerminalFilterByProto(const Protobuf::Message&, ServerFactoryContext&) { 162 0 : return false; 163 0 : } 164 : }; 165 : 166 : /** 167 : * Implemented by each upstream cluster network filter and registered via 168 : * Registry::registerFactory() or the convenience class RegisterFactory. 169 : */ 170 : class NamedUpstreamNetworkFilterConfigFactory : public ProtocolOptionsFactory { 171 : public: 172 : ~NamedUpstreamNetworkFilterConfigFactory() override = default; 173 : 174 : /** 175 : * Create a particular upstream network filter factory implementation. If the implementation is 176 : * unable to produce a factory with the provided parameters, it should throw an EnvoyException in 177 : * the case of general error. The returned callback should always be initialized. 178 : */ 179 : virtual Network::FilterFactoryCb 180 : createFilterFactoryFromProto(const Protobuf::Message& config, 181 : UpstreamFactoryContext& context) PURE; 182 : 183 0 : std::string category() const override { return "envoy.filters.upstream_network"; } 184 : 185 : /** 186 : * @return bool true if this filter must be the last filter in a filter chain, false otherwise. 187 : */ 188 0 : virtual bool isTerminalFilterByProto(const Protobuf::Message&, ServerFactoryContext&) { 189 0 : return false; 190 0 : } 191 : }; 192 : 193 : using FilterDependenciesPtr = 194 : std::unique_ptr<envoy::extensions::filters::common::dependency::v3::FilterDependencies>; 195 : using MatchingRequirementsPtr = 196 : std::unique_ptr<envoy::extensions::filters::common::dependency::v3::MatchingRequirements>; 197 : 198 : /** 199 : * Implemented by each HTTP filter and registered via Registry::registerFactory or the 200 : * convenience class RegisterFactory. 201 : */ 202 : class HttpFilterConfigFactoryBase : public ProtocolOptionsFactory { 203 : public: 204 0 : ~HttpFilterConfigFactoryBase() override = default; 205 : 206 : /** 207 : * @return ProtobufTypes::MessagePtr create an empty virtual host, route, or weighted 208 : * cluster-local config proto message for v2. The filter config, which arrives in an 209 : * opaque message, will be parsed into this empty proto. By default, this method 210 : * returns the same value as createEmptyConfigProto, and can be optionally overridden 211 : * in implementations. 212 : */ 213 0 : virtual ProtobufTypes::MessagePtr createEmptyRouteConfigProto() { 214 0 : return createEmptyConfigProto(); 215 0 : } 216 : 217 : /** 218 : * @return RouteSpecificFilterConfigConstSharedPtr allow the filter to pre-process per route 219 : * config. Returned object will be stored in the loaded route configuration. 220 : */ 221 : virtual Router::RouteSpecificFilterConfigConstSharedPtr 222 : createRouteSpecificFilterConfig(const Protobuf::Message&, ServerFactoryContext&, 223 0 : ProtobufMessage::ValidationVisitor&) { 224 0 : return nullptr; 225 0 : } 226 : 227 483 : std::string category() const override { return "envoy.filters.http"; } 228 : 229 : /** 230 : * @return FilterDependenciesPtr specification of dependencies required or 231 : * provided on the decode and encode paths. This function returns an empty 232 : * filter dependencies specification by default, and can be overridden. 233 : */ 234 300 : virtual FilterDependenciesPtr dependencies() { 235 300 : return std::make_unique< 236 300 : envoy::extensions::filters::common::dependency::v3::FilterDependencies>(); 237 300 : } 238 : 239 : /** 240 : * Match requirements for the filters created by this filter factory. These requirements inform 241 : * the validator what input/outputs are valid for a match tree specified via the 242 : * ExtensionWithMatcher wrapper, allowing us to reject the match tree at configuration time if 243 : * there are any violations. 244 : * 245 : * @return MatchingRequirementsPtr specification of matching requirements 246 : * for a match tree that can be used with this filter factory. 247 : */ 248 0 : virtual MatchingRequirementsPtr matchingRequirements() { 249 0 : return std::make_unique< 250 0 : envoy::extensions::filters::common::dependency::v3::MatchingRequirements>(); 251 0 : } 252 : 253 234 : std::set<std::string> configTypes() override { 254 234 : auto config_types = TypedFactory::configTypes(); 255 0 : 256 234 : if (auto message = createEmptyRouteConfigProto(); message != nullptr) { 257 234 : config_types.insert(createReflectableMessage(*message)->GetDescriptor()->full_name()); 258 234 : } 259 0 : 260 234 : return config_types; 261 234 : } 262 : 263 : /** 264 : * @return bool true if this filter must be the last filter in a filter chain, false otherwise. 265 : */ 266 : virtual bool isTerminalFilterByProto(const Protobuf::Message&, 267 0 : Server::Configuration::ServerFactoryContext&) { 268 0 : return false; 269 0 : } 270 : }; 271 : 272 : class NamedHttpFilterConfigFactory : public virtual HttpFilterConfigFactoryBase { 273 : public: 274 : /** 275 : * Create a particular http filter factory implementation. If the implementation is unable to 276 : * produce a factory with the provided parameters, it should throw an EnvoyException. The returned 277 : * callback should always be initialized. 278 : * @param config supplies the general Protobuf message to be marshaled into a filter-specific 279 : * configuration. 280 : * @param stat_prefix prefix for stat logging 281 : * @param context supplies the filter's context. 282 : * @return absl::StatusOr<Http::FilterFactoryCb> the factory creation function or an error if 283 : * creation fails. 284 : */ 285 : virtual absl::StatusOr<Http::FilterFactoryCb> 286 : createFilterFactoryFromProto(const Protobuf::Message& config, const std::string& stat_prefix, 287 : Server::Configuration::FactoryContext& context) PURE; 288 : 289 : /** 290 : * Create a particular http filter factory implementation. If the implementation is unable to 291 : * produce a factory with the provided parameters or this method is not supported, it should throw 292 : * an EnvoyException. The returned callback should always be initialized. 293 : * @param config supplies the general Protobuf message to be marshaled into a filter-specific 294 : * configuration. 295 : * @param stat_prefix prefix for stat logging 296 : * @param context supplies the filter's ServerFactoryContext. 297 : * @return Http::FilterFactoryCb the factory creation function. 298 : */ 299 : virtual Http::FilterFactoryCb 300 : createFilterFactoryFromProtoWithServerContext(const Protobuf::Message&, const std::string&, 301 0 : Server::Configuration::ServerFactoryContext&) { 302 0 : ExceptionUtil::throwEnvoyException( 303 0 : "Creating filter factory from server factory context is not supported"); 304 0 : return nullptr; 305 0 : } 306 : }; 307 : 308 : class UpstreamHttpFilterConfigFactory : public virtual HttpFilterConfigFactoryBase { 309 : public: 310 : /** 311 : * Create a particular http filter factory implementation. If the implementation is unable to 312 : * produce a factory with the provided parameters, it should throw an EnvoyException. The returned 313 : * callback should always be initialized. 314 : * @param config supplies the general Protobuf message to be marshaled into a filter-specific 315 : * configuration. 316 : * @param stat_prefix prefix for stat logging 317 : * @param context supplies the filter's context. 318 : * @return Http::FilterFactoryCb the factory creation function. 319 : */ 320 : virtual absl::StatusOr<Http::FilterFactoryCb> 321 : createFilterFactoryFromProto(const Protobuf::Message& config, const std::string& stat_prefix, 322 : Server::Configuration::UpstreamFactoryContext& context) PURE; 323 : }; 324 : 325 : } // namespace Configuration 326 : } // namespace Server 327 : } // namespace Envoy