mirror of
https://github.com/openappsec/openappsec.git
synced 2025-09-29 19:24:26 +03:00
sync code
This commit is contained in:
@@ -34,6 +34,8 @@ static const string invalidation_uri = "/api/v2/intelligence/invalidation";
|
||||
static const string registration_uri = "/api/v2/intelligence/invalidation/register";
|
||||
static const string query_uri = "/api/v2/intelligence/assets/query";
|
||||
static const string queries_uri = "/api/v2/intelligence/assets/queries";
|
||||
static const string fog_health_uri = "/access-manager/health/live";
|
||||
static const string intelligence_health_uri = "/show-health";
|
||||
|
||||
class I_InvalidationCallBack
|
||||
{
|
||||
@@ -291,6 +293,16 @@ private:
|
||||
I_MainLoop *mainloop;
|
||||
};
|
||||
|
||||
|
||||
class IntelligenceHealth : public ClientRest
|
||||
{
|
||||
public:
|
||||
bool isLocalHealthy() const { return healthy.isActive() && healthy.get(); }
|
||||
|
||||
private:
|
||||
S2C_PARAM(bool, healthy);
|
||||
};
|
||||
|
||||
class IntelligenceComponentV2::Impl
|
||||
:
|
||||
Singleton::Provide<I_Intelligence_IS_V2>::From<IntelligenceComponentV2>
|
||||
@@ -314,6 +326,35 @@ public:
|
||||
rest_api->addRestCall<ReceiveInvalidation>(RestAction::SET, "new-invalidation/source/invalidation");
|
||||
}
|
||||
|
||||
bool
|
||||
isIntelligenceHealthy() const override
|
||||
{
|
||||
dbgFlow(D_INTELLIGENCE) << "Checking intelligence health";
|
||||
IntelligenceHealth healthObj;
|
||||
if (hasLocalIntelligenceSupport()) {
|
||||
dbgDebug(D_INTELLIGENCE) << "Local intelligence supported";
|
||||
return sendLocalIntelligenceToLocalServer(healthObj).ok();
|
||||
}
|
||||
dbgTrace(D_INTELLIGENCE) << "Checking connection to the FOG";
|
||||
auto response = message->sendSyncMessage(
|
||||
HTTPMethod::GET,
|
||||
fog_health_uri,
|
||||
string(""),
|
||||
MessageCategory::INTELLIGENCE
|
||||
);
|
||||
|
||||
if (response.ok() && response.unpack().getHTTPStatusCode() == HTTPStatusCode::HTTP_OK) {
|
||||
dbgTrace(D_INTELLIGENCE) << "Connected to the FOG";
|
||||
return true;
|
||||
}
|
||||
|
||||
dbgTrace(D_INTELLIGENCE)
|
||||
<< "No connection to the FOG. "
|
||||
<< (response.ok() ? response.unpack() : response.getErr()).toString();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool
|
||||
sendInvalidation(const Invalidation &invalidation) const override
|
||||
{
|
||||
@@ -487,6 +528,36 @@ private:
|
||||
return genError("Could not send local intelligence invalidation");
|
||||
}
|
||||
|
||||
Maybe<Response>
|
||||
sendIntelligenceRequestImpl(const IntelligenceHealth &, const MessageMetadata &req_md) const
|
||||
{
|
||||
dbgFlow(D_INTELLIGENCE) << "Sending intelligence health check";
|
||||
IntelligenceHealth healthObj;
|
||||
auto req_data = message->sendSyncMessage(
|
||||
HTTPMethod::GET,
|
||||
intelligence_health_uri,
|
||||
healthObj,
|
||||
MessageCategory::INTELLIGENCE,
|
||||
req_md
|
||||
);
|
||||
if (req_data.ok() && healthObj.isLocalHealthy()) {
|
||||
dbgDebug(D_INTELLIGENCE) << "Intelligence is healthy.";
|
||||
return Response();
|
||||
}
|
||||
|
||||
if (!req_data.ok()) {
|
||||
dbgDebug(D_INTELLIGENCE)
|
||||
<< "Intelligence is not healthy. Body: "
|
||||
<< req_data.getErr().getBody()
|
||||
<< " Error: "
|
||||
<< req_data.getErr().toString();
|
||||
} else {
|
||||
dbgDebug(D_INTELLIGENCE) << "Intelligence return unhealthy status.";
|
||||
}
|
||||
|
||||
return genError("Intelligence is not healthy");
|
||||
}
|
||||
|
||||
Maybe<Response>
|
||||
sendIntelligenceRequestImpl(
|
||||
const InvalidationRegistration::RestCall ®istration,
|
||||
|
@@ -1341,3 +1341,59 @@ TEST_F(IntelligenceComponentTestV2, ignoreInProgressQueryTest_2)
|
||||
|
||||
EXPECT_EQ(objects_ids.size(), 2u);
|
||||
}
|
||||
|
||||
TEST_F(IntelligenceComponentTestV2, foghealthy)
|
||||
{
|
||||
Debug::setUnitTestFlag(D_INTELLIGENCE, Debug::DebugLevel::TRACE);
|
||||
I_Intelligence_IS_V2 *intell = Singleton::Consume<I_Intelligence_IS_V2>::by<IntelligenceComponentTestV2>();
|
||||
|
||||
HTTPResponse fog_res(
|
||||
HTTPStatusCode::HTTP_OK,
|
||||
string(
|
||||
"{"
|
||||
" \"up\": true,"
|
||||
" \"timestamp\":\"\""
|
||||
"}"
|
||||
)
|
||||
);
|
||||
|
||||
EXPECT_CALL(
|
||||
messaging_mock,
|
||||
sendSyncMessage(HTTPMethod::GET, "/access-manager/health/live", _, MessageCategory::INTELLIGENCE, _)
|
||||
).WillOnce(Return(fog_res));
|
||||
|
||||
EXPECT_TRUE(intell->isIntelligenceHealthy());
|
||||
}
|
||||
|
||||
TEST_F(IntelligenceComponentTestV2, localIntelligenceHealthy)
|
||||
{
|
||||
Debug::setUnitTestFlag(D_INTELLIGENCE, Debug::DebugLevel::TRACE);
|
||||
stringstream configuration;
|
||||
configuration << "{";
|
||||
configuration << " \"agentSettings\":[";
|
||||
configuration << " {\"key\":\"agent.config.useLocalIntelligence\",\"id\":\"id1\",\"value\":\"true\"}";
|
||||
configuration << " ],";
|
||||
configuration << " \"intelligence\":{";
|
||||
configuration << " \"local intelligence server ip\":\"127.0.0.1\",";
|
||||
configuration << " \"local intelligence server primary port\":9090";
|
||||
configuration << " }";
|
||||
configuration << "}";
|
||||
Singleton::Consume<Config::I_Config>::from(conf)->loadConfiguration(configuration);
|
||||
|
||||
I_Intelligence_IS_V2 *intell = Singleton::Consume<I_Intelligence_IS_V2>::by<IntelligenceComponentTestV2>();
|
||||
|
||||
string localHealthy(
|
||||
"{\n"
|
||||
" \"healthy\": true\n"
|
||||
"}\n"
|
||||
);
|
||||
|
||||
EXPECT_CALL(
|
||||
messaging_mock,
|
||||
sendSyncMessage(HTTPMethod::GET, "/show-health", _, MessageCategory::INTELLIGENCE, _)
|
||||
).WillOnce(Return(HTTPResponse(HTTPStatusCode::HTTP_OK, localHealthy)));
|
||||
|
||||
EXPECT_CALL(mock_rest, getListeningPort()).Times(1).WillRepeatedly(Return(8888));
|
||||
|
||||
EXPECT_TRUE(intell->isIntelligenceHealthy());
|
||||
}
|
||||
|
Reference in New Issue
Block a user