Jan_31_2024-Dev

This commit is contained in:
Ned Wright
2024-01-31 17:34:53 +00:00
parent 752a5785f0
commit 6d67818a94
376 changed files with 8101 additions and 7064 deletions

58
core/intelligence_is_v2/intelligence_comp_v2.cc Executable file → Normal file
View File

@@ -80,7 +80,7 @@ public:
{
stringstream res;
res << "{ \"apiVersion\": \"v2\", \"communicationType\": \"sync\", ";
res << "{ \"apiVersion\": \"v2\", \"communicationType\": \"sync\", \"callbackType\": \"invalidation\", ";
auto details = Singleton::Consume<I_AgentDetails>::by<IntelligenceComponentV2>();
res << "\"name\": \"" << details->getAgentId() << "\", ";
auto rest = Singleton::Consume<I_RestApi>::by<IntelligenceComponentV2>();
@@ -388,16 +388,14 @@ private:
<< "Invalidation value: "
<< (invalidation.genJson().ok() ? invalidation.genJson().unpack() : invalidation.genJson().getErr());
return message->sendNoReplyObject(
invalidation,
I_Messaging::Method::POST,
server,
*port,
Flags<MessageConnConfig>(),
MessageMetadata invalidation_req_md(server, *port);
invalidation_req_md.insertHeaders(getHTTPHeaders());
return message->sendSyncMessageWithoutResponse(
HTTPMethod::POST,
invalidation_uri,
getHTTPHeaders(),
nullptr,
MessageTypeTag::INTELLIGENCE
invalidation,
MessageCategory::INTELLIGENCE,
invalidation_req_md
);
}
@@ -409,27 +407,29 @@ private:
dbgTrace(D_INTELLIGENCE)
<< "Invalidation value: "
<< (invalidation.genJson().ok() ? invalidation.genJson().unpack() : invalidation.genJson().getErr());
return message->sendNoReplyObject(
invalidation,
I_Messaging::Method::POST,
MessageMetadata global_invalidation_req_md;
global_invalidation_req_md.insertHeaders(getHTTPHeaders());
return message->sendSyncMessageWithoutResponse(
HTTPMethod::POST,
invalidation_uri,
getHTTPHeaders(),
nullptr,
true,
MessageTypeTag::INTELLIGENCE
invalidation,
MessageCategory::INTELLIGENCE,
global_invalidation_req_md
);
}
string
map<string, string>
getHTTPHeaders() const
{
map<string, string> headers;
auto details = Singleton::Consume<I_AgentDetails>::by<IntelligenceComponentV2>();
auto tenant = details->getTenantId();
if (tenant == "") tenant = "Global";
headers["X-Tenant-Id"] = tenant;
auto agent = details->getAgentId();
headers["X-Source-Id"] = agent;
return "X-Tenant-Id: " + tenant + "\r\nX-Source-Id: " + agent;
return headers;
}
bool
@@ -472,17 +472,13 @@ private:
}
dbgTrace(D_INTELLIGENCE) << "Invalidation value: " << registration.genJson();
return message->sendNoReplyObject(
registration,
I_Messaging::Method::POST,
server,
*port,
Flags<MessageConnConfig>(),
MessageMetadata registration_req_md(server, *port);
return message->sendSyncMessageWithoutResponse(
HTTPMethod::POST,
registration_uri,
"",
nullptr,
MessageTypeTag::INTELLIGENCE
registration,
MessageCategory::INTELLIGENCE,
registration_req_md
);
}
@@ -497,7 +493,7 @@ private:
OfflineIntelligeceHandler offline_intelligence;
bool offline_mode_only = false;
InvalidationCallBack invalidations;
I_Messaging *message = nullptr;
I_Messaging *message = nullptr;
I_TimeGet *timer = nullptr;
I_MainLoop *mainloop = nullptr;
};

View File

@@ -4,4 +4,4 @@ link_directories(${BOOST_ROOT}/lib)
add_unit_test(
intelligence_is_v2_ut
"query_request_v2_ut.cc;query_response_v2_ut.cc;intelligence_comp_v2_ut.cc"
"intelligence_is_v2;singleton;shell_cmd;event_is;metric;message;agent_details;connkey;-lboost_regex")
"intelligence_is_v2;singleton;shell_cmd;event_is;metric;messaging;agent_details;connkey;-lboost_regex")

View File

@@ -142,10 +142,9 @@ TEST_F(IntelligenceComponentTestV2, fakeOnlineIntelligenceTest)
"}\n"
);
EXPECT_CALL(
messaging_mock,
sendMessage(true, _, I_Messaging::Method::POST, _, _, _, _, MessageTypeTag::INTELLIGENCE)
).WillOnce(Return(response_str));
EXPECT_CALL(messaging_mock, sendSyncMessage(HTTPMethod::POST, _, _, MessageCategory::INTELLIGENCE, _)
).WillOnce(Return(HTTPResponse(HTTPStatusCode::HTTP_OK, response_str)));
auto maybe_ans = intell->queryIntelligence<Profile>(request);
EXPECT_TRUE(maybe_ans.ok());
auto vec = maybe_ans.unpack();
@@ -285,10 +284,9 @@ TEST_F(IntelligenceComponentTestV2, multiAssetsIntelligenceTest)
"}\n"
);
EXPECT_CALL(
messaging_mock,
sendMessage(true, _, I_Messaging::Method::POST, _, _, _, _, MessageTypeTag::INTELLIGENCE)
).WillOnce(Return(response_str1));
EXPECT_CALL(messaging_mock, sendSyncMessage(HTTPMethod::POST, _, _, MessageCategory::INTELLIGENCE, _)
).WillOnce(Return(HTTPResponse(HTTPStatusCode::HTTP_OK, response_str1)));
auto maybe_ans = intell->queryIntelligence<Profile>(request);
EXPECT_TRUE(maybe_ans.ok());
auto vec = maybe_ans.unpack();
@@ -415,10 +413,9 @@ TEST_F(IntelligenceComponentTestV2, inProgressQueryTest)
"}\n"
);
EXPECT_CALL(
messaging_mock,
sendMessage(true, _, I_Messaging::Method::POST, _, _, _, _, MessageTypeTag::INTELLIGENCE)
).WillOnce(Return(in_progress_response_str)).WillOnce(Return(done_response_str));
EXPECT_CALL(messaging_mock, sendSyncMessage(HTTPMethod::POST, _, _, MessageCategory::INTELLIGENCE, _)
).WillOnce(Return(HTTPResponse(HTTPStatusCode::HTTP_OK, in_progress_response_str))
).WillOnce(Return(HTTPResponse(HTTPStatusCode::HTTP_OK, done_response_str)));
EXPECT_CALL(
mock_ml,
@@ -605,10 +602,8 @@ TEST_F(IntelligenceComponentTestV2, pagingQueryTest)
"}\n"
);
EXPECT_CALL(
messaging_mock,
sendMessage(true, _, I_Messaging::Method::POST, _, _, _, _, MessageTypeTag::INTELLIGENCE)
).WillOnce(Return(paging_in_progress_response_str1));
EXPECT_CALL(messaging_mock, sendSyncMessage(HTTPMethod::POST, _, _, MessageCategory::INTELLIGENCE, _)
).WillOnce(Return(HTTPResponse(HTTPStatusCode::HTTP_OK, paging_in_progress_response_str1)));
request.setAssetsLimit(2);
EXPECT_EQ(request.getAssetsLimit(), 2);
@@ -618,10 +613,8 @@ TEST_F(IntelligenceComponentTestV2, pagingQueryTest)
EXPECT_EQ(vec1.size(), 1);
EXPECT_EQ(request.isPagingFinished(), false);
EXPECT_CALL(
messaging_mock,
sendMessage(true, _, I_Messaging::Method::POST, _, _, _, _, MessageTypeTag::INTELLIGENCE)
).WillOnce(Return(paging_in_progress_response_str2));
EXPECT_CALL(messaging_mock, sendSyncMessage(HTTPMethod::POST, _, _, MessageCategory::INTELLIGENCE, _)
).WillOnce(Return(HTTPResponse(HTTPStatusCode::HTTP_OK, paging_in_progress_response_str2)));
auto maybe_ans2 = intell->queryIntelligence<Profile>(request);
EXPECT_TRUE(maybe_ans2.ok());
@@ -629,10 +622,9 @@ TEST_F(IntelligenceComponentTestV2, pagingQueryTest)
EXPECT_EQ(vec2.size(), 2);
EXPECT_EQ(request.isPagingFinished(), false);
EXPECT_CALL(
messaging_mock,
sendMessage(true, _, I_Messaging::Method::POST, _, _, _, _, MessageTypeTag::INTELLIGENCE)
).WillOnce(Return(paging_done_response_str));
EXPECT_CALL(messaging_mock, sendSyncMessage(HTTPMethod::POST, _, _, MessageCategory::INTELLIGENCE, _)
).WillOnce(Return(HTTPResponse(HTTPStatusCode::HTTP_OK, paging_done_response_str)));
auto maybe_ans3 = intell->queryIntelligence<Profile>(request);
EXPECT_TRUE(maybe_ans3.ok());
auto vec3 = maybe_ans3.unpack();
@@ -840,10 +832,9 @@ TEST_F(IntelligenceComponentTestV2, bulkOnlineIntelligenceTest)
"}\n"
);
Debug::setNewDefaultStdout(&cout);
EXPECT_CALL(
messaging_mock,
sendMessage(true, _, I_Messaging::Method::POST, _, _, _, _, MessageTypeTag::INTELLIGENCE)
).WillOnce(Return(response_str));
EXPECT_CALL(messaging_mock, sendSyncMessage(HTTPMethod::POST, _, _, MessageCategory::INTELLIGENCE, _)
).WillOnce(Return(HTTPResponse(HTTPStatusCode::HTTP_OK, response_str)));
auto maybe_ans = intell->queryIntelligence<Profile>(requests);
EXPECT_TRUE(maybe_ans.ok());
auto vec = maybe_ans.unpack();
@@ -1004,11 +995,9 @@ TEST_F(IntelligenceComponentTestV2, ignoreInProgressQueryTest_2)
"}\n"
);
EXPECT_CALL(
messaging_mock,
sendMessage(true, _, I_Messaging::Method::POST, _, _, _, _, MessageTypeTag::INTELLIGENCE))
.WillOnce(Return(paging_in_progress_response_str))
.WillOnce(Return(paging_done_response_str));
EXPECT_CALL(messaging_mock, sendSyncMessage(HTTPMethod::POST, _, _, MessageCategory::INTELLIGENCE, _))
.WillOnce(Return(HTTPResponse(HTTPStatusCode::HTTP_OK, paging_in_progress_response_str)))
.WillOnce(Return(HTTPResponse(HTTPStatusCode::HTTP_OK, paging_done_response_str)));
I_Intelligence_IS_V2 *intell = Singleton::Consume<I_Intelligence_IS_V2>::by<IntelligenceComponentTestV2>();
QueryRequest request(Condition::EQUALS, "category", "cloud", true, AttributeKeyType::NONE);

View File

@@ -124,6 +124,9 @@ public:
getListeningPort()
).WillRepeatedly(Return(7000));
ON_CALL(mock_details, getFogDomain()).WillByDefault(Return(Maybe<string>(string("fog_domain.com"))));
ON_CALL(mock_details, getFogPort()).WillByDefault(Return(Maybe<uint16_t>(443)));
conf.preload();
intelligence.preload();
intelligence.init();
@@ -175,8 +178,12 @@ TEST_F(IntelligenceInvalidation, sending_public_invalidation)
string invalidation_json;
EXPECT_CALL(
messaging_mock,
sendMessage(false, _, I_Messaging::Method::POST, invalidation_uri, _, _, true, MessageTypeTag::INTELLIGENCE)
).WillOnce(DoAll(SaveArg<1>(&invalidation_json), Return(string())));
sendSyncMessage(HTTPMethod::POST, invalidation_uri, _, MessageCategory::INTELLIGENCE, _)
).WillOnce(DoAll(
SaveArg<2>(&invalidation_json),
Return(HTTPResponse(HTTPStatusCode::HTTP_OK, ""))
));
EXPECT_TRUE(invalidation.report(i_intelligence));
string expected_json =
@@ -216,8 +223,12 @@ TEST_F(IntelligenceInvalidation, sending_private_invalidation)
string invalidation_json;
EXPECT_CALL(
messaging_mock,
sendMessage(false, _, I_Messaging::Method::POST, "127.0.0.1", 9090, _, invalidation_uri, _, _, _)
).WillOnce(DoAll(SaveArg<1>(&invalidation_json), Return(string())));
sendSyncMessage(HTTPMethod::POST, invalidation_uri, _, MessageCategory::INTELLIGENCE, _)
).WillOnce(DoAll(
SaveArg<2>(&invalidation_json),
Return(HTTPResponse(HTTPStatusCode::HTTP_OK, ""))
));
EXPECT_TRUE(invalidation.report(i_intelligence));
string expected_json =
@@ -256,10 +267,10 @@ TEST_F(IntelligenceInvalidation, register_for_invalidation)
string body;
EXPECT_CALL(
messaging_mock,
sendMessage(_, _, _, "127.0.0.1", 9090, _, "/api/v2/intelligence/invalidation/register", _, _, _)
sendSyncMessage(_, "/api/v2/intelligence/invalidation/register", _, _, _)
).WillOnce(DoAll(
SaveArg<1>(&body),
Return(string())
SaveArg<2>(&body),
Return(HTTPResponse(HTTPStatusCode::HTTP_OK, ""))
));
EXPECT_NE(i_intelligence->registerInvalidation(invalidation, callback), 0);
@@ -292,8 +303,8 @@ TEST_F(IntelligenceInvalidation, invalidation_callback)
EXPECT_CALL(
messaging_mock,
sendMessage(_, _, _, "127.0.0.1", 9090, _, "/api/v2/intelligence/invalidation/register", _, _, _)
).WillOnce(Return(string()));
sendSyncMessage(_, "/api/v2/intelligence/invalidation/register", _, _, _)
).WillOnce(Return(HTTPResponse(HTTPStatusCode::HTTP_OK, "")));
EXPECT_NE(i_intelligence->registerInvalidation(invalidation, callback), 0);
@@ -336,8 +347,8 @@ TEST_F(IntelligenceInvalidation, delete_invalidation_callback)
EXPECT_CALL(
messaging_mock,
sendMessage(_, _, _, "127.0.0.1", 9090, _, "/api/v2/intelligence/invalidation/register", _, _, _)
).WillOnce(Return(string()));
sendSyncMessage(_, "/api/v2/intelligence/invalidation/register", _, _, _)
).WillOnce(Return(HTTPResponse(HTTPStatusCode::HTTP_OK, "")));
auto callback_id = i_intelligence->registerInvalidation(invalidation, callback);
i_intelligence->unregisterInvalidation(*callback_id);
@@ -380,8 +391,9 @@ TEST_F(IntelligenceInvalidation, invalidation_short_handling)
EXPECT_CALL(
messaging_mock,
sendMessage(_, _, _, "127.0.0.1", 9090, _, "/api/v2/intelligence/invalidation/register", _, _, _)
).WillOnce(Return(string()));
sendSyncMessage(_, "/api/v2/intelligence/invalidation/register", _, _, _)
).WillOnce(Return(HTTPResponse(HTTPStatusCode::HTTP_OK, "")));
invalidation.startListening(i_intelligence, callback);
invalidation.stopListening(i_intelligence);
@@ -426,18 +438,18 @@ TEST_F(IntelligenceInvalidation, routine_registration)
EXPECT_CALL(
messaging_mock,
sendMessage(_, _, _, "127.0.0.1", 9090, _, "/api/v2/intelligence/invalidation/register", _, _, _)
).WillOnce(Return(string()));
sendSyncMessage(_, "/api/v2/intelligence/invalidation/register", _, _, _)
).WillOnce(Return(HTTPResponse(HTTPStatusCode::HTTP_OK, "")));
i_intelligence->registerInvalidation(invalidation, callback);
string body;
EXPECT_CALL(
messaging_mock,
sendMessage(_, _, _, "127.0.0.1", 9090, _, "/api/v2/intelligence/invalidation/register", _, _, _)
sendSyncMessage(_, "/api/v2/intelligence/invalidation/register", _, _, _)
).WillOnce(DoAll(
SaveArg<1>(&body),
Return(string())
SaveArg<2>(&body),
Return(HTTPResponse(HTTPStatusCode::HTTP_OK, ""))
));
routine();

View File

@@ -78,6 +78,52 @@ TEST(QueryRequestTestV2, QueryTest)
request2.saveToJson(out_ar2);
}
EXPECT_EQ(out2.str(), output_json2);
vector<string> range1;
range1.push_back("224.10.10.16");
range1.push_back("224.10.10.31");
vector<string> range2;
range2.push_back("224.11.10.16");
range2.push_back("224.11.10.31");
QueryRequest request3(Condition::RANGE, "ipv4AddressesRange", range1, true);
request3.addCondition(Condition::RANGE, "ipv4AddressesRange", range2);
string output_json3=
"{\n"
" \"limit\": 20,\n"
" \"fullResponse\": true,\n"
" \"query\": {\n"
" \"operator\": \"and\",\n"
" \"operands\": [\n"
" {\n"
" \"operator\": \"range\",\n"
" \"key\": \"mainAttributes.ipv4AddressesRange\",\n"
" \"value\": [\n"
" \"224.10.10.16\",\n"
" \"224.10.10.31\"\n"
" ]\n"
" },\n"
" {\n"
" \"operator\": \"range\",\n"
" \"key\": \"mainAttributes.ipv4AddressesRange\",\n"
" \"value\": [\n"
" \"224.11.10.16\",\n"
" \"224.11.10.31\"\n"
" ]\n"
" }\n"
" ]\n"
" }\n"
"}";
stringstream out3;
{
cereal::JSONOutputArchive out_ar3(out3);
request3.saveToJson(out_ar3);
}
EXPECT_EQ(out3.str(), output_json3);
}
TEST(QueryRequestTestV2, AttributesTest)

View File

1
core/intelligence_is_v2/intelligence_types_v2.cc Executable file → Normal file
View File

@@ -38,6 +38,7 @@ Intelligence_IS_V2::convertConditionTypeToString(const Condition &condition_type
{Condition::NOT_IN, "notIn"},
{Condition::GREATER_THAN, "greaterThan"},
{Condition::LESS_THAN, "lessThan"},
{Condition::RANGE, "range"},
};
auto condition_str = condition_type_to_string_map.find(condition_type);

21
core/intelligence_is_v2/query_filter_v2.cc Executable file → Normal file
View File

@@ -65,6 +65,16 @@ SerializableQueryFilter::SerializableQueryFilter(
condition_operands.emplace_back(condition_type, key, value);
}
SerializableQueryFilter::SerializableQueryFilter(
Condition condition_type,
const std::string &key,
const vector<string> &value
) {
condition_operands.emplace_back(condition_type, key, value);
}
void
SerializableQueryFilter::save(cereal::JSONOutputArchive &ar) const
{
@@ -97,6 +107,17 @@ SerializableQueryFilter::addCondition(Condition condition_type, const string &ke
condition_operands.emplace_back(condition_type, key, value);
}
void
SerializableQueryFilter::addCondition(Condition condition_type, const string &key, const vector<string> &value)
{
if (queries_operands.size() > 0) {
queries_operands.emplace_back(condition_type, key, value);
return;
}
if (condition_operands.size() == 1 && operator_type == Operator::NONE) operator_type = Operator::AND;
condition_operands.emplace_back(condition_type, key, value);
}
void
SerializableQueryFilter::saveCondition(cereal::JSONOutputArchive &ar) const
{

24
core/intelligence_is_v2/query_request_v2.cc Executable file → Normal file
View File

@@ -23,7 +23,7 @@ using namespace Intelligence_IS_V2;
USE_DEBUG_FLAG(D_INTELLIGENCE);
static const EnumArray<ObjectType, string> object_type_to_string_array{ "asset", "zone", "configuration" };
static const EnumArray<ObjectType, string> object_type_to_string_array{"asset", "zone", "configuration", "shortLived"};
BulkQueryRequest::BulkQueryRequest(QueryRequest &_request, int _index)
:
@@ -70,6 +70,18 @@ QueryRequest::QueryRequest(
full_response = full_reponse;
}
QueryRequest::QueryRequest(
Condition condition_type,
const string &key,
const vector<string> &value,
bool full_reponse,
AttributeKeyType attribute_type
) {
query = SerializableQueryFilter(condition_type, createAttributeString(key, attribute_type), value);
assets_limit = default_assets_limit;
full_response = full_reponse;
}
Maybe<string>
QueryRequest::convertObjectTypeToString() const
{
@@ -161,6 +173,16 @@ QueryRequest::addCondition (
query.addCondition(condition_type, createAttributeString(key, attribute_type), value);
}
void
QueryRequest::addCondition (
Condition condition_type,
const string &key,
const vector<string> &value,
AttributeKeyType attribute_type
) {
query.addCondition(condition_type, createAttributeString(key, attribute_type), value);
}
void
QueryRequest::setRequestedAttr(const string &attr, AttributeKeyType attr_type)
{

0
core/intelligence_is_v2/requested_attributes_v2.cc Executable file → Normal file
View File