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

Adapt agent metadata to latest changes requested #290

Merged
merged 5 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/agent/agent_info/include/agent_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

/// @brief Stores and manages information about an agent.
///
/// This class provides methods for getting and setting the agent's key,
/// This class provides methods for getting and setting the agent's name, key,
/// UUID, and groups. It also includes private methods for creating and
/// validating the key.
class AgentInfo
Expand All @@ -25,6 +25,10 @@ class AgentInfo
AgentInfo(std::function<nlohmann::json()> getOSInfo = nullptr,
std::function<nlohmann::json()> getNetworksInfo = nullptr);

/// @brief Gets the agent's name.
/// @return The agent's name.
std::string GetName() const;

/// @brief Gets the agent's key.
/// @return The agent's key.
std::string GetKey() const;
Expand All @@ -37,6 +41,10 @@ class AgentInfo
/// @return A vector of the agent's groups.
std::vector<std::string> GetGroups() const;

/// @brief Sets the agent's name.
/// @param name The agent's new name.
void SetName(const std::string& name);

/// @brief Sets the agent's key.
/// @param key The agent's new key.
/// @return True if the key was successfully set, false otherwise.
Expand Down Expand Up @@ -64,8 +72,8 @@ class AgentInfo

/// @brief Gets all the information about the agent.
/// @param agentIsRegistering Indicates if the agent is about to register.
/// @return A json object with all information about the agent.
nlohmann::json GetMetadataInfo(const bool agentIsRegistering) const;
/// @return A string with all information about the agent.
std::string GetMetadataInfo(const bool agentIsRegistering) const;

private:
/// @brief Creates a random key for the agent.
Expand All @@ -92,8 +100,11 @@ class AgentInfo

/// @brief Extracts the active IP address from the network JSON data.
/// @param networksJson JSON object containing network interface information.
/// @return Optional string with the active IP address if found; otherwise, `std::nullopt`.
std::optional<std::string> GetActiveIPAddress(const nlohmann::json& networksJson) const;
/// @return Vector of strings with the active IP addresses.
std::vector<std::string> GetActiveIPAddresses(const nlohmann::json& networksJson) const;

/// @brief The agent's name.
std::string m_name;

/// @brief The agent's key.
std::string m_key;
Expand Down
31 changes: 23 additions & 8 deletions src/agent/agent_info/src/agent_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace
AgentInfo::AgentInfo(std::function<nlohmann::json()> getOSInfo, std::function<nlohmann::json()> getNetworksInfo)
{
AgentInfoPersistance agentInfoPersistance;
m_name = agentInfoPersistance.GetName();
m_key = agentInfoPersistance.GetKey();
m_uuid = agentInfoPersistance.GetUUID();
m_groups = agentInfoPersistance.GetGroups();
Expand All @@ -44,6 +45,11 @@ AgentInfo::AgentInfo(std::function<nlohmann::json()> getOSInfo, std::function<nl
LoadHeaderInfo();
}

std::string AgentInfo::GetName() const
{
return m_name;
}

std::string AgentInfo::GetKey() const
{
return m_key;
Expand All @@ -59,6 +65,13 @@ std::vector<std::string> AgentInfo::GetGroups() const
return m_groups;
}

void AgentInfo::SetName(const std::string& name)
{
AgentInfoPersistance agentInfoPersistance;
agentInfoPersistance.SetName(name);
m_name = name;
}

bool AgentInfo::SetKey(const std::string& key)
{
AgentInfoPersistance agentInfoPersistance;
Expand Down Expand Up @@ -132,7 +145,7 @@ std::string AgentInfo::GetHeaderInfo() const
return m_headerInfo;
}

nlohmann::json AgentInfo::GetMetadataInfo(const bool agentIsRegistering) const
std::string AgentInfo::GetMetadataInfo(const bool agentIsRegistering) const
{
nlohmann::json metadataInfo;

Expand All @@ -143,11 +156,13 @@ nlohmann::json AgentInfo::GetMetadataInfo(const bool agentIsRegistering) const
metadataInfo["agent"]["key"] = GetKey();
}

return metadataInfo;
return metadataInfo.dump();
}

std::optional<std::string> AgentInfo::GetActiveIPAddress(const nlohmann::json& networksJson) const
std::vector<std::string> AgentInfo::GetActiveIPAddresses(const nlohmann::json& networksJson) const
{
std::vector<std::string> ipAddresses;

if (networksJson.contains("iface"))
{
for (const auto& iface : networksJson["iface"])
Expand All @@ -156,12 +171,12 @@ std::optional<std::string> AgentInfo::GetActiveIPAddress(const nlohmann::json& n
{
if (iface.contains("IPv4") && !iface["IPv4"].empty())
{
return iface["IPv4"][0].value("address", "");
ipAddresses.emplace_back(iface["IPv4"][0].value("address", ""));
}
}
}
}
return std::nullopt;
return ipAddresses;
}

void AgentInfo::LoadEndpointInfo()
Expand All @@ -178,14 +193,14 @@ void AgentInfo::LoadEndpointInfo()
if (m_getNetworksInfo != nullptr)
{
nlohmann::json networksInfo = m_getNetworksInfo();
auto ipAddress = GetActiveIPAddress(networksInfo);
m_endpointInfo["ip"] = ipAddress.value_or("Unknown");
m_endpointInfo["ip"] = GetActiveIPAddresses(networksInfo);
}
}

void AgentInfo::LoadMetadataInfo()
{
m_metadataInfo["id"] = GetUUID();
m_metadataInfo["name"] = GetName();
m_metadataInfo["type"] = GetType();
m_metadataInfo["version"] = GetVersion();
m_metadataInfo["groups"] = GetGroups();
Expand All @@ -195,8 +210,8 @@ void AgentInfo::LoadMetadataInfo()
nlohmann::json host;
nlohmann::json os;

host["ip"] = m_endpointInfo["ip"];
host["hostname"] = m_endpointInfo.value("hostname", "Unknown");
host["ip"] = m_endpointInfo.value("ip", "Unknown");
host["architecture"] = m_endpointInfo.value("architecture", "Unknown");

os["name"] = m_endpointInfo.value("os", "Unknown");
Expand Down
16 changes: 14 additions & 2 deletions src/agent/agent_info/src/agent_info_persistance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ void AgentInfoPersistance::CreateAgentInfoTable()
{
try
{
const std::vector<Column> columns = {Column("key", ColumnType::TEXT, true, false),
const std::vector<Column> columns = {Column("name", ColumnType::TEXT, true, false),
Column("key", ColumnType::TEXT, true, false),
Column("uuid", ColumnType::TEXT, true, false, true)};

m_db->CreateTable(AGENT_INFO_TABLE_NAME, columns);
Expand Down Expand Up @@ -92,7 +93,8 @@ void AgentInfoPersistance::InsertDefaultAgentInfo()

if (count == 0)
{
const std::vector<Column> columns = {Column("key", ColumnType::TEXT, ""),
const std::vector<Column> columns = {Column("name", ColumnType::TEXT, ""),
Column("key", ColumnType::TEXT, ""),
Column("uuid", ColumnType::TEXT, "")};

m_db->Insert(AGENT_INFO_TABLE_NAME, columns);
Expand Down Expand Up @@ -139,6 +141,11 @@ std::string AgentInfoPersistance::GetAgentInfoValue(const std::string& column) c
return value;
}

std::string AgentInfoPersistance::GetName() const
{
return GetAgentInfoValue("name");
}

std::string AgentInfoPersistance::GetKey() const
{
return GetAgentInfoValue("key");
Expand Down Expand Up @@ -174,6 +181,11 @@ std::vector<std::string> AgentInfoPersistance::GetGroups() const
return groupList;
}

void AgentInfoPersistance::SetName(const std::string& name)
{
SetAgentInfoValue("name", name);
}

void AgentInfoPersistance::SetKey(const std::string& key)
{
SetAgentInfoValue("key", key);
Expand Down
8 changes: 8 additions & 0 deletions src/agent/agent_info/src/agent_info_persistance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class AgentInfoPersistance
/// @brief Deleted move assignment operator.
AgentInfoPersistance& operator=(AgentInfoPersistance&&) = delete;

/// @brief Retrieves the agent's name from the database.
/// @return The name of the agent as a string.
std::string GetName() const;

/// @brief Retrieves the agent's key from the database.
/// @return The key of the agent as a string.
std::string GetKey() const;
Expand All @@ -43,6 +47,10 @@ class AgentInfoPersistance
/// @return A vector of strings, each representing a group name.
std::vector<std::string> GetGroups() const;

/// @brief Sets the agent's name in the database.
/// @param name The name to set.
void SetName(const std::string& name);

/// @brief Sets the agent's key in the database.
/// @param key The key to set.
void SetKey(const std::string& key);
Expand Down
20 changes: 20 additions & 0 deletions src/agent/agent_info/tests/agent_info_persistance_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,18 @@ TEST_F(AgentInfoPersistanceTest, TestConstruction)

TEST_F(AgentInfoPersistanceTest, TestDefaultValues)
{
EXPECT_EQ(persistance->GetName(), "");
EXPECT_EQ(persistance->GetKey(), "");
EXPECT_EQ(persistance->GetUUID(), "");
}

TEST_F(AgentInfoPersistanceTest, TestSetName)
{
const std::string newName = "new_name";
persistance->SetName(newName);
EXPECT_EQ(persistance->GetName(), newName);
}

TEST_F(AgentInfoPersistanceTest, TestSetKey)
{
const std::string newKey = "new_key";
Expand Down Expand Up @@ -60,6 +68,18 @@ TEST_F(AgentInfoPersistanceTest, TestSetGroupsDelete)
EXPECT_EQ(persistance->GetGroups(), newGroups);
}

TEST_F(AgentInfoPersistanceTest, TestResetToDefault)
{
const std::string newName = "new_name";
persistance->SetName(newName);
EXPECT_EQ(persistance->GetName(), newName);

persistance->ResetToDefault();
EXPECT_EQ(persistance->GetName(), "");
EXPECT_EQ(persistance->GetKey(), "");
EXPECT_EQ(persistance->GetUUID(), "");
}

int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
Expand Down
30 changes: 25 additions & 5 deletions src/agent/agent_info/tests/agent_info_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,35 @@ TEST_F(AgentInfoTest, TestDefaultConstructor)
TEST_F(AgentInfoTest, TestDefaultConstructorDefaultValues)
{
const AgentInfo agentInfo;
EXPECT_EQ(agentInfo.GetName(), "");
EXPECT_EQ(agentInfo.GetKey(), "");
EXPECT_NE(agentInfo.GetUUID(), "");
}

TEST_F(AgentInfoTest, TestPersistedValues)
{
AgentInfo agentInfo;
agentInfo.SetName("test_name");
agentInfo.SetKey("4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj");
agentInfo.SetUUID("test_uuid");
const AgentInfo agentInfoReloaded;
EXPECT_EQ(agentInfoReloaded.GetName(), "test_name");
EXPECT_EQ(agentInfoReloaded.GetKey(), "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj");
EXPECT_EQ(agentInfoReloaded.GetUUID(), "test_uuid");
}

TEST_F(AgentInfoTest, TestSetName)
{
AgentInfo agentInfo;
const std::string newName = "new_name";

agentInfo.SetName(newName);
EXPECT_EQ(agentInfo.GetName(), newName);

const AgentInfo agentInfoReloaded;
EXPECT_EQ(agentInfoReloaded.GetName(), newName);
}

TEST_F(AgentInfoTest, TestSetKey)
{
AgentInfo agentInfo;
Expand Down Expand Up @@ -101,14 +116,15 @@ TEST_F(AgentInfoTest, TestLoadMetadataInfoNoSysInfo)
{
const AgentInfo agentInfo;

auto metadataInfo = agentInfo.GetMetadataInfo(true);
auto metadataInfo = nlohmann::json::parse(agentInfo.GetMetadataInfo(true));

EXPECT_TRUE(metadataInfo["agent"] != nullptr);

// Agent information
EXPECT_EQ(metadataInfo["agent"]["type"], agentInfo.GetType());
EXPECT_EQ(metadataInfo["agent"]["version"], agentInfo.GetVersion());
EXPECT_EQ(metadataInfo["agent"]["id"], agentInfo.GetUUID());
EXPECT_EQ(metadataInfo["agent"]["name"], agentInfo.GetName());
EXPECT_EQ(metadataInfo["agent"]["key"], agentInfo.GetKey());
EXPECT_TRUE(metadataInfo["agent"]["groups"] != nullptr);

Expand Down Expand Up @@ -140,14 +156,15 @@ TEST_F(AgentInfoTest, TestLoadMetadataInfoRegistration)

const AgentInfo agentInfo([os]() { return os; }, [networks]() { return networks; });

auto metadataInfo = agentInfo.GetMetadataInfo(true);
auto metadataInfo = nlohmann::json::parse(agentInfo.GetMetadataInfo(true));

EXPECT_TRUE(metadataInfo["agent"] != nullptr);

// Agent information
EXPECT_EQ(metadataInfo["agent"]["type"], agentInfo.GetType());
EXPECT_EQ(metadataInfo["agent"]["version"], agentInfo.GetVersion());
EXPECT_EQ(metadataInfo["agent"]["id"], agentInfo.GetUUID());
EXPECT_EQ(metadataInfo["agent"]["name"], agentInfo.GetName());
EXPECT_EQ(metadataInfo["agent"]["key"], agentInfo.GetKey());
EXPECT_TRUE(metadataInfo["agent"]["groups"] != nullptr);

Expand All @@ -156,7 +173,8 @@ TEST_F(AgentInfoTest, TestLoadMetadataInfoRegistration)
EXPECT_TRUE(metadataInfo["agent"]["host"]["os"] != nullptr);
EXPECT_EQ(metadataInfo["agent"]["host"]["os"]["name"], "test_os");
EXPECT_EQ(metadataInfo["agent"]["host"]["os"]["platform"], "test_platform");
EXPECT_EQ(metadataInfo["agent"]["host"]["ip"], "127.0.0.1");
EXPECT_TRUE(metadataInfo["agent"]["host"]["ip"] != nullptr);
EXPECT_EQ(metadataInfo["agent"]["host"]["ip"][0], "127.0.0.1");
EXPECT_EQ(metadataInfo["agent"]["host"]["architecture"], "test_arch");
EXPECT_EQ(metadataInfo["agent"]["host"]["hostname"], "test_name");
}
Expand Down Expand Up @@ -185,14 +203,15 @@ TEST_F(AgentInfoTest, TestLoadMetadataInfoConnected)

const AgentInfo agentInfo([os]() { return os; }, [networks]() { return networks; });

auto metadataInfo = agentInfo.GetMetadataInfo(false);
auto metadataInfo = nlohmann::json::parse(agentInfo.GetMetadataInfo(false));

EXPECT_TRUE(metadataInfo["agent"] != nullptr);

// Agent information
EXPECT_EQ(metadataInfo["agent"]["type"], agentInfo.GetType());
EXPECT_EQ(metadataInfo["agent"]["version"], agentInfo.GetVersion());
EXPECT_EQ(metadataInfo["agent"]["id"], agentInfo.GetUUID());
EXPECT_EQ(metadataInfo["agent"]["name"], agentInfo.GetName());
EXPECT_TRUE(metadataInfo["agent"]["key"] == nullptr);
EXPECT_TRUE(metadataInfo["agent"]["groups"] != nullptr);

Expand All @@ -201,7 +220,8 @@ TEST_F(AgentInfoTest, TestLoadMetadataInfoConnected)
EXPECT_TRUE(metadataInfo["agent"]["host"]["os"] != nullptr);
EXPECT_EQ(metadataInfo["agent"]["host"]["os"]["name"], "test_os");
EXPECT_EQ(metadataInfo["agent"]["host"]["os"]["platform"], "test_platform");
EXPECT_EQ(metadataInfo["agent"]["host"]["ip"], "127.0.0.1");
EXPECT_TRUE(metadataInfo["agent"]["host"]["ip"] != nullptr);
EXPECT_EQ(metadataInfo["agent"]["host"]["ip"][0], "127.0.0.1");
EXPECT_EQ(metadataInfo["agent"]["host"]["architecture"], "test_arch");
EXPECT_EQ(metadataInfo["agent"]["host"]["hostname"], "test_name");
}
Expand Down
2 changes: 2 additions & 0 deletions src/agent/include/agent_registration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ namespace agent_registration
/// @param user The user's username.
/// @param password The user's password.
/// @param key The agent's key.
/// @param name The agent's name.
/// @param configFile The path to the configuration file.
AgentRegistration(std::string user,
std::string password,
const std::string& key,
const std::string& name,
std::optional<std::string> configFile);

/// @brief Registers the agent with the manager.
Expand Down
10 changes: 5 additions & 5 deletions src/agent/multitype_queue/include/imultitype_queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ class IMultiTypeQueue
* @param messageQuantity The quantity of messages to return.
* @param moduleName The name of the module requesting the message.
* @param moduleType The type of the module requesting the messages.
* @return boost::asio::awaitable<Message> Awaitable object representing the next message.
* @return boost::asio::awaitable<std::vector<Message>> Awaitable object representing the next N messages.
*/
virtual boost::asio::awaitable<Message> getNextNAwaitable(MessageType type,
int messageQuantity,
const std::string moduleName = "",
const std::string moduleType = "") = 0;
virtual boost::asio::awaitable<std::vector<Message>> getNextNAwaitable(MessageType type,
int messageQuantity,
const std::string moduleName = "",
const std::string moduleType = "") = 0;

/**
* @brief Retrieves the next N messages from the queue.
Expand Down
Loading
Loading