-
Notifications
You must be signed in to change notification settings - Fork 5.3k
thrift_proxy: Add thrift header to metadata filter #18637
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
18 commits
Select commit
Hold shift + click to select a range
6794dac
Add thrift header to metadata proto config
fishcakez 9c4743d
Fix import
fishcakez 83c85e8
Fix references
fishcakez 1e5f20f
Fix whitespace
fishcakez dc66777
Remove extension reference as doesnt exist yet
fishcakez b149c52
Fix format of proto
fishcakez 91fe95d
Remove local refs
fishcakez a7e7ef6
Replace min_bytes with min_len
fishcakez d333eb5
Format
fishcakez 2335e83
Add filter
fishcakez d134139
Merge remote-tracking branch 'upstream/main' into thrift-h2m-proto
fishcakez 8145a26
Release notes
fishcakez b49a1ac
Fix typo
fishcakez 99b8252
Fix doc
fishcakez 94ba57b
Fix toctree
fishcakez f68f277
Format
fishcakez 2a521f6
Merge remote-tracking branch 'upstream/main' into thrift-h2m-proto
fishcakez 3eeec11
Add more edge case tests
fishcakez 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
12 changes: 12 additions & 0 deletions
12
api/envoy/extensions/filters/network/thrift_proxy/filters/header_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_udpa//udpa/annotations:pkg", | ||
| ], | ||
| ) |
109 changes: 109 additions & 0 deletions
109
...sions/filters/network/thrift_proxy/filters/header_to_metadata/v3/header_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,109 @@ | ||
| syntax = "proto3"; | ||
|
|
||
| package envoy.extensions.filters.network.thrift_proxy.filters.header_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.thrift_proxy.filters.header_to_metadata.v3"; | ||
| option java_outer_classname = "HeaderToMetadataProto"; | ||
| option java_multiple_files = true; | ||
| option (udpa.annotations.file_status).package_version_status = ACTIVE; | ||
|
|
||
| // [#protodoc-title: Header-To-Metadata Filter] | ||
| // | ||
| // The configuration for transforming headers into metadata. This is useful | ||
| // for matching load balancer subsets, logging, etc. | ||
| // | ||
| // Header to Metadata :ref:`configuration overview <config_thrift_filters_header_to_metadata>`. | ||
| // [#extension: envoy.filters.thrift.header_to_metadata] | ||
|
|
||
| message HeaderToMetadata { | ||
| enum ValueType { | ||
| STRING = 0; | ||
|
|
||
| NUMBER = 1; | ||
|
|
||
| // The value is a serialized `protobuf.Value | ||
| // <https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/struct.proto#L62>`_. | ||
| PROTOBUF_VALUE = 2; | ||
| } | ||
|
|
||
| // ValueEncode defines the encoding algorithm. | ||
| enum ValueEncode { | ||
| // The value is not encoded. | ||
| NONE = 0; | ||
|
|
||
| // The value is encoded in `Base64 <https://tools.ietf.org/html/rfc4648#section-4>`_. | ||
| // Note: this is mostly used for STRING and PROTOBUF_VALUE to escape the | ||
| // non-ASCII characters in the header. | ||
| BASE64 = 1; | ||
| } | ||
|
|
||
| // [#next-free-field: 7] | ||
| message KeyValuePair { | ||
| // The namespace — if this is empty, the filter's namespace will be used. | ||
| string metadata_namespace = 1; | ||
|
|
||
| // The key to use within the namespace. | ||
| string key = 2 [(validate.rules).string = {min_len: 1}]; | ||
|
|
||
| oneof value_type { | ||
| // The value to pair with the given key. | ||
| // | ||
| // When used for on_present case, if value is non-empty it'll be used instead | ||
| // of the header value. If both are empty, no metadata is added. | ||
| // | ||
| // When used for on_missing case, a non-empty value must be provided otherwise | ||
| // no metadata is added. | ||
| string value = 3; | ||
|
|
||
| // If present, the header's value will be matched and substituted with this. | ||
| // If there is no match or substitution, the header value | ||
| // is used as-is. | ||
| // | ||
| // This is only used for on_present. | ||
| // | ||
| // Note: if the `value` field is non-empty this field should be empty. | ||
| type.matcher.v3.RegexMatchAndSubstitute regex_value_rewrite = 4; | ||
| } | ||
|
|
||
| // The value's type — defaults to string. | ||
| ValueType type = 5 [(validate.rules).enum = {defined_only: true}]; | ||
|
|
||
| // How is the value encoded, default is NONE (not encoded). | ||
| // The value will be decoded accordingly before storing to metadata. | ||
| ValueEncode encode = 6; | ||
| } | ||
|
|
||
| // A Rule defines what metadata to apply when a header is present or missing. | ||
| message Rule { | ||
| // Specifies that a match will be performed on the value of a header. | ||
| // | ||
| // The header to be extracted. | ||
| string header = 1 | ||
| [(validate.rules).string = {min_len: 1 well_known_regex: HTTP_HEADER_NAME strict: false}]; | ||
|
|
||
| // If the header is present, apply this metadata KeyValuePair. | ||
| // | ||
| // If the value in the KeyValuePair is non-empty, it'll be used instead | ||
| // of the header value. | ||
| KeyValuePair on_present = 2; | ||
|
|
||
| // If the header is not present, apply this metadata KeyValuePair. | ||
| // | ||
| // The value in the KeyValuePair must be set, since it'll be used in lieu | ||
| // of the missing header value. | ||
| KeyValuePair on_missing = 3; | ||
|
|
||
| // Whether or not to remove the header after a rule is applied. | ||
| // | ||
| // This prevents headers from leaking. | ||
| bool remove = 4; | ||
| } | ||
|
|
||
| // The list of rules to apply to requests. | ||
| repeated Rule request_rules = 1 [(validate.rules).repeated = {min_items: 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
90 changes: 90 additions & 0 deletions
90
...root/configuration/other_protocols/thrift_filters/header_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,90 @@ | ||
| .. _config_thrift_filters_header_to_metadata: | ||
|
|
||
| Envoy Header-To-Metadata Filter | ||
| =============================== | ||
| * :ref:`v3 API reference <envoy_v3_api_msg_extensions.filters.http.header_to_metadata.v3.Config>` | ||
| * This filter should be configured with the name *envoy.filters.http.header_to_metadata*. | ||
|
|
||
| This filter is configured with rules that will be matched against requests. | ||
| Each rule has either a header and can be triggered either when the header is present or missing. | ||
|
|
||
| When a rule is triggered, dynamic metadata will be added based on the configuration of the rule. | ||
| If the header is present, it's value is extracted and used along with the specified | ||
| key as metadata. If the header is missing, on missing case is triggered and the value | ||
| specified is used for adding metadata. | ||
|
|
||
| The metadata can then be used for load balancing decisions, consumed from logs, etc. | ||
|
|
||
| A typical use case for this filter is to dynamically match requests with load balancer | ||
| subsets. For this, a given header's value would be extracted and attached to the request | ||
| as dynamic metadata which would then be used to match a subset of endpoints. | ||
|
|
||
| Example | ||
| ------- | ||
|
|
||
| A sample filter configuration to route traffic to endpoints based on the presence or | ||
| absence of a version header could be: | ||
|
|
||
| .. code-block:: yaml | ||
|
|
||
| thrift_filters: | ||
| - name: envoy.filters.thrift.header_to_metadata | ||
| typed_config: | ||
| "@type": type.googleapis.com/envoy.extensions.filters.thrift.header_to_metadata.v3.HeaderToMetadata | ||
| request_rules: | ||
| - header: x-version | ||
| on_header_present: | ||
| metadata_namespace: envoy.lb | ||
| key: version | ||
| type: STRING | ||
| on_header_missing: | ||
| metadata_namespace: envoy.lb | ||
| key: default | ||
| value: 'true' | ||
| type: STRING | ||
| remove: false | ||
|
|
||
| A corresponding upstream cluster configuration could be: | ||
|
|
||
| .. code-block:: yaml | ||
|
|
||
| clusters: | ||
| - name: versioned-cluster | ||
| type: EDS | ||
| lb_policy: ROUND_ROBIN | ||
| lb_subset_config: | ||
| fallback_policy: ANY_ENDPOINT | ||
| subset_selectors: | ||
| - keys: | ||
| - default | ||
| - keys: | ||
| - version | ||
|
|
||
| This would then allow requests with the ``x-version`` header set to be matched against | ||
| endpoints with the corresponding version. Whereas requests with that header missing | ||
| would be matched with the default endpoints. | ||
|
|
||
| If the header's value needs to be transformed before it's added to the request as | ||
| dynamic metadata, this filter supports regex matching and substitution: | ||
|
|
||
| .. code-block:: yaml | ||
|
|
||
| thrift_filters: | ||
| - name: envoy.filters.thrift.header_to_metadata | ||
| typed_config: | ||
| "@type": type.googleapis.com/envoy.extensions.filters.thrift.header_to_metadata.v3.HeaderToMetadata | ||
| request_rules: | ||
| - header: ":path" | ||
| on_header_present: | ||
| metadata_namespace: envoy.lb | ||
| key: cluster | ||
| regex_value_rewrite: | ||
| pattern: | ||
| google_re2: {} | ||
| regex: "^/(cluster[\\d\\w-]+)/?.*$" | ||
| substitution: "\\1" | ||
|
|
||
| Statistics | ||
| ---------- | ||
|
|
||
| Currently, this filter generates no statistics. |
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
33 changes: 33 additions & 0 deletions
33
source/extensions/filters/network/thrift_proxy/filters/header_to_metadata/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,33 @@ | ||
| 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 = [ | ||
| ":header_to_metadata_filter_lib", | ||
| "//envoy/registry", | ||
| "//source/extensions/filters/network/thrift_proxy/filters:factory_base_lib", | ||
| "@envoy_api//envoy/extensions/filters/network/thrift_proxy/filters/header_to_metadata/v3:pkg_cc_proto", | ||
| ], | ||
| ) | ||
|
|
||
| envoy_cc_library( | ||
| name = "header_to_metadata_filter_lib", | ||
| srcs = ["header_to_metadata_filter.cc"], | ||
| hdrs = ["header_to_metadata_filter.h"], | ||
| deps = [ | ||
| "//envoy/server:filter_config_interface", | ||
| "//source/extensions/filters/network/thrift_proxy/filters:pass_through_filter_lib", | ||
| "@envoy_api//envoy/extensions/filters/network/thrift_proxy/filters/header_to_metadata/v3:pkg_cc_proto", | ||
| ], | ||
| ) |
35 changes: 35 additions & 0 deletions
35
source/extensions/filters/network/thrift_proxy/filters/header_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,35 @@ | ||
| #include "source/extensions/filters/network/thrift_proxy/filters/header_to_metadata/config.h" | ||
|
|
||
| #include <string> | ||
|
|
||
| #include "source/extensions/filters/network/thrift_proxy/filters/header_to_metadata/header_to_metadata_filter.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Extensions { | ||
| namespace ThriftFilters { | ||
| namespace HeaderToMetadataFilter { | ||
|
|
||
| using namespace Envoy::Extensions::NetworkFilters; | ||
|
|
||
| ThriftProxy::ThriftFilters::FilterFactoryCb | ||
| HeaderToMetadataFilterConfig::createFilterFactoryFromProtoTyped( | ||
| const envoy::extensions::filters::network::thrift_proxy::filters::header_to_metadata::v3:: | ||
| HeaderToMetadata& proto_config, | ||
| const std::string&, Server::Configuration::FactoryContext&) { | ||
| ConfigSharedPtr filter_config(std::make_shared<Config>(proto_config)); | ||
| return | ||
| [filter_config](ThriftProxy::ThriftFilters::FilterChainFactoryCallbacks& callbacks) -> void { | ||
| callbacks.addDecoderFilter(std::make_shared<HeaderToMetadataFilter>(filter_config)); | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Static registration for the header to metadata filter. @see RegisterFactory. | ||
| */ | ||
| REGISTER_FACTORY(HeaderToMetadataFilterConfig, | ||
| ThriftProxy::ThriftFilters::NamedThriftFilterConfigFactory); | ||
|
|
||
| } // namespace HeaderToMetadataFilter | ||
| } // namespace ThriftFilters | ||
| } // namespace Extensions | ||
| } // namespace Envoy | ||
33 changes: 33 additions & 0 deletions
33
source/extensions/filters/network/thrift_proxy/filters/header_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,33 @@ | ||
| #pragma once | ||
|
|
||
| #include "envoy/extensions/filters/network/thrift_proxy/filters/header_to_metadata/v3/header_to_metadata.pb.h" | ||
| #include "envoy/extensions/filters/network/thrift_proxy/filters/header_to_metadata/v3/header_to_metadata.pb.validate.h" | ||
|
|
||
| #include "source/extensions/filters/network/thrift_proxy/filters/factory_base.h" | ||
| #include "source/extensions/filters/network/thrift_proxy/filters/header_to_metadata/header_to_metadata_filter.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Extensions { | ||
| namespace ThriftFilters { | ||
| namespace HeaderToMetadataFilter { | ||
|
|
||
| /** | ||
| * Config registration for the header to metadata filter. @see NamedThriftFilterConfigFactory. | ||
| */ | ||
| class HeaderToMetadataFilterConfig : public ThriftProxy::ThriftFilters::FactoryBase< | ||
| envoy::extensions::filters::network::thrift_proxy:: | ||
| filters::header_to_metadata::v3::HeaderToMetadata> { | ||
| public: | ||
| HeaderToMetadataFilterConfig() : FactoryBase("envoy.filters.thrift.header_to_metadata") {} | ||
|
|
||
| private: | ||
| ThriftProxy::ThriftFilters::FilterFactoryCb createFilterFactoryFromProtoTyped( | ||
| const envoy::extensions::filters::network::thrift_proxy::filters::header_to_metadata::v3:: | ||
| HeaderToMetadata& proto_config, | ||
| const std::string& stats_prefix, Server::Configuration::FactoryContext& context) override; | ||
| }; | ||
|
|
||
| } // namespace HeaderToMetadataFilter | ||
| } // namespace ThriftFilters | ||
| } // 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.
nit: we don't need a new shared ptr on every call, pass a ref to filter config?