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
API Key management.
kishorenc29b422546108
diff --git a/include/auth_manager.h b/include/auth_manager.hnew file mode 100644index 00000000..7db4d199--- /dev/null+++ b/include/auth_manager.h@@ -0,0 +1,111 @@+#pragma once++#include <string>+#include <vector>+#include <map>+#include "json.hpp"+#include "option.h"+#include "store.h"++struct api_key_t {+ uint32_t id;+ std::string value;+ std::string description;+ std::vector<std::string> actions;+ std::vector<std::string> collections;++ api_key_t() {++ }++ api_key_t(const std::string& value, const std::string& description, const std::vector<std::string>& actions,+ const std::vector<std::string>& collections):+ value(value), description(description), actions(actions), collections(collections) {++ }++ Option<bool> load(const std::string & json_str) {+ nlohmann::json key_obj;++ try {+ key_obj = nlohmann::json::parse(json_str);+ } catch(...) {+ return Option<bool>(500, "Error while parsing JSON string.");+ }++ id = key_obj["id"];+ value = key_obj["value"];+ description = key_obj["description"].get<std::string>();+ actions = key_obj["actions"].get<std::vector<std::string>>();+ collections = key_obj["collections"].get<std::vector<std::string>>();++ return Option<bool>(true);+ }++ static Option<uint32_t> validate(const nlohmann::json & key_obj) {+ auto mandatory_keys = {+ "description", "actions", "collections"+ };++ for(auto key: mandatory_keys) {+ if(key_obj.count(key) == 0) {+ return Option<uint32_t>(400, std::string("Could not find a `") + key + "` key.");+ }+ }++ return Option<uint32_t>(200);+ }++ nlohmann::json to_json() const {+ nlohmann::json obj;+ obj["id"] = id;+ obj["value"] = value;+ obj["description"] = description;+ obj["actions"] = actions;+ obj["collections"] = collections;++ return obj;+ }++ api_key_t& truncate_value() {+ value = value.substr(0, 4); // return only first 4 chars+ return (*this);+ }+};++class AuthManager {++private:++ std::map<std::string, api_key_t> api_keys; // stores key_value => key mapping+ Store *store;++ // Auto incrementing API KEY ID+ uint32_t next_api_key_id;++ // Using a $ prefix so that these meta keys stay above record entries in a lexicographically ordered KV store+ static constexpr const char* API_KEY_NEXT_ID_KEY = "$KN";+ static constexpr const char* API_KEYS_PREFIX = "$KP";++ uint32_t get_next_api_key_id();++public:++ AuthManager() = default;++ Option<bool> init(Store *store);++ Option<std::vector<api_key_t>> list_keys();++ Option<api_key_t> get_key(uint32_t id, bool truncate_value = true);++ Option<api_key_t> create_key(api_key_t& api_key);++ Option<api_key_t> remove_key(uint32_t id);++ bool authenticate(+ const std::string& req_api_key,+ const std::string& action,+ const std::string& collection+ );+};\ No newline at end of filediff --git a/include/collection_manager.h b/include/collection_manager.hindex 22d72c5f..5c6d34a1 100644--- a/include/collection_manager.h+++ b/include/collection_manager.h@@ -6,11 +6,13 @@#include "store.h"#include "field.h"#include "collection.h"+#include "auth_manager.h"// Singleton, for managing meta information of all collections and house keepingclass CollectionManager {private:Store *store;+ AuthManager auth_manager;spp::sparse_hash_map<std::string, Collection*> collections;@@ -28,8 +30,8 @@ private:static constexpr const char* COLLECTION_DEFAULT_SORTING_FIELD_KEY = "default_sorting_field";static constexpr const char* COLLECTION_CREATED = "created_at";- std::string auth_key;- std::string search_only_auth_key;+ std::string bootstrap_auth_key;+ std::string bootstrap_search_only_auth_key;size_t default_num_indices;@@ -71,9 +73,7 @@ public:void add_to_collections(Collection* collection);- bool auth_key_matches(std::string auth_key_sent);-- bool search_only_auth_key_matches(const std::string & auth_key_sent);+ bool auth_key_matches(const std::string& auth_key_sent, const std::string& action, const std::string& collection);Option<Collection*> create_collection(const std::string name, const std::vector<field> & fields,const std::string & default_sorting_field,@@ -95,6 +95,8 @@ public:Store* get_store();+ AuthManager& getAuthManager();+// symlinksOption<std::string> resolve_symlink(const std::string & symlink_name);diff --git a/include/core_api.h b/include/core_api.hindex cfe950d7..790d812b 100644--- a/include/core_api.h+++ b/include/core_api.h@@ -2,7 +2,7 @@#include "http_server.h"-bool handle_authentication(const route_path & rpath, const std::string & auth_key);+bool handle_authentication(const http_req& req, const route_path& rpath, const std::string& auth_key);bool get_collections(http_req & req, http_res & res);diff --git a/include/http_data.h b/include/http_data.hindex 56694f60..277814e5 100644--- a/include/http_data.h+++ b/include/http_data.h@@ -5,6 +5,7 @@#include <map>#include <vector>#include <future>+#include <chrono>#include "json.hpp"#include "string_utils.h"@@ -116,12 +117,18 @@ struct http_req {uint64_t route_hash;std::map<std::string, std::string> params;std::string body;+ uint64_t seed;- http_req(): route_hash(1) {}+ http_req(): route_hash(1), seed(random_uint64_t()) {++ }http_req(h2o_req_t* _req, const std::string & http_method, uint64_t route_hash,const std::map<std::string, std::string> & params, std::string body):- _req(_req), http_method(http_method), route_hash(route_hash), params(params), body(body) {}+ _req(_req), http_method(http_method), route_hash(route_hash), params(params),+ body(body), seed(random_uint64_t()) {++ }// NOTE: we don't ser/de all fields, only ones needed for write forwarding@@ -129,6 +136,7 @@ struct http_req {nlohmann::json content = nlohmann::json::parse(serialized_content);route_hash = content["route_hash"];body = content["body"];+ seed = content["seed"];for (nlohmann::json::iterator it = content["params"].begin(); it != content["params"].end(); ++it) {params.emplace(it.key(), it.value());@@ -142,9 +150,16 @@ struct http_req {content["route_hash"] = route_hash;content["params"] = params;content["body"] = body;+ content["seed"] = seed;return content.dump();}++ uint64_t random_uint64_t() {+ thread_local std::mt19937 rg(std::random_device{}());+ thread_local std::uniform_int_distribution<uint64_t> pick(0, std::numeric_limits<uint64_t>::max());+ return pick(rg);+ }};struct request_response {@@ -157,6 +172,13 @@ struct route_path {std::vector<std::string> path_parts;bool (*handler)(http_req &, http_res &);bool async;+ std::string action;++ route_path(const std::string &httpMethod, const std::vector<std::string> &pathParts,+ bool (*handler)(http_req &, http_res &), bool async) :+ http_method(httpMethod), path_parts(pathParts), handler(handler), async(async) {+ action = _get_action();+ }inline bool operator< (const route_path& rhs) const {return true;@@ -168,6 +190,56 @@ struct route_path {uint64_t hash = StringUtils::hash_wy(method_path.c_str(), method_path.size());return (hash > 100) ? hash : (hash + 100); // [0-99] reserved for special codes}++ std::string _get_action() {+ // `resource:operation` forms an action+ // operations: create, get, list, delete, search, import, export++ std::string resource;+ std::string operation;++ size_t resource_index = 0;+ size_t identifier_index = 0;++ for(size_t i = 0; i < path_parts.size(); i++) {+ if(path_parts[i][0] == ':') {+ identifier_index = i;+ }+ }++ if(identifier_index == 0) {+ // means that no identifier found, so set the last part as resource+ resource_index = path_parts.size() - 1;+ } else if(identifier_index == (path_parts.size() - 1)) {+ // is already last position+ resource_index = identifier_index - 1;+ } else {+ resource_index = identifier_index + 1;+ }++ resource = path_parts[resource_index];++ if(resource_index != path_parts.size() - 1 && path_parts[resource_index+1][0] != ':') {+ // e.g. /collections/:collection/documents/search+ operation = path_parts[resource_index+1];+ } else {+ // e.g /collections or /collections/:collection/documents or /collections/:collection+ // get will be a list, post would be a create, etc.+ if(http_method == "GET") {+ operation = "list";+ } else if(http_method == "POST") {+ operation = "create";+ } else if(http_method == "PUT") {+ operation = "upsert";+ } else if(http_method == "DELETE") {+ operation = "delete";+ } else {+ operation = "unknown";+ }+ }++ return resource + ":" + operation;+ }};struct h2o_custom_generator_t {diff --git a/include/http_server.h b/include/http_server.hindex 289a325b..d0bd5bcd 100644--- a/include/http_server.h+++ b/include/http_server.h@@ -61,7 +61,7 @@ private:bool cors_enabled;- bool (*auth_handler)(const route_path & rpath, const std::string & auth_key);+ bool (*auth_handler)(const http_req& req, const route_path& rpath, const std::string& auth_key);static void on_accept(h2o_socket_t *listener, const char *err);@@ -93,7 +93,8 @@ public:bool is_alive() const;- void set_auth_handler(bool (*handler)(const route_path & rpath, const std::string & auth_key));+ void set_auth_handler(bool (*handler)(const http_req & req, const route_path & rpath,+ const std::string & auth_key));void get(const std::string & path, bool (*handler)(http_req & req, http_res & res), bool async = false);diff --git a/include/store.h b/include/store.hindex 9ae74f55..669bdcb5 100644--- a/include/store.h+++ b/include/store.h@@ -74,8 +74,7 @@ public:write_options.disableWAL = disable_wal;// open DB- const rocksdb::Status& s = init_db();- assert(s.ok());+ init_db();}~Store() {@@ -94,6 +93,7 @@ public:}}+ assert(s.ok());return s;}diff --git a/include/string_utils.h b/include/string_utils.hindex 61c879de..13cda34f 100644--- a/include/string_utils.h+++ b/include/string_utils.h@@ -7,6 +7,7 @@#include <unicode/translit.h>#include <iconv.h>#include <vector>+#include <random>#include "wyhash_v5.h"struct StringUtils {@@ -225,4 +226,6 @@ struct StringUtils {// reserve max() for use as a delimiterreturn hash != std::numeric_limits<uint64_t>::max() ? hash : (std::numeric_limits<uint64_t>::max()-1);}++ static std::string randstring(size_t length, uint64_t seed);};\ No newline at end of filediff --git a/src/auth_manager.cpp b/src/auth_manager.cppnew file mode 100644index 00000000..e9f04ed5--- /dev/null+++ b/src/auth_manager.cpp@@ -0,0 +1,153 @@+#include "auth_manager.h"++Option<std::vector<api_key_t>> AuthManager::list_keys() {+ std::vector<std::string> api_key_json_strs;+ store->scan_fill(API_KEYS_PREFIX, api_key_json_strs);++ std::vector<api_key_t> stored_api_keys;++ for(const auto& api_key_json_str: api_key_json_strs) {+ api_key_t api_key;+ Option<bool> load_op = api_key.load(api_key_json_str);+ if(!load_op.ok()) {+ return Option<std::vector<api_key_t>>(load_op.code(), load_op.error());+ }++ stored_api_keys.push_back(api_key.truncate_value());+ }++ return Option<std::vector<api_key_t>>(stored_api_keys);+}++Option<api_key_t> AuthManager::get_key(uint32_t id, bool truncate_value) {+ std::string api_key_store_key = std::string(API_KEYS_PREFIX) + "_" + std::to_string(id);+ std::string api_key_json_str;+ StoreStatus status = store->get(api_key_store_key, api_key_json_str);++ if(status == StoreStatus::FOUND) {+ api_key_t api_key;+ const Option<bool> & load_op = api_key.load(api_key_json_str);+ if(!load_op.ok()) {+ return Option<api_key_t>(load_op.code(), load_op.error());+ }++ if(truncate_value) {+ api_key.truncate_value();+ }+ return Option<api_key_t>(api_key);+ }++ if(status == StoreStatus::NOT_FOUND) {+ return Option<api_key_t>(404, "Not found.");+ }++ return Option<api_key_t>(500, "Error while fetching key from store.");+}++Option<api_key_t> AuthManager::create_key(api_key_t& api_key) {+ if(api_keys.count(api_key.value) != 0) {+ return Option<api_key_t>(409, "API key generation conflict.");+ }++ api_key.id = get_next_api_key_id();++ std::string api_key_store_key = std::string(API_KEYS_PREFIX) + "_" + std::to_string(api_key.id);+ const nlohmann::json & api_key_obj = api_key.to_json();++ bool inserted = store->insert(api_key_store_key, api_key_obj.dump());+ if(!inserted) {+ return Option<api_key_t>(500, "Could not store generated API key.");+ }++ api_keys.emplace(api_key.value, api_key);+ return Option<api_key_t>(api_key);+}++Option<api_key_t> AuthManager::remove_key(uint32_t id) {+ Option<api_key_t> key_op = get_key(id, false);++ if(!key_op.ok()) {+ return Option<api_key_t>(500, key_op.error());+ }++ std::string api_key_store_key = std::string(API_KEYS_PREFIX) + "_" + std::to_string(id);+ if(!store->remove(api_key_store_key)) {+ return Option<api_key_t>(500, "Could not delete API key.");+ }++ api_key_t&& key = key_op.get();+ api_keys.erase(key.value);++ return Option<api_key_t>(key.truncate_value());+}++uint32_t AuthManager::get_next_api_key_id() {+ store->increment(std::string(API_KEY_NEXT_ID_KEY), 1);+ return next_api_key_id++;+}++Option<bool> AuthManager::init(Store *store) {+ this->store = store;++ std::string next_api_key_id_str;+ StoreStatus next_api_key_id_status = store->get(API_KEY_NEXT_ID_KEY, next_api_key_id_str);++ if(next_api_key_id_status == StoreStatus::ERROR) {+ return Option<bool>(500, "Error while fetching the next API key id from the store.");+ }++ if(next_api_key_id_status == StoreStatus::FOUND) {+ next_api_key_id = (uint32_t) StringUtils::deserialize_uint32_t(next_api_key_id_str);+ } else {+ next_api_key_id = 0;+ }++ std::vector<std::string> api_key_json_strs;+ store->scan_fill(API_KEYS_PREFIX, api_key_json_strs);++ for(auto & api_key_json_str: api_key_json_strs) {+ api_key_t api_key;+ Option<bool> load_op = api_key.load(api_key_json_str);+ if(!load_op.ok()) {+ return Option<bool>(load_op.code(), load_op.error());+ }++ api_keys.emplace(api_key.value, api_key);+ }++ return Option<bool>(true);+}++bool AuthManager::authenticate(const std::string& req_api_key, const std::string& action,+ const std::string& collection) {+ if(api_keys.count(req_api_key) == 0) {+ return false;+ }++ const api_key_t& api_key = api_keys[req_api_key];++ // check if action is allowed++ bool action_is_allowed = false;+ for(const std::string& allowed_action: api_key.actions) {+ if(allowed_action == "*" || (action != "*" && allowed_action == action)) {+ action_is_allowed = true;+ break;+ }+ }++ if(!action_is_allowed) {+ return false;+ }++ // check if action is allowed against a specific collection++ for(const std::string& allowed_collection: api_key.collections) {+ if(allowed_collection == "*" || (collection != "*" && allowed_collection == collection) || collection.empty()) {+ return true;+ }+ }++ return false;+}+diff --git a/src/collection_manager.cpp b/src/collection_manager.cppindex 02488a3b..f2841513 100644--- a/src/collection_manager.cpp+++ b/src/collection_manager.cpp@@ -53,9 +53,11 @@ void CollectionManager::init(Store *store,const std::string & auth_key,const std::string & search_only_auth_key) {this->store = store;- this->auth_key = auth_key;- this->search_only_auth_key = search_only_auth_key;+ this->bootstrap_auth_key = auth_key;+ this->bootstrap_search_only_auth_key = search_only_auth_key;this->default_num_indices = default_num_indices;++ auth_manager.init(store);}Option<bool> CollectionManager::load(const size_t init_batch_size) {@@ -206,12 +208,24 @@ void CollectionManager::dispose() {store->close();}-bool CollectionManager::auth_key_matches(std::string auth_key_sent) {- return (auth_key == auth_key_sent);-}+bool CollectionManager::auth_key_matches(const std::string& auth_key_sent, const std::string& action,+ const std::string& collection) {+ if(auth_key_sent.empty()) {+ return false;+ }++ // check with bootstrap search only auth key+ if(action == "documents:search" && bootstrap_search_only_auth_key == auth_key_sent) {+ return true;+ }-bool CollectionManager::search_only_auth_key_matches(const std::string & auth_key_sent) {- return !auth_key_sent.empty() && (search_only_auth_key == auth_key_sent);+ // check with bootstrap auth key+ if(bootstrap_auth_key == auth_key_sent) {+ return true;+ }++ // finally, check managed auth keys+ return auth_manager.authenticate(auth_key_sent, action, collection);}Option<Collection*> CollectionManager::create_collection(const std::string name, const std::vector<field> & fields,@@ -399,3 +413,7 @@ Option<bool> CollectionManager::delete_symlink(const std::string & symlink_name)Store* CollectionManager::get_store() {return store;}++AuthManager& CollectionManager::getAuthManager() {+ return auth_manager;+}diff --git a/src/core_api.cpp b/src/core_api.cppindex 2c78ff00..1e2ee439 100644--- a/src/core_api.cpp+++ b/src/core_api.cpp@@ -32,10 +32,21 @@ nlohmann::json collection_summary_json(Collection *collection) {return json_response;}-bool handle_authentication(const route_path & rpath, const std::string & auth_key) {+bool handle_authentication(const http_req& req, const route_path& rpath, const std::string& auth_key) {CollectionManager & collectionManager = CollectionManager::get_instance();- return rpath.handler == get_health || collectionManager.auth_key_matches(auth_key) ||- (rpath.handler == get_search && collectionManager.search_only_auth_key_matches(auth_key));++ std::string collection = "*";++ if(req.params.count("collection") != 0) {+ collection = req.params.at("collection");+ }++ if(rpath.handler == get_health) {+ // health endpoint requires no authentication+ return true;+ }++ return collectionManager.auth_key_matches(auth_key, rpath.action, collection);}bool get_collections(http_req & req, http_res & res) {diff --git a/src/http_server.cpp b/src/http_server.cppindex e1f9a6c3..7e695789 100644--- a/src/http_server.cpp+++ b/src/http_server.cpp@@ -301,14 +301,14 @@ int HttpServer::catch_all_handler(h2o_handler_t *_self, h2o_req_t *req) {const std::string & req_body = std::string(req->entity.base, req->entity.len);// Extract auth key from header. If that does not exist, look for a GET parameter.- std::string auth_key_from_header = "";+ std::string api_auth_key_sent = "";ssize_t auth_header_cursor = h2o_find_header_by_str(&req->headers, AUTH_HEADER, strlen(AUTH_HEADER), -1);if(auth_header_cursor != -1) {h2o_iovec_t & slot = req->headers.entries[auth_header_cursor].value;- auth_key_from_header = std::string(slot.base, slot.len);+ api_auth_key_sent = std::string(slot.base, slot.len);} else if(query_map.count(AUTH_HEADER) != 0) {- auth_key_from_header = query_map[AUTH_HEADER];+ api_auth_key_sent = query_map[AUTH_HEADER];}// Handle CORS@@ -351,14 +351,7 @@ int HttpServer::catch_all_handler(h2o_handler_t *_self, h2o_req_t *req) {uint64_t route_hash = self->http_server->find_route(path_parts, http_method, &rpath);if(route_hash != static_cast<uint64_t>(ROUTE_CODES::NOT_FOUND)) {- bool authenticated = self->http_server->auth_handler(*rpath, auth_key_from_header);- if(!authenticated) {- std::string message = std::string("{\"message\": \"Forbidden - a valid `") + AUTH_HEADER +- "` header must be sent.\"}";- return send_response(req, 401, message);- }-- // routes match and is an authenticated request - iterate and extract path params+ // iterate and extract path paramsfor(size_t i = 0; i < rpath->path_parts.size(); i++) {const std::string & path_part = rpath->path_parts[i];if(path_part[0] == ':') {@@ -369,6 +362,14 @@ int HttpServer::catch_all_handler(h2o_handler_t *_self, h2o_req_t *req) {http_req* request = new http_req(req, http_method, route_hash, query_map, req_body);http_res* response = new http_res();+ bool authenticated = self->http_server->auth_handler(*request, *rpath, api_auth_key_sent);+ if(!authenticated) {+ std::string message = std::string("{\"message\": \"Forbidden - a valid `") + AUTH_HEADER ++ "` header must be sent.\"}";+ return send_response(req, 401, message);+ }++ // routes match and is an authenticated request// for writes, we defer to replication_stateif(http_method != "GET") {self->http_server->get_replication_state()->write(request, response);@@ -420,35 +421,36 @@ int HttpServer::send_response(h2o_req_t *req, int status_code, const std::stringreturn 0;}-void HttpServer::set_auth_handler(bool (*handler)(const route_path & rpath, const std::string & auth_key)) {+void HttpServer::set_auth_handler(bool (*handler)(const http_req& req, const route_path& rpath,+ const std::string& auth_key)) {auth_handler = handler;}void HttpServer::get(const std::string & path, bool (*handler)(http_req &, http_res &), bool async) {std::vector<std::string> path_parts;StringUtils::split(path, path_parts, "/");- route_path rpath = {"GET", path_parts, handler, async};+ route_path rpath("GET", path_parts, handler, async);routes.emplace_back(rpath.route_hash(), rpath);}void HttpServer::post(const std::string & path, bool (*handler)(http_req &, http_res &), bool async) {std::vector<std::string> path_parts;StringUtils::split(path, path_parts, "/");- route_path rpath = {"POST", path_parts, handler, async};+ route_path rpath("POST", path_parts, handler, async);routes.emplace_back(rpath.route_hash(), rpath);}void HttpServer::put(const std::string & path, bool (*handler)(http_req &, http_res &), bool async) {std::vector<std::string> path_parts;StringUtils::split(path, path_parts, "/");- route_path rpath = {"PUT", path_parts, handler, async};+ route_path rpath("PUT", path_parts, handler, async);routes.emplace_back(rpath.route_hash(), rpath);}void HttpServer::del(const std::string & path, bool (*handler)(http_req &, http_res &), bool async) {std::vector<std::string> path_parts;StringUtils::split(path, path_parts, "/");- route_path rpath = {"DELETE", path_parts, handler, async};+ route_path rpath("DELETE", path_parts, handler, async);routes.emplace_back(rpath.route_hash(), rpath);}diff --git a/src/string_utils.cpp b/src/string_utils.cppindex 53a35a6c..6e1d85c9 100644--- a/src/string_utils.cpp+++ b/src/string_utils.cpp@@ -48,3 +48,21 @@ void StringUtils::unicode_normalize(std::string & str) const {str.assign(lower_and_no_special_chars(out.str()));}++std::string StringUtils::randstring(size_t length, uint64_t seed) {+ static auto& chrs = "0123456789"+ "abcdefghijklmnopqrstuvwxyz"+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ";++ thread_local static std::mt19937 rg(seed);+ thread_local static std::uniform_int_distribution<std::string::size_type> pick(0, sizeof(chrs) - 2);++ std::string s;+ s.reserve(length);++ while(length--) {+ s += chrs[pick(rg)];+ }++ return s;+}diff --git a/test/auth_manager_test.cpp b/test/auth_manager_test.cppnew file mode 100644index 00000000..402e3051--- /dev/null+++ b/test/auth_manager_test.cpp@@ -0,0 +1,161 @@+#include <gtest/gtest.h>+#include <stdlib.h>+#include <iostream>+#include "auth_manager.h"++class AuthManagerTest : public ::testing::Test {+protected:+ Store *store;+ AuthManager auth_manager;++ void setupCollection() {+ std::string state_dir_path = "/tmp/typesense_test/auth_manager_test_db";+ system(("rm -rf "+state_dir_path+" && mkdir -p "+state_dir_path).c_str());++ store = new Store(state_dir_path);+ auth_manager.init(store);+ }++ virtual void SetUp() {+ setupCollection();+ }++ virtual void TearDown() {+ delete store;+ }+};++TEST_F(AuthManagerTest, CreateListDeleteAPIKeys) {+ auto list_op = auth_manager.list_keys();+ ASSERT_TRUE(list_op.ok());+ ASSERT_EQ(0, list_op.get().size());++ auto get_op = auth_manager.get_key(0);+ ASSERT_FALSE(get_op.ok());+ ASSERT_EQ(404, get_op.code());++ // test inserts++ api_key_t api_key1("abcd1", "test key 1", {"read", "write"}, {"collection1", "collection2"});+ api_key_t api_key2("abcd2", "test key 2", {"admin"}, {"*"});++ ASSERT_EQ("abcd1", api_key1.value);+ ASSERT_EQ("abcd2", api_key2.value);++ auto insert_op = auth_manager.create_key(api_key1);+ ASSERT_TRUE(insert_op.ok());+ ASSERT_EQ(5, insert_op.get().value.size());++ insert_op = auth_manager.create_key(api_key2);+ ASSERT_TRUE(insert_op.ok());+ ASSERT_EQ(5, insert_op.get().value.size());++ // get an individual key++ get_op = auth_manager.get_key(0);+ ASSERT_TRUE(get_op.ok());+ const api_key_t &key1 = get_op.get();+ ASSERT_EQ(4, key1.value.size());+ ASSERT_EQ("test key 1", key1.description);+ ASSERT_EQ(2, key1.actions.size());+ EXPECT_STREQ("read", key1.actions[0].c_str());+ EXPECT_STREQ("write", key1.actions[1].c_str());+ ASSERT_EQ(2, key1.collections.size());+ EXPECT_STREQ("collection1", key1.collections[0].c_str());+ EXPECT_STREQ("collection2", key1.collections[1].c_str());++ get_op = auth_manager.get_key(1);+ ASSERT_TRUE(get_op.ok());+ ASSERT_EQ(4, get_op.get().value.size());+ ASSERT_EQ("test key 2", get_op.get().description);++ get_op = auth_manager.get_key(1, false);+ ASSERT_TRUE(get_op.ok());+ ASSERT_NE(4, get_op.get().value.size());++ get_op = auth_manager.get_key(2, false);+ ASSERT_FALSE(get_op.ok());++ // listing keys+ list_op = auth_manager.list_keys();+ ASSERT_TRUE(list_op.ok());+ ASSERT_EQ(2, list_op.get().size());+ ASSERT_EQ("test key 1", list_op.get()[0].description);+ ASSERT_EQ("abcd", list_op.get()[0].value);+ ASSERT_EQ("test key 2", list_op.get()[1].description);+ ASSERT_EQ("abcd", list_op.get()[1].value);+}++TEST_F(AuthManagerTest, CheckRestoreOfAPIKeys) {+ api_key_t api_key1("abcd1", "test key 1", {"read", "write"}, {"collection1", "collection2"});+ api_key_t api_key2("abcd2", "test key 2", {"admin"}, {"*"});++ std::string key_value1 = auth_manager.create_key(api_key1).get().value;+ std::string key_value2 = auth_manager.create_key(api_key2).get().value;++ AuthManager auth_manager2;+ auth_manager2.init(store);++ // list keys++ auto list_op = auth_manager.list_keys();+ ASSERT_TRUE(list_op.ok());+ ASSERT_EQ(2, list_op.get().size());+ ASSERT_EQ("test key 1", list_op.get()[0].description);+ ASSERT_EQ("abcd", list_op.get()[0].value);+ ASSERT_STREQ(key_value1.substr(0, 4).c_str(), list_op.get()[0].value.c_str());+ ASSERT_EQ("test key 2", list_op.get()[1].description);+ ASSERT_EQ("abcd", list_op.get()[1].value);+ ASSERT_STREQ(key_value2.substr(0, 4).c_str(), list_op.get()[1].value.c_str());+}++TEST_F(AuthManagerTest, VerifyAuthentication) {+ // when no keys are present at all+ ASSERT_FALSE(auth_manager.authenticate("jdlaslasdasd", "", ""));++ // wildcard permission+ api_key_t wildcard_all_key = api_key_t("abcd1", "wildcard all key", {"*"}, {"*"});+ auth_manager.create_key(wildcard_all_key);++ ASSERT_FALSE(auth_manager.authenticate("jdlaslasdasd", "documents:create", "collection1"));+ ASSERT_TRUE(auth_manager.authenticate(wildcard_all_key.value, "metrics:get", ""));++ // wildcard on a collection+ api_key_t wildcard_coll_key = api_key_t("abcd2", "wildcard coll key", {"*"}, {"collection1"});+ auth_manager.create_key(wildcard_coll_key);++ ASSERT_FALSE(auth_manager.authenticate("adasda", "documents:create", "collection1"));+ ASSERT_TRUE(auth_manager.authenticate(wildcard_coll_key.value, "documents:get", "collection1"));+ ASSERT_FALSE(auth_manager.authenticate(wildcard_coll_key.value, "documents:get", "collection2"));++ // wildcard on multiple collections+ api_key_t wildcard_colls_key = api_key_t("abcd3", "wildcard coll key", {"*"}, {"collection1", "collection2", "collection3"});+ auth_manager.create_key(wildcard_colls_key);++ ASSERT_TRUE(auth_manager.authenticate(wildcard_colls_key.value, "documents:get", "collection1"));+ ASSERT_TRUE(auth_manager.authenticate(wildcard_colls_key.value, "documents:search", "collection2"));+ ASSERT_TRUE(auth_manager.authenticate(wildcard_colls_key.value, "documents:create", "collection3"));+ ASSERT_FALSE(auth_manager.authenticate(wildcard_colls_key.value, "documents:get", "collection4"));+ ASSERT_FALSE(auth_manager.authenticate(wildcard_colls_key.value, "documents:get", "*"));++ // only 1 action on multiple collections+ api_key_t one_action_key = api_key_t("abcd4", "one action key", {"documents:search"}, {"collection1", "collection2"});+ auth_manager.create_key(one_action_key);++ ASSERT_TRUE(auth_manager.authenticate(one_action_key.value, "documents:search", "collection1"));+ ASSERT_FALSE(auth_manager.authenticate(one_action_key.value, "documents:get", "collection2"));+ ASSERT_FALSE(auth_manager.authenticate(one_action_key.value, "documents:search", "collection5"));+ ASSERT_FALSE(auth_manager.authenticate(one_action_key.value, "*", "collection2"));++ // multiple actions on multiple collections+ api_key_t mul_acoll_key = api_key_t("abcd5", "multiple action/collection key",+ {"documents:get", "collections:list"}, {"metacollection", "collection2"});+ auth_manager.create_key(mul_acoll_key);++ ASSERT_TRUE(auth_manager.authenticate(mul_acoll_key.value, "documents:get", "metacollection"));+ ASSERT_TRUE(auth_manager.authenticate(mul_acoll_key.value, "collections:list", "collection2"));+ ASSERT_TRUE(auth_manager.authenticate(mul_acoll_key.value, "collections:list", "metacollection"));+ ASSERT_FALSE(auth_manager.authenticate(mul_acoll_key.value, "documents:search", "collection2"));+ ASSERT_FALSE(auth_manager.authenticate(mul_acoll_key.value, "documents:get", "collection5"));+ ASSERT_FALSE(auth_manager.authenticate(mul_acoll_key.value, "*", "*"));+}\ No newline at end of file