Skip to content

Commit

Permalink
fix: include the agent uuid in the inventory tables id calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
cborla committed Dec 11, 2024
1 parent 0aca1c3 commit 279f26d
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 78 deletions.
3 changes: 2 additions & 1 deletion src/agent/src/agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ Agent::Agent(const std::string& configFilePath, std::unique_ptr<ISignalHandler>
{ return m_configurationParser->GetConfig<T>(std::move(table), std::move(key)); })
, m_moduleManager([this](Message message) -> int { return m_messageQueue->push(std::move(message)); },
m_configurationParser,
[this](std::function<void()> task) { m_taskManager.EnqueueTask(std::move(task)); })
[this](std::function<void()> task) { m_taskManager.EnqueueTask(std::move(task)); },
m_agentInfo.GetUUID())
, m_commandHandler(m_dataPath)
{
// Check if agent is registered
Expand Down
5 changes: 4 additions & 1 deletion src/modules/include/moduleManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ class ModuleManager {
public:
ModuleManager(const std::function<int(Message)>& pushMessage,
std::shared_ptr<configuration::ConfigurationParser> configurationParser,
const std::function<void(std::function<void()>)>& createTask)
const std::function<void(std::function<void()>)>& createTask,
std::string uuid)
: m_pushMessage(pushMessage)
, m_configurationParser(std::move(configurationParser))
, m_createTask(createTask)
, m_agentUUID(std::move(uuid))
{}
~ModuleManager() = default;

Expand Down Expand Up @@ -54,4 +56,5 @@ class ModuleManager {
std::function<int(Message)> m_pushMessage;
std::shared_ptr<configuration::ConfigurationParser> m_configurationParser;
std::function<void(std::function<void()>)> m_createTask;
std::string m_agentUUID;
};
7 changes: 6 additions & 1 deletion src/modules/inventory/include/inventory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class Inventory {
static Inventory s_instance;
return s_instance;
}

void Start();
void Setup(std::shared_ptr<const configuration::ConfigurationParser> configurationParser);
void Stop();
Expand All @@ -42,6 +41,11 @@ class Inventory {
const std::string& normalizerType);
virtual void SendDeltaEvent(const std::string& data);

const std::string& AgentUUID() const { return m_agentUUID; };
void SetAgentUUID(const std::string& agentUUID) {
m_agentUUID = agentUUID;
}

private:
Inventory();
~Inventory() = default;
Expand Down Expand Up @@ -83,6 +87,7 @@ class Inventory {
std::string CalculateBase64Id(const nlohmann::json& data, const std::string& table);

const std::string m_moduleName {"inventory"};
std::string m_agentUUID {""}; // Agent UUID
std::shared_ptr<ISysInfo> m_spInfo;
std::function<void(const std::string&)> m_reportDiffFunction;
bool m_enabled; // Main switch
Expand Down
34 changes: 27 additions & 7 deletions src/modules/inventory/src/inventoryImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ constexpr std::time_t INVENTORY_DEFAULT_INTERVAL { 3600000 };
constexpr auto UNKNOWN_VALUE {" "};
constexpr auto EMPTY_VALUE {""};
constexpr auto UNKNOWN_DATE = nullptr;
constexpr size_t MAX_ID_SIZE = 512;

constexpr auto QUEUE_SIZE
{
Expand Down Expand Up @@ -299,7 +300,7 @@ std::string Inventory::GetPrimaryKeys([[maybe_unused]] const nlohmann::json& dat
std::string Inventory::CalculateBase64Id(const nlohmann::json& data, const std::string& table)
{
std::string primaryKey = GetPrimaryKeys(data, table);
std::string baseId = Name() + ":" + table + ":" + primaryKey;
std::string baseId = AgentUUID() + ":" + primaryKey;
std::string idBase64;
idBase64.resize(boost::beast::detail::base64::encoded_size(baseId.size()));
boost::beast::detail::base64::encode(&idBase64[0], baseId.c_str(), baseId.size());
Expand All @@ -324,9 +325,18 @@ void Inventory::NotifyChange(ReturnTypeCallback result, const nlohmann::json& da
msg["operation"] = OPERATION_MAP.at(result);
msg["data"] = EcsData(item, table);
msg["id"] = CalculateBase64Id(msg["data"], table);
msg["data"]["@timestamp"] = m_scanTime;
const auto msgToSend{msg.dump()};
m_reportDiffFunction(msgToSend);

if (msg["id"].is_string() && msg["id"].get<std::string>().size() <= MAX_ID_SIZE)
{
msg["data"]["@timestamp"] = m_scanTime;
const auto msgToSend{msg.dump()};
m_reportDiffFunction(msgToSend);
}
else
{
LogWarn("Event discarded for exceeding maximum size allowed in id field.");
LogTrace("Event discarded: {}", msg.dump());
}
}
}
else
Expand All @@ -337,9 +347,19 @@ void Inventory::NotifyChange(ReturnTypeCallback result, const nlohmann::json& da
msg["operation"] = OPERATION_MAP.at(result);
msg["data"] = EcsData(data, table);
msg["id"] = CalculateBase64Id(msg["data"], table);
msg["data"]["@timestamp"] = m_scanTime;
const auto msgToSend{msg.dump()};
m_reportDiffFunction(msgToSend);

if (msg["id"].is_string() && msg["id"].get<std::string>().size() <= MAX_ID_SIZE)
{
msg["data"]["@timestamp"] = m_scanTime;
const auto msgToSend{msg.dump()};
m_reportDiffFunction(msgToSend);
}
else
{
LogWarn("Event discarded for exceeding maximum size allowed in id field.");
LogTrace("Event discarded: {}", msg.dump());
}

// LCOV_EXCL_STOP
}
}
Expand Down
Loading

0 comments on commit 279f26d

Please sign in to comment.