Skip to content

Commit 63bef3d

Browse files
victorhoraFelipe Zimmerle
authored and
Felipe Zimmerle
committed
Support to JSON stuff on serial logging
1 parent 2988c5b commit 63bef3d

16 files changed

+7417
-7252
lines changed

headers/modsecurity/audit_log.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ class AuditLog {
5353
RelevantOnlyAuditLogStatus
5454
};
5555

56+
enum AuditLogFormat {
57+
NotSetAuditLogFormat,
58+
JSONAuditLogFormat,
59+
NativeAuditLogFormat
60+
};
61+
5662
enum AuditLogParts {
5763
/**
5864
* Audit log header (mandatory).
@@ -150,6 +156,7 @@ class AuditLog {
150156
bool setFilePath1(const std::basic_string<char>& path);
151157
bool setFilePath2(const std::basic_string<char>& path);
152158
bool setStorageDir(const std::basic_string<char>& path);
159+
bool setFormat(AuditLogFormat format);
153160

154161
int getDirectoryPermission();
155162
int getFilePermission();
@@ -186,6 +193,7 @@ class AuditLog {
186193
}
187194
return false;
188195
}
196+
AuditLogFormat m_format;
189197

190198
protected:
191199
int m_parts;
@@ -198,7 +206,7 @@ class AuditLog {
198206
int m_directoryPermission;
199207
int m_defaultDirectoryPermission = 0750;
200208

201-
private:
209+
private:
202210
AuditLogStatus m_status;
203211

204212
AuditLogType m_type;

headers/modsecurity/transaction.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,8 @@ class Transaction : public TransactionAnchoredVariables {
319319
size_t offset);
320320

321321
const char *getResponseBody();
322-
int getResponseBodyLength();
322+
size_t getResponseBodyLength();
323+
size_t getRequestBodyLength();
323324

324325
#ifndef NO_LOGS
325326
void debug(int, std::string);
@@ -612,7 +613,10 @@ int msc_process_uri(Transaction *transaction, const char *uri,
612613
const char *msc_get_response_body(Transaction *transaction);
613614

614615
/** @ingroup ModSecurity_C_API */
615-
int msc_get_response_body_length(Transaction *transaction);
616+
size_t msc_get_response_body_length(Transaction *transaction);
617+
618+
/** @ingroup ModSecurity_C_API */
619+
size_t msc_get_request_body_length(Transaction *transaction);
616620

617621
/** @ingroup ModSecurity_C_API */
618622
void msc_transaction_cleanup(Transaction *transaction);

src/audit_log/audit_log.cc

+8
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ bool AuditLog::setFilePath2(const std::basic_string<char>& path) {
129129
return true;
130130
}
131131

132+
bool AuditLog::setFormat(AuditLogFormat fmt) {
133+
this->m_format = fmt;
134+
return true;
135+
}
132136

133137
int AuditLog::addParts(int parts, const std::string& new_parts) {
134138
PARTS_CONSTAINS('A', AAuditLogPart)
@@ -349,6 +353,10 @@ bool AuditLog::merge(AuditLog *from, std::string *error) {
349353
m_parts = from->m_parts;
350354
}
351355

356+
if (from->m_format != NotSetAuditLogFormat) {
357+
m_format = from->m_format;
358+
}
359+
352360
return init(error);
353361
}
354362

src/audit_log/writer/parallel.cc

+10-1
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,21 @@ bool Parallel::init(std::string *error) {
103103

104104
bool Parallel::write(Transaction *transaction, int parts, std::string *error) {
105105
int fd;
106-
std::string log = transaction->toJSON(parts);
106+
std::string log;
107107
std::string fileName = logFilePath(&transaction->m_timeStamp,
108108
YearMonthDayDirectory | YearMonthDayAndTimeDirectory
109109
| YearMonthDayAndTimeFileName);
110110
bool ret;
111111

112+
if (transaction->m_rules->m_auditLog->m_format ==
113+
audit_log::AuditLog::JSONAuditLogFormat) {
114+
log = transaction->toJSON(parts);
115+
} else {
116+
std::string boundary;
117+
generateBoundary(&boundary);
118+
log = transaction->toOldAuditLogFormat(parts, "-" + boundary + "--");
119+
}
120+
112121
std::string logPath = m_audit->m_storage_dir;
113122
fileName = logPath + fileName + "-" + transaction->m_id;
114123

src/audit_log/writer/parallel.h

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "modsecurity/transaction.h"
2323
#include "modsecurity/audit_log.h"
2424
#include "src/utils/shared_files.h"
25+
#include "modsecurity/rules.h"
2526

2627
#ifdef __cplusplus
2728

src/audit_log/writer/serial.cc

+8-15
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,22 @@ Serial::~Serial() {
2828
}
2929

3030

31-
void Serial::generateBoundary(std::string *boundary) {
32-
static const char alphanum[] =
33-
"0123456789"
34-
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
35-
"abcdefghijklmnopqrstuvwxyz";
36-
37-
for (int i = 0; i < SERIAL_AUDIT_LOG_BOUNDARY_LENGTH; ++i) {
38-
boundary->append(1, alphanum[rand() % (sizeof(alphanum) - 1)]);
39-
}
40-
}
41-
42-
4331
bool Serial::init(std::string *error) {
4432
return utils::SharedFiles::getInstance().open(m_audit->m_path1, error);
4533
}
4634

4735

4836
bool Serial::write(Transaction *transaction, int parts, std::string *error) {
49-
std::string boundary;
5037
std::string msg;
5138

52-
generateBoundary(&boundary);
53-
msg = transaction->toOldAuditLogFormat(parts, "-" + boundary + "--");
39+
if (transaction->m_rules->m_auditLog->m_format ==
40+
audit_log::AuditLog::JSONAuditLogFormat) {
41+
msg = transaction->toJSON(parts);
42+
} else {
43+
std::string boundary;
44+
generateBoundary(&boundary);
45+
msg = transaction->toOldAuditLogFormat(parts, "-" + boundary + "--");
46+
}
5447

5548
return utils::SharedFiles::getInstance().write(m_audit->m_path1, msg,
5649
error);

src/audit_log/writer/serial.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,14 @@
2626
#include "src/utils/shared_files.h"
2727
#include "modsecurity/transaction.h"
2828
#include "modsecurity/audit_log.h"
29+
#include "modsecurity/rules.h"
2930

3031
#ifdef __cplusplus
3132

3233
namespace modsecurity {
3334
namespace audit_log {
3435
namespace writer {
3536

36-
#define SERIAL_AUDIT_LOG_BOUNDARY_LENGTH 8
37-
3837

3938
/** @ingroup ModSecurity_CPP_API */
4039
class Serial : public Writer {
@@ -49,7 +48,6 @@ class Serial : public Writer {
4948
bool write(Transaction *transaction, int parts,
5049
std::string *error) override;
5150

52-
void generateBoundary(std::string *boundary);
5351
};
5452

5553
} // namespace writer

src/audit_log/writer/writer.cc

+9
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,16 @@ namespace modsecurity {
2323
namespace audit_log {
2424
namespace writer {
2525

26+
void Writer::generateBoundary(std::string *boundary) {
27+
static const char alphanum[] =
28+
"0123456789"
29+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
30+
"abcdefghijklmnopqrstuvwxyz";
2631

32+
for (int i = 0; i < SERIAL_AUDIT_LOG_BOUNDARY_LENGTH; ++i) {
33+
boundary->append(1, alphanum[rand() % (sizeof(alphanum) - 1)]);
34+
}
35+
}
2736

2837
} // namespace writer
2938
} // namespace audit_log

src/audit_log/writer/writer.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131
#include "modsecurity/transaction.h"
3232
#include "modsecurity/audit_log.h"
3333

34+
#define SERIAL_AUDIT_LOG_BOUNDARY_LENGTH 8
3435

3536
namespace modsecurity {
3637
namespace audit_log {
3738
namespace writer {
3839

3940

40-
4141
/** @ingroup ModSecurity_CPP_API */
4242
class Writer {
4343
public:
@@ -51,6 +51,7 @@ class Writer {
5151
virtual bool write(Transaction *transaction, int parts,
5252
std::string *error) = 0;
5353

54+
void generateBoundary(std::string *boundary);
5455

5556
void refCountIncrease() {
5657
m_refereceCount++;

0 commit comments

Comments
 (0)