From 0b0f0394743059676875ce967857979a97df1769 Mon Sep 17 00:00:00 2001 From: vakarisz Date: Fri, 16 Sep 2022 17:39:46 +0300 Subject: [PATCH 01/18] Agent: Add 4 IslandAPIErrors --- .../transport/island_api_client_errors.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 monkey/infection_monkey/transport/island_api_client_errors.py diff --git a/monkey/infection_monkey/transport/island_api_client_errors.py b/monkey/infection_monkey/transport/island_api_client_errors.py new file mode 100644 index 000000000..d0dbeb0c4 --- /dev/null +++ b/monkey/infection_monkey/transport/island_api_client_errors.py @@ -0,0 +1,30 @@ +class IslandAPIError(Exception): + """ + Raised when something goes wrong when calling the Island API + """ + + pass + + +class IslandAPITimeoutError(IslandAPIError): + """ + Raised when the API request hits a timeout + """ + + pass + + +class IslandAPIConnectionError(IslandAPIError): + """ + Raised when the API request can't find/connect to the Island + """ + + pass + + +class IslandAPIRequestFailedError(IslandAPIError): + """ + Raised when the API request fails(malformed request or an error on the API side) + """ + + pass From 8386a006d0a9b33ece7d474072c26ede18b79fdc Mon Sep 17 00:00:00 2001 From: vakarisz Date: Fri, 16 Sep 2022 18:03:54 +0300 Subject: [PATCH 02/18] Agent: Implement IslandApiClient --- monkey/infection_monkey/transport/__init__.py | 1 + .../transport/island_api_client.py | 39 +++++++++++++++++++ .../transport/test_island_api_client.py | 30 ++++++++++++++ vulture_allowlist.py | 6 +++ 4 files changed, 76 insertions(+) create mode 100644 monkey/infection_monkey/transport/island_api_client.py create mode 100644 monkey/tests/unit_tests/infection_monkey/transport/test_island_api_client.py diff --git a/monkey/infection_monkey/transport/__init__.py b/monkey/infection_monkey/transport/__init__.py index 960bce311..b59fc76f9 100644 --- a/monkey/infection_monkey/transport/__init__.py +++ b/monkey/infection_monkey/transport/__init__.py @@ -1 +1,2 @@ from infection_monkey.transport.http import LockedHTTPServer +from .island_api_client import IslandApiClient diff --git a/monkey/infection_monkey/transport/island_api_client.py b/monkey/infection_monkey/transport/island_api_client.py new file mode 100644 index 000000000..19a4033bc --- /dev/null +++ b/monkey/infection_monkey/transport/island_api_client.py @@ -0,0 +1,39 @@ +import logging + +import requests + +from common.common_consts.timeouts import MEDIUM_REQUEST_TIMEOUT +from infection_monkey.transport.island_api_client_errors import ( + IslandAPIConnectionError, + IslandAPIError, + IslandAPITimeoutError, +) + +logger = logging.getLogger(__name__) + + +class IslandApiClient: + """ + Represents Island API client + """ + + def __init__(self, island_server: str): + """ + Tries to connect to the island. + + :param island_server: String representing the island ip address and port + :raises IslandAPIError: + """ + + try: + requests.get( # noqa: DUO123 + f"https://{island_server}/api?action=is-up", + verify=False, + timeout=MEDIUM_REQUEST_TIMEOUT, + ) + except requests.exceptions.ConnectionError as err: + raise IslandAPIConnectionError(err) + except TimeoutError as err: + raise IslandAPITimeoutError(err) + except Exception as err: + raise IslandAPIError(err) diff --git a/monkey/tests/unit_tests/infection_monkey/transport/test_island_api_client.py b/monkey/tests/unit_tests/infection_monkey/transport/test_island_api_client.py new file mode 100644 index 000000000..819dd0ab4 --- /dev/null +++ b/monkey/tests/unit_tests/infection_monkey/transport/test_island_api_client.py @@ -0,0 +1,30 @@ +import pytest +import requests +import requests_mock + +from infection_monkey.transport import IslandApiClient +from infection_monkey.transport.island_api_client_errors import ( + IslandAPIConnectionError, + IslandAPIError, + IslandAPITimeoutError, +) + +SERVER = "1.1.1.1:9999" + +ISLAND_URI = f"https://{SERVER}/api?action=is-up" + + +@pytest.mark.parametrize( + "actual_error, expected_error", + [ + (requests.exceptions.ConnectionError, IslandAPIConnectionError), + (TimeoutError, IslandAPITimeoutError), + (Exception, IslandAPIError), + ], +) +def test_island_api_client(actual_error, expected_error): + with requests_mock.Mocker() as m: + m.get(ISLAND_URI, exc=actual_error) + + with pytest.raises(expected_error): + IslandApiClient(SERVER) diff --git a/vulture_allowlist.py b/vulture_allowlist.py index 5abcf4452..a29efe83e 100644 --- a/vulture_allowlist.py +++ b/vulture_allowlist.py @@ -9,6 +9,8 @@ from common.agent_configuration.agent_sub_configurations import ( ) from common.credentials import Credentials, LMHash, NTHash from infection_monkey.exploit.log4shell_utils.ldap_server import LDAPServerFactory +from infection_monkey.transport import IslandApiClient +from infection_monkey.transport.island_api_client_errors import IslandAPIRequestFailedError from monkey_island.cc.event_queue import IslandEventTopic, PyPubSubIslandEventQueue from monkey_island.cc.models import Report from monkey_island.cc.models.networkmap import Arc, NetworkMap @@ -328,3 +330,7 @@ CC_TUNNEL IslandEventTopic.AGENT_CONNECTED IslandEventTopic.CLEAR_SIMULATION_DATA IslandEventTopic.RESET_AGENT_CONFIGURATION + +# TODO: Remove after #2292 is closed +IslandApiClient +IslandAPIRequestFailedError From d217d30b2e0eaf0c5c7f17633030bbe0a66e7716 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Mon, 19 Sep 2022 17:05:56 +0530 Subject: [PATCH 03/18] Agent: Change IslandApiClient -> IslandAPIClient --- monkey/infection_monkey/transport/__init__.py | 2 +- monkey/infection_monkey/transport/island_api_client.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/monkey/infection_monkey/transport/__init__.py b/monkey/infection_monkey/transport/__init__.py index b59fc76f9..5b5cccf83 100644 --- a/monkey/infection_monkey/transport/__init__.py +++ b/monkey/infection_monkey/transport/__init__.py @@ -1,2 +1,2 @@ from infection_monkey.transport.http import LockedHTTPServer -from .island_api_client import IslandApiClient +from .island_api_client import IslandAPIClient diff --git a/monkey/infection_monkey/transport/island_api_client.py b/monkey/infection_monkey/transport/island_api_client.py index 19a4033bc..56bb72383 100644 --- a/monkey/infection_monkey/transport/island_api_client.py +++ b/monkey/infection_monkey/transport/island_api_client.py @@ -12,7 +12,7 @@ from infection_monkey.transport.island_api_client_errors import ( logger = logging.getLogger(__name__) -class IslandApiClient: +class IslandAPIClient: """ Represents Island API client """ From 94dc2288cd3cc0f867c556dad1290796aed240aa Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Mon, 19 Sep 2022 17:06:27 +0530 Subject: [PATCH 04/18] Project: Change IslandApiClient -> IslandAPIClient in Vulture allowlist --- vulture_allowlist.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vulture_allowlist.py b/vulture_allowlist.py index a29efe83e..17fa98f07 100644 --- a/vulture_allowlist.py +++ b/vulture_allowlist.py @@ -9,7 +9,7 @@ from common.agent_configuration.agent_sub_configurations import ( ) from common.credentials import Credentials, LMHash, NTHash from infection_monkey.exploit.log4shell_utils.ldap_server import LDAPServerFactory -from infection_monkey.transport import IslandApiClient +from infection_monkey.transport import IslandAPIClient from infection_monkey.transport.island_api_client_errors import IslandAPIRequestFailedError from monkey_island.cc.event_queue import IslandEventTopic, PyPubSubIslandEventQueue from monkey_island.cc.models import Report @@ -332,5 +332,5 @@ IslandEventTopic.CLEAR_SIMULATION_DATA IslandEventTopic.RESET_AGENT_CONFIGURATION # TODO: Remove after #2292 is closed -IslandApiClient +IslandAPIClient IslandAPIRequestFailedError From 672fde0b94a1186feec8c3bb82e47bba2f9f9845 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Mon, 19 Sep 2022 17:06:41 +0530 Subject: [PATCH 05/18] UT: Change IslandApiClient -> IslandAPIClient --- .../infection_monkey/transport/test_island_api_client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monkey/tests/unit_tests/infection_monkey/transport/test_island_api_client.py b/monkey/tests/unit_tests/infection_monkey/transport/test_island_api_client.py index 819dd0ab4..ce9562641 100644 --- a/monkey/tests/unit_tests/infection_monkey/transport/test_island_api_client.py +++ b/monkey/tests/unit_tests/infection_monkey/transport/test_island_api_client.py @@ -2,7 +2,7 @@ import pytest import requests import requests_mock -from infection_monkey.transport import IslandApiClient +from infection_monkey.transport import IslandAPIClient from infection_monkey.transport.island_api_client_errors import ( IslandAPIConnectionError, IslandAPIError, @@ -27,4 +27,4 @@ def test_island_api_client(actual_error, expected_error): m.get(ISLAND_URI, exc=actual_error) with pytest.raises(expected_error): - IslandApiClient(SERVER) + IslandAPIClient(SERVER) From f5279a6cb108143ab921afc40585303465dec66c Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Mon, 19 Sep 2022 17:09:52 +0530 Subject: [PATCH 06/18] Agent: Import Island API errors in transport/__init__.py --- monkey/infection_monkey/transport/__init__.py | 6 ++++++ monkey/infection_monkey/transport/island_api_client.py | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/monkey/infection_monkey/transport/__init__.py b/monkey/infection_monkey/transport/__init__.py index 5b5cccf83..c9b793a50 100644 --- a/monkey/infection_monkey/transport/__init__.py +++ b/monkey/infection_monkey/transport/__init__.py @@ -1,2 +1,8 @@ from infection_monkey.transport.http import LockedHTTPServer +from .island_api_client_errors import ( + IslandAPIConnectionError, + IslandAPIError, + IslandAPIRequestFailedError, + IslandAPITimeoutError, +) from .island_api_client import IslandAPIClient diff --git a/monkey/infection_monkey/transport/island_api_client.py b/monkey/infection_monkey/transport/island_api_client.py index 56bb72383..f872de295 100644 --- a/monkey/infection_monkey/transport/island_api_client.py +++ b/monkey/infection_monkey/transport/island_api_client.py @@ -3,7 +3,7 @@ import logging import requests from common.common_consts.timeouts import MEDIUM_REQUEST_TIMEOUT -from infection_monkey.transport.island_api_client_errors import ( +from infection_monkey.transport import ( IslandAPIConnectionError, IslandAPIError, IslandAPITimeoutError, From c3ecb1e2e8b79ce25015741bf97fcf11aaaf912a Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Mon, 19 Sep 2022 17:10:34 +0530 Subject: [PATCH 07/18] Project: Fix Island API error import --- vulture_allowlist.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/vulture_allowlist.py b/vulture_allowlist.py index 17fa98f07..6f8d5876a 100644 --- a/vulture_allowlist.py +++ b/vulture_allowlist.py @@ -9,8 +9,7 @@ from common.agent_configuration.agent_sub_configurations import ( ) from common.credentials import Credentials, LMHash, NTHash from infection_monkey.exploit.log4shell_utils.ldap_server import LDAPServerFactory -from infection_monkey.transport import IslandAPIClient -from infection_monkey.transport.island_api_client_errors import IslandAPIRequestFailedError +from infection_monkey.transport import IslandAPIClient, IslandAPIRequestFailedError from monkey_island.cc.event_queue import IslandEventTopic, PyPubSubIslandEventQueue from monkey_island.cc.models import Report from monkey_island.cc.models.networkmap import Arc, NetworkMap From d53b3d5026afa30e9d0cecf9af90d2785929f5d2 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Mon, 19 Sep 2022 17:11:14 +0530 Subject: [PATCH 08/18] UT: Fix Island API errors' import --- .../infection_monkey/transport/test_island_api_client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monkey/tests/unit_tests/infection_monkey/transport/test_island_api_client.py b/monkey/tests/unit_tests/infection_monkey/transport/test_island_api_client.py index ce9562641..513a8a3e0 100644 --- a/monkey/tests/unit_tests/infection_monkey/transport/test_island_api_client.py +++ b/monkey/tests/unit_tests/infection_monkey/transport/test_island_api_client.py @@ -2,8 +2,8 @@ import pytest import requests import requests_mock -from infection_monkey.transport import IslandAPIClient -from infection_monkey.transport.island_api_client_errors import ( +from infection_monkey.transport import ( + IslandAPIClient, IslandAPIConnectionError, IslandAPIError, IslandAPITimeoutError, From b432a9f56795587e66a326db9666484be5d4c7bf Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Mon, 19 Sep 2022 17:16:10 +0530 Subject: [PATCH 09/18] Agent: Fix docstrings in IslandAPIClient --- .../infection_monkey/transport/island_api_client.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/monkey/infection_monkey/transport/island_api_client.py b/monkey/infection_monkey/transport/island_api_client.py index f872de295..1ec193230 100644 --- a/monkey/infection_monkey/transport/island_api_client.py +++ b/monkey/infection_monkey/transport/island_api_client.py @@ -14,15 +14,18 @@ logger = logging.getLogger(__name__) class IslandAPIClient: """ - Represents Island API client + A client for requests from the Agent to the Island API """ def __init__(self, island_server: str): """ - Tries to connect to the island. + Verifies connection to the Island by raising an error if it can't connect - :param island_server: String representing the island ip address and port - :raises IslandAPIError: + :param island_server: Address to the Island + :raises IslandAPIConnectionError: If a connection cannot be made with the Island + :raises IslandAPITimeoutError: If a timeout occurs before being able to connect + with the Island + :raises IslandAPIError: If an unexpected error occurs with the Island API """ try: From ca1bc06c5564c5fb33a974746bb570e718c8eb8f Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Mon, 19 Sep 2022 07:45:30 -0400 Subject: [PATCH 10/18] Agent: Add IIslandAPIClient --- .../infection_monkey/island_api_client/__init__.py | 1 + .../island_api_client/i_island_api_client.py | 12 ++++++++++++ vulture_allowlist.py | 4 +++- 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 monkey/infection_monkey/island_api_client/__init__.py create mode 100644 monkey/infection_monkey/island_api_client/i_island_api_client.py diff --git a/monkey/infection_monkey/island_api_client/__init__.py b/monkey/infection_monkey/island_api_client/__init__.py new file mode 100644 index 000000000..5f8a1c5d5 --- /dev/null +++ b/monkey/infection_monkey/island_api_client/__init__.py @@ -0,0 +1 @@ +from .i_island_api_client import IIslandAPIClient diff --git a/monkey/infection_monkey/island_api_client/i_island_api_client.py b/monkey/infection_monkey/island_api_client/i_island_api_client.py new file mode 100644 index 000000000..ef45a9fe9 --- /dev/null +++ b/monkey/infection_monkey/island_api_client/i_island_api_client.py @@ -0,0 +1,12 @@ +from abc import ABC, abstractmethod + + +class IIslandAPIClient(ABC): + @abstractmethod + def __init__(self, island_server: str): + """ + Construct and API client and connect it to the island + + :param island_server: String representing the island ip address and port + :raises IslandAPIError: If connection was unsuccessful + """ diff --git a/vulture_allowlist.py b/vulture_allowlist.py index 6f8d5876a..b3bbac8e6 100644 --- a/vulture_allowlist.py +++ b/vulture_allowlist.py @@ -9,6 +9,7 @@ from common.agent_configuration.agent_sub_configurations import ( ) from common.credentials import Credentials, LMHash, NTHash from infection_monkey.exploit.log4shell_utils.ldap_server import LDAPServerFactory +from infection_monkey.island_api_client import IIslandAPIClient from infection_monkey.transport import IslandAPIClient, IslandAPIRequestFailedError from monkey_island.cc.event_queue import IslandEventTopic, PyPubSubIslandEventQueue from monkey_island.cc.models import Report @@ -331,5 +332,6 @@ IslandEventTopic.CLEAR_SIMULATION_DATA IslandEventTopic.RESET_AGENT_CONFIGURATION # TODO: Remove after #2292 is closed -IslandAPIClient +IIslandAPIClient +IslandApiClient IslandAPIRequestFailedError From 0790952c47568b8bf533b4a22ec4ce6ee66cb034 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Mon, 19 Sep 2022 07:52:10 -0400 Subject: [PATCH 11/18] Project: Fix IslandApiClient -> IslandAPIClient --- vulture_allowlist.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vulture_allowlist.py b/vulture_allowlist.py index b3bbac8e6..07a0213f2 100644 --- a/vulture_allowlist.py +++ b/vulture_allowlist.py @@ -333,5 +333,5 @@ IslandEventTopic.RESET_AGENT_CONFIGURATION # TODO: Remove after #2292 is closed IIslandAPIClient -IslandApiClient +IslandAPIClient IslandAPIRequestFailedError From 184e54228dd500fc2762ce0bec0b19e88ba4ee6c Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Mon, 19 Sep 2022 07:56:34 -0400 Subject: [PATCH 12/18] Agent: Use relative import for errors in IslandAPIClient --- monkey/infection_monkey/transport/island_api_client.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/monkey/infection_monkey/transport/island_api_client.py b/monkey/infection_monkey/transport/island_api_client.py index 1ec193230..ab8436bbb 100644 --- a/monkey/infection_monkey/transport/island_api_client.py +++ b/monkey/infection_monkey/transport/island_api_client.py @@ -3,11 +3,8 @@ import logging import requests from common.common_consts.timeouts import MEDIUM_REQUEST_TIMEOUT -from infection_monkey.transport import ( - IslandAPIConnectionError, - IslandAPIError, - IslandAPITimeoutError, -) + +from . import IslandAPIConnectionError, IslandAPIError, IslandAPITimeoutError logger = logging.getLogger(__name__) From 6d5503bd6e3b94c753fdf351b67b4c7f740c35b9 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Mon, 19 Sep 2022 08:00:00 -0400 Subject: [PATCH 13/18] Agent: Move IslandAPIClient to island_api_client package --- monkey/infection_monkey/island_api_client/__init__.py | 7 +++++++ .../{transport => island_api_client}/island_api_client.py | 0 .../island_api_client_errors.py | 0 monkey/infection_monkey/transport/__init__.py | 7 ------- .../infection_monkey/island_api_client/__init__.py | 0 .../test_island_api_client.py | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) rename monkey/infection_monkey/{transport => island_api_client}/island_api_client.py (100%) rename monkey/infection_monkey/{transport => island_api_client}/island_api_client_errors.py (100%) create mode 100644 monkey/tests/unit_tests/infection_monkey/island_api_client/__init__.py rename monkey/tests/unit_tests/infection_monkey/{transport => island_api_client}/test_island_api_client.py (93%) diff --git a/monkey/infection_monkey/island_api_client/__init__.py b/monkey/infection_monkey/island_api_client/__init__.py index 5f8a1c5d5..957e734ef 100644 --- a/monkey/infection_monkey/island_api_client/__init__.py +++ b/monkey/infection_monkey/island_api_client/__init__.py @@ -1 +1,8 @@ +from .island_api_client_errors import ( + IslandAPIConnectionError, + IslandAPIError, + IslandAPIRequestFailedError, + IslandAPITimeoutError, +) from .i_island_api_client import IIslandAPIClient +from .island_api_client import IslandAPIClient diff --git a/monkey/infection_monkey/transport/island_api_client.py b/monkey/infection_monkey/island_api_client/island_api_client.py similarity index 100% rename from monkey/infection_monkey/transport/island_api_client.py rename to monkey/infection_monkey/island_api_client/island_api_client.py diff --git a/monkey/infection_monkey/transport/island_api_client_errors.py b/monkey/infection_monkey/island_api_client/island_api_client_errors.py similarity index 100% rename from monkey/infection_monkey/transport/island_api_client_errors.py rename to monkey/infection_monkey/island_api_client/island_api_client_errors.py diff --git a/monkey/infection_monkey/transport/__init__.py b/monkey/infection_monkey/transport/__init__.py index c9b793a50..960bce311 100644 --- a/monkey/infection_monkey/transport/__init__.py +++ b/monkey/infection_monkey/transport/__init__.py @@ -1,8 +1 @@ from infection_monkey.transport.http import LockedHTTPServer -from .island_api_client_errors import ( - IslandAPIConnectionError, - IslandAPIError, - IslandAPIRequestFailedError, - IslandAPITimeoutError, -) -from .island_api_client import IslandAPIClient diff --git a/monkey/tests/unit_tests/infection_monkey/island_api_client/__init__.py b/monkey/tests/unit_tests/infection_monkey/island_api_client/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/monkey/tests/unit_tests/infection_monkey/transport/test_island_api_client.py b/monkey/tests/unit_tests/infection_monkey/island_api_client/test_island_api_client.py similarity index 93% rename from monkey/tests/unit_tests/infection_monkey/transport/test_island_api_client.py rename to monkey/tests/unit_tests/infection_monkey/island_api_client/test_island_api_client.py index 513a8a3e0..0e1d7412b 100644 --- a/monkey/tests/unit_tests/infection_monkey/transport/test_island_api_client.py +++ b/monkey/tests/unit_tests/infection_monkey/island_api_client/test_island_api_client.py @@ -2,7 +2,7 @@ import pytest import requests import requests_mock -from infection_monkey.transport import ( +from infection_monkey.island_api_client import ( IslandAPIClient, IslandAPIConnectionError, IslandAPIError, From 43d6ebcc157634820d32c81d584913dfa77b92ff Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Mon, 19 Sep 2022 08:03:45 -0400 Subject: [PATCH 14/18] Agent: Rename IslandAPIClient -> HTTPIslandAPIClient --- monkey/infection_monkey/island_api_client/__init__.py | 2 +- .../{island_api_client.py => http_island_api_client.py} | 4 ++-- ...land_api_client.py => test_http_island_api_client.py} | 4 ++-- vulture_allowlist.py | 9 ++++++--- 4 files changed, 11 insertions(+), 8 deletions(-) rename monkey/infection_monkey/island_api_client/{island_api_client.py => http_island_api_client.py} (89%) rename monkey/tests/unit_tests/infection_monkey/island_api_client/{test_island_api_client.py => test_http_island_api_client.py} (91%) diff --git a/monkey/infection_monkey/island_api_client/__init__.py b/monkey/infection_monkey/island_api_client/__init__.py index 957e734ef..9fae9b510 100644 --- a/monkey/infection_monkey/island_api_client/__init__.py +++ b/monkey/infection_monkey/island_api_client/__init__.py @@ -5,4 +5,4 @@ from .island_api_client_errors import ( IslandAPITimeoutError, ) from .i_island_api_client import IIslandAPIClient -from .island_api_client import IslandAPIClient +from .http_island_api_client import HTTPIslandAPIClient diff --git a/monkey/infection_monkey/island_api_client/island_api_client.py b/monkey/infection_monkey/island_api_client/http_island_api_client.py similarity index 89% rename from monkey/infection_monkey/island_api_client/island_api_client.py rename to monkey/infection_monkey/island_api_client/http_island_api_client.py index ab8436bbb..d38c640d4 100644 --- a/monkey/infection_monkey/island_api_client/island_api_client.py +++ b/monkey/infection_monkey/island_api_client/http_island_api_client.py @@ -4,12 +4,12 @@ import requests from common.common_consts.timeouts import MEDIUM_REQUEST_TIMEOUT -from . import IslandAPIConnectionError, IslandAPIError, IslandAPITimeoutError +from . import IIslandAPIClient, IslandAPIConnectionError, IslandAPIError, IslandAPITimeoutError logger = logging.getLogger(__name__) -class IslandAPIClient: +class HTTPIslandAPIClient(IIslandAPIClient): """ A client for requests from the Agent to the Island API """ diff --git a/monkey/tests/unit_tests/infection_monkey/island_api_client/test_island_api_client.py b/monkey/tests/unit_tests/infection_monkey/island_api_client/test_http_island_api_client.py similarity index 91% rename from monkey/tests/unit_tests/infection_monkey/island_api_client/test_island_api_client.py rename to monkey/tests/unit_tests/infection_monkey/island_api_client/test_http_island_api_client.py index 0e1d7412b..f213b0569 100644 --- a/monkey/tests/unit_tests/infection_monkey/island_api_client/test_island_api_client.py +++ b/monkey/tests/unit_tests/infection_monkey/island_api_client/test_http_island_api_client.py @@ -3,7 +3,7 @@ import requests import requests_mock from infection_monkey.island_api_client import ( - IslandAPIClient, + HTTPIslandAPIClient, IslandAPIConnectionError, IslandAPIError, IslandAPITimeoutError, @@ -27,4 +27,4 @@ def test_island_api_client(actual_error, expected_error): m.get(ISLAND_URI, exc=actual_error) with pytest.raises(expected_error): - IslandAPIClient(SERVER) + HTTPIslandAPIClient(SERVER) diff --git a/vulture_allowlist.py b/vulture_allowlist.py index 07a0213f2..340d506f2 100644 --- a/vulture_allowlist.py +++ b/vulture_allowlist.py @@ -9,8 +9,11 @@ from common.agent_configuration.agent_sub_configurations import ( ) from common.credentials import Credentials, LMHash, NTHash from infection_monkey.exploit.log4shell_utils.ldap_server import LDAPServerFactory -from infection_monkey.island_api_client import IIslandAPIClient -from infection_monkey.transport import IslandAPIClient, IslandAPIRequestFailedError +from infection_monkey.island_api_client import ( + HTTPIslandAPIClient, + IIslandAPIClient, + IslandAPIRequestFailedError, +) from monkey_island.cc.event_queue import IslandEventTopic, PyPubSubIslandEventQueue from monkey_island.cc.models import Report from monkey_island.cc.models.networkmap import Arc, NetworkMap @@ -333,5 +336,5 @@ IslandEventTopic.RESET_AGENT_CONFIGURATION # TODO: Remove after #2292 is closed IIslandAPIClient -IslandAPIClient +HTTPIslandAPIClient IslandAPIRequestFailedError From f4f9ca6317f49cc63e2733e47fe9fdbb2e12192d Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Mon, 19 Sep 2022 08:18:46 -0400 Subject: [PATCH 15/18] Agent: Update island API client docstrings --- .../island_api_client/http_island_api_client.py | 12 +----------- .../island_api_client/i_island_api_client.py | 13 ++++++++++--- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/monkey/infection_monkey/island_api_client/http_island_api_client.py b/monkey/infection_monkey/island_api_client/http_island_api_client.py index d38c640d4..d9f9b1d9e 100644 --- a/monkey/infection_monkey/island_api_client/http_island_api_client.py +++ b/monkey/infection_monkey/island_api_client/http_island_api_client.py @@ -11,20 +11,10 @@ logger = logging.getLogger(__name__) class HTTPIslandAPIClient(IIslandAPIClient): """ - A client for requests from the Agent to the Island API + A client for the Island's HTTP API """ def __init__(self, island_server: str): - """ - Verifies connection to the Island by raising an error if it can't connect - - :param island_server: Address to the Island - :raises IslandAPIConnectionError: If a connection cannot be made with the Island - :raises IslandAPITimeoutError: If a timeout occurs before being able to connect - with the Island - :raises IslandAPIError: If an unexpected error occurs with the Island API - """ - try: requests.get( # noqa: DUO123 f"https://{island_server}/api?action=is-up", diff --git a/monkey/infection_monkey/island_api_client/i_island_api_client.py b/monkey/infection_monkey/island_api_client/i_island_api_client.py index ef45a9fe9..580158be5 100644 --- a/monkey/infection_monkey/island_api_client/i_island_api_client.py +++ b/monkey/infection_monkey/island_api_client/i_island_api_client.py @@ -2,11 +2,18 @@ from abc import ABC, abstractmethod class IIslandAPIClient(ABC): + """ + A client for the Island's API + """ + @abstractmethod def __init__(self, island_server: str): """ - Construct and API client and connect it to the island + Construct and island API client and connect it to the island - :param island_server: String representing the island ip address and port - :raises IslandAPIError: If connection was unsuccessful + :param island_server: The socket address of the API + :raises IslandAPIConnectionError: If a connection cannot be made to the island + :raises IslandAPITimeoutError: If a timeout occurs while attempting to connect to the island + :raises IslandAPIError: If an unexpected error occurred while attempting to connect to the + island """ From 04e82de641b1381bfb28670190f80200d2dbb10a Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Mon, 19 Sep 2022 09:25:56 -0400 Subject: [PATCH 16/18] Agent: Make language consistent in IIslandAPIClient docstrings --- .../infection_monkey/island_api_client/i_island_api_client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monkey/infection_monkey/island_api_client/i_island_api_client.py b/monkey/infection_monkey/island_api_client/i_island_api_client.py index 580158be5..4a168b3d5 100644 --- a/monkey/infection_monkey/island_api_client/i_island_api_client.py +++ b/monkey/infection_monkey/island_api_client/i_island_api_client.py @@ -12,8 +12,8 @@ class IIslandAPIClient(ABC): Construct and island API client and connect it to the island :param island_server: The socket address of the API - :raises IslandAPIConnectionError: If a connection cannot be made to the island + :raises IslandAPIConnectionError: If the client cannot successfully connect to the island :raises IslandAPITimeoutError: If a timeout occurs while attempting to connect to the island - :raises IslandAPIError: If an unexpected error occurred while attempting to connect to the + :raises IslandAPIError: If an unexpected error occurs while attempting to connect to the island """ From 1b037c4b3ade0f95cb2df88abbe2a65325ec22d0 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Mon, 19 Sep 2022 19:05:46 +0530 Subject: [PATCH 17/18] Agent: Add IslandAPIRequestError for errors that are the client's mistake --- .../infection_monkey/island_api_client/__init__.py | 1 + .../island_api_client/island_api_client_errors.py | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/monkey/infection_monkey/island_api_client/__init__.py b/monkey/infection_monkey/island_api_client/__init__.py index 9fae9b510..ec513e774 100644 --- a/monkey/infection_monkey/island_api_client/__init__.py +++ b/monkey/infection_monkey/island_api_client/__init__.py @@ -1,6 +1,7 @@ from .island_api_client_errors import ( IslandAPIConnectionError, IslandAPIError, + IslandAPIRequestError, IslandAPIRequestFailedError, IslandAPITimeoutError, ) diff --git a/monkey/infection_monkey/island_api_client/island_api_client_errors.py b/monkey/infection_monkey/island_api_client/island_api_client_errors.py index d0dbeb0c4..cdb23539a 100644 --- a/monkey/infection_monkey/island_api_client/island_api_client_errors.py +++ b/monkey/infection_monkey/island_api_client/island_api_client_errors.py @@ -22,9 +22,17 @@ class IslandAPIConnectionError(IslandAPIError): pass -class IslandAPIRequestFailedError(IslandAPIError): +class IslandAPIRequestError(IslandAPIError): """ - Raised when the API request fails(malformed request or an error on the API side) + Raised when the API request fails due to an error in the request sent from the client + """ + + pass + + +class IslandAPIRequestFailedError(IslandAPIError): + """ + Raised when the API request fails due to an error on the server """ pass From 9e79c7c6ef285825dabc5c7ac8ea51b2975438c4 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Mon, 19 Sep 2022 19:07:40 +0530 Subject: [PATCH 18/18] Project: Add IslandAPIRequestError to Vulture allowlist --- vulture_allowlist.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/vulture_allowlist.py b/vulture_allowlist.py index 340d506f2..2008f796a 100644 --- a/vulture_allowlist.py +++ b/vulture_allowlist.py @@ -12,6 +12,7 @@ from infection_monkey.exploit.log4shell_utils.ldap_server import LDAPServerFacto from infection_monkey.island_api_client import ( HTTPIslandAPIClient, IIslandAPIClient, + IslandAPIRequestError, IslandAPIRequestFailedError, ) from monkey_island.cc.event_queue import IslandEventTopic, PyPubSubIslandEventQueue @@ -338,3 +339,4 @@ IslandEventTopic.RESET_AGENT_CONFIGURATION IIslandAPIClient HTTPIslandAPIClient IslandAPIRequestFailedError +IslandAPIRequestError