From a70fb5c88451462dd853b7fb7ce7a78378afcaf2 Mon Sep 17 00:00:00 2001 From: Tomas Turina Date: Tue, 12 Nov 2024 18:47:36 +0000 Subject: [PATCH 1/5] feat: add name to agent metadata --- src/agent/agent_info/include/agent_info.hpp | 13 +++++++++++- src/agent/agent_info/src/agent_info.cpp | 14 +++++++++++++ .../agent_info/src/agent_info_persistance.cpp | 16 +++++++++++++-- .../agent_info/src/agent_info_persistance.hpp | 8 ++++++++ .../tests/agent_info_persistance_test.cpp | 20 +++++++++++++++++++ .../agent_info/tests/agent_info_test.cpp | 15 ++++++++++++++ src/agent/include/agent_registration.hpp | 2 ++ src/agent/src/agent_registration.cpp | 10 ++++++++++ src/agent/src/main.cpp | 1 + src/agent/src/process_options.cpp | 3 ++- src/agent/src/process_options.hpp | 2 ++ src/agent/tests/agent_registration_test.cpp | 11 +++++----- 12 files changed, 106 insertions(+), 9 deletions(-) diff --git a/src/agent/agent_info/include/agent_info.hpp b/src/agent/agent_info/include/agent_info.hpp index 7656f54b15..7b4657bdcc 100644 --- a/src/agent/agent_info/include/agent_info.hpp +++ b/src/agent/agent_info/include/agent_info.hpp @@ -8,7 +8,7 @@ /// @brief Stores and manages information about an agent. /// -/// This class provides methods for getting and setting the agent's key, +/// This class provides methods for getting and setting the agent's name, key, /// UUID, and groups. It also includes private methods for creating and /// validating the key. class AgentInfo @@ -25,6 +25,10 @@ class AgentInfo AgentInfo(std::function getOSInfo = nullptr, std::function getNetworksInfo = nullptr); + /// @brief Gets the agent's name. + /// @return The agent's name. + std::string GetName() const; + /// @brief Gets the agent's key. /// @return The agent's key. std::string GetKey() const; @@ -37,6 +41,10 @@ class AgentInfo /// @return A vector of the agent's groups. std::vector GetGroups() const; + /// @brief Sets the agent's name. + /// @param name The agent's new name. + void SetName(const std::string& name); + /// @brief Sets the agent's key. /// @param key The agent's new key. /// @return True if the key was successfully set, false otherwise. @@ -95,6 +103,9 @@ class AgentInfo /// @return Optional string with the active IP address if found; otherwise, `std::nullopt`. std::optional GetActiveIPAddress(const nlohmann::json& networksJson) const; + /// @brief The agent's name. + std::string m_name; + /// @brief The agent's key. std::string m_key; diff --git a/src/agent/agent_info/src/agent_info.cpp b/src/agent/agent_info/src/agent_info.cpp index 4e95a493a5..e600f5b8fc 100644 --- a/src/agent/agent_info/src/agent_info.cpp +++ b/src/agent/agent_info/src/agent_info.cpp @@ -19,6 +19,7 @@ namespace AgentInfo::AgentInfo(std::function getOSInfo, std::function getNetworksInfo) { AgentInfoPersistance agentInfoPersistance; + m_name = agentInfoPersistance.GetName(); m_key = agentInfoPersistance.GetKey(); m_uuid = agentInfoPersistance.GetUUID(); m_groups = agentInfoPersistance.GetGroups(); @@ -44,6 +45,11 @@ AgentInfo::AgentInfo(std::function getOSInfo, std::function AgentInfo::GetGroups() const return m_groups; } +void AgentInfo::SetName(const std::string& name) +{ + AgentInfoPersistance agentInfoPersistance; + agentInfoPersistance.SetName(name); + m_name = name; +} + bool AgentInfo::SetKey(const std::string& key) { AgentInfoPersistance agentInfoPersistance; @@ -186,6 +199,7 @@ void AgentInfo::LoadEndpointInfo() void AgentInfo::LoadMetadataInfo() { m_metadataInfo["id"] = GetUUID(); + m_metadataInfo["name"] = GetName(); m_metadataInfo["type"] = GetType(); m_metadataInfo["version"] = GetVersion(); m_metadataInfo["groups"] = GetGroups(); diff --git a/src/agent/agent_info/src/agent_info_persistance.cpp b/src/agent/agent_info/src/agent_info_persistance.cpp index 4b4b26c667..d51c80ce99 100644 --- a/src/agent/agent_info/src/agent_info_persistance.cpp +++ b/src/agent/agent_info/src/agent_info_persistance.cpp @@ -58,7 +58,8 @@ void AgentInfoPersistance::CreateAgentInfoTable() { try { - const std::vector columns = {Column("key", ColumnType::TEXT, true, false), + const std::vector columns = {Column("name", ColumnType::TEXT, true, false), + Column("key", ColumnType::TEXT, true, false), Column("uuid", ColumnType::TEXT, true, false, true)}; m_db->CreateTable(AGENT_INFO_TABLE_NAME, columns); @@ -92,7 +93,8 @@ void AgentInfoPersistance::InsertDefaultAgentInfo() if (count == 0) { - const std::vector columns = {Column("key", ColumnType::TEXT, ""), + const std::vector columns = {Column("name", ColumnType::TEXT, ""), + Column("key", ColumnType::TEXT, ""), Column("uuid", ColumnType::TEXT, "")}; m_db->Insert(AGENT_INFO_TABLE_NAME, columns); @@ -139,6 +141,11 @@ std::string AgentInfoPersistance::GetAgentInfoValue(const std::string& column) c return value; } +std::string AgentInfoPersistance::GetName() const +{ + return GetAgentInfoValue("name"); +} + std::string AgentInfoPersistance::GetKey() const { return GetAgentInfoValue("key"); @@ -174,6 +181,11 @@ std::vector AgentInfoPersistance::GetGroups() const return groupList; } +void AgentInfoPersistance::SetName(const std::string& name) +{ + SetAgentInfoValue("name", name); +} + void AgentInfoPersistance::SetKey(const std::string& key) { SetAgentInfoValue("key", key); diff --git a/src/agent/agent_info/src/agent_info_persistance.hpp b/src/agent/agent_info/src/agent_info_persistance.hpp index 5b403cd220..28df5e5716 100644 --- a/src/agent/agent_info/src/agent_info_persistance.hpp +++ b/src/agent/agent_info/src/agent_info_persistance.hpp @@ -31,6 +31,10 @@ class AgentInfoPersistance /// @brief Deleted move assignment operator. AgentInfoPersistance& operator=(AgentInfoPersistance&&) = delete; + /// @brief Retrieves the agent's name from the database. + /// @return The name of the agent as a string. + std::string GetName() const; + /// @brief Retrieves the agent's key from the database. /// @return The key of the agent as a string. std::string GetKey() const; @@ -43,6 +47,10 @@ class AgentInfoPersistance /// @return A vector of strings, each representing a group name. std::vector GetGroups() const; + /// @brief Sets the agent's name in the database. + /// @param name The name to set. + void SetName(const std::string& name); + /// @brief Sets the agent's key in the database. /// @param key The key to set. void SetKey(const std::string& key); diff --git a/src/agent/agent_info/tests/agent_info_persistance_test.cpp b/src/agent/agent_info/tests/agent_info_persistance_test.cpp index 2d72384111..b3f600e8af 100644 --- a/src/agent/agent_info/tests/agent_info_persistance_test.cpp +++ b/src/agent/agent_info/tests/agent_info_persistance_test.cpp @@ -25,10 +25,18 @@ TEST_F(AgentInfoPersistanceTest, TestConstruction) TEST_F(AgentInfoPersistanceTest, TestDefaultValues) { + EXPECT_EQ(persistance->GetName(), ""); EXPECT_EQ(persistance->GetKey(), ""); EXPECT_EQ(persistance->GetUUID(), ""); } +TEST_F(AgentInfoPersistanceTest, TestSetName) +{ + const std::string newName = "new_name"; + persistance->SetName(newName); + EXPECT_EQ(persistance->GetName(), newName); +} + TEST_F(AgentInfoPersistanceTest, TestSetKey) { const std::string newKey = "new_key"; @@ -60,6 +68,18 @@ TEST_F(AgentInfoPersistanceTest, TestSetGroupsDelete) EXPECT_EQ(persistance->GetGroups(), newGroups); } +TEST_F(AgentInfoPersistanceTest, TestResetToDefault) +{ + const std::string newName = "new_name"; + persistance->SetName(newName); + EXPECT_EQ(persistance->GetName(), newName); + + persistance->ResetToDefault(); + EXPECT_EQ(persistance->GetName(), ""); + EXPECT_EQ(persistance->GetKey(), ""); + EXPECT_EQ(persistance->GetUUID(), ""); +} + int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); diff --git a/src/agent/agent_info/tests/agent_info_test.cpp b/src/agent/agent_info/tests/agent_info_test.cpp index bed2661626..74c5dad5b9 100644 --- a/src/agent/agent_info/tests/agent_info_test.cpp +++ b/src/agent/agent_info/tests/agent_info_test.cpp @@ -25,6 +25,7 @@ TEST_F(AgentInfoTest, TestDefaultConstructor) TEST_F(AgentInfoTest, TestDefaultConstructorDefaultValues) { const AgentInfo agentInfo; + EXPECT_EQ(agentInfo.GetName(), ""); EXPECT_EQ(agentInfo.GetKey(), ""); EXPECT_NE(agentInfo.GetUUID(), ""); } @@ -32,13 +33,27 @@ TEST_F(AgentInfoTest, TestDefaultConstructorDefaultValues) TEST_F(AgentInfoTest, TestPersistedValues) { AgentInfo agentInfo; + agentInfo.SetName("test_name"); agentInfo.SetKey("4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj"); agentInfo.SetUUID("test_uuid"); const AgentInfo agentInfoReloaded; + EXPECT_EQ(agentInfoReloaded.GetName(), "test_name"); EXPECT_EQ(agentInfoReloaded.GetKey(), "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj"); EXPECT_EQ(agentInfoReloaded.GetUUID(), "test_uuid"); } +TEST_F(AgentInfoTest, TestSetName) +{ + AgentInfo agentInfo; + const std::string newName = "new_name"; + + agentInfo.SetName(newName); + EXPECT_EQ(agentInfo.GetName(), newName); + + const AgentInfo agentInfoReloaded; + EXPECT_EQ(agentInfoReloaded.GetName(), newName); +} + TEST_F(AgentInfoTest, TestSetKey) { AgentInfo agentInfo; diff --git a/src/agent/include/agent_registration.hpp b/src/agent/include/agent_registration.hpp index e7bba98b79..4c91edf06d 100644 --- a/src/agent/include/agent_registration.hpp +++ b/src/agent/include/agent_registration.hpp @@ -31,10 +31,12 @@ namespace agent_registration /// @param user The user's username. /// @param password The user's password. /// @param key The agent's key. + /// @param name The agent's name. /// @param configFile The path to the configuration file. AgentRegistration(std::string user, std::string password, const std::string& key, + const std::string& name, std::optional configFile); /// @brief Registers the agent with the manager. diff --git a/src/agent/src/agent_registration.cpp b/src/agent/src/agent_registration.cpp index 046b8d166d..80ac74646d 100644 --- a/src/agent/src/agent_registration.cpp +++ b/src/agent/src/agent_registration.cpp @@ -11,6 +11,7 @@ namespace agent_registration AgentRegistration::AgentRegistration(std::string user, std::string password, const std::string& key, + const std::string& name, std::optional configFile) : m_agentInfo([this]() { return m_sysInfo.os(); }, [this]() { return m_sysInfo.networks(); }) , m_configurationParser(configFile.has_value() && !configFile->empty() @@ -24,6 +25,15 @@ namespace agent_registration { throw std::invalid_argument("--key argument must be alphanumeric and 32 characters in length"); } + + if (!name.empty()) + { + m_agentInfo.SetName(name); + } + else + { + m_agentInfo.SetName(boost::asio::ip::host_name()); + } } bool AgentRegistration::Register(http_client::IHttpClient& httpClient) diff --git a/src/agent/src/main.cpp b/src/agent/src/main.cpp index 2763207fb1..b56aa266f7 100644 --- a/src/agent/src/main.cpp +++ b/src/agent/src/main.cpp @@ -23,6 +23,7 @@ int main(int argc, char* argv[]) RegisterAgent(cmdParser.GetOptionValue(OPT_USER), cmdParser.GetOptionValue(OPT_PASSWORD), cmdParser.GetOptionValue(OPT_KEY), + cmdParser.GetOptionValue(OPT_NAME), configFile); } else if (cmdParser.OptionExists(OPT_RESTART)) diff --git a/src/agent/src/process_options.cpp b/src/agent/src/process_options.cpp index 097d47f33a..aaa3c0c0fb 100644 --- a/src/agent/src/process_options.cpp +++ b/src/agent/src/process_options.cpp @@ -10,6 +10,7 @@ void RegisterAgent(const std::string& user, const std::string& password, const std::string& key, + const std::string& name, const std::string& configFile) { if (!user.empty() && !password.empty()) @@ -18,7 +19,7 @@ void RegisterAgent(const std::string& user, { std::cout << "Starting wazuh-agent registration\n"; - agent_registration::AgentRegistration reg(user, password, key, configFile); + agent_registration::AgentRegistration reg(user, password, key, name, configFile); http_client::HttpClient httpClient; if (reg.Register(httpClient)) diff --git a/src/agent/src/process_options.hpp b/src/agent/src/process_options.hpp index 65989df081..d9c9713f9e 100644 --- a/src/agent/src/process_options.hpp +++ b/src/agent/src/process_options.hpp @@ -16,6 +16,7 @@ static const auto OPT_RUN_SERVICE {"--run-service"}; static const auto OPT_USER {"--user"}; static const auto OPT_PASSWORD {"--password"}; static const auto OPT_KEY {"--key"}; +static const auto OPT_NAME {"--name"}; static const auto OPT_HELP {"--help"}; /// @brief Registers the agent with the given parameters. @@ -27,6 +28,7 @@ static const auto OPT_HELP {"--help"}; void RegisterAgent(const std::string& user, const std::string& password, const std::string& key, + const std::string& name, const std::string& configFile); /// @brief Restarts the agent using the specified configuration file. diff --git a/src/agent/tests/agent_registration_test.cpp b/src/agent/tests/agent_registration_test.cpp index 4304c22c3b..d8e5493da4 100644 --- a/src/agent/tests/agent_registration_test.cpp +++ b/src/agent/tests/agent_registration_test.cpp @@ -68,7 +68,7 @@ class RegisterTest : public ::testing::Test TEST_F(RegisterTest, RegistrationTestSuccess) { registration = std::make_unique( - "user", "password", "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj", std::nullopt); + "user", "password", "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj", "agent_name", std::nullopt); SysInfo sysInfo; agent = std::make_unique([&sysInfo]() mutable { return sysInfo.os(); }, [&sysInfo]() mutable { return sysInfo.networks(); }); @@ -101,7 +101,7 @@ TEST_F(RegisterTest, RegistrationTestSuccess) TEST_F(RegisterTest, RegistrationFailsIfAuthenticationFails) { registration = std::make_unique( - "user", "password", "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj", std::nullopt); + "user", "password", "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj", "agent_name", std::nullopt); agent = std::make_unique(); MockHttpClient mockHttpClient; @@ -117,7 +117,7 @@ TEST_F(RegisterTest, RegistrationFailsIfAuthenticationFails) TEST_F(RegisterTest, RegistrationFailsIfServerResponseIsNotOk) { registration = std::make_unique( - "user", "password", "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj", std::nullopt); + "user", "password", "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj", "agent_name", std::nullopt); agent = std::make_unique(); MockHttpClient mockHttpClient; @@ -137,7 +137,8 @@ TEST_F(RegisterTest, RegistrationFailsIfServerResponseIsNotOk) TEST_F(RegisterTest, RegistrationTestSuccessWithEmptyKey) { - registration = std::make_unique("user", "password", "", std::nullopt); + registration = + std::make_unique("user", "password", "", "agent_name", std::nullopt); SysInfo sysInfo; agent = std::make_unique([&sysInfo]() mutable { return sysInfo.os(); }, [&sysInfo]() mutable { return sysInfo.networks(); }); @@ -169,7 +170,7 @@ TEST_F(RegisterTest, RegistrationTestSuccessWithEmptyKey) TEST_F(RegisterTest, RegistrationTestFailWithBadKey) { - ASSERT_THROW(agent_registration::AgentRegistration("user", "password", "badKey", std::nullopt), + ASSERT_THROW(agent_registration::AgentRegistration("user", "password", "badKey", "agent_name", std::nullopt), std::invalid_argument); } From 9399c55c7e66322f723b37f1854bde78ac5b1507 Mon Sep 17 00:00:00 2001 From: Tomas Turina Date: Tue, 12 Nov 2024 21:52:50 +0000 Subject: [PATCH 2/5] fix: adapt getNextNAwaitable to be able to return multiple messages --- src/agent/agent_info/include/agent_info.hpp | 4 ++-- src/agent/agent_info/src/agent_info.cpp | 4 ++-- .../agent_info/tests/agent_info_test.cpp | 9 +++++--- .../include/imultitype_queue.hpp | 10 ++++----- .../include/multitype_queue.hpp | 14 ++++++------ .../multitype_queue/src/multitype_queue.cpp | 22 +++++-------------- .../tests/multitype_queue_test.cpp | 4 ++-- src/agent/src/agent_registration.cpp | 2 +- src/agent/src/message_queue_utils.cpp | 15 ++++++++----- src/agent/src/message_queue_utils.hpp | 4 ++-- src/agent/tests/agent_registration_test.cpp | 4 ++-- src/agent/tests/message_queue_utils_test.cpp | 18 ++++++++------- .../logcollector/tests/unit/queue_mock.hpp | 2 +- 13 files changed, 55 insertions(+), 57 deletions(-) diff --git a/src/agent/agent_info/include/agent_info.hpp b/src/agent/agent_info/include/agent_info.hpp index 7b4657bdcc..8a64af3317 100644 --- a/src/agent/agent_info/include/agent_info.hpp +++ b/src/agent/agent_info/include/agent_info.hpp @@ -72,8 +72,8 @@ class AgentInfo /// @brief Gets all the information about the agent. /// @param agentIsRegistering Indicates if the agent is about to register. - /// @return A json object with all information about the agent. - nlohmann::json GetMetadataInfo(const bool agentIsRegistering) const; + /// @return A string with all information about the agent. + std::string GetMetadataInfo(const bool agentIsRegistering) const; private: /// @brief Creates a random key for the agent. diff --git a/src/agent/agent_info/src/agent_info.cpp b/src/agent/agent_info/src/agent_info.cpp index e600f5b8fc..b4a57b85c0 100644 --- a/src/agent/agent_info/src/agent_info.cpp +++ b/src/agent/agent_info/src/agent_info.cpp @@ -145,7 +145,7 @@ std::string AgentInfo::GetHeaderInfo() const return m_headerInfo; } -nlohmann::json AgentInfo::GetMetadataInfo(const bool agentIsRegistering) const +std::string AgentInfo::GetMetadataInfo(const bool agentIsRegistering) const { nlohmann::json metadataInfo; @@ -156,7 +156,7 @@ nlohmann::json AgentInfo::GetMetadataInfo(const bool agentIsRegistering) const metadataInfo["agent"]["key"] = GetKey(); } - return metadataInfo; + return metadataInfo.dump(); } std::optional AgentInfo::GetActiveIPAddress(const nlohmann::json& networksJson) const diff --git a/src/agent/agent_info/tests/agent_info_test.cpp b/src/agent/agent_info/tests/agent_info_test.cpp index 74c5dad5b9..3e1b33db3b 100644 --- a/src/agent/agent_info/tests/agent_info_test.cpp +++ b/src/agent/agent_info/tests/agent_info_test.cpp @@ -116,7 +116,7 @@ TEST_F(AgentInfoTest, TestLoadMetadataInfoNoSysInfo) { const AgentInfo agentInfo; - auto metadataInfo = agentInfo.GetMetadataInfo(true); + auto metadataInfo = nlohmann::json::parse(agentInfo.GetMetadataInfo(true)); EXPECT_TRUE(metadataInfo["agent"] != nullptr); @@ -124,6 +124,7 @@ TEST_F(AgentInfoTest, TestLoadMetadataInfoNoSysInfo) EXPECT_EQ(metadataInfo["agent"]["type"], agentInfo.GetType()); EXPECT_EQ(metadataInfo["agent"]["version"], agentInfo.GetVersion()); EXPECT_EQ(metadataInfo["agent"]["id"], agentInfo.GetUUID()); + EXPECT_EQ(metadataInfo["agent"]["name"], agentInfo.GetName()); EXPECT_EQ(metadataInfo["agent"]["key"], agentInfo.GetKey()); EXPECT_TRUE(metadataInfo["agent"]["groups"] != nullptr); @@ -155,7 +156,7 @@ TEST_F(AgentInfoTest, TestLoadMetadataInfoRegistration) const AgentInfo agentInfo([os]() { return os; }, [networks]() { return networks; }); - auto metadataInfo = agentInfo.GetMetadataInfo(true); + auto metadataInfo = nlohmann::json::parse(agentInfo.GetMetadataInfo(true)); EXPECT_TRUE(metadataInfo["agent"] != nullptr); @@ -163,6 +164,7 @@ TEST_F(AgentInfoTest, TestLoadMetadataInfoRegistration) EXPECT_EQ(metadataInfo["agent"]["type"], agentInfo.GetType()); EXPECT_EQ(metadataInfo["agent"]["version"], agentInfo.GetVersion()); EXPECT_EQ(metadataInfo["agent"]["id"], agentInfo.GetUUID()); + EXPECT_EQ(metadataInfo["agent"]["name"], agentInfo.GetName()); EXPECT_EQ(metadataInfo["agent"]["key"], agentInfo.GetKey()); EXPECT_TRUE(metadataInfo["agent"]["groups"] != nullptr); @@ -200,7 +202,7 @@ TEST_F(AgentInfoTest, TestLoadMetadataInfoConnected) const AgentInfo agentInfo([os]() { return os; }, [networks]() { return networks; }); - auto metadataInfo = agentInfo.GetMetadataInfo(false); + auto metadataInfo = nlohmann::json::parse(agentInfo.GetMetadataInfo(false)); EXPECT_TRUE(metadataInfo["agent"] != nullptr); @@ -208,6 +210,7 @@ TEST_F(AgentInfoTest, TestLoadMetadataInfoConnected) EXPECT_EQ(metadataInfo["agent"]["type"], agentInfo.GetType()); EXPECT_EQ(metadataInfo["agent"]["version"], agentInfo.GetVersion()); EXPECT_EQ(metadataInfo["agent"]["id"], agentInfo.GetUUID()); + EXPECT_EQ(metadataInfo["agent"]["name"], agentInfo.GetName()); EXPECT_TRUE(metadataInfo["agent"]["key"] == nullptr); EXPECT_TRUE(metadataInfo["agent"]["groups"] != nullptr); diff --git a/src/agent/multitype_queue/include/imultitype_queue.hpp b/src/agent/multitype_queue/include/imultitype_queue.hpp index ded5b2f7df..23531228ac 100644 --- a/src/agent/multitype_queue/include/imultitype_queue.hpp +++ b/src/agent/multitype_queue/include/imultitype_queue.hpp @@ -64,12 +64,12 @@ class IMultiTypeQueue * @param messageQuantity The quantity of messages to return. * @param moduleName The name of the module requesting the message. * @param moduleType The type of the module requesting the messages. - * @return boost::asio::awaitable Awaitable object representing the next message. + * @return boost::asio::awaitable> Awaitable object representing the next N messages. */ - virtual boost::asio::awaitable getNextNAwaitable(MessageType type, - int messageQuantity, - const std::string moduleName = "", - const std::string moduleType = "") = 0; + virtual boost::asio::awaitable> getNextNAwaitable(MessageType type, + int messageQuantity, + const std::string moduleName = "", + const std::string moduleType = "") = 0; /** * @brief Retrieves the next N messages from the queue. diff --git a/src/agent/multitype_queue/include/multitype_queue.hpp b/src/agent/multitype_queue/include/multitype_queue.hpp index d350314aa1..89cf5dfd77 100644 --- a/src/agent/multitype_queue/include/multitype_queue.hpp +++ b/src/agent/multitype_queue/include/multitype_queue.hpp @@ -90,20 +90,20 @@ class MultiTypeQueue : public IMultiTypeQueue int push(std::vector messages) override; /** - * @copydoc IMultiTypeQueue::getNext(MessageType, const std::string) + * @copydoc IMultiTypeQueue::getNext(MessageType, const std::string, const std::string) */ Message getNext(MessageType type, const std::string moduleName = "", const std::string moduleType = "") override; /** - * @copydoc IMultiTypeQueue::getNextNAwaitable(MessageType, int, const std::string) + * @copydoc IMultiTypeQueue::getNextNAwaitable(MessageType, int, const std::string, const std::string) */ - boost::asio::awaitable getNextNAwaitable(MessageType type, - int messageQuantity, - const std::string moduleName = "", - const std::string moduleType = "") override; + boost::asio::awaitable> getNextNAwaitable(MessageType type, + int messageQuantity, + const std::string moduleName = "", + const std::string moduleType = "") override; /** - * @copydoc IMultiTypeQueue::getNextN(MessageType, int, const std::string) + * @copydoc IMultiTypeQueue::getNextN(MessageType, int, const std::string, const std::string) */ std::vector getNextN(MessageType type, int messageQuantity, diff --git a/src/agent/multitype_queue/src/multitype_queue.cpp b/src/agent/multitype_queue/src/multitype_queue.cpp index f83e6adf6b..b0275e598f 100644 --- a/src/agent/multitype_queue/src/multitype_queue.cpp +++ b/src/agent/multitype_queue/src/multitype_queue.cpp @@ -159,14 +159,14 @@ Message MultiTypeQueue::getNext(MessageType type, const std::string moduleName, return result; } -boost::asio::awaitable MultiTypeQueue::getNextNAwaitable(MessageType type, - int messageQuantity, - const std::string moduleName, - const std::string moduleType) +boost::asio::awaitable> MultiTypeQueue::getNextNAwaitable(MessageType type, + int messageQuantity, + const std::string moduleName, + const std::string moduleType) { boost::asio::steady_timer timer(co_await boost::asio::this_coro::executor); - Message result(type, "{}"_json, moduleName, moduleType, ""); + std::vector result; if (m_mapMessageTypeName.contains(type)) { while (isEmpty(type)) @@ -174,17 +174,7 @@ boost::asio::awaitable MultiTypeQueue::getNextNAwaitable(MessageType ty timer.expires_after(std::chrono::milliseconds(DEFAULT_TIMER_IN_MS)); co_await timer.async_wait(boost::asio::use_awaitable); } - - auto resultData = - m_persistenceDest->RetrieveMultiple(messageQuantity, m_mapMessageTypeName.at(type), moduleName, moduleType); - - if (!resultData.empty()) - { - result.data = resultData[0]["data"]; - result.metaData = resultData[0]["metadata"]; - result.moduleName = resultData[0]["moduleName"]; - result.moduleType = resultData[0]["moduleType"]; - } + result = getNextN(type, messageQuantity, moduleName, moduleType); } else { diff --git a/src/agent/multitype_queue/tests/multitype_queue_test.cpp b/src/agent/multitype_queue/tests/multitype_queue_test.cpp index b5c0648a49..489bc0f07c 100644 --- a/src/agent/multitype_queue/tests/multitype_queue_test.cpp +++ b/src/agent/multitype_queue/tests/multitype_queue_test.cpp @@ -491,8 +491,8 @@ TEST_F(MultiTypeQueueTest, GetNextAwaitableBase) [&multiTypeQueue]() -> boost::asio::awaitable { auto messageReceived = co_await multiTypeQueue.getNextNAwaitable(MessageType::STATELESS, 2); - EXPECT_EQ(messageReceived.data.at(0).at("data"), "content-1"); - EXPECT_EQ(messageReceived.data.at(1).at("data"), "content-2"); + EXPECT_EQ(messageReceived[0].data.at("data"), "content-1"); + EXPECT_EQ(messageReceived[1].data.at("data"), "content-2"); }, boost::asio::detached); diff --git a/src/agent/src/agent_registration.cpp b/src/agent/src/agent_registration.cpp index 80ac74646d..1cd1a000e9 100644 --- a/src/agent/src/agent_registration.cpp +++ b/src/agent/src/agent_registration.cpp @@ -53,7 +53,7 @@ namespace agent_registration m_agentInfo.GetHeaderInfo(), token.value(), "", - m_agentInfo.GetMetadataInfo(true).dump()); + m_agentInfo.GetMetadataInfo(true)); const auto res = httpClient.PerformHttpRequest(reqParams); diff --git a/src/agent/src/message_queue_utils.cpp b/src/agent/src/message_queue_utils.cpp index fb149f8b0f..8f99a00327 100644 --- a/src/agent/src/message_queue_utils.cpp +++ b/src/agent/src/message_queue_utils.cpp @@ -11,20 +11,23 @@ namespace boost::asio::awaitable GetMessagesFromQueue(std::shared_ptr multiTypeQueue, MessageType messageType, - std::function getMetadataInfo) + std::function getMetadataInfo) { - const auto message = co_await multiTypeQueue->getNextNAwaitable(messageType, NUM_EVENTS, "", ""); + const auto messages = co_await multiTypeQueue->getNextNAwaitable(messageType, NUM_EVENTS, "", ""); - nlohmann::json jsonObj; std::string output; if (getMetadataInfo != nullptr) { - jsonObj = getMetadataInfo(); - output = jsonObj.dump() + "\n"; + output = getMetadataInfo() + "\n"; } - co_return output + message.metaData + "\n" + message.data.dump(); + for (const auto& message : messages) + { + output += message.metaData + "\n" + message.data.dump() + "\n"; + } + + co_return output; } void PopMessagesFromQueue(std::shared_ptr multiTypeQueue, MessageType messageType) diff --git a/src/agent/src/message_queue_utils.hpp b/src/agent/src/message_queue_utils.hpp index 8724f77da8..98354563d9 100644 --- a/src/agent/src/message_queue_utils.hpp +++ b/src/agent/src/message_queue_utils.hpp @@ -16,10 +16,10 @@ class IMultiTypeQueue; /// @param multiTypeQueue The queue to get messages from /// @param messageType The type of messages to get from the queue /// @param getMetadataInfo Function to get the agent metadata -/// @return A JSON string containing the messages from the queue +/// @return A string containing the messages from the queue boost::asio::awaitable GetMessagesFromQueue(std::shared_ptr multiTypeQueue, MessageType messageType, - std::function getMetadataInfo); + std::function getMetadataInfo); /// @brief Removes a fixed number of messages from the specified queue /// @param multiTypeQueue The queue from which to remove messages diff --git a/src/agent/tests/agent_registration_test.cpp b/src/agent/tests/agent_registration_test.cpp index d8e5493da4..1a6a4ab24f 100644 --- a/src/agent/tests/agent_registration_test.cpp +++ b/src/agent/tests/agent_registration_test.cpp @@ -86,7 +86,7 @@ TEST_F(RegisterTest, RegistrationTestSuccess) agent->GetHeaderInfo(), "token", "", - bodyJson.dump()); + bodyJson); boost::beast::http::response expectedResponse; expectedResponse.result(boost::beast::http::status::ok); @@ -156,7 +156,7 @@ TEST_F(RegisterTest, RegistrationTestSuccessWithEmptyKey) agent->GetHeaderInfo(), "token", "", - bodyJson.dump()); + bodyJson); boost::beast::http::response expectedResponse; expectedResponse.result(boost::beast::http::status::ok); diff --git a/src/agent/tests/message_queue_utils_test.cpp b/src/agent/tests/message_queue_utils_test.cpp index e4f5ad5ab9..a9e0ff0e83 100644 --- a/src/agent/tests/message_queue_utils_test.cpp +++ b/src/agent/tests/message_queue_utils_test.cpp @@ -14,7 +14,7 @@ const nlohmann::json BASE_DATA_CONTENT = R"([{"data": {"id":"112233", "args": [" class MockMultiTypeQueue : public MultiTypeQueue { public: - MOCK_METHOD(boost::asio::awaitable, + MOCK_METHOD(boost::asio::awaitable>, getNextNAwaitable, (MessageType, int, const std::string, const std::string), (override)); @@ -41,11 +41,12 @@ TEST_F(MessageQueueUtilsTest, GetMessagesFromQueueTest) { std::vector data {R"({"event":{"original":"Testing message!"}})"}; std::string metadata {R"({"module":"logcollector","type":"file"})"}; - Message testMessage {MessageType::STATELESS, data, "", "", metadata}; + std::vector testMessages; + testMessages.emplace_back(MessageType::STATELESS, data, "", "", metadata); // NOLINTBEGIN(cppcoreguidelines-avoid-capturing-lambda-coroutines) EXPECT_CALL(*mockQueue, getNextNAwaitable(MessageType::STATELESS, 1, "", "")) - .WillOnce([&testMessage]() -> boost::asio::awaitable { co_return testMessage; }); + .WillOnce([&testMessages]() -> boost::asio::awaitable> { co_return testMessages; }); // NOLINTEND(cppcoreguidelines-avoid-capturing-lambda-coroutines) auto result = boost::asio::co_spawn( @@ -59,7 +60,7 @@ TEST_F(MessageQueueUtilsTest, GetMessagesFromQueueTest) const auto jsonResult = result.get(); std::string expectedString = R"({"module":"logcollector","type":"file"})" + std::string("\n") + - R"(["{\"event\":{\"original\":\"Testing message!\"}}"])"; + R"(["{\"event\":{\"original\":\"Testing message!\"}}"])" + std::string("\n"); ASSERT_EQ(jsonResult, expectedString); } @@ -68,21 +69,22 @@ TEST_F(MessageQueueUtilsTest, GetMessagesFromQueueMetadataTest) { std::vector data {R"({"event":{"original":"Testing message!"}})"}; std::string moduleMetadata {R"({"module":"logcollector","type":"file"})"}; - Message testMessage {MessageType::STATELESS, data, "", "", moduleMetadata}; + std::vector testMessages; + testMessages.emplace_back(MessageType::STATELESS, data, "", "", moduleMetadata); nlohmann::json metadata; metadata["agent"] = "test"; // NOLINTBEGIN(cppcoreguidelines-avoid-capturing-lambda-coroutines) EXPECT_CALL(*mockQueue, getNextNAwaitable(MessageType::STATELESS, 1, "", "")) - .WillOnce([&testMessage]() -> boost::asio::awaitable { co_return testMessage; }); + .WillOnce([&testMessages]() -> boost::asio::awaitable> { co_return testMessages; }); // NOLINTEND(cppcoreguidelines-avoid-capturing-lambda-coroutines) io_context.restart(); auto result = boost::asio::co_spawn( io_context, - GetMessagesFromQueue(mockQueue, MessageType::STATELESS, [&metadata]() { return metadata; }), + GetMessagesFromQueue(mockQueue, MessageType::STATELESS, [&metadata]() { return metadata.dump(); }), boost::asio::use_future); const auto timeout = std::chrono::steady_clock::now() + std::chrono::milliseconds(1); @@ -94,7 +96,7 @@ TEST_F(MessageQueueUtilsTest, GetMessagesFromQueueMetadataTest) std::string expectedString = R"({"agent":"test"})" + std::string("\n") + R"({"module":"logcollector","type":"file"})" + std::string("\n") + - R"(["{\"event\":{\"original\":\"Testing message!\"}}"])"; + R"(["{\"event\":{\"original\":\"Testing message!\"}}"])" + std::string("\n"); ASSERT_EQ(jsonResult, expectedString); } diff --git a/src/modules/logcollector/tests/unit/queue_mock.hpp b/src/modules/logcollector/tests/unit/queue_mock.hpp index 63975619d5..bffde325b5 100644 --- a/src/modules/logcollector/tests/unit/queue_mock.hpp +++ b/src/modules/logcollector/tests/unit/queue_mock.hpp @@ -13,7 +13,7 @@ class QueueMock : public IMultiTypeQueue { (MessageType type, const std::string module, const std::string moduleType), (override)); - MOCK_METHOD(boost::asio::awaitable, getNextNAwaitable, + MOCK_METHOD(boost::asio::awaitable>, getNextNAwaitable, (MessageType type, int messageQuantity, const std::string moduleName, const std::string moduleType), (override)); From 69ab7f8b136619fa34d0222e13522d5a2b947996 Mon Sep 17 00:00:00 2001 From: Tomas Turina Date: Tue, 12 Nov 2024 23:27:58 +0000 Subject: [PATCH 3/5] feat: add a list of active ips to agent metadata --- src/agent/agent_info/include/agent_info.hpp | 4 ++-- src/agent/agent_info/src/agent_info.cpp | 13 +++++++------ src/agent/agent_info/tests/agent_info_test.cpp | 6 ++++-- src/agent/src/message_queue_utils.cpp | 4 ++-- src/agent/tests/message_queue_utils_test.cpp | 6 +++--- 5 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/agent/agent_info/include/agent_info.hpp b/src/agent/agent_info/include/agent_info.hpp index 8a64af3317..d6259ae871 100644 --- a/src/agent/agent_info/include/agent_info.hpp +++ b/src/agent/agent_info/include/agent_info.hpp @@ -100,8 +100,8 @@ class AgentInfo /// @brief Extracts the active IP address from the network JSON data. /// @param networksJson JSON object containing network interface information. - /// @return Optional string with the active IP address if found; otherwise, `std::nullopt`. - std::optional GetActiveIPAddress(const nlohmann::json& networksJson) const; + /// @return Vector of strings with the active IP addresses. + std::vector GetActiveIPAddresses(const nlohmann::json& networksJson) const; /// @brief The agent's name. std::string m_name; diff --git a/src/agent/agent_info/src/agent_info.cpp b/src/agent/agent_info/src/agent_info.cpp index b4a57b85c0..7ca974f8f1 100644 --- a/src/agent/agent_info/src/agent_info.cpp +++ b/src/agent/agent_info/src/agent_info.cpp @@ -159,8 +159,10 @@ std::string AgentInfo::GetMetadataInfo(const bool agentIsRegistering) const return metadataInfo.dump(); } -std::optional AgentInfo::GetActiveIPAddress(const nlohmann::json& networksJson) const +std::vector AgentInfo::GetActiveIPAddresses(const nlohmann::json& networksJson) const { + std::vector ipAddresses; + if (networksJson.contains("iface")) { for (const auto& iface : networksJson["iface"]) @@ -169,12 +171,12 @@ std::optional AgentInfo::GetActiveIPAddress(const nlohmann::json& n { if (iface.contains("IPv4") && !iface["IPv4"].empty()) { - return iface["IPv4"][0].value("address", ""); + ipAddresses.emplace_back(iface["IPv4"][0].value("address", "")); } } } } - return std::nullopt; + return ipAddresses; } void AgentInfo::LoadEndpointInfo() @@ -191,8 +193,7 @@ void AgentInfo::LoadEndpointInfo() if (m_getNetworksInfo != nullptr) { nlohmann::json networksInfo = m_getNetworksInfo(); - auto ipAddress = GetActiveIPAddress(networksInfo); - m_endpointInfo["ip"] = ipAddress.value_or("Unknown"); + m_endpointInfo["ip"] = GetActiveIPAddresses(networksInfo); } } @@ -209,8 +210,8 @@ void AgentInfo::LoadMetadataInfo() nlohmann::json host; nlohmann::json os; + host["ip"] = m_endpointInfo["ip"]; host["hostname"] = m_endpointInfo.value("hostname", "Unknown"); - host["ip"] = m_endpointInfo.value("ip", "Unknown"); host["architecture"] = m_endpointInfo.value("architecture", "Unknown"); os["name"] = m_endpointInfo.value("os", "Unknown"); diff --git a/src/agent/agent_info/tests/agent_info_test.cpp b/src/agent/agent_info/tests/agent_info_test.cpp index 3e1b33db3b..8c399c85fd 100644 --- a/src/agent/agent_info/tests/agent_info_test.cpp +++ b/src/agent/agent_info/tests/agent_info_test.cpp @@ -173,7 +173,8 @@ TEST_F(AgentInfoTest, TestLoadMetadataInfoRegistration) EXPECT_TRUE(metadataInfo["agent"]["host"]["os"] != nullptr); EXPECT_EQ(metadataInfo["agent"]["host"]["os"]["name"], "test_os"); EXPECT_EQ(metadataInfo["agent"]["host"]["os"]["platform"], "test_platform"); - EXPECT_EQ(metadataInfo["agent"]["host"]["ip"], "127.0.0.1"); + EXPECT_TRUE(metadataInfo["agent"]["host"]["ip"] != nullptr); + EXPECT_EQ(metadataInfo["agent"]["host"]["ip"][0], "127.0.0.1"); EXPECT_EQ(metadataInfo["agent"]["host"]["architecture"], "test_arch"); EXPECT_EQ(metadataInfo["agent"]["host"]["hostname"], "test_name"); } @@ -219,7 +220,8 @@ TEST_F(AgentInfoTest, TestLoadMetadataInfoConnected) EXPECT_TRUE(metadataInfo["agent"]["host"]["os"] != nullptr); EXPECT_EQ(metadataInfo["agent"]["host"]["os"]["name"], "test_os"); EXPECT_EQ(metadataInfo["agent"]["host"]["os"]["platform"], "test_platform"); - EXPECT_EQ(metadataInfo["agent"]["host"]["ip"], "127.0.0.1"); + EXPECT_TRUE(metadataInfo["agent"]["host"]["ip"] != nullptr); + EXPECT_EQ(metadataInfo["agent"]["host"]["ip"][0], "127.0.0.1"); EXPECT_EQ(metadataInfo["agent"]["host"]["architecture"], "test_arch"); EXPECT_EQ(metadataInfo["agent"]["host"]["hostname"], "test_name"); } diff --git a/src/agent/src/message_queue_utils.cpp b/src/agent/src/message_queue_utils.cpp index 8f99a00327..e52f1c4789 100644 --- a/src/agent/src/message_queue_utils.cpp +++ b/src/agent/src/message_queue_utils.cpp @@ -19,12 +19,12 @@ boost::asio::awaitable GetMessagesFromQueue(std::shared_ptr Date: Wed, 13 Nov 2024 09:01:19 -0300 Subject: [PATCH 4/5] fix: agent_info reset to default values Due to recent changes in the agent's info implementation, where the sql logic was replaced with the SQLiteManager, a bug was introduced in the ResetToDefaults method by which the AgentInfo tables were not being cleaned up. --- .../agent_info/src/agent_info_persistance.cpp | 6 ++++-- src/agent/tests/agent_registration_test.cpp | 17 +++++++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/agent/agent_info/src/agent_info_persistance.cpp b/src/agent/agent_info/src/agent_info_persistance.cpp index d51c80ce99..ffda1e5566 100644 --- a/src/agent/agent_info/src/agent_info_persistance.cpp +++ b/src/agent/agent_info/src/agent_info_persistance.cpp @@ -223,8 +223,10 @@ void AgentInfoPersistance::ResetToDefault() { try { - m_db->Remove(AGENT_INFO_TABLE_NAME); - m_db->Remove(AGENT_GROUP_TABLE_NAME); + m_db->DropTable(AGENT_INFO_TABLE_NAME); + m_db->DropTable(AGENT_GROUP_TABLE_NAME); + CreateAgentInfoTable(); + CreateAgentGroupTable(); InsertDefaultAgentInfo(); } catch (const std::exception& e) diff --git a/src/agent/tests/agent_registration_test.cpp b/src/agent/tests/agent_registration_test.cpp index 1a6a4ab24f..ce8f1c2030 100644 --- a/src/agent/tests/agent_registration_test.cpp +++ b/src/agent/tests/agent_registration_test.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -67,6 +68,9 @@ class RegisterTest : public ::testing::Test TEST_F(RegisterTest, RegistrationTestSuccess) { + AgentInfoPersistance agentInfoPersistance; + agentInfoPersistance.ResetToDefault(); + registration = std::make_unique( "user", "password", "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj", "agent_name", std::nullopt); SysInfo sysInfo; @@ -78,7 +82,7 @@ TEST_F(RegisterTest, RegistrationTestSuccess) EXPECT_CALL(mockHttpClient, AuthenticateWithUserPassword(testing::_, testing::_, testing::_, testing::_)) .WillOnce(testing::Return("token")); - nlohmann::json bodyJson = agent->GetMetadataInfo(true); + const auto bodyJson = agent->GetMetadataInfo(true); http_client::HttpRequestParams reqParams(boost::beast::http::verb::post, "https://localhost:55000", @@ -100,6 +104,9 @@ TEST_F(RegisterTest, RegistrationTestSuccess) TEST_F(RegisterTest, RegistrationFailsIfAuthenticationFails) { + AgentInfoPersistance agentInfoPersistance; + agentInfoPersistance.ResetToDefault(); + registration = std::make_unique( "user", "password", "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj", "agent_name", std::nullopt); agent = std::make_unique(); @@ -116,6 +123,9 @@ TEST_F(RegisterTest, RegistrationFailsIfAuthenticationFails) TEST_F(RegisterTest, RegistrationFailsIfServerResponseIsNotOk) { + AgentInfoPersistance agentInfoPersistance; + agentInfoPersistance.ResetToDefault(); + registration = std::make_unique( "user", "password", "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj", "agent_name", std::nullopt); agent = std::make_unique(); @@ -137,6 +147,9 @@ TEST_F(RegisterTest, RegistrationFailsIfServerResponseIsNotOk) TEST_F(RegisterTest, RegistrationTestSuccessWithEmptyKey) { + AgentInfoPersistance agentInfoPersistance; + agentInfoPersistance.ResetToDefault(); + registration = std::make_unique("user", "password", "", "agent_name", std::nullopt); SysInfo sysInfo; @@ -148,7 +161,7 @@ TEST_F(RegisterTest, RegistrationTestSuccessWithEmptyKey) EXPECT_CALL(mockHttpClient, AuthenticateWithUserPassword(testing::_, testing::_, testing::_, testing::_)) .WillOnce(testing::Return("token")); - nlohmann::json bodyJson = agent->GetMetadataInfo(true); + const auto bodyJson = agent->GetMetadataInfo(true); http_client::HttpRequestParams reqParams(boost::beast::http::verb::post, "https://localhost:55000", From 5e5190c3ceedcf27628da80121c8bda21f4544d9 Mon Sep 17 00:00:00 2001 From: Tomas Turina Date: Wed, 13 Nov 2024 15:02:02 +0000 Subject: [PATCH 5/5] fix: metadatainfo is cached but not reloaded when agent info is changed --- src/agent/agent_info/include/agent_info.hpp | 6 --- src/agent/agent_info/src/agent_info.cpp | 53 +++++++-------------- src/agent/src/agent_registration.cpp | 2 +- 3 files changed, 19 insertions(+), 42 deletions(-) diff --git a/src/agent/agent_info/include/agent_info.hpp b/src/agent/agent_info/include/agent_info.hpp index d6259ae871..006863a684 100644 --- a/src/agent/agent_info/include/agent_info.hpp +++ b/src/agent/agent_info/include/agent_info.hpp @@ -92,9 +92,6 @@ class AgentInfo /// @brief Loads the endpoint information into `m_endpointInfo`. void LoadEndpointInfo(); - /// @brief Loads the metadata information into `m_metadataInfo`. - void LoadMetadataInfo(); - /// @brief Loads the header information into `m_headerInfo`. void LoadHeaderInfo(); @@ -118,9 +115,6 @@ class AgentInfo /// @brief The agent's endpoint information. nlohmann::json m_endpointInfo; - /// @brief The agent's metadata information. - nlohmann::json m_metadataInfo; - /// @brief The agent's header information. std::string m_headerInfo; diff --git a/src/agent/agent_info/src/agent_info.cpp b/src/agent/agent_info/src/agent_info.cpp index 7ca974f8f1..120d11ef9f 100644 --- a/src/agent/agent_info/src/agent_info.cpp +++ b/src/agent/agent_info/src/agent_info.cpp @@ -41,7 +41,6 @@ AgentInfo::AgentInfo(std::function getOSInfo, std::function AgentInfo::GetActiveIPAddresses(const nlohmann::json& networksJson) const @@ -185,9 +193,10 @@ void AgentInfo::LoadEndpointInfo() { nlohmann::json osInfo = m_getOSInfo(); m_endpointInfo["hostname"] = osInfo.value("hostname", "Unknown"); - m_endpointInfo["os"] = osInfo.value("os_name", "Unknown"); - m_endpointInfo["platform"] = osInfo.value("sysname", "Unknown"); m_endpointInfo["architecture"] = osInfo.value("architecture", "Unknown"); + m_endpointInfo["os"] = nlohmann::json::object(); + m_endpointInfo["os"]["name"] = osInfo.value("os_name", "Unknown"); + m_endpointInfo["os"]["platform"] = osInfo.value("sysname", "Unknown"); } if (m_getNetworksInfo != nullptr) @@ -197,39 +206,13 @@ void AgentInfo::LoadEndpointInfo() } } -void AgentInfo::LoadMetadataInfo() -{ - m_metadataInfo["id"] = GetUUID(); - m_metadataInfo["name"] = GetName(); - m_metadataInfo["type"] = GetType(); - m_metadataInfo["version"] = GetVersion(); - m_metadataInfo["groups"] = GetGroups(); - - if (!m_endpointInfo.empty()) - { - nlohmann::json host; - nlohmann::json os; - - host["ip"] = m_endpointInfo["ip"]; - host["hostname"] = m_endpointInfo.value("hostname", "Unknown"); - host["architecture"] = m_endpointInfo.value("architecture", "Unknown"); - - os["name"] = m_endpointInfo.value("os", "Unknown"); - os["platform"] = m_endpointInfo.value("platform", "Unknown"); - - host["os"] = os; - - m_metadataInfo["host"] = host; - } -} - void AgentInfo::LoadHeaderInfo() { - if (!m_endpointInfo.empty()) + if (!m_endpointInfo.empty() && m_endpointInfo.contains("os")) { m_headerInfo = PRODUCT_NAME + "/" + GetVersion() + " (" + GetType() + "; " + m_endpointInfo.value("architecture", "Unknown") + "; " + - m_endpointInfo.value("platform", "Unknown") + ")"; + m_endpointInfo["os"].value("platform", "Unknown") + ")"; } else { diff --git a/src/agent/src/agent_registration.cpp b/src/agent/src/agent_registration.cpp index 1cd1a000e9..eb32b57f65 100644 --- a/src/agent/src/agent_registration.cpp +++ b/src/agent/src/agent_registration.cpp @@ -32,7 +32,7 @@ namespace agent_registration } else { - m_agentInfo.SetName(boost::asio::ip::host_name()); + m_agentInfo.SetName(m_sysInfo.os().value("hostname", "Unknown")); } }