-
Notifications
You must be signed in to change notification settings - Fork 5.3k
IP Matcher on a list of CIDR ranges #16592
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
23 commits
Select commit
Hold shift + click to select a range
cd38be7
IP Matcher on a list of CIDR ranges
aguinet 16c5477
Rename IP to Ip + nit fixes
aguinet ef8fe28
API: remove unused protobuf imports
aguinet bdc5274
Review: nit on doc
aguinet 6d7f90b
Merge branch 'main' into feature/ips_matcher
aguinet e940002
Cleanup extension BUILD definition
aguinet a0360a7
Review by @snowp
aguinet e027a05
Add extension metadata
aguinet 04b5afe
emit stat + log when unable to parse an IP
aguinet c4b0f24
Merge branch 'main' into feature/ips_matcher
aguinet 354ac87
Adapt the build system
aguinet d565e78
synchronize ip matcher protobuf
aguinet 3b2391d
add config test with stat prefix
aguinet 7960aa3
Stats: prefix mandatory + documentation
aguinet 5fac922
Add code owners
aguinet 551ddcb
Review by @snowp
aguinet 6365fea
Merge branch 'main' into feature/ips_matcher
aguinet ff91bae
Adapt to new matching interface
aguinet ea28e0d
Add a release note
aguinet 700187a
Avoid a copy (fix clang-tidy warning)
aguinet ff53fe4
config: only ASSERT that ranges are valid
aguinet 5aaaa86
Doc: explicit the concatenation process in the names of the statistics
aguinet 13fd781
Fix generated api
aguinet 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,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/config/core/v3:pkg", | ||
| "@com_github_cncf_udpa//udpa/annotations:pkg", | ||
| ], | ||
| ) |
38 changes: 38 additions & 0 deletions
38
api/envoy/extensions/matching/input_matchers/ip/v3/ip.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,38 @@ | ||
| syntax = "proto3"; | ||
|
|
||
| package envoy.extensions.matching.input_matchers.ip.v3; | ||
|
|
||
| import "envoy/config/core/v3/address.proto"; | ||
|
|
||
| import "udpa/annotations/status.proto"; | ||
| import "validate/validate.proto"; | ||
|
|
||
| option java_package = "io.envoyproxy.envoy.extensions.matching.input_matchers.ip.v3"; | ||
| option java_outer_classname = "IpProto"; | ||
| option java_multiple_files = true; | ||
| option (udpa.annotations.file_status).package_version_status = ACTIVE; | ||
|
|
||
| // [#protodoc-title: IP matcher] | ||
| // [#extension: envoy.matching.input_matchers.ip] | ||
|
|
||
| // This input matcher matches IPv4 or IPv6 addresses against a list of CIDR | ||
| // ranges. It returns true if and only if the input IP belongs to at least one | ||
| // of these CIDR ranges. Internally, it uses a Level-Compressed trie, as | ||
| // described in the paper `IP-address lookup using LC-tries | ||
| // <https://www.nada.kth.se/~snilsson/publications/IP-address-lookup-using-LC-tries/>`_ | ||
| // by S. Nilsson and G. Karlsson. For "big" lists of IPs, this matcher is more | ||
| // efficient than multiple single IP matcher, that would have a linear cost. | ||
| message Ip { | ||
| // Match if the IP belongs to any of these CIDR ranges. | ||
| repeated config.core.v3.CidrRange cidr_ranges = 1 [(validate.rules).repeated = {min_items: 1}]; | ||
|
|
||
| // The human readable prefix to use when emitting statistics for the IP input | ||
| // matcher. Names in the table below are concatenated to this prefix. | ||
| // | ||
| // .. csv-table:: | ||
| // :header: Name, Type, Description | ||
| // :widths: 1, 1, 2 | ||
| // | ||
| // ip_parsing_failed, Counter, Total number of IP addresses the matcher was unable to parse | ||
| string stat_prefix = 2 [(validate.rules).string = {min_len: 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
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
generated_api_shadow/envoy/extensions/matching/input_matchers/ip/v3/BUILD
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
38 changes: 38 additions & 0 deletions
38
generated_api_shadow/envoy/extensions/matching/input_matchers/ip/v3/ip.proto
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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_library( | ||
| name = "ip_lib", | ||
| srcs = ["matcher.cc"], | ||
| hdrs = ["matcher.h"], | ||
| deps = [ | ||
| "//envoy/matcher:matcher_interface", | ||
| "//source/common/network:lc_trie_lib", | ||
| ], | ||
| ) | ||
|
|
||
| envoy_cc_extension( | ||
| name = "config", | ||
| srcs = ["config.cc"], | ||
| hdrs = ["config.h"], | ||
| deps = [ | ||
| ":ip_lib", | ||
| "//envoy/matcher:matcher_interface", | ||
| "//envoy/registry", | ||
| "//envoy/server:factory_context_interface", | ||
| "@envoy_api//envoy/extensions/matching/input_matchers/ip/v3:pkg_cc_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,47 @@ | ||
| #include "source/extensions/matching/input_matchers/ip/config.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Extensions { | ||
| namespace Matching { | ||
| namespace InputMatchers { | ||
| namespace IP { | ||
|
|
||
| Envoy::Matcher::InputMatcherFactoryCb | ||
| Config::createInputMatcherFactoryCb(const Protobuf::Message& config, | ||
| Server::Configuration::FactoryContext& context) { | ||
| const auto& ip_config = MessageUtil::downcastAndValidate< | ||
| const envoy::extensions::matching::input_matchers::ip::v3::Ip&>( | ||
| config, context.messageValidationVisitor()); | ||
|
|
||
| const auto& cidr_ranges = ip_config.cidr_ranges(); | ||
| std::vector<Network::Address::CidrRange> ranges; | ||
| ranges.reserve(cidr_ranges.size()); | ||
| for (const auto& cidr_range : cidr_ranges) { | ||
| const std::string& address = cidr_range.address_prefix(); | ||
| const uint32_t prefix_len = cidr_range.prefix_len().value(); | ||
| const auto range = Network::Address::CidrRange::create(address, prefix_len); | ||
| // We only assert that the range is valid because: | ||
| // * if "address" can't be parsed, it will throw an EnvoyException | ||
| // * prefix_len can't be < 0 as per the protobuf definition as an uint32_t | ||
| // * if prefix_len is too big, CidrRange::create clamps it to a valid value | ||
| // => it is thus not possible to create an invalid range. | ||
| ASSERT(range.isValid(), "address range should be valid!"); | ||
| ranges.emplace_back(std::move(range)); | ||
| } | ||
|
|
||
| const std::string& stat_prefix = ip_config.stat_prefix(); | ||
| Stats::Scope& scope = context.scope(); | ||
| return [ranges, stat_prefix, &scope]() { | ||
| return std::make_unique<Matcher>(ranges, stat_prefix, scope); | ||
| }; | ||
| } | ||
| /** | ||
| * Static registration for the consistent hashing matcher. @see RegisterFactory. | ||
| */ | ||
| REGISTER_FACTORY(Config, Envoy::Matcher::InputMatcherFactory); | ||
|
|
||
| } // namespace IP | ||
| } // namespace InputMatchers | ||
| } // namespace Matching | ||
| } // 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,33 @@ | ||
| #pragma once | ||
|
|
||
| #include "envoy/extensions/matching/input_matchers/ip/v3/ip.pb.h" | ||
| #include "envoy/extensions/matching/input_matchers/ip/v3/ip.pb.validate.h" | ||
| #include "envoy/matcher/matcher.h" | ||
| #include "envoy/server/factory_context.h" | ||
|
|
||
| #include "source/common/protobuf/utility.h" | ||
| #include "source/extensions/matching/input_matchers/ip/matcher.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Extensions { | ||
| namespace Matching { | ||
| namespace InputMatchers { | ||
| namespace IP { | ||
|
|
||
| class Config : public Envoy::Matcher::InputMatcherFactory { | ||
| public: | ||
| Envoy::Matcher::InputMatcherFactoryCb | ||
| createInputMatcherFactoryCb(const Protobuf::Message& config, | ||
| Server::Configuration::FactoryContext& factory_context) override; | ||
|
|
||
| std::string name() const override { return "envoy.matching.matchers.ip"; } | ||
|
|
||
| ProtobufTypes::MessagePtr createEmptyConfigProto() override { | ||
| return std::make_unique<envoy::extensions::matching::input_matchers::ip::v3::Ip>(); | ||
| } | ||
| }; | ||
| } // namespace IP | ||
| } // namespace InputMatchers | ||
| } // namespace Matching | ||
| } // 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,49 @@ | ||
| #include "source/extensions/matching/input_matchers/ip/matcher.h" | ||
|
|
||
| #include "source/common/network/utility.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Extensions { | ||
| namespace Matching { | ||
| namespace InputMatchers { | ||
| namespace IP { | ||
|
|
||
| namespace { | ||
|
|
||
| MatcherStats generateStats(absl::string_view prefix, Stats::Scope& scope) { | ||
| return MatcherStats{IP_MATCHER_STATS(POOL_COUNTER_PREFIX(scope, prefix))}; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| Matcher::Matcher(std::vector<Network::Address::CidrRange> const& ranges, | ||
| absl::string_view stat_prefix, | ||
| Stats::Scope& stat_scope) | ||
| : // We could put "false" instead of "true". What matters is that the IP | ||
| // belongs to the trie. We could further optimize the storage of LcTrie in | ||
| // this case by implementing an LcTrie<void> specialization that doesn't | ||
| // store any associated data. | ||
| trie_({{true, ranges}}), stats_(generateStats(stat_prefix, stat_scope)) {} | ||
|
|
||
| bool Matcher::match(absl::optional<absl::string_view> input) { | ||
| if (!input) { | ||
| return false; | ||
| } | ||
| const absl::string_view ip_str = *input; | ||
| if (ip_str.empty()) { | ||
| return false; | ||
| } | ||
| const auto ip = Network::Utility::parseInternetAddressNoThrow(std::string{ip_str}); | ||
| if (!ip) { | ||
| stats_.ip_parsing_failed_.inc(); | ||
| ENVOY_LOG(debug, "IP matcher: unable to parse address '{}'", ip_str); | ||
| return false; | ||
| } | ||
| return !trie_.getData(ip).empty(); | ||
| } | ||
|
|
||
| } // namespace IP | ||
| } // namespace InputMatchers | ||
| } // namespace Matching | ||
| } // 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,39 @@ | ||
| #pragma once | ||
|
|
||
| #include <vector> | ||
|
|
||
| #include "envoy/matcher/matcher.h" | ||
| #include "envoy/network/address.h" | ||
| #include "envoy/stats/stats_macros.h" | ||
|
|
||
| #include "source/common/network/lc_trie.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Extensions { | ||
| namespace Matching { | ||
| namespace InputMatchers { | ||
| namespace IP { | ||
|
|
||
| #define IP_MATCHER_STATS(COUNTER) COUNTER(ip_parsing_failed) | ||
|
|
||
| struct MatcherStats { | ||
| IP_MATCHER_STATS(GENERATE_COUNTER_STRUCT); | ||
| }; | ||
|
|
||
| class Matcher : public Envoy::Matcher::InputMatcher, Logger::Loggable<Logger::Id::filter> { | ||
| public: | ||
| Matcher(std::vector<Network::Address::CidrRange> const& ranges, absl::string_view stat_prefix, | ||
| Stats::Scope& stat_scope); | ||
| bool match(absl::optional<absl::string_view> input) override; | ||
| absl::optional<const MatcherStats> stats() const { return stats_; } | ||
|
|
||
| private: | ||
| const Network::LcTrie::LcTrie<bool> trie_; | ||
| MatcherStats stats_; | ||
| }; | ||
|
|
||
| } // namespace IP | ||
| } // namespace InputMatchers | ||
| } // namespace Matching | ||
| } // 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: clarify the the following counters will be concatenated to the stat_prefix
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.
Done in 5aaaa86