From f39007b0ce7312ac8017bd40464ad80e6cb7f6ca Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Tue, 20 Sep 2022 12:29:29 +0200 Subject: [PATCH] Agent: Implement send_events in HTTPIslandAPIClient --- .../http_island_api_client.py | 13 +++++++ .../test_http_island_api_client.py | 36 +++++++++++++++++++ 2 files changed, 49 insertions(+) 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 37feb8942..ab444c3e8 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 @@ -1,8 +1,10 @@ import functools import logging +from typing import Sequence import requests +from common.agent_event_serializers import JSONSerializable from common.common_consts.timeouts import LONG_REQUEST_TIMEOUT, MEDIUM_REQUEST_TIMEOUT from . import ( @@ -76,3 +78,14 @@ class HTTPIslandAPIClient(IIslandAPIClient): response.raise_for_status() return response.content + + @handle_island_errors + def send_events(self, events: Sequence[JSONSerializable]): + response = requests.post( # noqa: DUO123 + f"{self._api_url}/agent-events", + json=events, + verify=False, + timeout=MEDIUM_REQUEST_TIMEOUT, + ) + + response.raise_for_status() 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 bd6bfcb41..6562bf153 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 @@ -17,6 +17,7 @@ PBA_FILE = "dummy.pba" ISLAND_URI = f"https://{SERVER}/api?action=is-up" ISLAND_SEND_LOG_URI = f"https://{SERVER}/api/log" ISLAND_GET_PBA_FILE_URI = f"https://{SERVER}/api/pba/download/{PBA_FILE}" +ISLAND_SEND_EVENTS_URI = f"https://{SERVER}/api/agent-events" @pytest.mark.parametrize( @@ -118,3 +119,38 @@ def test_island_api_client_get_pba_file__status_code(status_code, expected_error with pytest.raises(expected_error): m.get(ISLAND_GET_PBA_FILE_URI, status_code=status_code) island_api_client.get_pba_file(filename=PBA_FILE) + + +@pytest.mark.parametrize( + "actual_error, expected_error", + [ + (requests.exceptions.ConnectionError, IslandAPIConnectionError), + (TimeoutError, IslandAPITimeoutError), + (Exception, IslandAPIError), + ], +) +def test_island_api_client__send_events(actual_error, expected_error): + with requests_mock.Mocker() as m: + m.get(ISLAND_URI) + island_api_client = HTTPIslandAPIClient(SERVER) + + with pytest.raises(expected_error): + m.post(ISLAND_SEND_EVENTS_URI, exc=actual_error) + island_api_client.send_events(events="some_data") + + +@pytest.mark.parametrize( + "status_code, expected_error", + [ + (401, IslandAPIRequestError), + (501, IslandAPIRequestFailedError), + ], +) +def test_island_api_client_send_events__status_code(status_code, expected_error): + with requests_mock.Mocker() as m: + m.get(ISLAND_URI) + island_api_client = HTTPIslandAPIClient(SERVER) + + with pytest.raises(expected_error): + m.post(ISLAND_SEND_EVENTS_URI, status_code=status_code) + island_api_client.send_events(events="some_data")