From 9864720fb5c0227920a2309c8478b04e03116789 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Fri, 22 May 2020 21:22:29 +0300 Subject: [PATCH] Get node label performance improvement and small BB test changes --- envs/monkey_zoo/blackbox/README.md | 3 ++ .../performance/endpoint_performance_test.py | 2 +- .../telemetry_performance_test_workflow.py | 4 +- monkey/monkey_island/cc/services/edge.py | 45 +++++++++++-------- .../cc/services/telemetry/processing/scan.py | 2 + 5 files changed, 36 insertions(+), 20 deletions(-) diff --git a/envs/monkey_zoo/blackbox/README.md b/envs/monkey_zoo/blackbox/README.md index dbdd54b41..56879e508 100644 --- a/envs/monkey_zoo/blackbox/README.md +++ b/envs/monkey_zoo/blackbox/README.md @@ -25,6 +25,9 @@ Configure a PyTest configuration with the additional arguments `-s --island=35.2 `monkey\envs\monkey_zoo\blackbox`. ### Running telemetry performance test + +**Before running performance test make sure browser is not sending requests to island!** + To run telemetry performance test follow these steps: 1. Gather monkey telemetries. 1. Enable "Export monkey telemetries" in Configuration -> Internal -> Tests if you don't have diff --git a/envs/monkey_zoo/blackbox/tests/performance/endpoint_performance_test.py b/envs/monkey_zoo/blackbox/tests/performance/endpoint_performance_test.py index b4f8f35c8..b8793452d 100644 --- a/envs/monkey_zoo/blackbox/tests/performance/endpoint_performance_test.py +++ b/envs/monkey_zoo/blackbox/tests/performance/endpoint_performance_test.py @@ -18,9 +18,9 @@ class EndpointPerformanceTest(BasicTest): def run(self) -> bool: # Collect timings for all pages - self.island_client.clear_caches() endpoint_timings = {} for endpoint in self.test_config.endpoints_to_test: + self.island_client.clear_caches() endpoint_timings[endpoint] = self.island_client.requests.get_request_time(endpoint, SupportedRequestMethod.GET) analyzer = PerformanceAnalyzer(self.test_config, endpoint_timings) diff --git a/envs/monkey_zoo/blackbox/tests/performance/telemetry_performance_test_workflow.py b/envs/monkey_zoo/blackbox/tests/performance/telemetry_performance_test_workflow.py index e8bef33d8..6d09752ca 100644 --- a/envs/monkey_zoo/blackbox/tests/performance/telemetry_performance_test_workflow.py +++ b/envs/monkey_zoo/blackbox/tests/performance/telemetry_performance_test_workflow.py @@ -15,7 +15,9 @@ class TelemetryPerformanceTestWorkflow(BasicTest): def run(self): try: if not self.quick_performance_test: - TelemetryPerformanceTest(island_client=self.island_client).test_telemetry_performance() + telem_sending_test = TelemetryPerformanceTest(island_client=self.island_client, + quick_performance_test=self.quick_performance_test) + telem_sending_test.test_telemetry_performance() performance_test = EndpointPerformanceTest(self.name, self.performance_config, self.island_client) assert performance_test.run() finally: diff --git a/monkey/monkey_island/cc/services/edge.py b/monkey/monkey_island/cc/services/edge.py index a8016f5a8..cd4ef737b 100644 --- a/monkey/monkey_island/cc/services/edge.py +++ b/monkey/monkey_island/cc/services/edge.py @@ -48,7 +48,9 @@ class EdgeService: "scans": [], "exploits": [], "tunnel": False, - "exploited": False + "exploited": False, + "src_label": EdgeService.get_label_for_endpoint(from_id), + "dst_label": EdgeService.get_label_for_endpoint(to_id) }) return mongo.db.edge.find_one({"_id": edge_insert_result.inserted_id}) @@ -67,7 +69,9 @@ class EdgeService: "id": edge_id, "from": edge_from, "to": edge_to, - "group": "island" + "group": "island", + "src_label": EdgeService.get_label_for_endpoint(edge_from), + "dst_label": EdgeService.get_label_for_endpoint(edge_to) } edge["_label"] = EdgeService.get_edge_label(edge) return edge @@ -118,7 +122,9 @@ class EdgeService: "id": edge["_id"], "from": edge["from"], "to": edge["to"], - "group": EdgeService.get_edge_group(edge) + "group": EdgeService.get_edge_group(edge), + "src_label": edge["src_label"], + "dst_label": edge["dst_label"] } @staticmethod @@ -141,24 +147,27 @@ class EdgeService: @staticmethod def get_edge_label(edge): + return "%s %s %s" % (edge['src_label'], RIGHT_ARROW, edge['dst_label']) + + @staticmethod + def get_label_for_endpoint(endpoint_id): node_service = monkey_island.cc.services.node.NodeService - from_id = edge["from"] - to_id = edge["to"] - - try: - from_label = Monkey.get_label_by_id(from_id) - except MonkeyNotFoundError: - from_label = node_service.get_node_by_id(from_id)['domain_name'] - - if to_id == ObjectId("000000000000000000000000"): - to_label = 'MonkeyIsland' + if endpoint_id == ObjectId("000000000000000000000000"): + return 'MonkeyIsland' + if Monkey.is_monkey(endpoint_id): + return Monkey.get_label_by_id(endpoint_id) else: - if Monkey.is_monkey(to_id): - to_label = Monkey.get_label_by_id(to_id) - else: - to_label = node_service.get_node_label(node_service.get_node_by_id(to_id)) + return node_service.get_node_label(node_service.get_node_by_id(endpoint_id)) - return "%s %s %s" % (from_label, RIGHT_ARROW, to_label) + @staticmethod + def update_label_by_endpoint(edge, endpoint_id): + label = EdgeService.get_label_for_endpoint(endpoint_id) + if endpoint_id == edge["to"]: + mongo_field = {"dst_label": label} + else: + mongo_field = {"src_label": label} + mongo.db.edge.update({"_id": edge["_id"]}, + {"$set": mongo_field}) RIGHT_ARROW = "\u2192" diff --git a/monkey/monkey_island/cc/services/telemetry/processing/scan.py b/monkey/monkey_island/cc/services/telemetry/processing/scan.py index bea451170..d57aef7c0 100644 --- a/monkey/monkey_island/cc/services/telemetry/processing/scan.py +++ b/monkey/monkey_island/cc/services/telemetry/processing/scan.py @@ -2,6 +2,7 @@ import copy from monkey_island.cc.database import mongo from monkey_island.cc.models import Monkey +from monkey_island.cc.services.edge import EdgeService from monkey_island.cc.services.telemetry.processing.utils import get_edge_by_scan_or_exploit_telemetry from monkey_island.cc.services.telemetry.zero_trust_tests.data_endpoints import test_open_data_endpoints from monkey_island.cc.services.telemetry.zero_trust_tests.segmentation import test_segmentation_violation @@ -42,3 +43,4 @@ def update_edges_and_nodes_based_on_scan_telemetry(telemetry_json): mongo.db.node.update({"_id": node["_id"]}, {"$set": {"os.version": scan_os["version"]}}, upsert=False) + EdgeService.update_label_by_endpoint(edge, node["_id"])