-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Extensions: Network filter to set upstream cluster from SNI #4489
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
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
dcc38b8
Cluster name from SNI
683285d
Merge remote-tracking branch 'upstream/master' into sni_cluster
f10f6b4
tests and more
c783a18
fixes
9bba070
tests and fixes
b430ae7
streamline
ac73c43
never use auto
e9680b3
Merge remote-tracking branch 'upstream/master' into sni_cluster
7a6a37c
docs, release notes
03dca75
two files
0408373
Merge remote-tracking branch 'upstream/master' into sni_cluster
2e30879
docs and formatting
cb8cd6e
Merge remote-tracking branch 'upstream/master' into sni_cluster
f63b194
formatting and renaming
377c741
Merge remote-tracking branch 'upstream/master' into sni_cluster
f99d175
codeowners
8dd96ef
assorted fixes
e49e40d
nits
e2254c0
std::unique
39ba79a
Merge remote-tracking branch 'upstream/master' into sni_cluster
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since v1 will be completely removed soon, should we mark this method non-PURE and in NamedNetworkFilterConfigFactory and make it
NOT_IMPLEMENTED?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was waiting for the 1.8 cycle. after that, we could remove these methods entirely from everywhere.