Skip to content

Commit a2405d2

Browse files
authored
Merge pull request #319 from wazuh/fix/314-registration-failure
Fix Agent Registration Behavior
2 parents df31b8d + bd62a34 commit a2405d2

File tree

5 files changed

+45
-40
lines changed

5 files changed

+45
-40
lines changed

src/agent/agent_info/include/agent_info.hpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,20 @@ class AgentInfo
4141
/// @return A vector of the agent's groups.
4242
std::vector<std::string> GetGroups() const;
4343

44-
/// @brief Sets the agent's name.
44+
/// @brief Sets the agent's name. The change is not saved to the database until `Save` is called.
4545
/// @param name The agent's new name.
4646
void SetName(const std::string& name);
4747

48-
/// @brief Sets the agent's key.
48+
/// @brief Sets the agent's key. The change is not saved to the database until `Save` is called.
4949
/// @param key The agent's new key.
5050
/// @return True if the key was successfully set, false otherwise.
5151
bool SetKey(const std::string& key);
5252

53-
/// @brief Sets the agent's UUID.
53+
/// @brief Sets the agent's UUID. The change is not saved to the database until `Save` is called.
5454
/// @param uuid The agent's new UUID.
5555
void SetUUID(const std::string& uuid);
5656

57-
/// @brief Sets the agent's groups.
57+
/// @brief Sets the agent's groups. The change is not saved to the database until `Save` is called.
5858
/// @param groupList A vector of the agent's new groups.
5959
void SetGroups(const std::vector<std::string>& groupList);
6060

@@ -75,6 +75,9 @@ class AgentInfo
7575
/// @return A string with all information about the agent.
7676
std::string GetMetadataInfo(const bool agentIsRegistering) const;
7777

78+
/// @brief Saves the agent's information to the database.
79+
void Save() const;
80+
7881
private:
7982
/// @brief Creates a random key for the agent.
8083
///

src/agent/agent_info/src/agent_info.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ AgentInfo::AgentInfo(std::function<nlohmann::json()> getOSInfo, std::function<nl
2727
if (m_uuid.empty())
2828
{
2929
m_uuid = boost::uuids::to_string(boost::uuids::random_generator()());
30-
agentInfoPersistance.SetUUID(m_uuid);
3130
}
3231

3332
if (getOSInfo != nullptr)
@@ -66,15 +65,11 @@ std::vector<std::string> AgentInfo::GetGroups() const
6665

6766
void AgentInfo::SetName(const std::string& name)
6867
{
69-
AgentInfoPersistance agentInfoPersistance;
70-
agentInfoPersistance.SetName(name);
7168
m_name = name;
7269
}
7370

7471
bool AgentInfo::SetKey(const std::string& key)
7572
{
76-
AgentInfoPersistance agentInfoPersistance;
77-
7873
if (!key.empty())
7974
{
8075
if (!ValidateKey(key))
@@ -88,22 +83,16 @@ bool AgentInfo::SetKey(const std::string& key)
8883
m_key = CreateKey();
8984
}
9085

91-
agentInfoPersistance.SetKey(m_key);
92-
9386
return true;
9487
}
9588

9689
void AgentInfo::SetUUID(const std::string& uuid)
9790
{
98-
AgentInfoPersistance agentInfoPersistance;
99-
agentInfoPersistance.SetUUID(uuid);
10091
m_uuid = uuid;
10192
}
10293

10394
void AgentInfo::SetGroups(const std::vector<std::string>& groupList)
10495
{
105-
AgentInfoPersistance agentInfoPersistance;
106-
agentInfoPersistance.SetGroups(groupList);
10796
m_groups = groupList;
10897
}
10998

@@ -168,6 +157,15 @@ std::string AgentInfo::GetMetadataInfo(const bool agentIsRegistering) const
168157
return agentMetadataInfo.dump();
169158
}
170159

160+
void AgentInfo::Save() const
161+
{
162+
AgentInfoPersistance agentInfoPersistance;
163+
agentInfoPersistance.SetName(m_name);
164+
agentInfoPersistance.SetKey(m_key);
165+
agentInfoPersistance.SetUUID(m_uuid);
166+
agentInfoPersistance.SetGroups(m_groups);
167+
}
168+
171169
std::vector<std::string> AgentInfo::GetActiveIPAddresses(const nlohmann::json& networksJson) const
172170
{
173171
std::vector<std::string> ipAddresses;

src/agent/agent_info/tests/agent_info_test.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ TEST_F(AgentInfoTest, TestPersistedValues)
3636
agentInfo.SetName("test_name");
3737
agentInfo.SetKey("4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj");
3838
agentInfo.SetUUID("test_uuid");
39+
agentInfo.Save();
3940
const AgentInfo agentInfoReloaded;
4041
EXPECT_EQ(agentInfoReloaded.GetName(), "test_name");
4142
EXPECT_EQ(agentInfoReloaded.GetKey(), "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj");
@@ -45,25 +46,27 @@ TEST_F(AgentInfoTest, TestPersistedValues)
4546
TEST_F(AgentInfoTest, TestSetName)
4647
{
4748
AgentInfo agentInfo;
49+
const std::string oldName = agentInfo.GetName();
4850
const std::string newName = "new_name";
4951

5052
agentInfo.SetName(newName);
5153
EXPECT_EQ(agentInfo.GetName(), newName);
5254

5355
const AgentInfo agentInfoReloaded;
54-
EXPECT_EQ(agentInfoReloaded.GetName(), newName);
56+
EXPECT_EQ(agentInfoReloaded.GetName(), oldName);
5557
}
5658

5759
TEST_F(AgentInfoTest, TestSetKey)
5860
{
5961
AgentInfo agentInfo;
62+
const std::string oldKey = agentInfo.GetKey();
6063
const std::string newKey = "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj";
6164

6265
agentInfo.SetKey(newKey);
6366
EXPECT_EQ(agentInfo.GetKey(), newKey);
6467

6568
const AgentInfo agentInfoReloaded;
66-
EXPECT_EQ(agentInfoReloaded.GetKey(), newKey);
69+
EXPECT_EQ(agentInfoReloaded.GetKey(), oldKey);
6770
}
6871

6972
TEST_F(AgentInfoTest, TestSetBadKey)
@@ -80,12 +83,13 @@ TEST_F(AgentInfoTest, TestSetEmptyKey)
8083
{
8184
AgentInfo agentInfo;
8285
const std::string newKey;
86+
const std::string oldKey = agentInfo.GetKey();
8387

8488
agentInfo.SetKey(newKey);
8589
EXPECT_NE(agentInfo.GetKey(), newKey);
8690

8791
const AgentInfo agentInfoReloaded;
88-
EXPECT_NE(agentInfoReloaded.GetKey(), newKey);
92+
EXPECT_EQ(agentInfoReloaded.GetKey(), oldKey);
8993
}
9094

9195
TEST_F(AgentInfoTest, TestSetUUID)
@@ -97,19 +101,20 @@ TEST_F(AgentInfoTest, TestSetUUID)
97101
EXPECT_EQ(agentInfo.GetUUID(), newUUID);
98102

99103
const AgentInfo agentInfoReloaded;
100-
EXPECT_EQ(agentInfoReloaded.GetUUID(), newUUID);
104+
EXPECT_NE(agentInfoReloaded.GetUUID(), newUUID);
101105
}
102106

103107
TEST_F(AgentInfoTest, TestSetGroups)
104108
{
105109
AgentInfo agentInfo;
110+
const std::vector<std::string> oldGroups = agentInfo.GetGroups();
106111
const std::vector<std::string> newGroups = {"t_group_1", "t_group_2"};
107112

108113
agentInfo.SetGroups(newGroups);
109114
EXPECT_EQ(agentInfo.GetGroups(), newGroups);
110115

111116
const AgentInfo agentInfoReloaded;
112-
EXPECT_EQ(agentInfoReloaded.GetGroups(), newGroups);
117+
EXPECT_EQ(agentInfoReloaded.GetGroups(), oldGroups);
113118
}
114119

115120
TEST_F(AgentInfoTest, TestLoadMetadataInfoNoSysInfo)

src/agent/src/agent_registration.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,13 @@ namespace agent_registration
6060

6161
const auto res = httpClient.PerformHttpRequest(reqParams);
6262

63-
if (res.result() != http::status::ok)
63+
if (res.result() != http::status::created)
6464
{
6565
std::cout << "Registration error: " << res.result_int() << ".\n";
6666
return false;
6767
}
6868

69+
m_agentInfo.Save();
6970
return true;
7071
}
7172

src/agent/tests/agent_registration_test.cpp

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,17 @@ TEST_F(RegisterTest, RegistrationTestSuccess)
7171
AgentInfoPersistance agentInfoPersistance;
7272
agentInfoPersistance.ResetToDefault();
7373

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

78+
agent->SetKey("4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj");
79+
agent->SetName("agent_name");
80+
agent->Save();
81+
82+
registration = std::make_unique<agent_registration::AgentRegistration>(
83+
"user", "password", agent->GetKey(), agent->GetName(), std::nullopt);
84+
8085
MockHttpClient mockHttpClient;
8186

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

95100
boost::beast::http::response<boost::beast::http::dynamic_body> expectedResponse;
96-
expectedResponse.result(boost::beast::http::status::ok);
101+
expectedResponse.result(boost::beast::http::status::created);
97102

98103
EXPECT_CALL(mockHttpClient, PerformHttpRequest(testing::Eq(reqParams))).WillOnce(testing::Return(expectedResponse));
99104

@@ -145,40 +150,33 @@ TEST_F(RegisterTest, RegistrationFailsIfServerResponseIsNotOk)
145150
ASSERT_FALSE(res);
146151
}
147152

148-
TEST_F(RegisterTest, RegistrationTestSuccessWithEmptyKey)
153+
TEST_F(RegisterTest, RegisteringWithoutAKeyGeneratesOneAutomatically)
149154
{
150155
AgentInfoPersistance agentInfoPersistance;
151156
agentInfoPersistance.ResetToDefault();
152157

158+
agent = std::make_unique<AgentInfo>();
159+
EXPECT_TRUE(agent->GetKey().empty());
160+
153161
registration =
154162
std::make_unique<agent_registration::AgentRegistration>("user", "password", "", "agent_name", std::nullopt);
155-
SysInfo sysInfo;
156-
agent = std::make_unique<AgentInfo>([&sysInfo]() mutable { return sysInfo.os(); },
157-
[&sysInfo]() mutable { return sysInfo.networks(); });
158163

159164
MockHttpClient mockHttpClient;
160165

161166
EXPECT_CALL(mockHttpClient, AuthenticateWithUserPassword(testing::_, testing::_, testing::_, testing::_))
162167
.WillOnce(testing::Return("token"));
163168

164-
const auto bodyJson = agent->GetMetadataInfo(true);
165-
166-
http_client::HttpRequestParams reqParams(boost::beast::http::verb::post,
167-
"https://localhost:55000",
168-
"/agents",
169-
agent->GetHeaderInfo(),
170-
"token",
171-
"",
172-
bodyJson);
173-
174169
boost::beast::http::response<boost::beast::http::dynamic_body> expectedResponse;
175-
expectedResponse.result(boost::beast::http::status::ok);
170+
expectedResponse.result(boost::beast::http::status::created);
176171

177-
EXPECT_CALL(mockHttpClient, PerformHttpRequest(testing::Eq(reqParams))).WillOnce(testing::Return(expectedResponse));
172+
EXPECT_CALL(mockHttpClient, PerformHttpRequest(testing::_)).WillOnce(testing::Return(expectedResponse));
178173

179174
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
180175
const bool res = registration->Register(mockHttpClient);
181176
ASSERT_TRUE(res);
177+
178+
agent = std::make_unique<AgentInfo>();
179+
EXPECT_FALSE(agent->GetKey().empty());
182180
}
183181

184182
TEST_F(RegisterTest, RegistrationTestFailWithBadKey)

0 commit comments

Comments
 (0)