-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathRequestProcessor.cpp
More file actions
203 lines (169 loc) · 6.67 KB
/
Copy pathRequestProcessor.cpp
File metadata and controls
203 lines (169 loc) · 6.67 KB
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
#include "Includes.h"
RequestProcessor requestProcessor;
std::string RequestProcessor::GetInitData() {
LoadEnvironment();
json initData = {
{"token", AIKIDO_GLOBAL(token)},
{"log_level", AIKIDO_GLOBAL(log_level_str)},
{"socket_path", AIKIDO_GLOBAL(socket_path)},
{"blocking", AIKIDO_GLOBAL(blocking)},
{"trust_proxy", AIKIDO_GLOBAL(trust_proxy)},
{"localhost_allowed_by_default", AIKIDO_GLOBAL(localhost_allowed_by_default)},
{"collect_api_schema", AIKIDO_GLOBAL(collect_api_schema)},
{"sapi", AIKIDO_GLOBAL(sapi_name)}};
return initData.dump();
}
bool RequestProcessor::ContextInit() {
return this->requestProcessorContextInitFn(GoContextCallback);
}
bool RequestProcessor::SendEvent(EVENT_ID eventId, std::string& output) {
if (!this->requestInitialized) {
return false;
}
AIKIDO_LOG_DEBUG("Sending event %s...\n", GetEventName(eventId));
char* charPtr = requestProcessorOnEventFn(eventId);
if (!charPtr) {
AIKIDO_LOG_DEBUG("Got event reply: nullptr\n");
return true;
}
AIKIDO_LOG_DEBUG("Got event reply: %s\n", charPtr);
output = charPtr;
free(charPtr);
return true;
}
void RequestProcessor::SendPreRequestEvent() {
try {
std::string outputEvent;
SendEvent(EVENT_PRE_REQUEST, outputEvent);
action.Execute(outputEvent);
} catch (const std::exception& e) {
AIKIDO_LOG_ERROR("Exception encountered in processing request init metadata: %s\n", e.what());
}
}
void RequestProcessor::SendPostRequestEvent() {
try {
std::string outputEvent;
SendEvent(EVENT_POST_REQUEST, outputEvent);
action.Execute(outputEvent);
} catch (const std::exception& e) {
AIKIDO_LOG_ERROR("Exception encountered in processing request shutdown metadata: %s\n", e.what());
}
}
/*
If the blocking mode is set from agent (different than -1), return that.
Otherwise, return the env variable AIKIDO_BLOCK.
*/
bool RequestProcessor::IsBlockingEnabled() {
if (!this->requestInitialized) {
return false;
}
int ret = requestProcessorGetBlockingModeFn();
if (ret == -1) {
return AIKIDO_GLOBAL(blocking);
}
return ret;
}
bool RequestProcessor::ReportStats() {
AIKIDO_LOG_INFO("Reporting stats to Aikido Request Processor...\n");
for (const auto& [sink, sinkStats] : stats) {
AIKIDO_LOG_INFO("Reporting stats for sink \"%s\" to Aikido Request Processor...\n", sink.c_str());
requestProcessorReportStatsFn(GoCreateString(sink), sinkStats.attacksDetected, sinkStats.attacksBlocked, sinkStats.interceptorThrewError, sinkStats.withoutContext, sinkStats.timings.size(), GoCreateSlice(sinkStats.timings));
}
stats.clear();
return true;
}
bool RequestProcessor::Init() {
if (this->initFailed) {
return false;
}
if (this->libHandle) {
return true;
}
std::string requestProcessorLibPath = AIKIDO_INSTALL_DIR "/aikido-request-processor.so";
this->libHandle = dlopen(requestProcessorLibPath.c_str(), RTLD_LAZY);
if (!this->libHandle) {
AIKIDO_LOG_ERROR("Error loading the Aikido Request Processor library from %s: %s!\n", requestProcessorLibPath.c_str(), dlerror());
this->initFailed = true;
return false;
}
AIKIDO_LOG_INFO("Initializing Aikido Request Processor...\n");
RequestProcessorInitFn requestProcessorInitFn = (RequestProcessorInitFn)dlsym(libHandle, "RequestProcessorInit");
this->requestProcessorContextInitFn = (RequestProcessorContextInitFn)dlsym(libHandle, "RequestProcessorContextInit");
this->requestProcessorConfigUpdateFn = (RequestProcessorConfigUpdateFn)dlsym(libHandle, "RequestProcessorConfigUpdate");
this->requestProcessorOnEventFn = (RequestProcessorOnEventFn)dlsym(libHandle, "RequestProcessorOnEvent");
this->requestProcessorGetBlockingModeFn = (RequestProcessorGetBlockingModeFn)dlsym(libHandle, "RequestProcessorGetBlockingMode");
this->requestProcessorReportStatsFn = (RequestProcessorReportStats)dlsym(libHandle, "RequestProcessorReportStats");
this->requestProcessorUninitFn = (RequestProcessorUninitFn)dlsym(libHandle, "RequestProcessorUninit");
if (!requestProcessorInitFn ||
!this->requestProcessorContextInitFn ||
!this->requestProcessorConfigUpdateFn ||
!this->requestProcessorOnEventFn ||
!this->requestProcessorGetBlockingModeFn ||
!this->requestProcessorReportStatsFn ||
!this->requestProcessorUninitFn) {
AIKIDO_LOG_ERROR("Error loading symbols from the Aikido Request Processor library!\n");
this->initFailed = true;
return false;
}
std::string initDataString = this->GetInitData();
if (!requestProcessorInitFn(GoCreateString(initDataString))) {
AIKIDO_LOG_ERROR("Failed to initialize Aikido Request Processor library: %s!\n", dlerror());
this->initFailed = true;
return false;
}
AIKIDO_LOG_INFO("Aikido Request Processor initialized successfully!\n");
return true;
}
bool RequestProcessor::RequestInit() {
if (!this->Init()) {
AIKIDO_LOG_ERROR("Failed to initialize the request processor!\n");
return false;
}
if (!request.Init()) {
AIKIDO_LOG_WARN("Failed to initialize the current request!\n");
return false;
}
this->requestInitialized = true;
this->numberOfRequests++;
ContextInit();
SendPreRequestEvent();
if ((this->numberOfRequests % AIKIDO_GLOBAL(report_stats_interval_to_agent)) == 0) {
requestProcessor.ReportStats();
}
return true;
}
void RequestProcessor::LoadConfigOnce() {
if (this->configReloaded) {
return;
}
AIKIDO_LOG_INFO("Reloading Aikido config...\n");
std::string initJson = this->GetInitData();
this->requestProcessorConfigUpdateFn(GoCreateString(initJson));
this->configReloaded = true;
}
void RequestProcessor::RequestShutdown() {
if (!request.Init()) {
AIKIDO_LOG_WARN("Failed to initialize the current request!\n");
return;
}
LoadConfigOnce();
SendPostRequestEvent();
this->requestInitialized = false;
}
void RequestProcessor::Uninit() {
if (!this->libHandle) {
return;
}
if (!this->initFailed && this->requestProcessorUninitFn) {
AIKIDO_LOG_INFO("Reporting final stats to Aikido Request Processor...\n");
this->ReportStats();
AIKIDO_LOG_INFO("Calling uninit for Aikido Request Processor...\n");
this->requestProcessorUninitFn();
}
dlclose(this->libHandle);
this->libHandle = nullptr;
AIKIDO_LOG_INFO("Aikido Request Processor unloaded!\n");
}
RequestProcessor::~RequestProcessor() {
this->Uninit();
}