Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Agent Registration Behavior #319

Merged
merged 8 commits into from
Nov 19, 2024
11 changes: 7 additions & 4 deletions src/agent/agent_info/include/agent_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@ class AgentInfo
/// @return A vector of the agent's groups.
std::vector<std::string> 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<std::string>& groupList);

Expand All @@ -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.
///
Expand Down
20 changes: 9 additions & 11 deletions src/agent/agent_info/src/agent_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ AgentInfo::AgentInfo(std::function<nlohmann::json()> getOSInfo, std::function<nl
if (m_uuid.empty())
{
m_uuid = boost::uuids::to_string(boost::uuids::random_generator()());
agentInfoPersistance.SetUUID(m_uuid);
}

if (getOSInfo != nullptr)
Expand Down Expand Up @@ -66,15 +65,11 @@ std::vector<std::string> 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))
Expand All @@ -88,22 +83,16 @@ 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<std::string>& groupList)
{
AgentInfoPersistance agentInfoPersistance;
agentInfoPersistance.SetGroups(groupList);
m_groups = groupList;
}

Expand Down Expand Up @@ -168,6 +157,15 @@ 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<std::string> AgentInfo::GetActiveIPAddresses(const nlohmann::json& networksJson) const
{
std::vector<std::string> ipAddresses;
Expand Down
15 changes: 10 additions & 5 deletions src/agent/agent_info/tests/agent_info_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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<std::string> oldGroups = agentInfo.GetGroups();
const std::vector<std::string> 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)
Expand Down
3 changes: 2 additions & 1 deletion src/agent/src/agent_registration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ 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;
}

m_agentInfo.Save();
return true;
}

Expand Down
36 changes: 17 additions & 19 deletions src/agent/tests/agent_registration_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,17 @@ TEST_F(RegisterTest, RegistrationTestSuccess)
AgentInfoPersistance agentInfoPersistance;
agentInfoPersistance.ResetToDefault();

registration = std::make_unique<agent_registration::AgentRegistration>(
"user", "password", "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj", "agent_name", std::nullopt);
SysInfo sysInfo;
agent = std::make_unique<AgentInfo>([&sysInfo]() mutable { return sysInfo.os(); },
[&sysInfo]() mutable { return sysInfo.networks(); });

agent->SetKey("4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj");
agent->SetName("agent_name");
agent->Save();

registration = std::make_unique<agent_registration::AgentRegistration>(
"user", "password", agent->GetKey(), agent->GetName(), std::nullopt);

MockHttpClient mockHttpClient;

EXPECT_CALL(mockHttpClient, AuthenticateWithUserPassword(testing::_, testing::_, testing::_, testing::_))
Expand All @@ -93,7 +98,7 @@ TEST_F(RegisterTest, RegistrationTestSuccess)
bodyJson);

boost::beast::http::response<boost::beast::http::dynamic_body> 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));

Expand Down Expand Up @@ -145,40 +150,33 @@ TEST_F(RegisterTest, RegistrationFailsIfServerResponseIsNotOk)
ASSERT_FALSE(res);
}

TEST_F(RegisterTest, RegistrationTestSuccessWithEmptyKey)
TEST_F(RegisterTest, RegisteringWithoutAKeyGeneratesOneAutomatically)
{
AgentInfoPersistance agentInfoPersistance;
agentInfoPersistance.ResetToDefault();

agent = std::make_unique<AgentInfo>();
EXPECT_TRUE(agent->GetKey().empty());

registration =
std::make_unique<agent_registration::AgentRegistration>("user", "password", "", "agent_name", std::nullopt);
SysInfo sysInfo;
agent = std::make_unique<AgentInfo>([&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<boost::beast::http::dynamic_body> 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));
EXPECT_CALL(mockHttpClient, PerformHttpRequest(testing::_)).WillOnce(testing::Return(expectedResponse));
jr0me marked this conversation as resolved.
Show resolved Hide resolved

// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
const bool res = registration->Register(mockHttpClient);
ASSERT_TRUE(res);

agent = std::make_unique<AgentInfo>();
EXPECT_FALSE(agent->GetKey().empty());
}

TEST_F(RegisterTest, RegistrationTestFailWithBadKey)
Expand Down
Loading