mirror of
https://github.com/typesense/typesense.git
synced 2025-05-17 20:22:32 +08:00
Fix embedded params vec being empty.
This commit is contained in:
parent
69d98d2751
commit
95c60db1a2
@ -114,9 +114,10 @@ bool post_clear_cache(const std::shared_ptr<http_req>& req, const std::shared_pt
|
||||
|
||||
// Misc helpers
|
||||
|
||||
void get_collections_for_auth(std::map<std::string, std::string> &req_params, const std::string &body,
|
||||
const route_path &rpath, const std::string& req_auth_key,
|
||||
std::vector<collection_key_t> &collections);
|
||||
void get_collections_for_auth(std::map<std::string, std::string>& req_params, const std::string& body,
|
||||
const route_path& rpath, const std::string& req_auth_key,
|
||||
std::vector<collection_key_t>& collections,
|
||||
std::vector<nlohmann::json>& embedded_params_vec);
|
||||
|
||||
bool is_doc_import_route(uint64_t route_hash);
|
||||
|
||||
|
@ -148,7 +148,8 @@ bool AuthManager::authenticate(const std::string& action,
|
||||
//LOG(INFO) << "AuthManager::authenticate()";
|
||||
|
||||
size_t num_keys_matched = 0;
|
||||
for(const auto& coll_key: collection_keys) {
|
||||
for(size_t i = 0; i < collection_keys.size(); i++) {
|
||||
const auto& coll_key = collection_keys[i];
|
||||
const auto& key_it = api_keys.find(coll_key.api_key);
|
||||
nlohmann::json embedded_params;
|
||||
|
||||
@ -166,7 +167,7 @@ bool AuthManager::authenticate(const std::string& action,
|
||||
}
|
||||
|
||||
num_keys_matched++;
|
||||
embedded_params_vec.push_back(embedded_params);
|
||||
embedded_params_vec[i] = embedded_params;
|
||||
}
|
||||
|
||||
//LOG(INFO) << "api_keys.size() = " << api_keys.size();
|
||||
|
@ -25,7 +25,7 @@ bool handle_authentication(std::map<std::string, std::string>& req_params,
|
||||
|
||||
std::vector<collection_key_t> collections;
|
||||
|
||||
get_collections_for_auth(req_params, body, rpath, req_auth_key, collections);
|
||||
get_collections_for_auth(req_params, body, rpath, req_auth_key, collections, embedded_params_vec);
|
||||
|
||||
if(rpath.handler == get_health) {
|
||||
// health endpoint requires no authentication
|
||||
@ -56,9 +56,10 @@ void defer_processing(const std::shared_ptr<http_req>& req, const std::shared_pt
|
||||
server->get_message_dispatcher()->send_message(HttpServer::DEFER_PROCESSING_MESSAGE, defer);
|
||||
}
|
||||
|
||||
void get_collections_for_auth(std::map<std::string, std::string>& req_params, const std::string& body,
|
||||
const route_path& rpath, const std::string& req_auth_key,
|
||||
std::vector<collection_key_t>& collections) {
|
||||
void get_collections_for_auth(std::map<std::string, std::string>& req_params, const string& body,
|
||||
const route_path& rpath, const string& req_auth_key,
|
||||
std::vector<collection_key_t>& collections,
|
||||
std::vector<nlohmann::json>& embedded_params_vec) {
|
||||
|
||||
if(rpath.handler == post_multi_search) {
|
||||
nlohmann::json obj = nlohmann::json::parse(body, nullptr, false);
|
||||
@ -66,6 +67,7 @@ void get_collections_for_auth(std::map<std::string, std::string>& req_params, co
|
||||
if(obj.is_discarded()) {
|
||||
LOG(ERROR) << "Multi search request body is malformed.";
|
||||
collections.emplace_back("", req_auth_key);
|
||||
embedded_params_vec.emplace_back(nlohmann::json::object());
|
||||
}
|
||||
|
||||
else if(obj.count("searches") != 0 && obj["searches"].is_array()) {
|
||||
@ -83,6 +85,7 @@ void get_collections_for_auth(std::map<std::string, std::string>& req_params, co
|
||||
req_auth_key;
|
||||
|
||||
collections.emplace_back(coll_name, access_key);
|
||||
embedded_params_vec.emplace_back(nlohmann::json::object());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -96,13 +99,18 @@ void get_collections_for_auth(std::map<std::string, std::string>& req_params, co
|
||||
} else if(obj.count("name") != 0 && obj["name"].is_string()) {
|
||||
collections.emplace_back(obj["name"].get<std::string>(), req_auth_key);
|
||||
}
|
||||
|
||||
embedded_params_vec.emplace_back(nlohmann::json::object());
|
||||
|
||||
} else if(req_params.count("collection") != 0) {
|
||||
collections.emplace_back(req_params.at("collection"), req_auth_key);
|
||||
embedded_params_vec.emplace_back(nlohmann::json::object());
|
||||
}
|
||||
}
|
||||
|
||||
if(collections.empty()) {
|
||||
collections.emplace_back("", req_auth_key);
|
||||
embedded_params_vec.emplace_back(nlohmann::json::object());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -138,7 +138,7 @@ TEST_F(AuthManagerTest, CheckRestoreOfAPIKeys) {
|
||||
|
||||
TEST_F(AuthManagerTest, VerifyAuthentication) {
|
||||
std::map<std::string, std::string> sparams;
|
||||
std::vector<nlohmann::json> embedded_params;
|
||||
std::vector<nlohmann::json> embedded_params(2);
|
||||
// when no keys are present at all
|
||||
ASSERT_FALSE(auth_manager.authenticate("", {collection_key_t("", "jdlaslasdasd")}, sparams, embedded_params));
|
||||
|
||||
@ -278,7 +278,7 @@ TEST_F(AuthManagerTest, GenerationOfAPIAction) {
|
||||
TEST_F(AuthManagerTest, ScopedAPIKeys) {
|
||||
std::map<std::string, std::string> params;
|
||||
params["filter_by"] = "country:USA";
|
||||
std::vector<nlohmann::json> embedded_params;
|
||||
std::vector<nlohmann::json> embedded_params(2);
|
||||
|
||||
// create a API key bound to search scope and a given collection
|
||||
api_key_t key_search_coll1("KeyVal", "test key", {"documents:search"}, {"coll1"}, FUTURE_TS);
|
||||
@ -338,7 +338,7 @@ TEST_F(AuthManagerTest, ScopedAPIKeys) {
|
||||
auth_manager.create_key(key_expiry);
|
||||
|
||||
empty_params.clear();
|
||||
embedded_params.clear();
|
||||
embedded_params.push_back(nlohmann::json::object());
|
||||
|
||||
std::string scoped_key3 = "K1M2STRDelZYNHpxNGVWUTlBTGpOWUl4dk8wNU8xdnVEZi9aSUcvZE5tcz1FeHBpeyJmaWx0ZXJfYnkiOi"
|
||||
"AidXNlcl9pZDoxMDgwIiwgImV4cGlyZXNfYXQiOiAyMjM3NzEyMjIwfQ==";
|
||||
@ -353,6 +353,7 @@ TEST_F(AuthManagerTest, ScopedAPIKeys) {
|
||||
auth_manager.create_key(key_expiry2);
|
||||
|
||||
empty_params.clear();
|
||||
embedded_params.push_back(nlohmann::json::object());
|
||||
|
||||
std::string scoped_key4 = "SXFKNldZZWRiWkVKVmI2RCt3OTlKNHpBZ24yWlRUbEdJdERtTy9IZ2REZz1FeHBpeyJmaWx0ZXJfYnkiOiAidXN"
|
||||
"lcl9pZDoxMDgwIiwgImV4cGlyZXNfYXQiOiAxNjA2NTYzMzE2fQ==";
|
||||
@ -366,6 +367,7 @@ TEST_F(AuthManagerTest, ScopedAPIKeys) {
|
||||
auth_manager.create_key(key_expiry3);
|
||||
|
||||
empty_params.clear();
|
||||
embedded_params.push_back(nlohmann::json::object());
|
||||
|
||||
std::string scoped_key5 = "V3JMNFJlZHRMVStrZHphNFVGZDh4MWltSmx6Yzk2R3QvS2ZwSE8weGRWQT1FeHBpeyJmaWx0ZXJfYnkiOiAidX"
|
||||
"Nlcl9pZDoxMDgwIiwgImV4cGlyZXNfYXQiOiA2NDcyMzM2MzIwMH0=";
|
||||
|
@ -216,14 +216,16 @@ TEST_F(CoreAPIUtilsTest, ExtractCollectionsFromRequestBody) {
|
||||
|
||||
route_path rpath("POST", {"collections"}, post_create_collection, false, false);
|
||||
std::vector<collection_key_t> collections;
|
||||
std::vector<nlohmann::json> embedded_params_vec;
|
||||
|
||||
get_collections_for_auth(req_params, body, rpath, "foo", collections);
|
||||
get_collections_for_auth(req_params, body, rpath, "foo", collections, embedded_params_vec);
|
||||
ASSERT_EQ(1, collections.size());
|
||||
ASSERT_EQ("coll1", collections[0].collection);
|
||||
ASSERT_EQ("foo", collections[0].api_key);
|
||||
|
||||
// badly constructed collection schema body
|
||||
collections.clear();
|
||||
embedded_params_vec.clear();
|
||||
body = R"(
|
||||
{
|
||||
"name": "coll1
|
||||
@ -235,12 +237,15 @@ TEST_F(CoreAPIUtilsTest, ExtractCollectionsFromRequestBody) {
|
||||
}
|
||||
)";
|
||||
|
||||
get_collections_for_auth(req_params, body, rpath, "foo", collections);
|
||||
get_collections_for_auth(req_params, body, rpath, "foo", collections, embedded_params_vec);
|
||||
ASSERT_EQ(1, collections.size());
|
||||
ASSERT_EQ("", collections[0].collection);
|
||||
ASSERT_EQ("foo", collections[0].api_key);
|
||||
ASSERT_EQ(1, embedded_params_vec.size());
|
||||
|
||||
collections.clear();
|
||||
embedded_params_vec.clear();
|
||||
|
||||
// missing collection name
|
||||
body = R"(
|
||||
{
|
||||
@ -252,14 +257,15 @@ TEST_F(CoreAPIUtilsTest, ExtractCollectionsFromRequestBody) {
|
||||
}
|
||||
)";
|
||||
|
||||
get_collections_for_auth(req_params, body, rpath, "foo", collections);
|
||||
get_collections_for_auth(req_params, body, rpath, "foo", collections, embedded_params_vec);
|
||||
ASSERT_EQ(1, collections.size());
|
||||
ASSERT_EQ("", collections[0].collection);
|
||||
ASSERT_EQ("foo", collections[0].api_key);
|
||||
|
||||
// check for multi_search
|
||||
collections.clear();
|
||||
rpath = route_path("GET", {"collections"}, post_multi_search, false, false);
|
||||
embedded_params_vec.clear();
|
||||
rpath = route_path("POST", {"collections"}, post_multi_search, false, false);
|
||||
body = R"(
|
||||
{"searches":[
|
||||
{
|
||||
@ -272,13 +278,14 @@ TEST_F(CoreAPIUtilsTest, ExtractCollectionsFromRequestBody) {
|
||||
}
|
||||
)";
|
||||
|
||||
get_collections_for_auth(req_params, body, rpath, "foo", collections);
|
||||
get_collections_for_auth(req_params, body, rpath, "foo", collections, embedded_params_vec);
|
||||
ASSERT_EQ(1, collections.size());
|
||||
ASSERT_EQ("products", collections[0].collection);
|
||||
ASSERT_EQ("bar", collections[0].api_key);
|
||||
|
||||
// get collection for multi-search
|
||||
collections.clear();
|
||||
embedded_params_vec.clear();
|
||||
body = R"(
|
||||
{"searches":
|
||||
{
|
||||
@ -290,12 +297,13 @@ TEST_F(CoreAPIUtilsTest, ExtractCollectionsFromRequestBody) {
|
||||
}
|
||||
)";
|
||||
|
||||
get_collections_for_auth(req_params, body, rpath, "foo", collections);
|
||||
get_collections_for_auth(req_params, body, rpath, "foo", collections, embedded_params_vec);
|
||||
ASSERT_EQ(1, collections.size());
|
||||
ASSERT_EQ("", collections[0].collection);
|
||||
ASSERT_EQ("foo", collections[0].api_key);
|
||||
|
||||
collections.clear();
|
||||
embedded_params_vec.clear();
|
||||
body = R"(
|
||||
{"searches":[
|
||||
{
|
||||
@ -307,7 +315,7 @@ TEST_F(CoreAPIUtilsTest, ExtractCollectionsFromRequestBody) {
|
||||
}
|
||||
)";
|
||||
|
||||
get_collections_for_auth(req_params, body, rpath, "foo", collections);
|
||||
get_collections_for_auth(req_params, body, rpath, "foo", collections, embedded_params_vec);
|
||||
ASSERT_EQ(1, collections.size());
|
||||
ASSERT_EQ("", collections[0].collection);
|
||||
ASSERT_EQ("bar", collections[0].api_key);
|
||||
@ -318,10 +326,13 @@ TEST_F(CoreAPIUtilsTest, ExtractCollectionsFromRequestBodyExtended) {
|
||||
std::map<std::string, std::string> req_params;
|
||||
|
||||
std::vector<collection_key_t> collections;
|
||||
get_collections_for_auth(req_params, "{]", rpath_multi_search, "", collections);
|
||||
std::vector<nlohmann::json> embedded_params_vec;
|
||||
|
||||
get_collections_for_auth(req_params, "{]", rpath_multi_search, "", collections, embedded_params_vec);
|
||||
|
||||
ASSERT_EQ(1, collections.size());
|
||||
ASSERT_EQ("", collections[0].collection);
|
||||
ASSERT_EQ(1, embedded_params_vec.size());
|
||||
|
||||
nlohmann::json sample_search_body;
|
||||
sample_search_body["searches"] = nlohmann::json::array();
|
||||
@ -335,7 +346,8 @@ TEST_F(CoreAPIUtilsTest, ExtractCollectionsFromRequestBodyExtended) {
|
||||
sample_search_body["searches"].push_back(search_query);
|
||||
|
||||
collections.clear();
|
||||
get_collections_for_auth(req_params, sample_search_body.dump(), rpath_multi_search, "", collections);
|
||||
embedded_params_vec.clear();
|
||||
get_collections_for_auth(req_params, sample_search_body.dump(), rpath_multi_search, "", collections, embedded_params_vec);
|
||||
|
||||
ASSERT_EQ(2, collections.size());
|
||||
ASSERT_EQ("company1", collections[0].collection);
|
||||
@ -344,29 +356,43 @@ TEST_F(CoreAPIUtilsTest, ExtractCollectionsFromRequestBodyExtended) {
|
||||
collections.clear();
|
||||
req_params["collection"] = "foo";
|
||||
|
||||
get_collections_for_auth(req_params, sample_search_body.dump(), rpath_multi_search, "", collections);
|
||||
get_collections_for_auth(req_params, sample_search_body.dump(), rpath_multi_search, "", collections, embedded_params_vec);
|
||||
|
||||
ASSERT_EQ(2, collections.size());
|
||||
ASSERT_EQ("company1", collections[0].collection);
|
||||
ASSERT_EQ("company2", collections[1].collection);
|
||||
|
||||
collections.clear();
|
||||
embedded_params_vec.clear();
|
||||
|
||||
// when one of the search arrays don't have an explicit collection, use the collection name from req param
|
||||
sample_search_body["searches"][1].erase("collection");
|
||||
|
||||
get_collections_for_auth(req_params, sample_search_body.dump(), rpath_multi_search, "", collections);
|
||||
get_collections_for_auth(req_params, sample_search_body.dump(), rpath_multi_search, "", collections, embedded_params_vec);
|
||||
|
||||
ASSERT_EQ(2, collections.size());
|
||||
ASSERT_EQ("company1", collections[0].collection);
|
||||
ASSERT_EQ("foo", collections[1].collection);
|
||||
|
||||
collections.clear();
|
||||
embedded_params_vec.clear();
|
||||
req_params.clear();
|
||||
|
||||
route_path rpath_search = route_path("GET", {"collections", ":collection", "documents", "search"}, get_search, false, false);
|
||||
get_collections_for_auth(req_params, sample_search_body.dump(), rpath_search, "", collections);
|
||||
get_collections_for_auth(req_params, sample_search_body.dump(), rpath_search, "", collections, embedded_params_vec);
|
||||
|
||||
ASSERT_EQ(1, collections.size());
|
||||
ASSERT_EQ("", collections[0].collection);
|
||||
ASSERT_EQ(1, embedded_params_vec.size());
|
||||
|
||||
collections.clear();
|
||||
embedded_params_vec.clear();
|
||||
req_params.clear();
|
||||
req_params["collection"] = "foo";
|
||||
|
||||
get_collections_for_auth(req_params, sample_search_body.dump(), rpath_search, "", collections, embedded_params_vec);
|
||||
|
||||
ASSERT_EQ(1, collections.size());
|
||||
ASSERT_EQ("foo", collections[0].collection);
|
||||
ASSERT_EQ(1, embedded_params_vec.size());
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user