Skip to content

Commit

Permalink
fix(broker/lua): Several fix in the lua Stream
Browse files Browse the repository at this point in the history
* Legacy OpenSSL md5 function replaced by the OpenSSL 3 version
* improvement of hostgroups cache in streamconnector
* improvement of servicegroups cache in streamconnector

REFS: MON-34072 MON-15480
  • Loading branch information
bouda1 authored and sechkem committed Sep 5, 2024
1 parent d18fa46 commit e59698b
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 63 deletions.
17 changes: 15 additions & 2 deletions broker/lua/inc/com/centreon/broker/lua/macro_cache.hh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "com/centreon/broker/neb/host_group.hh"
#include "com/centreon/broker/neb/host_group_member.hh"
#include "com/centreon/broker/neb/instance.hh"
#include "com/centreon/broker/neb/internal.hh"
#include "com/centreon/broker/neb/service.hh"
#include "com/centreon/broker/neb/service_group.hh"
#include "com/centreon/broker/neb/service_group_member.hh"
Expand All @@ -42,14 +43,26 @@ class macro_cache {
std::shared_ptr<persistent_cache> _cache;
absl::flat_hash_map<uint64_t, std::shared_ptr<io::data>> _instances;
absl::flat_hash_map<uint64_t, std::shared_ptr<io::data>> _hosts;
absl::flat_hash_map<uint64_t, std::shared_ptr<io::data>> _host_groups;
/* The host groups cache stores also a set with the pollers telling they need
* the cache. So if no more poller needs a host group, we can remove it from
* the cache. */
absl::flat_hash_map<uint64_t,
std::pair<std::shared_ptr<neb::pb_host_group>,
absl::flat_hash_set<uint32_t>>>
_host_groups;
absl::btree_map<std::pair<uint64_t, uint64_t>, std::shared_ptr<io::data>>
_host_group_members;
absl::flat_hash_map<std::pair<uint64_t, uint64_t>, std::shared_ptr<io::data>>
_custom_vars;
absl::flat_hash_map<std::pair<uint64_t, uint64_t>, std::shared_ptr<io::data>>
_services;
absl::flat_hash_map<uint64_t, std::shared_ptr<io::data>> _service_groups;
/* The service groups cache stores also a set with the pollers telling they
* need the cache. So if no more poller needs a service group, we can remove
* it from the cache. */
absl::flat_hash_map<uint64_t,
std::pair<std::shared_ptr<neb::pb_service_group>,
absl::flat_hash_set<uint32_t>>>
_service_groups;
absl::btree_map<std::tuple<uint64_t, uint64_t, uint64_t>,
std::shared_ptr<io::data>>
_service_group_members;
Expand Down
39 changes: 34 additions & 5 deletions broker/lua/src/broker_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "absl/strings/string_view.h"
#include "com/centreon/broker/config/applier/state.hh"

#include <openssl/md5.h>
#include <openssl/evp.h>
#include <cstdlib>
#include <cstring>
#include <iomanip>
Expand Down Expand Up @@ -810,6 +810,34 @@ static int l_broker_stat(lua_State* L) {
}
}

static void md5_message(const unsigned char* message,
size_t message_len,
unsigned char** digest,
unsigned int* digest_len) {
EVP_MD_CTX* mdctx;
auto handle_error = [](const std::string& msg) {
auto logger = log_v2::instance().get(log_v2::LUA);
logger->error(msg);
};
if ((mdctx = EVP_MD_CTX_new()) == nullptr) {
handle_error("lua: fail to call MD5 (EVP_MD_CTX_new call)");
}
if (1 != EVP_DigestInit_ex(mdctx, EVP_md5(), nullptr)) {
handle_error("lua: fail to call MD5 (EVP_DigestInit_ex call)");
}
if (1 != EVP_DigestUpdate(mdctx, message, message_len)) {
handle_error("lua: fail to call MD5 (EVP_DigestUpdate call)");
}
if ((*digest = (unsigned char*)OPENSSL_malloc(EVP_MD_size(EVP_md5()))) ==
nullptr) {
handle_error("lua: fail to call MD5 (OPENSSL_malloc call)");
}
if (1 != EVP_DigestFinal_ex(mdctx, *digest, digest_len)) {
handle_error("lua: fail to call MD5 (EVP_DigestFinal_ex call)");
}
EVP_MD_CTX_free(mdctx);
}

static int l_broker_md5(lua_State* L) {
auto digit = [](unsigned char d) -> char {
if (d < 10)
Expand All @@ -820,11 +848,12 @@ static int l_broker_md5(lua_State* L) {
size_t len;
const unsigned char* str =
reinterpret_cast<const unsigned char*>(lua_tolstring(L, -1, &len));
unsigned char md5[MD5_DIGEST_LENGTH];
MD5(str, len, md5);
char result[2 * MD5_DIGEST_LENGTH + 1];
unsigned char* md5;
uint32_t md5_len;
md5_message(str, len, &md5, &md5_len);
char result[2 * md5_len + 1];
char* tmp = result;
for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
for (uint32_t i = 0; i < md5_len; i++) {
*tmp = digit(md5[i] >> 4);
++tmp;
*tmp = digit(md5[i] & 0xf);
Expand Down
Loading

0 comments on commit e59698b

Please sign in to comment.