Skip to content

Commit

Permalink
Common TunnelResponseHeadersOrTrailers for UDP & TCP tunneling (#32619)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
IssaAbuKalbein committed Mar 6, 2024
1 parent 517f253 commit a19dce3
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 46 deletions.
1 change: 1 addition & 0 deletions envoy/http/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
Expand Down
9 changes: 9 additions & 0 deletions envoy/http/header_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -781,6 +782,14 @@ using ResponseTrailerMapConstSharedPtr = std::shared_ptr<const ResponseTrailerMa
using ResponseTrailerMapOptRef = OptRef<ResponseTrailerMap>;
using ResponseTrailerMapOptConstRef = OptRef<const ResponseTrailerMap>;

/**
* 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.
*/
Expand Down
1 change: 1 addition & 0 deletions source/common/http/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)

Expand Down
11 changes: 11 additions & 0 deletions source/common/http/header_map_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<envoy::config::core::v3::HeaderMap>();
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 <class T>
HeaderMapImplUtility::HeaderMapImplInfo makeHeaderMapImplInfo(absl::string_view name) {
Expand Down
6 changes: 6 additions & 0 deletions source/common/http/header_map_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <type_traits>

#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"
Expand Down Expand Up @@ -636,6 +637,11 @@ class ResponseTrailerMapImpl final : public TypedHeaderMapImpl<ResponseTrailerMa
HeaderEntryImpl* inline_headers_[];
};

class TunnelResponseHeadersOrTrailersImpl : public TunnelResponseHeadersOrTrailers {
public:
ProtobufTypes::MessagePtr serializeAsProto() const override;
};

template <class T>
std::unique_ptr<T>
createHeaderMap(const std::initializer_list<std::pair<LowerCaseString, std::string>>& values) {
Expand Down
1 change: 0 additions & 1 deletion source/common/tcp_proxy/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)
12 changes: 0 additions & 12 deletions source/common/tcp_proxy/tcp_proxy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -632,17 +631,6 @@ const Router::MetadataMatchCriteria* Filter::metadataMatchCriteria() {
}
}

ProtobufTypes::MessagePtr TunnelResponseHeadersOrTrailers::serializeAsProto() const {
auto proto_out = std::make_unique<envoy::config::core::v3::HeaderMap>();
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");
}
Expand Down
13 changes: 2 additions & 11 deletions source/common/tcp_proxy/tcp_proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,10 @@ using RouteConstSharedPtr = std::shared_ptr<const Route>;
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)) {}
Expand All @@ -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)) {}
Expand Down
11 changes: 0 additions & 11 deletions source/extensions/filters/udp/udp_proxy/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<envoy::config::core::v3::HeaderMap>();
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");
}
Expand Down
13 changes: 2 additions & 11 deletions source/extensions/filters/udp/udp_proxy/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {}
Expand All @@ -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)) {}
Expand Down

0 comments on commit a19dce3

Please sign in to comment.