mirror of
https://github.com/openappsec/openappsec.git
synced 2025-09-29 11:16:30 +03:00
Jul 31st update
This commit is contained in:
@@ -15,5 +15,6 @@ add_subdirectory(health_check)
|
||||
add_subdirectory(health_check_manager)
|
||||
add_subdirectory(updates_process_reporter)
|
||||
add_subdirectory(env_details)
|
||||
add_subdirectory(external_sdk_server)
|
||||
|
||||
#add_subdirectory(orchestration_ut)
|
||||
|
@@ -142,7 +142,7 @@ DetailsResolver::Impl::isCloudStorageEnabled()
|
||||
{
|
||||
auto cloud_storage_mode_override = getProfileAgentSetting<bool>("agent.cloudStorage.enabled");
|
||||
if (cloud_storage_mode_override.ok()) {
|
||||
dbgInfo(D_ORCHESTRATOR) << "Received cloud-storage mode override: " << *cloud_storage_mode_override;
|
||||
dbgDebug(D_ORCHESTRATOR) << "Received cloud-storage mode override: " << *cloud_storage_mode_override;
|
||||
return *cloud_storage_mode_override;
|
||||
}
|
||||
|
||||
|
@@ -142,7 +142,7 @@ DetailsResolvingHanlder::Impl::getResolvedDetails() const
|
||||
shared_ptr<ifstream> in_file =
|
||||
Singleton::Consume<I_OrchestrationTools>::by<DetailsResolvingHanlder>()->fileStreamWrapper(path);
|
||||
if (!in_file->is_open()) {
|
||||
dbgWarning(D_AGENT_DETAILS) << "Could not open file for processing. Path: " << path;
|
||||
dbgDebug(D_AGENT_DETAILS) << "Could not open file for processing. Path: " << path;
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,4 @@
|
||||
include_directories(${PROJECT_SOURCE_DIR}/core/external_sdk/)
|
||||
|
||||
add_library(external_sdk_server external_sdk_server.cc)
|
||||
add_subdirectory(external_sdk_server_ut)
|
@@ -0,0 +1,348 @@
|
||||
#include "external_sdk_server.h"
|
||||
|
||||
#include "external_agent_sdk.h"
|
||||
#include "log_generator.h"
|
||||
#include "rest_server.h"
|
||||
#include "generic_metric.h"
|
||||
#include "customized_cereal_map.h"
|
||||
#include "report/log_rest.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
USE_DEBUG_FLAG(D_EXTERNAL_SDK_USER);
|
||||
USE_DEBUG_FLAG(D_EXTERNAL_SDK_SERVER);
|
||||
|
||||
class ExternalSdkRest : public ServerRest
|
||||
{
|
||||
public:
|
||||
void
|
||||
doCall() override
|
||||
{
|
||||
dbgFlow(D_EXTERNAL_SDK_SERVER);
|
||||
Maybe<SdkApiType> sdk_event_type = convertToEnum<SdkApiType>(event_type.get());
|
||||
if (!sdk_event_type.ok()) {
|
||||
dbgWarning(D_EXTERNAL_SDK_SERVER) << "Received illegal event type. Type : " << event_type.get();
|
||||
throw JsonError("Illegal event type provided");
|
||||
}
|
||||
dbgDebug(D_EXTERNAL_SDK_SERVER)
|
||||
<< "Handling a new external sdk api call event. Type : "
|
||||
<< convertApiTypeToString(sdk_event_type.unpack());
|
||||
|
||||
I_ExternalSdkServer *sdk_server = Singleton::Consume<I_ExternalSdkServer>::from<ExternalSdkServer>();
|
||||
switch(sdk_event_type.unpack()) {
|
||||
case SdkApiType::SendCodeEvent: {
|
||||
if (!file.isActive()) {
|
||||
throw JsonError("File was not provided for code event");
|
||||
}
|
||||
if (!func.isActive()) {
|
||||
throw JsonError("Function was not provided for code event");
|
||||
}
|
||||
if (!line.isActive()) {
|
||||
throw JsonError("Line path was not provided for code event");
|
||||
}
|
||||
if (!trace_id.isActive()) {
|
||||
throw JsonError("Trace ID was not provided for code event");
|
||||
}
|
||||
if (!span_id.isActive()) {
|
||||
throw JsonError("Span ID was not provided for code event");
|
||||
}
|
||||
if (!message.isActive()) {
|
||||
throw JsonError("Message was not provided for code event");
|
||||
}
|
||||
sdk_server->sendDebug(
|
||||
file.get(),
|
||||
func.get(),
|
||||
line.get(),
|
||||
getDebugLevel(),
|
||||
trace_id.get(),
|
||||
span_id.get(),
|
||||
message.get(),
|
||||
additional_fields.isActive() ? additional_fields.get() : map<string, string>()
|
||||
);
|
||||
return;
|
||||
}
|
||||
case SdkApiType::SendEventDrivenEvent: {
|
||||
if (!event_name.isActive()) {
|
||||
throw JsonError("Event name was not provided for event");
|
||||
}
|
||||
sdk_server->sendLog(
|
||||
event_name.get(),
|
||||
getAudience(),
|
||||
getSeverity(),
|
||||
getPriority(),
|
||||
tag.get(),
|
||||
additional_fields.isActive() ? additional_fields.get() : map<string, string>()
|
||||
);
|
||||
return;
|
||||
}
|
||||
case SdkApiType::SendGetConfigRequest: {
|
||||
if (!config_path.isActive()) {
|
||||
throw JsonError("Config path was not provided for get configuration event");
|
||||
}
|
||||
Maybe<string> config_val = sdk_server->getConfigValue(config_path.get());
|
||||
config_value = config_val.ok() ? config_val.unpack() : "";
|
||||
return;
|
||||
}
|
||||
case SdkApiType::SendPeriodicEvent: {
|
||||
if (!event_name.isActive()) {
|
||||
throw JsonError("Event name was not provided for periodic event");
|
||||
}
|
||||
if (!service_name.isActive()) {
|
||||
throw JsonError("Service name was not provided for periodic event");
|
||||
}
|
||||
sdk_server->sendMetric(
|
||||
event_name,
|
||||
service_name,
|
||||
getAudienceTeam(),
|
||||
ReportIS::IssuingEngine::AGENT_CORE,
|
||||
additional_fields.isActive() ? additional_fields.get() : map<string, string>()
|
||||
);
|
||||
return;
|
||||
}
|
||||
default: {
|
||||
dbgError(D_EXTERNAL_SDK_SERVER) << "Received illegal event type. Type : " << event_type.get();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
static string
|
||||
convertApiTypeToString(SdkApiType type)
|
||||
{
|
||||
static const EnumArray<SdkApiType, string> api_type_string {
|
||||
"Code Event",
|
||||
"Periodic Event",
|
||||
"Event Driven",
|
||||
"Get Configuration",
|
||||
};
|
||||
return api_type_string[type];
|
||||
}
|
||||
|
||||
Debug::DebugLevel
|
||||
getDebugLevel()
|
||||
{
|
||||
static const map<int, Debug::DebugLevel> debug_levels = {
|
||||
{0, Debug::DebugLevel::TRACE},
|
||||
{1, Debug::DebugLevel::DEBUG},
|
||||
{2, Debug::DebugLevel::INFO},
|
||||
{3, Debug::DebugLevel::WARNING},
|
||||
{4, Debug::DebugLevel::ERROR}
|
||||
};
|
||||
if (!debug_level.isActive()) {
|
||||
throw JsonError("Debug level was not provided for code event");
|
||||
}
|
||||
auto level = debug_levels.find(debug_level.get());
|
||||
if(level == debug_levels.end()) {
|
||||
throw JsonError("Illegal debug level provided");
|
||||
}
|
||||
|
||||
return level->second;
|
||||
}
|
||||
|
||||
ReportIS::Severity
|
||||
getSeverity()
|
||||
{
|
||||
if (!severity.isActive()) {
|
||||
throw JsonError("Event severity was not provided for periodic event");
|
||||
}
|
||||
switch (severity.get()) {
|
||||
case EventSeverity::SeverityCritical: return ReportIS::Severity::CRITICAL;
|
||||
case EventSeverity::SeverityHigh: return ReportIS::Severity::HIGH;
|
||||
case EventSeverity::SeverityMedium: return ReportIS::Severity::MEDIUM;
|
||||
case EventSeverity::SeverityLow: return ReportIS::Severity::LOW;
|
||||
case EventSeverity::SeverityInfo: return ReportIS::Severity::INFO;
|
||||
default:
|
||||
throw JsonError("Illegal event severity provided");
|
||||
}
|
||||
}
|
||||
|
||||
ReportIS::Priority
|
||||
getPriority()
|
||||
{
|
||||
if (!priority.isActive()) {
|
||||
throw JsonError("Event priority was not provided");
|
||||
}
|
||||
switch (priority.get()) {
|
||||
case EventPriority::PriorityUrgent: return ReportIS::Priority::URGENT;
|
||||
case EventPriority::PriorityHigh: return ReportIS::Priority::HIGH;
|
||||
case EventPriority::PriorityMedium: return ReportIS::Priority::MEDIUM;
|
||||
case EventPriority::PriorityLow: return ReportIS::Priority::LOW;
|
||||
default:
|
||||
throw JsonError("Illegal event priority provided");
|
||||
}
|
||||
}
|
||||
|
||||
ReportIS::Audience
|
||||
getAudience()
|
||||
{
|
||||
if (!audience.isActive()) {
|
||||
throw JsonError("Event audience was not provided");
|
||||
}
|
||||
switch (audience.get()) {
|
||||
case EventAudience::AudienceSecurity: return ReportIS::Audience::SECURITY;
|
||||
case EventAudience::AudienceInternal: return ReportIS::Audience::INTERNAL;
|
||||
default:
|
||||
throw JsonError("Illegal event audience provided");
|
||||
}
|
||||
}
|
||||
|
||||
ReportIS::AudienceTeam
|
||||
getAudienceTeam()
|
||||
{
|
||||
if (!team.isActive()) {
|
||||
throw JsonError("Event audience team was not provided");
|
||||
}
|
||||
switch (team.get()) {
|
||||
case EventAudienceTeam::AudienceTeamAgentCore: return ReportIS::AudienceTeam::AGENT_CORE;
|
||||
case EventAudienceTeam::AudienceTeamIot: return ReportIS::AudienceTeam::IOT_NEXT;
|
||||
case EventAudienceTeam::AudienceTeamWaap: return ReportIS::AudienceTeam::WAAP;
|
||||
case EventAudienceTeam::AudienceTeamAgentIntelligence: return ReportIS::AudienceTeam::AGENT_INTELLIGENCE;
|
||||
default:
|
||||
throw JsonError("Illegal event audience team provided");
|
||||
}
|
||||
}
|
||||
|
||||
using additional_fields_map = map<string, string>;
|
||||
C2S_LABEL_PARAM(int, event_type, "eventType");
|
||||
C2S_LABEL_OPTIONAL_PARAM(additional_fields_map, additional_fields, "additionalFields");
|
||||
C2S_LABEL_OPTIONAL_PARAM(string, event_name, "eventName");
|
||||
C2S_LABEL_OPTIONAL_PARAM(string, service_name, "serviceName");
|
||||
C2S_OPTIONAL_PARAM(int, team);
|
||||
C2S_OPTIONAL_PARAM(int, audience);
|
||||
C2S_OPTIONAL_PARAM(int, severity);
|
||||
C2S_OPTIONAL_PARAM(int, priority);
|
||||
C2S_OPTIONAL_PARAM(string, tag);
|
||||
C2S_OPTIONAL_PARAM(string, file);
|
||||
C2S_OPTIONAL_PARAM(string, func);
|
||||
C2S_OPTIONAL_PARAM(int, line);
|
||||
C2S_LABEL_OPTIONAL_PARAM(int, debug_level, "debugLevel");
|
||||
C2S_LABEL_OPTIONAL_PARAM(string, trace_id, "traceId");
|
||||
C2S_LABEL_OPTIONAL_PARAM(string, span_id, "spanId");
|
||||
C2S_OPTIONAL_PARAM(string, message);
|
||||
C2S_LABEL_OPTIONAL_PARAM(string, config_path, "configPath");
|
||||
S2C_LABEL_OPTIONAL_PARAM(string, config_value, "configValue");
|
||||
};
|
||||
|
||||
class ExternalSdkServer::Impl
|
||||
:
|
||||
public Singleton::Provide<I_ExternalSdkServer>::From<ExternalSdkServer>
|
||||
{
|
||||
public:
|
||||
void
|
||||
init()
|
||||
{
|
||||
auto rest = Singleton::Consume<I_RestApi>::by<ExternalSdkServer>();
|
||||
rest->addRestCall<ExternalSdkRest>(RestAction::ADD, "sdk-call");
|
||||
}
|
||||
|
||||
void
|
||||
sendLog(
|
||||
const string &event_name,
|
||||
ReportIS::Audience audience,
|
||||
ReportIS::Severity severity,
|
||||
ReportIS::Priority priority,
|
||||
const string &tag_string,
|
||||
const map<string, string> &additional_fields)
|
||||
{
|
||||
Maybe<ReportIS::Tags> tag = TagAndEnumManagement::convertStringToTag(tag_string);
|
||||
set<ReportIS::Tags> tags;
|
||||
if (tag.ok()) tags.insert(tag.unpack());
|
||||
LogGen log(event_name, audience, severity, priority, tags);
|
||||
for (const auto &field : additional_fields) {
|
||||
log << LogField(field.first, field.second);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
sendDebug(
|
||||
const string &file_name,
|
||||
const string &function_name,
|
||||
unsigned int line_number,
|
||||
Debug::DebugLevel debug_level,
|
||||
const string &trace_id,
|
||||
const string &span_id,
|
||||
const string &message,
|
||||
const map<string, string> &additional_fields)
|
||||
{
|
||||
(void)trace_id;
|
||||
(void)span_id;
|
||||
Debug debug(file_name, function_name, line_number, debug_level, D_EXTERNAL_SDK_USER);
|
||||
debug.getStreamAggr() << message;
|
||||
bool is_first_key = true;
|
||||
for (const auto &field : additional_fields) {
|
||||
if (is_first_key) {
|
||||
is_first_key = false;
|
||||
debug.getStreamAggr() << ". ";
|
||||
} else {
|
||||
debug.getStreamAggr() << ", ";
|
||||
}
|
||||
debug.getStreamAggr() << "\"" << field.first << "\": \"" << field.second << "\"";
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
sendMetric(
|
||||
const string &event_title,
|
||||
const string &service_name,
|
||||
ReportIS::AudienceTeam team,
|
||||
ReportIS::IssuingEngine issuing_engine,
|
||||
const map<string, string> &additional_fields)
|
||||
{
|
||||
ScopedContext ctx;
|
||||
ctx.registerValue("Service Name", service_name);
|
||||
|
||||
set<ReportIS::Tags> tags;
|
||||
Report metric_to_fog(
|
||||
event_title,
|
||||
Singleton::Consume<I_TimeGet>::by<GenericMetric>()->getWalltime(),
|
||||
ReportIS::Type::PERIODIC,
|
||||
ReportIS::Level::LOG,
|
||||
ReportIS::LogLevel::INFO,
|
||||
ReportIS::Audience::INTERNAL,
|
||||
team,
|
||||
ReportIS::Severity::INFO,
|
||||
ReportIS::Priority::LOW,
|
||||
chrono::seconds(0),
|
||||
LogField("agentId", Singleton::Consume<I_AgentDetails>::by<GenericMetric>()->getAgentId()),
|
||||
tags,
|
||||
ReportIS::Tags::INFORMATIONAL,
|
||||
issuing_engine
|
||||
);
|
||||
|
||||
for (const auto &field : additional_fields) {
|
||||
metric_to_fog << LogField(field.first, field.second);
|
||||
}
|
||||
|
||||
LogRest metric_client_rest(metric_to_fog);
|
||||
|
||||
string fog_metric_uri = getConfigurationWithDefault<string>("/api/v1/agents/events", "metric", "fogMetricUri");
|
||||
Singleton::Consume<I_Messaging>::by<ExternalSdkServer>()->sendAsyncMessage(
|
||||
HTTPMethod::POST,
|
||||
fog_metric_uri,
|
||||
metric_client_rest,
|
||||
MessageCategory::METRIC,
|
||||
MessageMetadata(),
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
Maybe<string>
|
||||
getConfigValue(const string &config_path)
|
||||
{
|
||||
auto config_val = getProfileAgentSetting<string>(config_path);
|
||||
if (!config_val.ok()) {
|
||||
stringstream error;
|
||||
error << "Failed to get configuration. Config path: " << config_path << ", Error: " << config_val.getErr();
|
||||
return genError(error.str());
|
||||
}
|
||||
return config_val.unpack();
|
||||
}
|
||||
};
|
||||
|
||||
ExternalSdkServer::ExternalSdkServer() : Component("ExternalSdkServer"), pimpl(make_unique<Impl>()) {}
|
||||
ExternalSdkServer::~ExternalSdkServer() {}
|
||||
|
||||
void ExternalSdkServer::init() { pimpl->init(); }
|
||||
void ExternalSdkServer::fini() {}
|
||||
|
||||
void ExternalSdkServer::preload() {}
|
@@ -0,0 +1,7 @@
|
||||
link_directories(${BOOST_ROOT}/lib)
|
||||
|
||||
add_unit_test(
|
||||
external_sdk_server_ut
|
||||
"external_sdk_server_ut.cc"
|
||||
"external_sdk_server;mainloop;singleton;rest;environment;time_proxy;logging;event_is;metric;-lboost_context;agent_details;-lboost_regex;messaging;"
|
||||
)
|
@@ -0,0 +1,349 @@
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "external_sdk_server.h"
|
||||
|
||||
#include "cptest.h"
|
||||
#include "mock/mock_rest_api.h"
|
||||
#include "mock/mock_messaging.h"
|
||||
#include "mock/mock_logging.h"
|
||||
#include "mock/mock_time_get.h"
|
||||
#include "config.h"
|
||||
#include "config_component.h"
|
||||
#include "agent_details.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace testing;
|
||||
|
||||
class ExternalSdkServerTest : public Test
|
||||
{
|
||||
public:
|
||||
ExternalSdkServerTest()
|
||||
{
|
||||
EXPECT_CALL(rest_mocker, mockRestCall(RestAction::ADD, "sdk-call", _)).WillOnce(
|
||||
WithArg<2>(
|
||||
Invoke(
|
||||
[this](const unique_ptr<RestInit> &rest_ptr)
|
||||
{
|
||||
mock_sdk_rest = rest_ptr->getRest();
|
||||
return true;
|
||||
}
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
sdk_server.preload();
|
||||
sdk_server.init();
|
||||
i_sdk = Singleton::Consume<I_ExternalSdkServer>::from(sdk_server);
|
||||
}
|
||||
|
||||
~ExternalSdkServerTest()
|
||||
{
|
||||
sdk_server.fini();
|
||||
}
|
||||
|
||||
ExternalSdkServer sdk_server;
|
||||
NiceMock<MockTimeGet> mock_timer;
|
||||
StrictMock<MockMessaging> messaging_mocker;
|
||||
StrictMock<MockRestApi> rest_mocker;
|
||||
StrictMock<MockLogging> log_mocker;
|
||||
unique_ptr<ServerRest> mock_sdk_rest;
|
||||
I_ExternalSdkServer *i_sdk;
|
||||
ConfigComponent conf;
|
||||
AgentDetails agent_details;
|
||||
::Environment env;
|
||||
};
|
||||
|
||||
TEST_F(ExternalSdkServerTest, initTest)
|
||||
{
|
||||
}
|
||||
|
||||
TEST_F(ExternalSdkServerTest, configCall)
|
||||
{
|
||||
Maybe<string> no_conf = i_sdk->getConfigValue("key1");
|
||||
EXPECT_FALSE(no_conf.ok());
|
||||
string config_json =
|
||||
"{\n"
|
||||
"\"agentSettings\": [\n"
|
||||
"{\n"
|
||||
"\"id\": \"id1\",\n"
|
||||
"\"key\": \"key1\",\n"
|
||||
"\"value\": \"value1\"\n"
|
||||
"},\n"
|
||||
"{\n"
|
||||
"\"id\": \"id1\",\n"
|
||||
"\"key\": \"key2\",\n"
|
||||
"\"value\": \"value2\"\n"
|
||||
"}\n"
|
||||
"]\n"
|
||||
"}\n";
|
||||
conf.preload();
|
||||
istringstream conf_stream(config_json);
|
||||
ASSERT_TRUE(Singleton::Consume<Config::I_Config>::from(conf)->loadConfiguration(conf_stream));
|
||||
|
||||
Maybe<string> conf_found = i_sdk->getConfigValue("key1");
|
||||
ASSERT_TRUE(conf_found.ok());
|
||||
EXPECT_EQ(conf_found.unpack(), "value1");
|
||||
|
||||
conf_found = i_sdk->getConfigValue("key2");
|
||||
ASSERT_TRUE(conf_found.ok());
|
||||
EXPECT_EQ(conf_found.unpack(), "value2");
|
||||
|
||||
stringstream config_call_body;
|
||||
config_call_body << "{ \"eventType\": 3, \"configPath\": \"key1\" }";
|
||||
|
||||
Maybe<string> sdk_conf = mock_sdk_rest->performRestCall(config_call_body);
|
||||
ASSERT_TRUE(sdk_conf.ok());
|
||||
EXPECT_EQ(
|
||||
sdk_conf.unpack(),
|
||||
"{\n"
|
||||
" \"configValue\": \"value1\"\n"
|
||||
"}"
|
||||
);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
string
|
||||
toJson(const T &obj)
|
||||
{
|
||||
stringstream ss;
|
||||
{
|
||||
cereal::JSONOutputArchive ar(ss);
|
||||
obj.serialize(ar);
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
TEST_F(ExternalSdkServerTest, eventDrivenCall)
|
||||
{
|
||||
string generated_log;
|
||||
EXPECT_CALL(log_mocker, getCurrentLogId()).Times(2).WillRepeatedly(Return(0));
|
||||
EXPECT_CALL(log_mocker, sendLog(_)).Times(2).WillRepeatedly(
|
||||
WithArg<0>(
|
||||
Invoke(
|
||||
[&] (const Report &msg)
|
||||
{
|
||||
generated_log = toJson(msg);
|
||||
}
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
i_sdk->sendLog(
|
||||
"my log",
|
||||
ReportIS::Audience::INTERNAL,
|
||||
ReportIS::Severity::LOW,
|
||||
ReportIS::Priority::HIGH,
|
||||
"IPS",
|
||||
{{"key1", "value1"}, {"key2", "value2"}}
|
||||
);
|
||||
static const string expected_log =
|
||||
"{\n"
|
||||
" \"eventTime\": \"\",\n"
|
||||
" \"eventName\": \"my log\",\n"
|
||||
" \"eventSeverity\": \"Low\",\n"
|
||||
" \"eventPriority\": \"High\",\n"
|
||||
" \"eventType\": \"Event Driven\",\n"
|
||||
" \"eventLevel\": \"Log\",\n"
|
||||
" \"eventLogLevel\": \"info\",\n"
|
||||
" \"eventAudience\": \"Internal\",\n"
|
||||
" \"eventAudienceTeam\": \"\",\n"
|
||||
" \"eventFrequency\": 0,\n"
|
||||
" \"eventTags\": [\n"
|
||||
" \"IPS\"\n"
|
||||
" ],\n"
|
||||
" \"eventSource\": {\n"
|
||||
" \"agentId\": \"Unknown\",\n"
|
||||
" \"eventTraceId\": \"\",\n"
|
||||
" \"eventSpanId\": \"\",\n"
|
||||
" \"issuingEngineVersion\": \"\",\n"
|
||||
" \"serviceName\": \"Unnamed Nano Service\"\n"
|
||||
" },\n"
|
||||
" \"eventData\": {\n"
|
||||
" \"logIndex\": 0,\n"
|
||||
" \"key1\": \"value1\",\n"
|
||||
" \"key2\": \"value2\"\n"
|
||||
" }\n"
|
||||
"}";
|
||||
|
||||
EXPECT_EQ(generated_log, expected_log);
|
||||
|
||||
string event_call_body =
|
||||
"{\n"
|
||||
" \"eventType\": 2,\n"
|
||||
" \"eventName\": \"my log\",\n"
|
||||
" \"audience\": 1,\n"
|
||||
" \"severity\": 3,\n"
|
||||
" \"priority\": 1,\n"
|
||||
" \"tag\": \"IPS\",\n"
|
||||
" \"team\": 3,\n"
|
||||
" \"additionalFields\": {\n"
|
||||
" \"key1\": \"value1\",\n"
|
||||
" \"key2\": \"value2\"\n"
|
||||
" }\n"
|
||||
"}";
|
||||
|
||||
generated_log = "";
|
||||
stringstream event_call_stream;
|
||||
event_call_stream << event_call_body;
|
||||
EXPECT_TRUE(mock_sdk_rest->performRestCall(event_call_stream).ok());
|
||||
EXPECT_EQ(generated_log, expected_log);
|
||||
}
|
||||
|
||||
TEST_F(ExternalSdkServerTest, periodicEventCall)
|
||||
{
|
||||
string message_body;
|
||||
EXPECT_CALL(
|
||||
messaging_mocker,
|
||||
sendAsyncMessage(
|
||||
HTTPMethod::POST,
|
||||
"/api/v1/agents/events",
|
||||
_,
|
||||
MessageCategory::METRIC,
|
||||
_,
|
||||
false
|
||||
)
|
||||
).Times(2).WillRepeatedly(SaveArg<2>(&message_body));
|
||||
|
||||
i_sdk->sendMetric(
|
||||
"my metric",
|
||||
"matrix",
|
||||
ReportIS::AudienceTeam::AGENT_INTELLIGENCE,
|
||||
ReportIS::IssuingEngine::AGENT_CORE,
|
||||
{{"key", "value"}}
|
||||
);
|
||||
|
||||
static const string expected_message =
|
||||
"{\n"
|
||||
" \"log\": {\n"
|
||||
" \"eventTime\": \"\",\n"
|
||||
" \"eventName\": \"my metric\",\n"
|
||||
" \"eventSeverity\": \"Info\",\n"
|
||||
" \"eventPriority\": \"Low\",\n"
|
||||
" \"eventType\": \"Periodic\",\n"
|
||||
" \"eventLevel\": \"Log\",\n"
|
||||
" \"eventLogLevel\": \"info\",\n"
|
||||
" \"eventAudience\": \"Internal\",\n"
|
||||
" \"eventAudienceTeam\": \"Agent Intelligence\",\n"
|
||||
" \"eventFrequency\": 0,\n"
|
||||
" \"eventTags\": [\n"
|
||||
" \"Informational\"\n"
|
||||
" ],\n"
|
||||
" \"eventSource\": {\n"
|
||||
" \"agentId\": \"Unknown\",\n"
|
||||
" \"issuingEngine\": \"Agent Core\",\n"
|
||||
" \"eventTraceId\": \"\",\n"
|
||||
" \"eventSpanId\": \"\",\n"
|
||||
" \"issuingEngineVersion\": \"\",\n"
|
||||
" \"serviceName\": \"matrix\"\n"
|
||||
" },\n"
|
||||
" \"eventData\": {\n"
|
||||
" \"key\": \"value\"\n"
|
||||
" }\n"
|
||||
" }\n"
|
||||
"}";
|
||||
|
||||
EXPECT_EQ(message_body, expected_message);
|
||||
|
||||
string event_call_body =
|
||||
"{\n"
|
||||
" \"eventType\": 1,\n"
|
||||
" \"eventName\": \"my metric\",\n"
|
||||
" \"serviceName\": \"matrix\",\n"
|
||||
" \"team\": 3,\n"
|
||||
" \"additionalFields\": {\n"
|
||||
" \"key\": \"value\"\n"
|
||||
" }\n"
|
||||
"}";
|
||||
|
||||
stringstream event_call_stream;
|
||||
event_call_stream << event_call_body;
|
||||
|
||||
message_body = "";
|
||||
EXPECT_TRUE(mock_sdk_rest->performRestCall(event_call_stream).ok());
|
||||
EXPECT_EQ(message_body, expected_message);
|
||||
}
|
||||
|
||||
USE_DEBUG_FLAG(D_EXTERNAL_SDK_USER);
|
||||
USE_DEBUG_FLAG(D_EXTERNAL_SDK_SERVER);
|
||||
|
||||
TEST_F(ExternalSdkServerTest, codeEventCall)
|
||||
{
|
||||
ostringstream capture_debug;
|
||||
Debug::setUnitTestFlag(D_EXTERNAL_SDK_SERVER, Debug::DebugLevel::TRACE);
|
||||
Debug::setUnitTestFlag(D_EXTERNAL_SDK_USER, Debug::DebugLevel::TRACE);
|
||||
Debug::setNewDefaultStdout(&capture_debug);
|
||||
|
||||
i_sdk->sendDebug(
|
||||
"file.cc",
|
||||
"myFunc2",
|
||||
42,
|
||||
Debug::DebugLevel::TRACE,
|
||||
"123",
|
||||
"abc",
|
||||
"h#l1ow w0r!d",
|
||||
{{"hi", "universe"}}
|
||||
);
|
||||
|
||||
EXPECT_THAT(
|
||||
capture_debug.str(),
|
||||
HasSubstr(
|
||||
"[myFunc2@file.cc:42 | >>>] "
|
||||
"h#l1ow w0r!d. \"hi\": \"universe\"\n"
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
string debug_event =
|
||||
"{\n"
|
||||
" \"eventType\": 0,\n"
|
||||
" \"file\": \"my file\",\n"
|
||||
" \"func\": \"function_name\",\n"
|
||||
" \"line\": 42,\n"
|
||||
" \"debugLevel\": 0,\n"
|
||||
" \"traceId\": \"\",\n"
|
||||
" \"spanId\": \"span2323\",\n"
|
||||
" \"message\": \"some short debug\",\n"
|
||||
" \"team\": 1,\n"
|
||||
" \"additionalFields\": {\n"
|
||||
" \"name\": \"moshe\",\n"
|
||||
" \"food\": \"bamba\"\n"
|
||||
" }\n"
|
||||
"}";
|
||||
|
||||
stringstream event_call_stream;
|
||||
event_call_stream << debug_event;
|
||||
|
||||
EXPECT_TRUE(mock_sdk_rest->performRestCall(event_call_stream).ok());
|
||||
|
||||
EXPECT_THAT(
|
||||
capture_debug.str(),
|
||||
HasSubstr(
|
||||
"[function_name@my file:42 | >>>] "
|
||||
"some short debug. \"food\": \"bamba\", \"name\": \"moshe\"\n"
|
||||
)
|
||||
);
|
||||
|
||||
Debug::setNewDefaultStdout(&cout);
|
||||
}
|
||||
|
||||
TEST_F(ExternalSdkServerTest, ilegalEventCall)
|
||||
{
|
||||
string event_call_body =
|
||||
"{\n"
|
||||
" \"eventType\": 7,\n"
|
||||
" \"eventName\": \"my metric\",\n"
|
||||
" \"serviceName\": \"matrix\",\n"
|
||||
" \"team\": 3,\n"
|
||||
" \"additionalFields\": {\n"
|
||||
" \"key\": \"value\"\n"
|
||||
" }\n"
|
||||
"}";
|
||||
|
||||
stringstream event_call_stream;
|
||||
event_call_stream << event_call_body;
|
||||
|
||||
Maybe<string> failed_respond = mock_sdk_rest->performRestCall(event_call_stream);
|
||||
EXPECT_FALSE(failed_respond.ok());
|
||||
EXPECT_EQ(failed_respond.getErr(), "Illegal event type provided");
|
||||
}
|
@@ -50,6 +50,8 @@ public:
|
||||
return report.str();
|
||||
}
|
||||
|
||||
UpdatesFailureReason getReason() const { return reason; }
|
||||
|
||||
private:
|
||||
UpdatesProcessResult result;
|
||||
UpdatesConfigType type;
|
||||
|
@@ -34,6 +34,7 @@ private:
|
||||
void sendReoprt();
|
||||
|
||||
static std::vector<UpdatesProcessReport> reports;
|
||||
uint report_failure_count = 0;
|
||||
};
|
||||
|
||||
#endif // __UPDATES_PROCESS_REPORTER_H__
|
||||
|
@@ -1499,7 +1499,7 @@ private:
|
||||
<< " minutes from now.";
|
||||
upgrade_delay_time += chrono::minutes(upgrade_delay_interval);
|
||||
} catch (const exception& err) {
|
||||
dbgInfo(D_ORCHESTRATOR) << "Failed to parse upgrade delay interval.";
|
||||
dbgWarning(D_ORCHESTRATOR) << "Failed to parse upgrade delay interval.";
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -413,7 +413,7 @@ ServiceController::Impl::getUpdatedReconfStatus()
|
||||
}
|
||||
|
||||
if (!maybe_service.unpack().isServiceActive()) {
|
||||
dbgInfo(D_SERVICE_CONTROLLER)
|
||||
dbgDebug(D_SERVICE_CONTROLLER)
|
||||
<< "Service is not active, removing from registered services list. Service: "
|
||||
<< services_reconf_names[service_and_reconf_status.first]
|
||||
<< "ID: "
|
||||
@@ -508,7 +508,7 @@ ServiceController::Impl::loadRegisteredServicesFromFile()
|
||||
ar(cereal::make_nvp("Registered Services", pending_services));
|
||||
pending_services.erase("cp-nano-orchestration");
|
||||
|
||||
dbgInfo(D_SERVICE_CONTROLLER)
|
||||
dbgDebug(D_SERVICE_CONTROLLER)
|
||||
<< "Orchestration pending services loaded from file."
|
||||
<< " File: "
|
||||
<< registered_services_file
|
||||
@@ -516,7 +516,7 @@ ServiceController::Impl::loadRegisteredServicesFromFile()
|
||||
|
||||
for (const auto &id_service_pair : pending_services) {
|
||||
const auto &service = id_service_pair.second;
|
||||
dbgInfo(D_SERVICE_CONTROLLER)
|
||||
dbgDebug(D_SERVICE_CONTROLLER)
|
||||
<< "Service name: "
|
||||
<< service.getServiceName()
|
||||
<< ", Service ID: "
|
||||
@@ -548,14 +548,14 @@ ServiceController::Impl::writeRegisteredServicesToFile()
|
||||
cereal::JSONOutputArchive ar(ss);
|
||||
ar(cereal::make_nvp("Registered Services", registered_services_with_orch));
|
||||
|
||||
dbgInfo(D_SERVICE_CONTROLLER)
|
||||
dbgDebug(D_SERVICE_CONTROLLER)
|
||||
<< "Orchestration registered services file has been updated. File: "
|
||||
<< registered_services_file
|
||||
<< ". Registered Services:";
|
||||
|
||||
for (const auto &id_service_pair : registered_services_with_orch) {
|
||||
const auto &service = id_service_pair.second;
|
||||
dbgInfo(D_SERVICE_CONTROLLER)
|
||||
dbgDebug(D_SERVICE_CONTROLLER)
|
||||
<< "Service name: "
|
||||
<< service.getServiceName()
|
||||
<< ", Service ID: "
|
||||
|
@@ -31,9 +31,15 @@ UpdatesProcessReporter::upon(const UpdatesProcessEvent &event)
|
||||
if (event.getReason() == UpdatesFailureReason::CHECK_UPDATE) {
|
||||
if (event.getResult() == UpdatesProcessResult::SUCCESS && reports.empty()) {
|
||||
dbgTrace(D_UPDATES_PROCESS_REPORTER) << "Update proccess finished successfully";
|
||||
report_failure_count = 0;
|
||||
return;
|
||||
}
|
||||
dbgTrace(D_UPDATES_PROCESS_REPORTER) << "Update proccess finished with errors";
|
||||
report_failure_count++;
|
||||
if (report_failure_count <= 1) {
|
||||
reports.clear();
|
||||
return;
|
||||
}
|
||||
reports.emplace_back(
|
||||
UpdatesProcessReport(
|
||||
event.getResult(),
|
||||
@@ -54,18 +60,27 @@ UpdatesProcessReporter::upon(const UpdatesProcessEvent &event)
|
||||
void
|
||||
UpdatesProcessReporter::sendReoprt()
|
||||
{
|
||||
stringstream all_reports;
|
||||
all_reports << "Updates process reports:" << endl;
|
||||
stringstream full_reports;
|
||||
UpdatesFailureReason failure_reason = UpdatesFailureReason::NONE;
|
||||
full_reports << "Updates process reports:" << endl;
|
||||
full_reports << "report failure count:" << report_failure_count << endl;
|
||||
for (const auto &report : reports) {
|
||||
all_reports << report.toString() << endl;
|
||||
if (report.getReason() != UpdatesFailureReason::CHECK_UPDATE) {
|
||||
failure_reason = report.getReason();
|
||||
}
|
||||
full_reports << report.toString() << endl;
|
||||
}
|
||||
reports.clear();
|
||||
dbgTrace(D_UPDATES_PROCESS_REPORTER) << "Sending updates process report: " << endl << all_reports.str();
|
||||
LogGen(
|
||||
dbgTrace(D_UPDATES_PROCESS_REPORTER) << "Sending updates process report: " << endl << full_reports.str();
|
||||
LogGen log (
|
||||
"Updates process report",
|
||||
ReportIS::Audience::INTERNAL,
|
||||
ReportIS::Severity::HIGH,
|
||||
ReportIS::Priority::HIGH,
|
||||
ReportIS::Tags::ORCHESTRATOR
|
||||
) << LogField("eventMessage", all_reports.str());
|
||||
);
|
||||
log << LogField("eventMessage", full_reports.str());
|
||||
if (failure_reason != UpdatesFailureReason::NONE) {
|
||||
log.addToOrigin(LogField("eventCategory", convertUpdatesFailureReasonToStr(failure_reason)));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user