Skip to content
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

Ratelimit: Add optional descriptor key to generic_key action #12734

Merged
merged 2 commits into from
Aug 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/envoy/config/route/v3/route_components.proto
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,10 @@ message RateLimit {

// The value to use in the descriptor entry.
string descriptor_value = 1 [(validate.rules).string = {min_bytes: 1}];

// An optional key to use in the descriptor entry. If not set it defaults
// to 'generic_key' as the descriptor key.
string descriptor_key = 2;
}

// The following descriptor entry is appended to the descriptor:
Expand Down
4 changes: 4 additions & 0 deletions api/envoy/config/route/v4alpha/route_components.proto

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/root/version_history/current.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ New Features
* overload management: add :ref:`scaling <envoy_v3_api_field_config.overload.v3.Trigger.scaled>` trigger for OverloadManager actions.
* postgres network filter: :ref:`metadata <config_network_filters_postgres_proxy_dynamic_metadata>` is produced based on SQL query.
* ratelimit: added :ref:`enable_x_ratelimit_headers <envoy_v3_api_msg_extensions.filters.http.ratelimit.v3.RateLimit>` option to enable `X-RateLimit-*` headers as defined in `draft RFC <https://tools.ietf.org/id/draft-polli-ratelimit-headers-03.html>`_.
* ratelimit: added support for optional :ref:`descriptor_key <envoy_v3_api_field_config.route.v3.RateLimit.Action.generic_key>` to Generic Key action.
* rbac filter: added a log action to the :ref:`RBAC filter <envoy_v3_api_msg_config.rbac.v3.RBAC>` which sets dynamic metadata to inform access loggers whether to log.
* redis: added fault injection support :ref:`fault injection for redis proxy <envoy_v3_api_field_extensions.filters.network.redis_proxy.v3.RedisProxy.faults>`, described further in :ref:`configuration documentation <config_network_filters_redis_proxy>`.
* router: added a new :ref:`rate limited retry back off <envoy_v3_api_msg_config.route.v3.RetryPolicy.RateLimitedRetryBackOff>` strategy that uses headers like `Retry-After` or `X-RateLimit-Reset` to decide the back off interval.
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion source/common/router/router_ratelimit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ bool GenericKeyAction::populateDescriptor(const Router::RouteEntry&,
RateLimit::Descriptor& descriptor, const std::string&,
const Http::HeaderMap&, const Network::Address::Instance&,
const envoy::config::core::v3::Metadata*) const {
descriptor.entries_.push_back({"generic_key", descriptor_value_});
descriptor.entries_.push_back({descriptor_key_, descriptor_value_});
return true;
}

Expand Down
5 changes: 4 additions & 1 deletion source/common/router/router_ratelimit.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ class RemoteAddressAction : public RateLimitAction {
class GenericKeyAction : public RateLimitAction {
public:
GenericKeyAction(const envoy::config::route::v3::RateLimit::Action::GenericKey& action)
: descriptor_value_(action.descriptor_value()) {}
: descriptor_value_(action.descriptor_value()),
descriptor_key_(!action.descriptor_key().empty() ? action.descriptor_key()
: "generic_key") {}

// Router::RateLimitAction
bool populateDescriptor(const Router::RouteEntry& route, RateLimit::Descriptor& descriptor,
Expand All @@ -108,6 +110,7 @@ class GenericKeyAction : public RateLimitAction {

private:
const std::string descriptor_value_;
const std::string descriptor_key_;
};

/**
Expand Down
32 changes: 32 additions & 0 deletions test/common/router/router_ratelimit_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,38 @@ TEST_F(RateLimitPolicyEntryTest, RateLimitKey) {
testing::ContainerEq(descriptors_));
}

TEST_F(RateLimitPolicyEntryTest, GenericKeyWithSetDescriptorKey) {
const std::string yaml = R"EOF(
actions:
- generic_key:
descriptor_key: fake_key
descriptor_value: fake_value
)EOF";

setupTest(yaml);

rate_limit_entry_->populateDescriptors(route_, descriptors_, "", header_, default_remote_address_,
dynamic_metadata_);
EXPECT_THAT(std::vector<Envoy::RateLimit::Descriptor>({{{{"fake_key", "fake_value"}}}}),
testing::ContainerEq(descriptors_));
}

TEST_F(RateLimitPolicyEntryTest, GenericKeyWithEmptyDescriptorKey) {
const std::string yaml = R"EOF(
actions:
- generic_key:
descriptor_key: ""
descriptor_value: fake_value
)EOF";

setupTest(yaml);

rate_limit_entry_->populateDescriptors(route_, descriptors_, "", header_, default_remote_address_,
dynamic_metadata_);
EXPECT_THAT(std::vector<Envoy::RateLimit::Descriptor>({{{{"generic_key", "fake_value"}}}}),
testing::ContainerEq(descriptors_));
}

TEST_F(RateLimitPolicyEntryTest, DynamicMetaDataMatch) {
const std::string yaml = R"EOF(
actions:
Expand Down