forked from istio/proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extensions: Network filter to set upstream cluster from SNI (istio#4489)
*Description*: Use the SNI value as the upstream cluster name. This is similar to the cluster_header feature in HCM. Leverages the perConnectionState to dynamically control the cluster used by tcp_proxy filter. We plan to use this in Istio, where Pilot would manage two kubernetes setups, such that the envoys will have the same set of clusters, but the non-local clusters will have the IP of a Gateway envoy (edge/front envoy). mTLS traffic arriving at the gateway envoy will be routed to the internal (envoy)clusters based on the SNI value Depends on envoyproxy/envoy#4454 *Risk Level*: Low *Testing*: Unit tests *Docs Changes*: yes *Release Notes*: yes Signed-off-by: Shriram Rajagopalan <[email protected]>
- Loading branch information
Showing
18 changed files
with
346 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,4 @@ filters. | |
redis_proxy_filter | ||
tcp_proxy_filter | ||
thrift_proxy_filter | ||
sni_cluster_filter |
13 changes: 13 additions & 0 deletions
13
docs/root/configuration/network_filters/sni_cluster_filter.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
.. _config_network_filters_sni_cluster: | ||
|
||
Upstream Cluster from SNI | ||
========================= | ||
|
||
The `sni_cluster` is a network filter that uses the SNI value in a TLS | ||
connection as the upstream cluster name. The filter will not modify the | ||
upstream cluster for non-TLS connections. | ||
|
||
This filter has no configuration. It must be installed before the | ||
:ref:`tcp_proxy <config_network_filters_tcp_proxy>` filter. | ||
|
||
* :ref:`v2 API reference <envoy_api_field_listener.Filter.name>` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
licenses(["notice"]) # Apache 2 | ||
|
||
load( | ||
"//bazel:envoy_build_system.bzl", | ||
"envoy_cc_library", | ||
"envoy_package", | ||
) | ||
|
||
envoy_package() | ||
|
||
envoy_cc_library( | ||
name = "sni_cluster", | ||
srcs = ["sni_cluster.cc"], | ||
hdrs = ["sni_cluster.h"], | ||
deps = [ | ||
"//include/envoy/network:connection_interface", | ||
"//include/envoy/network:filter_interface", | ||
"//source/common/common:assert_lib", | ||
"//source/common/common:minimal_logger_lib", | ||
"//source/common/tcp_proxy", | ||
], | ||
) | ||
|
||
envoy_cc_library( | ||
name = "config", | ||
srcs = ["config.cc"], | ||
hdrs = ["config.h"], | ||
deps = [ | ||
":sni_cluster", | ||
"//include/envoy/registry", | ||
"//include/envoy/server:filter_config_interface", | ||
"//source/extensions/filters/network:well_known_names", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include "extensions/filters/network/sni_cluster/config.h" | ||
|
||
#include "envoy/registry/registry.h" | ||
#include "envoy/server/filter_config.h" | ||
|
||
#include "extensions/filters/network/sni_cluster/sni_cluster.h" | ||
|
||
namespace Envoy { | ||
namespace Extensions { | ||
namespace NetworkFilters { | ||
namespace SniCluster { | ||
|
||
Network::FilterFactoryCb | ||
SniClusterNetworkFilterConfigFactory::createFilterFactory(const Json::Object&, | ||
Server::Configuration::FactoryContext&) { | ||
// Only used in v1 filters. | ||
NOT_IMPLEMENTED_GCOVR_EXCL_LINE; | ||
} | ||
|
||
Network::FilterFactoryCb SniClusterNetworkFilterConfigFactory::createFilterFactoryFromProto( | ||
const Protobuf::Message&, Server::Configuration::FactoryContext&) { | ||
return [](Network::FilterManager& filter_manager) -> void { | ||
filter_manager.addReadFilter(std::make_shared<SniClusterFilter>()); | ||
}; | ||
} | ||
|
||
ProtobufTypes::MessagePtr SniClusterNetworkFilterConfigFactory::createEmptyConfigProto() { | ||
return std::make_unique<ProtobufWkt::Empty>(); | ||
} | ||
|
||
/** | ||
* Static registration for the sni_cluster filter. @see RegisterFactory. | ||
*/ | ||
static Registry::RegisterFactory<SniClusterNetworkFilterConfigFactory, | ||
Server::Configuration::NamedNetworkFilterConfigFactory> | ||
registered_; | ||
|
||
} // namespace SniCluster | ||
} // namespace NetworkFilters | ||
} // namespace Extensions | ||
} // namespace Envoy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#pragma once | ||
|
||
#include "envoy/server/filter_config.h" | ||
|
||
#include "extensions/filters/network/well_known_names.h" | ||
|
||
namespace Envoy { | ||
namespace Extensions { | ||
namespace NetworkFilters { | ||
namespace SniCluster { | ||
|
||
/** | ||
* Config registration for the sni_cluster filter. @see NamedNetworkFilterConfigFactory. | ||
*/ | ||
class SniClusterNetworkFilterConfigFactory | ||
: public Server::Configuration::NamedNetworkFilterConfigFactory { | ||
public: | ||
// NamedNetworkFilterConfigFactory | ||
Network::FilterFactoryCb createFilterFactory(const Json::Object&, | ||
Server::Configuration::FactoryContext&) override; | ||
Network::FilterFactoryCb | ||
createFilterFactoryFromProto(const Protobuf::Message&, | ||
Server::Configuration::FactoryContext&) override; | ||
ProtobufTypes::MessagePtr createEmptyConfigProto() override; | ||
std::string name() override { return NetworkFilterNames::get().SniCluster; } | ||
}; | ||
|
||
} // namespace SniCluster | ||
} // namespace NetworkFilters | ||
} // namespace Extensions | ||
} // namespace Envoy |
30 changes: 30 additions & 0 deletions
30
source/extensions/filters/network/sni_cluster/sni_cluster.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include "extensions/filters/network/sni_cluster/sni_cluster.h" | ||
|
||
#include "envoy/network/connection.h" | ||
|
||
#include "common/common/assert.h" | ||
#include "common/tcp_proxy/tcp_proxy.h" | ||
|
||
namespace Envoy { | ||
namespace Extensions { | ||
namespace NetworkFilters { | ||
namespace SniCluster { | ||
|
||
Network::FilterStatus SniClusterFilter::onNewConnection() { | ||
absl::string_view sni = read_callbacks_->connection().requestedServerName(); | ||
ENVOY_CONN_LOG(trace, "sni_cluster: new connection with server name {}", | ||
read_callbacks_->connection(), sni); | ||
|
||
if (!sni.empty()) { | ||
// Set the tcp_proxy cluster to the same value as SNI | ||
read_callbacks_->connection().perConnectionState().setData( | ||
TcpProxy::PerConnectionCluster::Key, std::make_unique<TcpProxy::PerConnectionCluster>(sni)); | ||
} | ||
|
||
return Network::FilterStatus::Continue; | ||
} | ||
|
||
} // namespace SniCluster | ||
} // namespace NetworkFilters | ||
} // namespace Extensions | ||
} // namespace Envoy |
34 changes: 34 additions & 0 deletions
34
source/extensions/filters/network/sni_cluster/sni_cluster.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#pragma once | ||
|
||
#include "envoy/network/filter.h" | ||
|
||
#include "common/common/logger.h" | ||
|
||
namespace Envoy { | ||
namespace Extensions { | ||
namespace NetworkFilters { | ||
namespace SniCluster { | ||
|
||
/** | ||
* Implementation of the sni_cluster filter that sets the upstream cluster name from | ||
* the SNI field in the TLS connection. | ||
*/ | ||
class SniClusterFilter : public Network::ReadFilter, Logger::Loggable<Logger::Id::filter> { | ||
public: | ||
// Network::ReadFilter | ||
Network::FilterStatus onData(Buffer::Instance&, bool) override { | ||
return Network::FilterStatus::Continue; | ||
} | ||
Network::FilterStatus onNewConnection() override; | ||
void initializeReadFilterCallbacks(Network::ReadFilterCallbacks& callbacks) override { | ||
read_callbacks_ = &callbacks; | ||
} | ||
|
||
private: | ||
Network::ReadFilterCallbacks* read_callbacks_{}; | ||
}; | ||
|
||
} // namespace SniCluster | ||
} // namespace NetworkFilters | ||
} // namespace Extensions | ||
} // namespace Envoy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
licenses(["notice"]) # Apache 2 | ||
|
||
load( | ||
"//bazel:envoy_build_system.bzl", | ||
"envoy_cc_test", | ||
"envoy_package", | ||
) | ||
load( | ||
"//test/extensions:extensions_build_system.bzl", | ||
"envoy_extension_cc_test", | ||
) | ||
|
||
envoy_package() | ||
|
||
envoy_extension_cc_test( | ||
name = "sni_cluster_test", | ||
srcs = ["sni_cluster_test.cc"], | ||
extension_name = "envoy.filters.network.sni_cluster", | ||
deps = [ | ||
"//source/extensions/filters/network/sni_cluster", | ||
"//source/extensions/filters/network/sni_cluster:config", | ||
"//test/mocks/network:network_mocks", | ||
"//test/mocks/server:server_mocks", | ||
], | ||
) |
Oops, something went wrong.