From d49f9c956ed66c48f46b87427316fabf71e112e6 Mon Sep 17 00:00:00 2001 From: Vikman Fernandez-Castro Date: Tue, 19 Nov 2024 12:07:25 +0100 Subject: [PATCH 1/8] fix(agent): prevent implicit agent info persistence on change --- src/agent/agent_info/src/agent_info.cpp | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/src/agent/agent_info/src/agent_info.cpp b/src/agent/agent_info/src/agent_info.cpp index c4eb78e009..4b918973e1 100644 --- a/src/agent/agent_info/src/agent_info.cpp +++ b/src/agent/agent_info/src/agent_info.cpp @@ -27,7 +27,6 @@ AgentInfo::AgentInfo(std::function getOSInfo, std::function AgentInfo::GetGroups() const void AgentInfo::SetName(const std::string& name) { - AgentInfoPersistance agentInfoPersistance; - agentInfoPersistance.SetName(name); m_name = name; } bool AgentInfo::SetKey(const std::string& key) { - AgentInfoPersistance agentInfoPersistance; - if (!key.empty()) { if (!ValidateKey(key)) @@ -88,22 +83,17 @@ bool AgentInfo::SetKey(const std::string& key) m_key = CreateKey(); } - agentInfoPersistance.SetKey(m_key); - return true; } + void AgentInfo::SetUUID(const std::string& uuid) { - AgentInfoPersistance agentInfoPersistance; - agentInfoPersistance.SetUUID(uuid); m_uuid = uuid; } void AgentInfo::SetGroups(const std::vector& groupList) { - AgentInfoPersistance agentInfoPersistance; - agentInfoPersistance.SetGroups(groupList); m_groups = groupList; } From 848e6abc9206d812eea3b14a9b0211d6d2012ae5 Mon Sep 17 00:00:00 2001 From: Vikman Fernandez-Castro Date: Tue, 19 Nov 2024 12:15:41 +0100 Subject: [PATCH 2/8] feat(agent): persist agent info on successful registration only --- src/agent/agent_info/include/agent_info.hpp | 3 +++ src/agent/agent_info/src/agent_info.cpp | 8 ++++++++ src/agent/src/agent_registration.cpp | 1 + 3 files changed, 12 insertions(+) diff --git a/src/agent/agent_info/include/agent_info.hpp b/src/agent/agent_info/include/agent_info.hpp index 006863a684..a2f8a0195c 100644 --- a/src/agent/agent_info/include/agent_info.hpp +++ b/src/agent/agent_info/include/agent_info.hpp @@ -75,6 +75,9 @@ class AgentInfo /// @return A string with all information about the agent. std::string GetMetadataInfo(const bool agentIsRegistering) const; + /// @brief Saves the agent's information to the database. + void Save() 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 4b918973e1..28f908b97a 100644 --- a/src/agent/agent_info/src/agent_info.cpp +++ b/src/agent/agent_info/src/agent_info.cpp @@ -158,6 +158,14 @@ std::string AgentInfo::GetMetadataInfo(const bool agentIsRegistering) const return agentMetadataInfo.dump(); } +void AgentInfo::Save() const { + AgentInfoPersistance agentInfoPersistance; + agentInfoPersistance.SetName(m_name); + agentInfoPersistance.SetKey(m_key); + agentInfoPersistance.SetUUID(m_uuid); + agentInfoPersistance.SetGroups(m_groups); +} + std::vector AgentInfo::GetActiveIPAddresses(const nlohmann::json& networksJson) const { std::vector ipAddresses; diff --git a/src/agent/src/agent_registration.cpp b/src/agent/src/agent_registration.cpp index 692e579797..56d88ff4d0 100644 --- a/src/agent/src/agent_registration.cpp +++ b/src/agent/src/agent_registration.cpp @@ -66,6 +66,7 @@ namespace agent_registration return false; } + m_agentInfo.Save(); return true; } From 3ada0d97490fd922a8d02c9ce290ebc41fa2ebce Mon Sep 17 00:00:00 2001 From: Vikman Fernandez-Castro Date: Tue, 19 Nov 2024 12:32:47 +0100 Subject: [PATCH 3/8] test(agent): update persistence policy in agent info unit tests --- src/agent/agent_info/tests/agent_info_test.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/agent/agent_info/tests/agent_info_test.cpp b/src/agent/agent_info/tests/agent_info_test.cpp index c87e677e09..b8e1f779d0 100644 --- a/src/agent/agent_info/tests/agent_info_test.cpp +++ b/src/agent/agent_info/tests/agent_info_test.cpp @@ -36,6 +36,7 @@ TEST_F(AgentInfoTest, TestPersistedValues) agentInfo.SetName("test_name"); agentInfo.SetKey("4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj"); agentInfo.SetUUID("test_uuid"); + agentInfo.Save(); const AgentInfo agentInfoReloaded; EXPECT_EQ(agentInfoReloaded.GetName(), "test_name"); EXPECT_EQ(agentInfoReloaded.GetKey(), "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj"); @@ -45,25 +46,27 @@ TEST_F(AgentInfoTest, TestPersistedValues) TEST_F(AgentInfoTest, TestSetName) { AgentInfo agentInfo; + const std::string oldName = agentInfo.GetName(); const std::string newName = "new_name"; agentInfo.SetName(newName); EXPECT_EQ(agentInfo.GetName(), newName); const AgentInfo agentInfoReloaded; - EXPECT_EQ(agentInfoReloaded.GetName(), newName); + EXPECT_EQ(agentInfoReloaded.GetName(), oldName); } TEST_F(AgentInfoTest, TestSetKey) { AgentInfo agentInfo; + const std::string oldKey = agentInfo.GetKey(); const std::string newKey = "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj"; agentInfo.SetKey(newKey); EXPECT_EQ(agentInfo.GetKey(), newKey); const AgentInfo agentInfoReloaded; - EXPECT_EQ(agentInfoReloaded.GetKey(), newKey); + EXPECT_EQ(agentInfoReloaded.GetKey(), oldKey); } TEST_F(AgentInfoTest, TestSetBadKey) @@ -80,12 +83,13 @@ TEST_F(AgentInfoTest, TestSetEmptyKey) { AgentInfo agentInfo; const std::string newKey; + const std::string oldKey = agentInfo.GetKey(); agentInfo.SetKey(newKey); EXPECT_NE(agentInfo.GetKey(), newKey); const AgentInfo agentInfoReloaded; - EXPECT_NE(agentInfoReloaded.GetKey(), newKey); + EXPECT_EQ(agentInfoReloaded.GetKey(), oldKey); } TEST_F(AgentInfoTest, TestSetUUID) @@ -97,19 +101,20 @@ TEST_F(AgentInfoTest, TestSetUUID) EXPECT_EQ(agentInfo.GetUUID(), newUUID); const AgentInfo agentInfoReloaded; - EXPECT_EQ(agentInfoReloaded.GetUUID(), newUUID); + EXPECT_NE(agentInfoReloaded.GetUUID(), newUUID); } TEST_F(AgentInfoTest, TestSetGroups) { AgentInfo agentInfo; + const std::vector oldGroups = agentInfo.GetGroups(); const std::vector newGroups = {"t_group_1", "t_group_2"}; agentInfo.SetGroups(newGroups); EXPECT_EQ(agentInfo.GetGroups(), newGroups); const AgentInfo agentInfoReloaded; - EXPECT_EQ(agentInfoReloaded.GetGroups(), newGroups); + EXPECT_EQ(agentInfoReloaded.GetGroups(), oldGroups); } TEST_F(AgentInfoTest, TestLoadMetadataInfoNoSysInfo) From c760a319647fa170d9b17dbf9159314c3b8d1824 Mon Sep 17 00:00:00 2001 From: Vikman Fernandez-Castro Date: Tue, 19 Nov 2024 15:10:11 +0100 Subject: [PATCH 4/8] test(agent): update persistence policy in agent registration unit tests --- src/agent/tests/agent_registration_test.cpp | 24 +++++++-------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/agent/tests/agent_registration_test.cpp b/src/agent/tests/agent_registration_test.cpp index ce8f1c2030..fcb56109b3 100644 --- a/src/agent/tests/agent_registration_test.cpp +++ b/src/agent/tests/agent_registration_test.cpp @@ -71,12 +71,17 @@ TEST_F(RegisterTest, RegistrationTestSuccess) AgentInfoPersistance agentInfoPersistance; agentInfoPersistance.ResetToDefault(); - registration = std::make_unique( - "user", "password", "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj", "agent_name", std::nullopt); SysInfo sysInfo; agent = std::make_unique([&sysInfo]() mutable { return sysInfo.os(); }, [&sysInfo]() mutable { return sysInfo.networks(); }); + agent->SetKey("4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj"); + agent->SetName("agent_name"); + agent->Save(); + + registration = std::make_unique( + "user", "password", "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj", "agent_name", std::nullopt); + MockHttpClient mockHttpClient; EXPECT_CALL(mockHttpClient, AuthenticateWithUserPassword(testing::_, testing::_, testing::_, testing::_)) @@ -152,29 +157,16 @@ TEST_F(RegisterTest, RegistrationTestSuccessWithEmptyKey) 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(); }); MockHttpClient mockHttpClient; EXPECT_CALL(mockHttpClient, AuthenticateWithUserPassword(testing::_, testing::_, testing::_, testing::_)) .WillOnce(testing::Return("token")); - const auto bodyJson = agent->GetMetadataInfo(true); - - http_client::HttpRequestParams reqParams(boost::beast::http::verb::post, - "https://localhost:55000", - "/agents", - agent->GetHeaderInfo(), - "token", - "", - bodyJson); - boost::beast::http::response expectedResponse; expectedResponse.result(boost::beast::http::status::ok); - EXPECT_CALL(mockHttpClient, PerformHttpRequest(testing::Eq(reqParams))).WillOnce(testing::Return(expectedResponse)); + EXPECT_CALL(mockHttpClient, PerformHttpRequest(testing::_)).WillOnce(testing::Return(expectedResponse)); // NOLINTNEXTLINE(cppcoreguidelines-init-variables) const bool res = registration->Register(mockHttpClient); From dbff0d59d3766cb90a05114c3b677a940e1b53d9 Mon Sep 17 00:00:00 2001 From: Vikman Fernandez-Castro Date: Tue, 19 Nov 2024 15:16:32 +0100 Subject: [PATCH 5/8] fix(agent): expect status code 201 at registration request --- src/agent/src/agent_registration.cpp | 2 +- src/agent/tests/agent_registration_test.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/agent/src/agent_registration.cpp b/src/agent/src/agent_registration.cpp index 56d88ff4d0..6867f7638e 100644 --- a/src/agent/src/agent_registration.cpp +++ b/src/agent/src/agent_registration.cpp @@ -60,7 +60,7 @@ namespace agent_registration const auto res = httpClient.PerformHttpRequest(reqParams); - if (res.result() != http::status::ok) + if (res.result() != http::status::created) { std::cout << "Registration error: " << res.result_int() << ".\n"; return false; diff --git a/src/agent/tests/agent_registration_test.cpp b/src/agent/tests/agent_registration_test.cpp index fcb56109b3..53cff6014d 100644 --- a/src/agent/tests/agent_registration_test.cpp +++ b/src/agent/tests/agent_registration_test.cpp @@ -98,7 +98,7 @@ TEST_F(RegisterTest, RegistrationTestSuccess) bodyJson); boost::beast::http::response expectedResponse; - expectedResponse.result(boost::beast::http::status::ok); + expectedResponse.result(boost::beast::http::status::created); EXPECT_CALL(mockHttpClient, PerformHttpRequest(testing::Eq(reqParams))).WillOnce(testing::Return(expectedResponse)); @@ -164,7 +164,7 @@ TEST_F(RegisterTest, RegistrationTestSuccessWithEmptyKey) .WillOnce(testing::Return("token")); boost::beast::http::response expectedResponse; - expectedResponse.result(boost::beast::http::status::ok); + expectedResponse.result(boost::beast::http::status::created); EXPECT_CALL(mockHttpClient, PerformHttpRequest(testing::_)).WillOnce(testing::Return(expectedResponse)); From bb77327988cd8050f708d9cf2c893149af281997 Mon Sep 17 00:00:00 2001 From: Tomas Turina Date: Tue, 19 Nov 2024 17:20:45 +0000 Subject: [PATCH 6/8] fix: fix style checks in agent_info.cpp file --- src/agent/agent_info/src/agent_info.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/agent/agent_info/src/agent_info.cpp b/src/agent/agent_info/src/agent_info.cpp index 28f908b97a..7df636afc8 100644 --- a/src/agent/agent_info/src/agent_info.cpp +++ b/src/agent/agent_info/src/agent_info.cpp @@ -86,7 +86,6 @@ bool AgentInfo::SetKey(const std::string& key) return true; } - void AgentInfo::SetUUID(const std::string& uuid) { m_uuid = uuid; @@ -158,7 +157,8 @@ std::string AgentInfo::GetMetadataInfo(const bool agentIsRegistering) const return agentMetadataInfo.dump(); } -void AgentInfo::Save() const { +void AgentInfo::Save() const +{ AgentInfoPersistance agentInfoPersistance; agentInfoPersistance.SetName(m_name); agentInfoPersistance.SetKey(m_key); From ba66f7ae9e2dbf43cba1ca42400c0c1b3cc7347a Mon Sep 17 00:00:00 2001 From: jr0me Date: Tue, 19 Nov 2024 14:53:22 -0300 Subject: [PATCH 7/8] fix: AgentRegistration tests changes Assert generation of keys when registering without providing one. --- src/agent/tests/agent_registration_test.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/agent/tests/agent_registration_test.cpp b/src/agent/tests/agent_registration_test.cpp index 53cff6014d..651be489ce 100644 --- a/src/agent/tests/agent_registration_test.cpp +++ b/src/agent/tests/agent_registration_test.cpp @@ -80,7 +80,7 @@ TEST_F(RegisterTest, RegistrationTestSuccess) agent->Save(); registration = std::make_unique( - "user", "password", "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj", "agent_name", std::nullopt); + "user", "password", agent->GetKey(), agent->GetName(), std::nullopt); MockHttpClient mockHttpClient; @@ -150,11 +150,14 @@ TEST_F(RegisterTest, RegistrationFailsIfServerResponseIsNotOk) ASSERT_FALSE(res); } -TEST_F(RegisterTest, RegistrationTestSuccessWithEmptyKey) +TEST_F(RegisterTest, RegisteringWithoutAKeyGeneratesOneAutomatically) { AgentInfoPersistance agentInfoPersistance; agentInfoPersistance.ResetToDefault(); + agent = std::make_unique(); + EXPECT_TRUE(agent->GetKey().empty()); + registration = std::make_unique("user", "password", "", "agent_name", std::nullopt); @@ -171,6 +174,9 @@ TEST_F(RegisterTest, RegistrationTestSuccessWithEmptyKey) // NOLINTNEXTLINE(cppcoreguidelines-init-variables) const bool res = registration->Register(mockHttpClient); ASSERT_TRUE(res); + + agent = std::make_unique(); + EXPECT_FALSE(agent->GetKey().empty()); } TEST_F(RegisterTest, RegistrationTestFailWithBadKey) From bd62a34bbec35dd5405069b38b82946ee32bf796 Mon Sep 17 00:00:00 2001 From: jr0me Date: Tue, 19 Nov 2024 14:56:54 -0300 Subject: [PATCH 8/8] fix: Update AgentInfo methods' documentation --- src/agent/agent_info/include/agent_info.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/agent/agent_info/include/agent_info.hpp b/src/agent/agent_info/include/agent_info.hpp index a2f8a0195c..c59040c857 100644 --- a/src/agent/agent_info/include/agent_info.hpp +++ b/src/agent/agent_info/include/agent_info.hpp @@ -41,20 +41,20 @@ class AgentInfo /// @return A vector of the agent's groups. std::vector GetGroups() const; - /// @brief Sets the agent's name. + /// @brief Sets the agent's name. The change is not saved to the database until `Save` is called. /// @param name The agent's new name. void SetName(const std::string& name); - /// @brief Sets the agent's key. + /// @brief Sets the agent's key. The change is not saved to the database until `Save` is called. /// @param key The agent's new key. /// @return True if the key was successfully set, false otherwise. bool SetKey(const std::string& key); - /// @brief Sets the agent's UUID. + /// @brief Sets the agent's UUID. The change is not saved to the database until `Save` is called. /// @param uuid The agent's new UUID. void SetUUID(const std::string& uuid); - /// @brief Sets the agent's groups. + /// @brief Sets the agent's groups. The change is not saved to the database until `Save` is called. /// @param groupList A vector of the agent's new groups. void SetGroups(const std::vector& groupList);