-
Notifications
You must be signed in to change notification settings - Fork 5.4k
ip tagging: filter skeleton #998
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
22 commits
Select commit
Hold shift + click to select a range
689af40
ip tagging: filter skeleton
61a794d
comments
3e94d5d
correction
70e473d
some testing
3c73c73
format
7176dc1
Merge branch 'master' into ip-tagging-filter
db04ac6
docs
232ab06
fix
133780f
remove baggage
f3d3da2
nits
291bb69
format
8f6de66
fix
3b96d32
address comments
2cef334
Merge branch 'master' into ip-tagging-filter
b0ed632
new filter registration
0843393
comment
bbb539d
comment
588c59c
update
970e112
update
476c46a
fix
c43598b
fix
27900bc
nit
junr03 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
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,48 @@ | ||
| .. _config_http_filters_ip_tagging: | ||
|
|
||
| Ip tagging filter | ||
| ==================== | ||
|
|
||
| This is an HTTP filter which enables Envoy to tag requests with extra information such as location, cloud source, and any | ||
| extra data. This is useful to prevent against DDoS. | ||
|
|
||
| **Note**: this filter is under active development, and currently does not perform any tagging on requests. In other | ||
| words, installing this filter is a no-op in the filter chain. | ||
|
|
||
| .. code-block:: json | ||
|
|
||
| { | ||
| "type": "decoder", | ||
| "name": "ip_tagging", | ||
| "config": { | ||
| "request_type": "...", | ||
| "ip_tags": [] | ||
| } | ||
| } | ||
|
|
||
| request_type | ||
| *(optional, string)* The type of requests the filter should apply to. The supported | ||
| types are *internal*, *external* or *both*. A request is considered internal if | ||
| :ref:`x-envoy-internal<config_http_conn_man_headers_x-envoy-internal>` is set to true. If | ||
| :ref:`x-envoy-internal<config_http_conn_man_headers_x-envoy-internal>` is not set or false, a | ||
| request is considered external. The filter defaults to *both*, and it will apply to all request | ||
| types. | ||
|
|
||
| ip_tags: | ||
| *(optional, array)* Specifies the list of ip tags to set for a request. | ||
|
|
||
| Ip tags | ||
| ------- | ||
| .. code-block:: json | ||
|
|
||
| { | ||
| "ip_tag_name": "...", | ||
| "ip_list": [] | ||
| } | ||
|
|
||
| ip_tag_name: | ||
| *(required, string)* Specifies the ip tag name to apply. | ||
|
|
||
| ip_list: | ||
| *(required, list of strings)* A list of IP address and subnet masks that will be tagged with the ``ip_tag_name``. Both | ||
| IPv4 and IPv6 CIDR addresses are allowed here. |
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,29 @@ | ||
| #include "common/http/filter/ip_tagging_filter.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Http { | ||
|
|
||
| IpTaggingFilter::IpTaggingFilter(IpTaggingFilterConfigSharedPtr config) : config_(config) {} | ||
|
|
||
| IpTaggingFilter::~IpTaggingFilter() {} | ||
|
|
||
| void IpTaggingFilter::onDestroy() {} | ||
|
|
||
| FilterHeadersStatus IpTaggingFilter::decodeHeaders(HeaderMap&, bool) { | ||
| return FilterHeadersStatus::Continue; | ||
| } | ||
|
|
||
| FilterDataStatus IpTaggingFilter::decodeData(Buffer::Instance&, bool) { | ||
| return FilterDataStatus::Continue; | ||
| } | ||
|
|
||
| FilterTrailersStatus IpTaggingFilter::decodeTrailers(HeaderMap&) { | ||
| return FilterTrailersStatus::Continue; | ||
| } | ||
|
|
||
| void IpTaggingFilter::setDecoderFilterCallbacks(StreamDecoderFilterCallbacks& callbacks) { | ||
| callbacks_ = &callbacks; | ||
| } | ||
|
|
||
| } // Http | ||
| } // 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,75 @@ | ||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
| #include <memory> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "envoy/http/filter.h" | ||
| #include "envoy/json/json_object.h" | ||
|
|
||
| #include "common/common/assert.h" | ||
| #include "common/json/config_schemas.h" | ||
| #include "common/json/json_validator.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Http { | ||
|
|
||
| /** | ||
| * Type of requests the filter should apply to. | ||
| */ | ||
| enum class FilterRequestType { Internal, External, Both }; | ||
|
|
||
| /** | ||
| * Configuration for the ip tagging filter. | ||
| */ | ||
| class IpTaggingFilterConfig : Json::Validator { | ||
| public: | ||
| IpTaggingFilterConfig(const Json::Object& json_config) | ||
| : Json::Validator(json_config, Json::Schema::IP_TAGGING_HTTP_FILTER_SCHEMA), | ||
| request_type_(stringToType(json_config.getString("request_type", "both"))) {} | ||
|
|
||
| FilterRequestType requestType() const { return request_type_; } | ||
|
|
||
| private: | ||
| static FilterRequestType stringToType(const std::string& request_type) { | ||
| if (request_type == "internal") { | ||
| return FilterRequestType::Internal; | ||
| } else if (request_type == "external") { | ||
| return FilterRequestType::External; | ||
| } else { | ||
| ASSERT(request_type == "both"); | ||
| return FilterRequestType::Both; | ||
| } | ||
| } | ||
|
|
||
| const FilterRequestType request_type_; | ||
| }; | ||
|
|
||
| typedef std::shared_ptr<IpTaggingFilterConfig> IpTaggingFilterConfigSharedPtr; | ||
|
|
||
| /** | ||
| * A filter that tags requests via the x-envoy-ip-tags header based on the request's trusted XFF | ||
| * address. | ||
| */ | ||
| class IpTaggingFilter : public StreamDecoderFilter { | ||
| public: | ||
| IpTaggingFilter(IpTaggingFilterConfigSharedPtr config); | ||
| ~IpTaggingFilter(); | ||
|
|
||
| // Http::StreamFilterBase | ||
| void onDestroy() override; | ||
|
|
||
| // Http::StreamDecoderFilter | ||
| FilterHeadersStatus decodeHeaders(HeaderMap& headers, bool end_stream) override; | ||
| FilterDataStatus decodeData(Buffer::Instance& data, bool end_stream) override; | ||
| FilterTrailersStatus decodeTrailers(HeaderMap& trailers) override; | ||
| void setDecoderFilterCallbacks(StreamDecoderFilterCallbacks& callbacks) override; | ||
|
|
||
| private: | ||
| IpTaggingFilterConfigSharedPtr config_; | ||
| StreamDecoderFilterCallbacks* callbacks_{}; | ||
| }; | ||
|
|
||
| } // Http | ||
| } // 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
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 "server/config/http/ip_tagging.h" | ||
|
|
||
| #include <string> | ||
|
|
||
| #include "common/http/filter/ip_tagging_filter.h" | ||
| #include "common/json/config_schemas.h" | ||
|
|
||
| namespace Envoy { | ||
|
Contributor
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. You can still write tests for loading the config under https://github.com/lyft/envoy/blob/master/test/server/config/http/config_test.cc |
||
| namespace Server { | ||
| namespace Configuration { | ||
|
|
||
| HttpFilterFactoryCb IpTaggingFilterConfig::createFilterFactory(HttpFilterType type, | ||
| const Json::Object& json_config, | ||
| const std::string&, | ||
| Server::Instance&) { | ||
| if (type != HttpFilterType::Decoder) { | ||
| throw EnvoyException( | ||
| fmt::format("{} ip tagging filter must be configured as a decoder filter.", name())); | ||
| } | ||
|
|
||
| Http::IpTaggingFilterConfigSharedPtr config(new Http::IpTaggingFilterConfig(json_config)); | ||
| return [config](Http::FilterChainFactoryCallbacks& callbacks) -> void { | ||
| callbacks.addStreamDecoderFilter( | ||
| Http::StreamDecoderFilterSharedPtr{new Http::IpTaggingFilter(config)}); | ||
| }; | ||
| } | ||
|
|
||
| std::string IpTaggingFilterConfig::name() { return "ip_tagging"; } | ||
|
|
||
| /** | ||
| * Static registration for the ip tagging filter. @see RegisterNamedHttpFilterConfigFactory. | ||
| */ | ||
| static RegisterNamedHttpFilterConfigFactory<IpTaggingFilterConfig> register_; | ||
|
|
||
| } // Configuration | ||
| } // Server | ||
| } // 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,27 @@ | ||
| #pragma once | ||
|
|
||
| #include <string> | ||
|
|
||
| #include "envoy/server/instance.h" | ||
|
|
||
| #include "server/config/network/http_connection_manager.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Server { | ||
| namespace Configuration { | ||
|
|
||
| /** | ||
| * Config registration for the router filter. @see NamedHttpFilterConfigFactory. | ||
| */ | ||
| class IpTaggingFilterConfig : public NamedHttpFilterConfigFactory { | ||
| public: | ||
| HttpFilterFactoryCb createFilterFactory(HttpFilterType type, const Json::Object& json_config, | ||
| const std::string& stat_prefix, | ||
| Server::Instance& server) override; | ||
|
|
||
| std::string name() override; | ||
| }; | ||
|
|
||
| } // Configuration | ||
| } // Server | ||
| } // 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
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.
ip_tags is supposed to be an array of ip_tag_name and ip_lists.