From d07760fe60c72c5838e93154fee36686d0315555 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Mon, 19 Sep 2022 18:44:31 +0530 Subject: [PATCH] Agent: Make HTTPIslandAPIClient.send_log() and HTTPIslandAPIClient.get_pba_file() static --- monkey/infection_monkey/control.py | 4 ++-- .../island_api_client/http_island_api_client.py | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/monkey/infection_monkey/control.py b/monkey/infection_monkey/control.py index 646da5239..2bb5e324f 100644 --- a/monkey/infection_monkey/control.py +++ b/monkey/infection_monkey/control.py @@ -77,12 +77,12 @@ class ControlClient: return try: telemetry = {"monkey_guid": GUID, "log": json.dumps(log)} - HTTPIslandAPIClient(self.server_address).send_log(json.dumps(telemetry)) + HTTPIslandAPIClient.send_log(self.server_address, json.dumps(telemetry)) except Exception as exc: logger.warning(f"Error connecting to control server {self.server_address}: {exc}") def get_pba_file(self, filename): try: - HTTPIslandAPIClient(self.server_address).get_pba_file(filename) + HTTPIslandAPIClient.get_pba_file(self.server_address, filename) except requests.exceptions.RequestException: return False 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 df1c2814d..b22eabad7 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 @@ -37,23 +37,24 @@ class HTTPIslandAPIClient(IIslandAPIClient): verify=False, timeout=MEDIUM_REQUEST_TIMEOUT, ) - self._island_server = island_server # TODO: set server address as object property when init is called in find_server and pass # object around? won't need to pass island server and create object in every function + @staticmethod @handle_island_errors - def send_log(self, data: str): + def send_log(server_address: str, data: str): requests.post( # noqa: DUO123 - "https://%s/api/log" % (self._island_server,), + "https://%s/api/log" % (server_address,), json=data, verify=False, timeout=MEDIUM_REQUEST_TIMEOUT, ) + @staticmethod @handle_island_errors - def get_pba_file(self, filename: str): + def get_pba_file(server_address: str, filename: str): return requests.get( # noqa: DUO123 - "https://%s/api/pba/download/%s" % (self.server_address, filename), + "https://%s/api/pba/download/%s" % (server_address, filename), verify=False, timeout=LONG_REQUEST_TIMEOUT, )