-
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 2 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 |
---|---|---|
|
@@ -101,6 +101,11 @@ class StreamFilterCallbacks { | |
* put into the access log. | ||
*/ | ||
virtual AccessLog::RequestInfo& requestInfo() PURE; | ||
|
||
/** | ||
* @return the trusted address for the connection. | ||
*/ | ||
virtual const std::string& address() PURE; | ||
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. Perhaps downstreamAddress() ? 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 like it, changed to downstreamAddress(). |
||
}; | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -221,6 +221,7 @@ class ConnectionManagerImpl : Logger::Loggable<Logger::Id::http>, | |
const Router::StableRouteTable& routeTable() override { return *this; } | ||
uint64_t streamId() override; | ||
AccessLog::RequestInfo& requestInfo() override; | ||
const std::string& address() override; | ||
|
||
// Router::StableRouteTable | ||
const Router::RedirectEntry* redirectRequest(const HeaderMap& headers) const { | ||
|
@@ -328,6 +329,7 @@ class ConnectionManagerImpl : Logger::Loggable<Logger::Id::http>, | |
void encodeTrailers(ActiveStreamEncoderFilter* filter, HeaderMap& trailers); | ||
void maybeEndEncode(bool end_stream); | ||
uint64_t streamId() { return stream_id_; } | ||
const std::string& address() { return address_; } | ||
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 don't think this is needed 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. Yes, it will be removed. |
||
|
||
// Http::StreamCallbacks | ||
void onResetStream(StreamResetReason reason) override; | ||
|
@@ -366,6 +368,7 @@ class ConnectionManagerImpl : Logger::Loggable<Logger::Id::http>, | |
std::list<std::function<void()>> reset_callbacks_; | ||
State state_; | ||
AccessLog::RequestInfoImpl request_info_; | ||
std::string address_; | ||
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. downstream_address_ or whatever you choose 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. Changed to downstream_address_. |
||
}; | ||
|
||
typedef std::unique_ptr<ActiveStream> ActiveStreamPtr; | ||
|
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 |
---|---|---|
|
@@ -16,7 +16,8 @@ const Http::HeaderMapImpl Filter::TOO_MANY_REQUESTS_HEADER{ | |
|
||
void ServiceToServiceAction::populateDescriptors(const Router::RouteEntry& route, | ||
std::vector<::RateLimit::Descriptor>& descriptors, | ||
FilterConfig& config, const HeaderMap&) { | ||
FilterConfig& config, const HeaderMap&, | ||
StreamDecoderFilterCallbacks&) { | ||
// We limit on 2 dimensions. | ||
// 1) All calls to the given cluster. | ||
// 2) Calls to the given cluster and from this cluster. | ||
|
@@ -28,7 +29,8 @@ void ServiceToServiceAction::populateDescriptors(const Router::RouteEntry& route | |
|
||
void RequestHeadersAction::populateDescriptors(const Router::RouteEntry& route, | ||
std::vector<::RateLimit::Descriptor>& descriptors, | ||
FilterConfig&, const HeaderMap& headers) { | ||
FilterConfig&, const HeaderMap& headers, | ||
StreamDecoderFilterCallbacks&) { | ||
std::string header_value = headers.get(header_name_); | ||
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. const std::string& while you are here 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. |
||
if (header_value.empty()) { | ||
return; | ||
|
@@ -44,6 +46,25 @@ void RequestHeadersAction::populateDescriptors(const Router::RouteEntry& route, | |
descriptors.push_back({{{"route_key", route_key}, {descriptor_key_, header_value}}}); | ||
} | ||
|
||
void RemoteAddressAction::populateDescriptors(const Router::RouteEntry& route, | ||
std::vector<::RateLimit::Descriptor>& descriptors, | ||
FilterConfig&, const HeaderMap&, | ||
StreamDecoderFilterCallbacks& callbacks) { | ||
const std::string& remote_address = callbacks.address(); | ||
if (remote_address.empty()) { | ||
return; | ||
} | ||
|
||
descriptors.push_back({{{"remote_address", remote_address}}}); | ||
|
||
const std::string& route_key = route.rateLimitPolicy().routeKey(); | ||
if (route_key.empty()) { | ||
return; | ||
} | ||
|
||
descriptors.push_back({{{"route_key", route_key}, {"remote_address", remote_address}}}); | ||
} | ||
|
||
FilterConfig::FilterConfig(const Json::Object& config, const std::string& local_service_cluster, | ||
Stats::Store& stats_store, Runtime::Loader& runtime) | ||
: domain_(config.getString("domain")), local_service_cluster_(local_service_cluster), | ||
|
@@ -54,6 +75,8 @@ FilterConfig::FilterConfig(const Json::Object& config, const std::string& local_ | |
actions_.emplace_back(new ServiceToServiceAction()); | ||
} else if (type == "request_headers") { | ||
actions_.emplace_back(new RequestHeadersAction(action)); | ||
} else if (type == "remote_address") { | ||
actions_.emplace_back(new RemoteAddressAction()); | ||
} else { | ||
throw EnvoyException(fmt::format("unknown http rate limit filter action '{}'", type)); | ||
} | ||
|
@@ -69,7 +92,7 @@ FilterHeadersStatus Filter::decodeHeaders(HeaderMap& headers, bool) { | |
if (route && route->rateLimitPolicy().doGlobalLimiting()) { | ||
std::vector<::RateLimit::Descriptor> descriptors; | ||
for (const ActionPtr& action : config_->actions()) { | ||
action->populateDescriptors(*route, descriptors, *config_, headers); | ||
action->populateDescriptors(*route, descriptors, *config_, headers, *callbacks_); | ||
} | ||
|
||
if (!descriptors.empty()) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -341,4 +341,22 @@ TEST_F(ConnectionManagerUtilityTest, MutateResponseHeadersReturnXRequestId) { | |
EXPECT_EQ("request-id", response_headers.get("x-request-id")); | ||
} | ||
|
||
TEST_F(ConnectionManagerUtilityTest, TwoAddressesInXFF) { | ||
const std::string first_address = "34.0.0.1"; | ||
const std::string second_address = "10.0.0.1"; | ||
HeaderMapImpl request_headers{ | ||
{"x-forwarded-for", fmt::format("{0},{1}", first_address, second_address)}}; | ||
EXPECT_EQ(second_address, ConnectionManagerUtility::getLastAddressFromXFF(request_headers)); | ||
} | ||
|
||
TEST_F(ConnectionManagerUtilityTest, EmptyXFF) { | ||
HeaderMapImpl request_headers; | ||
EXPECT_EQ("", ConnectionManagerUtility::getLastAddressFromXFF(request_headers)); | ||
} | ||
|
||
TEST_F(ConnectionManagerUtilityTest, OneAddressInXff) { | ||
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. Fix capitalization to be consistent. 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 |
||
const std::string first_address = "34.0.0.1"; | ||
HeaderMapImpl request_headers{{"x-forwarded-for", first_address}}; | ||
EXPECT_EQ(first_address, ConnectionManagerUtility::getLastAddressFromXFF(request_headers)); | ||
} | ||
} // 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_, address()).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_, address()).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_, address()).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