Places the classes related to audit log into a separate namespace

This commit is contained in:
Felipe Zimmerle 2016-01-14 14:25:33 -03:00
parent 2830525f89
commit b06eaadac7
14 changed files with 97 additions and 74 deletions

View File

@ -33,7 +33,9 @@
namespace modsecurity { namespace modsecurity {
class Rule; class Rule;
namespace audit_log {
class AuditLog; class AuditLog;
}
namespace actions { namespace actions {
class Action; class Action;
} }
@ -207,7 +209,7 @@ class RulesProperties {
std::ostringstream parserError; std::ostringstream parserError;
AuditLog *audit_log; audit_log::AuditLog *audit_log;
OnFailedRemoteRulesAction remoteRulesActionOnFailed; OnFailedRemoteRulesAction remoteRulesActionOnFailed;
}; };

View File

@ -177,10 +177,10 @@ libmodsecurity_la_SOURCES = \
parser/seclang-scanner.ll \ parser/seclang-scanner.ll \
parser/driver.cc \ parser/driver.cc \
transaction.cc \ transaction.cc \
audit_log.cc \ audit_log/audit_log.cc \
audit_log_writer.cc \ audit_log/writer.cc \
audit_log_writer_serial.cc \ audit_log/writer/serial.cc \
audit_log_writer_parallel.cc \ audit_log/writer/parallel.cc \
modsecurity.cc \ modsecurity.cc \
rules.cc \ rules.cc \
utils.cc \ utils.cc \

View File

@ -13,7 +13,7 @@
* *
*/ */
#include "src/audit_log.h" #include "audit_log/audit_log.h"
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
@ -21,8 +21,8 @@
#include <fstream> #include <fstream>
#include "src/audit_log_writer_parallel.h" #include "audit_log/writer/parallel.h"
#include "src/audit_log_writer_serial.h" #include "audit_log/writer/serial.h"
#include "utils/regex.h" #include "utils/regex.h"
#define PARTS_CONSTAINS(a, c) \ #define PARTS_CONSTAINS(a, c) \
@ -38,6 +38,7 @@
} }
namespace modsecurity { namespace modsecurity {
namespace audit_log {
AuditLog::AuditLog() AuditLog::AuditLog()
: m_status(OffAuditLogStatus), : m_status(OffAuditLogStatus),
@ -178,10 +179,10 @@ bool AuditLog::setType(AuditLogType audit_type) {
bool AuditLog::init() { bool AuditLog::init() {
if (m_type == ParallelAuditLogType) { if (m_type == ParallelAuditLogType) {
m_writer = new AuditLogWriterParallel(this); m_writer = new audit_log::writer::Parallel(this);
} }
if (m_type == SerialAuditLogType) { if (m_type == SerialAuditLogType) {
m_writer = new AuditLogWriterSerial(this); m_writer = new audit_log::writer::Serial(this);
} }
m_writer->refCountIncrease(); m_writer->refCountIncrease();
@ -253,4 +254,5 @@ bool AuditLog::close() {
} }
} // namespace audit_log
} // namespace modsecurity } // namespace modsecurity

View File

@ -19,15 +19,16 @@
#include <string> #include <string>
#endif #endif
#ifndef SRC_AUDIT_LOG_H_ #ifndef SRC_AUDIT_LOG_AUDIT_LOG_H_
#define SRC_AUDIT_LOG_H_ #define SRC_AUDIT_LOG_AUDIT_LOG_H_
#include "modsecurity/transaction.h" #include "modsecurity/transaction.h"
#include "src/audit_log_writer.h" #include "audit_log/writer.h"
#ifdef __cplusplus #ifdef __cplusplus
namespace modsecurity { namespace modsecurity {
namespace audit_log {
/** @ingroup ModSecurity_CPP_API */ /** @ingroup ModSecurity_CPP_API */
class AuditLog { class AuditLog {
@ -176,11 +177,12 @@ class AuditLog {
AuditLogType m_type; AuditLogType m_type;
std::string m_relevant; std::string m_relevant;
AuditLogWriter *m_writer; audit_log::Writer *m_writer;
int m_refereceCount; int m_refereceCount;
}; };
} // namespace audit_log
} // namespace modsecurity } // namespace modsecurity
#endif #endif
#endif // SRC_AUDIT_LOG_H_ #endif // SRC_AUDIT_LOG_AUDIT_LOG_H_

View File

@ -13,15 +13,16 @@
* *
*/ */
#include "src/audit_log_writer.h" #include "audit_log/writer.h"
#include <string> #include <string>
#include "src/audit_log.h" #include "audit_log/audit_log.h"
namespace modsecurity { namespace modsecurity {
namespace audit_log {
std::string AuditLogWriter::file_name(const std::string& unique_id) { std::string Writer::file_name(const std::string& unique_id) {
time_t timer; time_t timer;
time(&timer); time(&timer);
@ -33,10 +34,11 @@ std::string AuditLogWriter::file_name(const std::string& unique_id) {
* Temporary print the log into the std::cout to debug purposes. * Temporary print the log into the std::cout to debug purposes.
* *
*/ */
bool AuditLogWriter::write(Transaction *transaction, int parts) { bool Writer::write(Transaction *transaction, int parts) {
std::cout << transaction->toJSON(0) << std::endl; std::cout << transaction->toJSON(0) << std::endl;
return true; return true;
} }
} // namespace audit_log
} // namespace modsecurity } // namespace modsecurity

View File

@ -26,16 +26,18 @@
#ifdef __cplusplus #ifdef __cplusplus
namespace modsecurity { namespace modsecurity {
namespace audit_log {
class AuditLog; class AuditLog;
/** @ingroup ModSecurity_CPP_API */ /** @ingroup ModSecurity_CPP_API */
class AuditLogWriter { class Writer {
public: public:
explicit AuditLogWriter(AuditLog *audit) explicit Writer(AuditLog *audit)
: m_audit(audit), : m_audit(audit),
m_refereceCount(0) { } m_refereceCount(0) { }
virtual ~AuditLogWriter() { } virtual ~Writer() { }
virtual void refCountIncrease() = 0; virtual void refCountIncrease() = 0;
virtual void refCountDecreaseAndCheck() = 0; virtual void refCountDecreaseAndCheck() = 0;
@ -50,6 +52,7 @@ class AuditLogWriter {
int m_refereceCount; int m_refereceCount;
}; };
} // namespace audit_log
} // namespace modsecurity } // namespace modsecurity
#endif #endif

View File

@ -13,7 +13,7 @@
* *
*/ */
#include "src/audit_log_writer_parallel.h" #include "audit_log/writer/parallel.h"
#include <time.h> #include <time.h>
#include <stdio.h> #include <stdio.h>
@ -24,15 +24,16 @@
#include <fstream> #include <fstream>
#include "src/audit_log.h" #include "audit_log/audit_log.h"
#include "modsecurity/transaction.h" #include "modsecurity/transaction.h"
#include "src/utils.h" #include "src/utils.h"
#include "utils/md5.h" #include "utils/md5.h"
namespace modsecurity { namespace modsecurity {
namespace audit_log {
namespace writer {
Parallel::~Parallel() {
AuditLogWriterParallel::~AuditLogWriterParallel() {
if (log1.is_open()) { if (log1.is_open()) {
log1.close(); log1.close();
} }
@ -43,7 +44,7 @@ AuditLogWriterParallel::~AuditLogWriterParallel() {
} }
inline std::string AuditLogWriterParallel::logFilePath(time_t *t, inline std::string Parallel::logFilePath(time_t *t,
int part) { int part) {
struct tm timeinfo; struct tm timeinfo;
char tstr[300]; char tstr[300];
@ -73,7 +74,7 @@ inline std::string AuditLogWriterParallel::logFilePath(time_t *t,
} }
bool AuditLogWriterParallel::init() { bool Parallel::init() {
/** TODO:: Check if the directory exists. */ /** TODO:: Check if the directory exists. */
/** TODO:: Checking if we have permission to write in the target dir */ /** TODO:: Checking if we have permission to write in the target dir */
@ -89,7 +90,7 @@ bool AuditLogWriterParallel::init() {
} }
bool AuditLogWriterParallel::write(Transaction *transaction, int parts) { bool Parallel::write(Transaction *transaction, int parts) {
FILE *fp; FILE *fp;
int fd; int fd;
std::string log = transaction->toJSON(parts); std::string log = transaction->toJSON(parts);
@ -136,5 +137,6 @@ bool AuditLogWriterParallel::write(Transaction *transaction, int parts) {
return true; return true;
} }
} // namespace writer
} // namespace audit_log
} // namespace modsecurity } // namespace modsecurity

View File

@ -18,20 +18,22 @@
#ifndef SRC_AUDIT_LOG_WRITER_PARALLEL_H_ #ifndef SRC_AUDIT_LOG_WRITER_PARALLEL_H_
#define SRC_AUDIT_LOG_WRITER_PARALLEL_H_ #define SRC_AUDIT_LOG_WRITER_PARALLEL_H_
#include "src/audit_log_writer.h" #include "audit_log/writer.h"
#include "modsecurity/transaction.h" #include "modsecurity/transaction.h"
#ifdef __cplusplus #ifdef __cplusplus
namespace modsecurity { namespace modsecurity {
namespace audit_log {
namespace writer {
/** @ingroup ModSecurity_CPP_API */ /** @ingroup ModSecurity_CPP_API */
class AuditLogWriterParallel : public AuditLogWriter { class Parallel : public audit_log::Writer {
public: public:
explicit AuditLogWriterParallel(AuditLog *audit) explicit Parallel(AuditLog *audit)
: AuditLogWriter(audit) { } : audit_log::Writer(audit) { }
~AuditLogWriterParallel() override; ~Parallel() override;
bool init() override; bool init() override;
bool write(Transaction *transaction, int parts) override; bool write(Transaction *transaction, int parts) override;
@ -75,6 +77,8 @@ class AuditLogWriterParallel : public AuditLogWriter {
inline std::string logFilePath(time_t *t, int part); inline std::string logFilePath(time_t *t, int part);
}; };
} // namespace writer
} // namespace audit_log
} // namespace modsecurity } // namespace modsecurity
#endif #endif

View File

@ -13,23 +13,24 @@
* *
*/ */
#include "src/audit_log_writer_serial.h" #include "audit_log/writer/serial.h"
// #include <mutex> // #include <mutex>
#include "src/audit_log.h" #include "audit_log/audit_log.h"
namespace modsecurity { namespace modsecurity {
namespace audit_log {
namespace writer {
// static std::mutex serialLoggingMutex; // static std::mutex serialLoggingMutex;
AuditLogWriterSerial::~AuditLogWriterSerial() { Serial::~Serial() {
m_log.close(); m_log.close();
} }
void AuditLogWriterSerial::generateBoundary(std::string *boundary) { void Serial::generateBoundary(std::string *boundary) {
static const char alphanum[] = static const char alphanum[] =
"0123456789" "0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
@ -41,13 +42,13 @@ void AuditLogWriterSerial::generateBoundary(std::string *boundary) {
} }
bool AuditLogWriterSerial::init() { bool Serial::init() {
m_log.open(m_audit->m_path1, std::fstream::out | std::fstream::app); m_log.open(m_audit->m_path1, std::fstream::out | std::fstream::app);
return true; return true;
} }
bool AuditLogWriterSerial::write(Transaction *transaction, int parts) { bool Serial::write(Transaction *transaction, int parts) {
std::string boundary; std::string boundary;
generateBoundary(&boundary); generateBoundary(&boundary);
@ -62,5 +63,6 @@ bool AuditLogWriterSerial::write(Transaction *transaction, int parts) {
return true; return true;
} }
} // namespace writer
} // namespace audit_log
} // namespace modsecurity } // namespace modsecurity

View File

@ -22,22 +22,24 @@
#ifndef SRC_AUDIT_LOG_WRITER_SERIAL_H_ #ifndef SRC_AUDIT_LOG_WRITER_SERIAL_H_
#define SRC_AUDIT_LOG_WRITER_SERIAL_H_ #define SRC_AUDIT_LOG_WRITER_SERIAL_H_
#include "src/audit_log_writer.h" #include "audit_log/writer.h"
#include "modsecurity/transaction.h" #include "modsecurity/transaction.h"
#ifdef __cplusplus #ifdef __cplusplus
namespace modsecurity { namespace modsecurity {
namespace audit_log {
namespace writer {
#define SERIAL_AUDIT_LOG_BOUNDARY_LENGTH 8 #define SERIAL_AUDIT_LOG_BOUNDARY_LENGTH 8
/** @ingroup ModSecurity_CPP_API */ /** @ingroup ModSecurity_CPP_API */
class AuditLogWriterSerial : public AuditLogWriter { class Serial : public audit_log::Writer {
public: public:
explicit AuditLogWriterSerial(AuditLog *audit) explicit Serial(audit_log::AuditLog *audit)
: AuditLogWriter(audit) { } : audit_log::Writer(audit) { }
~AuditLogWriterSerial() override; ~Serial() override;
void refCountIncrease() override { void refCountIncrease() override {
m_refereceCount++; m_refereceCount++;
@ -65,6 +67,8 @@ class AuditLogWriterSerial : public AuditLogWriter {
void generateBoundary(std::string *boundary); void generateBoundary(std::string *boundary);
}; };
} // namespace writer
} // namespace audit_log
} // namespace modsecurity } // namespace modsecurity
#endif #endif

View File

@ -16,9 +16,9 @@
#include "parser/driver.h" #include "parser/driver.h"
#include "parser/seclang-parser.hh" #include "parser/seclang-parser.hh"
#include "src/audit_log.h" #include "audit_log/audit_log.h"
using modsecurity::AuditLog; using modsecurity::audit_log::AuditLog;
using modsecurity::Rule; using modsecurity::Rule;
namespace modsecurity { namespace modsecurity {
@ -27,7 +27,7 @@ namespace Parser {
Driver::Driver() Driver::Driver()
: trace_scanning(false), : trace_scanning(false),
trace_parsing(false) { trace_parsing(false) {
audit_log = new AuditLog(); audit_log = new audit_log::AuditLog();
audit_log->refCountIncrease(); audit_log->refCountIncrease();
} }

View File

@ -27,7 +27,7 @@
#include "modsecurity/modsecurity.h" #include "modsecurity/modsecurity.h"
#include "modsecurity/rules.h" #include "modsecurity/rules.h"
#include "modsecurity/rules_properties.h" #include "modsecurity/rules_properties.h"
#include "src/audit_log.h" #include "audit_log/audit_log.h"
#include "parser/seclang-parser.hh" #include "parser/seclang-parser.hh"

View File

@ -37,7 +37,7 @@ class Driver;
#include "operators/operator.h" #include "operators/operator.h"
#include "modsecurity/rule.h" #include "modsecurity/rule.h"
#include "utils/geo_lookup.h" #include "utils/geo_lookup.h"
#include "audit_log.h" #include "audit_log/audit_log.h"
#include "utils.h" #include "utils.h"
#include "variables/variations/count.h" #include "variables/variations/count.h"
@ -295,15 +295,15 @@ audit_log:
/* SecAuditEngine */ /* SecAuditEngine */
| CONFIG_DIR_AUDIT_ENG CONFIG_VALUE_RELEVANT_ONLY | CONFIG_DIR_AUDIT_ENG CONFIG_VALUE_RELEVANT_ONLY
{ {
driver.audit_log->setStatus(modsecurity::AuditLog::RelevantOnlyAuditLogStatus); driver.audit_log->setStatus(modsecurity::audit_log::AuditLog::RelevantOnlyAuditLogStatus);
} }
| CONFIG_DIR_AUDIT_ENG CONFIG_VALUE_OFF | CONFIG_DIR_AUDIT_ENG CONFIG_VALUE_OFF
{ {
driver.audit_log->setStatus(modsecurity::AuditLog::OffAuditLogStatus); driver.audit_log->setStatus(modsecurity::audit_log::AuditLog::OffAuditLogStatus);
} }
| CONFIG_DIR_AUDIT_ENG CONFIG_VALUE_ON | CONFIG_DIR_AUDIT_ENG CONFIG_VALUE_ON
{ {
driver.audit_log->setStatus(modsecurity::AuditLog::OnAuditLogStatus); driver.audit_log->setStatus(modsecurity::audit_log::AuditLog::OnAuditLogStatus);
} }
/* SecAuditLogFileMode */ /* SecAuditLogFileMode */
@ -342,11 +342,11 @@ audit_log:
/* SecAuditLogType */ /* SecAuditLogType */
| CONFIG_DIR_AUDIT_TPE CONFIG_VALUE_SERIAL | CONFIG_DIR_AUDIT_TPE CONFIG_VALUE_SERIAL
{ {
driver.audit_log->setType(modsecurity::AuditLog::SerialAuditLogType); driver.audit_log->setType(modsecurity::audit_log::AuditLog::SerialAuditLogType);
} }
| CONFIG_DIR_AUDIT_TPE CONFIG_VALUE_PARALLEL | CONFIG_DIR_AUDIT_TPE CONFIG_VALUE_PARALLEL
{ {
driver.audit_log->setType(modsecurity::AuditLog::ParallelAuditLogType); driver.audit_log->setType(modsecurity::audit_log::AuditLog::ParallelAuditLogType);
} }
; ;

View File

@ -37,7 +37,7 @@
#include "modsecurity/intervention.h" #include "modsecurity/intervention.h"
#include "modsecurity/modsecurity.h" #include "modsecurity/modsecurity.h"
#include "request_body_processor/multipart.h" #include "request_body_processor/multipart.h"
#include "src/audit_log.h" #include "audit_log/audit_log.h"
#include "src/unique_id.h" #include "src/unique_id.h"
#include "src/utils.h" #include "src/utils.h"
@ -1303,7 +1303,7 @@ std::string Transaction::toOldAuditLogFormat(int parts,
audit_log << " " << this->m_serverPort; audit_log << " " << this->m_serverPort;
audit_log << std::endl; audit_log << std::endl;
if (parts & AuditLog::BAuditLogPart) { if (parts & audit_log::AuditLog::BAuditLogPart) {
audit_log << "--" << trailer << "-" << "B--" << std::endl; audit_log << "--" << trailer << "-" << "B--" << std::endl;
audit_log << this->m_method << " " << this->m_uri << " " << "HTTP/"; audit_log << this->m_method << " " << this->m_uri << " " << "HTTP/";
audit_log << this->m_httpVersion << std::endl; audit_log << this->m_httpVersion << std::endl;
@ -1321,19 +1321,19 @@ std::string Transaction::toOldAuditLogFormat(int parts,
} }
} }
} }
if (parts & AuditLog::CAuditLogPart) { if (parts & audit_log::AuditLog::CAuditLogPart) {
audit_log << "--" << trailer << "-" << "C--" << std::endl; audit_log << "--" << trailer << "-" << "C--" << std::endl;
/** TODO: write audit_log C part. */ /** TODO: write audit_log C part. */
} }
if (parts & AuditLog::DAuditLogPart) { if (parts & audit_log::AuditLog::DAuditLogPart) {
audit_log << "--" << trailer << "-" << "D--" << std::endl; audit_log << "--" << trailer << "-" << "D--" << std::endl;
/** TODO: write audit_log D part. */ /** TODO: write audit_log D part. */
} }
if (parts & AuditLog::EAuditLogPart) { if (parts & audit_log::AuditLog::EAuditLogPart) {
audit_log << "--" << trailer << "-" << "E--" << std::endl; audit_log << "--" << trailer << "-" << "E--" << std::endl;
/** TODO: write audit_log E part. */ /** TODO: write audit_log E part. */
} }
if (parts & AuditLog::FAuditLogPart) { if (parts & audit_log::AuditLog::FAuditLogPart) {
audit_log << "--" << trailer << "-" << "F--" << std::endl; audit_log << "--" << trailer << "-" << "F--" << std::endl;
for (auto h : m_collections.m_transient) { for (auto h : m_collections.m_transient) {
std::string filter = "RESPONSE_HEADERS:"; std::string filter = "RESPONSE_HEADERS:";
@ -1348,23 +1348,23 @@ std::string Transaction::toOldAuditLogFormat(int parts,
} }
} }
} }
if (parts & AuditLog::GAuditLogPart) { if (parts & audit_log::AuditLog::GAuditLogPart) {
audit_log << "--" << trailer << "-" << "G--" << std::endl; audit_log << "--" << trailer << "-" << "G--" << std::endl;
/** TODO: write audit_log G part. */ /** TODO: write audit_log G part. */
} }
if (parts & AuditLog::HAuditLogPart) { if (parts & audit_log::AuditLog::HAuditLogPart) {
audit_log << "--" << trailer << "-" << "H--" << std::endl; audit_log << "--" << trailer << "-" << "H--" << std::endl;
/** TODO: write audit_log H part. */ /** TODO: write audit_log H part. */
} }
if (parts & AuditLog::IAuditLogPart) { if (parts & audit_log::AuditLog::IAuditLogPart) {
audit_log << "--" << trailer << "-" << "I--" << std::endl; audit_log << "--" << trailer << "-" << "I--" << std::endl;
/** TODO: write audit_log I part. */ /** TODO: write audit_log I part. */
} }
if (parts & AuditLog::JAuditLogPart) { if (parts & audit_log::AuditLog::JAuditLogPart) {
audit_log << "--" << trailer << "-" << "J--" << std::endl; audit_log << "--" << trailer << "-" << "J--" << std::endl;
/** TODO: write audit_log J part. */ /** TODO: write audit_log J part. */
} }
if (parts & AuditLog::KAuditLogPart) { if (parts & audit_log::AuditLog::KAuditLogPart) {
audit_log << "--" << trailer << "-" << "K--" << std::endl; audit_log << "--" << trailer << "-" << "K--" << std::endl;
/** TODO: write audit_log K part. */ /** TODO: write audit_log K part. */
} }
@ -1415,12 +1415,12 @@ std::string Transaction::toJSON(int parts) {
LOGFY_ADD_INT("http_version", m_httpVersion); LOGFY_ADD_INT("http_version", m_httpVersion);
LOGFY_ADD("uri", this->m_uri); LOGFY_ADD("uri", this->m_uri);
if (parts & AuditLog::CAuditLogPart) { if (parts & audit_log::AuditLog::CAuditLogPart) {
LOGFY_ADD("body", this->m_requestBody.str().c_str()); LOGFY_ADD("body", this->m_requestBody.str().c_str());
} }
/* request headers */ /* request headers */
if (parts & AuditLog::BAuditLogPart) { if (parts & audit_log::AuditLog::BAuditLogPart) {
yajl_gen_string(g, reinterpret_cast<const unsigned char*>("headers"), yajl_gen_string(g, reinterpret_cast<const unsigned char*>("headers"),
strlen("headers")); strlen("headers"));
yajl_gen_map_open(g); yajl_gen_map_open(g);
@ -1449,13 +1449,13 @@ std::string Transaction::toJSON(int parts) {
strlen("response")); strlen("response"));
yajl_gen_map_open(g); yajl_gen_map_open(g);
if (parts & AuditLog::GAuditLogPart) { if (parts & audit_log::AuditLog::GAuditLogPart) {
LOGFY_ADD("body", this->m_responseBody.str().c_str()); LOGFY_ADD("body", this->m_responseBody.str().c_str());
} }
LOGFY_ADD_NUM("http_code", m_httpCodeReturned); LOGFY_ADD_NUM("http_code", m_httpCodeReturned);
/* response headers */ /* response headers */
if (parts & AuditLog::FAuditLogPart) { if (parts & audit_log::AuditLog::FAuditLogPart) {
yajl_gen_string(g, reinterpret_cast<const unsigned char*>("headers"), yajl_gen_string(g, reinterpret_cast<const unsigned char*>("headers"),
strlen("headers")); strlen("headers"));
yajl_gen_map_open(g); yajl_gen_map_open(g);
@ -1478,7 +1478,7 @@ std::string Transaction::toJSON(int parts) {
yajl_gen_map_close(g); yajl_gen_map_close(g);
/* producer */ /* producer */
if (parts & AuditLog::HAuditLogPart) { if (parts & audit_log::AuditLog::HAuditLogPart) {
yajl_gen_string(g, reinterpret_cast<const unsigned char*>("producer"), yajl_gen_string(g, reinterpret_cast<const unsigned char*>("producer"),
strlen("producer")); strlen("producer"));
yajl_gen_map_open(g); yajl_gen_map_open(g);