-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Add envoy.filters.network.sni_to_metadata #41275
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
Open
bplotnick
wants to merge
8
commits into
envoyproxy:main
Choose a base branch
from
bplotnick:add-sni-to-metadata
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
be98075
Add sni_to_metadata
bplotnick 60613d1
make exception free
bplotnick 8e7f06b
format
bplotnick fb53e8a
Add docs
bplotnick 5a7c625
Remove extraneous includes
bplotnick 7ec6c2d
Add changelog
bplotnick 93798dd
envoy_cc_extension
bplotnick a948a8a
Fix docs ref
bplotnick 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
12 changes: 12 additions & 0 deletions
12
api/envoy/extensions/filters/network/sni_to_metadata/v3/BUILD
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,12 @@ | ||
# DO NOT EDIT. This file is generated by tools/proto_format/proto_sync.py. | ||
|
||
load("@envoy_api//bazel:api_build_system.bzl", "api_proto_package") | ||
|
||
licenses(["notice"]) # Apache 2 | ||
|
||
api_proto_package( | ||
deps = [ | ||
"//envoy/type/matcher/v3:pkg", | ||
"@com_github_cncf_xds//udpa/annotations:pkg", | ||
], | ||
) |
53 changes: 53 additions & 0 deletions
53
api/envoy/extensions/filters/network/sni_to_metadata/v3/sni_to_metadata.proto
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,53 @@ | ||
syntax = "proto3"; | ||
|
||
package envoy.extensions.filters.network.sni_to_metadata.v3; | ||
|
||
import "envoy/type/matcher/v3/regex.proto"; | ||
|
||
import "udpa/annotations/status.proto"; | ||
import "validate/validate.proto"; | ||
|
||
option java_package = "io.envoyproxy.envoy.extensions.filters.network.sni_to_metadata.v3"; | ||
option java_outer_classname = "SniToMetadataProto"; | ||
option java_multiple_files = true; | ||
option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/network/sni_to_metadata/v3;sni_to_metadatav3"; | ||
option (udpa.annotations.file_status).package_version_status = ACTIVE; | ||
|
||
// [#protodoc-title: SNI to Metadata Filter] | ||
|
||
// Configuration proto schema for ``envoy.extensions.filters.network.sni_to_metadata`` network filter. | ||
// [#extension: envoy.filters.network.sni_to_metadata] | ||
message SniToMetadataFilter { | ||
// MetadataTarget defines where to store extracted metadata. | ||
message MetadataTarget { | ||
// The metadata namespace to use when storing the result. | ||
// If empty, defaults to ``envoy.filters.network.sni_to_metadata`` | ||
string metadata_namespace = 1; | ||
|
||
// The metadata key to use when storing the result. | ||
string metadata_key = 2 [(validate.rules).string = {min_len: 1}]; | ||
|
||
// The metadata value to store. If empty, the entire matched SNI value will be used. | ||
// This field supports capture group substitution using numbered groups from the regex pattern. | ||
// For example: ``app-\\1-\\2`` where ``\\1`` and ``\\2`` refer to the first and second capture groups (note escaped backslashes). | ||
string metadata_value = 3; | ||
} | ||
|
||
// ConnectionRule defines a rule for extracting metadata from SNI. | ||
message ConnectionRule { | ||
// The regex pattern to match against the SNI value. | ||
// Supports Google RE2 numbered capture groups. | ||
// Example: ``^([^.]+)\.([^.]+)\.([^.]+)\.example\.com$`` | ||
// If not specified, the rule will always match and use the entire SNI value. | ||
type.matcher.v3.RegexMatcher pattern = 1; | ||
|
||
// List of metadata targets to populate when this rule matches. | ||
// Each target can use capture groups from the regex pattern in its metadata_value. | ||
// If no pattern is specified, metadata_value will be used as-is or default to the full SNI. | ||
repeated MetadataTarget metadata_targets = 2 [(validate.rules).repeated = {min_items: 1}]; | ||
} | ||
|
||
// List of connection rules to evaluate against the SNI. | ||
// Rules are evaluated in order, and the first matching rule will be applied. | ||
repeated ConnectionRule connection_rules = 1 [(validate.rules).repeated = {min_items: 1}]; | ||
} | ||
Comment on lines
+20
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you take the HeaderToMetadata as a reference and use the similar API (like the same |
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
28 changes: 28 additions & 0 deletions
28
docs/root/configuration/listeners/network_filters/sni_to_metadata_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,28 @@ | ||
.. _config_network_filters_sni_to_metadata: | ||
|
||
SNI-to-Metadata Filter | ||
======================= | ||
|
||
.. attention:: | ||
|
||
SNI-to-Metadata Filter support should be considered alpha and not production ready. | ||
|
||
|
||
The SNI-to-Metadata Filter is a filter that extracts the SNI of the client connection and stores it in the connection dynamic metadata. | ||
It is able to conditionally extract based on regex patters as well as extract fields and format the metadata using regex capture groups. | ||
|
||
Example Configuration | ||
---------------------- | ||
|
||
.. code-block:: yaml | ||
|
||
network_filters: | ||
- name: envoy.filters.network.sni_to_metadata | ||
typed_config: | ||
"@type": type.googleapis.com/envoy.extensions.filters.network.sni_to_metadata.v3.SniToMetadataFilter | ||
connection_rules: | ||
- pattern: ^([^.]+)\.([^.]+)\.([^.]+)\.example\.com$ | ||
metadata_targets: | ||
- metadata_key: app_name | ||
metadata_namespace: envoy.lb | ||
metadata_value: \1 |
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,39 @@ | ||
load( | ||
"//bazel:envoy_build_system.bzl", | ||
"envoy_cc_extension", | ||
"envoy_cc_library", | ||
"envoy_extension_package", | ||
) | ||
|
||
licenses(["notice"]) # Apache 2 | ||
|
||
envoy_extension_package() | ||
|
||
envoy_cc_extension( | ||
name = "config", | ||
srcs = ["config.cc"], | ||
hdrs = ["config.h"], | ||
deps = [ | ||
":filter_lib", | ||
"//source/common/common:logger_lib", | ||
"//source/extensions/filters/network:well_known_names", | ||
"//source/extensions/filters/network/common:factory_base_lib", | ||
"@envoy_api//envoy/extensions/filters/network/sni_to_metadata/v3:pkg_cc_proto", | ||
], | ||
) | ||
|
||
envoy_cc_library( | ||
name = "filter_lib", | ||
srcs = ["filter.cc"], | ||
hdrs = ["filter.h"], | ||
deps = [ | ||
"//envoy/network:connection_interface", | ||
"//envoy/network:filter_interface", | ||
"//source/common/common:logger_lib", | ||
"//source/common/common:regex_lib", | ||
"//source/common/protobuf", | ||
"@com_google_absl//absl/strings", | ||
"@com_googlesource_code_re2//:re2", | ||
"@envoy_api//envoy/extensions/filters/network/sni_to_metadata/v3:pkg_cc_proto", | ||
], | ||
) |
37 changes: 37 additions & 0 deletions
37
source/extensions/filters/network/sni_to_metadata/config.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,37 @@ | ||
#include "source/extensions/filters/network/sni_to_metadata/config.h" | ||
|
||
#include "envoy/registry/registry.h" | ||
|
||
#include "source/extensions/filters/network/sni_to_metadata/filter.h" | ||
#include "source/extensions/filters/network/well_known_names.h" | ||
|
||
namespace Envoy { | ||
namespace Extensions { | ||
namespace NetworkFilters { | ||
namespace SniToMetadata { | ||
|
||
SniToMetadataFilterFactory::SniToMetadataFilterFactory() | ||
: Common::ExceptionFreeFactoryBase<FilterConfig>(NetworkFilterNames::get().SniToMetadata) {} | ||
|
||
absl::StatusOr<Network::FilterFactoryCb> | ||
SniToMetadataFilterFactory::createFilterFactoryFromProtoTyped( | ||
const FilterConfig& config, Server::Configuration::FactoryContext& context) { | ||
|
||
absl::Status creation_status = absl::OkStatus(); | ||
ConfigSharedPtr filter_config = std::make_shared<Config>( | ||
config, context.serverFactoryContext().regexEngine(), creation_status); | ||
|
||
RETURN_IF_NOT_OK_REF(creation_status); | ||
|
||
return [filter_config](Network::FilterManager& filter_manager) -> void { | ||
filter_manager.addReadFilter(std::make_shared<Filter>(filter_config)); | ||
}; | ||
} | ||
|
||
REGISTER_FACTORY(SniToMetadataFilterFactory, | ||
Server::Configuration::NamedNetworkFilterConfigFactory); | ||
|
||
} // namespace SniToMetadata | ||
} // namespace NetworkFilters | ||
} // namespace Extensions | ||
} // namespace Envoy |
31 changes: 31 additions & 0 deletions
31
source/extensions/filters/network/sni_to_metadata/config.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,31 @@ | ||
#pragma once | ||
|
||
#include "envoy/extensions/filters/network/sni_to_metadata/v3/sni_to_metadata.pb.h" | ||
#include "envoy/extensions/filters/network/sni_to_metadata/v3/sni_to_metadata.pb.validate.h" | ||
|
||
#include "source/extensions/filters/network/common/factory_base.h" | ||
|
||
namespace Envoy { | ||
namespace Extensions { | ||
namespace NetworkFilters { | ||
namespace SniToMetadata { | ||
|
||
using FilterConfig = envoy::extensions::filters::network::sni_to_metadata::v3::SniToMetadataFilter; | ||
|
||
/** | ||
* Config registration for the SNI to metadata filter. @see NamedNetworkFilterConfigFactory. | ||
*/ | ||
class SniToMetadataFilterFactory : public Common::ExceptionFreeFactoryBase<FilterConfig> { | ||
public: | ||
SniToMetadataFilterFactory(); | ||
|
||
private: | ||
absl::StatusOr<Network::FilterFactoryCb> | ||
createFilterFactoryFromProtoTyped(const FilterConfig& config, | ||
Server::Configuration::FactoryContext& context) override; | ||
}; | ||
|
||
} // namespace SniToMetadata | ||
} // namespace NetworkFilters | ||
} // namespace Extensions | ||
} // namespace Envoy |
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.
We prefer StringMatcher rather than single regex matcher. :)