Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tcp_proxy: extend tunneling_config with auto_sni #20230

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,14 @@ message TcpProxy {
option (udpa.annotations.versioning).previous_message_type =
"envoy.config.filter.network.tcp_proxy.v2.TcpProxy.TunnelingConfig";

message AutoSni {
// The port to which the downstream connection is tunneled.
// When the port has 0 value, then 443 is used in the request-target.
uint32 port = 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uint32 is not an optional type. Do you want to use Uint32 or clarify the default_port means value 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're right. I used uint32 intentionally and by "optional" I meant that 0 value will be handled as 443. I will fix this description.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

}

// The hostname to send in the synthesized CONNECT headers to the upstream proxy.
string hostname = 1 [(validate.rules).string = {min_len: 1}];
string hostname = 1;

// Use POST method instead of CONNECT method to tunnel the TCP stream.
// The 'protocol: bytestream' header is also NOT set for HTTP/2 to comply with the spec.
Expand All @@ -83,6 +89,12 @@ message TcpProxy {
// Neither *:-prefixed* pseudo-headers nor the Host: header can be overridden.
repeated config.core.v3.HeaderValueOption headers_to_add = 3
[(validate.rules).repeated = {max_items: 1000}];

// Automatically set hostname in the request-target for the underlying tunnel request
// using SNI from a downstream connection.
// Supports only TLS connections and requires to enable envoy.filters.listener.tls_inspector.
// In case of plain TCP request, upstream connection cannot be established and request is refused.
AutoSni auto_sni = 4;
}

message OnDemand {
Expand Down
4 changes: 4 additions & 0 deletions envoy/tcp/upstream.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class TunnelingConfigHelper {
// The host name of the tunneling upstream HTTP request.
virtual const std::string& hostname() const PURE;

virtual bool useAutoSni() const PURE;

virtual int defaultPort() const PURE;

// The method of the upstream HTTP request. True if using POST method, CONNECT otherwise.
virtual bool usePost() const PURE;

Expand Down
8 changes: 7 additions & 1 deletion source/common/tcp_proxy/tcp_proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,20 @@ class TunnelingConfigHelperImpl : public TunnelingConfigHelper {
TunnelingConfigHelperImpl(
const envoy::extensions::filters::network::tcp_proxy::v3::TcpProxy_TunnelingConfig&
config_message)
: hostname_(config_message.hostname()), use_post_(config_message.use_post()),
: hostname_(config_message.hostname()), use_auto_sni_(config_message.has_auto_sni()),
default_port_(config_message.has_auto_sni() ? config_message.auto_sni().port() : 0),
use_post_(config_message.use_post()),
header_parser_(Envoy::Router::HeaderParser::configure(config_message.headers_to_add())) {}
const std::string& hostname() const override { return hostname_; }
bool useAutoSni() const override { return use_auto_sni_; }
int defaultPort() const override { return default_port_; }
bool usePost() const override { return use_post_; }
Envoy::Http::HeaderEvaluator& headerEvaluator() const override { return *header_parser_; }

private:
const std::string hostname_;
const bool use_auto_sni_;
const int default_port_;
const bool use_post_;
std::unique_ptr<Envoy::Router::HeaderParser> header_parser_;
};
Expand Down
19 changes: 16 additions & 3 deletions source/common/tcp_proxy/upstream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "source/common/http/utility.h"
#include "source/common/runtime/runtime_features.h"

#include "fmt/printf.h"

namespace Envoy {
namespace TcpProxy {
using TunnelingConfig =
Expand Down Expand Up @@ -270,12 +272,17 @@ bool Http2Upstream::isValidResponse(const Http::ResponseHeaderMap& headers) {
void Http2Upstream::setRequestEncoder(Http::RequestEncoder& request_encoder, bool is_ssl) {
request_encoder_ = &request_encoder;
request_encoder_->getStream().addCallbacks(*this);

const std::string& hostname =
config_.useAutoSni()
? fmt::sprintf("%s:%d",
downstream_info_.downstreamAddressProvider().requestedServerName(),
config_.defaultPort())
: config_.hostname();
const std::string& scheme =
is_ssl ? Http::Headers::get().SchemeValues.Https : Http::Headers::get().SchemeValues.Http;
auto headers = Http::createHeaderMap<Http::RequestHeaderMapImpl>({
{Http::Headers::get().Method, config_.usePost() ? "POST" : "CONNECT"},
{Http::Headers::get().Host, config_.hostname()},
{Http::Headers::get().Host, hostname},
{Http::Headers::get().Path, "/"},
{Http::Headers::get().Scheme, scheme},
});
Expand All @@ -302,9 +309,15 @@ void Http1Upstream::setRequestEncoder(Http::RequestEncoder& request_encoder, boo
request_encoder_->enableTcpTunneling();
ASSERT(request_encoder_->http1StreamEncoderOptions() != absl::nullopt);

const std::string& hostname =
config_.useAutoSni()
? fmt::sprintf("%s:%d",
downstream_info_.downstreamAddressProvider().requestedServerName(),
config_.defaultPort())
: config_.hostname();
auto headers = Http::createHeaderMap<Http::RequestHeaderMapImpl>({
{Http::Headers::get().Method, config_.usePost() ? "POST" : "CONNECT"},
{Http::Headers::get().Host, config_.hostname()},
{Http::Headers::get().Host, hostname},
});

if (config_.usePost()) {
Expand Down