-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Rate Limit Configuration: Header value match action #526
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
Changes from 4 commits
c66f9aa
d892606
122207f
7b31174
9c2432f
b7e6cb4
0612fa5
d9ea002
a06a1f6
8aed769
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -611,6 +611,32 @@ const std::string Json::Schema::HTTP_RATE_LIMITS_CONFIGURATION_SCHEMA(R"EOF( | |
| }, | ||
| "required" : ["type", "descriptor_value"], | ||
| "additionalProperties" : false | ||
| }, | ||
| "header_value_match" : { | ||
| "type" : "object", | ||
| "properties" : { | ||
| "type" : { | ||
| "type" : "string", | ||
| "enum" : ["header_value_match"] | ||
| }, | ||
| "descriptor_value" : {"type" : "string"}, | ||
| "headers" : { | ||
|
Member
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. This is also duplicated several times now. Can we de-dup and do a sub-schema check in the ConfigUtility code? |
||
| "type" : "array", | ||
| "minItems" : 1, | ||
| "items" : { | ||
| "type" : "object", | ||
| "properties" : { | ||
| "name" : {"type" : "string"}, | ||
| "value" : {"type" : "string"}, | ||
| "regex" : {"type" : "boolean"} | ||
| }, | ||
| "required" : ["name"], | ||
| "additionalProperties" : false | ||
| } | ||
| }, | ||
| "required" : ["type", "descriptor_value", "headers"], | ||
| "additionalProperties" : false | ||
| } | ||
| } | ||
| }, | ||
| "type" : "object", | ||
|
|
@@ -626,7 +652,8 @@ const std::string Json::Schema::HTTP_RATE_LIMITS_CONFIGURATION_SCHEMA(R"EOF( | |
| {"$ref" : "#/definitions/destination_cluster"}, | ||
| {"$ref" : "#/definitions/request_headers"}, | ||
| {"$ref" : "#/definitions/remote_address"}, | ||
| {"$ref" : "#/definitions/generic_key"} | ||
| {"$ref" : "#/definitions/generic_key"}, | ||
| {"$ref" : "#/definitions/header_value_match"} | ||
| ] | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| #include "config_utility.h" | ||
|
Member
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. Did you change any code in config_utility or is it just copy/paste to a new file without any changes?
Contributor
Author
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. Copy Paste
Member
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. OK not a big deal but in the future I would do a separate code move PR, just easier to review. |
||
|
|
||
| namespace Router { | ||
|
|
||
| Upstream::ResourcePriority ConfigUtility::parsePriority(const Json::Object& config) { | ||
| std::string priority_string = config.getString("priority", "default"); | ||
| if (priority_string == "default") { | ||
| return Upstream::ResourcePriority::Default; | ||
| } else if (priority_string == "high") { | ||
| return Upstream::ResourcePriority::High; | ||
| } else { | ||
| throw EnvoyException(fmt::format("invalid resource priority '{}'", priority_string)); | ||
| } | ||
| } | ||
|
|
||
| bool ConfigUtility::matchHeaders(const Http::HeaderMap& request_headers, | ||
| const std::vector<HeaderData>& config_headers) { | ||
| bool matches = true; | ||
|
|
||
| if (!config_headers.empty()) { | ||
| for (const HeaderData& cfg_header_data : config_headers) { | ||
| const Http::HeaderEntry* header = request_headers.get(cfg_header_data.name_); | ||
| if (cfg_header_data.value_.empty()) { | ||
| matches &= (header != nullptr); | ||
| } else if (!cfg_header_data.is_regex_) { | ||
| matches &= (header != nullptr) && (header->value() == cfg_header_data.value_.c_str()); | ||
| } else { | ||
| matches &= (header != nullptr) && | ||
| std::regex_match(header->value().c_str(), cfg_header_data.regex_pattern_); | ||
| } | ||
| if (!matches) { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return matches; | ||
| } | ||
|
|
||
| } // Router | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| #pragma once | ||
|
|
||
| #include "envoy/json/json_object.h" | ||
| #include "envoy/upstream/resource_manager.h" | ||
|
|
||
| #include "common/http/headers.h" | ||
|
|
||
| namespace Router { | ||
|
|
||
| /** | ||
| * Utility routines for loading route configuration and matching runtime request headers. | ||
| */ | ||
| class ConfigUtility { | ||
| public: | ||
| struct HeaderData { | ||
| HeaderData(const Http::LowerCaseString& name, const std::string& value, const bool is_regex) | ||
| : name_(name), value_(value), regex_pattern_(value_, std::regex::optimize), | ||
| is_regex_(is_regex) {} | ||
|
|
||
| const Http::LowerCaseString name_; | ||
| const std::string value_; | ||
| const std::regex regex_pattern_; | ||
| const bool is_regex_; | ||
| }; | ||
|
|
||
| /** | ||
| * @return the resource priority parsed from JSON. | ||
| */ | ||
| static Upstream::ResourcePriority parsePriority(const Json::Object& config); | ||
|
|
||
| /** | ||
| * See if the specified headers are present in the request headers. | ||
| * @param headers supplies the list of headers to match | ||
| * @param request_headers supplies the list of request headers to compare against search_list | ||
| * @return true all the headers (and values) in the search_list set are found in the | ||
| * request_headers | ||
| */ | ||
| static bool matchHeaders(const Http::HeaderMap& headers, | ||
| const std::vector<HeaderData>& request_headers); | ||
| }; | ||
|
|
||
| } // Router |
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 think we now have this text in potentially 3 places in the docs. Can we de-dup this and just link to it from all 3 places?