Fix edge case in initialization of auth manager.

Must be initialized only when CollectionManager is ready to be loaded.
This commit is contained in:
kishorenc 2020-08-23 12:20:23 +05:30
parent 0004b5d9e2
commit 9f83d9e0c2
3 changed files with 39 additions and 36 deletions

View File

@ -1,9 +1,40 @@
#include "auth_manager.h"
#include <openssl/evp.h>
#include <openssl/hmac.h>
constexpr const char* AuthManager::DOCUMENTS_SEARCH_ACTION;
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);
}
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);
@ -90,38 +121,6 @@ uint32_t AuthManager::get_next_api_key_id() {
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, std::map<std::string, std::string>& params) {

View File

@ -64,11 +64,14 @@ void CollectionManager::init(Store *store,
this->store = store;
this->bootstrap_auth_key = auth_key;
this->max_memory_ratio = max_memory_ratio;
auth_manager.init(store);
}
Option<bool> CollectionManager::load(const size_t init_batch_size) {
Option<bool> auth_init_op = auth_manager.init(store);
if(!auth_init_op.ok()) {
LOG(ERROR) << "Auth manager init failed, error=" << auth_init_op.error();
}
std::string next_collection_id_str;
StoreStatus next_coll_id_status = store->get(NEXT_COLLECTION_ID_KEY, next_collection_id_str);

View File

@ -953,7 +953,8 @@ bool get_keys(http_req &req, http_res &res) {
nlohmann::json res_json;
res_json["keys"] = nlohmann::json::array();
for(const auto & key: keys_op.get()) {
const std::vector<api_key_t>& keys = keys_op.get();
for(const auto & key: keys) {
nlohmann::json key_obj = key.to_json();
key_obj["value_prefix"] = key_obj["value"];
key_obj.erase("value");