-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http: added a new dual header mutation filter that could be used as d…
…ownstream/upstream filter (#25658) Check #24100 for more detailed context. Risk Level: low. new L7 extension. Testing: unit. Docs Changes: added. Release Notes: added. Signed-off-by: wbpcode <[email protected]>
- Loading branch information
code
authored
Mar 9, 2023
1 parent
ba3ae3c
commit d448b84
Showing
28 changed files
with
1,369 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
api/envoy/extensions/filters/http/header_mutation/v3/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# DO NOT EDIT. This file is generated by tools/proto_format/proto_sync.py. | ||
|
||
load("@envoy_api//bazel:api_build_system.bzl", "api_proto_package") | ||
|
||
licenses(["notice"]) # Apache 2 | ||
|
||
api_proto_package( | ||
deps = [ | ||
"//envoy/config/common/mutation_rules/v3:pkg", | ||
"@com_github_cncf_udpa//udpa/annotations:pkg", | ||
], | ||
) |
36 changes: 36 additions & 0 deletions
36
api/envoy/extensions/filters/http/header_mutation/v3/header_mutation.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
syntax = "proto3"; | ||
|
||
package envoy.extensions.filters.http.header_mutation.v3; | ||
|
||
import "envoy/config/common/mutation_rules/v3/mutation_rules.proto"; | ||
|
||
import "udpa/annotations/status.proto"; | ||
|
||
option java_package = "io.envoyproxy.envoy.extensions.filters.http.header_mutation.v3"; | ||
option java_outer_classname = "HeaderMutationProto"; | ||
option java_multiple_files = true; | ||
option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/http/header_mutation/v3;header_mutationv3"; | ||
option (udpa.annotations.file_status).package_version_status = ACTIVE; | ||
|
||
// [#protodoc-title: Header mutation filter configuration] | ||
// [#extension: envoy.filters.http.header_mutation] | ||
|
||
message Mutations { | ||
// The request mutations are applied before the request is forwarded to the upstream cluster. | ||
repeated config.common.mutation_rules.v3.HeaderMutation request_mutations = 1; | ||
|
||
// The response mutations are applied before the response is sent to the downstream client. | ||
repeated config.common.mutation_rules.v3.HeaderMutation response_mutations = 2; | ||
} | ||
|
||
// Per route configuration for the header mutation filter. If this is configured at multiple levels | ||
// (route level, virtual host level, and route table level), only the most specific one will be used. | ||
message HeaderMutationPerRoute { | ||
Mutations mutations = 1; | ||
} | ||
|
||
// Configuration for the header mutation filter. The mutation rules in the filter configuration will | ||
// always be applied first and then the per-route mutation rules, if both are specified. | ||
message HeaderMutation { | ||
Mutations mutations = 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
docs/root/configuration/http/http_filters/header_mutation_filter.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
.. _config_http_filters_header_mutation: | ||
|
||
Header Mutation | ||
=============== | ||
|
||
* This filter should be configured with the type URL ``type.googleapis.com/envoy.extensions.filters.http.header_mutation.v3.HeaderMutation``. | ||
* :ref:`v3 API reference <envoy_v3_api_msg_extensions.filters.http.header_mutation.v3.HeaderMutation>` | ||
|
||
This is a filter that can be used to add, remove, append, or update HTTP headers. It can be added in any position in the filter chain | ||
and used as downstream or upstream HTTP filter. The filter can be configured to apply the header mutations to the request, response, or both. | ||
|
||
|
||
In most cases, this filter would be a more flexible alternative to the ``request_headers_to_add``, ``request_headers_to_remove``, | ||
``response_headers_to_add``, and ``response_headers_to_remove`` fields in the :ref:`route configuration <envoy_v3_api_msg_config.route.v3.RouteConfiguration>`. | ||
|
||
|
||
The filter provides complete control over the position and order of the header mutations. It may be used to influence later route picks if | ||
the route cache is cleared by a filter executing after the header mutation filter. | ||
|
||
|
||
In addition, this filter can be used as upstream filter and mutate the request headers after load balancing and host selection. | ||
|
||
|
||
Please note that as an encoder filter, this filter follows the standard rules of when it will execute in situations such as local replies - response | ||
headers will not be unconditionally added in cases where the filter would be bypassed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#include "source/common/http/header_mutation.h" | ||
|
||
#include "source/common/router/header_parser.h" | ||
|
||
namespace Envoy { | ||
namespace Http { | ||
|
||
namespace { | ||
|
||
using HeaderAppendAction = envoy::config::core::v3::HeaderValueOption::HeaderAppendAction; | ||
using HeaderValueOption = envoy::config::core::v3::HeaderValueOption; | ||
|
||
// TODO(wbpcode): Inherit from Envoy::Router::HeadersToAddEntry to make sure the formatter | ||
// has the same behavior as the router's formatter. We should try to find a more clean way | ||
// to reuse the formatter after the router's formatter is completely removed. | ||
class AppendMutation : public HeaderEvaluator, public Envoy::Router::HeadersToAddEntry { | ||
public: | ||
AppendMutation(const HeaderValueOption& header_value_option) | ||
: HeadersToAddEntry(header_value_option), header_name_(header_value_option.header().key()) {} | ||
|
||
void evaluateHeaders(Http::HeaderMap& headers, const Http::RequestHeaderMap& request_headers, | ||
const Http::ResponseHeaderMap& response_headers, | ||
const StreamInfo::StreamInfo& stream_info) const override { | ||
std::string value = formatter_->format(request_headers, response_headers, stream_info); | ||
|
||
if (!value.empty() || add_if_empty_) { | ||
switch (append_action_) { | ||
PANIC_ON_PROTO_ENUM_SENTINEL_VALUES; | ||
case HeaderValueOption::APPEND_IF_EXISTS_OR_ADD: | ||
headers.addReferenceKey(header_name_, value); | ||
return; | ||
case HeaderValueOption::ADD_IF_ABSENT: { | ||
auto header = headers.get(header_name_); | ||
if (!header.empty()) { | ||
return; | ||
} | ||
headers.addReferenceKey(header_name_, value); | ||
break; | ||
} | ||
case HeaderValueOption::OVERWRITE_IF_EXISTS_OR_ADD: | ||
headers.setReferenceKey(header_name_, value); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
private: | ||
Envoy::Http::LowerCaseString header_name_; | ||
}; | ||
|
||
class RemoveMutation : public HeaderEvaluator { | ||
public: | ||
RemoveMutation(const std::string& header_name) : header_name_(header_name) {} | ||
|
||
void evaluateHeaders(Http::HeaderMap& headers, const Http::RequestHeaderMap&, | ||
const Http::ResponseHeaderMap&, | ||
const StreamInfo::StreamInfo&) const override { | ||
headers.remove(header_name_); | ||
} | ||
|
||
private: | ||
const Envoy::Http::LowerCaseString header_name_; | ||
}; | ||
} // namespace | ||
|
||
HeaderMutations::HeaderMutations(const ProtoHeaderMutatons& header_mutations) { | ||
for (const auto& mutation : header_mutations) { | ||
switch (mutation.action_case()) { | ||
case envoy::config::common::mutation_rules::v3::HeaderMutation::ActionCase::kAppend: | ||
header_mutations_.emplace_back(std::make_unique<AppendMutation>(mutation.append())); | ||
break; | ||
case envoy::config::common::mutation_rules::v3::HeaderMutation::ActionCase::kRemove: | ||
header_mutations_.emplace_back(std::make_unique<RemoveMutation>(mutation.remove())); | ||
break; | ||
default: | ||
PANIC_DUE_TO_PROTO_UNSET; | ||
} | ||
} | ||
} | ||
|
||
void HeaderMutations::evaluateHeaders(Http::HeaderMap& headers, | ||
const Http::RequestHeaderMap& request_headers, | ||
const Http::ResponseHeaderMap& response_headers, | ||
const StreamInfo::StreamInfo& stream_info) const { | ||
for (const auto& mutation : header_mutations_) { | ||
mutation->evaluateHeaders(headers, request_headers, response_headers, stream_info); | ||
} | ||
} | ||
|
||
} // namespace Http | ||
} // namespace Envoy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#pragma once | ||
|
||
#include "envoy/config/common/mutation_rules/v3/mutation_rules.pb.h" | ||
#include "envoy/http/header_evaluator.h" | ||
|
||
#include "source/common/protobuf/protobuf.h" | ||
|
||
namespace Envoy { | ||
namespace Http { | ||
|
||
using ProtoHeaderMutatons = | ||
Protobuf::RepeatedPtrField<envoy::config::common::mutation_rules::v3::HeaderMutation>; | ||
using ProtoHeaderValueOption = envoy::config::core::v3::HeaderValueOption; | ||
|
||
class HeaderMutations : public HeaderEvaluator { | ||
public: | ||
HeaderMutations(const ProtoHeaderMutatons& header_mutations); | ||
|
||
// Http::HeaderEvaluator | ||
void evaluateHeaders(Http::HeaderMap& headers, const Http::RequestHeaderMap& request_headers, | ||
const Http::ResponseHeaderMap& response_headers, | ||
const StreamInfo::StreamInfo& stream_info) const override; | ||
|
||
private: | ||
std::vector<std::unique_ptr<HeaderEvaluator>> header_mutations_; | ||
}; | ||
|
||
} // namespace Http | ||
} // namespace Envoy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
load( | ||
"//bazel:envoy_build_system.bzl", | ||
"envoy_cc_extension", | ||
"envoy_cc_library", | ||
"envoy_extension_package", | ||
) | ||
|
||
licenses(["notice"]) # Apache 2 | ||
|
||
envoy_extension_package() | ||
|
||
envoy_cc_library( | ||
name = "header_mutation_lib", | ||
srcs = ["header_mutation.cc"], | ||
hdrs = ["header_mutation.h"], | ||
deps = [ | ||
"//envoy/server:filter_config_interface", | ||
"//source/common/config:utility_lib", | ||
"//source/common/http:header_map_lib", | ||
"//source/common/http:header_mutation_lib", | ||
"//source/common/protobuf:utility_lib", | ||
"//source/extensions/filters/http/common:pass_through_filter_lib", | ||
"@envoy_api//envoy/extensions/filters/http/header_mutation/v3:pkg_cc_proto", | ||
], | ||
) | ||
|
||
envoy_cc_extension( | ||
name = "config", | ||
srcs = ["config.cc"], | ||
hdrs = ["config.h"], | ||
deps = [ | ||
":header_mutation_lib", | ||
"//envoy/registry", | ||
"//source/common/protobuf:utility_lib", | ||
"//source/extensions/filters/http/common:factory_base_lib", | ||
"@envoy_api//envoy/extensions/filters/http/header_mutation/v3:pkg_cc_proto", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include "source/extensions/filters/http/header_mutation/config.h" | ||
|
||
#include <memory> | ||
|
||
#include "envoy/registry/registry.h" | ||
|
||
namespace Envoy { | ||
namespace Extensions { | ||
namespace HttpFilters { | ||
namespace HeaderMutation { | ||
|
||
Http::FilterFactoryCb HeaderMutationFactoryConfig::createFilterFactoryFromProtoTyped( | ||
const ProtoConfig& config, const std::string&, DualInfo, | ||
Server::Configuration::ServerFactoryContext&) { | ||
auto filter_config = std::make_shared<HeaderMutationConfig>(config); | ||
return [filter_config](Http::FilterChainFactoryCallbacks& callbacks) -> void { | ||
callbacks.addStreamFilter(std::make_shared<HeaderMutation>(filter_config)); | ||
}; | ||
} | ||
|
||
Router::RouteSpecificFilterConfigConstSharedPtr | ||
HeaderMutationFactoryConfig::createRouteSpecificFilterConfigTyped( | ||
const PerRouteProtoConfig& proto_config, Server::Configuration::ServerFactoryContext&, | ||
ProtobufMessage::ValidationVisitor&) { | ||
return std::make_shared<PerRouteHeaderMutation>(proto_config); | ||
} | ||
|
||
using UpstreamHeaderMutationFactoryConfig = HeaderMutationFactoryConfig; | ||
|
||
REGISTER_FACTORY(HeaderMutationFactoryConfig, Server::Configuration::NamedHttpFilterConfigFactory); | ||
REGISTER_FACTORY(UpstreamHeaderMutationFactoryConfig, | ||
Server::Configuration::UpstreamHttpFilterConfigFactory); | ||
|
||
} // namespace HeaderMutation | ||
} // namespace HttpFilters | ||
} // namespace Extensions | ||
} // namespace Envoy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#pragma once | ||
|
||
#include "envoy/extensions/filters/http/header_mutation/v3/header_mutation.pb.h" | ||
#include "envoy/extensions/filters/http/header_mutation/v3/header_mutation.pb.validate.h" | ||
|
||
#include "source/extensions/filters/http/common/factory_base.h" | ||
#include "source/extensions/filters/http/header_mutation/header_mutation.h" | ||
|
||
namespace Envoy { | ||
namespace Extensions { | ||
namespace HttpFilters { | ||
namespace HeaderMutation { | ||
|
||
/** | ||
* Config registration for the stateful session filter. @see NamedHttpFilterConfigFactory. | ||
*/ | ||
class HeaderMutationFactoryConfig | ||
: public Common::DualFactoryBase<ProtoConfig, PerRouteProtoConfig> { | ||
public: | ||
HeaderMutationFactoryConfig() : DualFactoryBase("envoy.filters.http.header_mutation") {} | ||
|
||
private: | ||
Http::FilterFactoryCb | ||
createFilterFactoryFromProtoTyped(const ProtoConfig& proto_config, | ||
const std::string& stats_prefix, DualInfo info, | ||
Server::Configuration::ServerFactoryContext& context) override; | ||
Router::RouteSpecificFilterConfigConstSharedPtr | ||
createRouteSpecificFilterConfigTyped(const PerRouteProtoConfig& proto_config, | ||
Server::Configuration::ServerFactoryContext&, | ||
ProtobufMessage::ValidationVisitor&) override; | ||
}; | ||
|
||
} // namespace HeaderMutation | ||
} // namespace HttpFilters | ||
} // namespace Extensions | ||
} // namespace Envoy |
Oops, something went wrong.