Skip to content

Commit

Permalink
feat(AgentRegistration): makes validationMode a member of AgentRegist…
Browse files Browse the repository at this point in the history
…ration class
  • Loading branch information
Nicogp committed Dec 26, 2024
1 parent 16d5f60 commit b803df7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
10 changes: 7 additions & 3 deletions src/agent/include/agent_registration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
10 changes: 6 additions & 4 deletions src/agent/src/agent_registration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand All @@ -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())
{
Expand All @@ -48,7 +50,7 @@ namespace agent_registration
m_serverUrl,
"/agents",
m_agentInfo.GetHeaderInfo(),
verificationMode,
m_verificationMode,
token.value(),
"",
m_agentInfo.GetMetadataInfo(true));
Expand Down
4 changes: 2 additions & 2 deletions src/agent/src/process_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down
25 changes: 15 additions & 10 deletions src/agent/tests/agent_registration_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ TEST_F(RegisterTest, RegistrationTestSuccess)
agent->Save();

registration = std::make_unique<agent_registration::AgentRegistration>(
"https://localhost:55000", "user", "password", agent->GetKey(), agent->GetName(), ".");
"https://localhost:55000", "user", "password", agent->GetKey(), agent->GetName(), ".", "full");

MockHttpClient mockHttpClient;

Expand All @@ -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);
}

Expand All @@ -74,8 +74,13 @@ TEST_F(RegisterTest, RegistrationFailsIfAuthenticationFails)
AgentInfoPersistance agentInfoPersistance(".");
agentInfoPersistance.ResetToDefault();

registration = std::make_unique<agent_registration::AgentRegistration>(
"https://localhost:55000", "user", "password", "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj", "agent_name", ".");
registration = std::make_unique<agent_registration::AgentRegistration>("https://localhost:55000",
"user",
"password",
"4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj",
"agent_name",
".",
"certificate");
agent = std::make_unique<AgentInfo>(".");

MockHttpClient mockHttpClient;
Expand All @@ -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);
}

Expand All @@ -94,7 +99,7 @@ TEST_F(RegisterTest, RegistrationFailsIfServerResponseIsNotOk)
agentInfoPersistance.ResetToDefault();

registration = std::make_unique<agent_registration::AgentRegistration>(
"https://localhost:55000", "user", "password", "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj", "agent_name", ".");
"https://localhost:55000", "user", "password", "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj", "agent_name", ".", "none");
agent = std::make_unique<AgentInfo>(".");

MockHttpClient mockHttpClient;
Expand All @@ -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);
}

Expand All @@ -121,7 +126,7 @@ TEST_F(RegisterTest, RegisteringWithoutAKeyGeneratesOneAutomatically)
EXPECT_TRUE(agent->GetKey().empty());

registration = std::make_unique<agent_registration::AgentRegistration>(
"https://localhost:55000", "user", "password", "", "agent_name", ".");
"https://localhost:55000", "user", "password", "", "agent_name", ".", "full");

MockHttpClient mockHttpClient;

Expand All @@ -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<AgentInfo>(".");
Expand All @@ -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);
}

Expand Down

0 comments on commit b803df7

Please sign in to comment.