From 73fbc22e3d20ef9c90670a4837a231e23efd3c0b Mon Sep 17 00:00:00 2001 From: Kekoa Kaaikala Date: Mon, 3 Oct 2022 17:04:27 +0000 Subject: [PATCH 1/6] BB: Remove find_monkeys_in_db --- .../blackbox/island_client/monkey_island_client.py | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/envs/monkey_zoo/blackbox/island_client/monkey_island_client.py b/envs/monkey_zoo/blackbox/island_client/monkey_island_client.py index bc8115db745..28745b5b8c5 100644 --- a/envs/monkey_zoo/blackbox/island_client/monkey_island_client.py +++ b/envs/monkey_zoo/blackbox/island_client/monkey_island_client.py @@ -139,14 +139,6 @@ def _reset_island_mode(self): LOGGER.error("Failed to reset island mode") assert False - def find_monkeys_in_db(self, query): - if query is None: - raise TypeError - response = self.requests.get( - MONKEY_TEST_ENDPOINT, MonkeyIslandClient.form_find_query_for_request(query) - ) - return MonkeyIslandClient.get_test_query_results(response) - def find_telems_in_db(self, query: dict): if query is None: raise TypeError @@ -186,5 +178,5 @@ def get_test_query_results(response): return json.loads(response.content)["results"] def is_all_monkeys_dead(self): - query = {"dead": False} - return len(self.find_monkeys_in_db(query)) == 0 + agents = self.get_agents() + return all([a.stop_time is not None for a in agents]) From e0c9717da9b1849afd51939f8109e6f529d086a8 Mon Sep 17 00:00:00 2001 From: Kekoa Kaaikala Date: Mon, 3 Oct 2022 17:11:37 +0000 Subject: [PATCH 2/6] BB: Update test_compabitiblity to use new api --- envs/os_compatibility/test_compatibility.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/envs/os_compatibility/test_compatibility.py b/envs/os_compatibility/test_compatibility.py index f43323e19f6..b6cb81577e1 100644 --- a/envs/os_compatibility/test_compatibility.py +++ b/envs/os_compatibility/test_compatibility.py @@ -1,3 +1,6 @@ +from ipaddress import IPv4Address +from typing import Collection + import pytest from envs.monkey_zoo.blackbox.island_client.monkey_island_client import MonkeyIslandClient @@ -40,18 +43,17 @@ def island_client(island): @pytest.mark.usefixtures("island_client") # noinspection PyUnresolvedReferences class TestOSCompatibility(object): - def test_os_compat(self, island_client): + def test_os_compat(self, island_client: MonkeyIslandClient): print() - all_monkeys = island_client.get_all_monkeys_from_db() - ips_that_communicated = [] - for monkey in all_monkeys: - for ip in monkey["ip_addresses"]: - if ip in machine_list: - ips_that_communicated.append(ip) - break + ips_that_communicated = self._get_agent_ips(island_client) for ip, os in machine_list.items(): - if ip not in ips_that_communicated: + if IPv4Address(ip) not in ips_that_communicated: print("{} didn't communicate to island".format(os)) if len(ips_that_communicated) < len(machine_list): assert False + + def _get_agent_ips(self, island_client: MonkeyIslandClient) -> Collection[IPv4Address]: + agents = island_client.get_agents() + machines = island_client.get_machines() + return {i.ip for a in agents for i in machines[a.machine_id].network_interfaces} From 2bea6197862287cd9a72a466d367278ace0c92af Mon Sep 17 00:00:00 2001 From: Kekoa Kaaikala Date: Mon, 3 Oct 2022 17:13:27 +0000 Subject: [PATCH 3/6] BB: Removed unused method and endpoint --- .../blackbox/island_client/monkey_island_client.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/envs/monkey_zoo/blackbox/island_client/monkey_island_client.py b/envs/monkey_zoo/blackbox/island_client/monkey_island_client.py index 28745b5b8c5..28dc1f135c5 100644 --- a/envs/monkey_zoo/blackbox/island_client/monkey_island_client.py +++ b/envs/monkey_zoo/blackbox/island_client/monkey_island_client.py @@ -15,7 +15,6 @@ GET_AGENTS_ENDPOINT = "api/agents" GET_LOG_ENDPOINT = "api/agent-logs" GET_MACHINES_ENDPOINT = "api/machines" -MONKEY_TEST_ENDPOINT = "api/test/monkey" TELEMETRY_TEST_ENDPOINT = "api/test/telemetry" LOGGER = logging.getLogger(__name__) @@ -147,12 +146,6 @@ def find_telems_in_db(self, query: dict): ) return MonkeyIslandClient.get_test_query_results(response) - def get_all_monkeys_from_db(self): - response = self.requests.get( - MONKEY_TEST_ENDPOINT, MonkeyIslandClient.form_find_query_for_request(None) - ) - return MonkeyIslandClient.get_test_query_results(response) - def get_agents(self) -> Sequence[Agent]: response = self.requests.get(GET_AGENTS_ENDPOINT) From b713cce893cfbd220d1460ae6d3ba2b71a7e21d3 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Tue, 4 Oct 2022 15:41:07 -0400 Subject: [PATCH 4/6] Island: Remove /api/test/monkey endpoint --- CHANGELOG.md | 1 + monkey/monkey_island/cc/app.py | 2 -- .../blackbox/monkey_blackbox_endpoint.py | 16 ---------------- 3 files changed, 1 insertion(+), 18 deletions(-) delete mode 100644 monkey/monkey_island/cc/resources/blackbox/monkey_blackbox_endpoint.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b4eac50c5a..af69caa9747 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -115,6 +115,7 @@ Changelog](https://keepachangelog.com/en/1.0.0/). - "/api/island-configuration" endpoint. #2003 - "-t/--tunnel" from agent command line arguments. #2216 - "/api/monkey-control/neets-to-stop". #2261 +- "GET /api/test/monkey" endpoint. #2269 ### Fixed - A bug in network map page that caused delay of telemetry log loading. #1545 diff --git a/monkey/monkey_island/cc/app.py b/monkey/monkey_island/cc/app.py index 18af5f2a6d6..f842d8ffcbc 100644 --- a/monkey/monkey_island/cc/app.py +++ b/monkey/monkey_island/cc/app.py @@ -32,7 +32,6 @@ from monkey_island.cc.resources.attack.attack_report import AttackReport from monkey_island.cc.resources.auth import Authenticate, Register, RegistrationStatus, init_jwt from monkey_island.cc.resources.blackbox.log_blackbox_endpoint import LogBlackboxEndpoint -from monkey_island.cc.resources.blackbox.monkey_blackbox_endpoint import MonkeyBlackboxEndpoint from monkey_island.cc.resources.blackbox.telemetry_blackbox_endpoint import ( TelemetryBlackboxEndpoint, ) @@ -207,7 +206,6 @@ def init_restful_endpoints(api: FlaskDIWrapper): # API Spec: Fix all the following endpoints, see comments in the resource classes # Note: Preferably, the API will provide a rich feature set and allow access to all of the # necessary data. This would make these endpoints obsolete. - api.add_resource(MonkeyBlackboxEndpoint) api.add_resource(LogBlackboxEndpoint) api.add_resource(TelemetryBlackboxEndpoint) diff --git a/monkey/monkey_island/cc/resources/blackbox/monkey_blackbox_endpoint.py b/monkey/monkey_island/cc/resources/blackbox/monkey_blackbox_endpoint.py deleted file mode 100644 index 4a140f26537..00000000000 --- a/monkey/monkey_island/cc/resources/blackbox/monkey_blackbox_endpoint.py +++ /dev/null @@ -1,16 +0,0 @@ -from bson import json_util -from flask import request - -from monkey_island.cc.database import mongo -from monkey_island.cc.resources.AbstractResource import AbstractResource -from monkey_island.cc.resources.request_authentication import jwt_required - - -class MonkeyBlackboxEndpoint(AbstractResource): - # API Spec: Rename to noun, BlackboxTestsMonkeys or something - urls = ["/api/test/monkey"] - - @jwt_required - def get(self, **kw): - find_query = json_util.loads(request.args.get("find_query")) - return {"results": list(mongo.db.monkey.find(find_query))} From 6ae767632201b528ab89f98b375b0dd234b2b651 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Tue, 4 Oct 2022 15:43:25 -0400 Subject: [PATCH 5/6] BB: Pass generator instead of list comprehension to all() This will allow a short-circuit. --- envs/monkey_zoo/blackbox/island_client/monkey_island_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/envs/monkey_zoo/blackbox/island_client/monkey_island_client.py b/envs/monkey_zoo/blackbox/island_client/monkey_island_client.py index 28dc1f135c5..4f723e8a7a0 100644 --- a/envs/monkey_zoo/blackbox/island_client/monkey_island_client.py +++ b/envs/monkey_zoo/blackbox/island_client/monkey_island_client.py @@ -172,4 +172,4 @@ def get_test_query_results(response): def is_all_monkeys_dead(self): agents = self.get_agents() - return all([a.stop_time is not None for a in agents]) + return all((a.stop_time is not None for a in agents)) From bbbb1ac773547fd52955a2c7314542e4dcde64b0 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Tue, 4 Oct 2022 16:08:33 -0400 Subject: [PATCH 6/6] Island: Remove disused LogBlackboxEndpoint --- CHANGELOG.md | 1 + monkey/monkey_island/cc/app.py | 2 -- .../blackbox/log_blackbox_endpoint.py | 20 ------------------- 3 files changed, 1 insertion(+), 22 deletions(-) delete mode 100644 monkey/monkey_island/cc/resources/blackbox/log_blackbox_endpoint.py diff --git a/CHANGELOG.md b/CHANGELOG.md index af69caa9747..66a2d865e68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -116,6 +116,7 @@ Changelog](https://keepachangelog.com/en/1.0.0/). - "-t/--tunnel" from agent command line arguments. #2216 - "/api/monkey-control/neets-to-stop". #2261 - "GET /api/test/monkey" endpoint. #2269 +- "GET /api/test/log" endpoint. #2269 ### Fixed - A bug in network map page that caused delay of telemetry log loading. #1545 diff --git a/monkey/monkey_island/cc/app.py b/monkey/monkey_island/cc/app.py index f842d8ffcbc..63ee6a72b71 100644 --- a/monkey/monkey_island/cc/app.py +++ b/monkey/monkey_island/cc/app.py @@ -31,7 +31,6 @@ from monkey_island.cc.resources.AbstractResource import AbstractResource from monkey_island.cc.resources.attack.attack_report import AttackReport from monkey_island.cc.resources.auth import Authenticate, Register, RegistrationStatus, init_jwt -from monkey_island.cc.resources.blackbox.log_blackbox_endpoint import LogBlackboxEndpoint from monkey_island.cc.resources.blackbox.telemetry_blackbox_endpoint import ( TelemetryBlackboxEndpoint, ) @@ -206,7 +205,6 @@ def init_restful_endpoints(api: FlaskDIWrapper): # API Spec: Fix all the following endpoints, see comments in the resource classes # Note: Preferably, the API will provide a rich feature set and allow access to all of the # necessary data. This would make these endpoints obsolete. - api.add_resource(LogBlackboxEndpoint) api.add_resource(TelemetryBlackboxEndpoint) diff --git a/monkey/monkey_island/cc/resources/blackbox/log_blackbox_endpoint.py b/monkey/monkey_island/cc/resources/blackbox/log_blackbox_endpoint.py deleted file mode 100644 index 643b7f592ae..00000000000 --- a/monkey/monkey_island/cc/resources/blackbox/log_blackbox_endpoint.py +++ /dev/null @@ -1,20 +0,0 @@ -from bson import json_util -from flask import request - -from monkey_island.cc.database import database, mongo -from monkey_island.cc.resources.AbstractResource import AbstractResource -from monkey_island.cc.resources.request_authentication import jwt_required - - -class LogBlackboxEndpoint(AbstractResource): - # API Spec: Rename to noun, BlackboxTestsLogs or something - urls = ["/api/test/log"] - - @jwt_required - def get(self): - find_query = json_util.loads(request.args.get("find_query")) - log = mongo.db.log.find_one(find_query) - if not log: - return {"results": None} - log_file = database.gridfs.get(log["file_id"]) - return {"results": log_file.read().decode()}