-
Notifications
You must be signed in to change notification settings - Fork 19
/
EventAggregator.h
399 lines (329 loc) · 13.6 KB
/
EventAggregator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
microsoft-oms-auditd-plugin
Copyright (c) Microsoft Corporation
All rights reserved.
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef AUOMS_EVENTAGGREGATOR_H
#define AUOMS_EVENTAGGREGATOR_H
#include <atomic>
#include <chrono>
#include <functional>
#include <queue>
#include <unordered_map>
#include <vector>
#include <string_view>
#include <map>
#include "EventMatcher.h"
#include "EventId.h"
#include <rapidjson/document.h>
/*
[
{
"match_rule": {
"record_types": [],
"field_rules": [
{
"field_name": "syscall",
"op": "re",
"value": "execve"
},
{
"field_name": "cmdline",
"op": "re",
"value": "/stuff/[0-9]+/foo"
},
]
},
"aggregation_fields": {
"time": {
"mode": "raw" // interp, dynamic
"output_name": "aggregated_time"
}
"pid": {},
},
"time_field_mode": "drop", (full, delta, drop)
"serial_field_mode": "drop",
"max_size": 2048,
"max_time": 300,
"send_first_as_raw": true
},
{
"match_rule": {
"record_types": [],
"field_rules": [
{
"field_name": "syscall",
"op": "re",
"values": ["execve","open"]
},
]
},
},
]
*/
enum class AggregationFieldMode: int {
NORMAL = -1,
DROP = 0,
RAW = 1,
INTERP = 2,
DYNAMIC = 3,
DELTA = 4,
};
class AggregationField {
public:
AggregationField(const std::string& name): _name(name), _mode(AggregationFieldMode::DYNAMIC), _output_name(name) {}
AggregationField(const std::string& name, AggregationFieldMode mode): _name(name), _mode(mode), _output_name(name) {}
AggregationField(const std::string& name, AggregationFieldMode mode, const std::string& output_name): _name(name), _mode(mode), _output_name(output_name) {}
inline const std::string& Name() const {
return _name;
}
inline AggregationFieldMode Mode() const {
return _mode;
}
inline const std::string& OutputName() const {
return _output_name;
}
private:
std::string _name;
AggregationFieldMode _mode;
std::string _output_name;
};
class AggregationRule {
public:
static constexpr uint32_t DEFAULT_MAX_PENDING = 1024;
static constexpr uint32_t MIN_MAX_PENDING = 1;
static constexpr uint32_t MAX_MAX_PENDING = 10240;
static constexpr uint32_t DEFAULT_MAX_SIZE = 8192;
static constexpr uint32_t MIN_MAX_SIZE = 128;
static constexpr uint32_t MAX_MAX_SIZE = 128*1024;
static constexpr uint32_t DEFAULT_MAX_COUNT = 1024;
static constexpr uint32_t MIN_MAX_COUNT = 2;
static constexpr uint32_t MAX_MAX_COUNT = 128*1024;
static constexpr uint32_t DEFAULT_MAX_TIME = 900; // 15 minutes
static constexpr uint32_t MIN_MAX_TIME = 1; // 1 second
static constexpr uint32_t MAX_MAX_TIME = 3600; // 1 hour
static constexpr bool DEFAULT_SEND_FIRST = false;
AggregationRule(const std::shared_ptr<EventMatchRule>& match_rule, const std::vector<AggregationField>& aggregation_fields,
AggregationFieldMode time_field_mode, AggregationFieldMode serial_field_mode,
uint32_t max_pending, uint32_t max_count, uint32_t max_size, uint32_t max_time, bool send_first)
: _match_rule(match_rule), _aggregation_fields(aggregation_fields), _aggregation_fields_map(),
_time_field_mode(time_field_mode), _serial_field_mode(serial_field_mode),
_max_pending(max_pending), _max_count(max_count), _max_size(max_size), _max_time(max_time), _send_first(send_first)
{
if (_max_pending < MIN_MAX_PENDING) {
_max_pending = MIN_MAX_PENDING;
} else if (_max_pending > MAX_MAX_PENDING) {
_max_pending = MAX_MAX_PENDING;
}
if (_max_count < MIN_MAX_COUNT) {
_max_count = MIN_MAX_COUNT;
} else if (_max_count > MAX_MAX_COUNT) {
_max_count = MAX_MAX_COUNT;
}
if (_max_size < MIN_MAX_SIZE) {
_max_size - MIN_MAX_SIZE;
} else if (_max_size > MAX_MAX_SIZE) {
_max_size - MAX_MAX_SIZE;
}
if (_max_time < MIN_MAX_TIME) {
_max_time = MIN_MAX_TIME;
} else if (_max_time > MAX_MAX_TIME) {
_max_time = MAX_MAX_TIME;
}
_num_drop_fields = 0;
for (int i = 0; i < _aggregation_fields.size(); ++i) {
if (_aggregation_fields[i].Mode() == AggregationFieldMode::DROP) {
_num_drop_fields += 1;
}
_aggregation_fields_map.emplace(std::make_pair(std::string_view(_aggregation_fields[i].Name()), i));
}
}
static void RulesFromJSON(const rapidjson::Value& value, std::vector<std::shared_ptr<AggregationRule>>& rules);
static std::shared_ptr<AggregationRule> FromJSON(const rapidjson::Value& value);
static std::shared_ptr<AggregationRule> FromJSON(const std::string& str);
void ToJSON(rapidjson::Writer<rapidjson::StringBuffer>& writer) const;
std::string ToJSONString() const;
inline std::shared_ptr<EventMatchRule> MatchRule() const {
return _match_rule;
}
inline const std::vector<AggregationField>& AggregationFields() const {
return _aggregation_fields;
}
inline int NumDropFields() const {
return _num_drop_fields;
}
inline AggregationFieldMode FieldMode(const std::string_view& name) const {
auto it = _aggregation_fields_map.find(name);
if (it != _aggregation_fields_map.end()) {
return _aggregation_fields[it->second].Mode();
} else {
return AggregationFieldMode::NORMAL;
}
}
inline bool HasAggregationField(const std::string_view& name) const {
return _aggregation_fields_map.count(name) > 0;
}
inline AggregationFieldMode TimeFieldMode() const {
return _time_field_mode;
}
inline AggregationFieldMode SerialFieldMode() const {
return _serial_field_mode;
}
inline uint32_t MaxPending() const {
return _max_pending;
}
inline uint32_t MaxCount() const {
return _max_count;
}
inline uint32_t MaxSize() const {
return _max_size;
}
inline uint32_t MaxTime() const {
return _max_time;
}
inline bool SendFirst() const {
return _send_first;
}
// The aggregation key is the set of non-aggregated fields
// The string_views placed in key point to data in event and thus have the
// same validity lifespan
void CalcAggregationKey(std::vector<std::string_view>& key, const Event& event) const;
private:
std::shared_ptr<EventMatchRule> _match_rule;
std::vector<AggregationField> _aggregation_fields;
std::unordered_map<std::string_view, int> _aggregation_fields_map;
int _num_drop_fields;
AggregationFieldMode _time_field_mode;
AggregationFieldMode _serial_field_mode;
uint32_t _max_pending;
uint32_t _max_count;
uint32_t _max_size; //bytes
uint32_t _max_time; //seconds
bool _send_first;
};
class EventAggregator;
class AggregatedEvent {
public:
AggregatedEvent(const std::shared_ptr<AggregationRule>& rule): _rule(rule), _last_event(0, 0, 0) {
_id = _next_id.fetch_add(1);
_count = 0;
_expiration_time = std::chrono::steady_clock::now() + std::chrono::seconds(_rule->MaxTime());
_data.reserve(AggregationRule::MIN_MAX_SIZE);
_aggregated_fields.resize(_rule->AggregationFields().size());
_event_times.reserve(AggregationRule::MIN_MAX_COUNT);
_event_serials.reserve(AggregationRule::MIN_MAX_COUNT);
for (auto& x : _aggregated_fields) {
x.reserve(AggregationRule::MIN_MAX_COUNT);
}
}
static std::shared_ptr<AggregatedEvent> Read(FILE* file, std::vector<std::shared_ptr<AggregationRule>> rules);
void Write(FILE* file, const std::unordered_map<std::shared_ptr<AggregationRule>, int>& rules_map) const;
inline const std::shared_ptr<AggregationRule>& Rule() const {
return _rule;
}
inline bool Empty() const {
return _count == 0;
}
inline uint64_t Id() const {
return _id;
}
inline std::chrono::steady_clock::time_point ExpirationTime() const {
return _expiration_time;
}
inline std::pair<std::chrono::steady_clock::time_point, uint64_t> AgeKey() const {
return std::make_pair(_expiration_time, _id);
}
inline const std::vector<std::string_view>& AggregationKey() const {
return _agg_key;
}
// Return true of the event was added
// Return false if the event was not added (and thus the AggregatedEvent is full)
bool AddEvent(const Event& event);
int BuildEvent(EventBuilder& builder, rapidjson::StringBuffer& buffer) const;
private:
friend class EventAggregator;
AggregatedEvent() {}
static std::atomic<uint64_t> _next_id;
std::shared_ptr<AggregationRule> _rule;
std::chrono::steady_clock::time_point _expiration_time;
uint64_t _id;
EventId _first_event;
EventId _last_event;
uint32_t _count;
std::vector<uint8_t> _origin_event;
std::vector<std::string_view> _agg_key;
std::string _data;
std::vector<std::pair<size_t, size_t>> _event_times;
std::vector<std::pair<size_t, size_t>> _event_serials;
std::vector<std::vector<std::pair<size_t, size_t>>> _aggregated_fields;
};
template<>
struct std::hash<std::vector<std::string_view>>
{
std::size_t operator()(std::vector<std::string_view> const& v) const noexcept
{
size_t h = 0;
for (auto& i : v) {
// This is algorithm is taken from boost:hash_combine();
h ^= std::hash<std::string_view>{}(i) + 0x9e3779b9 + (h << 6) + (h >> 2);
}
return h;
}
};
class EventAggregator {
public:
EventAggregator():
_allocator(std::make_shared<BasicEventBuilderAllocator>(256*1024)),
_builder(_allocator, DefaultPrioritizer::Create(0)),
_matcher(std::make_shared<EventMatcher>())
{}
// Set rules
// If existing rules exist, any events associated with old rules that are not in the new set, will be flushed to the _ready_events queue.
void SetRules(const std::vector<std::shared_ptr<AggregationRule>>& rules);
// Load saved aggregation state from file
// Any previous state is lost
void Load(const std::string& path);
// Save aggregation state to file
void Save(const std::string& path);
// Check if event is aggregated
// Return true if the event was consumed (aggregated)
bool AddEvent(const Event& event);
// Check for complete/ready aggregated events and handle one
// If handler_fn returns ret.second == true, then event was consumed
// ret.get<0>, true if handler_fn invoked, false if no events ready
// ret.get<1>, if get<0> is true, then ret.first from handler_fn, otherwise ret from AggregatedEvent::BuildEvent()
// ret.get<2>, if get<0> is true, then ret.second from handler_fn, otherwise false
std::tuple<bool, int64_t, bool> HandleEvent(const std::function<std::pair<int64_t, bool> (const Event& event)>& handler_fn);
size_t NumReadyAggregates() const {
return _ready_events.size();
}
size_t NumPendingAggregates() const {
size_t count = 0;
for (auto& e : _events) {
count += e->_events.size();
}
return count;
}
private:
class PerRuleAgg {
public:
explicit PerRuleAgg(const std::shared_ptr<AggregationRule>& rule): _rule(rule), _events(16) {}
std::shared_ptr<AggregationRule> _rule;
std::unordered_map<std::vector<std::string_view>,std::shared_ptr<AggregatedEvent>> _events;
std::map<std::pair<std::chrono::steady_clock::time_point, uint64_t>, std::vector<std::string_view>> _events_age;
};
std::vector<std::shared_ptr<AggregationRule>> _rules;
std::shared_ptr<EventMatcher> _matcher;
std::vector<std::shared_ptr<PerRuleAgg>> _events;
std::map<std::pair<std::chrono::steady_clock::time_point, uint64_t>, std::pair<std::shared_ptr<AggregatedEvent>, int>> _aged_events;
std::queue<std::shared_ptr<AggregatedEvent>> _ready_events;
std::vector<std::string_view> _tmp_key;
rapidjson::StringBuffer _js_buffer;
std::shared_ptr<BasicEventBuilderAllocator> _allocator;
EventBuilder _builder;
};
#endif //AUOMS_EVENTAGGREGATOR_H