From 70fc895fbe7aee24736ef09440d68811a267e0b1 Mon Sep 17 00:00:00 2001 From: frikilax Date: Tue, 23 Mar 2021 12:26:05 +0100 Subject: [PATCH 1/4] FBUFFER::FIXED:: should check if key exists and is a SET not a LIST --- samples/fbuffer/Connectors/AConnector.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/fbuffer/Connectors/AConnector.cpp b/samples/fbuffer/Connectors/AConnector.cpp index a4c7614..60c6af4 100644 --- a/samples/fbuffer/Connectors/AConnector.cpp +++ b/samples/fbuffer/Connectors/AConnector.cpp @@ -69,7 +69,7 @@ bool AConnector::PrepareKeysInRedis(){ DARWIN_LOG_DEBUG("AConnector::PrepareKeysInRedis:: key '"+ redis_list + "' is '" + reply + "'"); if(reply != "none") { - if(reply != "list") { + if(reply != "set") { DARWIN_LOG_ERROR("AConnector::PrepareKeysInRedis:: key '" + redis_list + "' is already present " "but seems to be used for something else, " "cannot start the filter and risk overriding data in Redis"); From 42420bf2de63a848c47da41532aec607993929eb Mon Sep 17 00:00:00 2001 From: frikilax Date: Tue, 23 Mar 2021 15:12:10 +0100 Subject: [PATCH 2/4] REDIS_MANAGER::FIXED:: TestIsMaster(ip, port) logic was inverted --- toolkit/RedisManager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toolkit/RedisManager.cpp b/toolkit/RedisManager.cpp index 0b28392..613be53 100644 --- a/toolkit/RedisManager.cpp +++ b/toolkit/RedisManager.cpp @@ -84,7 +84,7 @@ namespace darwin { if(ConnectAddress(ip, port, &context)) { reply = GetContextRole(context); - if(reply and strncmp(reply->element[0]->str, "master", 6)) { + if(reply and strncmp(reply->element[0]->str, "master", 6) == 0) { ret = true; } freeReplyObject(reply); From 2a1ff1a2e52e07b7e660631300e29b4c7dec7aa9 Mon Sep 17 00:00:00 2001 From: frikilax Date: Wed, 24 Mar 2021 08:09:57 +0000 Subject: [PATCH 3/4] REDIS::ADDED: Parameters to connect to Redis via IP --- samples/base/AlertManager.cpp | 74 ++++++++++++++++++++++------- samples/base/AlertManager.hpp | 19 +++++++- samples/fbuffer/Generator.cpp | 56 ++++++++++++++++------ samples/fconnection/Generator.cpp | 52 ++++++++++++++++----- samples/fconnection/Generator.hpp | 2 + samples/fend/Generator.cpp | 42 +++++++++++++---- samples/fsession/Generator.cpp | 43 +++++++++++++---- samples/ftanomaly/Generator.cpp | 51 ++++++++++++++++---- samples/ftest/Generator.cpp | 77 +++++++++++++++++++++---------- samples/ftest/Generator.hpp | 2 +- 10 files changed, 323 insertions(+), 95 deletions(-) diff --git a/samples/base/AlertManager.cpp b/samples/base/AlertManager.cpp index 63db0c5..47f5e36 100644 --- a/samples/base/AlertManager.cpp +++ b/samples/base/AlertManager.cpp @@ -24,12 +24,12 @@ namespace darwin { bool redis_status = false; bool logs_status = false; - _redis = configuration.HasMember("redis_socket_path"); + _redis = configuration.HasMember("redis_socket_path") or configuration.HasMember("redis_ip"); _log = configuration.HasMember("log_file_path"); if (!_redis and !_log){ DARWIN_LOG_WARNING("AlertManager:: Need at least one of these parameters to raise alerts: " - "'redis_socket_path' or 'log_file_path'"); + "'redis_socket_path', 'redis_ip' or 'log_file_path'"); return false; } @@ -65,27 +65,42 @@ namespace darwin { bool AlertManager::LoadRedisConfig(const rapidjson::Document& configuration) { DARWIN_LOGGER; - std::string redis_socket_path; + std::string redis_socket_path, redis_ip; + unsigned int redis_port = 6379; GetStringField(configuration, "redis_socket_path", redis_socket_path); - if (redis_socket_path.empty()) { - DARWIN_LOG_WARNING("AlertManager:: 'redis_socket_path' needs to be a non-empty string. Ignoring REDIS configuration..."); - _redis = false; // Error configuring redis, disabling... - return false; - } + GetStringField(configuration, "redis_ip", redis_ip); + GetUIntField(configuration, "redis_port", redis_port); GetStringField(configuration, "alert_redis_list_name", this->_redis_list_name); GetStringField(configuration, "alert_redis_channel_name", this->_redis_channel_name); - if(_redis_list_name.empty() and _redis_channel_name.empty()) { + + if(this->_redis_list_name.empty() and this->_redis_channel_name.empty()) { DARWIN_LOG_WARNING("AlertManager:: if 'redis_socket_path' is provided, you need to provide at least" " 'alert_redis_list_name' or 'alert_redis_channel_name'." "Unable to configure REDIS. Ignoring REDIS configuration..."); _redis = false; // Error configuring redis, disabling... return false; } - if(!ConfigRedis(redis_socket_path)){ - DARWIN_LOG_WARNING("AlertManager:: Error when configuring REDIS. Ignoring REDIS configuration..."); - _redis = false; // Error configuring redis, disabling... - return false; + + if(not redis_socket_path.empty()){ + if (not ConfigRedis(redis_socket_path)) { + DARWIN_LOG_WARNING("AlertManager:: Error when configuring REDIS using '" + redis_socket_path + + "', ignoring REDIS configuration."); + _redis = false; // Error configuring redis, disabling... + return false; + } + } else if (not redis_ip.empty()) { + if (not ConfigRedis(redis_ip, redis_port)) { + DARWIN_LOG_WARNING("AlertManager:: Error when configuring REDIS using '" + redis_ip + ":" + + std::to_string(redis_port) + "', ignoring REDIS configuration."); + _redis = false; // Error configuring redis, disabling... + return false; + } + } else { + DARWIN_LOG_WARNING("AlertManager:: No 'redis_socket_path' or 'redis_ip' in configuration, " + "ignoring REDIS configuration."); + _redis = false; // Error configuring redis, disabling... + return false; } return true; } @@ -101,14 +116,32 @@ namespace darwin { DARWIN_LOG_INFO(std::string("AlertManager:: '") + field_name + "' set to " + var); return true; } else { - DARWIN_LOG_WARNING(std::string("AlertManager:: '") + field_name + "' needs to be a non-empty string." - "Ignoring this field..."); + DARWIN_LOG_WARNING(std::string("AlertManager:: '") + field_name + "' needs to be a non-empty string, " + "ignoring this field."); + } + } + return false; + } + + bool AlertManager::GetUIntField(const rapidjson::Document& configuration, + const char* const field_name, + unsigned int& var) { + DARWIN_LOGGER; + + if (configuration.HasMember(field_name)){ + if (configuration[field_name].IsUint()) { + var = configuration[field_name].GetUint(); + DARWIN_LOG_INFO(std::string("AlertManager:: '") + field_name + "' set to " + std::to_string(var)); + return true; + } else { + DARWIN_LOG_WARNING(std::string("AlertManager:: '") + field_name + "' needs to be an integer, " + "ignoring this field."); } } return false; } - bool AlertManager::ConfigRedis(std::string redis_socket_path) { + bool AlertManager::ConfigRedis(const std::string redis_socket_path) { DARWIN_LOGGER; DARWIN_LOG_DEBUG("AlertManager:: Redis configuration..."); @@ -117,6 +150,15 @@ namespace darwin { return redis.FindAndConnect(); } + bool AlertManager::ConfigRedis(const std::string redis_ip, const int redis_port) { + DARWIN_LOGGER; + DARWIN_LOG_DEBUG("AlertManager:: Redis configuration..."); + + darwin::toolkit::RedisManager& redis = darwin::toolkit::RedisManager::GetInstance(); + redis.SetIpConnection(redis_ip, redis_port); + return redis.FindAndConnect(); + } + void AlertManager::Alert(const std::string& str) { if (str.length() <= 0) return; diff --git a/samples/base/AlertManager.hpp b/samples/base/AlertManager.hpp index 35b8a47..855b5d8 100644 --- a/samples/base/AlertManager.hpp +++ b/samples/base/AlertManager.hpp @@ -112,17 +112,32 @@ namespace darwin { /// \brief Configure the RedisManager. /// \param redis_socket_path The path to the redis socket used for alerting /// \return True on success, false on error - bool ConfigRedis(std::string redis_socket_path); + bool ConfigRedis(const std::string redis_socket_path); + + /// \brief Configure the RedisManager. + /// \param redis_ip The ip to the redis node used for alerting + /// \param redis_port The port to the redis node used for alerting + /// \return True on success, false on error + bool ConfigRedis(const std::string redis_ip, const int redis_port); /// \brief Check and extract `field_name' of `configuration'. `field_name' *MUST* be a string. /// \param configuration The configurations a json object. /// \param field_name The field name. Creating a string here would be pointless. - /// \param var The string object ot fill with the extracted value. Initial value is not modified on error. + /// \param var The string object to fill with the extracted value. Initial value is not modified on error. /// \return False on error, true on success. Logs appropriately. static bool GetStringField(const rapidjson::Document& configuration, const char* const field_name, std::string& var); + /// \brief Check and extract `field_name' of `configuration'. `field_name' *MUST* be an unsigned integer. + /// \param configuration The configurations a json object. + /// \param field_name The field name. Creating a string here would be pointless. + /// \param var The unsigned integer object to fill with the extracted value. Initial value is not modified on error. + /// \return False on error, true on success. Logs appropriately. + static bool GetUIntField(const rapidjson::Document& configuration, + const char* const field_name, + unsigned int& var); + /// \brief Write the logs in file /// \return True on success, false on error bool WriteLogs(const std::string& logs); diff --git a/samples/fbuffer/Generator.cpp b/samples/fbuffer/Generator.cpp index e86692d..41461a7 100644 --- a/samples/fbuffer/Generator.cpp +++ b/samples/fbuffer/Generator.cpp @@ -22,8 +22,8 @@ bool Generator::ConfigureNetworkObject(boost::asio::io_context &context) { for (auto &output : this->_output_configs) { std::shared_ptr newOutput = _CreateOutput(context, output); if (newOutput == nullptr) { - DARWIN_LOG_WARNING("Generator::ConfigureNetworkObject:: Unable to create the Output. Output " + - output._filter_type + " ignored."); + DARWIN_LOG_WARNING("Generator::ConfigureNetworkObject:: Unable to create the Output. Output " + + output._filter_type + " ignored."); continue; } this->_outputs.push_back(newOutput); @@ -51,20 +51,48 @@ bool Generator::ConfigureNetworkObject(boost::asio::io_context &context) { bool Generator::LoadConfig(const rapidjson::Document &configuration) { DARWIN_LOGGER; + std::string redis_socket_path, redis_ip; + unsigned int redis_port = 6379; DARWIN_LOG_DEBUG("Generator::LoadConfig:: Loading classifier..."); - if (not configuration.HasMember("redis_socket_path")) { - DARWIN_LOG_CRITICAL("Generator::LoadConfig:: 'redis_socket_path' parameter missing, mandatory"); - return false; + if(configuration.HasMember("redis_socket_path")) { + if (not configuration["redis_socket_path"].IsString()) { + DARWIN_LOG_CRITICAL("Generator::LoadConfig:: 'redis_socket_path' needs to be a string"); + return false; + } else { + redis_socket_path = configuration["redis_socket_path"].GetString(); + } } - if (not configuration["redis_socket_path"].IsString()) { - DARWIN_LOG_CRITICAL("Generator::LoadConfig:: 'redis_socket_path' needs to be a string"); - return false; + + if(configuration.HasMember("redis_ip")) { + if (not configuration["redis_ip"].IsString()) { + DARWIN_LOG_CRITICAL("Generator::LoadConfig:: 'redis_ip' needs to be a string"); + return false; + } else { + redis_ip = configuration["redis_ip"].GetString(); + } + } + + if(configuration.HasMember("redis_port")) { + if (not configuration["redis_port"].IsUint()) { + DARWIN_LOG_CRITICAL("Generator::LoadConfig:: 'redis_port' needs to be an unsigned integer"); + return false; + } else { + redis_port = configuration["redis_port"].GetUint(); + } } - std::string redis_socket_path = configuration["redis_socket_path"].GetString(); darwin::toolkit::RedisManager& redis = darwin::toolkit::RedisManager::GetInstance(); - redis.SetUnixConnection(redis_socket_path); + if (not redis_socket_path.empty()) { + redis.SetUnixConnection(redis_socket_path); + } else if (not redis_ip.empty()) { + redis.SetIpConnection(redis_ip, redis_port); + } else { + DARWIN_LOG_CRITICAL("Generator::LoadConfig:: no valid way to connect to Redis, " + "please set 'redis_socket_path' or 'redis_ip' (and optionally 'redis_port')."); + return false; + } + // Done in AlertManager before arriving here, but will allow better transition from redis singleton if(not redis.FindAndConnect()) { DARWIN_LOG_CRITICAL("Generator::LoadConfig:: Could not connect to a redis!"); @@ -187,14 +215,14 @@ bool Generator::LoadOutputs(const rapidjson::Value &array) { if (not array[i].HasMember("filter_type") or not array[i]["filter_type"].IsString()) { DARWIN_LOG_WARNING("Generator::LoadOutputs:: 'filter_type' field missing or is not a string in outputs. Output ignored."); continue; - } + } if (not array[i].HasMember("filter_socket_path") or not array[i]["filter_socket_path"].IsString()) { - DARWIN_LOG_WARNING("Generator::LoadOutputs:: 'filter_socket_path' field missing or is not a string in outputs. Output '" + + DARWIN_LOG_WARNING("Generator::LoadOutputs:: 'filter_socket_path' field missing or is not a string in outputs. Output '" + std::string(array[i]["filter_type"].GetString()) + "' ignored."); continue; - } + } if (not array[i].HasMember("interval") or not array[i]["interval"].IsInt64()) { - DARWIN_LOG_WARNING("Generator::LoadOutputs:: 'interval' field missing or is not an integer in outputs. Output '" + + DARWIN_LOG_WARNING("Generator::LoadOutputs:: 'interval' field missing or is not an integer in outputs. Output '" + std::string(array[i]["filter_type"].GetString()) + "' ignored."); continue; } diff --git a/samples/fconnection/Generator.cpp b/samples/fconnection/Generator.cpp index a3e7bd6..ad75a7d 100644 --- a/samples/fconnection/Generator.cpp +++ b/samples/fconnection/Generator.cpp @@ -35,20 +35,35 @@ bool Generator::LoadConfig(const rapidjson::Document &configuration) { DARWIN_LOGGER; DARWIN_LOG_DEBUG("ConnectionSupervision:: Generator:: Loading classifier..."); - std::string redis_socket_path; - std::string init_data_path; + std::string redis_socket_path, redis_ip, init_data_path; + unsigned int redis_port = 6379; - if (!configuration.HasMember("redis_socket_path")) { - DARWIN_LOG_CRITICAL("ConnectionSupervision:: Generator:: Missing parameter: 'redis_socket_path'"); - return false; + if(configuration.HasMember("redis_socket_path")) { + if (not configuration["redis_socket_path"].IsString()) { + DARWIN_LOG_CRITICAL("ConnectionSupervision:: Generator:: 'redis_socket_path' needs to be a string"); + return false; + } else { + redis_socket_path = configuration["redis_socket_path"].GetString(); + } } - if (!configuration["redis_socket_path"].IsString()) { - DARWIN_LOG_CRITICAL("ConnectionSupervision:: Generator:: 'redis_socket_path' needs to be a string"); - return false; + if(configuration.HasMember("redis_ip")) { + if (not configuration["redis_ip"].IsString()) { + DARWIN_LOG_CRITICAL("ConnectionSupervision:: Generator:: 'redis_ip' needs to be a string"); + return false; + } else { + redis_ip = configuration["redis_ip"].GetString(); + } } - redis_socket_path = configuration["redis_socket_path"].GetString(); + if(configuration.HasMember("redis_port")) { + if (not configuration["redis_port"].IsUint()) { + DARWIN_LOG_CRITICAL("ConnectionSupervision:: Generator:: 'redis_port' needs to be an unsigned integer"); + return false; + } else { + redis_port = configuration["redis_port"].GetUint(); + } + } if (configuration.HasMember("init_data_path")) { if (!configuration["init_data_path"].IsString()) { @@ -70,10 +85,14 @@ bool Generator::LoadConfig(const rapidjson::Document &configuration) { _redis_expire = configuration["redis_expire"].GetUint(); } - return ConfigRedis(redis_socket_path, init_data_path); + return ConfigRedis(redis_socket_path, redis_ip, redis_port, init_data_path); } -bool Generator::ConfigRedis(const std::string &redis_socket_path, const std::string &init_data_path) { +bool Generator::ConfigRedis( + const std::string &redis_socket_path, + const std::string &redis_ip, + unsigned int redis_port, + const std::string &init_data_path) { DARWIN_LOGGER; std::ifstream init_data_stream; @@ -82,7 +101,16 @@ bool Generator::ConfigRedis(const std::string &redis_socket_path, const std::str darwin::toolkit::RedisManager& redis = darwin::toolkit::RedisManager::GetInstance(); // Done in AlertManager before arriving here, but will allow better transition from redis singleton - redis.SetUnixConnection(redis_socket_path); + if (not redis_socket_path.empty()) { + redis.SetUnixConnection(redis_socket_path); + } else if (not redis_ip.empty()) { + redis.SetIpConnection(redis_ip, redis_port); + } else { + DARWIN_LOG_ERROR("ConnectionSupervision::Generator::ConfigureRedis:: no valid way to connect to Redis, " + "please set 'redis_socket_path' or 'redis_ip' (and optionally 'redis_port')."); + return false; + } + if(not redis.FindAndConnect()) { DARWIN_LOG_ERROR("ConnectionSupervision::Generator::ConfigureRedis:: Could not configure Redis connection."); return false; diff --git a/samples/fconnection/Generator.hpp b/samples/fconnection/Generator.hpp index 7a5762a..f0f82a4 100644 --- a/samples/fconnection/Generator.hpp +++ b/samples/fconnection/Generator.hpp @@ -32,6 +32,8 @@ class Generator: public AGenerator { private: bool ConfigRedis(const std::string &redis_socket_path, + const std::string &redis_ip, + unsigned int redis_port, const std::string &init_data_path); unsigned int _redis_expire = 0; diff --git a/samples/fend/Generator.cpp b/samples/fend/Generator.cpp index 9faed1f..9b8f2ee 100644 --- a/samples/fend/Generator.cpp +++ b/samples/fend/Generator.cpp @@ -59,21 +59,47 @@ bool Generator::LoadClassifier(const rapidjson::Document &configuration) { DARWIN_LOGGER; DARWIN_LOG_DEBUG("End:: Generator:: Loading classifier..."); - if (!configuration.HasMember("redis_socket_path")) { - DARWIN_LOG_CRITICAL("End:: Generator:: Missing parameter: 'redis_socket_path'"); - return false; + std::string redis_socket_path, redis_ip; + unsigned int redis_port = 6379; + + if(configuration.HasMember("redis_socket_path")) { + if (not configuration["redis_socket_path"].IsString()) { + DARWIN_LOG_CRITICAL("End:: Generator:: 'redis_socket_path' needs to be a string"); + return false; + } else { + redis_socket_path = configuration["redis_socket_path"].GetString(); + } } - if (!configuration["redis_socket_path"].IsString()) { - DARWIN_LOG_CRITICAL("End:: Generator:: 'redis_socket_path' needs to be a string"); - return false; + if(configuration.HasMember("redis_ip")) { + if (not configuration["redis_ip"].IsString()) { + DARWIN_LOG_CRITICAL("End:: Generator:: 'redis_ip' needs to be a string"); + return false; + } else { + redis_ip = configuration["redis_ip"].GetString(); + } } - std::string redis_socket_path = configuration["redis_socket_path"].GetString(); + if(configuration.HasMember("redis_port")) { + if (not configuration["redis_port"].IsUint()) { + DARWIN_LOG_CRITICAL("End:: Generator:: 'redis_port' needs to be an unsigned integer"); + return false; + } else { + redis_port = configuration["redis_port"].GetUint(); + } + } darwin::toolkit::RedisManager& redis = darwin::toolkit::RedisManager::GetInstance(); // Done in AlertManager before arriving here, but will allow better transition from redis singleton - redis.SetUnixConnection(redis_socket_path); + if (not redis_socket_path.empty()) { + redis.SetUnixConnection(redis_socket_path); + } else if (not redis_ip.empty()) { + redis.SetIpConnection(redis_ip, redis_port); + } else { + DARWIN_LOG_ERROR("End:: Generator:: no valid way to connect to Redis, " + "please set 'redis_socket_path' or 'redis_ip' (and optionally 'redis_port')."); + return false; + } return redis.FindAndConnect(); } diff --git a/samples/fsession/Generator.cpp b/samples/fsession/Generator.cpp index 61d23e4..f561547 100644 --- a/samples/fsession/Generator.cpp +++ b/samples/fsession/Generator.cpp @@ -34,22 +34,47 @@ bool Generator::LoadConfig(const rapidjson::Document &configuration) { DARWIN_LOGGER; DARWIN_LOG_DEBUG("Session:: Generator:: Loading configuration..."); - std::string redis_socket_path; + std::string redis_socket_path, redis_ip; + unsigned int redis_port = 6379; - if (!configuration.HasMember("redis_socket_path")) { - DARWIN_LOG_CRITICAL("Session:: Generator:: Missing parameter: 'redis_socket_path'"); - return false; + if(configuration.HasMember("redis_socket_path")) { + if (not configuration["redis_socket_path"].IsString()) { + DARWIN_LOG_CRITICAL("Session:: Generator:: 'redis_socket_path' needs to be a string"); + return false; + } else { + redis_socket_path = configuration["redis_socket_path"].GetString(); + } } - if (!configuration["redis_socket_path"].IsString()) { - DARWIN_LOG_CRITICAL("Session:: Generator:: 'redis_socket_path' needs to be a string"); - return false; + if(configuration.HasMember("redis_ip")) { + if (not configuration["redis_ip"].IsString()) { + DARWIN_LOG_CRITICAL("Session:: Generator:: 'redis_ip' needs to be a string"); + return false; + } else { + redis_ip = configuration["redis_ip"].GetString(); + } + } + + if(configuration.HasMember("redis_port")) { + if (not configuration["redis_port"].IsUint()) { + DARWIN_LOG_CRITICAL("Session:: Generator:: 'redis_port' needs to be an unsigned integer"); + return false; + } else { + redis_port = configuration["redis_port"].GetUint(); + } } - redis_socket_path = configuration["redis_socket_path"].GetString(); darwin::toolkit::RedisManager& redis = darwin::toolkit::RedisManager::GetInstance(); // Done in AlertManager before arriving here, but will allow better transition from redis singleton - redis.SetUnixConnection(redis_socket_path); + if (not redis_socket_path.empty()) { + redis.SetUnixConnection(redis_socket_path); + } else if (not redis_ip.empty()) { + redis.SetIpConnection(redis_ip, redis_port); + } else { + DARWIN_LOG_ERROR("Session:: Generator:: no valid way to connect to Redis, " + "please set 'redis_socket_path' or 'redis_ip' (and optionally 'redis_port')."); + return false; + } return redis.FindAndConnect(); } diff --git a/samples/ftanomaly/Generator.cpp b/samples/ftanomaly/Generator.cpp index 1f1da82..870d8d9 100644 --- a/samples/ftanomaly/Generator.cpp +++ b/samples/ftanomaly/Generator.cpp @@ -34,22 +34,52 @@ bool Generator::ConfigureAlerting(const std::string& tags) { bool Generator::LoadConfig(const rapidjson::Document &configuration) { DARWIN_LOGGER; DARWIN_LOG_DEBUG("TAnomaly:: Generator:: Loading configuration..."); + + std::string redis_socket_path, redis_ip; + unsigned int redis_port = 6379; _redis_internal = darwin::Core::instance().GetFilterName() + REDIS_INTERNAL_LIST; // Generating name for the internaly used redis set bool start_detection_thread = false; //whether to start the anomaly thread unsigned int detection_frequency = 300; //detection thread launching frequency in seconds - darwin::toolkit::RedisManager& redis = darwin::toolkit::RedisManager::GetInstance(); - if(not configuration.HasMember("redis_socket_path")) { - DARWIN_LOG_CRITICAL("TAnomaly::Generator:: 'redis_socket_path' parameter missing, mandatory"); - return false; + if(configuration.HasMember("redis_socket_path")) { + if (not configuration["redis_socket_path"].IsString()) { + DARWIN_LOG_CRITICAL("TAnomaly:: Generator:: 'redis_socket_path' needs to be a string"); + return false; + } else { + redis_socket_path = configuration["redis_socket_path"].GetString(); + } + } + + if(configuration.HasMember("redis_ip")) { + if (not configuration["redis_ip"].IsString()) { + DARWIN_LOG_CRITICAL("TAnomaly:: Generator:: 'redis_ip' needs to be a string"); + return false; + } else { + redis_ip = configuration["redis_ip"].GetString(); + } + } + + if(configuration.HasMember("redis_port")) { + if (not configuration["redis_port"].IsUint()) { + DARWIN_LOG_CRITICAL("TAnomaly:: Generator:: 'redis_port' needs to be an unsigned integer"); + return false; + } else { + redis_port = configuration["redis_port"].GetUint(); + } } - if (not configuration["redis_socket_path"].IsString()) { - DARWIN_LOG_CRITICAL("TAnomaly:: Generator:: 'redis_socket_path' needs to be a string"); + + darwin::toolkit::RedisManager& redis = darwin::toolkit::RedisManager::GetInstance(); + // Done in AlertManager before arriving here, but will allow better transition from redis singleton + if (not redis_socket_path.empty()) { + redis.SetUnixConnection(redis_socket_path); + } else if (not redis_ip.empty()) { + redis.SetIpConnection(redis_ip, redis_port); + } else { + DARWIN_LOG_ERROR("TAnomaly:: Generator:: no valid way to connect to Redis, " + "please set 'redis_socket_path' or 'redis_ip' (and optionally 'redis_port')."); return false; } - std::string redis_socket_path = configuration["redis_socket_path"].GetString(); - redis.SetUnixConnection(redis_socket_path); // Done in AlertManager before arriving here, but will allow better transition from redis singleton if(not redis.FindAndConnect()) { DARWIN_LOG_CRITICAL("TAnomaly:: Generator:: Could not connect to a redis!"); @@ -57,7 +87,10 @@ bool Generator::LoadConfig(const rapidjson::Document &configuration) { } // start detection thread if the local redis server is a master - if(darwin::toolkit::RedisManager::TestIsMaster(redis_socket_path)) { + if(not redis_socket_path.empty() and darwin::toolkit::RedisManager::TestIsMaster(redis_socket_path)) { + DARWIN_LOG_INFO("TAnomaly::Generator:: direct connection to redis master found, detection thread will be started"); + start_detection_thread = true; + } else if (not redis_ip.empty() and darwin::toolkit::RedisManager::TestIsMaster(redis_ip, redis_port)) { DARWIN_LOG_INFO("TAnomaly::Generator:: direct connection to redis master found, detection thread will be started"); start_detection_thread = true; } diff --git a/samples/ftest/Generator.cpp b/samples/ftest/Generator.cpp index 191b949..e9c6b91 100644 --- a/samples/ftest/Generator.cpp +++ b/samples/ftest/Generator.cpp @@ -34,48 +34,77 @@ bool Generator::ConfigureAlerting(const std::string& tags) { bool Generator::LoadConfig(const rapidjson::Document &configuration) { DARWIN_LOGGER; DARWIN_LOG_DEBUG("Test:: Generator:: Loading Configuration..."); - std::string redis_socket_path; + std::string redis_socket_path, redis_ip; + unsigned int redis_port = 6379; if (configuration.HasMember("fail_config")) return false; + if(configuration.HasMember("redis_socket_path")) { - if (configuration["redis_socket_path"].IsString()) { + if (not configuration["redis_socket_path"].IsString()) { + DARWIN_LOG_WARNING("Test:: Generator:: 'redis_socket_path' needs to be a string"); + } else { redis_socket_path = configuration["redis_socket_path"].GetString(); + } + } - if (configuration.HasMember("redis_list_name")){ - if (configuration["redis_list_name"].IsString()) { - _redis_list_name = configuration["redis_list_name"].GetString(); - DARWIN_LOG_INFO("Test:: Generator:: 'redis_list_name' set to " + _redis_list_name); - } - } - - if (configuration.HasMember("redis_channel_name")){ - if (configuration["redis_channel_name"].IsString()) { - _redis_channel_name = configuration["redis_channel_name"].GetString(); - DARWIN_LOG_INFO("Test:: Generator:: 'redis_channel_name' set to " + _redis_channel_name); - } - } - - if(not ConfigRedis(redis_socket_path)){ - DARWIN_LOG_WARNING("Test:: Generator:: Error when configuring REDIS"); - return true; - } + if(configuration.HasMember("redis_ip")) { + if (not configuration["redis_ip"].IsString()) { + DARWIN_LOG_WARNING("Test:: Generator:: 'redis_ip' needs to be a string"); + } else { + redis_ip = configuration["redis_ip"].GetString(); } - else { - DARWIN_LOG_WARNING("Test:: Generator:: 'redis_socket_path' needs to be a string, ignoring"); + } + + if(configuration.HasMember("redis_port")) { + if (not configuration["redis_port"].IsUint()) { + DARWIN_LOG_WARNING("Test:: Generator:: 'redis_port' needs to be an unsigned integer"); + } else { + redis_port = configuration["redis_port"].GetUint(); } } + if (configuration.HasMember("redis_list_name")){ + if (configuration["redis_list_name"].IsString()) { + _redis_list_name = configuration["redis_list_name"].GetString(); + DARWIN_LOG_INFO("Test:: Generator:: 'redis_list_name' set to " + _redis_list_name); + } + } + + if (configuration.HasMember("redis_channel_name")){ + if (configuration["redis_channel_name"].IsString()) { + _redis_channel_name = configuration["redis_channel_name"].GetString(); + DARWIN_LOG_INFO("Test:: Generator:: 'redis_channel_name' set to " + _redis_channel_name); + } + } + + + if(not ConfigRedis(redis_socket_path, redis_ip, redis_port)){ + DARWIN_LOG_WARNING("Test:: Generator:: Error when configuring REDIS"); + return true; + } + return true; } -bool Generator::ConfigRedis(std::string redis_socket_path) { +bool Generator::ConfigRedis( + const std::string& redis_socket_path, + const std::string& redis_ip, + unsigned int redis_port) { DARWIN_LOGGER; DARWIN_LOG_DEBUG("Test:: Generator:: Redis configuration..."); darwin::toolkit::RedisManager& redis = darwin::toolkit::RedisManager::GetInstance(); // Done in AlertManager before arriving here, but will allow better transition from redis singleton - redis.SetUnixConnection(redis_socket_path); + if (not redis_socket_path.empty()) { + redis.SetUnixConnection(redis_socket_path); + } else if (not redis_ip.empty()) { + redis.SetIpConnection(redis_ip, redis_port); + } else { + DARWIN_LOG_ERROR("Test:: Generator:: no valid way to connect to Redis, " + "please set 'redis_socket_path' or 'redis_ip' (and optionally 'redis_port')."); + return false; + } return redis.FindAndConnect(); } diff --git a/samples/ftest/Generator.hpp b/samples/ftest/Generator.hpp index 46dd6f6..879449f 100644 --- a/samples/ftest/Generator.hpp +++ b/samples/ftest/Generator.hpp @@ -32,7 +32,7 @@ class Generator: public AGenerator { virtual bool ConfigureAlerting(const std::string& tags) override final; private: - bool ConfigRedis(std::string redis_socket_path); + bool ConfigRedis(const std::string& redis_socket_path, const std::string& redis_ip, unsigned int redis_port); std::string _redis_list_name; std::string _redis_channel_name; From ff96cae0f2ccb06291a05020d9d8d09059de45ee Mon Sep 17 00:00:00 2001 From: frikilax Date: Wed, 24 Mar 2021 08:10:41 +0000 Subject: [PATCH 4/4] TESTS::Fixed: 'check_start_stop' did not check filter running properly --- tests/core/base.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/core/base.py b/tests/core/base.py index 61cc0b3..76bc6f7 100644 --- a/tests/core/base.py +++ b/tests/core/base.py @@ -37,10 +37,9 @@ def check_start_stop(): filter.configure(FTEST_CONFIG) filter.valgrind_start() - try: - kill(filter.process.pid, 0) - except OSError as e: - logging.error("check_start_stop: Process {} not running: {}".format(filter.process.pid, e)) + + if not filter.check_run(): + logging.error("check_start_stop: Process {} not running".format(filter.process.pid)) return False if filter.stop() is not True: