forked from microsoft/OMS-Auditd-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventProcessorTests.cpp
356 lines (293 loc) · 15.2 KB
/
EventProcessorTests.cpp
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
/*
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.
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE "EventProcessorTests"
#include <boost/test/unit_test.hpp>
#include "PriorityQueue.h"
#include "Logger.h"
#include "TempDir.h"
#include "TestEventData.h"
#include "RawEventProcessor.h"
#include "RawEventAccumulator.h"
#include "StringUtils.h"
#include "EventPrioritizer.h"
#include "InputBuffer.h"
#include <fstream>
#include <stdexcept>
#include <iostream>
extern "C" {
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
};
void write_file(const std::string& path, const std::string& text) {
std::ofstream out;
out.exceptions(std::ofstream::failbit|std::ofstream::badbit|std::ofstream::eofbit);
out.open(path);
out << text;
out.close();
}
class RawEventQueue: public IEventBuilderAllocator {
public:
explicit RawEventQueue(std::shared_ptr<RawEventProcessor> proc): _buffer(), _size(0), _proc(std::move(proc)) {}
bool Allocate(void** data, size_t size) override {
if (_size != size) {
_size = size;
}
if (_buffer.size() < _size) {
_buffer.resize(_size);
}
*data = _buffer.data();
return true;
}
int Commit() override {
if (_size > InputBuffer::MAX_DATA_SIZE) {
return -1;
}
_proc->ProcessData(_buffer.data(), _size);
_size = 0;
return 1;
}
bool Rollback() override {
_size = 0;
return true;
}
private:
std::vector<uint8_t> _buffer;
size_t _size;
std::shared_ptr<RawEventProcessor> _proc;
};
void diff_event(int idx, const Event& e, const Event& a) {
std::stringstream msg;
if (e.Seconds() != a.Seconds()) {
msg << "Event["<<idx<<"] Seconds Mismatch: expected " << e.Seconds() << ", got " << a.Seconds();
throw std::runtime_error(msg.str());
}
if (e.Milliseconds() != a.Milliseconds()) {
msg << "Event["<<idx<<"] Milliseconds Mismatch: expected " << e.Milliseconds() << ", got " << a.Milliseconds();
throw std::runtime_error(msg.str());
}
if (e.Serial() != a.Serial()) {
msg << "Event["<<idx<<"] Serial Mismatch: expected " << e.Serial() << ", got " << a.Serial();
throw std::runtime_error(msg.str());
}
if (e.Flags() != a.Flags()) {
msg << "Event["<<idx<<"] Flags Mismatch: expected " << e.Flags() << ", got " << a.Flags();
throw std::runtime_error(msg.str());
}
if (e.Pid() != a.Pid()) {
msg << "Event["<<idx<<"] Pid Mismatch: expected " << e.Pid() << ", got " << a.Pid();
throw std::runtime_error(msg.str());
}
if (e.NumRecords() != a.NumRecords()) {
msg << "Event["<<idx<<"] NumRecords Mismatch: expected " << e.NumRecords() << ", got " << a.NumRecords();
throw std::runtime_error(msg.str());
}
for (int r = 0; r < e.NumRecords(); ++r) {
auto er = e.RecordAt(r);
auto ar = a.RecordAt(r);
if (er.RecordType() != ar.RecordType()) {
msg << "Event["<<idx<<"].Record[" << r << "] RecordType Mismatch: expected " << er.RecordType() << ", got " << ar.RecordType();
throw std::runtime_error(msg.str());
}
if (er.RecordTypeNamePtr() == nullptr || ar.RecordTypeNamePtr() == nullptr) {
if (er.RecordTypeNamePtr() != ar.RecordTypeNamePtr()) {
msg << "Event["<<idx<<"].Record[" << r << "] RecordTypeName Mismatch: expected "
<< (er.RecordTypeNamePtr() == nullptr ? "null" : er.RecordTypeName())
<< ", got "
<< (ar.RecordTypeNamePtr() == nullptr ? "null" : ar.RecordTypeName());
throw std::runtime_error(msg.str());
}
} else {
if (strcmp(er.RecordTypeNamePtr(), ar.RecordTypeNamePtr()) != 0) {
msg << "Event["<<idx<<"].Record[" << r << "] RecordTypeName Mismatch: expected " << er.RecordTypeNamePtr() << ", got " << ar.RecordTypeNamePtr();
throw std::runtime_error(msg.str());
}
}
if (er.RecordTextPtr() == nullptr || ar.RecordTextPtr() == nullptr) {
if (er.RecordTextPtr() != ar.RecordTextPtr()) {
msg << "Event["<<idx<<"].Record[" << r << "] RecordText Mismatch: expected "
<< (er.RecordTextPtr() == nullptr ? "null" : er.RecordTextPtr())
<< ", got "
<< (ar.RecordTextPtr() == nullptr ? "null" : ar.RecordTextPtr());
throw std::runtime_error(msg.str());
}
} else {
if (strcmp(er.RecordTextPtr(), ar.RecordTextPtr()) != 0) {
msg << "Event["<<idx<<"].Record[" << r << "] RecordText Mismatch: expected " << er.RecordTextPtr() << ", got " << ar.RecordTextPtr();
throw std::runtime_error(msg.str());
}
}
if (er.NumFields() != ar.NumFields()) {
msg << "Event["<<idx<<"].Record[" << r << "] NumFields Mismatch: expected " << er.NumFields() << ", got " << ar.NumFields() << "\n";
std::unordered_set<std::string> _en;
std::unordered_set<std::string> _an;
for (auto f : er) {
_en.emplace(f.FieldNamePtr(), f.FieldNameSize());
}
for (auto f : ar) {
_an.emplace(f.FieldNamePtr(), f.FieldNameSize());
}
for (auto name : _en) {
if (_an.count(name) == 0) {
msg << " Expected Field Name Not Found: " << name << "\n";
}
}
for (auto name : _an) {
if (_en.count(name) == 0) {
msg << " Unxpected Field Name Found: " << name << "\n";
}
}
throw std::runtime_error(msg.str());
}
for (int f = 0; f < er.NumFields(); ++f) {
auto ef = er.FieldAt(f);
auto af = ar.FieldAt(f);
if (ef.FieldNamePtr() == nullptr || af.FieldNamePtr() == nullptr) {
if (ef.FieldNamePtr() != af.FieldNamePtr()) {
msg << "Event["<<idx<<"].Record[" << r << "].Field[" << f << "] FieldName Mismatch: expected "
<< (ef.FieldNamePtr() == nullptr ? "null" : ef.FieldNamePtr())
<< ", got "
<< (af.FieldNamePtr() == nullptr ? "null" : af.FieldNamePtr());
throw std::runtime_error(msg.str());
}
} else {
if (strcmp(ef.FieldNamePtr(), af.FieldNamePtr()) != 0) {
msg << "Event["<<idx<<"].Record[" << r << "].Field[" << f << "] FieldName Mismatch: expected " << ef.FieldNamePtr() << ", got " << af.FieldNamePtr();
throw std::runtime_error(msg.str());
}
}
if (ef.RawValuePtr() == nullptr || af.RawValuePtr() == nullptr) {
if (ef.RawValuePtr() != af.RawValuePtr()) {
msg << "Event["<<idx<<"].Record[" << r << "].Field[" << f << "] RawValue Mismatch: expected "
<< (ef.RawValuePtr() == nullptr ? "null" : ef.RawValuePtr())
<< ", got "
<< (af.RawValuePtr() == nullptr ? "null" : af.RawValuePtr());
throw std::runtime_error(msg.str());
}
} else {
if (strcmp(ef.RawValuePtr(), af.RawValuePtr()) != 0) {
msg << "Event["<<idx<<"].Record[" << r << "].Field[" << f << "] RawValue Mismatch: expected " << ef.RawValuePtr() << ", got " << af.RawValuePtr();
throw std::runtime_error(msg.str());
}
}
if (ef.InterpValuePtr() == nullptr || af.InterpValuePtr() == nullptr) {
if (ef.InterpValuePtr() != af.InterpValuePtr()) {
msg << "Event["<<idx<<"].Record[" << r << "].Field[" << f << "] (Name="<<ef.FieldName()<<") InterpValue Mismatch: expected "
<< (ef.InterpValuePtr() == nullptr ? "null" : ef.InterpValuePtr())
<< ", got "
<< (af.InterpValuePtr() == nullptr ? "null" : af.InterpValuePtr());
throw std::runtime_error(msg.str());
}
} else {
if (strcmp(ef.InterpValuePtr(), af.InterpValuePtr()) != 0) {
msg << "Event["<<idx<<"].Record[" << r << "].Field[" << f << "] (Name="<<ef.FieldName()<<") InterpValue Mismatch: expected " << ef.InterpValuePtr() << ", got " << af.InterpValuePtr();
throw std::runtime_error(msg.str());
}
}
if (ef.FieldType() != af.FieldType()) {
msg << "Event["<<idx<<"].Record[" << r << "].Field[" << f << "] (Name="<<ef.FieldName()<<") FieldType Mismatch: expected " << static_cast<uint>(ef.FieldType()) << ", got " << static_cast<uint>(af.FieldType());
throw std::runtime_error(msg.str());
}
}
}
}
BOOST_AUTO_TEST_CASE( basic_test ) {
TempDir dir("/tmp/EventProcessorTests");
write_file(dir.Path() + "/passwd", passwd_file_text);
write_file(dir.Path() + "/group", group_file_text);
auto user_db = std::make_shared<UserDB>(dir.Path());
user_db->update();
auto expected_queue = new TestEventQueue();
auto actual_queue = new TestEventQueue();
auto metrics_queue = new TestEventQueue();
auto prioritizer = DefaultPrioritizer::Create(0);
auto expected_allocator = std::shared_ptr<IEventBuilderAllocator>(expected_queue);
auto actual_allocator = std::shared_ptr<IEventBuilderAllocator>(actual_queue);
auto metrics_allocator = std::shared_ptr<IEventBuilderAllocator>(metrics_queue);
auto expected_builder = std::make_shared<EventBuilder>(expected_allocator, prioritizer);
auto actual_builder = std::make_shared<EventBuilder>(actual_allocator, prioritizer);
auto metrics_builder = std::make_shared<EventBuilder>(metrics_allocator, prioritizer);
auto proc_filter = std::make_shared<ProcFilter>(user_db);
std::shared_ptr<FiltersEngine> filtersEngine; // Intentionally left unassigned
std::shared_ptr<ProcessTree> processTree; // Intentionally left unassigned
auto metrics = std::make_shared<Metrics>("test", metrics_builder);
auto cmdline_redactor = std::make_shared<CmdlineRedactor>();
auto test_rule = std::make_shared<const CmdlineRedactionRule>(test_redaction_rule_filename, test_redaction_rule_name, test_redaction_rule_regex, '*');
cmdline_redactor->AddRule(test_rule);
auto raw_proc = std::make_shared<RawEventProcessor>(actual_builder, user_db, cmdline_redactor, processTree, filtersEngine, metrics);
auto actual_raw_queue = new RawEventQueue(raw_proc);
auto actual_raw_allocator = std::shared_ptr<IEventBuilderAllocator>(actual_raw_queue);
auto actual_raw_builder = std::make_shared<EventBuilder>(actual_raw_allocator, prioritizer);
for (auto e : test_events) {
e.Write(expected_builder);
}
RawEventAccumulator accumulator(actual_raw_builder, metrics);
for (int i = 0; i < raw_test_events.size(); i++) {
auto raw_event = raw_test_events[i];
auto do_flush = raw_events_do_flush[i];
std::string event_txt = raw_event;
auto lines = split(event_txt, '\n');
for (auto& line: lines) {
std::unique_ptr<RawEventRecord> record = std::make_unique<RawEventRecord>();
std::memcpy(record->Data(), line.c_str(), line.size());
if (record->Parse(RecordType::UNKNOWN, line.size())) {
accumulator.AddRecord(std::move(record));
} else {
Logger::Warn("Received unparsable event data: %s", line.c_str());
}
}
if (do_flush) {
accumulator.Flush(0);
}
}
BOOST_REQUIRE_EQUAL(expected_queue->GetEventCount(), actual_queue->GetEventCount());
for (size_t idx = 0; idx < expected_queue->GetEventCount(); ++idx) {
diff_event(idx, expected_queue->GetEvent(idx), actual_queue->GetEvent(idx));
}
}
BOOST_AUTO_TEST_CASE( oversized_event_test ) {
TempDir dir("/tmp/EventProcessorTests");
write_file(dir.Path() + "/passwd", passwd_file_text);
write_file(dir.Path() + "/group", group_file_text);
auto user_db = std::make_shared<UserDB>(dir.Path());
user_db->update();
auto actual_queue = new TestEventQueue();
auto metrics_queue = new TestEventQueue();
auto prioritizer = DefaultPrioritizer::Create(0);
auto actual_allocator = std::shared_ptr<IEventBuilderAllocator>(actual_queue);
auto metrics_allocator = std::shared_ptr<IEventBuilderAllocator>(metrics_queue);
auto actual_builder = std::make_shared<EventBuilder>(actual_allocator, prioritizer);
auto metrics_builder = std::make_shared<EventBuilder>(metrics_allocator, prioritizer);
auto proc_filter = std::make_shared<ProcFilter>(user_db);
auto filtersEngine = std::make_shared<FiltersEngine>();
auto processTree = std::make_shared<ProcessTree>(user_db, filtersEngine);
auto metrics = std::make_shared<Metrics>("test", metrics_builder);
auto cmdline_redactor = std::make_shared<CmdlineRedactor>();
auto raw_proc = std::make_shared<RawEventProcessor>(actual_builder, user_db, cmdline_redactor, processTree, filtersEngine, metrics);
auto actual_raw_queue = new RawEventQueue(raw_proc);
auto actual_raw_allocator = std::shared_ptr<IEventBuilderAllocator>(actual_raw_queue);
auto actual_raw_builder = std::make_shared<EventBuilder>(actual_raw_allocator, prioritizer);
RawEventAccumulator accumulator(actual_raw_builder, metrics);
auto lines = split(oversized_event_text, '\n');
for (auto& line: lines) {
std::unique_ptr<RawEventRecord> record = std::make_unique<RawEventRecord>();
std::memcpy(record->Data(), line.c_str(), line.size());
if (record->Parse(RecordType::UNKNOWN, line.size())) {
accumulator.AddRecord(std::move(record));
} else {
Logger::Warn("Received unparsable event data: %s", line.c_str());
}
}
accumulator.Flush(0);
BOOST_REQUIRE_EQUAL(actual_queue->GetEventCount(), 1);
Event e = actual_queue->GetEvent(0);
BOOST_REQUIRE_LE(e.Size(), InputBuffer::MAX_DATA_SIZE);
}