From a19dce320722859214df5e61f69cd29bce9e3a90 Mon Sep 17 00:00:00 2001 From: IssaAbuKalbein <86603440+IssaAbuKalbein@users.noreply.github.com> Date: Thu, 7 Mar 2024 00:23:59 +0200 Subject: [PATCH] Common TunnelResponseHeadersOrTrailers for UDP & TCP tunneling (#32619) Commit Message: Common TunnelResponseHeadersOrTrailers for UDP & TCP tunneling Additional Description: Risk Level: low Testing: Docs Changes: N/A Release Notes: N/A Platform Specific Features: None --- envoy/http/BUILD | 1 + envoy/http/header_map.h | 9 +++++++++ source/common/http/BUILD | 1 + source/common/http/header_map_impl.cc | 11 +++++++++++ source/common/http/header_map_impl.h | 6 ++++++ source/common/tcp_proxy/BUILD | 1 - source/common/tcp_proxy/tcp_proxy.cc | 12 ------------ source/common/tcp_proxy/tcp_proxy.h | 13 ++----------- source/extensions/filters/udp/udp_proxy/config.cc | 11 ----------- source/extensions/filters/udp/udp_proxy/config.h | 13 ++----------- 10 files changed, 32 insertions(+), 46 deletions(-) diff --git a/envoy/http/BUILD b/envoy/http/BUILD index ba992698b4ee..84c59df4bc2a 100644 --- a/envoy/http/BUILD +++ b/envoy/http/BUILD @@ -140,6 +140,7 @@ envoy_cc_library( deps = [ ":header_formatter_interface", "//envoy/common:union_string", + "//envoy/stream_info:filter_state_interface", "//source/common/common:assert_lib", "//source/common/common:hash_lib", ], diff --git a/envoy/http/header_map.h b/envoy/http/header_map.h index 0e95b6db91dd..e53cc5b00622 100644 --- a/envoy/http/header_map.h +++ b/envoy/http/header_map.h @@ -13,6 +13,7 @@ #include "envoy/common/pure.h" #include "envoy/common/union_string.h" #include "envoy/http/header_formatter.h" +#include "envoy/stream_info/filter_state.h" #include "source/common/common/assert.h" #include "source/common/common/hash.h" @@ -781,6 +782,14 @@ using ResponseTrailerMapConstSharedPtr = std::shared_ptr; using ResponseTrailerMapOptConstRef = OptRef; +/** + * Base class for both tunnel response headers and trailers. + */ +class TunnelResponseHeadersOrTrailers : public StreamInfo::FilterState::Object { +public: + virtual const HeaderMap& value() const PURE; +}; + /** * Convenient container type for storing Http::LowerCaseString and std::string key/value pairs. */ diff --git a/source/common/http/BUILD b/source/common/http/BUILD index 444b77bcd70e..de2969ce6056 100644 --- a/source/common/http/BUILD +++ b/source/common/http/BUILD @@ -444,6 +444,7 @@ envoy_cc_library( "//source/common/common:utility_lib", "//source/common/runtime:runtime_features_lib", "//source/common/singleton:const_singleton", + "@envoy_api//envoy/config/core/v3:pkg_cc_proto", ], ) diff --git a/source/common/http/header_map_impl.cc b/source/common/http/header_map_impl.cc index 1431e66ffe6b..7b4827e673c9 100644 --- a/source/common/http/header_map_impl.cc +++ b/source/common/http/header_map_impl.cc @@ -499,6 +499,17 @@ size_t HeaderMapImpl::removeInline(HeaderEntryImpl** ptr_to_entry) { return 1; } +ProtobufTypes::MessagePtr TunnelResponseHeadersOrTrailersImpl::serializeAsProto() const { + auto proto_out = std::make_unique(); + value().iterate([&proto_out](const HeaderEntry& e) -> HeaderMap::Iterate { + auto* new_header = proto_out->add_headers(); + new_header->set_key(std::string(e.key().getStringView())); + new_header->set_value(std::string(e.value().getStringView())); + return HeaderMap::Iterate::Continue; + }); + return proto_out; +} + namespace { template HeaderMapImplUtility::HeaderMapImplInfo makeHeaderMapImplInfo(absl::string_view name) { diff --git a/source/common/http/header_map_impl.h b/source/common/http/header_map_impl.h index df7c911e93de..1a7f6039f24b 100644 --- a/source/common/http/header_map_impl.h +++ b/source/common/http/header_map_impl.h @@ -8,6 +8,7 @@ #include #include "envoy/common/optref.h" +#include "envoy/config/core/v3/base.pb.h" #include "envoy/http/header_map.h" #include "source/common/common/non_copyable.h" @@ -636,6 +637,11 @@ class ResponseTrailerMapImpl final : public TypedHeaderMapImpl std::unique_ptr createHeaderMap(const std::initializer_list>& values) { diff --git a/source/common/tcp_proxy/BUILD b/source/common/tcp_proxy/BUILD index 3e1f10dba933..966088ed4a08 100644 --- a/source/common/tcp_proxy/BUILD +++ b/source/common/tcp_proxy/BUILD @@ -83,7 +83,6 @@ envoy_cc_library( "//source/common/upstream:load_balancer_lib", "//source/extensions/upstreams/tcp/generic:config", "@envoy_api//envoy/config/accesslog/v3:pkg_cc_proto", - "@envoy_api//envoy/config/core/v3:pkg_cc_proto", "@envoy_api//envoy/extensions/filters/network/tcp_proxy/v3:pkg_cc_proto", ], ) diff --git a/source/common/tcp_proxy/tcp_proxy.cc b/source/common/tcp_proxy/tcp_proxy.cc index 4e49e16597a8..f60f31ec44db 100644 --- a/source/common/tcp_proxy/tcp_proxy.cc +++ b/source/common/tcp_proxy/tcp_proxy.cc @@ -6,7 +6,6 @@ #include "envoy/buffer/buffer.h" #include "envoy/config/accesslog/v3/accesslog.pb.h" -#include "envoy/config/core/v3/base.pb.h" #include "envoy/event/dispatcher.h" #include "envoy/event/timer.h" #include "envoy/extensions/filters/network/tcp_proxy/v3/tcp_proxy.pb.h" @@ -632,17 +631,6 @@ const Router::MetadataMatchCriteria* Filter::metadataMatchCriteria() { } } -ProtobufTypes::MessagePtr TunnelResponseHeadersOrTrailers::serializeAsProto() const { - auto proto_out = std::make_unique(); - value().iterate([&proto_out](const Http::HeaderEntry& e) -> Http::HeaderMap::Iterate { - auto* new_header = proto_out->add_headers(); - new_header->set_key(std::string(e.key().getStringView())); - new_header->set_value(std::string(e.value().getStringView())); - return Http::HeaderMap::Iterate::Continue; - }); - return proto_out; -} - const std::string& TunnelResponseHeaders::key() { CONSTRUCT_ON_FIRST_USE(std::string, "envoy.tcp_proxy.propagate_response_headers"); } diff --git a/source/common/tcp_proxy/tcp_proxy.h b/source/common/tcp_proxy/tcp_proxy.h index a7b5e491191d..45f34b901405 100644 --- a/source/common/tcp_proxy/tcp_proxy.h +++ b/source/common/tcp_proxy/tcp_proxy.h @@ -116,19 +116,10 @@ using RouteConstSharedPtr = std::shared_ptr; using TunnelingConfig = envoy::extensions::filters::network::tcp_proxy::v3::TcpProxy_TunnelingConfig; -/** - * Base class for both tunnel response headers and trailers. - */ -class TunnelResponseHeadersOrTrailers : public StreamInfo::FilterState::Object { -public: - ProtobufTypes::MessagePtr serializeAsProto() const override; - virtual const Http::HeaderMap& value() const PURE; -}; - /** * Response headers for the tunneling connections. */ -class TunnelResponseHeaders : public TunnelResponseHeadersOrTrailers { +class TunnelResponseHeaders : public Http::TunnelResponseHeadersOrTrailersImpl { public: TunnelResponseHeaders(Http::ResponseHeaderMapPtr&& response_headers) : response_headers_(std::move(response_headers)) {} @@ -142,7 +133,7 @@ class TunnelResponseHeaders : public TunnelResponseHeadersOrTrailers { /** * Response trailers for the tunneling connections. */ -class TunnelResponseTrailers : public TunnelResponseHeadersOrTrailers { +class TunnelResponseTrailers : public Http::TunnelResponseHeadersOrTrailersImpl { public: TunnelResponseTrailers(Http::ResponseTrailerMapPtr&& response_trailers) : response_trailers_(std::move(response_trailers)) {} diff --git a/source/extensions/filters/udp/udp_proxy/config.cc b/source/extensions/filters/udp/udp_proxy/config.cc index 5675cdc4c537..ea860409033a 100644 --- a/source/extensions/filters/udp/udp_proxy/config.cc +++ b/source/extensions/filters/udp/udp_proxy/config.cc @@ -11,17 +11,6 @@ constexpr uint32_t DefaultMaxConnectAttempts = 1; constexpr uint32_t DefaultMaxBufferedDatagrams = 1024; constexpr uint64_t DefaultMaxBufferedBytes = 16384; -ProtobufTypes::MessagePtr TunnelResponseHeadersOrTrailers::serializeAsProto() const { - auto proto_out = std::make_unique(); - value().iterate([&proto_out](const Http::HeaderEntry& e) -> Http::HeaderMap::Iterate { - auto* new_header = proto_out->add_headers(); - new_header->set_key(std::string(e.key().getStringView())); - new_header->set_value(std::string(e.value().getStringView())); - return Http::HeaderMap::Iterate::Continue; - }); - return proto_out; -} - const std::string& TunnelResponseHeaders::key() { CONSTRUCT_ON_FIRST_USE(std::string, "envoy.udp_proxy.propagate_response_headers"); } diff --git a/source/extensions/filters/udp/udp_proxy/config.h b/source/extensions/filters/udp/udp_proxy/config.h index 6778ea3b0992..29793bf6d2a4 100644 --- a/source/extensions/filters/udp/udp_proxy/config.h +++ b/source/extensions/filters/udp/udp_proxy/config.h @@ -14,19 +14,10 @@ namespace UdpProxy { using TunnelingConfig = envoy::extensions::filters::udp::udp_proxy::v3::UdpProxyConfig::UdpTunnelingConfig; -/** - * Base class for both tunnel response headers and trailers. - */ -class TunnelResponseHeadersOrTrailers : public StreamInfo::FilterState::Object { -public: - ProtobufTypes::MessagePtr serializeAsProto() const override; - virtual const Http::HeaderMap& value() const PURE; -}; - /** * Response headers for the tunneling connections. */ -class TunnelResponseHeaders : public TunnelResponseHeadersOrTrailers { +class TunnelResponseHeaders : public Http::TunnelResponseHeadersOrTrailersImpl { public: TunnelResponseHeaders(Http::ResponseHeaderMapPtr&& response_headers) : response_headers_(std::move(response_headers)) {} @@ -40,7 +31,7 @@ class TunnelResponseHeaders : public TunnelResponseHeadersOrTrailers { /** * Response trailers for the tunneling connections. */ -class TunnelResponseTrailers : public TunnelResponseHeadersOrTrailers { +class TunnelResponseTrailers : public Http::TunnelResponseHeadersOrTrailersImpl { public: TunnelResponseTrailers(Http::ResponseTrailerMapPtr&& response_trailers) : response_trailers_(std::move(response_trailers)) {}