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 all 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
27 changes: 16 additions & 11 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 @@ -84,16 +92,16 @@ class AgentInfo
/// @brief Loads the endpoint information into `m_endpointInfo`.
void LoadEndpointInfo();

/// @brief Loads the metadata information into `m_metadataInfo`.
void LoadMetadataInfo();

/// @brief Loads the header information into `m_headerInfo`.
void LoadHeaderInfo();

/// @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 All @@ -107,9 +115,6 @@ class AgentInfo
/// @brief The agent's endpoint information.
nlohmann::json m_endpointInfo;

/// @brief The agent's metadata information.
nlohmann::json m_metadataInfo;

/// @brief The agent's header information.
std::string m_headerInfo;

Expand Down
78 changes: 38 additions & 40 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 @@ -40,10 +41,14 @@ AgentInfo::AgentInfo(std::function<nlohmann::json()> getOSInfo, std::function<nl
}

LoadEndpointInfo();
LoadMetadataInfo();
LoadHeaderInfo();
}

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

std::string AgentInfo::GetKey() const
{
return m_key;
Expand All @@ -59,6 +64,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,22 +144,33 @@ 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;
nlohmann::json agentMetadataInfo;
agentMetadataInfo["agent"] = nlohmann::json::object();
agentMetadataInfo["agent"]["id"] = GetUUID();
agentMetadataInfo["agent"]["name"] = GetName();
agentMetadataInfo["agent"]["type"] = GetType();
agentMetadataInfo["agent"]["version"] = GetVersion();
agentMetadataInfo["agent"]["groups"] = GetGroups();

metadataInfo["agent"] = m_metadataInfo;
if (!m_endpointInfo.empty())
{
agentMetadataInfo["agent"]["host"] = m_endpointInfo;
}

if (agentIsRegistering)
{
metadataInfo["agent"]["key"] = GetKey();
agentMetadataInfo["agent"]["key"] = GetKey();
}

return metadataInfo;
return agentMetadataInfo.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 +179,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 @@ -170,51 +193,26 @@ void AgentInfo::LoadEndpointInfo()
{
nlohmann::json osInfo = m_getOSInfo();
m_endpointInfo["hostname"] = osInfo.value("hostname", "Unknown");
m_endpointInfo["os"] = osInfo.value("os_name", "Unknown");
m_endpointInfo["platform"] = osInfo.value("sysname", "Unknown");
m_endpointInfo["architecture"] = osInfo.value("architecture", "Unknown");
m_endpointInfo["os"] = nlohmann::json::object();
m_endpointInfo["os"]["name"] = osInfo.value("os_name", "Unknown");
m_endpointInfo["os"]["platform"] = osInfo.value("sysname", "Unknown");
}

if (m_getNetworksInfo != nullptr)
{
nlohmann::json networksInfo = m_getNetworksInfo();
auto ipAddress = GetActiveIPAddress(networksInfo);
m_endpointInfo["ip"] = ipAddress.value_or("Unknown");
}
}

void AgentInfo::LoadMetadataInfo()
{
m_metadataInfo["id"] = GetUUID();
m_metadataInfo["type"] = GetType();
m_metadataInfo["version"] = GetVersion();
m_metadataInfo["groups"] = GetGroups();

if (!m_endpointInfo.empty())
{
nlohmann::json host;
nlohmann::json os;

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");
os["platform"] = m_endpointInfo.value("platform", "Unknown");

host["os"] = os;

m_metadataInfo["host"] = host;
m_endpointInfo["ip"] = GetActiveIPAddresses(networksInfo);
}
}

void AgentInfo::LoadHeaderInfo()
{
if (!m_endpointInfo.empty())
if (!m_endpointInfo.empty() && m_endpointInfo.contains("os"))
{
m_headerInfo = PRODUCT_NAME + "/" + GetVersion() + " (" + GetType() + "; " +
m_endpointInfo.value("architecture", "Unknown") + "; " +
m_endpointInfo.value("platform", "Unknown") + ")";
m_endpointInfo["os"].value("platform", "Unknown") + ")";
}
else
{
Expand Down
22 changes: 18 additions & 4 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 Expand Up @@ -211,8 +223,10 @@ void AgentInfoPersistance::ResetToDefault()
{
try
{
m_db->Remove(AGENT_INFO_TABLE_NAME);
m_db->Remove(AGENT_GROUP_TABLE_NAME);
m_db->DropTable(AGENT_INFO_TABLE_NAME);
m_db->DropTable(AGENT_GROUP_TABLE_NAME);
CreateAgentInfoTable();
CreateAgentGroupTable();
InsertDefaultAgentInfo();
}
catch (const std::exception& e)
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
Loading
Loading