From b803df76b013473314623fe74b02d93c5e0d58f8 Mon Sep 17 00:00:00 2001 From: Nicolas Gomez Palacios Date: Thu, 26 Dec 2024 13:25:35 -0300 Subject: [PATCH] feat(AgentRegistration): makes validationMode a member of AgentRegistration class --- src/agent/include/agent_registration.hpp | 10 ++++++--- src/agent/src/agent_registration.cpp | 10 +++++---- src/agent/src/process_options.cpp | 4 ++-- src/agent/tests/agent_registration_test.cpp | 25 ++++++++++++--------- 4 files changed, 30 insertions(+), 19 deletions(-) diff --git a/src/agent/include/agent_registration.hpp b/src/agent/include/agent_registration.hpp index 264f94b10b..397c8983d8 100644 --- a/src/agent/include/agent_registration.hpp +++ b/src/agent/include/agent_registration.hpp @@ -33,19 +33,20 @@ namespace agent_registration /// @param key The agent's key. /// @param name The agent's name. /// @param dbFolderPath The path to the database folder. + /// @param verificationMode The connection verification mode. AgentRegistration(std::string url, std::string user, std::string password, const std::string& key, const std::string& name, - const std::string& dbFolderPath); + const std::string& dbFolderPath, + std::string verificationMode); /// @brief Registers the agent with the manager. /// /// @param httpClient The HTTP client to use for registration. - /// @param verificationMode The verification mode to use for registration. /// @return True if the registration was successful, false otherwise. - bool Register(http_client::IHttpClient& httpClient, const std::string& verificationMode); + bool Register(http_client::IHttpClient& httpClient); private: /// @brief The system's information. @@ -62,5 +63,8 @@ namespace agent_registration /// @brief The user's password. std::string m_password; + + /// @brief The connection verification mode. + std::string m_verificationMode; }; } // namespace agent_registration diff --git a/src/agent/src/agent_registration.cpp b/src/agent/src/agent_registration.cpp index fdfc7b24aa..432d8fbc24 100644 --- a/src/agent/src/agent_registration.cpp +++ b/src/agent/src/agent_registration.cpp @@ -15,12 +15,14 @@ namespace agent_registration std::string password, const std::string& key, const std::string& name, - const std::string& dbFolderPath) + const std::string& dbFolderPath, + std::string verificationMode) : m_agentInfo( dbFolderPath, [this]() { return m_sysInfo.os(); }, [this]() { return m_sysInfo.networks(); }) , m_serverUrl(std::move(url)) , m_user(std::move(user)) , m_password(std::move(password)) + , m_verificationMode(std::move(verificationMode)) { if (!m_agentInfo.SetKey(key)) { @@ -33,10 +35,10 @@ namespace agent_registration } } - bool AgentRegistration::Register(http_client::IHttpClient& httpClient, const std::string& verificationMode) + bool AgentRegistration::Register(http_client::IHttpClient& httpClient) { const auto token = httpClient.AuthenticateWithUserPassword( - m_serverUrl, m_agentInfo.GetHeaderInfo(), m_user, m_password, verificationMode); + m_serverUrl, m_agentInfo.GetHeaderInfo(), m_user, m_password, m_verificationMode); if (!token.has_value()) { @@ -48,7 +50,7 @@ namespace agent_registration m_serverUrl, "/agents", m_agentInfo.GetHeaderInfo(), - verificationMode, + m_verificationMode, token.value(), "", m_agentInfo.GetMetadataInfo(true)); diff --git a/src/agent/src/process_options.cpp b/src/agent/src/process_options.cpp index 5c64be7e96..618bca704c 100644 --- a/src/agent/src/process_options.cpp +++ b/src/agent/src/process_options.cpp @@ -28,10 +28,10 @@ void RegisterAgent(const std::string& url, { std::cout << "Starting wazuh-agent registration\n"; - agent_registration::AgentRegistration reg(url, user, password, key, name, dbFolderPath); + agent_registration::AgentRegistration reg(url, user, password, key, name, dbFolderPath, verificationMode); http_client::HttpClient httpClient; - if (reg.Register(httpClient, verificationMode)) + if (reg.Register(httpClient)) { std::cout << "wazuh-agent registered\n"; } diff --git a/src/agent/tests/agent_registration_test.cpp b/src/agent/tests/agent_registration_test.cpp index 7d9630a94d..54cdd33b6e 100644 --- a/src/agent/tests/agent_registration_test.cpp +++ b/src/agent/tests/agent_registration_test.cpp @@ -40,7 +40,7 @@ TEST_F(RegisterTest, RegistrationTestSuccess) agent->Save(); registration = std::make_unique( - "https://localhost:55000", "user", "password", agent->GetKey(), agent->GetName(), "."); + "https://localhost:55000", "user", "password", agent->GetKey(), agent->GetName(), ".", "full"); MockHttpClient mockHttpClient; @@ -65,7 +65,7 @@ TEST_F(RegisterTest, RegistrationTestSuccess) EXPECT_CALL(mockHttpClient, PerformHttpRequest(testing::Eq(reqParams))).WillOnce(testing::Return(expectedResponse)); // NOLINTNEXTLINE(cppcoreguidelines-init-variables) - const bool res = registration->Register(mockHttpClient, "full"); + const bool res = registration->Register(mockHttpClient); ASSERT_TRUE(res); } @@ -74,8 +74,13 @@ TEST_F(RegisterTest, RegistrationFailsIfAuthenticationFails) AgentInfoPersistance agentInfoPersistance("."); agentInfoPersistance.ResetToDefault(); - registration = std::make_unique( - "https://localhost:55000", "user", "password", "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj", "agent_name", "."); + registration = std::make_unique("https://localhost:55000", + "user", + "password", + "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj", + "agent_name", + ".", + "certificate"); agent = std::make_unique("."); MockHttpClient mockHttpClient; @@ -84,7 +89,7 @@ TEST_F(RegisterTest, RegistrationFailsIfAuthenticationFails) .WillOnce(testing::Return(std::nullopt)); // NOLINTNEXTLINE(cppcoreguidelines-init-variables) - const bool res = registration->Register(mockHttpClient, "full"); + const bool res = registration->Register(mockHttpClient); ASSERT_FALSE(res); } @@ -94,7 +99,7 @@ TEST_F(RegisterTest, RegistrationFailsIfServerResponseIsNotOk) agentInfoPersistance.ResetToDefault(); registration = std::make_unique( - "https://localhost:55000", "user", "password", "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj", "agent_name", "."); + "https://localhost:55000", "user", "password", "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj", "agent_name", ".", "none"); agent = std::make_unique("."); MockHttpClient mockHttpClient; @@ -108,7 +113,7 @@ TEST_F(RegisterTest, RegistrationFailsIfServerResponseIsNotOk) EXPECT_CALL(mockHttpClient, PerformHttpRequest(testing::_)).WillOnce(testing::Return(expectedResponse)); // NOLINTNEXTLINE(cppcoreguidelines-init-variables) - const bool res = registration->Register(mockHttpClient, "full"); + const bool res = registration->Register(mockHttpClient); ASSERT_FALSE(res); } @@ -121,7 +126,7 @@ TEST_F(RegisterTest, RegisteringWithoutAKeyGeneratesOneAutomatically) EXPECT_TRUE(agent->GetKey().empty()); registration = std::make_unique( - "https://localhost:55000", "user", "password", "", "agent_name", "."); + "https://localhost:55000", "user", "password", "", "agent_name", ".", "full"); MockHttpClient mockHttpClient; @@ -135,7 +140,7 @@ TEST_F(RegisterTest, RegisteringWithoutAKeyGeneratesOneAutomatically) EXPECT_CALL(mockHttpClient, PerformHttpRequest(testing::_)).WillOnce(testing::Return(expectedResponse)); // NOLINTNEXTLINE(cppcoreguidelines-init-variables) - const bool res = registration->Register(mockHttpClient, "full"); + const bool res = registration->Register(mockHttpClient); ASSERT_TRUE(res); agent = std::make_unique("."); @@ -145,7 +150,7 @@ TEST_F(RegisterTest, RegisteringWithoutAKeyGeneratesOneAutomatically) TEST_F(RegisterTest, RegistrationTestFailWithBadKey) { ASSERT_THROW(agent_registration::AgentRegistration( - "https://localhost:55000", "user", "password", "badKey", "agent_name", "."), + "https://localhost:55000", "user", "password", "badKey", "agent_name", ".", "full"), std::invalid_argument); }