Refactoring on Transaction class: adding comments and renaming variables

This commit is contained in:
Felipe Zimmerle 2016-01-14 11:58:40 -03:00
parent 6f1e6f37d7
commit 4db5cc7d26
19 changed files with 48 additions and 47 deletions

View File

@ -24,7 +24,7 @@ namespace modsecurity {
namespace actions { namespace actions {
bool AuditLog::evaluate(Rule *rule, Transaction *transaction) { bool AuditLog::evaluate(Rule *rule, Transaction *transaction) {
transaction->save_in_auditlog = true; transaction->m_toBeSavedInAuditlogs = true;
return true; return true;
} }

View File

@ -38,7 +38,7 @@ bool Block::evaluate(Rule *rule, Transaction *transaction) {
#endif #endif
for (Action *a : rule->actions_runtime_pos) { for (Action *a : rule->actions_runtime_pos) {
if (a->isDisruptive() == true) { if (a->isDisruptive() == true) {
transaction->actions.push_back(a); transaction->m_actions.push_back(a);
} }
} }
return true; return true;

View File

@ -36,7 +36,7 @@ CtlAuditLogParts::CtlAuditLogParts(std::string action)
} }
bool CtlAuditLogParts::evaluate(Rule *rule, Transaction *transaction) { bool CtlAuditLogParts::evaluate(Rule *rule, Transaction *transaction) {
transaction->auditLogModifier.push_back( transaction->m_auditLogModifier.push_back(
std::make_pair(mPartsAction, mParts)); std::make_pair(mPartsAction, mParts));
return true; return true;
} }

View File

@ -34,7 +34,7 @@ bool Deny::evaluate(Rule *rule, Transaction *transaction) {
#ifndef NO_LOGS #ifndef NO_LOGS
transaction->debug(8, "Running action deny"); transaction->debug(8, "Running action deny");
#endif #endif
transaction->actions.push_back(this); transaction->m_actions.push_back(this);
return true; return true;
} }

View File

@ -24,7 +24,7 @@ namespace modsecurity {
namespace actions { namespace actions {
bool Log::evaluate(Rule *rule, Transaction *transaction) { bool Log::evaluate(Rule *rule, Transaction *transaction) {
transaction->save_in_auditlog = true; transaction->m_toBeSavedInAuditlogs = true;
/* FIXME: transaction->serverLog("Something...."); */ /* FIXME: transaction->serverLog("Something...."); */
transaction->debug(9, "Saving transaction to logs"); transaction->debug(9, "Saving transaction to logs");
return true; return true;

View File

@ -39,7 +39,7 @@ bool LogData::evaluate(Rule *rule, Transaction *transaction) {
#ifndef NO_LOGS #ifndef NO_LOGS
transaction->debug(9, "Saving msg: " + msg); transaction->debug(9, "Saving msg: " + msg);
#endif #endif
transaction->rulesMessages.push_back(msg); transaction->m_rulesMessages.push_back(msg);
transaction->serverLog(msg); transaction->serverLog(msg);
return true; return true;
} }

View File

@ -39,7 +39,7 @@ bool Msg::evaluate(Rule *rule, Transaction *transaction) {
#ifndef NO_LOGS #ifndef NO_LOGS
transaction->debug(9, "Saving msg: " + msg); transaction->debug(9, "Saving msg: " + msg);
#endif #endif
transaction->rulesMessages.push_back(msg); transaction->m_rulesMessages.push_back(msg);
transaction->serverLog(msg); transaction->serverLog(msg);
return true; return true;
} }

View File

@ -24,7 +24,7 @@ namespace modsecurity {
namespace actions { namespace actions {
bool NoAuditLog::evaluate(Rule *rule, Transaction *transaction) { bool NoAuditLog::evaluate(Rule *rule, Transaction *transaction) {
transaction->do_not_save_in_auditlog = true; transaction->m_toNotBeSavedInAuditLogs = true;
return true; return true;
} }

View File

@ -32,7 +32,7 @@ Pass::Pass(std::string action)
bool Pass::evaluate(Rule *rule, Transaction *transaction) { bool Pass::evaluate(Rule *rule, Transaction *transaction) {
transaction->actions.clear(); transaction->m_actions.clear();
return true; return true;
} }

View File

@ -43,7 +43,7 @@ Redirect::Redirect(const std::string& action)
bool Redirect::evaluate(Rule *rule, Transaction *transaction) { bool Redirect::evaluate(Rule *rule, Transaction *transaction) {
m_urlExpanded = MacroExpansion::expand(m_url, transaction); m_urlExpanded = MacroExpansion::expand(m_url, transaction);
transaction->actions.push_back(this); transaction->m_actions.push_back(this);
return true; return true;
} }

View File

@ -54,11 +54,11 @@ bool Severity::evaluate(Rule *rule, Transaction *transaction) {
#ifndef NO_LOGS #ifndef NO_LOGS
transaction->debug(9, "This rule severity is: " + \ transaction->debug(9, "This rule severity is: " + \
std::to_string(this->m_severity) + " current transaction is: " + \ std::to_string(this->m_severity) + " current transaction is: " + \
std::to_string(transaction->highest_severity)); std::to_string(transaction->m_highestSeverityAction));
#endif #endif
if (transaction->highest_severity > this->m_severity) { if (transaction->m_highestSeverityAction > this->m_severity) {
transaction->highest_severity = this->m_severity; transaction->m_highestSeverityAction = this->m_severity;
} }
return true; return true;
} }

View File

@ -34,7 +34,7 @@ Status::Status(std::string action)
bool Status::evaluate(Rule *rule, Transaction *transaction) { bool Status::evaluate(Rule *rule, Transaction *transaction) {
transaction->actions.push_back(this); transaction->m_actions.push_back(this);
return true; return true;
} }

View File

@ -39,7 +39,7 @@ bool Tag::evaluate(Rule *rule, Transaction *transaction) {
#ifndef NO_LOGS #ifndef NO_LOGS
transaction->debug(9, "Rule tag: " + tag); transaction->debug(9, "Rule tag: " + tag);
#endif #endif
transaction->ruleTags.push_back(tag); transaction->m_ruleTags.push_back(tag);
return true; return true;
} }

View File

@ -225,8 +225,8 @@ bool AuditLog::saveIfRelevant(Transaction *transaction) {
bool AuditLog::saveIfRelevant(Transaction *transaction, int parts) { bool AuditLog::saveIfRelevant(Transaction *transaction, int parts) {
if (this->isRelevant(transaction->httpCodeReturned) == false && if (this->isRelevant(transaction->m_httpCodeReturned) == false &&
transaction->save_in_auditlog == false) { transaction->m_toBeSavedInAuditlogs == false) {
return false; return false;
} }
@ -235,7 +235,7 @@ bool AuditLog::saveIfRelevant(Transaction *transaction, int parts) {
* we won't save it. * we won't save it.
* *
*/ */
if (transaction->do_not_save_in_auditlog == true) { if (transaction->m_toNotBeSavedInAuditLogs == true) {
return false; return false;
} }

View File

@ -34,7 +34,7 @@ std::string AuditLogWriter::file_name(const std::string& unique_id) {
* *
*/ */
bool AuditLogWriter::write(Transaction *transaction, int parts) { bool AuditLogWriter::write(Transaction *transaction, int parts) {
std::cout << transaction->to_json(0) << std::endl; std::cout << transaction->toJSON(0) << std::endl;
return true; return true;
} }

View File

@ -92,23 +92,23 @@ bool AuditLogWriterParallel::init() {
bool AuditLogWriterParallel::write(Transaction *transaction, int parts) { bool AuditLogWriterParallel::write(Transaction *transaction, int parts) {
FILE *fp; FILE *fp;
int fd; int fd;
std::string log = transaction->to_json(parts); std::string log = transaction->toJSON(parts);
std::string fileName = logFilePath(&transaction->timeStamp, std::string fileName = logFilePath(&transaction->m_timeStamp,
YearMonthDayDirectory | YearMonthDayAndTimeDirectory YearMonthDayDirectory | YearMonthDayAndTimeDirectory
| YearMonthDayAndTimeFileName); | YearMonthDayAndTimeFileName);
std::string logPath = m_audit->m_storage_dir; std::string logPath = m_audit->m_storage_dir;
fileName = logPath + fileName + "-" + transaction->id; fileName = logPath + fileName + "-" + transaction->m_id;
if (logPath.empty()) { if (logPath.empty()) {
return false; return false;
} }
createDir((logPath + createDir((logPath +
logFilePath(&transaction->timeStamp, YearMonthDayDirectory)).c_str(), logFilePath(&transaction->m_timeStamp, YearMonthDayDirectory)).c_str(),
m_audit->directoryPermission); m_audit->directoryPermission);
createDir((logPath + createDir((logPath +
logFilePath(&transaction->timeStamp, YearMonthDayDirectory logFilePath(&transaction->m_timeStamp, YearMonthDayDirectory
| YearMonthDayAndTimeDirectory)).c_str(), | YearMonthDayAndTimeDirectory)).c_str(),
m_audit->directoryPermission); m_audit->directoryPermission);

View File

@ -19,26 +19,27 @@
#include <yajl/yajl_tree.h> #include <yajl/yajl_tree.h>
#include <yajl/yajl_gen.h> #include <yajl/yajl_gen.h>
#endif #endif
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <ctime>
#include <iostream>
#include <unordered_map>
#include <fstream>
#include <vector>
#include <iomanip>
#include <set>
#include <cstdio> #include <cstdio>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <set>
#include <unordered_map>
#include <vector>
#include "modsecurity/modsecurity.h"
#include "modsecurity/intervention.h"
#include "actions/action.h" #include "actions/action.h"
#include "actions/deny.h" #include "actions/deny.h"
#include "src/utils.h" #include "modsecurity/intervention.h"
#include "modsecurity/modsecurity.h"
#include "request_body_processor/multipart.h"
#include "src/audit_log.h" #include "src/audit_log.h"
#include "src/unique_id.h" #include "src/unique_id.h"
#include "request_body_processor/multipart.h" #include "src/utils.h"
using modsecurity::actions::Action; using modsecurity::actions::Action;
using modsecurity::RequestBodyProcessor::Multipart; using modsecurity::RequestBodyProcessor::Multipart;
@ -89,7 +90,7 @@ Transaction::Transaction(ModSecurity *ms, Rules *rules, void *logCbData)
m_clientPort(0), m_clientPort(0),
m_serverPort(0), m_serverPort(0),
m_uri(""), m_uri(""),
m_protocol(""), m_method(""),
m_httpVersion(""), m_httpVersion(""),
m_rules(rules), m_rules(rules),
m_toBeSavedInAuditlogs(false), m_toBeSavedInAuditlogs(false),
@ -107,7 +108,7 @@ Transaction::Transaction(ModSecurity *ms, Rules *rules, void *logCbData)
m_responseHeadersNames(NULL), m_responseHeadersNames(NULL),
m_responseContentType(NULL), m_responseContentType(NULL),
m_marker(""), m_marker(""),
start(cpu_seconds()), m_creationTimeStamp(cpu_seconds()),
m_logCbData(logCbData), m_logCbData(logCbData),
m_ms(ms) { m_ms(ms) {
m_id = std::to_string(this->m_timeStamp) + \ m_id = std::to_string(this->m_timeStamp) + \
@ -235,7 +236,7 @@ int Transaction::processConnection(const char *client, int cPort,
* *
* @param transaction ModSecurity transaction. * @param transaction ModSecurity transaction.
* @param uri Uri. * @param uri Uri.
* @param protocol Protocol (GET, POST, PUT). * @param method Method (GET, POST, PUT).
* @param http_version Http version (1.0, 1.2, 2.0). * @param http_version Http version (1.0, 1.2, 2.0).
* *
* @returns If the operation was successful or not. * @returns If the operation was successful or not.
@ -243,14 +244,14 @@ int Transaction::processConnection(const char *client, int cPort,
* @retval false Operation failed. * @retval false Operation failed.
* *
*/ */
int Transaction::processURI(const char *uri, const char *protocol, int Transaction::processURI(const char *uri, const char *method,
const char *http_version) { const char *http_version) {
#ifndef NO_LOGS #ifndef NO_LOGS
debug(4, "Starting phase URI. (SecRules 0 + 1/2)"); debug(4, "Starting phase URI. (SecRules 0 + 1/2)");
#endif #endif
m_protocol = protocol; m_method = method;
m_httpVersion = http_version; m_httpVersion = http_version;
m_uri = uri; m_uri = uri;
std::string uri_s(uri); std::string uri_s(uri);
@ -259,7 +260,7 @@ int Transaction::processURI(const char *uri, const char *protocol,
size_t pos = m_uri_decoded.find("?"); size_t pos = m_uri_decoded.find("?");
size_t pos_raw = uri_s.find("?"); size_t pos_raw = uri_s.find("?");
m_collections.store("REQUEST_LINE", std::string(protocol) + " " + m_collections.store("REQUEST_LINE", std::string(method) + " " +
std::string(uri) + " HTTP/" + std::string(http_version)); std::string(uri) + " HTTP/" + std::string(http_version));
if (pos_raw != std::string::npos) { if (pos_raw != std::string::npos) {
@ -282,7 +283,7 @@ int Transaction::processURI(const char *uri, const char *protocol,
path_info.length() - offset); path_info.length() - offset);
m_collections.store("REQUEST_BASENAME", basename); m_collections.store("REQUEST_BASENAME", basename);
} }
m_collections.store("REQUEST_METHOD", protocol); m_collections.store("REQUEST_METHOD", method);
m_collections.store("REQUEST_PROTOCOL", m_collections.store("REQUEST_PROTOCOL",
"HTTP/" + std::string(http_version)); "HTTP/" + std::string(http_version));
@ -1257,7 +1258,7 @@ std::string Transaction::toOldAuditLogFormatIndex(const std::string &filename,
ss << tstr << " "; ss << tstr << " ";
ss << "\""; ss << "\"";
ss << this->m_protocol << " "; ss << this->m_method << " ";
ss << this->m_uri << " "; ss << this->m_uri << " ";
ss << "HTTP/" << m_httpVersion; ss << "HTTP/" << m_httpVersion;
ss << "\" "; ss << "\" ";
@ -1304,7 +1305,7 @@ std::string Transaction::toOldAuditLogFormat(int parts,
if (parts & AuditLog::BAuditLogPart) { if (parts & AuditLog::BAuditLogPart) {
audit_log << "--" << trailer << "-" << "B--" << std::endl; audit_log << "--" << trailer << "-" << "B--" << std::endl;
audit_log << this->m_protocol << " " << 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;
for (auto h : m_collections.m_transient) { for (auto h : m_collections.m_transient) {
@ -1410,7 +1411,7 @@ std::string Transaction::toJSON(int parts) {
strlen("request")); strlen("request"));
yajl_gen_map_open(g); yajl_gen_map_open(g);
LOGFY_ADD("protocol", m_protocol); LOGFY_ADD("protocol", m_method);
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);

View File

@ -31,7 +31,7 @@ void Duration::evaluateInternal(Transaction *transaction,
std::vector<const transaction::Variable *> *l) { std::vector<const transaction::Variable *> *l) {
std::string res; std::string res;
double e = cpu_seconds() - transaction->start; double e = cpu_seconds() - transaction->m_creationTimeStamp;
res = std::to_string(e); res = std::to_string(e);

View File

@ -29,7 +29,7 @@ namespace Variables {
void HighestSeverity::evaluateInternal(Transaction *transaction, void HighestSeverity::evaluateInternal(Transaction *transaction,
std::vector<const transaction::Variable *> *l) { std::vector<const transaction::Variable *> *l) {
l->push_back(new transaction::Variable("HIGHEST_SEVERITY", l->push_back(new transaction::Variable("HIGHEST_SEVERITY",
std::to_string(transaction->highest_severity))); std::to_string(transaction->m_highestSeverityAction)));
} }