diff --git a/monkey/monkey_island/cc/resources/agents.py b/monkey/monkey_island/cc/resources/agents.py index b727bd9ae..4fb9837ff 100644 --- a/monkey/monkey_island/cc/resources/agents.py +++ b/monkey/monkey_island/cc/resources/agents.py @@ -5,6 +5,7 @@ from http import HTTPStatus from flask import make_response, request from common import AgentRegistrationData +from monkey_island.cc.event_queue import IIslandEventQueue, IslandEventTopic from monkey_island.cc.resources.AbstractResource import AbstractResource logger = logging.getLogger(__name__) @@ -13,12 +14,18 @@ logger = logging.getLogger(__name__) class Agents(AbstractResource): urls = ["/api/agents"] + def __init__(self, island_event_queue: IIslandEventQueue): + self._island_event_queue = island_event_queue + def post(self): try: # Just parse for now agent_registration_data = AgentRegistrationData(**request.json) logger.debug(f"Agent registered: {agent_registration_data}") + self._island_event_queue( + IslandEventTopic.AGENT_CONNECTED, agent_registration_data=agent_registration_data + ) return make_response({}, HTTPStatus.NO_CONTENT) except (TypeError, ValueError, json.JSONDecodeError) as err: diff --git a/monkey/tests/unit_tests/monkey_island/cc/resources/test_agents.py b/monkey/tests/unit_tests/monkey_island/cc/resources/test_agents.py index 3ab90feb6..8bb61250b 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/resources/test_agents.py +++ b/monkey/tests/unit_tests/monkey_island/cc/resources/test_agents.py @@ -1,8 +1,12 @@ from http import HTTPStatus +from unittest.mock import MagicMock from uuid import UUID +import pytest +from tests.common import StubDIContainer from tests.unit_tests.monkey_island.conftest import get_url_for_resource +from monkey_island.cc.event_queue import IIslandEventQueue from monkey_island.cc.resources import Agents AGENTS_URL = get_url_for_resource(Agents) @@ -17,6 +21,15 @@ AGENT_REGISTRATION_DICT = { } +@pytest.fixture +def flask_client(build_flask_client): + container = StubDIContainer() + container.register_instance(IIslandEventQueue, MagicMock(spec=IIslandEventQueue)) + + with build_flask_client(container) as flask_client: + yield flask_client + + def test_agent_registration(flask_client): print(AGENTS_URL) resp = flask_client.post(