mirror of
https://github.com/openappsec/openappsec.git
synced 2025-09-29 19:24:26 +03:00
sync code
This commit is contained in:
@@ -26,6 +26,7 @@
|
||||
#include "config.h"
|
||||
#include "i_instance_awareness.h"
|
||||
#include "i_signal_handler.h"
|
||||
#include "hash_combine.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -283,6 +284,16 @@ private:
|
||||
S2C_PARAM(string, output);
|
||||
};
|
||||
|
||||
void
|
||||
AlertInfo::evalParams()
|
||||
{
|
||||
id = 0;
|
||||
hashCombine(id, family_id);
|
||||
hashCombine(id, functionality);
|
||||
hashCombine(id, description);
|
||||
hashCombine(id, static_cast<size_t>(team));
|
||||
}
|
||||
|
||||
// LCOV_EXCL_START - function is covered in unit-test, but not detected bt gcov
|
||||
Debug::Debug(
|
||||
const string &file_name,
|
||||
@@ -830,6 +841,14 @@ Debug::isCommunicationFlag(const DebugFlags &flag)
|
||||
);
|
||||
}
|
||||
|
||||
void
|
||||
Debug::sendAlert(const AlertInfo &alert)
|
||||
{
|
||||
for (auto &added_stream : current_active_streams) {
|
||||
added_stream->sendAlert(alert);
|
||||
}
|
||||
}
|
||||
|
||||
Debug::DebugLevel Debug::lowest_global_level = default_level;
|
||||
I_TimeGet *Debug::time = nullptr;
|
||||
I_MainLoop *Debug::mainloop = nullptr;
|
||||
|
@@ -55,6 +55,7 @@ public:
|
||||
);
|
||||
|
||||
virtual void finishMessage() { *stream << std::endl; }
|
||||
virtual void sendAlert(const AlertInfo &) {}
|
||||
|
||||
std::ostream * getStream() const { return stream; }
|
||||
|
||||
@@ -112,6 +113,7 @@ public:
|
||||
) override;
|
||||
|
||||
void finishMessage() override;
|
||||
void sendAlert(const AlertInfo &_alert) override { possible_alert = _alert; }
|
||||
|
||||
private:
|
||||
void sendBufferedMessages();
|
||||
@@ -133,6 +135,7 @@ private:
|
||||
std::string trace_id;
|
||||
std::string span_id;
|
||||
uint line;
|
||||
Maybe<AlertInfo, void> possible_alert = genError("");
|
||||
};
|
||||
|
||||
#endif // __DEBUG_EX_H__
|
||||
|
@@ -39,11 +39,32 @@ void doPMExecTrace() { dbgTrace(D_PM_EXEC) << "PM_EXEC trace message"; line = to
|
||||
|
||||
template <typename ...Args> void doManyFlags(Args ...args) { dbgDebug(args...) << "stab"; line = to_string(__LINE__); }
|
||||
|
||||
TEST(DebugBaseTest, alert_obkect)
|
||||
{
|
||||
AlertInfo alert1(AlertTeam::CORE, "testing");
|
||||
EXPECT_EQ(alert1.getTeam(), AlertTeam::CORE);
|
||||
EXPECT_EQ(alert1.getFunctionality(), "testing");
|
||||
EXPECT_EQ(alert1.getDescription(), "");
|
||||
EXPECT_EQ(alert1.getFamilyId(), 0u);
|
||||
EXPECT_NE(alert1.getId(), 0u);
|
||||
|
||||
auto alert2 = alert1("additional data", 5);
|
||||
EXPECT_EQ(alert2.getTeam(), AlertTeam::CORE);
|
||||
EXPECT_EQ(alert2.getFunctionality(), "testing");
|
||||
EXPECT_EQ(alert2.getDescription(), "additional data");
|
||||
EXPECT_EQ(alert2.getFamilyId(), 5u);
|
||||
EXPECT_NE(alert2.getId(), 0u);
|
||||
EXPECT_NE(alert1.getId(), alert2.getId());
|
||||
}
|
||||
|
||||
TEST(DebugBaseTest, death_on_panic)
|
||||
{
|
||||
cptestPrepareToDie();
|
||||
|
||||
EXPECT_DEATH(dbgAssert(1==2) << "Does your school teach otherwise?", "Does your school teach otherwise?");
|
||||
EXPECT_DEATH(
|
||||
dbgAssert(1==2) << AlertInfo(AlertTeam::CORE, "testing") << "Does your school teach otherwise?",
|
||||
"Does your school teach otherwise?"
|
||||
);
|
||||
}
|
||||
|
||||
TEST(DebugBaseTest, default_levels)
|
||||
@@ -1014,7 +1035,7 @@ TEST(DebugFogTest, fog_stream)
|
||||
" \"agentId\": \"Unknown\",\n"
|
||||
" \"issuingFunction\": \"handleThresholdReach\",\n"
|
||||
" \"issuingFile\": \"debug_streams.cc\",\n"
|
||||
" \"issuingLine\": 344,\n"
|
||||
" \"issuingLine\": 364,\n"
|
||||
" \"eventTraceId\": \"\",\n"
|
||||
" \"eventSpanId\": \"\",\n"
|
||||
" \"issuingEngineVersion\": \"\",\n"
|
||||
@@ -1117,3 +1138,123 @@ TEST(DebugFogTest, fog_stream)
|
||||
EXPECT_CALL(mock_mainloop, stop(0));
|
||||
Debug::fini();
|
||||
}
|
||||
|
||||
TEST(DebugFogTest, alert_fog_stream)
|
||||
{
|
||||
ConfigComponent conf;
|
||||
::Environment env;
|
||||
env.preload();
|
||||
env.init();
|
||||
stringstream capture_debug;
|
||||
conf.preload();
|
||||
|
||||
StrictMock<MockMainLoop> mock_mainloop;
|
||||
StrictMock<MockTimeGet> mock_time;
|
||||
NiceMock<MockAgentDetails> mock_agent_details;
|
||||
|
||||
ON_CALL(mock_agent_details, getFogDomain()).WillByDefault(Return(Maybe<string>(string("fog_domain.com"))));
|
||||
ON_CALL(mock_agent_details, getFogPort()).WillByDefault(Return(Maybe<uint16_t>(443)));
|
||||
|
||||
EXPECT_CALL(mock_agent_details, getAgentId()).WillRepeatedly(Return("Unknown"));
|
||||
EXPECT_CALL(mock_agent_details, getOrchestrationMode()).WillRepeatedly(Return(OrchestrationMode::ONLINE));
|
||||
|
||||
EXPECT_CALL(mock_time, getWalltimeStr(_)).WillRepeatedly(Return(string("2016-11-13T17:31:24.087")));
|
||||
I_MainLoop::Routine send_debug_routine = nullptr;
|
||||
|
||||
EXPECT_CALL(mock_mainloop, addRecurringRoutine(_, _, _, _, _))
|
||||
.WillOnce(DoAll(SaveArg<2>(&send_debug_routine), Return(0)));
|
||||
|
||||
StrictMock<MockMessaging> messaging_mock;
|
||||
string message_body;
|
||||
|
||||
EXPECT_CALL(messaging_mock, sendAsyncMessage(
|
||||
_,
|
||||
"/api/v1/agents/events/bulk",
|
||||
_,
|
||||
_,
|
||||
_,
|
||||
_
|
||||
)).WillRepeatedly(SaveArg<2>(&message_body));
|
||||
|
||||
Singleton::Consume<Config::I_Config>::from(conf)->loadConfiguration(
|
||||
vector<string>{"--orchestration-mode=online_mode"}
|
||||
);
|
||||
Debug::preload();
|
||||
string config_json =
|
||||
"{"
|
||||
" \"Debug I/S\": {"
|
||||
" \"Sent debug bulk size\": ["
|
||||
" {"
|
||||
" \"value\": 2"
|
||||
" }"
|
||||
" ]"
|
||||
" },"
|
||||
" \"Debug\": [{"
|
||||
" \"Streams\": ["
|
||||
" {"
|
||||
" \"Output\": \"FOG\""
|
||||
" },"
|
||||
" {"
|
||||
" \"Output\": \"STDOUT\""
|
||||
" }"
|
||||
" ]"
|
||||
" }]"
|
||||
"}";
|
||||
|
||||
istringstream ss(config_json);
|
||||
Singleton::Consume<Config::I_Config>::from(conf)->loadConfiguration(ss);
|
||||
|
||||
Debug::DebugAlert("MockFile", "MockFunction", 0, Debug::DebugLevel::ERROR, D_FW).getStreamAggr()
|
||||
<< AlertInfo(AlertTeam::CORE, "testing")
|
||||
<< "Generic error message";
|
||||
|
||||
string expected_message =
|
||||
"{\n"
|
||||
" \"logs\": [\n"
|
||||
" {\n"
|
||||
" \"id\": 1,\n"
|
||||
" \"log\": {\n"
|
||||
" \"eventTime\": \"2016-11-13T17:31:24.087\",\n"
|
||||
" \"eventName\": \"Debug message\",\n"
|
||||
" \"eventSeverity\": \"High\",\n"
|
||||
" \"eventPriority\": \"Low\",\n"
|
||||
" \"eventType\": \"Code Related\",\n"
|
||||
" \"eventLevel\": \"Log\",\n"
|
||||
" \"eventLogLevel\": \"error\",\n"
|
||||
" \"eventAudience\": \"Internal\",\n"
|
||||
" \"eventAudienceTeam\": \"\",\n"
|
||||
" \"eventFrequency\": 0,\n"
|
||||
" \"eventTags\": [\n"
|
||||
" \"Informational\"\n"
|
||||
" ],\n"
|
||||
" \"eventSource\": {\n"
|
||||
" \"agentId\": \"Unknown\",\n"
|
||||
" \"issuingFunction\": \"MockFunction\",\n"
|
||||
" \"issuingFile\": \"MockFile\",\n"
|
||||
" \"issuingLine\": 0,\n"
|
||||
" \"eventTraceId\": \"\",\n"
|
||||
" \"eventSpanId\": \"\",\n"
|
||||
" \"issuingEngineVersion\": \"\",\n"
|
||||
" \"serviceName\": \"Unnamed Nano Service\"\n"
|
||||
" },\n"
|
||||
" \"eventData\": {\n"
|
||||
" \"eventMessage\": \"Generic error message\",\n"
|
||||
" \"eventId\": 6255310698607853351,\n"
|
||||
" \"eventFamilyId\": 0,\n"
|
||||
" \"eventFunctionality\": \"testing\",\n"
|
||||
" \"eventDescription\": \"\",\n"
|
||||
" \"eventResponseTeam\": \"Core\"\n"
|
||||
" }\n"
|
||||
" }\n"
|
||||
" }\n"
|
||||
" ]\n"
|
||||
"}";
|
||||
|
||||
send_debug_routine();
|
||||
|
||||
EXPECT_EQ(message_body, expected_message);
|
||||
|
||||
EXPECT_CALL(mock_mainloop, doesRoutineExist(0)).WillOnce(Return(true));
|
||||
EXPECT_CALL(mock_mainloop, stop(0));
|
||||
Debug::fini();
|
||||
}
|
||||
|
@@ -230,6 +230,18 @@ DebugFogStream::printHeader(
|
||||
}
|
||||
}
|
||||
|
||||
static string
|
||||
getTeam(const Maybe<AlertInfo, void> &alert)
|
||||
{
|
||||
switch((*alert).getTeam()) {
|
||||
case AlertTeam::CORE: return "Core";
|
||||
case AlertTeam::WAAP: return "Waap";
|
||||
case AlertTeam::SDWAN: return "SDWAN";
|
||||
case AlertTeam::IOT: return "IoT";
|
||||
}
|
||||
return "Core";
|
||||
}
|
||||
|
||||
void
|
||||
DebugFogStream::finishMessage()
|
||||
{
|
||||
@@ -265,6 +277,14 @@ DebugFogStream::finishMessage()
|
||||
);
|
||||
message_to_fog << LogField("eventMessage", message.str());
|
||||
|
||||
if (possible_alert.ok()) {
|
||||
message_to_fog << LogField("eventId", (*possible_alert).getId());
|
||||
message_to_fog << LogField("eventFamilyId", (*possible_alert).getFamilyId());
|
||||
message_to_fog << LogField("eventFunctionality", (*possible_alert).getFunctionality());
|
||||
message_to_fog << LogField("eventDescription", (*possible_alert).getDescription());
|
||||
message_to_fog << LogField("eventResponseTeam", getTeam(possible_alert));
|
||||
}
|
||||
|
||||
if (!getConfigurationWithDefault<bool>(true, "Debug I/S", "Enable bulk of debugs")) {
|
||||
LogRest rest(move(message_to_fog));
|
||||
Singleton::Consume<I_MainLoop>::by<Debug>()->addOneTimeRoutine(
|
||||
@@ -370,18 +390,20 @@ DebugFogStream::getSeverity() const
|
||||
return Severity::CRITICAL;
|
||||
}
|
||||
|
||||
static const AlertInfo alert(AlertTeam::CORE, "debug configuration");
|
||||
|
||||
LogLevel
|
||||
DebugFogStream::getLogLevel() const
|
||||
{
|
||||
switch (level) {
|
||||
case Debug::DebugLevel::NOISE: dbgAssert(false) << "Impossible LogLevel 'Noise'"; break;
|
||||
case Debug::DebugLevel::NOISE: dbgAssert(false) << alert << "Impossible LogLevel 'Noise'"; break;
|
||||
case Debug::DebugLevel::TRACE: return LogLevel::TRACE;
|
||||
case Debug::DebugLevel::DEBUG: return LogLevel::DEBUG;
|
||||
case Debug::DebugLevel::WARNING: return LogLevel::WARNING;
|
||||
case Debug::DebugLevel::INFO: return LogLevel::INFO;
|
||||
case Debug::DebugLevel::ERROR: return LogLevel::ERROR;
|
||||
case Debug::DebugLevel::ASSERTION: return LogLevel::ERROR;
|
||||
case Debug::DebugLevel::NONE: dbgAssert(false) << "Impossible LogLevel 'None'"; break;
|
||||
case Debug::DebugLevel::NONE: dbgAssert(false) << alert << "Impossible LogLevel 'None'"; break;
|
||||
}
|
||||
|
||||
return LogLevel::INFO;
|
||||
|
Reference in New Issue
Block a user