Skip to content

Commit

Permalink
cluster: adding host rewrite (envoyproxy#17083)
Browse files Browse the repository at this point in the history
Risk Level: low (config guarded addition)
Testing: new unit tests
Docs Changes: in API docs
Release Notes: inline
Fixes envoyproxy#16775

Signed-off-by: Alyssa Wilk <[email protected]>
  • Loading branch information
alyssawilk authored Jun 24, 2021
1 parent 70c436d commit 818f8ed
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 5 deletions.
9 changes: 8 additions & 1 deletion api/envoy/config/route/v3/route_components.proto
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ message Route {
message WeightedCluster {
option (udpa.annotations.versioning).previous_message_type = "envoy.api.v2.route.WeightedCluster";

// [#next-free-field: 11]
// [#next-free-field: 12]
message ClusterWeight {
option (udpa.annotations.versioning).previous_message_type =
"envoy.api.v2.route.WeightedCluster.ClusterWeight";
Expand Down Expand Up @@ -378,6 +378,13 @@ message WeightedCluster {
// :ref:`FilterConfig<envoy_v3_api_msg_config.route.v3.FilterConfig>`
// message to specify additional options.]
map<string, google.protobuf.Any> typed_per_filter_config = 10;

oneof host_rewrite_specifier {
// Indicates that during forwarding, the host header will be swapped with
// this value.
string host_rewrite_literal = 11
[(validate.rules).string = {well_known_regex: HTTP_HEADER_VALUE strict: false}];
}
}

// Specifies one or more upstream clusters associated with the route.
Expand Down
9 changes: 8 additions & 1 deletion api/envoy/config/route/v4alpha/route_components.proto

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/root/version_history/current.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ New Features
* bandwidth_limit: added new :ref:`HTTP bandwidth limit filter <config_http_filters_bandwidth_limit>`.
* bootstrap: added :ref:`dns_resolution_config <envoy_v3_api_field_config.bootstrap.v3.Bootstrap.dns_resolution_config>` to aggregate all of the DNS resolver configuration in a single message. By setting one such configuration option ``no_default_search_domain`` as true the DNS resolver will not use the default search domains. And by setting the configuration ``resolvers`` we can specify the external DNS servers to be used for external DNS query.
* cluster: added :ref:`dns_resolution_config <envoy_v3_api_field_config.cluster.v3.Cluster.dns_resolution_config>` to aggregate all of the DNS resolver configuration in a single message. By setting one such configuration option ``no_default_search_domain`` as true the DNS resolver will not use the default search domains.
* cluster: added :ref:`host_rewrite_literal <envoy_v3_api_field_config.route.v3.WeightedCluster.ClusterWeight.host_rewrite_literal>` to WeightedCluster.
* composite filter: can now be used with filters that also add an access logger, such as the WASM filter.
* config: added stat :ref:`config_reload_time_ms <subscription_statistics>`.
* connection_limit: added new :ref:`Network connection limit filter <config_network_filters_connection_limit>`.
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion source/common/router/config_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,8 @@ RouteEntryImplBase::WeightedClusterEntry::WeightedClusterEntry(
cluster.response_headers_to_remove())),
per_filter_configs_(cluster.typed_per_filter_config(),
cluster.hidden_envoy_deprecated_per_filter_config(),
optional_http_filters, factory_context, validator) {
optional_http_filters, factory_context, validator),
host_rewrite_(cluster.host_rewrite_literal()) {
if (cluster.has_metadata_match()) {
const auto filter_it = cluster.metadata_match().filter_metadata().find(
Envoy::Config::MetadataFilters::get().ENVOY_LB);
Expand Down
4 changes: 4 additions & 0 deletions source/common/router/config_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,9 @@ class RouteEntryImplBase : public RouteEntry,
const StreamInfo::StreamInfo& stream_info,
bool insert_envoy_original_path) const override {
request_headers_parser_->evaluateHeaders(headers, stream_info);
if (!host_rewrite_.empty()) {
headers.setHost(host_rewrite_);
}
DynamicRouteEntry::finalizeRequestHeaders(headers, stream_info, insert_envoy_original_path);
}
void finalizeResponseHeaders(Http::ResponseHeaderMap& headers,
Expand All @@ -791,6 +794,7 @@ class RouteEntryImplBase : public RouteEntry,
HeaderParserPtr request_headers_parser_;
HeaderParserPtr response_headers_parser_;
PerFilterConfigs per_filter_configs_;
const std::string host_rewrite_;
};

using WeightedClusterEntrySharedPtr = std::shared_ptr<WeightedClusterEntry>;
Expand Down
2 changes: 2 additions & 0 deletions test/common/router/config_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5202,6 +5202,7 @@ TEST_F(RouteMatcherTest, TestWeightedClusterHeaderManipulation) {
key: x-resp-cluster
value: cluster1
response_headers_to_remove: [ "x-remove-cluster1" ]
host_rewrite_literal: "new_host1"
- name: cluster2
weight: 50
request_headers_to_add:
Expand All @@ -5227,6 +5228,7 @@ TEST_F(RouteMatcherTest, TestWeightedClusterHeaderManipulation) {

route->finalizeRequestHeaders(headers, stream_info, true);
EXPECT_EQ("cluster1", headers.get_("x-req-cluster"));
EXPECT_EQ("new_host1", headers.getHostValue());

route->finalizeResponseHeaders(resp_headers, stream_info);
EXPECT_EQ("cluster1", resp_headers.get_("x-resp-cluster"));
Expand Down

0 comments on commit 818f8ed

Please sign in to comment.