First release of open-appsec source code

This commit is contained in:
roybarda
2022-10-26 19:33:19 +03:00
parent 3883109caf
commit a883352f79
1353 changed files with 276290 additions and 1 deletions

View File

@@ -0,0 +1,7 @@
include_directories(include)
add_library(
intelligence_is_v2 intelligence_comp_v2.cc query_request_v2.cc
intelligence_types_v2.cc query_filter_v2.cc requested_attributes_v2.cc query_types_v2.cc
)
add_subdirectory(intelligence_is_v2_ut)

View File

@@ -0,0 +1,145 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "intelligence_comp_v2.h"
#include <fstream>
#include "cache.h"
#include "config.h"
#include "table.h"
#include "intelligence_is_v2/query_response_v2.h"
using namespace std;
using namespace chrono;
using namespace Intelligence_IS_V2;
USE_DEBUG_FLAG(D_INTELLIGENCE);
class IntelligenceComponentV2::Impl
:
Singleton::Provide<I_Intelligence_IS_V2>::From<IntelligenceComponentV2>
{
public:
class OfflineIntelligeceHandler
{
public:
void
init()
{
filesystem_prefix = getFilesystemPathConfig();
dbgTrace(D_INTELLIGENCE) << "OfflineIntelligeceHandler init. file systen prefix: " << filesystem_prefix;
offline_intelligence_path = getConfigurationWithDefault<string>(
filesystem_prefix + "/conf/offline/intelligence",
"intelligence",
"offline intelligence path"
);
}
Maybe<string>
getValueByIdentifier(const string &identifier) const
{
string asset_file_path = offline_intelligence_path + "/" + identifier;
ifstream asset_info(asset_file_path);
if (!asset_info.is_open()) {
return genError("Could not open file: " + asset_file_path);
}
stringstream info_txt;
info_txt << asset_info.rdbuf();
asset_info.close();
return info_txt.str();
}
private:
string filesystem_prefix = "";
string offline_intelligence_path = "";
};
void
init()
{
offline_mode_only = getConfigurationWithDefault<bool>(false, "intelligence", "offline intelligence only");
registerConfigLoadCb([&]() {
offline_mode_only = getConfigurationWithDefault<bool>(false, "intelligence", "offline intelligence only");
});
offline_intelligence.init();
message = Singleton::Consume<I_Messaging>::by<IntelligenceComponentV2>();
timer = Singleton::Consume<I_TimeGet>::by<IntelligenceComponentV2>();
mainloop = Singleton::Consume<I_MainLoop>::by<IntelligenceComponentV2>();
}
I_Messaging *
getMessaging() const override
{
return message != NULL ? message : Singleton::Consume<I_Messaging>::by<IntelligenceComponentV2>();
}
I_TimeGet *
getTimer() const override
{
return timer != NULL ? timer : Singleton::Consume<I_TimeGet>::by<IntelligenceComponentV2>();
}
I_MainLoop *
getMainloop() const override
{
return mainloop != NULL ? mainloop : Singleton::Consume<I_MainLoop>::by<IntelligenceComponentV2>();
}
Maybe<string>
getOfflineInfoString(const SerializableQueryFilter &query) const override
{
string ip_attr_key = "mainAttributes.ip";
string identifier_value = move(query.getConditionValueByKey(ip_attr_key));
if (identifier_value == "") {
return genError("could not find IP main attribute in the given query.");
}
return offline_intelligence.getValueByIdentifier(identifier_value);
}
bool
getIsOfflineOnly() const override
{
return offline_mode_only;
}
private:
OfflineIntelligeceHandler offline_intelligence;
bool offline_mode_only = false;
I_Messaging *message = nullptr;
I_TimeGet *timer = nullptr;
I_MainLoop *mainloop = nullptr;
};
IntelligenceComponentV2::IntelligenceComponentV2()
:
Component("IntelligenceComponentV2"),
pimpl(make_unique<Impl>())
{}
IntelligenceComponentV2::~IntelligenceComponentV2() {}
void IntelligenceComponentV2::init() { pimpl->init(); }
void
IntelligenceComponentV2::preload()
{
registerExpectedConfiguration<string>("intelligence", "offline intelligence path");
registerExpectedConfiguration<bool>("intelligence", "offline intelligence only");
registerExpectedConfiguration<uint>("intelligence", "maximum request overall time");
registerExpectedConfiguration<uint>("intelligence", "maximum request lap time");
registerExpectedConfigFile("agent-intelligence", Config::ConfigFileType::Policy);
}

View File

@@ -0,0 +1,7 @@
file(COPY offline_intelligence_files_v2 DESTINATION .)
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")

View File

@@ -0,0 +1,678 @@
#include "intelligence_comp_v2.h"
#include "intelligence_is_v2/intelligence_query_v2.h"
#include "read_attribute_v2.h"
#include "cptest.h"
#include "singleton.h"
#include "config.h"
#include "config_component.h"
#include "mock/mock_messaging.h"
#include "mock/mock_mainloop.h"
#include "mock/mock_time_get.h"
using namespace std;
using namespace testing;
using namespace chrono;
using namespace Intelligence_IS_V2;
USE_DEBUG_FLAG(D_METRICS);
class IntelligenceComponentTestV2
:
public Test,
Singleton::Consume<I_Intelligence_IS_V2>
{
public:
IntelligenceComponentTestV2()
{
debug_output.clear();
Debug::setNewDefaultStdout(&debug_output);
Debug::setUnitTestFlag(D_METRICS, Debug::DebugLevel::TRACE);
setConfiguration<bool>(false, string("metric"), string("fogMetricSendEnable"));
EXPECT_CALL(
mock_ml,
addRecurringRoutine(I_MainLoop::RoutineType::System, chrono::microseconds(600000000), _, _, _))
.WillRepeatedly(DoAll(SaveArg<2>(&routine), Return(0)));
string offline_intel_path = cptestFnameInExeDir("offline_intelligence_files_v2");
setConfiguration<string>(offline_intel_path, string("intelligence"), string("offline intelligence path"));
intelligence.preload();
intelligence.init();
}
~IntelligenceComponentTestV2()
{
Debug::setNewDefaultStdout(&cout);
}
stringstream debug_output;
StrictMock<MockMainLoop> mock_ml;
NiceMock<MockTimeGet> mock_time;
::Environment env;
ConfigComponent conf;
StrictMock<MockMessaging> messaging_mock;
IntelligenceComponentV2 intelligence;
I_MainLoop::Routine routine;
};
class Profile
{
public:
Profile() {}
DataString getUser() const{ return user;}
DataString getPhase() const{ return phase;}
template <typename Archive>
void
serialize(Archive &ar)
{
try {
ReadAttribute<DataString>("user", user).serialize(ar);
} catch (const exception &e) {}
try {
ReadAttribute<DataString>("phase", phase).serialize(ar);
} catch (const exception &e) {}
}
private:
DataString user;
DataString phase;
};
TEST_F(IntelligenceComponentTestV2, fakeOnlineIntelligenceTest)
{
I_Intelligence_IS_V2 *intell = Singleton::Consume<I_Intelligence_IS_V2>::by<IntelligenceComponentTestV2>();
QueryRequest request(Condition::EQUALS, "category", "cloud", true);
string response_str(
"{\n"
" \"assetCollections\": [\n"
" {\n"
" \"schemaVersion\": 1,\n"
" \"assetType\": \"workload-cloud-fake-online-test\",\n"
" \"assetTypeSchemaVersion\": 1,\n"
" \"permissionType\": \"tenant\",\n"
" \"permissionGroupId\": \"fake-online-test-group\",\n"
" \"name\": \"fake-online-test-asset\",\n"
" \"class\": \"workload\",\n"
" \"category\": \"cloud\",\n"
" \"family\": \"fake-online-test\",\n"
" \"mainAttributes\": {\n"
" \"ipv4Addresses\": \"1.1.1.1\",\n"
" \"phase\": \"testing\"\n"
" },\n"
" \"sources\": [\n"
" {\n"
" \"tenantId\": \"175bb55c-e36f-4ac5-a7b1-7afa1229aa00\",\n"
" \"sourceId\": \"54d7de10-7b2e-4505-955b-cc2c2c7aaa00\",\n"
" \"assetId\": \"50255c3172b4fb7fda93025f0bfaa7abefd1\",\n"
" \"ttl\": 120,\n"
" \"expirationTime\": \"2020-07-29T11:21:12.253Z\",\n"
" \"confidence\": 500,\n"
" \"attributes\": {\n"
" \"phase\": \"testing\",\n"
" \"user\": \"Omry\",\n"
" \"owners\": { \"names\": [ { \"name1\": \"Bob\", \"name2\": \"Alice\" } ] }\n"
" }\n"
" }\n"
" ]\n"
" }\n"
" ],\n"
" \"status\": \"done\",\n"
" \"totalNumAssets\": 2,\n"
" \"cursor\": \"start\"\n"
"}\n"
);
EXPECT_CALL(
messaging_mock,
sendMessage(true, _, I_Messaging::Method::POST, _, _, _, _, MessageTypeTag::INTELLIGENCE)
).WillOnce(Return(response_str));
auto maybe_ans = intell->queryIntelligence<Profile>(request);
EXPECT_TRUE(maybe_ans.ok());
auto vec = maybe_ans.unpack();
EXPECT_EQ(vec.size(), 1u);
auto iter = vec.begin();
EXPECT_EQ(iter->getData().begin()->getUser().toString(), "Omry");
EXPECT_EQ(iter->getData().begin()->getPhase().toString(), "testing");
}
TEST_F(IntelligenceComponentTestV2, multiAssetsIntelligenceTest)
{
I_Intelligence_IS_V2 *intell = Singleton::Consume<I_Intelligence_IS_V2>::by<IntelligenceComponentTestV2>();
QueryRequest request(Condition::EQUALS, "category", "cloud", true);
string response_str1(
"{\n"
" \"assetCollections\": [\n"
" {\n"
" \"schemaVersion\": 1,\n"
" \"assetType\": \"workload-cloud-fake-online-test\",\n"
" \"assetTypeSchemaVersion\": 1,\n"
" \"permissionType\": \"tenant\",\n"
" \"permissionGroupId\": \"fake-online-test-group\",\n"
" \"name\": \"fake-online-test-asset-1\",\n"
" \"class\": \"workload\",\n"
" \"category\": \"cloud\",\n"
" \"family\": \"fake-online-test\",\n"
" \"mainAttributes\": {\n"
" \"ipv4Addresses\": \"1.1.1.1\",\n"
" \"phase\": \"testing\"\n"
" },\n"
" \"sources\": [\n"
" {\n"
" \"tenantId\": \"175bb55c-e36f-4ac5-a7b1-7afa1229aa00\",\n"
" \"sourceId\": \"54d7de10-7b2e-4505-955b-cc2c2c7aaa00\",\n"
" \"assetId\": \"50255c3172b4fb7fda93025f0bfaa7abefd0\",\n"
" \"ttl\": 120,\n"
" \"expirationTime\": \"2020-07-29T11:21:12.253Z\",\n"
" \"confidence\": 500,\n"
" \"attributes\": {\n"
" \"phase\": \"fake online test1\",\n"
" \"user\": \"Omry\",\n"
" \"owners\": { \"names\": [ { \"name1\": \"no\", \"name2\": \"one\" } ] }\n"
" }\n"
" },\n"
" {\n"
" \"tenantId\": \"175bb55c-e36f-4ac5-a7b1-7afa1229aa01\",\n"
" \"sourceId\": \"54d7de10-7b2e-4505-955b-cc2c2c7aaa01\",\n"
" \"assetId\": \"50255c3172b4fb7fda93025f0bfaa7abefd1\",\n"
" \"ttl\": 120,\n"
" \"expirationTime\": \"2020-07-29T11:21:12.253Z\",\n"
" \"confidence\": 500,\n"
" \"attributes\": {\n"
" \"phase\": \"fake online test1\",\n"
" \"user\": \"Max\",\n"
" \"owners\": { \"names\": [ { \"name1\": \"every\", \"name2\": \"one\" } ] }\n"
" }\n"
" },\n"
" {\n"
" \"tenantId\": \"175bb55c-e36f-4ac5-a7b1-7afa1229aa01\",\n"
" \"sourceId\": \"54d7de10-7b2e-4505-955b-cc2c2c7aaa01\",\n"
" \"assetId\": \"50255c3172b4fb7fda93025f0bfaa7abefd1\",\n"
" \"ttl\": 120,\n"
" \"expirationTime\": \"2020-07-29T11:21:12.253Z\",\n"
" \"confidence\": 500,\n"
" \"attributes\": {\n"
" \"phase\": \"fake online test1\",\n"
" \"user\": \"Roy\",\n"
" \"owners\": { \"names\": [ { \"name1\": \"Aviv\", \"name2\": \"Cochavi\" } ] }\n"
" }\n"
" }\n"
" ]\n"
" },\n"
" {\n"
" \"schemaVersion\": 1,\n"
" \"assetType\": \"workload-cloud-fake-online-test\",\n"
" \"assetTypeSchemaVersion\": 1,\n"
" \"permissionType\": \"tenant\",\n"
" \"permissionGroupId\": \"fake-online-test-group\",\n"
" \"name\": \"fake-online-test-asset-2\",\n"
" \"class\": \"workload\",\n"
" \"category\": \"cloud\",\n"
" \"family\": \"fake-online-test\",\n"
" \"mainAttributes\": {\n"
" \"ipv4Addresses\": \"1.1.1.2\",\n"
" \"phase\": \"testing\"\n"
" },\n"
" \"sources\": [\n"
" {\n"
" \"tenantId\": \"175bb55c-e36f-4ac5-a7b1-7afa1229aa02\",\n"
" \"sourceId\": \"54d7de10-7b2e-4505-955b-cc2c2c7aaa02\",\n"
" \"assetId\": \"50255c3172b4fb7fda93025f0bfaa7abefd2\",\n"
" \"ttl\": 120,\n"
" \"expirationTime\": \"2020-07-29T11:21:12.253Z\",\n"
" \"confidence\": 500,\n"
" \"attributes\": {\n"
" \"phase\": \"fake online test2\",\n"
" \"user\": \"Daniel\",\n"
" \"owners\": { \"names\": [ { \"name1\": \"Omry\", \"name2\": \"David\" } ] }\n"
" }\n"
" }\n"
" ]\n"
" },\n"
" {\n"
" \"schemaVersion\": 1,\n"
" \"assetType\": \"workload-cloud-fake-online-test\",\n"
" \"assetTypeSchemaVersion\": 1,\n"
" \"permissionType\": \"tenant\",\n"
" \"permissionGroupId\": \"fake-online-test-group\",\n"
" \"name\": \"fake-online-test-asset-2\",\n"
" \"class\": \"workload\",\n"
" \"category\": \"cloud\",\n"
" \"family\": \"fake-online-test\",\n"
" \"mainAttributes\": {\n"
" \"ipv4Addresses\": \"1.1.1.3\",\n"
" \"phase\": \"testing\"\n"
" },\n"
" \"sources\": [\n"
" {\n"
" \"tenantId\": \"175bb55c-e36f-4ac5-a7b1-7afa1229aa03\",\n"
" \"sourceId\": \"54d7de10-7b2e-4505-955b-cc2c2c7aaa03\",\n"
" \"assetId\": \"50255c3172b4fb7fda93025f0bfaa7abefd3\",\n"
" \"ttl\": 120,\n"
" \"expirationTime\": \"2020-07-29T11:21:12.253Z\",\n"
" \"confidence\": 500,\n"
" \"attributes\": {\n"
" \"phase\": \"fake online test3\",\n"
" \"user\": \"Oren\",\n"
" \"owners\": { \"names\": [ { \"name1\": \"Omry\", \"name2\": \"David\" } ] }\n"
" }\n"
" }\n"
" ]\n"
" }\n"
" ],\n"
" \"status\": \"done\",\n"
" \"totalNumAssets\": 2\n"
"}\n"
);
EXPECT_CALL(
messaging_mock,
sendMessage(true, _, I_Messaging::Method::POST, _, _, _, _, MessageTypeTag::INTELLIGENCE)
).WillOnce(Return(response_str1));
auto maybe_ans = intell->queryIntelligence<Profile>(request);
EXPECT_TRUE(maybe_ans.ok());
auto vec = maybe_ans.unpack();
EXPECT_EQ(vec.size(), 3);
auto iter = vec.begin();
auto asset_sources_vec = iter->getData();
auto data_it = asset_sources_vec.begin();
EXPECT_EQ(data_it->getUser().toString(), "Omry");
EXPECT_EQ(data_it->getPhase().toString(), "fake online test1");
data_it++;
EXPECT_EQ(data_it->getUser().toString(), "Max");
EXPECT_EQ(data_it->getPhase().toString(), "fake online test1");
data_it++;
EXPECT_EQ(data_it->getUser().toString(), "Roy");
EXPECT_EQ(data_it->getPhase().toString(), "fake online test1");
iter++;
EXPECT_EQ(iter->getData().begin()->getUser().toString(), "Daniel");
EXPECT_EQ(iter->getData().begin()->getPhase().toString(), "fake online test2");
iter++;
EXPECT_EQ(iter->getData().begin()->getUser().toString(), "Oren");
EXPECT_EQ(iter->getData().begin()->getPhase().toString(), "fake online test3");
}
TEST_F(IntelligenceComponentTestV2, inProgressQueryTest)
{
I_Intelligence_IS_V2 *intell = Singleton::Consume<I_Intelligence_IS_V2>::by<IntelligenceComponentTestV2>();
QueryRequest request(Condition::EQUALS, "category", "cloud", true);
string in_progress_response_str(
"{\n"
" \"assetCollections\": [\n"
" {\n"
" \"schemaVersion\": 1,\n"
" \"assetType\": \"workload-cloud-fake-online-test\",\n"
" \"assetTypeSchemaVersion\": 1,\n"
" \"permissionType\": \"tenant\",\n"
" \"permissionGroupId\": \"fake-online-test-group\",\n"
" \"name\": \"fake-online-test-asset\",\n"
" \"class\": \"workload\",\n"
" \"category\": \"cloud\",\n"
" \"family\": \"fake-online-test\",\n"
" \"mainAttributes\": {\n"
" \"ipv4Addresses\": \"1.1.1.1\",\n"
" \"phase\": \"testing\"\n"
" },\n"
" \"sources\": [\n"
" {\n"
" \"tenantId\": \"175bb55c-e36f-4ac5-a7b1-7afa1229aa00\",\n"
" \"sourceId\": \"54d7de10-7b2e-4505-955b-cc2c2c7aaa00\",\n"
" \"assetId\": \"50255c3172b4fb7fda93025f0bfaa7abefd1\",\n"
" \"ttl\": 120,\n"
" \"expirationTime\": \"2020-07-29T11:21:12.253Z\",\n"
" \"confidence\": 500,\n"
" \"attributes\": {\n"
" \"phase\": \"fake online test\",\n"
" \"user\": \"Omry\",\n"
" \"owners\": { \"names\": [ { \"name1\": \"no\", \"name2\": \"one\" } ] }\n"
" }\n"
" }\n"
" ]\n"
" }\n"
" ],\n"
" \"status\": \"inProgress\",\n"
" \"totalNumAssets\": 1,\n"
" \"cursor\": \"start\"\n"
"}\n"
);
string done_response_str(
"{\n"
" \"assetCollections\": [\n"
" {\n"
" \"schemaVersion\": 1,\n"
" \"assetType\": \"workload-cloud-fake-online-test\",\n"
" \"assetTypeSchemaVersion\": 1,\n"
" \"permissionType\": \"tenant\",\n"
" \"permissionGroupId\": \"fake-online-test-group\",\n"
" \"name\": \"fake-online-test-asset\",\n"
" \"class\": \"workload\",\n"
" \"category\": \"cloud\",\n"
" \"family\": \"fake-online-test\",\n"
" \"mainAttributes\": {\n"
" \"ipv4Addresses\": \"1.1.1.1\",\n"
" \"phase\": \"testing\"\n"
" },\n"
" \"sources\": [\n"
" {\n"
" \"tenantId\": \"175bb55c-e36f-4ac5-a7b1-7afa1229aa00\",\n"
" \"sourceId\": \"54d7de10-7b2e-4505-955b-cc2c2c7aaa00\",\n"
" \"assetId\": \"50255c3172b4fb7fda93025f0bfaa7abefd1\",\n"
" \"ttl\": 120,\n"
" \"expirationTime\": \"2020-07-29T11:21:12.253Z\",\n"
" \"confidence\": 500,\n"
" \"attributes\": {\n"
" \"phase\": \"fake online test\",\n"
" \"user\": \"Omry\",\n"
" \"owners\": { \"names\": [ { \"name1\": \"no\", \"name2\": \"one\" } ] }\n"
" }\n"
" },\n"
" {\n"
" \"tenantId\": \"175bb55c-e36f-4ac5-a7b1-7afa1229bb11\",\n"
" \"sourceId\": \"54d7de10-7b2e-4505-955b-cc2c2c7bbb11\",\n"
" \"assetId\": \"cb068860528cb6bfb000cc35e79f11aeefed2\",\n"
" \"ttl\": 120,\n"
" \"expirationTime\": \"2020-07-29T11:21:12.253Z\",\n"
" \"confidence\": 600,\n"
" \"attributes\": {\n"
" \"phase\": \"fake online test\",\n"
" \"user\": \"Max\",\n"
" \"owners\": { \"names\": [ { \"name1\": \"Bob\", \"name2\": \"Alice\" } ] }\n"
" }\n"
" }\n"
" ]\n"
" }\n"
" ],\n"
" \"status\": \"done\",\n"
" \"totalNumAssets\": 2\n"
"}\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(
mock_ml,
yield(true)
);
auto maybe_ans = intell->queryIntelligence<Profile>(request);
EXPECT_TRUE(maybe_ans.ok());
auto vec = maybe_ans.unpack();
EXPECT_EQ(vec.size(), 1u);
vector<AssetReply<Profile>>::iterator assets_iter = vec.begin();
vector<SerializableAssetSource<Profile>>::const_iterator sources_iter = assets_iter->getSources().begin();
EXPECT_EQ(sources_iter->getAttributes().begin()->getUser().toString(), "Omry");
sources_iter++;
EXPECT_EQ(sources_iter->getAttributes().begin()->getUser().toString(), "Max");
}
TEST_F(IntelligenceComponentTestV2, pagingQueryTest)
{
I_Intelligence_IS_V2 *intell = Singleton::Consume<I_Intelligence_IS_V2>::by<IntelligenceComponentTestV2>();
QueryRequest request(Condition::EQUALS, "category", "cloud", true);
request.activatePaging();
string paging_done_response_str(
"{\n"
" \"assetCollections\": [\n"
" {\n"
" \"schemaVersion\": 1,\n"
" \"assetType\": \"workload-cloud-fake-online-test\",\n"
" \"assetTypeSchemaVersion\": 1,\n"
" \"permissionType\": \"tenant\",\n"
" \"permissionGroupId\": \"fake-online-test-group\",\n"
" \"name\": \"fake-online-test-asset\",\n"
" \"class\": \"workload\",\n"
" \"category\": \"cloud\",\n"
" \"family\": \"fake-online-test\",\n"
" \"mainAttributes\": {\n"
" \"ipv4Addresses\": \"1.1.1.1\",\n"
" \"phase\": \"testing\"\n"
" },\n"
" \"sources\": [\n"
" {\n"
" \"tenantId\": \"175bb55c-e36f-4ac5-a7b1-7afa1229aa00\",\n"
" \"sourceId\": \"54d7de10-7b2e-4505-955b-cc2c2c7aaa00\",\n"
" \"assetId\": \"50255c3172b4fb7fda93025f0bfaa7abefd1\",\n"
" \"ttl\": 120,\n"
" \"expirationTime\": \"2020-07-29T11:21:12.253Z\",\n"
" \"confidence\": 500,\n"
" \"attributes\": {\n"
" \"phase\": \"fake online test\",\n"
" \"user\": \"Omry\",\n"
" \"owners\": { \"names\": [ { \"name1\": \"Bob\", \"name2\": \"Alice\" } ] }\n"
" }\n"
" },\n"
" {\n"
" \"tenantId\": \"175bb55c-e36f-4ac5-a7b1-7afa1229bb11\",\n"
" \"sourceId\": \"54d7de10-7b2e-4505-955b-cc2c2c7bbb11\",\n"
" \"assetId\": \"cb068860528cb6bfb000cc35e79f11aeefed2\",\n"
" \"ttl\": 120,\n"
" \"expirationTime\": \"2020-07-29T11:21:12.253Z\",\n"
" \"confidence\": 600,\n"
" \"attributes\": {\n"
" \"phase\": \"fake online test\",\n"
" \"user\": \"Max\",\n"
" \"owners\": { \"names\": [ { \"name1\": \"Bob\", \"name2\": \"Alice\" } ] }\n"
" }\n"
" }\n"
" ]\n"
" }\n"
" ],\n"
" \"status\": \"done\",\n"
" \"totalNumAssets\": 2,\n"
" \"cursor\": \"abcd\"\n"
"}\n"
);
string paging_in_progress_response_str1(
"{\n"
" \"assetCollections\": [\n"
" {\n"
" \"schemaVersion\": 1,\n"
" \"assetType\": \"workload-cloud-fake-online-test\",\n"
" \"assetTypeSchemaVersion\": 1,\n"
" \"permissionType\": \"tenant\",\n"
" \"permissionGroupId\": \"fake-online-test-group\",\n"
" \"name\": \"fake-online-test-asset1\",\n"
" \"class\": \"workload\",\n"
" \"category\": \"cloud\",\n"
" \"family\": \"fake-online-test1\",\n"
" \"mainAttributes\": {\n"
" \"ipv4Addresses\": \"1.1.1.1\",\n"
" \"phase\": \"testing\"\n"
" },\n"
" \"sources\": [\n"
" {\n"
" \"tenantId\": \"175bb55c-e36f-4ac5-a7b1-7afa1229aa00\",\n"
" \"sourceId\": \"54d7de10-7b2e-4505-955b-cc2c2c7aaa00\",\n"
" \"assetId\": \"50255c3172b4fb7fda93025f0bfaa7abefd1\",\n"
" \"ttl\": 120,\n"
" \"expirationTime\": \"2020-07-29T11:21:12.253Z\",\n"
" \"confidence\": 500,\n"
" \"attributes\": {\n"
" \"phase\": \"fake online test1\",\n"
" \"user\": \"Omry\",\n"
" \"owners\": { \"names\": [ { \"name1\": \"Bob\", \"name2\": \"Alice\" } ] }\n"
" }\n"
" }\n"
" ]\n"
" }\n"
" ],\n"
" \"status\": \"inProgress\",\n"
" \"totalNumAssets\": 2,\n"
" \"cursor\": \"abcd\"\n"
"}\n"
);
string paging_in_progress_response_str2(
"{\n"
" \"assetCollections\": [\n"
" {\n"
" \"schemaVersion\": 1,\n"
" \"assetType\": \"workload-cloud-fake-online-test\",\n"
" \"assetTypeSchemaVersion\": 1,\n"
" \"permissionType\": \"tenant\",\n"
" \"permissionGroupId\": \"fake-online-test-group\",\n"
" \"name\": \"fake-online-test-asset1\",\n"
" \"class\": \"workload\",\n"
" \"category\": \"cloud\",\n"
" \"family\": \"fake-online-test1\",\n"
" \"mainAttributes\": {\n"
" \"ipv4Addresses\": \"1.1.1.1\",\n"
" \"phase\": \"testing\"\n"
" },\n"
" \"sources\": [\n"
" {\n"
" \"tenantId\": \"175bb55c-e36f-4ac5-a7b1-7afa1229aa00\",\n"
" \"sourceId\": \"54d7de10-7b2e-4505-955b-cc2c2c7aaa00\",\n"
" \"assetId\": \"50255c3172b4fb7fda93025f0bfaa7abefd1\",\n"
" \"ttl\": 120,\n"
" \"expirationTime\": \"2020-07-29T11:21:12.253Z\",\n"
" \"confidence\": 500,\n"
" \"attributes\": {\n"
" \"phase\": \"fake online test1\",\n"
" \"user\": \"Omry\",\n"
" \"owners\": { \"names\": [ { \"name1\": \"Bob\", \"name2\": \"Alice\" } ] }\n"
" }\n"
" }\n"
" ]\n"
" },\n"
" {\n"
" \"schemaVersion\": 1,\n"
" \"assetType\": \"workload-cloud-fake-online-test\",\n"
" \"assetTypeSchemaVersion\": 1,\n"
" \"permissionType\": \"tenant\",\n"
" \"permissionGroupId\": \"fake-online-test-group\",\n"
" \"name\": \"fake-online-test-asset2\",\n"
" \"class\": \"workload\",\n"
" \"category\": \"cloud\",\n"
" \"family\": \"fake-online-test2\",\n"
" \"mainAttributes\": {\n"
" \"ipv4Addresses\": \"1.1.1.2\",\n"
" \"phase\": \"testing\"\n"
" },\n"
" \"sources\": [\n"
" {\n"
" \"tenantId\": \"175bb55c-e36f-4ac5-a7b1-7afa1229aa00\",\n"
" \"sourceId\": \"54d7de10-7b2e-4505-955b-cc2c2c7aaa00\",\n"
" \"assetId\": \"50255c3172b4fb7fda93025f0bfaa7abefd1\",\n"
" \"ttl\": 120,\n"
" \"expirationTime\": \"2020-07-29T11:21:12.253Z\",\n"
" \"confidence\": 500,\n"
" \"attributes\": {\n"
" \"phase\": \"fake online test2\",\n"
" \"user\": \"Max\",\n"
" \"owners\": { \"names\": [ { \"name1\": \"Bob\", \"name2\": \"Alice\" } ] }\n"
" }\n"
" }\n"
" ]\n"
" }\n"
" ],\n"
" \"status\": \"done\",\n"
" \"totalNumAssets\": 2,\n"
" \"cursor\": \"efgh\"\n"
"}\n"
);
EXPECT_CALL(
messaging_mock,
sendMessage(true, _, I_Messaging::Method::POST, _, _, _, _, MessageTypeTag::INTELLIGENCE)
).WillOnce(Return(paging_in_progress_response_str1));
request.setAssetsLimit(2);
EXPECT_EQ(request.getAssetsLimit(), 2);
auto maybe_ans1 = intell->queryIntelligence<Profile>(request);
EXPECT_TRUE(maybe_ans1.ok());
auto vec1 = maybe_ans1.unpack();
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));
auto maybe_ans2 = intell->queryIntelligence<Profile>(request);
EXPECT_TRUE(maybe_ans2.ok());
auto vec2 = maybe_ans2.unpack();
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));
auto maybe_ans3 = intell->queryIntelligence<Profile>(request);
EXPECT_TRUE(maybe_ans3.ok());
auto vec3 = maybe_ans3.unpack();
EXPECT_EQ(vec3.size(), 1);
EXPECT_EQ(request.isPagingFinished(), true);
vector<AssetReply<Profile>>::iterator assets_iter = vec3.begin();
vector<SerializableAssetSource<Profile>>::const_iterator sources_iter = assets_iter->getSources().begin();
EXPECT_EQ(sources_iter->getAttributes().begin()->getUser().toString(), "Omry");
}
TEST_F(IntelligenceComponentTestV2, offlineIntelligenceTest)
{
setConfiguration<bool>(true, string("intelligence"), string("offline intelligence only"));
intelligence.init();
I_Intelligence_IS_V2 *intell = Singleton::Consume<I_Intelligence_IS_V2>::by<IntelligenceComponentTestV2>();
QueryRequest request(Condition::EQUALS, "ip", "1.2.3.4", true);
auto maybe_ans = intell->queryIntelligence<Profile>(request);
ASSERT_TRUE(maybe_ans.ok());
vector<AssetReply<Profile>> vec = maybe_ans.unpack();
vector<AssetReply<Profile>>::iterator assets_iter = vec.begin();
EXPECT_EQ(assets_iter->getAssetSchemaVersion(), 1);
EXPECT_EQ(assets_iter->getAssetType(), "workload-cloud-ip");
EXPECT_EQ(assets_iter->getAssetTypeSchemaVersion(), 1);
EXPECT_EQ(assets_iter->getAssetPermissionGroupId(), "offline-group-id");
EXPECT_EQ(assets_iter->getAssetName(), "offline-asset");
EXPECT_EQ(assets_iter->getAssetClass(), "workload");
EXPECT_EQ(assets_iter->getAssetCategory(), "cloud");
EXPECT_EQ(assets_iter->getAssetFamily(), "offline family");
EXPECT_EQ(assets_iter->getAssetGroup(), "offline testing");
EXPECT_EQ(assets_iter->getAssetOrder(), "");
EXPECT_EQ(assets_iter->getAssetKind(), "");
map<string, vector<string>> attributes_map = assets_iter->getMainAttributes();
EXPECT_EQ(attributes_map["ip"].front(), "1.2.3.4");
vector<SerializableAssetSource<Profile>>::const_iterator sources_iter = assets_iter->getSources().begin();
EXPECT_EQ(sources_iter->getTenantId(), "175bb55c-e36f-4ac5-a7b1-7afa1229aa00");
EXPECT_EQ(sources_iter->getSourceId(), "54d7de10-7b2e-4505-955b-cc2c2c7aaa00");
EXPECT_EQ(sources_iter->getAssetId(), "50255c3172b4fb7fda93025f0bfaa7abefd1");
EXPECT_EQ(sources_iter->getTTL(), chrono::seconds(120));
EXPECT_EQ(sources_iter->getExpirationTime(), "2010-05-15T21:50:12.253Z");
EXPECT_EQ(sources_iter->getConfidence(), 700);
EXPECT_EQ(sources_iter->getAttributes().begin()->getUser().toString(), "Omry");
EXPECT_EQ(sources_iter->getAttributes().begin()->getPhase().toString(), "offline test");
sources_iter++;
EXPECT_EQ(sources_iter->getTenantId(), "175bb55c-e36f-4ac5-a7b1-7afa1229bb11");
EXPECT_EQ(sources_iter->getSourceId(), "54d7de10-7b2e-4505-955b-cc2c2c7bbb11");
EXPECT_EQ(sources_iter->getAssetId(), "cb068860528cb6bfb000cc35e79f11aeefed2");
EXPECT_EQ(sources_iter->getTTL(), chrono::seconds(120));
EXPECT_EQ(sources_iter->getExpirationTime(), "2010-05-15T21:50:12.253Z");
EXPECT_EQ(sources_iter->getConfidence(), 600);
EXPECT_EQ(sources_iter->getAttributes().begin()->getUser().toString(), "Max");
EXPECT_EQ(sources_iter->getAttributes().begin()->getPhase().toString(), "offline test");
}

View File

@@ -0,0 +1,49 @@
{
"assetCollections": [
{
"schemaVersion": 1,
"assetType": "workload-cloud-ip",
"assetTypeSchemaVersion": 1,
"permissionType": "tenant",
"permissionGroupId": "offline-group-id",
"name": "offline-asset",
"class": "workload",
"category": "cloud",
"family": "offline family",
"group": "offline testing",
"order": "",
"kind": "",
"mainAttributes": {
"ip": "1.2.3.4"
},
"sources": [
{
"tenantId": "175bb55c-e36f-4ac5-a7b1-7afa1229aa00",
"sourceId": "54d7de10-7b2e-4505-955b-cc2c2c7aaa00",
"assetId": "50255c3172b4fb7fda93025f0bfaa7abefd1",
"ttl": 120,
"expirationTime": "2010-05-15T21:50:12.253Z",
"confidence": 700,
"attributes": {
"user": "Omry",
"phase": "offline test"
}
},
{
"tenantId": "175bb55c-e36f-4ac5-a7b1-7afa1229bb11",
"sourceId": "54d7de10-7b2e-4505-955b-cc2c2c7bbb11",
"assetId": "cb068860528cb6bfb000cc35e79f11aeefed2",
"ttl": 120,
"expirationTime": "2010-05-15T21:50:12.253Z",
"confidence": 600,
"attributes": {
"user": "Max",
"phase": "offline test"
}
}
]
}
],
"status": "done",
"totalNumAssets": 2
}

View File

@@ -0,0 +1,440 @@
#include "intelligence_is_v2/query_request_v2.h"
#include "cptest.h"
using namespace std;
using namespace testing;
TEST(QueryRequestTestV2, QueryTest)
{
QueryRequest request(Condition::EQUALS, "phase", "testing", true);
SerializableQueryFilter request_query1 = request.getQuery();
vector<SerializableQueryCondition> request_operands1 = request_query1.getConditionOperands();
SerializableQueryCondition request_condition = *request_operands1.begin();
EXPECT_EQ(request_query1.getOperator(), Operator::NONE);
EXPECT_EQ(request_condition.getKey(), "mainAttributes.phase");
EXPECT_EQ(request_condition.getValue(), "testing");
request.addCondition(Condition::EQUALS, "user1", "Omry");
request.addCondition(Condition::EQUALS, "user2", "Max");
SerializableQueryFilter request_query2 = request.getQuery();
vector<SerializableQueryCondition> request_operands2 = request_query2.getConditionOperands();
vector<SerializableQueryCondition>::iterator it = request_operands2.begin();
it++;
EXPECT_EQ(request_query2.getOperator(), Operator::AND);
EXPECT_EQ(it->getKey(), "mainAttributes.user1");
EXPECT_EQ(it->getValue(), "Omry");
it++;
EXPECT_EQ(it->getKey(), "mainAttributes.user2");
EXPECT_EQ(it->getValue(), "Max");
string output_json =
"{\n"
" \"limit\": 20,\n"
" \"fullResponse\": true,\n"
" \"query\": {\n"
" \"operator\": \"and\",\n"
" \"operands\": [\n"
" {\n"
" \"operator\": \"equals\",\n"
" \"key\": \"mainAttributes.phase\",\n"
" \"value\": \"testing\"\n"
" },\n"
" {\n"
" \"operator\": \"equals\",\n"
" \"key\": \"mainAttributes.user1\",\n"
" \"value\": \"Omry\"\n"
" },\n"
" {\n"
" \"operator\": \"equals\",\n"
" \"key\": \"mainAttributes.user2\",\n"
" \"value\": \"Max\"\n"
" }\n"
" ]\n"
" }\n"
"}";
stringstream out;
{
cereal::JSONOutputArchive out_ar(out);
request.saveToJson(out_ar);
}
EXPECT_EQ(out.str(), output_json);
}
TEST(QueryRequestTestV2, AttributesTest)
{
QueryRequest request(Condition::EQUALS, "phase", "testing", true);
SerializableAttributesMap request_attributes_map1 = request.getRequestedAttributes();
EXPECT_TRUE(request_attributes_map1.isRequestedAttributesMapEmpty());
request.setRequestedAttr("countryName");
SerializableAttributesMap request_attributes_map2 = request.getRequestedAttributes();
EXPECT_EQ(request_attributes_map2.getAttributeByKey("attributes.countryName"), 500);
request.setRequestedAttr("reputationSeverity", 30);
SerializableAttributesMap request_attributes_map3 = request.getRequestedAttributes();
EXPECT_EQ(request_attributes_map3.getAttributeByKey("attributes.reputationSeverity"), 30);
string output_json =
"{\n"
" \"limit\": 20,\n"
" \"fullResponse\": true,\n"
" \"query\": {\n"
" \"operator\": \"equals\",\n"
" \"key\": \"mainAttributes.phase\",\n"
" \"value\": \"testing\"\n"
" },\n"
" \"requestedAttributes\": [\n"
" {\n"
" \"key\": \"attributes.reputationSeverity\",\n"
" \"minConfidence\": 30\n"
" },\n"
" {\n"
" \"key\": \"attributes.countryName\",\n"
" \"minConfidence\": 500\n"
" }\n"
" ]\n"
"}";
stringstream out;
{
cereal::JSONOutputArchive out_ar(out);
request.saveToJson(out_ar);
}
EXPECT_EQ(out.str(), output_json);
}
TEST(QueryRequestTestV2, AndQueryTest)
{
QueryRequest request1(Condition::EQUALS, "phase", "testing1", true);
QueryRequest request2(Condition::EQUALS, "phase", "testing2", true);
QueryRequest and_request = request1 && request2;
stringstream out;
{
cereal::JSONOutputArchive out_ar(out);
and_request.saveToJson(out_ar);
}
string output_json =
"{\n"
" \"limit\": 20,\n"
" \"fullResponse\": true,\n"
" \"query\": {\n"
" \"operator\": \"and\",\n"
" \"operands\": [\n"
" {\n"
" \"operator\": \"equals\",\n"
" \"key\": \"mainAttributes.phase\",\n"
" \"value\": \"testing1\"\n"
" },\n"
" {\n"
" \"operator\": \"equals\",\n"
" \"key\": \"mainAttributes.phase\",\n"
" \"value\": \"testing2\"\n"
" }\n"
" ]\n"
" }\n"
"}";
EXPECT_EQ(out.str(), output_json);
}
TEST(QueryRequestTestV2, OrQueryTest)
{
QueryRequest request1(Condition::EQUALS, "phase", "testing1", true);
QueryRequest request2(Condition::EQUALS, "phase", "testing2", true);
QueryRequest and_request = request1 || request2;
stringstream out;
{
cereal::JSONOutputArchive out_ar(out);
and_request.saveToJson(out_ar);
}
string output_json =
"{\n"
" \"limit\": 20,\n"
" \"fullResponse\": true,\n"
" \"query\": {\n"
" \"operator\": \"or\",\n"
" \"operands\": [\n"
" {\n"
" \"operator\": \"equals\",\n"
" \"key\": \"mainAttributes.phase\",\n"
" \"value\": \"testing1\"\n"
" },\n"
" {\n"
" \"operator\": \"equals\",\n"
" \"key\": \"mainAttributes.phase\",\n"
" \"value\": \"testing2\"\n"
" }\n"
" ]\n"
" }\n"
"}";
EXPECT_EQ(out.str(), output_json);
}
TEST(QueryRequestTestV2, AndWithConditionQueryTest)
{
QueryRequest request1(Condition::EQUALS, "phase", "testing1", true);
QueryRequest request2(Condition::EQUALS, "phase", "testing2", true);
QueryRequest and_with_cond_request = request1 && request2;
and_with_cond_request.addCondition(Condition::EQUALS, "user1", "Oren");
stringstream out;
{
cereal::JSONOutputArchive out_ar(out);
and_with_cond_request.saveToJson(out_ar);
}
string output_json =
"{\n"
" \"limit\": 20,\n"
" \"fullResponse\": true,\n"
" \"query\": {\n"
" \"operator\": \"and\",\n"
" \"operands\": [\n"
" {\n"
" \"operator\": \"equals\",\n"
" \"key\": \"mainAttributes.phase\",\n"
" \"value\": \"testing1\"\n"
" },\n"
" {\n"
" \"operator\": \"equals\",\n"
" \"key\": \"mainAttributes.phase\",\n"
" \"value\": \"testing2\"\n"
" },\n"
" {\n"
" \"operator\": \"equals\",\n"
" \"key\": \"mainAttributes.user1\",\n"
" \"value\": \"Oren\"\n"
" }\n"
" ]\n"
" }\n"
"}";
EXPECT_EQ(out.str(), output_json);
}
TEST(QueryRequestTestV2, SemiComplexQueryTest)
{
QueryRequest request1(Condition::EQUALS, "phase", "testing1", true);
QueryRequest request2(Condition::EQUALS, "phase", "testing2", true);
QueryRequest request3(Condition::EQUALS, "CountryCode", "USA", true);
QueryRequest semi_complex_query_request = (request1 || request2) && request3;
stringstream out;
{
cereal::JSONOutputArchive out_ar(out);
semi_complex_query_request.saveToJson(out_ar);
}
string output_json =
"{\n"
" \"limit\": 20,\n"
" \"fullResponse\": true,\n"
" \"query\": {\n"
" \"operator\": \"and\",\n"
" \"operands\": [\n"
" {\n"
" \"operator\": \"or\",\n"
" \"operands\": [\n"
" {\n"
" \"operator\": \"equals\",\n"
" \"key\": \"mainAttributes.phase\",\n"
" \"value\": \"testing1\"\n"
" },\n"
" {\n"
" \"operator\": \"equals\",\n"
" \"key\": \"mainAttributes.phase\",\n"
" \"value\": \"testing2\"\n"
" }\n"
" ]\n"
" },\n"
" {\n"
" \"operator\": \"equals\",\n"
" \"key\": \"mainAttributes.CountryCode\",\n"
" \"value\": \"USA\"\n"
" }\n"
" ]\n"
" }\n"
"}";
EXPECT_EQ(out.str(), output_json);
}
TEST(QueryRequestTestV2, ComplexQueryTest)
{
QueryRequest request1(Condition::EQUALS, "phase", "testing1", true);
QueryRequest request2(Condition::EQUALS, "phase", "testing2", true);
QueryRequest request3(Condition::EQUALS, "CountryCode", "USA", true);
QueryRequest request4(Condition::EQUALS, "CountryCode", "IL", true);
QueryRequest request5 = request1 && request2;
QueryRequest request6 = request3 || request4;
QueryRequest complex_query_request = request5 || request6;
stringstream out;
{
cereal::JSONOutputArchive out_ar(out);
complex_query_request.saveToJson(out_ar);
}
string output_json =
"{\n"
" \"limit\": 20,\n"
" \"fullResponse\": true,\n"
" \"query\": {\n"
" \"operator\": \"or\",\n"
" \"operands\": [\n"
" {\n"
" \"operator\": \"and\",\n"
" \"operands\": [\n"
" {\n"
" \"operator\": \"equals\",\n"
" \"key\": \"mainAttributes.phase\",\n"
" \"value\": \"testing1\"\n"
" },\n"
" {\n"
" \"operator\": \"equals\",\n"
" \"key\": \"mainAttributes.phase\",\n"
" \"value\": \"testing2\"\n"
" }\n"
" ]\n"
" },\n"
" {\n"
" \"operator\": \"or\",\n"
" \"operands\": [\n"
" {\n"
" \"operator\": \"equals\",\n"
" \"key\": \"mainAttributes.CountryCode\",\n"
" \"value\": \"USA\"\n"
" },\n"
" {\n"
" \"operator\": \"equals\",\n"
" \"key\": \"mainAttributes.CountryCode\",\n"
" \"value\": \"IL\"\n"
" }\n"
" ]\n"
" }\n"
" ]\n"
" }\n"
"}";
complex_query_request.addCondition(Condition::EQUALS, "user1", "Oren");
stringstream out_with_new_condition;
{
cereal::JSONOutputArchive out_ar(out_with_new_condition);
complex_query_request.saveToJson(out_ar);
}
string output_json_with_condition =
"{\n"
" \"limit\": 20,\n"
" \"fullResponse\": true,\n"
" \"query\": {\n"
" \"operator\": \"or\",\n"
" \"operands\": [\n"
" {\n"
" \"operator\": \"and\",\n"
" \"operands\": [\n"
" {\n"
" \"operator\": \"equals\",\n"
" \"key\": \"mainAttributes.phase\",\n"
" \"value\": \"testing1\"\n"
" },\n"
" {\n"
" \"operator\": \"equals\",\n"
" \"key\": \"mainAttributes.phase\",\n"
" \"value\": \"testing2\"\n"
" }\n"
" ]\n"
" },\n"
" {\n"
" \"operator\": \"or\",\n"
" \"operands\": [\n"
" {\n"
" \"operator\": \"equals\",\n"
" \"key\": \"mainAttributes.CountryCode\",\n"
" \"value\": \"USA\"\n"
" },\n"
" {\n"
" \"operator\": \"equals\",\n"
" \"key\": \"mainAttributes.CountryCode\",\n"
" \"value\": \"IL\"\n"
" }\n"
" ]\n"
" },\n"
" {\n"
" \"operator\": \"equals\",\n"
" \"key\": \"mainAttributes.user1\",\n"
" \"value\": \"Oren\"\n"
" }\n"
" ]\n"
" }\n"
"}";
EXPECT_EQ(out_with_new_condition.str(), output_json_with_condition);
}
TEST(QueryRequestTestV2, OneLinerComplexQueryTest)
{
QueryRequest complex_query_request = (
(
QueryRequest(Condition::EQUALS, "phase", "testing1", true) &&
QueryRequest(Condition::EQUALS, "phase", "testing2", true)
) || (
QueryRequest(Condition::EQUALS, "CountryCode", "USA", true) ||
QueryRequest(Condition::EQUALS, "CountryCode", "IL", true)
)
);
stringstream out;
{
cereal::JSONOutputArchive out_ar(out);
complex_query_request.saveToJson(out_ar);
}
string output_json =
"{\n"
" \"limit\": 20,\n"
" \"fullResponse\": true,\n"
" \"query\": {\n"
" \"operator\": \"or\",\n"
" \"operands\": [\n"
" {\n"
" \"operator\": \"and\",\n"
" \"operands\": [\n"
" {\n"
" \"operator\": \"equals\",\n"
" \"key\": \"mainAttributes.phase\",\n"
" \"value\": \"testing1\"\n"
" },\n"
" {\n"
" \"operator\": \"equals\",\n"
" \"key\": \"mainAttributes.phase\",\n"
" \"value\": \"testing2\"\n"
" }\n"
" ]\n"
" },\n"
" {\n"
" \"operator\": \"or\",\n"
" \"operands\": [\n"
" {\n"
" \"operator\": \"equals\",\n"
" \"key\": \"mainAttributes.CountryCode\",\n"
" \"value\": \"USA\"\n"
" },\n"
" {\n"
" \"operator\": \"equals\",\n"
" \"key\": \"mainAttributes.CountryCode\",\n"
" \"value\": \"IL\"\n"
" }\n"
" ]\n"
" }\n"
" ]\n"
" }\n"
"}";
EXPECT_EQ(out.str(), output_json);
}

View File

@@ -0,0 +1,464 @@
#include "intelligence_is_v2/query_response_v2.h"
#include "intelligence_is_v2/asset_source_v2.h"
#include "intelligence_is_v2/intelligence_types_v2.h"
#include "intelligence_is_v2/data_string_v2.h"
#include "cptest.h"
#include "debug.h"
#include "intelligence_comp_v2.h"
#include "read_attribute_v2.h"
#include "mock/mock_mainloop.h"
#include "mock/mock_time_get.h"
using namespace std;
using namespace testing;
using namespace Intelligence_IS_V2;
USE_DEBUG_FLAG(D_INTELLIGENCE);
class stringData1
{
public:
stringData1() {}
DataString getData() const{ return data;}
DataString getData1() const{ return data1;}
template <typename Archive>
void
serialize(Archive &ar)
{
try{
ReadAttribute<DataString>("color", data).serialize(ar);
} catch (exception &e) {
dbgError(D_INTELLIGENCE) << "Requested attribute was not found: color";
}
try {
ReadAttribute<DataString>("user", data1).serialize(ar);
} catch (const exception &e) {
dbgError(D_INTELLIGENCE) << "Requested attribute was not found: user";
}
}
private:
DataString data;
DataString data1;
};
TEST(QueryResponseTestV2, ReadAttributeTest)
{
StrictMock<MockMainLoop> mock_ml;
NiceMock<MockTimeGet> time_get;
IntelligenceComponentV2 new_intelligence;
DataString data;
ReadAttribute<DataString> obj("user", data);
string data_str(
"{"
" \"net\": \"7.7.7.0/24\","
" \"user\": \"Omry\""
"}"
);
stringstream ss(data_str);
{
cereal::JSONInputArchive ar(ss);
obj.serialize(ar);
}
EXPECT_EQ(obj.getData().toString(), "Omry");
}
TEST(QueryResponseTestV2, stringData1Test)
{
StrictMock<MockMainLoop> mock_ml;
NiceMock<MockTimeGet> time_get;
IntelligenceComponentV2 new_intelligence;
DataString data;
stringData1 obj;
string data_str(
"{\n"
" \"color\": \"red\",\n"
" \"owners\": { \"names\": [ { \"name1\": \"Bob\", \"name2\": \"Alice\" } ] },\n"
" \"user\": \"Omry\"\n"
"}"
);
stringstream ss(data_str);
{
cereal::JSONInputArchive ar(ss);
obj.serialize(ar);
}
EXPECT_EQ(obj.getData().toString(), "red");
EXPECT_EQ(obj.getData1().toString(), "Omry");
}
TEST(QueryResponseTestV2, QueryResponseTestV2)
{
StrictMock<MockMainLoop> mock_ml;
NiceMock<MockTimeGet> time_get;
IntelligenceComponentV2 new_intelligence;
DataString data;
IntelligenceQueryResponse<stringData1> obj;
string data_str(
"{\n"
" \"assetCollections\": [\n"
" {\n"
" \"schemaVersion\": 1,\n"
" \"assetType\": \"workload-cloud-ip\",\n"
" \"assetTypeSchemaVersion\": 1,\n"
" \"permissionType\": \"tenant\",\n"
" \"permissionGroupId\": \"some-group-id\",\n"
" \"name\": \"[1.1.1.1]\",\n"
" \"class\": \"workload\",\n"
" \"category\": \"cloud\",\n"
" \"family\": \"ip\",\n"
" \"group\": \"\",\n"
" \"order\": \"\",\n"
" \"kind\": \"\",\n"
" \"mainAttributes\": {\n"
" \"ipv4Addresses\": [\n"
" \"1.1.1.1\",\n"
" \"2.2.2.2\"\n"
" ],\n"
" \"phase\": \"testing\"\n"
" },\n"
" \"sources\": [\n"
" {\n"
" \"tenantId\": \"175bb55c-e36f-4ac5-a7b1-7afa1229aa00\",\n"
" \"sourceId\": \"54d7de10-7b2e-4505-955b-cc2c2c7aaa00\",\n"
" \"assetId\": \"50255c3172b4fb7fda93025f0bfaa7abefd1\",\n"
" \"ttl\": 120,\n"
" \"expirationTime\": \"2020-07-29T11:21:12.253Z\",\n"
" \"confidence\": 500,\n"
" \"attributes\": {\n"
" \"color\": \"red\",\n"
" \"user\": \"Omry\",\n"
" \"owners\": { \"names\": [ { \"name1\": \"Bob\", \"name2\": \"Alice\" } ] }\n"
" }\n"
" },\n"
" {\n"
" \"tenantId\": \"175bb55c-e36f-4ac5-a7b1-7afa1229bb11\",\n"
" \"sourceId\": \"54d7de10-7b2e-4505-955b-cc2c2c7bbb11\",\n"
" \"assetId\": \"cb068860528cb6bfb000cc35e79f11aeefed2\",\n"
" \"ttl\": 120,\n"
" \"expirationTime\": \"2020-07-29T11:21:12.253Z\",\n"
" \"confidence\": 600,\n"
" \"attributes\": {\n"
" \"color\": \"white\",\n"
" \"user\": \"Max\",\n"
" \"owners\": { \"names\": [ { \"name1\": \"Bob\", \"name2\": \"Alice\" } ] }\n"
" }\n"
" }\n"
" ]\n"
" }\n"
" ],\n"
" \"status\": \"done\",\n"
" \"totalNumAssets\": 2,\n"
" \"cursor\": \"start\"\n"
"}\n"
);
stringstream ss(data_str);
{
cereal::JSONInputArchive ar(ss);
obj.loadFromJson(ar);
}
EXPECT_EQ(obj.getAmountOfAssets(), 2);
EXPECT_EQ(obj.getResponseStatus(), ResponseStatus::DONE);
EXPECT_EQ(obj.getData().begin()->getAssetSchemaVersion(), 1);
EXPECT_EQ(obj.getData().begin()->getAssetType(), "workload-cloud-ip");
EXPECT_EQ(obj.getData().begin()->getAssetTypeSchemaVersion(), 1);
EXPECT_EQ(obj.getData().begin()->getAssetPermissionGroupId(), "some-group-id");
EXPECT_EQ(obj.getData().begin()->getAssetName(), "[1.1.1.1]");
EXPECT_EQ(obj.getData().begin()->getAssetClass(), "workload");
EXPECT_EQ(obj.getData().begin()->getAssetCategory(), "cloud");
EXPECT_EQ(obj.getData().begin()->getAssetFamily(), "ip");
EXPECT_EQ(obj.getData().begin()->getAssetGroup(), "");
EXPECT_EQ(obj.getData().begin()->getAssetOrder(), "");
EXPECT_EQ(obj.getData().begin()->getAssetKind(), "");
map<string, vector<string>> attributes_map = obj.getData().begin()->getMainAttributes();
vector<string>::const_iterator ipv4_it = attributes_map["ipv4Addresses"].begin();
EXPECT_EQ(*ipv4_it, "1.1.1.1");
ipv4_it++;
EXPECT_EQ(*ipv4_it, "2.2.2.2");
vector<string>::const_iterator phase_it = attributes_map["phase"].begin();
EXPECT_EQ(*phase_it, "testing");
vector<SerializableAssetSource<stringData1>>::const_iterator soucres_it =
obj.getData().begin()->getSources().begin();
EXPECT_EQ(soucres_it->getTenantId(), "175bb55c-e36f-4ac5-a7b1-7afa1229aa00");
EXPECT_EQ(soucres_it->getSourceId(), "54d7de10-7b2e-4505-955b-cc2c2c7aaa00");
EXPECT_EQ(soucres_it->getAssetId(), "50255c3172b4fb7fda93025f0bfaa7abefd1");
EXPECT_EQ(soucres_it->getTTL(), chrono::seconds(120));
EXPECT_EQ(soucres_it->getExpirationTime(), "2020-07-29T11:21:12.253Z");
EXPECT_EQ(soucres_it->getConfidence(), 500);
EXPECT_EQ(soucres_it->getAttributes().begin()->getData().toString(), "red");
EXPECT_EQ(soucres_it->getAttributes().begin()->getData1().toString(), "Omry");
soucres_it++;
EXPECT_EQ(soucres_it->getTenantId(), "175bb55c-e36f-4ac5-a7b1-7afa1229bb11");
EXPECT_EQ(soucres_it->getSourceId(), "54d7de10-7b2e-4505-955b-cc2c2c7bbb11");
EXPECT_EQ(soucres_it->getAssetId(), "cb068860528cb6bfb000cc35e79f11aeefed2");
EXPECT_EQ(soucres_it->getTTL(), chrono::seconds(120));
EXPECT_EQ(soucres_it->getExpirationTime(), "2020-07-29T11:21:12.253Z");
EXPECT_EQ(soucres_it->getConfidence(), 600);
EXPECT_EQ(soucres_it->getAttributes().begin()->getData().toString(), "white");
EXPECT_EQ(soucres_it->getAttributes().begin()->getData1().toString(), "Max");
vector<AssetReply<stringData1>> asset_collections = obj.getData();
EXPECT_EQ(asset_collections.size(), 1);
vector<AssetReply<stringData1>>::const_iterator asset_collections_it = asset_collections.begin();
vector<stringData1> asset_sources = asset_collections_it->getData();
EXPECT_EQ(asset_sources.size(), 2);
vector<stringData1>::iterator asset_sources_it = asset_sources.begin();
EXPECT_EQ(asset_sources_it->getData().toString(), "red");
EXPECT_EQ(asset_sources_it->getData1().toString(), "Omry");
asset_sources_it++;
EXPECT_EQ(asset_sources_it->getData().toString(), "white");
EXPECT_EQ(asset_sources_it->getData1().toString(), "Max");
}
TEST(QueryResponseTestV2, MainAttributesTestV2)
{
StrictMock<MockMainLoop> mock_ml;
NiceMock<MockTimeGet> time_get;
IntelligenceComponentV2 new_intelligence;
DataString data;
IntelligenceQueryResponse<stringData1> obj;
string string_attribute(
"{\n"
" \"assetCollections\": [\n"
" {\n"
" \"schemaVersion\": 1,\n"
" \"assetType\": \"workload-cloud-ip\",\n"
" \"assetTypeSchemaVersion\": 1,\n"
" \"permissionType\": \"tenant\",\n"
" \"permissionGroupId\": \"some-group-id\",\n"
" \"name\": \"[1.1.1.1]\",\n"
" \"class\": \"workload\",\n"
" \"category\": \"cloud\",\n"
" \"family\": \"ip\",\n"
" \"group\": \"\",\n"
" \"order\": \"\",\n"
" \"kind\": \"\",\n"
" \"mainAttributes\": {\n"
" \"team\": \"hapoel\"\n"
" },\n"
" \"sources\": [\n"
" {\n"
" \"tenantId\": \"175bb55c-e36f-4ac5-a7b1-7afa1229aa00\",\n"
" \"sourceId\": \"54d7de10-7b2e-4505-955b-cc2c2c7aaa00\",\n"
" \"assetId\": \"50255c3172b4fb7fda93025f0bfaa7abefd1\",\n"
" \"ttl\": 120,\n"
" \"expirationTime\": \"2020-07-29T11:21:12.253Z\",\n"
" \"confidence\": 500,\n"
" \"attributes\": {\n"
" \"color\": \"red\",\n"
" \"user\": \"Omry\",\n"
" \"owners\": { \"names\": [ { \"name1\": \"Bob\", \"name2\": \"Alice\" } ] }\n"
" }\n"
" }\n"
" ]\n"
" }\n"
" ],\n"
" \"status\": \"done\",\n"
" \"totalNumAssets\": 1,\n"
" \"cursor\": \"start\"\n"
"}\n"
);
stringstream ss(string_attribute);
{
cereal::JSONInputArchive ar(ss);
obj.loadFromJson(ar);
}
map<string, vector<string>> attributes_map = obj.getData().begin()->getMainAttributes();
vector<string>::const_iterator team_it = attributes_map["team"].begin();
EXPECT_EQ(*team_it, "hapoel");
string many_strings_attribute(
"{\n"
" \"assetCollections\": [\n"
" {\n"
" \"schemaVersion\": 1,\n"
" \"assetType\": \"workload-cloud-ip\",\n"
" \"assetTypeSchemaVersion\": 1,\n"
" \"permissionType\": \"tenant\",\n"
" \"permissionGroupId\": \"some-group-id\",\n"
" \"name\": \"[1.1.1.1]\",\n"
" \"class\": \"workload\",\n"
" \"category\": \"cloud\",\n"
" \"family\": \"ip\",\n"
" \"group\": \"\",\n"
" \"order\": \"\",\n"
" \"kind\": \"\",\n"
" \"mainAttributes\": {\n"
" \"team\": \"hapoel\",\n"
" \"city\": \"tel-aviv\",\n"
" \"color\": \"red\"\n"
" },\n"
" \"sources\": [\n"
" {\n"
" \"tenantId\": \"175bb55c-e36f-4ac5-a7b1-7afa1229aa00\",\n"
" \"sourceId\": \"54d7de10-7b2e-4505-955b-cc2c2c7aaa00\",\n"
" \"assetId\": \"50255c3172b4fb7fda93025f0bfaa7abefd1\",\n"
" \"ttl\": 120,\n"
" \"expirationTime\": \"2020-07-29T11:21:12.253Z\",\n"
" \"confidence\": 500,\n"
" \"attributes\": {\n"
" \"color\": \"red\",\n"
" \"user\": \"Omry\",\n"
" \"owners\": { \"names\": [ { \"name1\": \"Bob\", \"name2\": \"Alice\" } ] }\n"
" }\n"
" }\n"
" ]\n"
" }\n"
" ],\n"
" \"status\": \"done\",\n"
" \"totalNumAssets\": 1,\n"
" \"cursor\": \"start\"\n"
"}\n"
);
stringstream ss2(many_strings_attribute);
{
cereal::JSONInputArchive ar(ss2);
obj.loadFromJson(ar);
}
map<string, vector<string>> attributes_map2 = obj.getData().begin()->getMainAttributes();
vector<string>::const_iterator team_it2 = attributes_map2["team"].begin();
EXPECT_EQ(*team_it2, "hapoel");
vector<string>::const_iterator city_it = attributes_map2["city"].begin();
EXPECT_EQ(*city_it, "tel-aviv");
vector<string>::const_iterator color_it = attributes_map2["color"].begin();
EXPECT_EQ(*color_it, "red");
string strings_vector_attribute(
"{\n"
" \"assetCollections\": [\n"
" {\n"
" \"schemaVersion\": 1,\n"
" \"assetType\": \"workload-cloud-ip\",\n"
" \"assetTypeSchemaVersion\": 1,\n"
" \"permissionType\": \"tenant\",\n"
" \"permissionGroupId\": \"some-group-id\",\n"
" \"name\": \"[1.1.1.1]\",\n"
" \"class\": \"workload\",\n"
" \"category\": \"cloud\",\n"
" \"family\": \"ip\",\n"
" \"group\": \"\",\n"
" \"order\": \"\",\n"
" \"kind\": \"\",\n"
" \"mainAttributes\": {\n"
" \"team\": [\n"
" \"hapoel\",\n"
" \"tel-aviv\"\n"
" ]\n"
" },\n"
" \"sources\": [\n"
" {\n"
" \"tenantId\": \"175bb55c-e36f-4ac5-a7b1-7afa1229aa00\",\n"
" \"sourceId\": \"54d7de10-7b2e-4505-955b-cc2c2c7aaa00\",\n"
" \"assetId\": \"50255c3172b4fb7fda93025f0bfaa7abefd1\",\n"
" \"ttl\": 120,\n"
" \"expirationTime\": \"2020-07-29T11:21:12.253Z\",\n"
" \"confidence\": 500,\n"
" \"attributes\": {\n"
" \"color\": \"red\",\n"
" \"user\": \"Omry\",\n"
" \"owners\": { \"names\": [ { \"name1\": \"Bob\", \"name2\": \"Alice\" } ] }\n"
" }\n"
" }\n"
" ]\n"
" }\n"
" ],\n"
" \"status\": \"done\",\n"
" \"totalNumAssets\": 1,\n"
" \"cursor\": \"start\"\n"
"}\n"
);
stringstream ss3(strings_vector_attribute);
{
cereal::JSONInputArchive ar(ss3);
obj.loadFromJson(ar);
}
map<string, vector<string>> attributes_map3 = obj.getData().begin()->getMainAttributes();
vector<string>::const_iterator team_it3 = attributes_map3["team"].begin();
EXPECT_EQ(*team_it3, "hapoel");
team_it3++;
EXPECT_EQ(*team_it3, "tel-aviv");
}
TEST(QueryResponseTestV2, IntelligenceFailTest)
{
StrictMock<MockMainLoop> mock_ml;
NiceMock<MockTimeGet> time_get;
IntelligenceComponentV2 new_intelligence;
DataString data;
IntelligenceQueryResponse<stringData1> obj;
string status_fail_data_str(
"{\n"
" \"assetCollections\": [\n"
" {\n"
" \"schemaVersion\": 1,\n"
" \"assetType\": \"workload-cloud-ip\",\n"
" \"assetTypeSchemaVersion\": 1,\n"
" \"permissionType\": \"tenant\",\n"
" \"permissionGroupId\": \"fail-group-id\",\n"
" \"name\": \"[1.1.1.1]\",\n"
" \"class\": \"workload\",\n"
" \"category\": \"cloud\",\n"
" \"family\": \"ip\",\n"
" \"group\": \"\",\n"
" \"order\": \"\",\n"
" \"kind\": \"\",\n"
" \"mainAttributes\": {\n"
" \"team\": [\n"
" \"FAIL\"\n"
" ]\n"
" },\n"
" \"sources\": [\n"
" {\n"
" \"tenantId\": \"175bb55c-e36f-4ac5-a7b1-7afa1229aa00\",\n"
" \"sourceId\": \"54d7de10-7b2e-4505-955b-cc2c2c7aaa00\",\n"
" \"assetId\": \"50255c3172b4fb7fda93025f0bfaa7abefd1\",\n"
" \"ttl\": 120,\n"
" \"expirationTime\": \"2020-07-29T11:21:12.253Z\",\n"
" \"confidence\": 500,\n"
" \"attributes\": {\n"
" \"color\": \"status\",\n"
" \"user\": \"fail\"\n"
" }\n"
" }\n"
" ]\n"
" }\n"
" ],\n"
" \"status\": \"ERROR!!!\",\n"
" \"totalNumAssets\": 1,\n"
" \"cursor\": \"start\"\n"
"}\n"
);
string error_str = "Received illegal Response Status. Status: ERROR!!!";
stringstream ss(status_fail_data_str);
{
cereal::JSONInputArchive ar(ss);
try {
obj.loadFromJson(ar);
} catch (exception &e) {
EXPECT_EQ(e.what(), error_str);
}
}
}

View File

@@ -0,0 +1,91 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "intelligence_is_v2/intelligence_types_v2.h"
using namespace std;
using namespace Intelligence_IS_V2;
struct EnumClassHash
{
template <typename T>
std::size_t operator()(T t) const
{
return static_cast<std::size_t>(t);
}
};
const string &
Intelligence_IS_V2::convertConditionTypeToString(const Condition &condition_type)
{
static const unordered_map<Condition, string, EnumClassHash> condition_type_to_string_map = {
{Condition::EQUALS, "equals"},
{Condition::NOT_EQUALS, "notEquals"},
{Condition::MATCH, "match"},
{Condition::STARTS_WITH, "startsWith"},
{Condition::CONTAINS, "contains"},
{Condition::IN, "in"},
{Condition::NOT_IN, "notIn"},
};
auto condition_str = condition_type_to_string_map.find(condition_type);
if (condition_str != condition_type_to_string_map.end()) {
return condition_str->second;
}
throw IntelligenceException("Received illegal Condition Type.");
}
const string &
Intelligence_IS_V2::convertOperationTypeToString(const Operator &operation_type)
{
static const unordered_map<Operator, string, EnumClassHash> operation_type_to_string_map = {
{Operator::AND, "and"},
{Operator::OR, "or"}
};
if (operation_type_to_string_map.find(operation_type) != operation_type_to_string_map.end()) {
return operation_type_to_string_map.at(operation_type);
}
if (operation_type == Operator::NONE) throw IntelligenceException("Received illegal \'NONE\' operation Type.");
throw IntelligenceException("Received illegal Operation Type.");
}
string
Intelligence_IS_V2::createAttributeString(const string &key, AttributeKeyType attribute_type)
{
string attribute_string;
switch (attribute_type) {
case AttributeKeyType::MAIN:
attribute_string = "mainAttributes." + key;
return attribute_string;
case AttributeKeyType::REGULAR:
attribute_string = "attributes." + key;
return attribute_string;
case AttributeKeyType::NONE:
attribute_string = key;
return attribute_string;
}
throw IntelligenceException("Received illegal Attribute Type.");
return attribute_string;
}
ResponseStatus
Intelligence_IS_V2::convertStringToResponseStatus(const string &status)
{
if (status == "done") return ResponseStatus::DONE;
if (status == "inProgress") return ResponseStatus::IN_PROGRESS;
throw IntelligenceException("Received illegal Response Status. Status: " + status);
}

View File

@@ -0,0 +1,141 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "intelligence_is_v2/query_filter_v2.h"
#include "intelligence_is_v2/intelligence_types_v2.h"
#include "cereal/archives/json.hpp"
#include "cereal/types/string.hpp"
#include "debug.h"
using namespace std;
using namespace Intelligence_IS_V2;
USE_DEBUG_FLAG(D_INTELLIGENCE);
void
SerializableQueryCondition::save(cereal::JSONOutputArchive &ar) const
{
ar(
cereal::make_nvp("operator", string(convertConditionTypeToString(condition_type))),
cereal::make_nvp("key", key),
cereal::make_nvp("value", value)
);
}
SerializableQueryFilter::SerializableQueryFilter(Condition condition_type, const string &key, const string &value)
{
condition_operands.push_back(SerializableQueryCondition(condition_type, key, value));
}
void
SerializableQueryFilter::save(cereal::JSONOutputArchive &ar) const
{
if (operator_type == Operator::NONE) {
saveCondition(ar);
} else {
saveOperation(ar);
}
}
void
SerializableQueryFilter::addCondition(Condition condition_type, const string &key, const string &value)
{
if (queries_operands.size() > 0) {
SerializableQueryFilter new_query_filter(condition_type, key, value);
queries_operands.push_back(new_query_filter);
return;
}
if (condition_operands.size() == 1 && operator_type == Operator::NONE) {
operator_type = Operator::AND;
}
SerializableQueryCondition cond(condition_type, key, value);
condition_operands.push_back(cond);
}
void
SerializableQueryFilter::saveCondition(cereal::JSONOutputArchive &ar) const
{
SerializableQueryCondition cond = *condition_operands.begin();
Condition condition_type = cond.getConditionType();
string condition_str = convertConditionTypeToString(condition_type);
string filter_key = cond.getKey();
string filter_value = cond.getValue();
ar(
cereal::make_nvp("operator", condition_str),
cereal::make_nvp("key", filter_key),
cereal::make_nvp("value", filter_value)
);
}
void
SerializableQueryFilter::saveOperation(cereal::JSONOutputArchive &ar) const
{
string operator_str = convertOperationTypeToString(operator_type);
if (condition_operands.size() > 0) {
ar(
cereal::make_nvp("operator", operator_str),
cereal::make_nvp("operands", condition_operands)
);
} else if (queries_operands.size() == 1) {
queries_operands[0].saveCondition(ar);
} else if (queries_operands.size() > 0) {
ar(
cereal::make_nvp("operator", operator_str),
cereal::make_nvp("operands", queries_operands)
);
} else {
dbgWarning(D_INTELLIGENCE) << "No conditions or queries to save";
}
}
const string &
SerializableQueryFilter::getConditionValueByKey(const string &key) const
{
for (const SerializableQueryCondition &condition : condition_operands) {
if (condition.getConditionType() == Condition::EQUALS && condition.getKey() == key) {
return condition.getValue();
}
}
static string empty_str = "";
return empty_str;
}
SerializableQueryFilter
SerializableQueryFilter::calcOperator(const SerializableQueryFilter &other_query, const Operator &operator_type)
{
SerializableQueryFilter query_filter_res;
vector<SerializableQueryFilter> new_queries_operands;
new_queries_operands.push_back(*this);
new_queries_operands.push_back(other_query);
query_filter_res.queries_operands = new_queries_operands;
query_filter_res.operator_type = operator_type;
return query_filter_res;
}
SerializableQueryFilter
SerializableQueryFilter::operator &&(const SerializableQueryFilter &other_query)
{
return calcOperator(other_query, Operator::AND);
}
SerializableQueryFilter
SerializableQueryFilter::operator ||(const SerializableQueryFilter &other_query)
{
return calcOperator(other_query, Operator::OR);
}

View File

@@ -0,0 +1,173 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "intelligence_is_v2/query_request_v2.h"
#include "debug.h"
const uint QueryRequest::default_min_confidence = 500;
const uint QueryRequest::default_assets_limit = 20;
using namespace std;
using namespace Intelligence_IS_V2;
USE_DEBUG_FLAG(D_INTELLIGENCE);
QueryRequest::QueryRequest(
Condition condition_type,
const string &key,
const 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;
}
void
QueryRequest::saveToJson(cereal::JSONOutputArchive &ar) const
{
ar(
cereal::make_nvp("limit", assets_limit),
cereal::make_nvp("fullResponse", full_response),
cereal::make_nvp("query", query)
);
if (cursor.ok()) ar(cereal::make_nvp("cursor", cursor.unpack().second));
requested_attributes.save(ar);
query_types.save(ar);
}
uint
QueryRequest::getAssetsLimit() const
{
return assets_limit;
}
const SerializableQueryFilter &
QueryRequest::getQuery() const
{
return query;
}
const SerializableAttributesMap &
QueryRequest::getRequestedAttributes() const
{
return requested_attributes;
}
void
QueryRequest::addCondition (
Condition condition_type,
const string &key,
const string &value,
AttributeKeyType attribute_type
) {
query.addCondition(condition_type, createAttributeString(key, attribute_type), value);
}
void
QueryRequest::setRequestedAttr(const string &attr, AttributeKeyType attr_type)
{
setRequestedAttr(attr, default_min_confidence, attr_type);
}
void
QueryRequest::setRequestedAttr(const string &attr, uint min_conf, AttributeKeyType attr_type)
{
requested_attributes.setSerializableAttribute(createAttributeString(attr, attr_type), min_conf);
}
void
QueryRequest::setTenantsList(const vector<string> tenants)
{
query_types.setSerializableTenantList(tenants);
}
void
QueryRequest::setAssetsLimit(uint _assets_limit)
{
assets_limit = _assets_limit;
}
bool
QueryRequest::checkMinConfidence(uint upper_confidence_limit)
{
return requested_attributes.checkMinConfidence(upper_confidence_limit);
}
void
QueryRequest::activatePaging()
{
cursor = RequestCursor(CursorState::START, "start");
}
bool
QueryRequest::isPagingActivated()
{
return cursor.ok();
}
Maybe<CursorState>
QueryRequest::getCursorState()
{
if (!cursor.ok()) return genError("Paging not activated");
return cursor.unpack().first;
}
bool
QueryRequest::isPagingFinished()
{
if (!cursor.ok()) throw IntelligenceException("Paging is not activated.");
return cursor.unpack().first == CursorState::DONE;
}
void
QueryRequest::setCursor(CursorState state, const string &value)
{
cursor = RequestCursor(state, value);
}
QueryRequest
QueryRequest::calcQueryRequestOperator(const QueryRequest &other_query, const Operator &operator_type)
{
QueryRequest res_req_query;
SerializableQueryFilter res_query_filter;
if (operator_type == Operator::AND) {
dbgTrace(D_INTELLIGENCE) << "Calculating query request AND operator";
res_query_filter = (this->query && other_query.getQuery());
} else if (operator_type == Operator::OR) {
dbgTrace(D_INTELLIGENCE) << "Calculating query request OR operator";
res_query_filter = (this->query || other_query.getQuery());
}
res_req_query.query = res_query_filter;
res_req_query.assets_limit = this->assets_limit;
res_req_query.full_response = this->full_response;
res_req_query.cursor = this->cursor;
res_req_query.requested_attributes = this->requested_attributes;
res_req_query.query_types = this->query_types;
return res_req_query;
}
QueryRequest
QueryRequest::operator &&(const QueryRequest &other_query)
{
return calcQueryRequestOperator(other_query, Operator::AND);
}
QueryRequest
QueryRequest::operator ||(const QueryRequest &other_query)
{
return calcQueryRequestOperator(other_query, Operator::OR);
}

View File

@@ -0,0 +1,38 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "intelligence_is_v2/query_types_v2.h"
using namespace std;
using namespace Intelligence_IS_V2;
void
serializableTenantList::serialize(cereal::JSONOutputArchive &ar) const
{
ar(cereal::make_nvp("multiTenant", tenants));
}
void
SerializableQueryTypes::save(cereal::JSONOutputArchive &ar) const
{
if (!is_nsaas) return;
serializableTenantList serializable_tenants(tenants);
ar(cereal::make_nvp("queryTypes", serializable_tenants));
}
void
SerializableQueryTypes::setSerializableTenantList(const std::vector<std::string> _tenants)
{
tenants = _tenants;
is_nsaas = true;
};

View File

@@ -0,0 +1,63 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "intelligence_is_v2/requested_attributes_v2.h"
#include <tuple>
using namespace std;
using namespace Intelligence_IS_V2;
void
serializableAttribute::serialize(cereal::JSONOutputArchive &ar) const
{
ar(
cereal::make_nvp("key", key),
cereal::make_nvp("minConfidence", min_confidence)
);
}
void
SerializableAttributesMap::save(cereal::JSONOutputArchive &ar) const
{
if (requested_attributes.empty()) return;
vector<serializableAttribute> all_attributes;
for (auto const & iter : requested_attributes) {
serializableAttribute attribute(iter.first, iter.second);
all_attributes.push_back(attribute);
}
ar(cereal::make_nvp("requestedAttributes", all_attributes));
}
void
SerializableAttributesMap::setSerializableAttribute(const string &attribute, uint confidence)
{
requested_attributes[attribute] = confidence;
};
uint
SerializableAttributesMap::getAttributeByKey(const string &key) const
{
return requested_attributes.at(key);
}
bool
SerializableAttributesMap::checkMinConfidence(uint upper_confidence_limit)
{
for (auto const &attribute : requested_attributes) {
if (attribute.second == 0 || attribute.second > upper_confidence_limit) return false;
}
return true;
}