diff --git a/monkey/infection_monkey/control.py b/monkey/infection_monkey/control.py index 41b3511d9..887ae4221 100644 --- a/monkey/infection_monkey/control.py +++ b/monkey/infection_monkey/control.py @@ -68,13 +68,6 @@ class ControlClient: except Exception as exc: logger.warning(f"Error connecting to control server {self.server_address}: {exc}") - def send_log(self, log): - try: - telemetry = {"monkey_guid": GUID, "log": json.dumps(log)} - self._island_api_client.send_log(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: return self._island_api_client.get_pba_file(filename) 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 624a2c504..eb2420c95 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 @@ -16,7 +16,7 @@ from common.common_consts.timeouts import ( SHORT_REQUEST_TIMEOUT, ) from common.credentials import Credentials -from common.types import SocketAddress +from common.types import AgentID, SocketAddress from . import ( AbstractIslandAPIClientFactory, @@ -92,9 +92,9 @@ class HTTPIslandAPIClient(IIslandAPIClient): self._api_url = f"https://{island_server}/api" @handle_island_errors - def send_log(self, log_contents: str): - response = requests.post( # noqa: DUO123 - f"{self._api_url}/log", + def send_log(self, agent_id: AgentID, log_contents: str): + response = requests.put( # noqa: DUO123 + f"{self._api_url}/agent-logs/{agent_id}", json=log_contents, verify=False, timeout=MEDIUM_REQUEST_TIMEOUT, 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 3fd83daaa..86d42ee88 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 @@ -5,7 +5,7 @@ from common import AgentRegistrationData, AgentSignals, OperatingSystem from common.agent_configuration import AgentConfiguration from common.agent_events import AbstractAgentEvent from common.credentials import Credentials -from common.types import SocketAddress +from common.types import AgentID, SocketAddress class IIslandAPIClient(ABC): @@ -30,10 +30,11 @@ class IIslandAPIClient(ABC): """ @abstractmethod - def send_log(self, log_contents: str): + def send_log(self, agent_id: AgentID, log_contents: str): """ Send the contents of the agent's log to the island + :param agent_id: The ID of the agent whose logs are being sent :param log_contents: The contents of the agent's log :raises IslandAPIConnectionError: If the client cannot successfully connect to the island :raises IslandAPIRequestError: If an error occurs while attempting to connect to the diff --git a/monkey/infection_monkey/monkey.py b/monkey/infection_monkey/monkey.py index acd10ce95..c54569360 100644 --- a/monkey/infection_monkey/monkey.py +++ b/monkey/infection_monkey/monkey.py @@ -111,6 +111,7 @@ class InfectionMonkey: self._singleton = SystemSingleton() self._opts = self._get_arguments(args) + self._agent_id = get_agent_id() self._agent_event_serializer_registry = self._setup_agent_event_serializers() @@ -122,7 +123,7 @@ class InfectionMonkey: server_address=self._island_address, island_api_client=self._island_api_client ) self._control_channel = ControlChannel( - str(self._island_address), get_agent_id(), self._island_api_client + str(self._island_address), self._agent_id, self._island_api_client ) self._register_agent() @@ -176,7 +177,7 @@ class InfectionMonkey: def _register_agent(self): agent_registration_data = AgentRegistrationData( - id=get_agent_id(), + id=self._agent_id, machine_hardware_id=get_machine_id(), start_time=agent_process.get_start_time(), # parent_id=parent, @@ -496,11 +497,11 @@ class InfectionMonkey: monkey_log_path = get_agent_log_path() if monkey_log_path.is_file(): with open(monkey_log_path, "r") as f: - log = f.read() + log_contents = f.read() else: - log = "" + log_contents = "" - self._control_client.send_log(log) + self._island_api_client.send_log(self._agent_id, log_contents) @staticmethod def _self_delete() -> bool: diff --git a/monkey/tests/unit_tests/infection_monkey/island_api_client/test_http_island_api_client.py b/monkey/tests/unit_tests/infection_monkey/island_api_client/test_http_island_api_client.py index 56a480082..e65ba4eba 100644 --- a/monkey/tests/unit_tests/infection_monkey/island_api_client/test_http_island_api_client.py +++ b/monkey/tests/unit_tests/infection_monkey/island_api_client/test_http_island_api_client.py @@ -37,7 +37,7 @@ AGENT_REGISTRATION = AgentRegistrationData( TIMESTAMP = 123456789 ISLAND_URI = f"https://{SERVER}/api?action=is-up" -ISLAND_SEND_LOG_URI = f"https://{SERVER}/api/log" +ISLAND_SEND_LOG_URI = f"https://{SERVER}/api/agent-logs/{AGENT_ID}" ISLAND_GET_PBA_FILE_URI = f"https://{SERVER}/api/pba/download/{PBA_FILE}" ISLAND_GET_AGENT_BINARY_URI = f"https://{SERVER}/api/agent-binaries/{WINDOWS}" ISLAND_SEND_EVENTS_URI = f"https://{SERVER}/api/agent-events" @@ -119,8 +119,8 @@ def test_island_api_client__send_log(island_api_client, actual_error, expected_e island_api_client.connect(SERVER) with pytest.raises(expected_error): - m.post(ISLAND_SEND_LOG_URI, exc=actual_error) - island_api_client.send_log(log_contents="some_data") + m.put(ISLAND_SEND_LOG_URI, exc=actual_error) + island_api_client.send_log(agent_id=AGENT_ID, log_contents="some_data") @pytest.mark.parametrize( @@ -136,8 +136,8 @@ def test_island_api_client_send_log__status_code(island_api_client, status_code, island_api_client.connect(SERVER) with pytest.raises(expected_error): - m.post(ISLAND_SEND_LOG_URI, status_code=status_code) - island_api_client.send_log(log_contents="some_data") + m.put(ISLAND_SEND_LOG_URI, status_code=status_code) + island_api_client.send_log(agent_id=AGENT_ID, log_contents="some_data") @pytest.mark.parametrize(