Skip to content
API Documentation
BastionGuard™
bastionguard::rootguard::JsonEventSink Class Referencefinal

#include <JsonEventSink.hpp>

Inheritance diagram for bastionguard::rootguard::JsonEventSink:
bastionguard::rootguard::IEventSink

Public Member Functions

 JsonEventSink (const std::filesystem::path &path)
void onEvent (const rg_event &event) noexcept override
Public Member Functions inherited from bastionguard::rootguard::IEventSink
virtual ~IEventSink ()=default

Static Private Member Functions

static std::string escape (const char *value)

Private Attributes

std::ofstream stream_
std::mutex mutex_

Detailed Description

Definition at line 11 of file JsonEventSink.hpp.

Constructor & Destructor Documentation

◆ JsonEventSink()

bastionguard::rootguard::JsonEventSink::JsonEventSink ( const std::filesystem::path & path)
explicit

Definition at line 73 of file JsonEventSink.cpp.

74 : stream_(path, std::ios::app)
75{
76 if (!stream_)
77 throw std::runtime_error("impossibile aprire il log JSON: " + path.string());
78}

References stream_.

Member Function Documentation

◆ escape()

std::string bastionguard::rootguard::JsonEventSink::escape ( const char * value)
staticprivate

Definition at line 80 of file JsonEventSink.cpp.

81{
82 std::ostringstream output;
83 for (const unsigned char character : std::string(value ? value : "")) {
84 switch (character) {
85 case '"': output << "\\\""; break;
86 case '\\': output << "\\\\"; break;
87 case '\b': output << "\\b"; break;
88 case '\f': output << "\\f"; break;
89 case '\n': output << "\\n"; break;
90 case '\r': output << "\\r"; break;
91 case '\t': output << "\\t"; break;
92 default:
93 if (character < 0x20)
94 output << "\\u" << std::hex << std::setw(4) << std::setfill('0')
95 << static_cast<unsigned int>(character) << std::dec;
96 else
97 output << static_cast<char>(character);
98 }
99 }
100 return output.str();
101}

Referenced by onEvent().

◆ onEvent()

void bastionguard::rootguard::JsonEventSink::onEvent ( const rg_event & event)
overridevirtualnoexcept

Implements bastionguard::rootguard::IEventSink.

Definition at line 103 of file JsonEventSink.cpp.

104{
105 try {
106 std::lock_guard lock(mutex_);
107 stream_ << '{'
108 << "\"received_at\":\"" << receivedAtUtc() << "\","
109 << "\"kernel_monotonic_ns\":" << event.timestamp_ns << ','
110 << "\"event\":\"" << eventName(event.event_type) << "\","
111 << "\"verdict\":\"" << verdictName(event.verdict) << "\","
112 << "\"pid\":" << event.pid << ','
113 << "\"tgid\":" << event.tgid << ','
114 << "\"ppid\":" << event.ppid << ','
115 << "\"old_euid\":" << event.old_euid << ','
116 << "\"new_euid\":" << event.new_euid << ','
117 << "\"rule_id\":" << event.rule_id << ','
118 << "\"reason_flags\":" << event.reason_flags << ','
119 << "\"lsm_flags\":" << event.lsm_flags << ','
120 << "\"auxiliary\":" << event.auxiliary << ','
121 << "\"path_resolution\":\"" << pathResolutionName(event.path_resolution) << "\","
122 << "\"device\":" << event.file.device << ','
123 << "\"inode\":" << event.file.inode << ','
124 << "\"actor_device\":" << event.actor_file.device << ','
125 << "\"actor_inode\":" << event.actor_file.inode << ','
126 << "\"parent_device\":" << event.parent_file.device << ','
127 << "\"parent_inode\":" << event.parent_file.inode << ','
128 << "\"actor_start_boottime_ns\":" << event.actor_start_boottime_ns << ','
129 << "\"old_mode\":" << event.old_mode << ','
130 << "\"new_mode\":" << event.new_mode << ','
131 << "\"old_uid\":" << event.old_uid << ','
132 << "\"new_uid\":" << event.new_uid << ','
133 << "\"old_gid\":" << event.old_gid << ','
134 << "\"new_gid\":" << event.new_gid << ','
135 << "\"comm\":\"" << escape(event.comm) << "\","
136 << "\"parent_comm\":\"" << escape(event.parent_comm) << "\","
137 << "\"actor_executable\":\"" << escape(event.actor_executable) << "\","
138 << "\"parent_executable\":\"" << escape(event.parent_executable) << "\","
139 << "\"filename\":\"" << escape(event.filename) << "\","
140 << "\"xattr_name\":\"" << escape(event.xattr_name) << "\""
141 << "}\n";
142 stream_.flush();
143 } catch (...) {
144 // Logging must never crash the security engine.
145 }
146}
static std::string escape(const char *value)
const char * pathResolutionName(const __u32 resolution) noexcept
const char * verdictName(const __u32 verdict) noexcept
char filename[RG_FILENAME_LEN]
char xattr_name[RG_XATTR_NAME_LEN]
char parent_executable[RG_FILENAME_LEN]
char comm[RG_COMM_LEN]
char actor_executable[RG_FILENAME_LEN]
char parent_comm[RG_COMM_LEN]
__u32 path_resolution

References escape(), mutex_, and stream_.

Referenced by anonymous_namespace{ActionHelper.cpp}::logAction().

Member Data Documentation

◆ mutex_

std::mutex bastionguard::rootguard::JsonEventSink::mutex_
private

Definition at line 20 of file JsonEventSink.hpp.

Referenced by onEvent().

◆ stream_

std::ofstream bastionguard::rootguard::JsonEventSink::stream_
private

Definition at line 19 of file JsonEventSink.hpp.

Referenced by JsonEventSink(), and onEvent().


The documentation for this class was generated from the following files: