Skip to content

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

Added rate limiting feature (#709)

Ozan Armağan3280665cc917
diff --git a/include/app_metrics.h b/include/app_metrics.h
index 8606dab4..a84fe3fc 100644
--- a/include/app_metrics.h
+++ b/include/app_metrics.h
@@ -6,6 +6,7 @@
#include "config.h"
#include <string>
#include <shared_mutex>
+#include <mutex>
#include <fstream>
class AppMetrics {
@@ -79,4 +80,4 @@ public:
void window_reset();
void get(const std::string& rps_key, const std::string& latency_key, nlohmann::json &result) const;
-};
\ No newline at end of file
+};
diff --git a/include/core_api.h b/include/core_api.h
index 6888ff8f..839b8934 100644
--- a/include/core_api.h
+++ b/include/core_api.h
@@ -2,6 +2,7 @@
#include "http_server.h"
#include "auth_manager.h"
+#include "ratelimit_manager.h"
bool handle_authentication(std::map<std::string, std::string>& req_params,
std::vector<nlohmann::json>& embedded_params_vec,
@@ -114,6 +115,18 @@ bool post_config(const std::shared_ptr<http_req>& req, const std::shared_ptr<htt
bool post_clear_cache(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);
+// Rate Limiting
+
+bool get_rate_limits(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);
+
+bool get_rate_limit(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);
+
+bool put_rate_limit(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);
+
+bool del_rate_limit(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);
+
+bool post_rate_limit(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);
+
// Misc helpers
void get_collections_for_auth(std::map<std::string, std::string>& req_params, const std::string& body,
diff --git a/include/ratelimit_manager.h b/include/ratelimit_manager.h
new file mode 100644
index 00000000..90f85eae
--- /dev/null
+++ b/include/ratelimit_manager.h
@@ -0,0 +1,237 @@
+#pragma once
+
+#include <string>
+#include <vector>
+#include <tuple>
+#include <unordered_map>
+#include <mutex>
+#include <shared_mutex>
+#include <json.hpp>
+#include <magic_enum.hpp>
+#include "lru/lru.hpp"
+#include "option.h"
+#include "store.h"
+
+
+
+
+// Action enum for rate limit rules
+enum class RateLimitAction {
+ allow,
+ block,
+ throttle
+};
+
+enum class RateLimitedEntityType {
+ ip,
+ api_key
+};
+
+struct 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 key
+struct rate_limit_entity_t {
+ RateLimitedEntityType entity_type;
+ std::string entity_id;
+
+ // Equality operator for rate_limit_entity_t
+ bool operator==(const rate_limit_entity_t& other) const {
+ return std::tie(entity_type, entity_id) == std::tie(other.entity_type, other.entity_id);
+ }
+};
+
+// Request counter struct for ip addresses to keep track of requests for current and previous sampling period
+struct request_counter_t {
+ int64_t current_requests_count_minute = 0;
+ int64_t current_requests_count_hour = 0;
+ int64_t previous_requests_count_minute = 0;
+ int64_t previous_requests_count_hour = 0;
+ int64_t threshold_exceed_count_minute = 0;
+ time_t last_reset_time_minute = 0;
+ time_t last_reset_time_hour = 0;
+
+ void reset() {
+ current_requests_count_minute = 0;
+ current_requests_count_hour = 0;
+ threshold_exceed_count_minute = 0;
+ previous_requests_count_minute = 0;
+ previous_requests_count_hour = 0;
+ last_reset_time_minute = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
+ last_reset_time_hour = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
+ }
+
+
+ // not-equal operator overload
+ bool operator!=(const request_counter_t& other) const{
+ return std::tie(current_requests_count_minute, current_requests_count_hour, previous_requests_count_minute, previous_requests_count_hour, last_reset_time_minute, last_reset_time_hour) !=
+ std::tie(other.current_requests_count_minute, other.current_requests_count_hour, other.previous_requests_count_minute, other.previous_requests_count_hour, other.last_reset_time_minute, other.last_reset_time_hour);
+ }
+};
+
+// Struct to store ban information for ip addresses
+struct rate_limit_status_t {
+ uint32_t status_id;
+ int64_t throttling_from;
+ int64_t throttling_to;
+ std::string value;
+ RateLimitedEntityType entity_type;
+
+
+ const nlohmann::json to_json() const;
+
+ void parse_json(const nlohmann::json& json);
+};
+
+
+// Hash function for rate_limit_entity_t
+namespace std {
+ template <>
+ struct hash<rate_limit_entity_t> {
+ std::size_t operator()(const rate_limit_entity_t& k) const{
+ return ((std::hash<int>()(static_cast<int>(k.entity_type)) ^ (std::hash<std::string>()(k.entity_id) << 1)) >> 1);
+ }
+ };
+}
+
+class RateLimitManager
+{
+ public:
+
+ RateLimitManager(const RateLimitManager&) = delete;
+
+ RateLimitManager& operator=(const RateLimitManager&) = delete;
+
+ RateLimitManager(RateLimitManager&&) = delete;
+
+ RateLimitManager& operator=(RateLimitManager&&) = delete;
+
+ static RateLimitManager* getInstance();
+
+ // Remove rate limit for entity
+ bool remove_rule_entity(const RateLimitedEntityType entity_type, const std::string &entity);
+
+ // Get vector of banned entities
+ const 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);
+
+ // Add rule by JSON
+ Option<nlohmann::json> add_rule(const nlohmann::json &rule_json);
+
+ // Edit rule by JSON
+ Option<nlohmann::json> edit_rule(const uint64_t id, const nlohmann::json &rule_json);
+
+ // Find rule by ID
+ Option<nlohmann::json> find_rule_by_id(const uint64_t id);
+
+ // Delete rule by ID
+ bool delete_rule_by_id(const uint64_t id);
+
+ // Get All rules as vector
+ const std::vector<rate_limit_rule_t> get_all_rules();
+
+ // Get all rules as json
+ const nlohmann::json get_all_rules_json();
+
+ // Clear all rules
+ void clear_all();
+
+ // Internal function to set base time
+ void _set_base_timestamp(const time_t& base_time);
+
+ // Set store
+ Option<bool> init(Store* store);
+
+ private:
+
+ RateLimitManager() {
+ rate_limit_request_counts.capacity(10000);
+ }
+
+ // Store for rate limit rules
+ Store *store;
+
+ // Using a $ prefix so that these meta keys stay above record entries in a lexicographically ordered KV store
+
+ // Prefix for rate limit rules
+ static constexpr const char* RULES_NEXT_ID = "$RLN";
+ static constexpr const char* RULES_PREFIX = "$RLRP";
+
+ // Prefix for bans
+ static constexpr const char* BANS_NEXT_ID = "$RLBN";
+ static constexpr const char* BANS_PREFIX = "$RLBP";
+
+
+
+
+ // ID of latest added rule
+ inline static uint32_t last_rule_id = 0;
+
+ // ID of latest added ban
+ inline static uint32_t last_ban_id = 0;
+
+ // Store for rate_limit_rule_t
+ std::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;
+
+ // Unordered map to point rules from rule store for entities
+ std::unordered_map<rate_limit_entity_t, 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;
+
+ // Mutex to protect access to ip_rate_limits and api_key_rate_limits
+ std::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);
+ // 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);
+
+ // Helper function to check if JSON rule is valid
+ Option<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);
+
+ // Helper function to insert rule in store
+ void insert_rule(const rate_limit_rule_t &rule);
+
+ // Helper function to get rule key for DB store from ID
+ std::string get_rule_key(const uint32_t id);
+
+
+ // Helper function to get ban key for DB store from ID
+ std::string get_ban_key(const uint32_t id);
+
+ // Base timestamp
+ time_t base_timestamp = 0;
+
+ // Helper function to get current timestamp
+ time_t get_current_time();
+
+ // Singleton instance
+ inline static RateLimitManager *instance;
+
+};
+
diff --git a/include/store.h b/include/store.h
index b94420a7..a35532b7 100644
--- a/include/store.h
+++ b/include/store.h
@@ -14,6 +14,7 @@
#include <rocksdb/merge_operator.h>
#include <rocksdb/transaction_log.h>
#include <butil/file_util.h>
+#include <mutex>
#include <rocksdb/utilities/checkpoint.h>
#include <rocksdb/utilities/table_properties_collectors.h>
#include "string_utils.h"
@@ -367,4 +368,4 @@ public:
db->GetProperty("rocksdb.cur-size-all-mem-tables", &memtable_usage);
LOG(INFO) << "rocksdb.cur-size-all-mem-tables: " << memtable_usage;
}
-};
\ No newline at end of file
+};
diff --git a/src/core_api.cpp b/src/core_api.cpp
index 47dd6c68..e0ed4181 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -1,5 +1,6 @@
#include <chrono>
#include <thread>
+#include <cstdlib>
#include <app_metrics.h>
#include "typesense_server_utils.h"
#include "core_api.h"
@@ -10,6 +11,7 @@
#include "logger.h"
#include "core_api_utils.h"
#include "lru/lru.hpp"
+#include "ratelimit_manager.h"
using namespace std::chrono_literals;
@@ -1688,3 +1690,85 @@ bool del_preset(const std::shared_ptr<http_req>& req, const std::shared_ptr<http
res->set_200(res_json.dump());
return true;
}
+bool get_rate_limits(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {
+ RateLimitManager* rateLimitManager = RateLimitManager::getInstance();
+
+ res->set_200(rateLimitManager->get_all_rules_json().dump());
+ return true;
+}
+
+bool 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
+ uint64_t id = std::stoull(req->params["id"]);
+ const auto& rule_option = rateLimitManager->find_rule_by_id(id);
+
+ if(!rule_option.ok()) {
+ res->set(rule_option.code(), rule_option.error());
+ return false;
+ }
+
+ res->set_200(rule_option.get().dump());
+ return true;
+}
+
+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;
+ uint64_t id = std::stoull(req->params["id"]);
+
+ try {
+ req_json = nlohmann::json::parse(req->body);
+ } catch(const nlohmann::json::parse_error& e) {
+ res->set_400("Invalid JSON");
+ return false;
+ }
+
+ const auto& edit_rule_result = rateLimitManager->edit_rule(id, req_json);
+
+ if(!edit_rule_result.ok()) {
+ res->set(edit_rule_result.code(), edit_rule_result.error());
+ return false;
+ }
+
+ res->set_200(edit_rule_result.get().dump());
+ return true;
+
+}
+
+bool del_rate_limit(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {
+ RateLimitManager* rateLimitManager = RateLimitManager::getInstance();
+ uint64_t id = std::stoull(req->params["id"]);
+ const auto& rule_option = rateLimitManager->find_rule_by_id(id);
+
+ if(!rule_option.ok()) {
+ res->set(rule_option.code(), rule_option.error());
+ return false;
+ }
+
+ rateLimitManager->delete_rule_by_id(id);
+ res->set_200("OK");
+ return true;
+}
+
+bool post_rate_limit(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {
+ RateLimitManager* rateLimitManager = RateLimitManager::getInstance();
+ nlohmann::json req_json;
+
+ try {
+ req_json = nlohmann::json::parse(req->body);
+ } catch (const std::exception & e) {
+ res->set_400("Bad JSON.");
+ return false;
+ }
+
+ auto add_rule_result = rateLimitManager->add_rule(req_json);
+
+ if(!add_rule_result.ok()) {
+ res->set(add_rule_result.code(), add_rule_result.error());
+ return false;
+ }
+
+ res->set_200(add_rule_result.get().dump());
+ return true;
+}
\ No newline at end of file
diff --git a/src/http_server.cpp b/src/http_server.cpp
index c6c53d36..6dcf8ed2 100644
--- a/src/http_server.cpp
+++ b/src/http_server.cpp
@@ -10,6 +10,7 @@
#include <app_metrics.h>
#include "raft_server.h"
#include "logger.h"
+#include "ratelimit_manager.h"
HttpServer::HttpServer(const std::string & version, const std::string & listen_address,
uint32_t listen_port, const std::string & ssl_cert_path, const std::string & ssl_cert_key_path,
@@ -454,6 +455,13 @@ int HttpServer::catch_all_handler(h2o_handler_t *_h2o_handler, h2o_req_t *req) {
const std::string & body = std::string(req->entity.base, req->entity.len);
std::vector<nlohmann::json> embedded_params_vec;
+
+ 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);
+ }
+
+
if(root_resource != "multi_search") {
// multi_search needs to be handled later because the API key could be part of request body and
// the whole request body might not be available right now.
@@ -466,6 +474,8 @@ int HttpServer::catch_all_handler(h2o_handler_t *_h2o_handler, h2o_req_t *req) {
}
}
+
+
std::shared_ptr<http_req> request = std::make_shared<http_req>(req, rpath->http_method, path_without_query,
route_hash, query_map, embedded_params_vec,
api_auth_key_sent, body, client_ip);
diff --git a/src/main/typesense_server.cpp b/src/main/typesense_server.cpp
index 7a950e30..c2c64cd9 100644
--- a/src/main/typesense_server.cpp
+++ b/src/main/typesense_server.cpp
@@ -76,6 +76,13 @@ void master_server_routes() {
server->post("/operations/vote", post_vote, false, false);
server->post("/operations/cache/clear", post_clear_cache, false, false);
+
+ server->get("/limits", get_rate_limits);
+ 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->post("/config", post_config, false, false);
}
diff --git a/src/ratelimit_manager.cpp b/src/ratelimit_manager.cpp
new file mode 100644
index 00000000..c5b43662
--- /dev/null
+++ b/src/ratelimit_manager.cpp
@@ -0,0 +1,490 @@
+#include "ratelimit_manager.h"
+#include "string_utils.h"
+#include "logger.h"
+#include <iterator>
+
+RateLimitManager * RateLimitManager::getInstance() {
+ if (!instance) {
+ instance = new RateLimitManager();
+ }
+
+ return instance;
+}
+
+
+bool RateLimitManager::remove_rule_entity(const RateLimitedEntityType entity_type, const std::string &entity) {
+ // lock mutex
+ std::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;
+
+}
+
+void RateLimitManager::temp_ban_entity(const rate_limit_entity_t& entity, const int64_t number_of_days) {
+ // lock mutex
+ std::unique_lock<std::shared_mutex>lock(rate_limit_mutex);
+ temp_ban_entity_wrapped(entity, number_of_days);
+}
+
+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) {
+
+ auto entity_rule = rate_limit_entities.find(entity);
+ auto wildcard_rule = rate_limit_entities.find(rate_limit_entity_t{entity.entity_type, ".*"});
+
+ 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) {
+ return true;
+ }
+ else if(rule.action == RateLimitAction::allow) {
+ return false;
+ }
+ 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;
+ }
+ 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;
+ }
+ // Increment request counts
+ request_counts.current_requests_count_minute++;
+ request_counts.current_requests_count_hour++;
+ }
+ }
+ return false;
+}
+
+Option<nlohmann::json> RateLimitManager::find_rule_by_id(const uint64_t id) {
+ std::shared_lock<std::shared_mutex> lock(rate_limit_mutex);
+ if(rule_store.count(id) > 0) {
+ return Option<nlohmann::json>(rule_store.at(id).to_json());
+ }
+ return Option<nlohmann::json>(404, "Not Found");
+}
+
+bool RateLimitManager::delete_rule_by_id(const uint64_t id) {
+ std::unique_lock<std::shared_mutex> lock(rate_limit_mutex);
+ const std::string rule_store_key = get_rule_key(id);
+ bool deleted = store->remove(rule_store_key);
+ if(!deleted) {
+ return false;
+ }
+ // Check if a rule exists for the given ID
+ if(rule_store.count(id) > 0) {
+ auto rule = rule_store.at(id);
+ // Remove rule from rule store
+ rule_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});
+ }
+ return true;
+ }
+ return false;
+}
+
+
+const std::vector<rate_limit_rule_t> RateLimitManager::get_all_rules() {
+ std::shared_lock<std::shared_mutex> lock(rate_limit_mutex);
+ // Get all rules in a vector
+ std::vector<rate_limit_rule_t> rules;
+ for(const auto &rule : rule_store) {
+ rules.push_back(rule.second);
+ }
+ return 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;
+ for (auto & element: throttled_entities) {
+ if (element.second.entity_type == entity_type) {
+ banned_entities.push_back(element.second);
+ }
+ }
+ // Get permanent bans
+ for (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++;
+ }
+ }
+ }
+ return banned_entities;
+}
+
+void RateLimitManager::clear_all() {
+ std::unique_lock<std::shared_mutex> lock(rate_limit_mutex);
+ rate_limit_request_counts.clear();
+ rate_limit_entities.clear();
+ throttled_entities.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) {
+ 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};
+ 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}});
+ last_ban_id++;
+ if(rate_limit_request_counts.contains(entity)){
+ // 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;
+ }
+}
+
+const nlohmann::json RateLimitManager::get_all_rules_json() {
+ std::shared_lock<std::shared_mutex> lock(rate_limit_mutex);
+ nlohmann::json rules_json = nlohmann::json::array();
+ for(const auto &rule : rule_store) {
+ rules_json.push_back(rule.second.to_json());
+ }
+ return 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;
+ }
+ return rule;
+}
+
+const nlohmann::json rate_limit_status_t::to_json() const {
+ nlohmann::json status;
+ status["throttling_from"] = throttling_from;
+ status["throttling_to"] = throttling_to;
+ status["value"] = value;
+ status["entity_type"] = magic_enum::enum_name(entity_type);
+ return status;
+}
+
+void rate_limit_status_t::parse_json(const nlohmann::json &json) {
+ 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();
+}
+
+
+Option<nlohmann::json> RateLimitManager::add_rule(const nlohmann::json &rule_json) {
+ std::unique_lock<std::shared_mutex> lock(rate_limit_mutex);
+ auto rule_validation_result = is_valid_rule(rule_json);
+ if(!rule_validation_result.ok()) {
+ return Option<nlohmann::json>(rule_validation_result.code(), rule_validation_result.error());
+ }
+ 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());
+ }
+ rate_limit_rule_t parsed_rule = parsed_rule_option.get();
+ parsed_rule.id = last_rule_id++;
+ const std::string rule_store_key = get_rule_key(parsed_rule.id);
+ bool inserted = store->insert(rule_store_key, parsed_rule.to_json().dump());
+ if(!inserted) {
+ return Option<nlohmann::json>(500, "Failed to insert rule into the DB store");
+ }
+ store->increment(std::string(RULES_NEXT_ID), 1);
+ // Insert rule to rule store
+ insert_rule(parsed_rule);
+ nlohmann::json response;
+ response["message"] = "Rule added successfully.";
+ response["rule"] = parsed_rule.to_json();
+ return Option<nlohmann::json>(response);
+}
+
+Option<nlohmann::json> RateLimitManager::edit_rule(const uint64_t id, const nlohmann::json &rule_json) {
+ const auto& rule_option = find_rule_by_id(id);
+ if(!rule_option.ok()) {
+ return Option<nlohmann::json>(rule_option.code(), rule_option.error());
+ }
+ auto rule_validation_result = is_valid_rule(rule_json);
+ if(!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);
+ if(!parsed_rule_option.ok()) {
+ return Option<nlohmann::json>(parsed_rule_option.code(), parsed_rule_option.error());
+ }
+ rate_limit_rule_t parsed_rule = parsed_rule_option.get();
+ parsed_rule.id = id;
+ const std::string rule_store_key = get_rule_key(parsed_rule.id);
+ bool inserted = store->insert(rule_store_key, parsed_rule.to_json().dump());
+ if(!inserted) {
+ return Option<nlohmann::json>(500, "Failed to update rule in the DB store");
+ }
+ 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});
+ }
+ // Insert new rule to rule store
+ insert_rule(parsed_rule);
+ nlohmann::json response;
+ response["message"] = "Rule updated successfully.";
+ response["rule"] = parsed_rule.to_json();
+ return Option<nlohmann::json>(response);
+}
+
+Option<bool> RateLimitManager::is_valid_rule(const nlohmann::json &rule_json) {
+ 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.");
+ }
+ 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.");
+ }
+ 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.");
+ }
+ 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_1h") > 0 && rule_json["max_requests_1h"].is_number_integer() == false) {
+ 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_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["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.");
+ }
+ }
+ } else {
+ return Option<bool>(400, "Invalid action.");
+ }
+ return Option<bool>(true);
+}
+
+
+Option<rate_limit_rule_t> RateLimitManager::parse_rule(const nlohmann::json &rule_json, bool alert_if_exists)
+{
+ 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);
+ }
+ }
+ 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);
+ }
+ }
+ 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_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"];
+ }
+ return Option<rate_limit_rule_t>(new_rule);
+}
+
+
+void RateLimitManager::insert_rule(const rate_limit_rule_t &rule) {
+ 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)});
+ }
+}
+
+
+Option<bool> RateLimitManager::init(Store *store) {
+ std::unique_lock<std::shared_mutex> lock(rate_limit_mutex);
+ this->store = store;
+ // Load rules from database
+ std::string last_rule_id_str;
+ StoreStatus last_rule_id_status = store->get(std::string(RULES_NEXT_ID), last_rule_id_str);
+ if(last_rule_id_status == StoreStatus::ERROR) {
+ return Option<bool>(500, "Error while fetching rule next id from database.");
+ }
+ else if(last_rule_id_status == StoreStatus::FOUND) {
+ last_rule_id = StringUtils::deserialize_uint32_t(last_rule_id_str);
+ }
+ else {
+ last_rule_id = 0;
+ }
+ std::vector<std::string> rule_json_strs;
+ store->scan_fill(RULES_PREFIX, rule_json_strs);
+ for(const auto& rule_json_str: rule_json_strs) {
+ nlohmann::json rule_json = nlohmann::json::parse(rule_json_str);
+ Option<rate_limit_rule_t> rule_option = parse_rule(rule_json);
+ if(!rule_option.ok()) {
+ return Option<bool>(rule_option.code(), rule_option.error());
+ }
+ auto rule = rule_option.get();
+ rule.id = rule_json["id"];
+ insert_rule(rule);
+ }
+ // Load bans from database
+ std::string last_ban_id_str;
+ StoreStatus last_ban_id_status = store->get(BANS_NEXT_ID, last_ban_id_str);
+ if(last_ban_id_status == StoreStatus::ERROR) {
+ return Option<bool>(500, "Error while fetching ban next id from database.");
+ }
+ else if(last_ban_id_status == StoreStatus::FOUND) {
+ last_ban_id = StringUtils::deserialize_uint32_t(last_ban_id_str);
+ }
+ else {
+ last_ban_id = 0;
+ }
+ std::vector<std::string> ban_json_strs;
+ store->scan_fill(BANS_PREFIX, ban_json_strs);
+ for(const auto& ban_json_str: ban_json_strs) {
+ 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});
+ }
+ LOG(INFO) << "Loaded " << rule_store.size() << " rate limit rules.";
+ LOG(INFO) << "Loaded " << throttled_entities.size() << " rate limit bans.";
+ return Option<bool>(true);
+}
+
+std::string RateLimitManager::get_rule_key(const uint32_t id) {
+ return std::string(RULES_PREFIX) + "_" + std::to_string(id);
+}
+
+std::string RateLimitManager::get_ban_key(const uint32_t id) {
+ return std::string(BANS_PREFIX) + "_" + std::to_string(id);
+}
+
+time_t RateLimitManager::get_current_time() {
+ return base_timestamp + std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());;
+}
+
+void RateLimitManager::_set_base_timestamp(const time_t& timestamp) {
+ base_timestamp = timestamp;
+}
\ No newline at end of file
diff --git a/src/typesense_server_utils.cpp b/src/typesense_server_utils.cpp
index 3e9e51ad..5332caec 100644
--- a/src/typesense_server_utils.cpp
+++ b/src/typesense_server_utils.cpp
@@ -15,6 +15,7 @@
#include <ifaddrs.h>
#include "core_api.h"
+#include "ratelimit_manager.h"
#include "typesense_server_utils.h"
#include "file_utils.h"
#include "threadpool.h"
@@ -442,6 +443,13 @@ int run_server(const Config & config, const std::string & version, void (*master
CollectionManager & collectionManager = CollectionManager::get_instance();
collectionManager.init(&store, &app_thread_pool, config.get_max_memory_ratio(),
config.get_api_key(), quit_raft_service, batch_indexer);
+
+ RateLimitManager *rateLimitManager = RateLimitManager::getInstance();
+ auto rate_limit_manager_init = rateLimitManager->init(&store);
+
+ if(!rate_limit_manager_init.ok()) {
+ LOG(INFO) << "Failed to initialize rate limit manager: " << rate_limit_manager_init.error();
+ }
// first we start the peering service
diff --git a/test/ratelimit_test.cpp b/test/ratelimit_test.cpp
new file mode 100644
index 00000000..61557d1b
--- /dev/null
+++ b/test/ratelimit_test.cpp
@@ -0,0 +1,439 @@
+#include <gtest/gtest.h>
+#include <string>
+#include <thread>
+#include "ratelimit_manager.h"
+#include "logger.h"
+
+// Google test for RateLimitManager
+class RateLimitManagerTest : public ::testing::Test
+{
+protected:
+ RateLimitManager *manager = RateLimitManager::getInstance();
+ Store *store;
+
+
+ void changeBaseTimestamp(const uint64_t new_base_timestamp) {
+ manager->_set_base_timestamp(new_base_timestamp);
+ }
+
+
+
+ RateLimitManagerTest() {
+
+ }
+
+ virtual ~RateLimitManagerTest() {
+ // You can do clean-up work that doesn't throw exceptions here.
+ manager->clear_all();
+
+ }
+
+ // If the constructor and destructor are not enough for setting up
+ // and cleaning up each test, you can define the following methods:
+
+ virtual void SetUp() {
+ std::string state_dir_path = "/tmp/typesense_test/rate_limit_manager_test_db";
+ system(("rm -rf "+state_dir_path+" && mkdir -p "+state_dir_path).c_str());
+
+ store = new Store(state_dir_path);
+ manager->init(store);
+ }
+
+ virtual void TearDown() {
+ delete store;
+ }
+
+ // Objects declared here can be used by all tests in the test case for Foo.
+};
+
+TEST_F(RateLimitManagerTest, TestAddRateLimitApiKey) {
+ auto res = 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);
+}
+
+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_1h", 100},
+ {"auto_ban_threshold_num", 10},
+ {"auto_ban_num_days", 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"},
+ {"ip_addresses", nlohmann::json::array({"0.0.0.1"})}
+ });
+ EXPECT_EQ(manager->get_banned_entities(RateLimitedEntityType::ip).size(), 1);
+}
+
+TEST_F(RateLimitManagerTest, TestGetTrackedIps) {
+ 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}
+ });
+ 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";
+ EXPECT_TRUE(found);
+}
+
+TEST_F(RateLimitManagerTest, TestGetTrackedApiKeys) {
+ 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}
+ });
+ 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";
+ EXPECT_TRUE(found);
+}
+
+TEST_F(RateLimitManagerTest, TestBanIpPermanently) {
+ 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";
+ 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";
+ 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) {
+ manager->add_rule({
+ {"action", "block"},
+ {"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";
+ EXPECT_TRUE(found);
+}
+
+TEST_F(RateLimitManagerTest, TestIsBannedIpTemp) {
+ manager->add_rule({
+ {"action", "throttle"},
+ {"ip_addresses", nlohmann::json::array({"0.0.0.1"})},
+ {"max_requests_60s", 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"}}));
+}
+
+TEST_F(RateLimitManagerTest, TestIsBannedAPIKeyPermanently) {
+ manager->add_rule({
+ {"action", "block"},
+ {"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";
+ EXPECT_TRUE(found);
+}
+
+TEST_F(RateLimitManagerTest, TestIsBannedAPIKeyTemp) {
+ manager->add_rule({
+ {"action", "throttle"},
+ {"api_keys", nlohmann::json::array({"test"})},
+ {"max_requests_60s", 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"}}));
+}
+
+TEST_F(RateLimitManagerTest, TestAllowAPIKey) {
+ manager->add_rule({
+ {"action", "allow"},
+ {"api_keys", nlohmann::json::array({"test"})}
+ });
+ EXPECT_TRUE(manager->get_all_rules().size() == 1);
+ EXPECT_FALSE(manager->is_rate_limited({{RateLimitedEntityType::api_key, "test_"}}));
+}
+
+TEST_F(RateLimitManagerTest, TestAllowIp) {
+ manager->add_rule({
+ {"action", "allow"},
+ {"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"}}));
+}
+
+TEST_F(RateLimitManagerTest, TestThrottleAPIKey) {
+ manager->add_rule({
+ {"action", "throttle"},
+ {"api_keys", nlohmann::json::array({"test"})},
+ {"max_requests_60s", 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"}}));
+}
+
+TEST_F(RateLimitManagerTest, TestDeleteRuleByID) {
+ manager->add_rule({
+ {"action", "throttle"},
+ {"api_keys", nlohmann::json::array({"test"})},
+ {"max_requests_60s", 1},
+ {"max_requests_1h", 1}
+ });
+ EXPECT_TRUE(manager->get_all_rules().size() == 1);
+ auto rules = manager->get_all_rules();
+ manager->delete_rule_by_id(rules[0].id);
+ EXPECT_EQ(manager->get_all_rules().size(), 0);
+}
+
+TEST_F(RateLimitManagerTest, TestMinuteRateLimitAPIKey) {
+ manager->add_rule({
+ {"action", "throttle"},
+ {"api_keys", nlohmann::json::array({"test"})},
+ {"max_requests_60s", 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"}}));
+}
+
+TEST_F(RateLimitManagerTest, TestHourRateLimitAPIKey) {
+ manager->add_rule({
+ {"action", "throttle"},
+ {"api_keys", nlohmann::json::array({"test"})},
+ {"max_requests_60s", -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"}}));
+}
+
+TEST_F(RateLimitManagerTest, TestMinuteRateLimitIp) {
+ manager->add_rule({
+ {"action", "throttle"},
+ {"ip_addresses", nlohmann::json::array({"0.0.0.1"})},
+ {"max_requests_60s", 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"}}));
+}
+
+TEST_F(RateLimitManagerTest, TestHourRateLimitIp) {
+ manager->add_rule({
+ {"action", "throttle"},
+ {"ip_addresses", nlohmann::json::array({"0.0.0.1"})},
+ {"max_requests_60s", -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"}}));
+}
+
+TEST_F(RateLimitManagerTest, TestGetAllRules) {
+ manager->add_rule({
+ {"action", "throttle"},
+ {"ip_addresses", nlohmann::json::array({"0.0.0.1"})},
+ {"max_requests_60s", -1},
+ {"max_requests_1h", 5}
+ });
+ manager->add_rule({
+ {"action", "throttle"},
+ {"api_keys", nlohmann::json::array({"test"})},
+ {"max_requests_60s", 5},
+ {"max_requests_1h", -1}
+ });
+ EXPECT_TRUE(manager->get_all_rules().size() == 2);
+}
+
+TEST_F(RateLimitManagerTest, TestGetAllRulesEmpty) {
+ auto rules = manager->get_all_rules();
+ EXPECT_EQ(rules.size(), 0);
+}
+
+TEST_F(RateLimitManagerTest, TestGetAllRulesJSON) {
+ manager->add_rule({
+ {"action", "throttle"},
+ {"api_keys", nlohmann::json::array({"test"})},
+ {"max_requests_60s", 5},
+ {"max_requests_1h", -1}
+ });
+ nlohmann::json rules = manager->get_all_rules_json();
+ EXPECT_EQ(rules.is_array(), true);
+ 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);
+}
+
+TEST_F(RateLimitManagerTest, TestAutoBan) {
+ manager->add_rule({
+ {"action", "throttle"},
+ {"api_keys", nlohmann::json::array({"test"})},
+ {"max_requests_60s", 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"}}));
+ 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"}}));
+}
+
+
+TEST_F(RateLimitManagerTest, TestWildcard) {
+ manager->add_rule({
+ {"action", "throttle"},
+ {"api_keys", nlohmann::json::array({".*"})},
+ {"max_requests_60s", 5},
+ {"max_requests_1h", -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, "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"}}));
+}
+
+TEST_F(RateLimitManagerTest, TestCorrectOrderofRules) {
+ manager->add_rule({
+ {"action", "throttle"},
+ {"api_keys", nlohmann::json::array({".*"})},
+ {"max_requests_60s", 2},
+ {"max_requests_1h", -1}
+ });
+ manager->add_rule({
+ {"action", "throttle"},
+ {"api_keys", nlohmann::json::array({"test"})},
+ {"max_requests_60s", 5},
+ {"max_requests_1h", -1}
+ });
+ manager->add_rule({
+ {"action", "block"},
+ {"api_keys", nlohmann::json::array({"test1"})},
+ });
+ manager->add_rule({
+ {"action", "allow"},
+ {"api_keys", nlohmann::json::array({"test2"})},
+ });
+ 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