-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: convert GenerateStatelessEvent to a class
- Loading branch information
Showing
7 changed files
with
264 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
#include "statelessEvent.hpp" | ||
|
||
constexpr int BYTES_IN_KILOBYTE = 1024; | ||
constexpr int KILOBYTES_IN_MEGABYTE = 1024; | ||
constexpr int BYTES_IN_MEGABYTE = BYTES_IN_KILOBYTE * KILOBYTES_IN_MEGABYTE; | ||
|
||
StatelessEvent::StatelessEvent(std::string op, std::string time, nlohmann::json d) | ||
: operation(std::move(op)), created(std::move(time)), data(std::move(d)) {} | ||
|
||
nlohmann::json NetworkEvent::generate() const { | ||
std::string interface = data["observer"]["ingress"]["interface"]["name"]; | ||
std::string action, reason; | ||
|
||
if (operation == "create") { | ||
action = "network-interface-detected"; | ||
reason = "New network interface " + interface + " detected"; | ||
} else if (operation == "update") { | ||
action = "network-interface-updated"; | ||
reason = "Network interface " + interface + " updated"; | ||
} else { | ||
action = "network-interface-removed"; | ||
reason = "Network interface " + interface + " was removed"; | ||
} | ||
|
||
return {{"event", {{"action", action}, {"category", {"network"}}, {"type", {operation == "create" ? "info" : operation == "update" ? "change" : "deletion"}}, {"created", created}, {"reason", reason}}}}; | ||
} | ||
|
||
nlohmann::json PackageEvent::generate() const { | ||
std::string packageName = data["package"]["name"]; | ||
std::string version = data["package"]["version"]; | ||
std::string action, reason; | ||
|
||
if (operation == "create") { | ||
action = "package-installed"; | ||
reason = "Package " + packageName + " (version " + version + ") was installed"; | ||
} else if (operation == "update") { | ||
action = "package-updated"; | ||
reason = "Package " + packageName + " updated"; | ||
} else { | ||
action = "package-removed"; | ||
reason = "Package " + packageName + " (version " + version + ") was removed"; | ||
} | ||
|
||
return {{"event", {{"action", action}, {"category", {"package"}}, {"type", {operation == "create" ? "installation" : operation == "update" ? "change" : "deletion"}}, {"created", created}, {"reason", reason}}}}; | ||
} | ||
|
||
nlohmann::json HotfixEvent::generate() const { | ||
std::string hotfixID = data["package"]["hotfix"]["name"]; | ||
std::string action, reason; | ||
|
||
if (operation == "create") { | ||
action = "hotfix-installed"; | ||
reason = "Hotfix " + hotfixID + " was installed"; | ||
} else if (operation == "update") { | ||
action = "hotfix-updated"; | ||
reason = "Hotfix " + hotfixID + " was updated"; | ||
} else { | ||
action = "hotfix-removed"; | ||
reason = "Hotfix " + hotfixID + " was removed"; | ||
} | ||
|
||
return {{"event", {{"action", action}, {"category", {"hotfix"}}, {"type", {operation == "create" ? "installation" : "deletion"}}, {"created", created}, {"reason", reason}}}}; | ||
} | ||
|
||
nlohmann::json PortEvent::generate() const { | ||
int srcPort = data["source"]["port"]; | ||
int destPort = data["destination"]["port"]; | ||
std::string action, reason; | ||
|
||
if (operation == "create") { | ||
action = "port-detected"; | ||
reason = "New connection from source port " + std::to_string(srcPort) + " to destination port " + std::to_string(destPort); | ||
} else if (operation == "update") { | ||
action = "port-updated"; | ||
reason = "Updated connection from source port " + std::to_string(srcPort) + " to destination port " + std::to_string(destPort); | ||
} else { | ||
action = "port-closed"; | ||
reason = "Closed connection from source port " + std::to_string(srcPort) + " to destination port " + std::to_string(destPort); | ||
} | ||
|
||
return {{"event", {{"action", action}, {"category", {"network"}}, {"type", {operation == "create" ? "connection" : operation == "update" ? "change" : "end"}}, {"created", created}, {"reason", reason}}}}; | ||
} | ||
|
||
nlohmann::json ProcessEvent::generate() const { | ||
std::string processName = data["process"]["name"]; | ||
std::string pid = data["process"]["pid"]; | ||
std::string action, reason; | ||
|
||
if (operation == "create") { | ||
action = "process-started"; | ||
reason = "Process " + processName + " (PID: " + pid + ") was started"; | ||
} else if (operation == "update") { | ||
action = "process-updated"; | ||
reason = "Process " + processName + " (PID: " + pid + ") was updated"; | ||
} else { | ||
action = "process-stopped"; | ||
reason = "Process " + processName + " (PID: " + pid + ") was stopped"; | ||
} | ||
|
||
return {{"event", {{"action", action}, {"category", {"process"}}, {"type", {operation == "create" ? "start" : operation == "update" ? "change" : "end"}}, {"created", created}, {"reason", reason}}}}; | ||
} | ||
|
||
nlohmann::json SystemEvent::generate() const { | ||
std::string hostname = data["host"]["hostname"]; | ||
std::string osVersion = data["host"]["os"]["version"]; | ||
std::string action = (operation == "update") ? "system-updated" : "system-detected"; | ||
std::string reason = "System " + hostname + " is running OS version " + osVersion; | ||
|
||
return {{"event", {{"action", action}, {"category", {"host"}}, {"type", {operation == "update" ? "change" : "info"}}, {"created", created}, {"reason", reason}}}}; | ||
} | ||
|
||
nlohmann::json HardwareEvent::generate() const { | ||
std::string cpuName = data["host"]["cpu"]["name"]; | ||
int memoryTotalGB = data["host"]["memory"]["total"].get<int>() / BYTES_IN_MEGABYTE; | ||
std::string serialNumber = data["observer"]["serial_number"]; | ||
std::string action, reason; | ||
|
||
if (operation == "create") { | ||
action = "hardware-detected"; | ||
reason = "New hardware detected: " + cpuName + " with " + std::to_string(memoryTotalGB) + " GB memory"; | ||
} else if (operation == "update") { | ||
action = "hardware-updated"; | ||
reason = "Hardware changed"; | ||
} else if (operation == "remove") { | ||
action = "hardware-removed"; | ||
reason = "Hardware with serial number " + serialNumber + " was removed"; | ||
} | ||
|
||
return {{"event", {{"action", action}, {"category", {"host"}}, {"type", {operation == "create" ? "start" : operation == "update" ? "change" : "removed"}}, {"created", created}, {"reason", reason}}}}; | ||
} | ||
|
||
std::unique_ptr<StatelessEvent> CreateEvent(const std::string& type, const std::string& operation, const std::string& created, const nlohmann::json& data) { | ||
if (type == "networks") return std::make_unique<NetworkEvent>(operation, created, data); | ||
if (type == "packages") return std::make_unique<PackageEvent>(operation, created, data); | ||
if (type == "hotfixes") return std::make_unique<HotfixEvent>(operation, created, data); | ||
if (type == "ports") return std::make_unique<PortEvent>(operation, created, data); | ||
if (type == "processes") return std::make_unique<ProcessEvent>(operation, created, data); | ||
if (type == "system") return std::make_unique<SystemEvent>(operation, created, data); | ||
if (type == "hardware") return std::make_unique<HardwareEvent>(operation, created, data); | ||
|
||
return nullptr; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#pragma once | ||
|
||
#include <nlohmann/json.hpp> | ||
#include <memory> | ||
#include <string> | ||
|
||
class StatelessEvent { | ||
protected: | ||
std::string operation; | ||
std::string created; | ||
nlohmann::json data; | ||
|
||
public: | ||
StatelessEvent(std::string op, std::string time, nlohmann::json d); | ||
virtual nlohmann::json generate() const = 0; | ||
virtual ~StatelessEvent() = default; | ||
}; | ||
|
||
class NetworkEvent : public StatelessEvent { public: using StatelessEvent::StatelessEvent; nlohmann::json generate() const override; }; | ||
class PackageEvent : public StatelessEvent { public: using StatelessEvent::StatelessEvent; nlohmann::json generate() const override; }; | ||
class HotfixEvent : public StatelessEvent { public: using StatelessEvent::StatelessEvent; nlohmann::json generate() const override; }; | ||
class PortEvent : public StatelessEvent { public: using StatelessEvent::StatelessEvent; nlohmann::json generate() const override; }; | ||
class ProcessEvent : public StatelessEvent { public: using StatelessEvent::StatelessEvent; nlohmann::json generate() const override; }; | ||
class SystemEvent : public StatelessEvent { public: using StatelessEvent::StatelessEvent; nlohmann::json generate() const override; }; | ||
class HardwareEvent : public StatelessEvent { public: using StatelessEvent::StatelessEvent; nlohmann::json generate() const override; }; | ||
|
||
std::unique_ptr<StatelessEvent> CreateEvent(const std::string& type, const std::string& operation, const std::string& created, const nlohmann::json& data); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
find_package(GTest CONFIG REQUIRED) | ||
|
||
add_executable(statelessEvent_unit_test statelessEvent_test.cpp) | ||
configure_target(statelessEvent_unit_test) | ||
target_include_directories(statelessEvent_unit_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../include ${CMAKE_CURRENT_SOURCE_DIR}/../src/inventory/events) | ||
target_link_libraries(statelessEvent_unit_test PRIVATE | ||
Inventory | ||
GTest::gtest | ||
GTest::gtest_main | ||
pthread) | ||
add_test(NAME StatelessEventUnitTest COMMAND statelessEvent_unit_test) |
Oops, something went wrong.