-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Remote Address rate limiting #107
Changes from 3 commits
5164d62
ca4335f
42ef771
c1ee2bc
c9d6871
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#include "conn_manager_utility.h" | ||
|
||
#include "common/common/empty_string.h" | ||
#include "common/http/headers.h" | ||
#include "common/http/utility.h" | ||
#include "common/network/utility.h" | ||
|
@@ -128,4 +129,15 @@ void ConnectionManagerUtility::mutateResponseHeaders(Http::HeaderMap& response_h | |
} | ||
} | ||
|
||
std::string | ||
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. i'd add this to http::utility where we do manipulations with xff header 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. Yeah that makes sense, let's move code/tests there. 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. That does make more sense. I'll move it there. |
||
ConnectionManagerUtility::getLastAddressFromXFF(const Http::HeaderMap& request_headers) { | ||
std::vector<std::string> xff_address_list = | ||
StringUtil::split(request_headers.get(Headers::get().ForwardedFor), ','); | ||
|
||
if (xff_address_list.empty()) { | ||
return EMPTY_STRING; | ||
} | ||
return xff_address_list.back(); | ||
} | ||
|
||
} // Http |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#include "common/common/empty_string.h" | ||
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. nit: alpha order 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. Fixed. |
||
#include "common/buffer/buffer_impl.h" | ||
#include "common/http/filter/ratelimit.h" | ||
#include "common/stats/stats_impl.h" | ||
|
@@ -11,6 +12,7 @@ using testing::InSequence; | |
using testing::Invoke; | ||
using testing::NiceMock; | ||
using testing::Return; | ||
using testing::ReturnRef; | ||
using testing::WithArgs; | ||
|
||
namespace Http { | ||
|
@@ -91,6 +93,15 @@ class HttpRateLimitFilterTest : public testing::Test { | |
} | ||
)EOF"; | ||
|
||
const std::string address_json = R"EOF( | ||
{ | ||
"domain": "foo", | ||
"actions": [ | ||
{"type": "remote_address"} | ||
] | ||
} | ||
)EOF"; | ||
|
||
FilterConfigPtr config_; | ||
::RateLimit::MockClient* client_; | ||
std::unique_ptr<Filter> filter_; | ||
|
@@ -319,5 +330,63 @@ TEST_F(HttpRateLimitFilterTest, NoRateLimitHeaderMatch) { | |
EXPECT_EQ(FilterTrailersStatus::Continue, filter_->decodeTrailers(request_headers_)); | ||
} | ||
|
||
TEST_F(HttpRateLimitFilterTest, AddressRateLimiting) { | ||
SetUpTest(address_json); | ||
filter_callbacks_.route_table_.route_entry_.rate_limit_policy_.do_global_limiting_ = true; | ||
|
||
std::string address = "10.0.0.1"; | ||
EXPECT_CALL(filter_callbacks_, downstreamAddress()).WillOnce(ReturnRef(address)); | ||
EXPECT_CALL(*client_, limit(_, "foo", testing::ContainerEq(std::vector<::RateLimit::Descriptor>{ | ||
{{{"remote_address", address}}}}))) | ||
.WillOnce(WithArgs<0>(Invoke([&](::RateLimit::RequestCallbacks& callbacks) | ||
-> void { request_callbacks_ = &callbacks; }))); | ||
|
||
EXPECT_EQ(FilterHeadersStatus::StopIteration, filter_->decodeHeaders(request_headers_, false)); | ||
EXPECT_EQ(FilterDataStatus::StopIterationAndBuffer, filter_->decodeData(data_, false)); | ||
EXPECT_EQ(FilterTrailersStatus::StopIteration, filter_->decodeTrailers(request_headers_)); | ||
|
||
EXPECT_CALL(filter_callbacks_, continueDecoding()); | ||
request_callbacks_->complete(::RateLimit::LimitStatus::OK); | ||
|
||
EXPECT_EQ(1U, stats_store_.counter("cluster.fake_cluster.ratelimit.ok").value()); | ||
} | ||
|
||
TEST_F(HttpRateLimitFilterTest, RouteAddressRateLimiting) { | ||
SetUpTest(address_json); | ||
filter_callbacks_.route_table_.route_entry_.rate_limit_policy_.do_global_limiting_ = true; | ||
filter_callbacks_.route_table_.route_entry_.rate_limit_policy_.route_key_ = "test_key"; | ||
|
||
std::string address = "10.0.0.1"; | ||
EXPECT_CALL(filter_callbacks_, downstreamAddress()).WillOnce(ReturnRef(address)); | ||
EXPECT_CALL(*client_, | ||
limit(_, "foo", testing::ContainerEq(std::vector<::RateLimit::Descriptor>{ | ||
{{{"remote_address", address}}}, | ||
{{{"route_key", "test_key"}, {"remote_address", address}}}}))) | ||
.WillOnce(WithArgs<0>(Invoke([&](::RateLimit::RequestCallbacks& callbacks) | ||
-> void { request_callbacks_ = &callbacks; }))); | ||
|
||
EXPECT_EQ(FilterHeadersStatus::StopIteration, filter_->decodeHeaders(request_headers_, false)); | ||
EXPECT_EQ(FilterDataStatus::StopIterationAndBuffer, filter_->decodeData(data_, false)); | ||
EXPECT_EQ(FilterTrailersStatus::StopIteration, filter_->decodeTrailers(request_headers_)); | ||
|
||
EXPECT_CALL(filter_callbacks_, continueDecoding()); | ||
request_callbacks_->complete(::RateLimit::LimitStatus::OK); | ||
|
||
EXPECT_EQ(1U, stats_store_.counter("cluster.fake_cluster.ratelimit.ok").value()); | ||
} | ||
|
||
TEST_F(HttpRateLimitFilterTest, NoAddressRateLimiting) { | ||
SetUpTest(address_json); | ||
filter_callbacks_.route_table_.route_entry_.rate_limit_policy_.do_global_limiting_ = true; | ||
|
||
EXPECT_CALL(filter_callbacks_, downstreamAddress()).WillOnce(ReturnRef(EMPTY_STRING)); | ||
|
||
EXPECT_CALL(*client_, limit(_, _, _)).Times(0); | ||
|
||
EXPECT_EQ(FilterHeadersStatus::Continue, filter_->decodeHeaders(request_headers_, false)); | ||
EXPECT_EQ(FilterDataStatus::Continue, filter_->decodeData(data_, false)); | ||
EXPECT_EQ(FilterTrailersStatus::Continue, filter_->decodeTrailers(request_headers_)); | ||
} | ||
|
||
} // RateLimit | ||
} // Http |
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.
send key form as well as this one like the header one.
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.
Updated documents and ratelimit.cc