Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences
C++ algoliadatastoreelasticsearchenterprise-searchfacetingfull-text-searchfuzzy-searchgeosearchin-memoryinstantsearchmerchandisingpineconesearchsearch-enginesemantic-searchsimilarity-searchsite-searchsynonymstypo-tolerancevector-search
Rate limit API updates & improvements (#846)
Ozan Armağan07b456c2290d
diff --git a/include/core_api.h b/include/core_api.hindex 0bc1a615..fd5a4140 100644--- a/include/core_api.h+++ b/include/core_api.h@@ -131,6 +131,14 @@ bool del_rate_limit(const std::shared_ptr<http_req>& req, const std::shared_ptr<bool post_rate_limit(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);+bool get_active_throttles(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);++bool get_limit_exceed_counts(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);++bool del_throttle(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);++bool del_exceed(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);+// Misc helpersvoid get_collections_for_auth(std::map<std::string, std::string>& req_params, const std::string& body,@@ -143,3 +151,5 @@ bool is_doc_import_route(uint64_t route_hash);bool is_doc_write_route(uint64_t route_hash);bool is_doc_del_route(uint64_t route_hash);++Option<std::pair<std::string,std::string>> get_api_key_and_ip(const std::string& metadata);diff --git a/include/ratelimit_manager.h b/include/ratelimit_manager.hindex 90f85eae..8b3731e6 100644--- a/include/ratelimit_manager.h+++ b/include/ratelimit_manager.h@@ -22,30 +22,29 @@ enum class RateLimitAction {throttle};+// Entity type enum for rate limit rulesenum class RateLimitedEntityType {ip,api_key};+// overload operator! to get inverse of RateLimitedEntityType+inline RateLimitedEntityType operator!(const RateLimitedEntityType& entity_type) {+ switch (entity_type) {+ case RateLimitedEntityType::ip:+ return RateLimitedEntityType::api_key;+ case RateLimitedEntityType::api_key:+ return RateLimitedEntityType::ip;+ }+}++// Max requests struct for rate limit rulesstruct rate_limit_max_requests_t {int64_t minute_threshold = -1;int64_t hour_threshold = -1;};-struct rate_limit_rule_t {- uint32_t id;- RateLimitAction action;- RateLimitedEntityType entity_type;- std::vector<std::string> entity_ids;- rate_limit_max_requests_t max_requests;- int64_t auto_ban_threshold_num = -1;- int64_t auto_ban_num_days = -1;-- const nlohmann::json to_json() const;--};-// Entry struct for rate limit rule pointer hash map as keystruct rate_limit_entity_t {RateLimitedEntityType entity_type;@@ -57,6 +56,21 @@ struct rate_limit_entity_t {}};+// Struct for rate limit rules+struct rate_limit_rule_t {+ uint32_t id;+ RateLimitAction action;+ std::vector<rate_limit_entity_t> entities;+ rate_limit_max_requests_t max_requests;+ int64_t auto_ban_1m_threshold = -1;+ int64_t auto_ban_1m_duration_hours = -1;+ bool apply_limit_per_entity = false;+ uint32_t priority = 0;++ const nlohmann::json to_json() const;++};+// Request counter struct for ip addresses to keep track of requests for current and previous sampling periodstruct request_counter_t {int64_t current_requests_count_minute = 0;@@ -90,8 +104,18 @@ struct rate_limit_status_t {uint32_t status_id;int64_t throttling_from;int64_t throttling_to;- std::string value;- RateLimitedEntityType entity_type;+ rate_limit_entity_t entity;+ // optional second entity for AND bans+ Option<rate_limit_entity_t> and_entity = Option<rate_limit_entity_t>(404, "Not Found");++ rate_limit_status_t(const uint32_t status_id, const int64_t throttling_from, const int64_t throttling_to, const rate_limit_entity_t &entity, const rate_limit_entity_t* and_entity = nullptr) : status_id(status_id), throttling_from(throttling_from), throttling_to(throttling_to), entity(entity) {+ if (and_entity != nullptr) {+ this->and_entity = Option<rate_limit_entity_t>(*and_entity);+ }+ }++ // default constructor+ rate_limit_status_t() = default;const nlohmann::json to_json() const;@@ -99,6 +123,24 @@ struct rate_limit_status_t {void parse_json(const nlohmann::json& json);};+// Struct to store how many requests made by exceeded rate limit entities+struct rate_limit_exceed_t {+ uint32_t rule_id;+ std::string entities;+ uint64_t request_count = 0;++ const nlohmann::json to_json() const {+ nlohmann::json json;+ std::string api_key = entities.substr(0, entities.find("_"));+ std::string ip = entities.substr(entities.find("_") + 1);+ json["id"] = rule_id;+ json["api_key"] = api_key;+ json["ip"] = ip;+ json["request_count"] = request_count;+ return json;+ }+};+// Hash function for rate_limit_entity_tnamespace std {@@ -124,14 +166,11 @@ class RateLimitManagerstatic RateLimitManager* getInstance();- // Remove rate limit for entity- bool remove_rule_entity(const RateLimitedEntityType entity_type, const std::string &entity);-// Get vector of banned entitiesconst std::vector<rate_limit_status_t> get_banned_entities(const RateLimitedEntityType entity_type);// Check if request is rate limited for given entities- bool is_rate_limited(const std::vector<rate_limit_entity_t> &entities);+ bool is_rate_limited(const rate_limit_entity_t& api_key_entity, const rate_limit_entity_t& ip_entity);// Add rule by JSONOption<nlohmann::json> add_rule(const nlohmann::json &rule_json);@@ -145,12 +184,24 @@ class RateLimitManager// Delete rule by IDbool delete_rule_by_id(const uint64_t id);+ // Delete ban by ID+ bool delete_ban_by_id(const uint64_t id);++ // Delete throttle by ID+ bool delete_throttle_by_id(const uint32_t id);+// Get All rules as vectorconst std::vector<rate_limit_rule_t> get_all_rules();// Get all rules as jsonconst nlohmann::json get_all_rules_json();+ // Get exceeded entities and request counts as JSON+ const nlohmann::json get_exceeded_entities_json();++ // Get autobanned entities as JSON+ const nlohmann::json get_throttled_entities_json();+// Clear all rulesvoid clear_all();@@ -170,7 +221,6 @@ class RateLimitManagerStore *store;// Using a $ prefix so that these meta keys stay above record entries in a lexicographically ordered KV store-// Prefix for rate limit rulesstatic constexpr const char* RULES_NEXT_ID = "$RLN";static constexpr const char* RULES_PREFIX = "$RLRP";@@ -179,8 +229,9 @@ class RateLimitManagerstatic constexpr const char* BANS_NEXT_ID = "$RLBN";static constexpr const char* BANS_PREFIX = "$RLBP";--+ // Static instance of wildcard entities+ inline static const rate_limit_entity_t WILDCARD_IP = rate_limit_entity_t{RateLimitedEntityType::ip, ".*"};+ inline static const rate_limit_entity_t WILDCARD_API_KEY = rate_limit_entity_t{RateLimitedEntityType::api_key, ".*"};// ID of latest added ruleinline static uint32_t last_rule_id = 0;@@ -188,31 +239,37 @@ class RateLimitManager// ID of latest added baninline static uint32_t last_ban_id = 0;+ // ID of latest added throttle+ inline static uint32_t last_throttle_id = 0;+// Store for rate_limit_rule_tstd::unordered_map<uint64_t,rate_limit_rule_t> rule_store;// LRU Cache to store rate limit and request counts for entities- LRU::Cache<rate_limit_entity_t, request_counter_t> rate_limit_request_counts;+ LRU::Cache<std::string, request_counter_t> rate_limit_request_counts;// Unordered map to point rules from rule store for entities- std::unordered_map<rate_limit_entity_t, rate_limit_rule_t*> rate_limit_entities;+ std::unordered_map<rate_limit_entity_t, std::vector<rate_limit_rule_t*>> rate_limit_entities;// Unordered map to store banned entities- std::unordered_map<rate_limit_entity_t, rate_limit_status_t> throttled_entities;+ std::unordered_map<std::string, rate_limit_status_t> throttled_entities;++ // Hash map to store exceeds+ std::unordered_map<std::string, rate_limit_exceed_t> rate_limit_exceeds;// Mutex to protect access to ip_rate_limits and api_key_rate_limitsstd::shared_mutex rate_limit_mutex;// Helper function to ban an entity temporarily- void temp_ban_entity(const rate_limit_entity_t& entity, const int64_t number_of_days);+ void temp_ban_entity(const rate_limit_entity_t& entity, const uint64_t number_of_hours);// Helper function to ban an entity temporarily without locking mutex- void temp_ban_entity_wrapped(const rate_limit_entity_t& entity, const int64_t number_of_days);+ void temp_ban_entity_wrapped(const rate_limit_entity_t& entity, const uint64_t number_of_hours, const rate_limit_entity_t* and_entity = nullptr);// Helper function to check if JSON rule is validOption<bool> is_valid_rule(const nlohmann::json &rule_json);// Parse JSON rule to rate_limit_rule_t- Option<rate_limit_rule_t> parse_rule(const nlohmann::json &rule_json, bool alert_if_exists = true);+ static Option<rate_limit_rule_t> parse_rule(const nlohmann::json &rule_json);// Helper function to insert rule in storevoid insert_rule(const rate_limit_rule_t &rule);@@ -230,6 +287,15 @@ class RateLimitManager// Helper function to get current timestamptime_t get_current_time();+ // Helper function to get throttle key for entity if exists+ Option<std::string> get_throttle_key(const rate_limit_entity_t& ip_entity, const rate_limit_entity_t& api_key_entity);++ // Helper function to get request counter key according to rule type+ static const std::string get_request_counter_key(const rate_limit_rule_t& rule, const rate_limit_entity_t& ip_entity, const rate_limit_entity_t& api_key_entity);++ // Fill bucket rule for the given entity+ void fill_bucket(const rate_limit_entity_t& target_entity, const rate_limit_entity_t& other_entity, std::vector<rate_limit_rule_t*> &rules_bucket);+// Singleton instanceinline static RateLimitManager *instance;diff --git a/src/core_api.cpp b/src/core_api.cppindex a8ab91e8..8a53e707 100644--- a/src/core_api.cpp+++ b/src/core_api.cpp@@ -2,6 +2,7 @@#include <thread>#include <cstdlib>#include <app_metrics.h>+#include <regex>#include "typesense_server_utils.h"#include "core_api.h"#include "string_utils.h"@@ -511,7 +512,24 @@ bool post_multi_search(const std::shared_ptr<http_req>& req, const std::shared_preturn false;}- //LOG(INFO) << "REQ: " << req_json.dump(-1);+ // Get API key and IP+ if(!req->metadata.empty()) {+ auto api_key_ip_op = get_api_key_and_ip(req->metadata);+ if(!api_key_ip_op.ok()) {+ res->set(api_key_ip_op.code(), api_key_ip_op.error());+ return false;+ }+ const auto& api_key_ip = api_key_ip_op.get();+ auto rate_limit_manager = RateLimitManager::getInstance();++ // Check rate limiting first before doing any search, don't want to waste time if we're rate limited+ for(size_t i = 0; i < searches.size(); i++) {+ if(RateLimitManager::getInstance()->is_rate_limited({RateLimitedEntityType::api_key, api_key_ip.first}, {RateLimitedEntityType::ip, api_key_ip.second})) {+ res->set(429, "Rate limit exceeded or blocked");+ return false;+ }+ }+ }for(size_t i = 0; i < searches.size(); i++) {auto& search_params = searches[i];@@ -1821,6 +1839,10 @@ bool get_rate_limits(const std::shared_ptr<http_req>& req, const std::shared_ptrbool get_rate_limit(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {RateLimitManager* rateLimitManager = RateLimitManager::getInstance();// Convert param id to uint64_t+ if(!StringUtils::is_uint32_t(req->params["id"])) {+ res->set_400("{\"message\": \"Invalid ID\"}");+ return false;+ }uint64_t id = std::stoull(req->params["id"]);const auto& rule_option = rateLimitManager->find_rule_by_id(id);@@ -1836,6 +1858,10 @@ bool get_rate_limit(const std::shared_ptr<http_req>& req, const std::shared_ptr<bool put_rate_limit(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {RateLimitManager* rateLimitManager = RateLimitManager::getInstance();nlohmann::json req_json;+ if(!StringUtils::is_uint32_t(req->params["id"])) {+ res->set_400("{\"message\": \"Invalid ID\"}");+ return false;+ }uint64_t id = std::stoull(req->params["id"]);try {@@ -1859,6 +1885,10 @@ bool put_rate_limit(const std::shared_ptr<http_req>& req, const std::shared_ptr<bool del_rate_limit(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {RateLimitManager* rateLimitManager = RateLimitManager::getInstance();+ if(!StringUtils::is_uint32_t(req->params["id"])) {+ res->set_400("{\"message\": \"Invalid ID\"}");+ return false;+ }uint64_t id = std::stoull(req->params["id"]);const auto& rule_option = rateLimitManager->find_rule_by_id(id);@@ -1868,7 +1898,9 @@ bool del_rate_limit(const std::shared_ptr<http_req>& req, const std::shared_ptr<}rateLimitManager->delete_rule_by_id(id);- res->set_200("OK");+ nlohmann::json res_json;+ res_json["id"] = id;+ res->set_200(res_json.dump());return true;}@@ -1892,4 +1924,85 @@ bool post_rate_limit(const std::shared_ptr<http_req>& req, const std::shared_ptrres->set_200(add_rule_result.get().dump());return true;+}++bool get_active_throttles(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {+ RateLimitManager* rateLimitManager = RateLimitManager::getInstance();+ res->set_200(rateLimitManager->get_throttled_entities_json().dump());+ return true;+}++bool del_throttle(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {+ RateLimitManager* rateLimitManager = RateLimitManager::getInstance();+ if(!StringUtils::is_uint32_t(req->params["id"])) {+ res->set_400("{\"message\": \"Invalid ID\"}");+ return false;+ }+ uint64_t id = std::stoull(req->params["id"]);+ bool res_ = rateLimitManager->delete_ban_by_id(id);+ if(!res_) {+ res->set_400("{\"message\": \"Invalid ID\"}");+ return false;+ }+ nlohmann::json res_json;+ res_json["id"] = id;+ res->set_200(res_json.dump());+ return true;+}++bool del_exceed(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {+ RateLimitManager* rateLimitManager = RateLimitManager::getInstance();+ if(!StringUtils::is_uint32_t(req->params["id"])) {+ res->set_400("{\"message\": \"Invalid ID\"}");+ return false;+ }+ uint64_t id = std::stoull(req->params["id"]);+ bool res_ = rateLimitManager->delete_throttle_by_id(id);+ if(!res_) {+ res->set_400("{\"message\": \"Invalid ID\"}");+ return false;+ }+ nlohmann::json res_json;+ res_json["id"] = id;+ res->set_200(res_json.dump());+ return true;+}++bool get_limit_exceed_counts(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {+ RateLimitManager* rateLimitManager = RateLimitManager::getInstance();+ res->set_200(rateLimitManager->get_exceeded_entities_json().dump());+ return true;+}++Option<std::pair<std::string,std::string>> get_api_key_and_ip(const std::string& metadata) {+ // format <length of api_key>:<api_key><ip>+ // length of api_key is a uint32_t+ if(metadata.size() < 10) {+ return Option<std::pair<std::string,std::string>>(400, "Invalid metadata");+ }++ if(metadata.find(":") == std::string::npos) {+ return Option<std::pair<std::string,std::string>>(400, "Invalid metadata");+ }++ if(!StringUtils::is_uint32_t(metadata.substr(0, metadata.find(":")))) {+ return Option<std::pair<std::string,std::string>>(400, "Invalid metadata");+ }++ if(metadata.size() < std::stoul(metadata.substr(0, metadata.find(":"))) + metadata.find(":") + 7) {+ return Option<std::pair<std::string,std::string>>(400, "Invalid metadata");+ }++ uint32_t api_key_length = static_cast<uint32_t>(std::stoul(metadata.substr(0, metadata.find(":"))));++ std::string api_key = metadata.substr(metadata.find(":") + 1, api_key_length);+ std::string ip = metadata.substr(metadata.find(":") + 1 + api_key_length);++ // validate IP address+ std::regex ip_pattern("\\b(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\b");+ if(!std::regex_match(ip, ip_pattern)) {+ return Option<std::pair<std::string,std::string>>(400, "Invalid metadata");+ }++ return Option<std::pair<std::string,std::string>>(std::make_pair(api_key, ip));}\ No newline at end of filediff --git a/src/http_server.cpp b/src/http_server.cppindex a1a6bdca..b6cabeb9 100644--- a/src/http_server.cpp+++ b/src/http_server.cpp@@ -456,7 +456,7 @@ int HttpServer::catch_all_handler(h2o_handler_t *_h2o_handler, h2o_req_t *req) {std::vector<nlohmann::json> embedded_params_vec;- if(RateLimitManager::getInstance()->is_rate_limited({{RateLimitedEntityType::api_key, api_auth_key_sent}, {RateLimitedEntityType::ip, client_ip}})) {+ if(RateLimitManager::getInstance()->is_rate_limited({RateLimitedEntityType::api_key, api_auth_key_sent}, {RateLimitedEntityType::ip, client_ip})) {std::string message = "{ \"message\": \"Rate limit exceeded or blocked\"}";return send_response(req, 429, message);}@@ -495,6 +495,12 @@ int HttpServer::catch_all_handler(h2o_handler_t *_h2o_handler, h2o_req_t *req) {//LOG(INFO) << "Init res: " << custom_gen->response << ", ref count: " << custom_gen->response.use_count();+ if(root_resource == "multi_search") {+ // format is <length of api_auth_key_sent>:<api_auth_key_sent><client_ip>+ std::string multi_search_key = std::to_string(api_auth_key_sent.length()) + ":" + api_auth_key_sent + client_ip;+ request->metadata = multi_search_key;+ }+// routes match and is an authenticated request// do any additional pre-request middleware operations hereif(rpath->action == "keys:create") {diff --git a/src/main/typesense_server.cpp b/src/main/typesense_server.cppindex b32057ed..cb632f2a 100644--- a/src/main/typesense_server.cpp+++ b/src/main/typesense_server.cpp@@ -79,11 +79,14 @@ void master_server_routes() {server->post("/operations/db/compact", post_compact_db, false, false);server->get("/limits", get_rate_limits);+ server->get("/limits/active", get_active_throttles);+ server->get("/limits/exceeds", get_limit_exceed_counts);server->get("/limits/:id", get_rate_limit);server->post("/limits", post_rate_limit);server->put("/limits/:id", put_rate_limit);server->del("/limits/:id", del_rate_limit);-+ server->del("/limits/active/:id", del_throttle);+ server->del("/limits/exceeds/:id", del_exceed);server->post("/config", post_config, false, false);}diff --git a/src/ratelimit_manager.cpp b/src/ratelimit_manager.cppindex 01634d30..8b5062cb 100644--- a/src/ratelimit_manager.cpp+++ b/src/ratelimit_manager.cpp@@ -4,121 +4,136 @@#include <iterator>RateLimitManager * RateLimitManager::getInstance() {- if (!instance) {+ if(!instance) {instance = new RateLimitManager();}-return instance;}--bool RateLimitManager::remove_rule_entity(const RateLimitedEntityType entity_type, const std::string &entity) {+void RateLimitManager::temp_ban_entity(const rate_limit_entity_t& entity, const uint64_t number_of_hours) {// lock mutexstd::unique_lock<std::shared_mutex>lock(rate_limit_mutex);- // Check if a rule exists for the given IP- if (rate_limit_entities.count(rate_limit_entity_t{entity_type,entity}) == 0) {- return false;- }- // Remove the rule from the rule store- rule_store.erase(rate_limit_entities.at(rate_limit_entity_t{entity_type,entity})->id);- // Remove the rule from the entity rate limits map- rate_limit_entities.erase(rate_limit_entity_t{entity_type,entity});-- return true;-+ temp_ban_entity_wrapped(entity, number_of_hours);}-void RateLimitManager::temp_ban_entity(const rate_limit_entity_t& entity, const int64_t number_of_days) {+bool RateLimitManager::is_rate_limited(const rate_limit_entity_t& api_key_entity, const rate_limit_entity_t& ip_entity) {// lock mutexstd::unique_lock<std::shared_mutex>lock(rate_limit_mutex);- temp_ban_entity_wrapped(entity, number_of_days);-}+ std::vector<rate_limit_rule_t*> rules_bucket;-bool RateLimitManager::is_rate_limited(const std::vector<rate_limit_entity_t> &entities) {- // lock mutex- std::unique_lock<std::shared_mutex>lock(rate_limit_mutex);- // Check if any of the entities has rule- for(const auto &entity : entities) {+ // get wildcard rules+ fill_bucket(WILDCARD_IP, api_key_entity, rules_bucket);+ fill_bucket(WILDCARD_API_KEY, ip_entity, rules_bucket);- auto entity_rule = rate_limit_entities.find(entity);- auto wildcard_rule = rate_limit_entities.find(rate_limit_entity_t{entity.entity_type, ".*"});+ // get rules for the IP entity+ fill_bucket(ip_entity, api_key_entity, rules_bucket);- if (entity_rule == rate_limit_entities.end() && wildcard_rule == rate_limit_entities.end()) {- continue;- }- const auto& rule = entity_rule != rate_limit_entities.end() ? *(entity_rule->second) : *(wildcard_rule->second);- if (rule.action == RateLimitAction::block) {+ // get rules for the API key entity+ fill_bucket(api_key_entity, ip_entity, rules_bucket);++ if(rules_bucket.empty()) {+ return false;+ }++ // sort rules_bucket by priority in ascending order+ std::sort(rules_bucket.begin(), rules_bucket.end(), [](rate_limit_rule_t* rule1, rate_limit_rule_t* rule2) {+ return rule1->priority < rule2->priority;+ });++ // get the rule with the highest priority+ auto& rule = *rules_bucket.front();+ // get key for throttling if exists+ auto throttle_key = get_throttle_key(ip_entity, api_key_entity);++ if(rule.action == RateLimitAction::block) {+ return true;+ }+ else if(rule.action == RateLimitAction::allow) {+ return false;+ }++ // check if any throttle exists and still valid+ while(throttle_key.ok()) {+ auto key = throttle_key.get();+ // Check ifban duration is not over+ if(throttled_entities.at(key).throttling_to > get_current_time()) {return true;}- else if(rule.action == RateLimitAction::allow) {- return false;+ // Remove ban from DB store+ std::string ban_key = std::string(BANS_PREFIX) + "_" + std::to_string(throttled_entities.at(key).status_id);+ store->remove(ban_key);+ // Remove ban+ throttled_entities.erase(key);+ rate_limit_exceeds.erase(key);+ // Reset request counts+ auto& request_counts = rate_limit_request_counts.lookup(key);+ request_counts.reset();+ // Get next throttle key if exists+ throttle_key = get_throttle_key(ip_entity, api_key_entity);+ }++ // get request counter key according to rule type+ auto request_counter_key = get_request_counter_key(rule, ip_entity, api_key_entity);++ if(!rate_limit_request_counts.contains(request_counter_key)){+ rate_limit_request_counts.insert(request_counter_key, request_counter_t{});+ }+ auto& request_counts = rate_limit_request_counts.lookup(request_counter_key);+ // Check iflast reset time was more than 1 minute ago+ if(request_counts.last_reset_time_minute <= get_current_time() - 60) {+ request_counts.previous_requests_count_minute = request_counts.current_requests_count_minute;+ request_counts.current_requests_count_minute = 0;+ if(request_counts.last_reset_time_minute <= get_current_time() - 120) {+ request_counts.previous_requests_count_minute = 0;}- else if(throttled_entities.count(entity) > 0) {- // Check if ban duration is not over- if (throttled_entities.at(entity).throttling_to > get_current_time()) {- return true;- }- // Remove ban from DB store- std::string ban_key = std::string(BANS_PREFIX) + "_" + std::to_string(throttled_entities.at(entity).status_id);- store->remove(ban_key);- // Remove ban- throttled_entities.erase(entity);- // Reset request counts- auto& request_counts = rate_limit_request_counts.lookup(entity);- request_counts.reset();- // Check if throttle rule still exists- if (rule.action == RateLimitAction::throttle) {- // Increase because of this request- request_counts.current_requests_count_minute++;- request_counts.current_requests_count_hour++;- }- continue;+ request_counts.last_reset_time_minute = get_current_time();+ }+ // Check iflast reset time was more than 1 hour ago+ if(request_counts.last_reset_time_hour <= get_current_time() - 3600) {+ request_counts.previous_requests_count_hour = request_counts.current_requests_count_hour;+ request_counts.current_requests_count_hour = 0;+ if(request_counts.last_reset_time_hour <= get_current_time() - 7200) {+ request_counts.previous_requests_count_hour = 0;}- else {- if(!rate_limit_request_counts.contains(entity)){- rate_limit_request_counts.insert(entity, request_counter_t{});- }- auto& request_counts = rate_limit_request_counts.lookup(entity);- // Check if last reset time was more than 1 minute ago- if (request_counts.last_reset_time_minute <= get_current_time() - 60) {- request_counts.previous_requests_count_minute = request_counts.current_requests_count_minute;- request_counts.current_requests_count_minute = 0;- if(request_counts.last_reset_time_minute <= get_current_time() - 120) {- request_counts.previous_requests_count_minute = 0;- }- request_counts.last_reset_time_minute = get_current_time();- }- // Check if last reset time was more than 1 hour ago- if (request_counts.last_reset_time_hour <= get_current_time() - 3600) {- request_counts.previous_requests_count_hour = request_counts.current_requests_count_hour;- request_counts.current_requests_count_hour = 0;- if(request_counts.last_reset_time_hour <= get_current_time() - 7200) {- request_counts.previous_requests_count_hour = 0;- }- request_counts.last_reset_time_hour = get_current_time();- }- // Check if request count is over the limit- auto current_rate_for_minute = (60 - (get_current_time() - request_counts.last_reset_time_minute)) / 60 * request_counts.previous_requests_count_minute;- current_rate_for_minute += request_counts.current_requests_count_minute;- if(rule.max_requests.minute_threshold >= 0 && current_rate_for_minute >= rule.max_requests.minute_threshold) {- bool auto_ban_is_enabled = (rule.auto_ban_threshold_num > 0 && rule.auto_ban_num_days > 0);- if(auto_ban_is_enabled) {- request_counts.threshold_exceed_count_minute++;- if(request_counts.threshold_exceed_count_minute > rule.auto_ban_threshold_num) {- temp_ban_entity_wrapped(entity, rule.auto_ban_num_days);- }- }- return true;- }- auto current_rate_for_hour = (3600 - (get_current_time() - request_counts.last_reset_time_hour)) / 3600 * request_counts.previous_requests_count_hour;- current_rate_for_hour += request_counts.current_requests_count_hour;- if(rule.max_requests.hour_threshold >= 0 && current_rate_for_hour >= rule.max_requests.hour_threshold) {- return true;+ request_counts.last_reset_time_hour = get_current_time();+ }+ // Check if request count is over the limit+ auto current_rate_for_minute = (60 - (get_current_time() - request_counts.last_reset_time_minute)) / 60 * request_counts.previous_requests_count_minute;+ current_rate_for_minute += request_counts.current_requests_count_minute;+ if(rule.max_requests.minute_threshold >= 0 && current_rate_for_minute >= rule.max_requests.minute_threshold) {+ bool auto_ban_is_enabled = (rule.auto_ban_1m_threshold > 0 && rule.auto_ban_1m_duration_hours > 0);+ // If key is not in exceed map that means, it is a new exceed, not a continued exceed+ if(rate_limit_exceeds.count(request_counter_key) == 0) {+ rate_limit_exceeds.insert({request_counter_key, rate_limit_exceed_t{last_throttle_id++, request_counter_key, 1}});+ request_counts.threshold_exceed_count_minute++;+ } else {+ // else it is a continued exceed, so just increment the request count+ rate_limit_exceeds[request_counter_key].request_count++;+ }+ // If auto ban is enabled, check if threshold is exceeded+ if(auto_ban_is_enabled) {+ if(request_counts.threshold_exceed_count_minute > rule.auto_ban_1m_threshold) {+ temp_ban_entity_wrapped(request_counter_key.substr(0, request_counter_key.find("_")) == ".*" ? WILDCARD_API_KEY : api_key_entity, rule.auto_ban_1m_duration_hours, (request_counter_key.substr((request_counter_key.find("_") + 1)) == ".*" && !rule.apply_limit_per_entity) ? nullptr : &ip_entity);}- // Increment request counts- request_counts.current_requests_count_minute++;- request_counts.current_requests_count_hour++;+ }+ return true;+ }+ auto current_rate_for_hour = (3600 - (get_current_time() - request_counts.last_reset_time_hour)) / 3600 * request_counts.previous_requests_count_hour;+ current_rate_for_hour += request_counts.current_requests_count_hour;+ if(rule.max_requests.hour_threshold >= 0 && current_rate_for_hour >= rule.max_requests.hour_threshold) {+ if(rate_limit_exceeds.count(request_counter_key) == 0) {+ rate_limit_exceeds.insert({request_counter_key, rate_limit_exceed_t{last_throttle_id++, request_counter_key, 1}});+ } else {+ rate_limit_exceeds[request_counter_key].request_count++;}+ return true;+ }+ // Increment request counts+ request_counts.current_requests_count_minute++;+ request_counts.current_requests_count_hour++;+ // If key is in exceed map that means, it is no longer exceed, so remove it from the map+ if(rate_limit_exceeds.count(request_counter_key) > 0) {+ rate_limit_exceeds.erase(request_counter_key);}return false;}@@ -138,14 +153,27 @@ bool RateLimitManager::delete_rule_by_id(const uint64_t id) {if(!deleted) {return false;}- // Check if a rule exists for the given ID+ // Check ifa rule exists for the given IDif(rule_store.count(id) > 0) {- auto rule = rule_store.at(id);+ auto rule = &rule_store.at(id);+ // Remove rule from rate limit rule pointer+ for(auto& entity: rate_limit_entities) {+ for(auto it = entity.second.begin(); it != entity.second.end(); ) {+ if(*it == rule) {+ it = entity.second.erase(it);+ } else {+ ++it;+ }+ }+ }// Remove rule from rule storerule_store.erase(id);- // Remove rule from rate limit rule pointer- for(const auto &entity : rule.entity_ids) {- rate_limit_entities.erase(rate_limit_entity_t{rule.entity_type,entity});+ for(auto it = rate_limit_entities.begin(); it != rate_limit_entities.end(); ) {+ if(it->second.empty()) {+ it = rate_limit_entities.erase(it);+ } else {+ ++it;+ }}return true;}@@ -165,18 +193,25 @@ const std::vector<rate_limit_rule_t> RateLimitManager::get_all_rules() {const std::vector<rate_limit_status_t> RateLimitManager::get_banned_entities(const RateLimitedEntityType entity_type) {std::shared_lock<std::shared_mutex> lock(rate_limit_mutex);- std::vector < rate_limit_status_t > banned_entities;+ std::vector <rate_limit_status_t> banned_entities;for (auto & element: throttled_entities) {- if (element.second.entity_type == entity_type) {+ if(element.second.entity.entity_type == entity_type) {banned_entities.push_back(element.second);}++ if(element.second.and_entity.ok()) {+ if(element.second.and_entity.get().entity_type == entity_type) {+ banned_entities.push_back(element.second);+ }+ }}// Get permanent bansfor (auto & element: rule_store) {- if (element.second.action == RateLimitAction::block && element.second.entity_type == entity_type) {- for (auto & entity : element.second.entity_ids) {- banned_entities.push_back(rate_limit_status_t{last_ban_id, 0, 0, entity, entity_type});- last_ban_id++;+ if(element.second.action == RateLimitAction::block) {+ for(const auto& entity: element.second.entities) {+ if(entity.entity_type == entity_type) {+ banned_entities.push_back(rate_limit_status_t(0, 0, 0, entity));+ }}}}@@ -188,28 +223,30 @@ void RateLimitManager::clear_all() {rate_limit_request_counts.clear();rate_limit_entities.clear();throttled_entities.clear();+ rate_limit_exceeds.clear();rule_store.clear();last_rule_id = 0;last_ban_id = 0;base_timestamp = 0;}-void RateLimitManager::temp_ban_entity_wrapped(const rate_limit_entity_t& entity, const int64_t number_of_days) {- // Check if entity is already banned- if (throttled_entities.count(entity) > 0) {+void RateLimitManager::temp_ban_entity_wrapped(const rate_limit_entity_t& entity, const uint64_t number_of_hours, const rate_limit_entity_t* and_entity) {+ std::string key = entity.entity_id + "_" + (and_entity != nullptr ? and_entity->entity_id : ".*");+ // Check ifentity is already banned+ if(throttled_entities.count(key) > 0) {return;}auto now = get_current_time();// Add entity to throttled_entities for the given number of days- rate_limit_status_t status{last_ban_id, now, now + (number_of_days * 24 * 60 * 60), entity.entity_id, entity.entity_type};+ rate_limit_status_t status(last_ban_id, now, now + (number_of_hours * 60 * 60), entity, and_entity);std::string ban_key = get_ban_key(last_ban_id);store->insert(ban_key, status.to_json().dump());- throttled_entities.insert({entity, rate_limit_status_t{last_ban_id, now, now + (number_of_days * 24 * 60 * 60), entity.entity_id, entity.entity_type}});+ throttled_entities.insert({key, status});last_ban_id++;- if(rate_limit_request_counts.contains(entity)){+ if(rate_limit_request_counts.contains(key)){// Reset counters for the given entity- rate_limit_request_counts.lookup(entity).current_requests_count_minute = 0;- rate_limit_request_counts.lookup(entity).current_requests_count_hour = 0;+ rate_limit_request_counts.lookup(key).current_requests_count_minute = 0;+ rate_limit_request_counts.lookup(key).current_requests_count_hour = 0;}}@@ -223,44 +260,67 @@ const nlohmann::json RateLimitManager::get_all_rules_json() {}const nlohmann::json rate_limit_rule_t::to_json() const {- nlohmann::json rule;- rule["id"] = id;- rule["action"] = magic_enum::enum_name(action);- rule["entity_type"] = magic_enum::enum_name(entity_type);- if(entity_type == RateLimitedEntityType::ip) {- rule["ip_addresses"] = entity_ids;- } else {- rule["api_keys"] = entity_ids;- }- if(max_requests.minute_threshold >= 0) {- rule["max_requests"]["minute_threshold"] = max_requests.minute_threshold;- }- if(max_requests.hour_threshold >= 0) {- rule["max_requests"]["hour_threshold"] = max_requests.hour_threshold;- }- if(auto_ban_threshold_num >= 0) {- rule["auto_ban_threshold_num"] = auto_ban_threshold_num;- }- if(auto_ban_num_days >= 0) {- rule["auto_ban_num_days"] = auto_ban_num_days;+ nlohmann::json rule;+ nlohmann::json api_keys_json = nlohmann::json::array();+ nlohmann::json ip_addresses_json = nlohmann::json::array();+ rule["id"] = id;+ rule["action"] = magic_enum::enum_name(action);+ rule["priority"] = priority;+ for(const auto &entity : entities) {+ if(entity.entity_type == RateLimitedEntityType::api_key) {+ api_keys_json.push_back(entity.entity_id);+ } else if(entity.entity_type == RateLimitedEntityType::ip) {+ ip_addresses_json.push_back(entity.entity_id);}- return rule;+ }+ if(max_requests.minute_threshold >= 0) {+ rule["max_requests"]["minute_threshold"] = max_requests.minute_threshold;+ }+ if(max_requests.hour_threshold >= 0) {+ rule["max_requests"]["hour_threshold"] = max_requests.hour_threshold;+ }+ if(auto_ban_1m_threshold >= 0) {+ rule["auto_ban_1m_threshold"] = auto_ban_1m_threshold;+ }+ if(auto_ban_1m_duration_hours >= 0) {+ rule["auto_ban_1m_duration_hours"] = auto_ban_1m_duration_hours;+ }+ if(!api_keys_json.empty()) {+ rule["api_keys"] = api_keys_json;+ }+ if(!ip_addresses_json.empty()) {+ rule["ip_addresses"] = ip_addresses_json;+ }+ return rule;}const nlohmann::json rate_limit_status_t::to_json() const {nlohmann::json status;+ status["id"] = status_id;status["throttling_from"] = throttling_from;status["throttling_to"] = throttling_to;- status["value"] = value;- status["entity_type"] = magic_enum::enum_name(entity_type);+ status["value"] = entity.entity_id;+ status["entity_type"] = magic_enum::enum_name(entity.entity_type);+ if(and_entity.ok()) {+ auto and_entity_value = and_entity.get();+ status["and_entity"] = nlohmann::json::object();+ status["and_entity"]["value"] = and_entity_value.entity_id;+ status["and_entity"]["entity_type"] = magic_enum::enum_name(and_entity_value.entity_type);+ }return status;}void rate_limit_status_t::parse_json(const nlohmann::json &json) {+ status_id = json["id"];throttling_from = json["throttling_from"];throttling_to = json["throttling_to"];- value = json["value"];- entity_type = magic_enum::enum_cast<RateLimitedEntityType>(json["entity_type"].get<std::string>()).value();+ entity.entity_id = json["value"];+ entity.entity_type = magic_enum::enum_cast<RateLimitedEntityType>(json["entity_type"].get<std::string>()).value();+ if(json.contains("and_entity")) {+ and_entity = Option<rate_limit_entity_t>(rate_limit_entity_t{magic_enum::enum_cast<RateLimitedEntityType>(json["and_entity"]["entity_type"].get<std::string>()).value(), json["and_entity"]["value"]});+ } else {+ and_entity = Option<rate_limit_entity_t>(404, "No and_entity found");+ }}@@ -283,7 +343,9 @@ Option<nlohmann::json> RateLimitManager::add_rule(const nlohmann::json &rule_jso}store->increment(std::string(RULES_NEXT_ID), 1);// Insert rule to rule store+ lock.unlock();insert_rule(parsed_rule);+ lock.lock();nlohmann::json response;response["message"] = "Rule added successfully.";response["rule"] = parsed_rule.to_json();@@ -291,6 +353,7 @@ Option<nlohmann::json> RateLimitManager::add_rule(const nlohmann::json &rule_jso}Option<nlohmann::json> RateLimitManager::edit_rule(const uint64_t id, const nlohmann::json &rule_json) {+ std::unique_lock<std::shared_mutex> lock(rate_limit_mutex);const auto& rule_option = find_rule_by_id(id);if(!rule_option.ok()) {return Option<nlohmann::json>(rule_option.code(), rule_option.error());@@ -299,7 +362,7 @@ Option<nlohmann::json> RateLimitManager::edit_rule(const uint64_t id, const nlohif(!rule_validation_result.ok()) {return Option<nlohmann::json>(rule_validation_result.code(), rule_validation_result.error());}- auto parsed_rule_option = parse_rule(rule_json, false);+ auto parsed_rule_option = parse_rule(rule_json);if(!parsed_rule_option.ok()) {return Option<nlohmann::json>(parsed_rule_option.code(), parsed_rule_option.error());}@@ -312,11 +375,16 @@ Option<nlohmann::json> RateLimitManager::edit_rule(const uint64_t id, const nloh}auto old_rule = rule_store.at(id);// Remove rule from rate limit rule pointer- for(const auto &entity : old_rule.entity_ids) {- rate_limit_entities.erase(rate_limit_entity_t{old_rule.entity_type,entity});+ for(const auto &entity : old_rule.entities) {+ auto& vec = rate_limit_entities.at(entity);+ std::remove_if(vec.begin(), vec.end(), [&](const auto &rule) {+ return rule->id == id;+ });}// Insert new rule to rule store+ lock.unlock();insert_rule(parsed_rule);+ lock.lock();nlohmann::json response;response["message"] = "Rule updated successfully.";response["rule"] = parsed_rule.to_json();@@ -324,48 +392,54 @@ Option<nlohmann::json> RateLimitManager::edit_rule(const uint64_t id, const nloh}Option<bool> RateLimitManager::is_valid_rule(const nlohmann::json &rule_json) {- if (rule_json.count("action") == 0) {+ if(rule_json.count("action") == 0) {return Option<bool>(400, "Parameter `action` is required.");}- if (rule_json["action"] == "allow") {- if ((rule_json.count("ip_addresses") == 0 && rule_json.count("api_keys") == 0) || (rule_json.count("ip_addresses") > 0 && rule_json.count("api_keys") > 0)) {- return Option<bool>(400, "Either `ip_addresses` or `api_keys` is required.");- }+ if(rule_json.count("apply_limit_per_entity") > 0 && rule_json["apply_limit_per_entity"].is_boolean() == false) {+ return Option<bool>(400, "Parameter `apply_limit_per_entity` must be a boolean.");+ }+ if((rule_json.count("ip_addresses") == 0 && rule_json.count("api_keys") == 0)) {+ return Option<bool>(400, "Parameter `ip_addresses` or `api_keys` is required.");+ }+ if(rule_json.count("ip_addresses") > 0 && (!rule_json["ip_addresses"].is_array() || !rule_json["ip_addresses"][0].is_string())) {+ return Option<bool>(400, "Parameter `ip_addresses` must be an array of strings.");+ }+ if(rule_json.count("api_keys") > 0 && (!rule_json["api_keys"].is_array() || !rule_json["api_keys"][0].is_string())) {+ return Option<bool>(400, "Parameter `api_keys` must be an array of strings.");+ }+ if(rule_json.count("api_keys") > 0 && rule_json.count("ip_addresses") > 0 && rule_json["api_keys"].size() == 0 && rule_json["ip_addresses"].size() == 0) {+ return Option<bool>(400, "Parameter `ip_addresses` or `api_keys` must have at least one value.");+ }+ if(rule_json.count("api_keys") > 0 && rule_json.count("ip_addresses") > 0 && rule_json["api_keys"].size() > 1 && rule_json["ip_addresses"].size() > 1) {+ return Option<bool>(400, "Many to many rule is not supported.");+ }+ if(rule_json["action"].is_string() == false) {+ return Option<bool>(400, "Parameter `action` must be a string.");+ }+ if(rule_json["action"] == "allow") {return Option<bool>(true);- } else if (rule_json["action"] == "block") {- if ((rule_json.count("ip_addresses") == 0 && rule_json.count("api_keys") == 0) || (rule_json.count("ip_addresses") > 0 && rule_json.count("api_keys") > 0)) {- return Option<bool>(400, "Either `ip_addresses` or `api_keys` is required.");- }+ } else if(rule_json["action"] == "block") {return Option<bool>(true);- } else if (rule_json["action"] == "throttle") {- if (rule_json.count("max_requests_60s") == 0 && rule_json.count("max_requests_1h") == 0) {- return Option<bool>(400, "At least one of `max_requests_60s` or `max_requests_1h` is required.");+ } else if(rule_json["action"] == "throttle") {+ if(rule_json.count("max_requests_1m") == 0 && rule_json.count("max_requests_1h") == 0) {+ return Option<bool>(400, "At least one of `max_requests_1m` or `max_requests_1h` is required.");}- if (rule_json.count("max_requests_60s") > 0 && rule_json["max_requests_60s"].is_number_integer() == false) {- return Option<bool>(400, "Parameter `max_requests_60s` must be an integer.");+ if(rule_json.count("max_requests_1m") > 0 && !rule_json["max_requests_1m"].is_number_integer()) {+ return Option<bool>(400, "Parameter `max_requests_1m` must be an integer.");}- if (rule_json.count("max_requests_1h") > 0 && rule_json["max_requests_1h"].is_number_integer() == false) {+ if(rule_json.count("max_requests_1h") > 0 && !rule_json["max_requests_1h"].is_number_integer()) {return Option<bool>(400, "Parameter `max_requests_1h` must be an integer.");}- if ((rule_json.count("ip_addresses") == 0 && rule_json.count("api_keys") == 0) || (rule_json.count("ip_addresses") > 0 && rule_json.count("api_keys") > 0)) {- return Option<bool>(400, "Either `ip_addresses` or `api_keys` is required.");- }- if(rule_json.count("ip_addresses") > 0 && !rule_json["ip_addresses"].is_array() && !rule_json["ip_addresses"][0].is_string()) {- return Option<bool>(400, "Parameter `ip_addresses` must be an array of strings.");- }- if(rule_json.count("api_keys") > 0 && !rule_json["api_keys"].is_array() && !rule_json["api_keys"][0].is_string()) {- return Option<bool>(400, "Parameter `api_keys` must be an array of strings.");- }- if((rule_json.count("auto_ban_threshold_num") > 0 && rule_json.count("auto_ban_num_days") == 0) || (rule_json.count("auto_ban_threshold_num") == 0 && rule_json.count("auto_ban_num_days") > 0)) {- return Option<bool>(400, "Both `auto_ban_threshold_num` and `auto_ban_num_days` are required if either is specified.");+ if((rule_json.count("auto_ban_1m_threshold") > 0 && rule_json.count("auto_ban_1m_duration_hours") == 0) || (rule_json.count("auto_ban_1m_threshold") == 0 && rule_json.count("auto_ban_1m_duration_hours") > 0)) {+ return Option<bool>(400, "Both `auto_ban_1m_threshold` and `auto_ban_1m_duration_hours` are required ifeither is specified.");}- if(rule_json.count("auto_ban_threshold_num") > 0 && rule_json.count("auto_ban_num_days") > 0) {- if(!rule_json["auto_ban_threshold_num"].is_number_integer() || !rule_json["auto_ban_num_days"].is_number_integer()) {- return Option<bool>(400, "Parameters `auto_ban_threshold_num` and `auto_ban_num_days` must be integers.");+ if(rule_json.count("auto_ban_1m_threshold") > 0 && rule_json.count("auto_ban_1m_duration_hours") > 0) {+ if(!rule_json["auto_ban_1m_threshold"].is_number_integer() || !rule_json["auto_ban_1m_duration_hours"].is_number_integer()) {+ return Option<bool>(400, "Parameters `auto_ban_1m_threshold` and `auto_ban_1m_duration_hours` must be integers.");}- if(rule_json["auto_ban_threshold_num"].get<int>() < 0 || rule_json["auto_ban_num_days"].get<int>() < 0) {- return Option<bool>(400, "Both `auto_ban_threshold_num` and `auto_ban_num_days` must be greater than 0.");+ if(rule_json["auto_ban_1m_threshold"].get<int>() < 0 || rule_json["auto_ban_1m_duration_hours"].get<int>() < 0) {+ return Option<bool>(400, "Both `auto_ban_1m_threshold` and `auto_ban_1m_duration_hours` must be greater than 0.");}}} else {@@ -374,49 +448,44 @@ Option<bool> RateLimitManager::is_valid_rule(const nlohmann::json &rule_json) {return Option<bool>(true);}--Option<rate_limit_rule_t> RateLimitManager::parse_rule(const nlohmann::json &rule_json, bool alert_if_exists)+Option<rate_limit_rule_t> RateLimitManager::parse_rule(const nlohmann::json &rule_json){rate_limit_rule_t new_rule;new_rule.action = magic_enum::enum_cast<RateLimitAction>(rule_json["action"].get<std::string>()).value();if(rule_json.count("ip_addresses") > 0) {- new_rule.entity_type = RateLimitedEntityType::ip;for(const auto& ip: rule_json["ip_addresses"]) {- // Check if a rule exists for the entity- if (rate_limit_entities.count(rate_limit_entity_t{new_rule.entity_type, ip}) > 0 && alert_if_exists) {- return Option<rate_limit_rule_t>(400, "A rule already exists for one of the entities");- }- new_rule.entity_ids.push_back(ip);+ new_rule.entities.push_back(rate_limit_entity_t{RateLimitedEntityType::ip, ip});}}if(rule_json.count("api_keys") > 0) {- new_rule.entity_type = RateLimitedEntityType::api_key;for(const auto& api_key: rule_json["api_keys"]) {- // Check if a rule exists for the entity- if (rate_limit_entities.count(rate_limit_entity_t{new_rule.entity_type, api_key}) > 0 && alert_if_exists) {- return Option<rate_limit_rule_t>(400, "A rule already exists for one of the entities");- }- new_rule.entity_ids.push_back(api_key);+ new_rule.entities.push_back(rate_limit_entity_t{RateLimitedEntityType::api_key, api_key});}}- if(rule_json.count("max_requests_60s") > 0) {- new_rule.max_requests.minute_threshold = rule_json["max_requests_60s"];+ if(rule_json.count("max_requests_1m") > 0) {+ new_rule.max_requests.minute_threshold = rule_json["max_requests_1m"];}if(rule_json.count("max_requests_1h") > 0) {new_rule.max_requests.hour_threshold = rule_json["max_requests_1h"];}- if(rule_json.count("auto_ban_threshold_num") > 0 && rule_json.count("auto_ban_num_days") > 0) {- new_rule.auto_ban_threshold_num = rule_json["auto_ban_threshold_num"];- new_rule.auto_ban_num_days = rule_json["auto_ban_num_days"];+ if(rule_json.count("auto_ban_1m_threshold") > 0 && rule_json.count("auto_ban_1m_duration_hours") > 0) {+ new_rule.auto_ban_1m_threshold = rule_json["auto_ban_1m_threshold"];+ new_rule.auto_ban_1m_duration_hours = rule_json["auto_ban_1m_duration_hours"];+ }+ if(rule_json.count("apply_limit_per_entity") > 0) {+ new_rule.apply_limit_per_entity = rule_json["apply_limit_per_entity"].get<bool>();+ }+ if(rule_json.count("priority") > 0) {+ new_rule.priority = rule_json["priority"];}return Option<rate_limit_rule_t>(new_rule);}-void RateLimitManager::insert_rule(const rate_limit_rule_t &rule) {+ std::unique_lock<std::shared_mutex> lock(rate_limit_mutex);rule_store[rule.id] = rule;- for(const auto &entity : rule.entity_ids) {- rate_limit_entities.insert({rate_limit_entity_t{rule.entity_type,entity}, &rule_store.at(rule.id)});+ for(const auto &entity : rule.entities) {+ rate_limit_entities[entity].push_back(&rule_store[rule.id]);}}@@ -448,7 +517,9 @@ Option<bool> RateLimitManager::init(Store *store) {}auto rule = rule_option.get();rule.id = rule_json["id"];+ lock.unlock();insert_rule(rule);+ lock.lock();}// Load bans from databasestd::string last_ban_id_str;@@ -462,7 +533,6 @@ Option<bool> RateLimitManager::init(Store *store) {else {last_ban_id = 0;}-std::vector<std::string> ban_json_strs;store->scan_fill(std::string(BANS_PREFIX) + "_", std::string(BANS_PREFIX) + "`", ban_json_strs);@@ -470,7 +540,8 @@ Option<bool> RateLimitManager::init(Store *store) {nlohmann::json ban_json = nlohmann::json::parse(ban_json_str);rate_limit_status_t ban_status;ban_status.parse_json(ban_json);- throttled_entities.insert({rate_limit_entity_t{ban_status.entity_type, ban_status.value}, ban_status});+ std::string key = ban_status.entity.entity_id + "_" + (ban_status.and_entity.ok() ? ban_status.and_entity.get().entity_id : ".*");+ throttled_entities.insert({key, ban_status});}LOG(INFO) << "Loaded " << rule_store.size() << " rate limit rules.";LOG(INFO) << "Loaded " << throttled_entities.size() << " rate limit bans.";@@ -491,4 +562,147 @@ time_t RateLimitManager::get_current_time() {void RateLimitManager::_set_base_timestamp(const time_t& timestamp) {base_timestamp = timestamp;-}\ No newline at end of file+}++Option<std::string> RateLimitManager::get_throttle_key(const rate_limit_entity_t& ip_entity, const rate_limit_entity_t& api_key_entity) {+ if(throttled_entities.count(api_key_entity.entity_id + "_" + ip_entity.entity_id) > 0) {+ return Option<std::string>(api_key_entity.entity_id + "_" + ip_entity.entity_id);+ }+ else if(throttled_entities.count(api_key_entity.entity_id + "_.*") > 0) {+ return Option<std::string>(api_key_entity.entity_id + "_.*");+ }+ else if(throttled_entities.count(".*_" + ip_entity.entity_id) > 0) {+ return Option<std::string>(".*_" + ip_entity.entity_id);+ }+ else if(throttled_entities.count(".*_.*") > 0) {+ return Option<std::string>(".*_.*");+ }+ return Option<std::string>(404, "No throttle found.");+}++const std::string RateLimitManager::get_request_counter_key(const rate_limit_rule_t& rule, const rate_limit_entity_t& ip_entity, const rate_limit_entity_t& api_key_entity) {+ bool has_api_key = false, has_ip = false, has_wildcard_ip = false, has_wildcard_api_key = false;+ for(const auto& entity: rule.entities) {+ if(entity.entity_type == RateLimitedEntityType::ip) {+ has_ip = true;+ if(entity.entity_id == ".*") {+ has_wildcard_ip = true;+ }+ }+ else if(entity.entity_type == RateLimitedEntityType::api_key) {+ has_api_key = true;+ if(entity.entity_id == ".*") {+ has_wildcard_api_key = true;+ }+ }+ }+ std::string key;+ if(!has_api_key || has_wildcard_api_key) {+ key += ".*";+ } else {+ key += api_key_entity.entity_id;+ }+ key += "_";+ if((!has_ip || has_wildcard_ip) && !rule.apply_limit_per_entity) {+ key += ".*";+ } else {+ key += ip_entity.entity_id;+ }+ return key;+}++const nlohmann::json RateLimitManager::get_exceeded_entities_json() {+ std::shared_lock<std::shared_mutex> lock(rate_limit_mutex);+ nlohmann::json exceeded_entities_json = nlohmann::json::array();+ for(const auto& entity: rate_limit_exceeds) {+ exceeded_entities_json.push_back(entity.second.to_json());+ }+ return exceeded_entities_json;+}++const nlohmann::json RateLimitManager::get_throttled_entities_json() {+ std::shared_lock<std::shared_mutex> lock(rate_limit_mutex);+ nlohmann::json throttled_entities_json = nlohmann::json::array();+ for(const auto& entity: throttled_entities) {+ auto json = entity.second.to_json();+ json[json["entity_type"].get<std::string>() == "ip" ? "ip_address" : "api_key"] = json["value"];+ json.erase("entity_type");+ json.erase("value");+ if(json["and_entity"].is_object()) {+ json[json["and_entity"]["entity_type"].get<std::string>() == "ip" ? "ip_address" : "api_key"] = json["and_entity"]["value"];+ json.erase("and_entity");+ }+ if(json["api_key"] == ".*") {+ json.erase("api_key");+ }+ throttled_entities_json.push_back(json);+ }+ return throttled_entities_json;+}++bool RateLimitManager::delete_ban_by_id(const uint64_t id) {+ std::unique_lock<std::shared_mutex> lock(rate_limit_mutex);+ std::string ban_key = get_ban_key(id);+ bool deleted = store->remove(ban_key);+ if(!deleted) {+ return false;+ }+ auto ban = std::find_if(throttled_entities.begin(), throttled_entities.end(), [id](const auto& ban) {+ return ban.second.status_id == id;+ });+ if(ban != throttled_entities.end()) {+ throttled_entities.erase(ban);+ } else {+ return false;+ }+ return true;+}++void RateLimitManager::fill_bucket(const rate_limit_entity_t& target_entity, const rate_limit_entity_t& other_entity, std::vector<rate_limit_rule_t*> &rules_bucket) {+ auto it = rate_limit_entities.find(target_entity);+ if(it == rate_limit_entities.end()) {+ return;+ }+ for(const auto& rule: it->second) {+ // Skip if rule already exists in bucket+ if(std::find(rules_bucket.begin(), rules_bucket.end(), rule) != rules_bucket.end()) {+ continue;+ }+ // Add the rule only If:+ // A. it has no entity with type of other_entity+ // B. it has an entity with type of other_entity and it's value is equal to other_entity's value+ // C. it has an entity with type of other_entity and it's value is equal to ".*"+ bool has_other_entity = false;+ for(const auto& entity: rule->entities) {+ if(entity.entity_type == other_entity.entity_type) {+ has_other_entity = true;+ if(entity.entity_id == other_entity.entity_id || entity.entity_id == ".*") {+ rules_bucket.push_back(rule);+ }+ }+ }+ if(!has_other_entity) {+ rules_bucket.push_back(rule);+ }+ }+}++bool RateLimitManager::delete_throttle_by_id(const uint32_t id) {+ std::unique_lock<std::shared_mutex> lock(rate_limit_mutex);+ bool flag = false;+ decltype(rate_limit_exceeds)::iterator iterator;+ for(auto it = rate_limit_exceeds.begin(); it != rate_limit_exceeds.end(); it++) {+ if(it->second.rule_id == id) {+ flag = true;+ iterator = it;+ break;+ }+ }+ if(!flag) {+ return false;+ }+ rate_limit_request_counts.erase(iterator->first);+ rate_limit_exceeds.erase(iterator);+ return true;+}+diff --git a/src/typesense_server_utils.cpp b/src/typesense_server_utils.cppindex b24de958..57a79db2 100644--- a/src/typesense_server_utils.cpp+++ b/src/typesense_server_utils.cpp@@ -446,7 +446,7 @@ int run_server(const Config & config, const std::string & version, void (*masterconfig.get_api_key(), quit_raft_service, batch_indexer);RateLimitManager *rateLimitManager = RateLimitManager::getInstance();- auto rate_limit_manager_init = rateLimitManager->init(&store);+ auto rate_limit_manager_init = rateLimitManager->init(&meta_store);if(!rate_limit_manager_init.ok()) {LOG(INFO) << "Failed to initialize rate limit manager: " << rate_limit_manager_init.error();diff --git a/test/core_api_utils_test.cpp b/test/core_api_utils_test.cppindex 772407cd..229385db 100644--- a/test/core_api_utils_test.cpp+++ b/test/core_api_utils_test.cpp@@ -646,6 +646,47 @@ TEST_F(CoreAPIUtilsTest, ExportWithFilter) {ASSERT_EQ('}', export_state.res_body->back());}+TEST_F(CoreAPIUtilsTest, TestParseAPIKeyIPFromMetadata) {+ // format <length of api key>:<api key><ip address>+ std::string valid_metadata = "4:abcd127.0.0.1";+ std::string invalid_ip = "4:abcd127.0.0.1:1234";+ std::string invalid_api_key = "3:abcd127.0.0.1";+ std::string no_length = "abcd127.0.0.1";+ std::string no_colon = "4abcd127.0.0.1";+ std::string no_ip = "4:abcd";+ std::string only_length = "4:";+ std::string only_colon = ":";+ std::string only_ip = "127.0.0.1";++ Option<std::pair<std::string, std::string>> res = get_api_key_and_ip(valid_metadata);+ EXPECT_TRUE(res.ok());+ EXPECT_EQ("abcd", res.get().first);+ EXPECT_EQ("127.0.0.1", res.get().second);++ res = get_api_key_and_ip(invalid_ip);+ EXPECT_FALSE(res.ok());++ res = get_api_key_and_ip(invalid_api_key);+ EXPECT_FALSE(res.ok());++ res = get_api_key_and_ip(no_length);+ EXPECT_FALSE(res.ok());++ res = get_api_key_and_ip(no_colon);+ EXPECT_FALSE(res.ok());++ res = get_api_key_and_ip(no_ip);+ EXPECT_FALSE(res.ok());++ res = get_api_key_and_ip(only_length);+ EXPECT_FALSE(res.ok());++ res = get_api_key_and_ip(only_colon);+ EXPECT_FALSE(res.ok());++ res = get_api_key_and_ip(only_ip);+ EXPECT_FALSE(res.ok());+}TEST_F(CoreAPIUtilsTest, ExportIncludeExcludeFields) {nlohmann::json schema = R"({"name": "coll1",@@ -765,4 +806,4 @@ TEST_F(CoreAPIUtilsTest, ExportIncludeExcludeFieldsWithFilter) {ASSERT_EQ(1, doc["name"].count("first"));collectionManager.drop_collection("coll1");-}\ No newline at end of file+}diff --git a/test/ratelimit_test.cpp b/test/ratelimit_test.cppindex 61557d1b..ce35edfa 100644--- a/test/ratelimit_test.cpp+++ b/test/ratelimit_test.cpp@@ -3,6 +3,7 @@#include <thread>#include "ratelimit_manager.h"#include "logger.h"+#include "core_api.h"// Google test for RateLimitManagerclass RateLimitManagerTest : public ::testing::Test@@ -50,10 +51,10 @@ TEST_F(RateLimitManagerTest, TestAddRateLimitApiKey) {auto res = manager->add_rule({{"action", "throttle"},{"api_keys", nlohmann::json::array({"test"})},- {"max_requests_60s", 10},+ {"max_requests_1m", 10},{"max_requests_1h", 100},- {"auto_ban_threshold_num", 10},- {"auto_ban_num_days", 1}+ {"auto_ban_1m_threshold", 10},+ {"auto_ban_1m_duration_hours", 1}});@@ -64,43 +65,15 @@ TEST_F(RateLimitManagerTest, TestAddRateLimitIp) {auto res = manager->add_rule({{"action", "throttle"},{"ip_addresses", nlohmann::json::array({"0.0.0.1"})},- {"max_requests_60s", 10},+ {"max_requests_1m", 10},{"max_requests_1h", 100},- {"auto_ban_threshold_num", 10},- {"auto_ban_num_days", 1}+ {"auto_ban_1m_threshold", 10},+ {"auto_ban_1m_duration_hours", 1}});EXPECT_EQ(manager->get_all_rules().size(), 1);}-TEST_F(RateLimitManagerTest, TestRemoveRateLimitApiKey) {- manager->add_rule({- {"action", "throttle"},- {"api_keys", nlohmann::json::array({"test"})},- {"max_requests_60s", 10},- {"max_requests_1h", 100},- {"auto_ban_threshold_num", 10},- {"auto_ban_num_days", 1}- });- EXPECT_EQ(manager->get_all_rules().size(), 1);- manager->remove_rule_entity(RateLimitedEntityType::api_key, "test");- EXPECT_EQ(manager->get_all_rules().size(), 0);-}--TEST_F(RateLimitManagerTest, TestRemoveRateLimitIp) {- manager->add_rule({- {"action", "throttle"},- {"ip_addresses", nlohmann::json::array({"0.0.0.1"})},- {"max_requests_60s", 10},- {"max_requests_1h", 100},- {"auto_ban_threshold_num", 10},- {"auto_ban_num_days", 1}- });- EXPECT_EQ(manager->get_all_rules().size(), 1);- manager->remove_rule_entity(RateLimitedEntityType::ip, "0.0.0.1");- EXPECT_EQ(manager->get_all_rules().size(), 0);-}-TEST_F(RateLimitManagerTest, TestGetBannedIps) {manager->add_rule({{"action", "block"},@@ -113,14 +86,14 @@ TEST_F(RateLimitManagerTest, TestGetTrackedIps) {manager->add_rule({{"action", "throttle"},{"ip_addresses", nlohmann::json::array({"0.0.0.1"})},- {"max_requests_60s", 10},+ {"max_requests_1m", 10},{"max_requests_1h", 100},- {"auto_ban_threshold_num", 10},- {"auto_ban_num_days", 1}+ {"auto_ban_1m_threshold", 10},+ {"auto_ban_1m_duration_hours", 1}});- auto entities = manager->get_all_rules();- bool found = entities[0].action == RateLimitAction::throttle && entities[0].max_requests.minute_threshold == 10 && entities[0].max_requests.hour_threshold == 100;- found = found && entities[0].entity_type == RateLimitedEntityType::ip && entities[0].entity_ids[0] == "0.0.0.1";+ auto rules = manager->get_all_rules();+ bool found = rules[0].action == RateLimitAction::throttle && rules[0].max_requests.minute_threshold == 10 && rules[0].max_requests.hour_threshold == 100;+ found = found && rules[0].entities[0].entity_type == RateLimitedEntityType::ip && rules[0].entities[0].entity_id == "0.0.0.1";EXPECT_TRUE(found);}@@ -128,14 +101,14 @@ TEST_F(RateLimitManagerTest, TestGetTrackedApiKeys) {manager->add_rule({{"action", "throttle"},{"api_keys", nlohmann::json::array({"test"})},- {"max_requests_60s", 10},+ {"max_requests_1m", 10},{"max_requests_1h", 100},- {"auto_ban_threshold_num", 10},- {"auto_ban_num_days", 1}+ {"auto_ban_1m_threshold", 10},+ {"auto_ban_1m_duration_hours", 1}});- auto entities = manager->get_all_rules();- bool found = entities[0].action == RateLimitAction::throttle && entities[0].max_requests.minute_threshold == 10 && entities[0].max_requests.hour_threshold == 100;- found = found && entities[0].entity_type == RateLimitedEntityType::api_key && entities[0].entity_ids[0] == "test";+ auto rules = manager->get_all_rules();+ bool found = rules[0].action == RateLimitAction::throttle && rules[0].max_requests.minute_threshold == 10 && rules[0].max_requests.hour_threshold == 100;+ found = found && rules[0].entities[0].entity_type == RateLimitedEntityType::api_key && rules[0].entities[0].entity_id == "test";EXPECT_TRUE(found);}@@ -144,21 +117,9 @@ TEST_F(RateLimitManagerTest, TestBanIpPermanently) {{"action", "block"},{"ip_addresses", nlohmann::json::array({"0.0.0.1"})}});- auto entities = manager->get_all_rules();- bool found = entities[0].action == RateLimitAction::block && entities[0].entity_type == RateLimitedEntityType::ip && entities[0].entity_ids[0] == "0.0.0.1";- EXPECT_TRUE(found);-}--TEST_F(RateLimitManagerTest, TestUnbanIp) {- manager->add_rule({- {"action", "block"},- {"ip_addresses", nlohmann::json::array({"0.0.0.1"})}- });- auto entities = manager->get_all_rules();- bool found = entities[0].action == RateLimitAction::block && entities[0].entity_type == RateLimitedEntityType::ip && entities[0].entity_ids[0] == "0.0.0.1";+ auto rules = manager->get_all_rules();+ bool found = rules[0].action == RateLimitAction::block && rules[0].entities[0].entity_type == RateLimitedEntityType::ip && rules[0].entities[0].entity_id == "0.0.0.1";EXPECT_TRUE(found);- manager->remove_rule_entity(RateLimitedEntityType::ip, "0.0.0.1");- EXPECT_EQ(manager->get_all_rules().size(), 0);}TEST_F(RateLimitManagerTest, TestIsBannedIp) {@@ -167,8 +128,9 @@ TEST_F(RateLimitManagerTest, TestIsBannedIp) {{"ip_addresses", nlohmann::json::array({"0.0.0.1"})}});EXPECT_TRUE(manager->get_banned_entities(RateLimitedEntityType::ip).size() == 1);- auto entities = manager->get_banned_entities(RateLimitedEntityType::ip);- bool found = entities[0].entity_type == RateLimitedEntityType::ip && entities[0].value == "0.0.0.1";+ auto banned_entities = manager->get_banned_entities(RateLimitedEntityType::ip);+ bool found = banned_entities[0].entity.entity_type == RateLimitedEntityType::ip && banned_entities[0].entity.entity_id == "0.0.0.1";+ EXPECT_EQ(banned_entities[0].and_entity.ok(), false);EXPECT_TRUE(found);}@@ -176,12 +138,12 @@ TEST_F(RateLimitManagerTest, TestIsBannedIpTemp) {manager->add_rule({{"action", "throttle"},{"ip_addresses", nlohmann::json::array({"0.0.0.1"})},- {"max_requests_60s", 1},+ {"max_requests_1m", 1},{"max_requests_1h", 1}});EXPECT_TRUE(manager->get_all_rules().size() == 1);- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::ip, "0.0.0.1"}}));- EXPECT_TRUE(manager->is_rate_limited({{RateLimitedEntityType::ip, "0.0.0.1"}}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"},{RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"},{RateLimitedEntityType::ip, "0.0.0.1"}));}TEST_F(RateLimitManagerTest, TestIsBannedAPIKeyPermanently) {@@ -190,8 +152,8 @@ TEST_F(RateLimitManagerTest, TestIsBannedAPIKeyPermanently) {{"api_keys", nlohmann::json::array({"test"})}});EXPECT_TRUE(manager->get_banned_entities(RateLimitedEntityType::api_key).size() == 1);- auto entities = manager->get_banned_entities(RateLimitedEntityType::api_key);- bool found = entities[0].entity_type == RateLimitedEntityType::api_key && entities[0].value == "test";+ auto banned_entities = manager->get_banned_entities(RateLimitedEntityType::api_key);+ bool found = banned_entities[0].entity.entity_type == RateLimitedEntityType::api_key && banned_entities[0].entity.entity_id == "test";EXPECT_TRUE(found);}@@ -199,12 +161,12 @@ TEST_F(RateLimitManagerTest, TestIsBannedAPIKeyTemp) {manager->add_rule({{"action", "throttle"},{"api_keys", nlohmann::json::array({"test"})},- {"max_requests_60s", 1},+ {"max_requests_1m", 1},{"max_requests_1h", 1}});EXPECT_TRUE(manager->get_all_rules().size() == 1);- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_TRUE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"},{RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"},{RateLimitedEntityType::ip, "0.0.0.1"}));}TEST_F(RateLimitManagerTest, TestAllowAPIKey) {@@ -213,7 +175,7 @@ TEST_F(RateLimitManagerTest, TestAllowAPIKey) {{"api_keys", nlohmann::json::array({"test"})}});EXPECT_TRUE(manager->get_all_rules().size() == 1);- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test_"}}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test_"},{RateLimitedEntityType::ip, "0.0.0.1"}));}TEST_F(RateLimitManagerTest, TestAllowIp) {@@ -222,26 +184,26 @@ TEST_F(RateLimitManagerTest, TestAllowIp) {{"ip_addresses", nlohmann::json::array({"0.0.0.1"})}});EXPECT_TRUE(manager->get_all_rules().size() == 1);- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::ip, "0.0.0.1"}}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"},{RateLimitedEntityType::ip, "0.0.0.1"}));}TEST_F(RateLimitManagerTest, TestThrottleAPIKey) {manager->add_rule({{"action", "throttle"},{"api_keys", nlohmann::json::array({"test"})},- {"max_requests_60s", 1},+ {"max_requests_1m", 1},{"max_requests_1h", 1}});EXPECT_TRUE(manager->get_all_rules().size() == 1);- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_TRUE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));}TEST_F(RateLimitManagerTest, TestDeleteRuleByID) {manager->add_rule({{"action", "throttle"},{"api_keys", nlohmann::json::array({"test"})},- {"max_requests_60s", 1},+ {"max_requests_1m", 1},{"max_requests_1h", 1}});EXPECT_TRUE(manager->get_all_rules().size() == 1);@@ -254,77 +216,77 @@ TEST_F(RateLimitManagerTest, TestMinuteRateLimitAPIKey) {manager->add_rule({{"action", "throttle"},{"api_keys", nlohmann::json::array({"test"})},- {"max_requests_60s", 5},+ {"max_requests_1m", 5},{"max_requests_1h", -1}});EXPECT_TRUE(manager->get_all_rules().size() == 1);- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_TRUE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));}TEST_F(RateLimitManagerTest, TestHourRateLimitAPIKey) {manager->add_rule({{"action", "throttle"},{"api_keys", nlohmann::json::array({"test"})},- {"max_requests_60s", -1},+ {"max_requests_1m", -1},{"max_requests_1h", 5}});EXPECT_TRUE(manager->get_all_rules().size() == 1);- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_TRUE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));}TEST_F(RateLimitManagerTest, TestMinuteRateLimitIp) {manager->add_rule({{"action", "throttle"},{"ip_addresses", nlohmann::json::array({"0.0.0.1"})},- {"max_requests_60s", 5},+ {"max_requests_1m", 5},{"max_requests_1h", -1}});EXPECT_TRUE(manager->get_all_rules().size() == 1);- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::ip, "0.0.0.1"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::ip, "0.0.0.1"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::ip, "0.0.0.1"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::ip, "0.0.0.1"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::ip, "0.0.0.1"}}));- EXPECT_TRUE(manager->is_rate_limited({{RateLimitedEntityType::ip, "0.0.0.1"}}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));}TEST_F(RateLimitManagerTest, TestHourRateLimitIp) {manager->add_rule({{"action", "throttle"},{"ip_addresses", nlohmann::json::array({"0.0.0.1"})},- {"max_requests_60s", -1},+ {"max_requests_1m", -1},{"max_requests_1h", 5}});EXPECT_TRUE(manager->get_all_rules().size() == 1);- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::ip, "0.0.0.1"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::ip, "0.0.0.1"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::ip, "0.0.0.1"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::ip, "0.0.0.1"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::ip, "0.0.0.1"}}));- EXPECT_TRUE(manager->is_rate_limited({{RateLimitedEntityType::ip, "0.0.0.1"}}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));}TEST_F(RateLimitManagerTest, TestGetAllRules) {manager->add_rule({{"action", "throttle"},{"ip_addresses", nlohmann::json::array({"0.0.0.1"})},- {"max_requests_60s", -1},+ {"max_requests_1m", -1},{"max_requests_1h", 5}});manager->add_rule({{"action", "throttle"},{"api_keys", nlohmann::json::array({"test"})},- {"max_requests_60s", 5},+ {"max_requests_1m", 5},{"max_requests_1h", -1}});EXPECT_TRUE(manager->get_all_rules().size() == 2);@@ -339,7 +301,7 @@ TEST_F(RateLimitManagerTest, TestGetAllRulesJSON) {manager->add_rule({{"action", "throttle"},{"api_keys", nlohmann::json::array({"test"})},- {"max_requests_60s", 5},+ {"max_requests_1m", 5},{"max_requests_1h", -1}});nlohmann::json rules = manager->get_all_rules_json();@@ -347,93 +309,511 @@ TEST_F(RateLimitManagerTest, TestGetAllRulesJSON) {EXPECT_EQ(rules.size(), 1);EXPECT_EQ(rules.at(0).is_object(), true);EXPECT_EQ(rules.at(0).at("id").is_number(), true);- EXPECT_EQ(rules.at(0).at("entity_type").is_string(), true);EXPECT_EQ(rules.at(0).at("api_keys").is_array(), true);+ EXPECT_EQ(rules.at(0).at("api_keys").size(), 1);+ EXPECT_EQ(rules.at(0).at("api_keys").at(0).is_string(), true);+ EXPECT_EQ(rules.at(0).count("ip_addresses"), 0);}TEST_F(RateLimitManagerTest, TestAutoBan) {manager->add_rule({{"action", "throttle"},{"api_keys", nlohmann::json::array({"test"})},- {"max_requests_60s", 5},+ {"max_requests_1m", 5},{"max_requests_1h", -1},- {"auto_ban_threshold_num", 2},- {"auto_ban_num_days", 1}- });- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_TRUE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));+ {"auto_ban_1m_threshold", 2},+ {"auto_ban_1m_duration_hours", 1}+ });+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));this->changeBaseTimestamp(120);- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_TRUE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- this->changeBaseTimestamp(60*60*24);- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ this->changeBaseTimestamp(240);+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ this->changeBaseTimestamp(60*59);+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ this->changeBaseTimestamp(60*60*2);+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));}-TEST_F(RateLimitManagerTest, TestWildcard) {+TEST_F(RateLimitManagerTest, TestWildcardAPIKeyWithFlag) {manager->add_rule({{"action", "throttle"},{"api_keys", nlohmann::json::array({".*"})},- {"max_requests_60s", 5},- {"max_requests_1h", -1}+ {"max_requests_1m", 5},+ {"max_requests_1h", -1},+ {"apply_limit_per_entity", true}});- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_TRUE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test1"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test1"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test1"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test1"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test1"}}));- EXPECT_TRUE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test1"}}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.2"}));}-TEST_F(RateLimitManagerTest, TestCorrectOrderofRules) {+TEST_F(RateLimitManagerTest, TestWildcardAPIKeyWithoutFlag) {manager->add_rule({{"action", "throttle"},{"api_keys", nlohmann::json::array({".*"})},- {"max_requests_60s", 2},- {"max_requests_1h", -1}+ {"max_requests_1m", 5},+ {"max_requests_1h", -1},+ });+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+}++TEST_F(RateLimitManagerTest, TestPriority) {+ manager->add_rule({+ {"action", "throttle"},+ {"api_keys", nlohmann::json::array({".*"})},+ {"max_requests_1m", 2},+ {"max_requests_1h", -1},+ {"priority", 3},+ {"apply_limit_per_entity", true}});manager->add_rule({{"action", "throttle"},{"api_keys", nlohmann::json::array({"test"})},- {"max_requests_60s", 5},- {"max_requests_1h", -1}+ {"max_requests_1m", 5},+ {"max_requests_1h", -1},+ {"priority", 1}});manager->add_rule({{"action", "block"},{"api_keys", nlohmann::json::array({"test1"})},+ {"priority", 4}});manager->add_rule({{"action", "allow"},{"api_keys", nlohmann::json::array({"test2"})},+ {"priority", 0}+ });++ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));++ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.2"}));++ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test2"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test2"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test2"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+}++TEST_F(RateLimitManagerTest, TestAndRule) {+ manager->add_rule({+ {"action", "throttle"},+ {"api_keys", nlohmann::json::array({"test"})},+ {"ip_addresses", nlohmann::json::array({"0.0.0.1"})},+ {"max_requests_1m", 5},+ {"max_requests_1h", -1},+ {"priority", 3}+ });++ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));++ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.1"}));++ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+}+++TEST_F(RateLimitManagerTest, TestExceedCounter) {+ manager->add_rule({+ {"action", "throttle"},+ {"api_keys", nlohmann::json::array({".*"})},+ {"priority", 3},+ {"apply_limit_per_entity", true},+ {"max_requests_1m", 3},+ {"max_requests_1h", -1}+ });++ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));++ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));++ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.2"}));++ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+++ const auto exceeds = manager->get_exceeded_entities_json();+ EXPECT_EQ(exceeds.size(), 2);++ EXPECT_EQ(exceeds[0]["api_key"], ".*");+ EXPECT_EQ(exceeds[0]["ip"], "0.0.0.2");+ EXPECT_EQ(exceeds[0]["request_count"], 10);++ EXPECT_EQ(exceeds[1]["api_key"], ".*");+ EXPECT_EQ(exceeds[1]["ip"], "0.0.0.1");+ EXPECT_EQ(exceeds[1]["request_count"], 9);+}++TEST_F(RateLimitManagerTest, TestActiveThrottles) {+ manager->add_rule({+ {"action", "throttle"},+ {"ip_addresses", nlohmann::json::array({"0.0.0.1"})},+ {"priority", 3},+ {"max_requests_1m", 3},+ {"max_requests_1h", -1},+ {"auto_ban_1m_threshold", 1},+ {"auto_ban_1m_duration_hours", 1}+ });++ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ manager->_set_base_timestamp(120);+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));++ const auto throttles = manager->get_throttled_entities_json();+ EXPECT_EQ(throttles.size(), 1);+ EXPECT_EQ(throttles[0]["ip_address"], "0.0.0.1");+ EXPECT_EQ(throttles[0].count("api_key"), 0);+ EXPECT_EQ(throttles[0].count("throttling_from"), 1);+ EXPECT_EQ(throttles[0].count("throttling_to"), 1);+}++TEST_F(RateLimitManagerTest, TestMultiSearchRateLimiting) {+ manager->add_rule({+ {"action", "throttle"},+ {"ip_addresses", nlohmann::json::array({".*"})},+ {"priority", 3},+ {"max_requests_1m", 3},+ {"max_requests_1h", -1},+ {"apply_limit_per_entity", true}+ });++ std::shared_ptr<http_req> req = std::make_shared<http_req>();+ std::shared_ptr<http_res> res = std::make_shared<http_res>(nullptr);+ nlohmann::json body;+ body["searches"] = nlohmann::json::array();+ nlohmann::json search;+ search["collection"] = "players";+ search["filter_by"] = "score: > 100";+ body["searches"].push_back(search);+ body["searches"].push_back(search);+ body["searches"].push_back(search);+ body["searches"].push_back(search);+ body["searches"].push_back(search);++ req->embedded_params_vec.push_back(nlohmann::json::object());+ req->embedded_params_vec.push_back(nlohmann::json::object());+ req->embedded_params_vec.push_back(nlohmann::json::object());+ req->embedded_params_vec.push_back(nlohmann::json::object());+ req->embedded_params_vec.push_back(nlohmann::json::object());+ req->body = body.dump();+ req->metadata = "4:test0.0.0.1";++ EXPECT_FALSE(post_multi_search(req, res));+ EXPECT_EQ(res->status_code, 429);+ EXPECT_EQ(res->body, "{\"message\": \"Rate limit exceeded or blocked\"}");++ body.erase("searches");+ body["searches"] = nlohmann::json::array();+ body["searches"].push_back(search);+ body["searches"].push_back(search);++ req->embedded_params_vec.pop_back();+ req->embedded_params_vec.pop_back();+ req->embedded_params_vec.pop_back();++ req->body = body.dump();+ req->metadata = "4:test0.0.0.2";++ EXPECT_TRUE(post_multi_search(req, res));+ EXPECT_EQ(res->status_code, 200);+}++TEST_F(RateLimitManagerTest, TestDeleteBanByID) {+ manager->add_rule({+ {"action", "throttle"},+ {"ip_addresses", nlohmann::json::array({".*"})},+ {"priority", 3},+ {"max_requests_1m", 3},+ {"max_requests_1h", -1},+ {"apply_limit_per_entity", true},+ {"auto_ban_1m_threshold", 1},+ {"auto_ban_1m_duration_hours", 1}+ });++ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ manager->_set_base_timestamp(120);+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));++ const auto throttles = manager->get_throttled_entities_json();+ EXPECT_EQ(throttles.size(), 1);+ EXPECT_EQ(throttles[0]["ip_address"], "0.0.0.1");+ EXPECT_EQ(throttles[0].count("api_key"), 0);+ EXPECT_EQ(throttles[0].count("throttling_from"), 1);+ EXPECT_EQ(throttles[0].count("throttling_to"), 1);++ EXPECT_TRUE(manager->delete_ban_by_id(throttles[0]["id"]));+ EXPECT_EQ(manager->get_throttled_entities_json().size(), 0);+}+++TEST_F(RateLimitManagerTest, TestInvalidRules) {+ auto res = manager->add_rule({+ {"action", "throttle"},+ {"ip_addresses", nlohmann::json::array({"0.0.0.1", "0.0.0.2"})},+ {"api_keys", nlohmann::json::array({"test1", "test2"})},+ {"priority", 3},+ {"max_requests_1m", 3},+ {"max_requests_1h", -1},+ {"apply_limit_per_entity", true},+ {"auto_ban_1m_threshold", 1},+ {"auto_ban_1m_duration_hours", 1}+ });++ EXPECT_FALSE(res.ok());+ EXPECT_EQ(400, res.code());+ EXPECT_EQ("Many to many rule is not supported.", res.error());++ res = manager->add_rule({+ {"action", "throttle"},+ {"priority", 3},+ {"max_requests_1m", 3},+ {"max_requests_1h", -1},+ {"apply_limit_per_entity", true},+ {"auto_ban_1m_threshold", 1},+ {"auto_ban_1m_duration_hours", 1}+ });++ EXPECT_FALSE(res.ok());+ EXPECT_EQ(400, res.code());+ EXPECT_EQ("Parameter `ip_addresses` or `api_keys` is required.", res.error());+++ res = manager->add_rule({+ {"ip_addresses", nlohmann::json::array({".*"})},+ {"priority", 3},+ {"max_requests_1m", 3},+ {"max_requests_1h", -1},+ {"apply_limit_per_entity", true},+ {"auto_ban_1m_threshold", 1},+ {"auto_ban_1m_duration_hours", 1}+ });++ EXPECT_FALSE(res.ok());+ EXPECT_EQ(400, res.code());+ EXPECT_EQ("Parameter `action` is required.", res.error());++ res = manager->add_rule({+ {"action", "throttle"},+ {"ip_addresses", ".*"},+ {"priority", 3},+ {"max_requests_1m", 3},+ {"max_requests_1h", -1},+ {"apply_limit_per_entity", true},+ {"auto_ban_1m_threshold", 1},+ {"auto_ban_1m_duration_hours", 1}+ });++ EXPECT_FALSE(res.ok());+ EXPECT_EQ(400, res.code());+ EXPECT_EQ("Parameter `ip_addresses` must be an array of strings.", res.error());++ res = manager->add_rule({+ {"action", "throttle"},+ {"ip_addresses", nlohmann::json::array({".*"})},+ {"priority", 3}+ });++ EXPECT_FALSE(res.ok());+ EXPECT_EQ(400, res.code());+ EXPECT_EQ("At least one of `max_requests_1m` or `max_requests_1h` is required.", res.error());++ res = manager->add_rule({+ {"action", "throttle"},+ {"ip_addresses", nlohmann::json::array({".*"})},+ {"priority", 3},+ {"max_requests_1m", "aa"}+ });++ EXPECT_FALSE(res.ok());+ EXPECT_EQ(400, res.code());+ EXPECT_EQ("Parameter `max_requests_1m` must be an integer.", res.error());++ res = manager->add_rule({+ {"action", "invalid"},+ {"ip_addresses", nlohmann::json::array({".*"})},+ {"priority", 3},+ {"max_requests_1m", 3},+ {"max_requests_1h", 3},+ {"apply_limit_per_entity", true},+ {"auto_ban_1m_threshold", 1},+ {"auto_ban_1m_duration_hours", 1}+ });++ EXPECT_FALSE(res.ok());+ EXPECT_EQ(400, res.code());+ EXPECT_EQ("Invalid action.", res.error());+}++TEST_F(RateLimitManagerTest, TestOneToManyRule) {+ manager->add_rule({+ {"action", "throttle"},+ {"ip_addresses", nlohmann::json::array({"0.0.0.1", "0.0.0.2"})},+ {"api_keys", nlohmann::json::array({"test"})},+ {"priority", 3},+ {"max_requests_1m", 2},+ {"max_requests_1h", -1},+ {"apply_limit_per_entity", true},+ {"auto_ban_1m_threshold", 1},+ {"auto_ban_1m_duration_hours", 1}+ });++ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.1"}));++ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));++ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.2"}));++ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.2"}));+}++TEST_F(RateLimitManagerTest, TestDeleteThrottleByID) {+ manager->add_rule({+ {"action", "throttle"},+ {"ip_addresses", nlohmann::json::array({".*"})},+ {"priority", 3},+ {"max_requests_1m", 3},+ {"max_requests_1h", -1},+ {"apply_limit_per_entity", true}});- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test3"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test3"}}));- EXPECT_TRUE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test3"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_TRUE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test"}}));- EXPECT_TRUE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test1"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test2"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test2"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test2"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test2"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test2"}}));- EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test2"}}));-}\ No newline at end of file++ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));++ auto exceeds = manager->get_exceeded_entities_json();+ EXPECT_EQ(1, exceeds.size());+ auto id = exceeds[0]["id"];+ auto res = manager->delete_throttle_by_id(id);+ EXPECT_TRUE(res);+ exceeds = manager->get_exceeded_entities_json();+ EXPECT_EQ(0, exceeds.size());+ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test"}, {RateLimitedEntityType::ip, "0.0.0.1"}));++}++TEST_F(RateLimitManagerTest, TestOneToManyFillTest) {+ manager->add_rule({+ {"action", "block"},+ {"ip_addresses", nlohmann::json::array({"0.0.0.1"})},+ {"api_keys", nlohmann::json::array({"test", "test1", "test2"})},+ {"priority", 3},+ });++ EXPECT_TRUE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.1"}));++ auto rules = manager->get_all_rules_json();+ EXPECT_EQ(1, rules.size());+ manager->delete_rule_by_id(rules[0]["id"]);+ EXPECT_EQ(0, manager->get_all_rules_json().size());++ manager->add_rule({+ {"action", "block"},+ {"ip_addresses", nlohmann::json::array({"0.0.0.1"})},+ {"api_keys", nlohmann::json::array({"test", "test2"})},+ {"priority", 3},+ });++ LOG(INFO) << manager->get_all_rules_json();++ EXPECT_FALSE(manager->is_rate_limited({RateLimitedEntityType::api_key, "test1"}, {RateLimitedEntityType::ip, "0.0.0.1"}));+}