Skip to content

Commit e59698b

Browse files
bouda1sechkem
authored andcommitted
fix(broker/lua): Several fix in the lua Stream
* 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
1 parent d18fa46 commit e59698b

File tree

4 files changed

+217
-63
lines changed

4 files changed

+217
-63
lines changed

broker/lua/inc/com/centreon/broker/lua/macro_cache.hh

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "com/centreon/broker/neb/host_group.hh"
2828
#include "com/centreon/broker/neb/host_group_member.hh"
2929
#include "com/centreon/broker/neb/instance.hh"
30+
#include "com/centreon/broker/neb/internal.hh"
3031
#include "com/centreon/broker/neb/service.hh"
3132
#include "com/centreon/broker/neb/service_group.hh"
3233
#include "com/centreon/broker/neb/service_group_member.hh"
@@ -42,14 +43,26 @@ class macro_cache {
4243
std::shared_ptr<persistent_cache> _cache;
4344
absl::flat_hash_map<uint64_t, std::shared_ptr<io::data>> _instances;
4445
absl::flat_hash_map<uint64_t, std::shared_ptr<io::data>> _hosts;
45-
absl::flat_hash_map<uint64_t, std::shared_ptr<io::data>> _host_groups;
46+
/* The host groups cache stores also a set with the pollers telling they need
47+
* the cache. So if no more poller needs a host group, we can remove it from
48+
* the cache. */
49+
absl::flat_hash_map<uint64_t,
50+
std::pair<std::shared_ptr<neb::pb_host_group>,
51+
absl::flat_hash_set<uint32_t>>>
52+
_host_groups;
4653
absl::btree_map<std::pair<uint64_t, uint64_t>, std::shared_ptr<io::data>>
4754
_host_group_members;
4855
absl::flat_hash_map<std::pair<uint64_t, uint64_t>, std::shared_ptr<io::data>>
4956
_custom_vars;
5057
absl::flat_hash_map<std::pair<uint64_t, uint64_t>, std::shared_ptr<io::data>>
5158
_services;
52-
absl::flat_hash_map<uint64_t, std::shared_ptr<io::data>> _service_groups;
59+
/* The service groups cache stores also a set with the pollers telling they
60+
* need the cache. So if no more poller needs a service group, we can remove
61+
* it from the cache. */
62+
absl::flat_hash_map<uint64_t,
63+
std::pair<std::shared_ptr<neb::pb_service_group>,
64+
absl::flat_hash_set<uint32_t>>>
65+
_service_groups;
5366
absl::btree_map<std::tuple<uint64_t, uint64_t, uint64_t>,
5467
std::shared_ptr<io::data>>
5568
_service_group_members;

broker/lua/src/broker_utils.cc

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include "absl/strings/string_view.h"
2525
#include "com/centreon/broker/config/applier/state.hh"
2626

27-
#include <openssl/md5.h>
27+
#include <openssl/evp.h>
2828
#include <cstdlib>
2929
#include <cstring>
3030
#include <iomanip>
@@ -810,6 +810,34 @@ static int l_broker_stat(lua_State* L) {
810810
}
811811
}
812812

813+
static void md5_message(const unsigned char* message,
814+
size_t message_len,
815+
unsigned char** digest,
816+
unsigned int* digest_len) {
817+
EVP_MD_CTX* mdctx;
818+
auto handle_error = [](const std::string& msg) {
819+
auto logger = log_v2::instance().get(log_v2::LUA);
820+
logger->error(msg);
821+
};
822+
if ((mdctx = EVP_MD_CTX_new()) == nullptr) {
823+
handle_error("lua: fail to call MD5 (EVP_MD_CTX_new call)");
824+
}
825+
if (1 != EVP_DigestInit_ex(mdctx, EVP_md5(), nullptr)) {
826+
handle_error("lua: fail to call MD5 (EVP_DigestInit_ex call)");
827+
}
828+
if (1 != EVP_DigestUpdate(mdctx, message, message_len)) {
829+
handle_error("lua: fail to call MD5 (EVP_DigestUpdate call)");
830+
}
831+
if ((*digest = (unsigned char*)OPENSSL_malloc(EVP_MD_size(EVP_md5()))) ==
832+
nullptr) {
833+
handle_error("lua: fail to call MD5 (OPENSSL_malloc call)");
834+
}
835+
if (1 != EVP_DigestFinal_ex(mdctx, *digest, digest_len)) {
836+
handle_error("lua: fail to call MD5 (EVP_DigestFinal_ex call)");
837+
}
838+
EVP_MD_CTX_free(mdctx);
839+
}
840+
813841
static int l_broker_md5(lua_State* L) {
814842
auto digit = [](unsigned char d) -> char {
815843
if (d < 10)
@@ -820,11 +848,12 @@ static int l_broker_md5(lua_State* L) {
820848
size_t len;
821849
const unsigned char* str =
822850
reinterpret_cast<const unsigned char*>(lua_tolstring(L, -1, &len));
823-
unsigned char md5[MD5_DIGEST_LENGTH];
824-
MD5(str, len, md5);
825-
char result[2 * MD5_DIGEST_LENGTH + 1];
851+
unsigned char* md5;
852+
uint32_t md5_len;
853+
md5_message(str, len, &md5, &md5_len);
854+
char result[2 * md5_len + 1];
826855
char* tmp = result;
827-
for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
856+
for (uint32_t i = 0; i < md5_len; i++) {
828857
*tmp = digit(md5[i] >> 4);
829858
++tmp;
830859
*tmp = digit(md5[i] & 0xf);

0 commit comments

Comments
 (0)