Skip to content

Commit 48b161e

Browse files
vadimeisenbergibmhtuch
authored andcommitted
tls: add functionality to override requested server name in the upstream cluster (istio#4973)
Add functionality to override requested server name in the upstream cluster Risk Level: medium Testing: unit tests Docs Changes: yes Release Notes: yes Related to istio#4076 Signed-off-by: Vadim Eisenberg <[email protected]>
1 parent 08310eb commit 48b161e

File tree

68 files changed

+889
-293
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+889
-293
lines changed

include/envoy/network/transport_socket.h

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include "envoy/common/pure.h"
55
#include "envoy/ssl/connection.h"
66

7+
#include "absl/types/optional.h"
8+
79
namespace Envoy {
810
namespace Network {
911

@@ -136,6 +138,32 @@ class TransportSocket {
136138

137139
typedef std::unique_ptr<TransportSocket> TransportSocketPtr;
138140

141+
/**
142+
* Options for creating transport sockets.
143+
*/
144+
class TransportSocketOptions {
145+
public:
146+
virtual ~TransportSocketOptions() {}
147+
148+
/**
149+
* @return the const optional server name to set in the transport socket, for example SNI for
150+
* SSL, regardless of the upstream cluster configuration. Filters that influence
151+
* upstream connection selection, such as tcp_proxy, should take this option into account
152+
* and should pass it through to the connection pool to ensure the correct endpoints are
153+
* selected and the upstream connection is set up accordingly.
154+
*/
155+
virtual const absl::optional<std::string>& serverNameOverride() const PURE;
156+
157+
/**
158+
* @param vector of bytes to which the option should append hash key data that will be used
159+
* to separate connections based on the option. Any data already in the key vector must
160+
* not be modified.
161+
*/
162+
virtual void hashKey(std::vector<uint8_t>& key) const PURE;
163+
};
164+
165+
typedef std::shared_ptr<TransportSocketOptions> TransportSocketOptionsSharedPtr;
166+
139167
/**
140168
* A factory for creating transport socket. It will be associated to filter chains and clusters.
141169
*/
@@ -149,9 +177,11 @@ class TransportSocketFactory {
149177
virtual bool implementsSecureTransport() const PURE;
150178

151179
/**
180+
* @param options for creating the transport socket
152181
* @return Network::TransportSocketPtr a transport socket to be passed to connection.
153182
*/
154-
virtual TransportSocketPtr createTransportSocket() const PURE;
183+
virtual TransportSocketPtr
184+
createTransportSocket(TransportSocketOptionsSharedPtr options) const PURE;
155185
};
156186

157187
typedef std::unique_ptr<TransportSocketFactory> TransportSocketFactoryPtr;

include/envoy/upstream/cluster_manager.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,10 @@ class ClusterManager {
131131
* Can return nullptr if there is no host available in the cluster or if the cluster does not
132132
* exist.
133133
*/
134-
virtual Tcp::ConnectionPool::Instance* tcpConnPoolForCluster(const std::string& cluster,
135-
ResourcePriority priority,
136-
LoadBalancerContext* context) PURE;
134+
virtual Tcp::ConnectionPool::Instance*
135+
tcpConnPoolForCluster(const std::string& cluster, ResourcePriority priority,
136+
LoadBalancerContext* context,
137+
Network::TransportSocketOptionsSharedPtr transport_socket_options) PURE;
137138

138139
/**
139140
* Allocate a load balanced TCP connection for a cluster. The created connection is already
@@ -143,8 +144,9 @@ class ClusterManager {
143144
* Returns both a connection and the host that backs the connection. Both can be nullptr if there
144145
* is no host available in the cluster.
145146
*/
146-
virtual Host::CreateConnectionData tcpConnForCluster(const std::string& cluster,
147-
LoadBalancerContext* context) PURE;
147+
virtual Host::CreateConnectionData
148+
tcpConnForCluster(const std::string& cluster, LoadBalancerContext* context,
149+
Network::TransportSocketOptionsSharedPtr transport_socket_options) PURE;
148150

149151
/**
150152
* Returns a client that can be used to make async HTTP calls against the given cluster. The
@@ -271,7 +273,8 @@ class ClusterManagerFactory {
271273
virtual Tcp::ConnectionPool::InstancePtr
272274
allocateTcpConnPool(Event::Dispatcher& dispatcher, HostConstSharedPtr host,
273275
ResourcePriority priority,
274-
const Network::ConnectionSocket::OptionsSharedPtr& options) PURE;
276+
const Network::ConnectionSocket::OptionsSharedPtr& options,
277+
Network::TransportSocketOptionsSharedPtr transport_socket_options) PURE;
275278

276279
/**
277280
* Allocate a cluster from configuration proto.

include/envoy/upstream/upstream.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ class Host : virtual public HostDescription {
7373
*/
7474
virtual CreateConnectionData
7575
createConnection(Event::Dispatcher& dispatcher,
76-
const Network::ConnectionSocket::OptionsSharedPtr& options) const PURE;
76+
const Network::ConnectionSocket::OptionsSharedPtr& options,
77+
Network::TransportSocketOptionsSharedPtr transport_socket_options) const PURE;
7778

7879
/**
7980
* Create a health check connection for this host.

source/common/common/BUILD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,11 @@ envoy_cc_library(
273273
],
274274
)
275275

276+
envoy_cc_library(
277+
name = "scalar_to_byte_vector_lib",
278+
hdrs = ["scalar_to_byte_vector.h"],
279+
)
280+
276281
envoy_cc_library(
277282
name = "token_bucket_impl_lib",
278283
srcs = ["token_bucket_impl.cc"],
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
3+
#include <inttypes.h>
4+
5+
#include <vector>
6+
7+
namespace Envoy {
8+
template <typename T> void pushScalarToByteVector(T val, std::vector<uint8_t>& bytes) {
9+
uint8_t* byte_ptr = reinterpret_cast<uint8_t*>(&val);
10+
for (uint32_t byte_index = 0; byte_index < sizeof val; byte_index++) {
11+
bytes.push_back(*byte_ptr++);
12+
}
13+
}
14+
} // namespace Envoy

source/common/http/http1/conn_pool.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ ConnPoolImpl::ActiveClient::ActiveClient(ConnPoolImpl& parent)
285285
parent_.conn_connect_ms_ = std::make_unique<Stats::Timespan>(
286286
parent_.host_->cluster().stats().upstream_cx_connect_ms_, parent_.dispatcher_.timeSystem());
287287
Upstream::Host::CreateConnectionData data =
288-
parent_.host_->createConnection(parent_.dispatcher_, parent_.socket_options_);
288+
parent_.host_->createConnection(parent_.dispatcher_, parent_.socket_options_, nullptr);
289289
real_host_description_ = data.host_description_;
290290
codec_client_ = parent_.createCodecClient(data);
291291
codec_client_->addConnectionCallbacks(*this);

source/common/http/http2/conn_pool.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ ConnPoolImpl::ActiveClient::ActiveClient(ConnPoolImpl& parent)
262262
parent_.conn_connect_ms_ = std::make_unique<Stats::Timespan>(
263263
parent_.host_->cluster().stats().upstream_cx_connect_ms_, parent_.dispatcher_.timeSystem());
264264
Upstream::Host::CreateConnectionData data =
265-
parent_.host_->createConnection(parent_.dispatcher_, parent_.socket_options_);
265+
parent_.host_->createConnection(parent_.dispatcher_, parent_.socket_options_, nullptr);
266266
real_host_description_ = data.host_description_;
267267
client_ = parent_.createCodecClient(data);
268268
client_->addConnectionCallbacks(*this);

source/common/network/BUILD

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,24 @@ envoy_cc_library(
240240
"@envoy_api//envoy/api/v2/core:base_cc",
241241
],
242242
)
243+
244+
envoy_cc_library(
245+
name = "transport_socket_options_lib",
246+
srcs = ["transport_socket_options_impl.cc"],
247+
hdrs = ["transport_socket_options_impl.h"],
248+
deps = [
249+
"//include/envoy/network:transport_socket_interface",
250+
"//source/common/common:scalar_to_byte_vector_lib",
251+
"//source/common/common:utility_lib",
252+
],
253+
)
254+
255+
envoy_cc_library(
256+
name = "upstream_server_name_lib",
257+
srcs = ["upstream_server_name.cc"],
258+
hdrs = ["upstream_server_name.h"],
259+
deps = [
260+
"//include/envoy/stream_info:filter_state_interface",
261+
"//source/common/common:macros",
262+
],
263+
)

source/common/network/raw_buffer_socket.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ std::string RawBufferSocket::protocol() const { return EMPTY_STRING; }
8282

8383
void RawBufferSocket::onConnected() { callbacks_->raiseEvent(ConnectionEvent::Connected); }
8484

85-
TransportSocketPtr RawBufferSocketFactory::createTransportSocket() const {
85+
TransportSocketPtr
86+
RawBufferSocketFactory::createTransportSocket(TransportSocketOptionsSharedPtr) const {
8687
return std::make_unique<RawBufferSocket>();
8788
}
8889

source/common/network/raw_buffer_socket.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class RawBufferSocket : public TransportSocket, protected Logger::Loggable<Logge
2929
class RawBufferSocketFactory : public TransportSocketFactory {
3030
public:
3131
// Network::TransportSocketFactory
32-
TransportSocketPtr createTransportSocket() const override;
32+
TransportSocketPtr createTransportSocket(TransportSocketOptionsSharedPtr options) const override;
3333
bool implementsSecureTransport() const override;
3434
};
3535

0 commit comments

Comments
 (0)