From 0e2b165c158c69d512dccc7724797c93b77d3866 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 12:18:50 +0530 Subject: [PATCH 01/42] Agent: Rename IEventSerializer -> IAgentEventSerializer --- monkey/common/event_serializers/__init__.py | 2 +- .../event_serializers/event_serializer_registry.py | 12 +++++++----- .../common/event_serializers/i_event_serializer.py | 2 +- .../event_serializers/pydantic_event_serializer.py | 4 ++-- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/monkey/common/event_serializers/__init__.py b/monkey/common/event_serializers/__init__.py index c654f75f6a3..ac26002a57c 100644 --- a/monkey/common/event_serializers/__init__.py +++ b/monkey/common/event_serializers/__init__.py @@ -1,5 +1,5 @@ from .consts import EVENT_TYPE_FIELD -from .i_event_serializer import IEventSerializer, JSONSerializable +from .i_event_serializer import IAgentEventSerializer, JSONSerializable from .event_serializer_registry import EventSerializerRegistry from .pydantic_event_serializer import PydanticEventSerializer from .register import register_common_agent_event_serializers diff --git a/monkey/common/event_serializers/event_serializer_registry.py b/monkey/common/event_serializers/event_serializer_registry.py index b4f43f7b097..09134798b62 100644 --- a/monkey/common/event_serializers/event_serializer_registry.py +++ b/monkey/common/event_serializers/event_serializer_registry.py @@ -1,6 +1,6 @@ from typing import Type, Union -from common.event_serializers import IEventSerializer +from common.event_serializers import IAgentEventSerializer from common.events import AbstractAgentEvent @@ -22,18 +22,20 @@ def __init__(self): self._registry = {} def __setitem__( - self, event_class: Type[AbstractAgentEvent], event_serializer: IEventSerializer + self, event_class: Type[AbstractAgentEvent], event_serializer: IAgentEventSerializer ): if not issubclass(event_class, AbstractAgentEvent): raise TypeError(f"Event class must be of type: {AbstractAgentEvent.__name__}") - if not isinstance(event_serializer, IEventSerializer): - raise TypeError(f"Event serializer must be of type: {IEventSerializer.__name__}") + if not isinstance(event_serializer, IAgentEventSerializer): + raise TypeError(f"Event serializer must be of type: {IAgentEventSerializer.__name__}") self._registry[event_class] = event_serializer self._registry[event_class.__name__] = event_serializer - def __getitem__(self, event_class: Union[str, Type[AbstractAgentEvent]]) -> IEventSerializer: + def __getitem__( + self, event_class: Union[str, Type[AbstractAgentEvent]] + ) -> IAgentEventSerializer: if not (isinstance(event_class, str) or issubclass(event_class, AbstractAgentEvent)): raise TypeError( f"Registry get key {event_class} must be of type: {AbstractAgentEvent.__name__} or " diff --git a/monkey/common/event_serializers/i_event_serializer.py b/monkey/common/event_serializers/i_event_serializer.py index dd6d9ef1973..9a56fcc65a5 100644 --- a/monkey/common/event_serializers/i_event_serializer.py +++ b/monkey/common/event_serializers/i_event_serializer.py @@ -14,7 +14,7 @@ ] -class IEventSerializer(ABC): +class IAgentEventSerializer(ABC): """ Manages serialization and deserialization of events """ diff --git a/monkey/common/event_serializers/pydantic_event_serializer.py b/monkey/common/event_serializers/pydantic_event_serializer.py index 2291f3de9e4..dfc26586574 100644 --- a/monkey/common/event_serializers/pydantic_event_serializer.py +++ b/monkey/common/event_serializers/pydantic_event_serializer.py @@ -4,14 +4,14 @@ from common.events import AbstractAgentEvent from common.utils.code_utils import del_key -from . import EVENT_TYPE_FIELD, IEventSerializer, JSONSerializable +from . import EVENT_TYPE_FIELD, IAgentEventSerializer, JSONSerializable logger = logging.getLogger(__name__) T = TypeVar("T", bound=AbstractAgentEvent) -class PydanticEventSerializer(IEventSerializer, Generic[T]): +class PydanticEventSerializer(IAgentEventSerializer, Generic[T]): def __init__(self, event_class: Type[T]): self._event_class = event_class From 619dc68c0efa45fdfad55e0947dfbb0e15737d6e Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 12:19:24 +0530 Subject: [PATCH 02/42] UT: Use IAgentEventSerializer in place of IEventSerializer --- .../event_serializers/test_event_serializer_registry.py | 8 ++++---- .../event_serializers/test_pydantic_event_serializer.py | 8 ++++++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/monkey/tests/unit_tests/common/event_serializers/test_event_serializer_registry.py b/monkey/tests/unit_tests/common/event_serializers/test_event_serializer_registry.py index f83c54ba415..f918ede2371 100644 --- a/monkey/tests/unit_tests/common/event_serializers/test_event_serializer_registry.py +++ b/monkey/tests/unit_tests/common/event_serializers/test_event_serializer_registry.py @@ -3,7 +3,7 @@ import pytest from pydantic import Field -from common.event_serializers import EventSerializerRegistry, IEventSerializer +from common.event_serializers import EventSerializerRegistry, IAgentEventSerializer from common.events import AbstractAgentEvent @@ -19,8 +19,8 @@ class NoneEvent(AbstractAgentEvent): none_param: float = Field(default=1.0) -SOME_SERIALIZER = MagicMock(spec=IEventSerializer) -OTHER_SERIALIZER = MagicMock(spec=IEventSerializer) +SOME_SERIALIZER = MagicMock(spec=IAgentEventSerializer) +OTHER_SERIALIZER = MagicMock(spec=IAgentEventSerializer) @pytest.fixture @@ -50,7 +50,7 @@ def test_event_serializer_registry_set_unsupported_type(event_serializer_registr def test_event_serializer_registry_set_unsupported_type_key(event_serializer_registry): with pytest.raises(TypeError): - event_serializer_registry["BogusKey"] = MagicMock(spec=IEventSerializer) + event_serializer_registry["BogusKey"] = MagicMock(spec=IAgentEventSerializer) def test_event_serializer_registry_get_unsuported_type(event_serializer_registry): diff --git a/monkey/tests/unit_tests/common/event_serializers/test_pydantic_event_serializer.py b/monkey/tests/unit_tests/common/event_serializers/test_pydantic_event_serializer.py index 4958f0303e7..cd545e43236 100644 --- a/monkey/tests/unit_tests/common/event_serializers/test_pydantic_event_serializer.py +++ b/monkey/tests/unit_tests/common/event_serializers/test_pydantic_event_serializer.py @@ -5,7 +5,11 @@ import pytest from pydantic import Field -from common.event_serializers import EVENT_TYPE_FIELD, IEventSerializer, PydanticEventSerializer +from common.event_serializers import ( + EVENT_TYPE_FIELD, + IAgentEventSerializer, + PydanticEventSerializer, +) from common.events import AbstractAgentEvent AGENT_ID = UUID("f811ad00-5a68-4437-bd51-7b5cc1768ad5") @@ -26,7 +30,7 @@ class PydanticEvent(AbstractAgentEvent): @pytest.fixture -def pydantic_event_serializer() -> IEventSerializer: +def pydantic_event_serializer() -> IAgentEventSerializer: return PydanticEventSerializer(PydanticEvent) From 3b22bf0cb4dd1b4aa7b9d6866426061dc3782999 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 12:21:11 +0530 Subject: [PATCH 03/42] Common:Rename i_event_serializer.py -> i_agent_event_serializer.py --- monkey/common/event_serializers/__init__.py | 2 +- .../{i_event_serializer.py => i_agent_event_serializer.py} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename monkey/common/event_serializers/{i_event_serializer.py => i_agent_event_serializer.py} (100%) diff --git a/monkey/common/event_serializers/__init__.py b/monkey/common/event_serializers/__init__.py index ac26002a57c..2fd901681e1 100644 --- a/monkey/common/event_serializers/__init__.py +++ b/monkey/common/event_serializers/__init__.py @@ -1,5 +1,5 @@ from .consts import EVENT_TYPE_FIELD -from .i_event_serializer import IAgentEventSerializer, JSONSerializable +from .i_agent_event_serializer import IAgentEventSerializer, JSONSerializable from .event_serializer_registry import EventSerializerRegistry from .pydantic_event_serializer import PydanticEventSerializer from .register import register_common_agent_event_serializers diff --git a/monkey/common/event_serializers/i_event_serializer.py b/monkey/common/event_serializers/i_agent_event_serializer.py similarity index 100% rename from monkey/common/event_serializers/i_event_serializer.py rename to monkey/common/event_serializers/i_agent_event_serializer.py From c4ab5d99a9eceacc47551496eeefa1a0562e163c Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 12:22:10 +0530 Subject: [PATCH 04/42] Agent: Fix import in agent_event_forwarder.py --- monkey/infection_monkey/agent_event_forwarder.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/monkey/infection_monkey/agent_event_forwarder.py b/monkey/infection_monkey/agent_event_forwarder.py index f8e4dfef7ca..cb4f8bdda0f 100644 --- a/monkey/infection_monkey/agent_event_forwarder.py +++ b/monkey/infection_monkey/agent_event_forwarder.py @@ -6,8 +6,7 @@ import requests from common.common_consts.timeouts import MEDIUM_REQUEST_TIMEOUT -from common.event_serializers import EventSerializerRegistry -from common.event_serializers.i_event_serializer import JSONSerializable +from common.event_serializers import EventSerializerRegistry, JSONSerializable from common.events import AbstractAgentEvent from infection_monkey.utils.threading import create_daemon_thread From 6ced4af5dcdc3f3d47ef2c0a0c1a37bb57ed6b7c Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 12:24:44 +0530 Subject: [PATCH 05/42] Common: Rename EventSerializerRegistry -> AgentEventSerializerRegistry --- monkey/common/event_serializers/__init__.py | 2 +- .../common/event_serializers/event_serializer_registry.py | 4 ++-- monkey/common/event_serializers/register.py | 6 ++++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/monkey/common/event_serializers/__init__.py b/monkey/common/event_serializers/__init__.py index 2fd901681e1..e8c35607df9 100644 --- a/monkey/common/event_serializers/__init__.py +++ b/monkey/common/event_serializers/__init__.py @@ -1,5 +1,5 @@ from .consts import EVENT_TYPE_FIELD from .i_agent_event_serializer import IAgentEventSerializer, JSONSerializable -from .event_serializer_registry import EventSerializerRegistry +from .event_serializer_registry import AgentEventSerializerRegistry from .pydantic_event_serializer import PydanticEventSerializer from .register import register_common_agent_event_serializers diff --git a/monkey/common/event_serializers/event_serializer_registry.py b/monkey/common/event_serializers/event_serializer_registry.py index 09134798b62..c750a1e8930 100644 --- a/monkey/common/event_serializers/event_serializer_registry.py +++ b/monkey/common/event_serializers/event_serializer_registry.py @@ -4,12 +4,12 @@ from common.events import AbstractAgentEvent -class EventSerializerRegistry: +class AgentEventSerializerRegistry: """ Registry for event serializers using event class. Example: - event_serializer_registry = EventSerializerRegistry() + event_serializer_registry = AgentEventSerializerRegistry() event_serializer_registry[MyEvent] = MyEventSerializer() my_event_dict = {"type": "MyEvent", "data": "123"} diff --git a/monkey/common/event_serializers/register.py b/monkey/common/event_serializers/register.py index 6c533c9bffa..ddf6a288ded 100644 --- a/monkey/common/event_serializers/register.py +++ b/monkey/common/event_serializers/register.py @@ -1,9 +1,11 @@ from common.events import CredentialsStolenEvent -from . import EventSerializerRegistry, PydanticEventSerializer +from . import AgentEventSerializerRegistry, PydanticEventSerializer -def register_common_agent_event_serializers(event_serializer_registry: EventSerializerRegistry): +def register_common_agent_event_serializers( + event_serializer_registry: AgentEventSerializerRegistry, +): event_serializer_registry[CredentialsStolenEvent] = PydanticEventSerializer( CredentialsStolenEvent ) From 19a59bf3c272ad3dc720b9947da73ea001ed5545 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 12:27:08 +0530 Subject: [PATCH 06/42] Agent: Use AgentEventSerializerRegistry in place of EventSerializerRegistry --- monkey/infection_monkey/agent_event_forwarder.py | 4 ++-- monkey/infection_monkey/monkey.py | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/monkey/infection_monkey/agent_event_forwarder.py b/monkey/infection_monkey/agent_event_forwarder.py index cb4f8bdda0f..c0a731fcfd1 100644 --- a/monkey/infection_monkey/agent_event_forwarder.py +++ b/monkey/infection_monkey/agent_event_forwarder.py @@ -6,7 +6,7 @@ import requests from common.common_consts.timeouts import MEDIUM_REQUEST_TIMEOUT -from common.event_serializers import EventSerializerRegistry, JSONSerializable +from common.event_serializers import AgentEventSerializerRegistry, JSONSerializable from common.events import AbstractAgentEvent from infection_monkey.utils.threading import create_daemon_thread @@ -23,7 +23,7 @@ class AgentEventForwarder: """ def __init__( - self, server_address: str, agent_event_serializer_registry: EventSerializerRegistry + self, server_address: str, agent_event_serializer_registry: AgentEventSerializerRegistry ): self._server_address = server_address self._agent_event_serializer_registry = agent_event_serializer_registry diff --git a/monkey/infection_monkey/monkey.py b/monkey/infection_monkey/monkey.py index dfc34bec2e5..ccc03adf9b8 100644 --- a/monkey/infection_monkey/monkey.py +++ b/monkey/infection_monkey/monkey.py @@ -11,7 +11,7 @@ from common.event_queue import IAgentEventQueue, PyPubSubAgentEventQueue from common.event_serializers import ( - EventSerializerRegistry, + AgentEventSerializerRegistry, register_common_agent_event_serializers, ) from common.events import CredentialsStolenEvent @@ -213,8 +213,8 @@ def _setup(self): register_signal_handlers(self._master) # TODO: This is just a placeholder for now. We will modify/integrate it with PR #2279. - def _setup_agent_event_serializers(self) -> EventSerializerRegistry: - agent_event_serializer_registry = EventSerializerRegistry() + def _setup_agent_event_serializers(self) -> AgentEventSerializerRegistry: + agent_event_serializer_registry = AgentEventSerializerRegistry() register_common_agent_event_serializers(agent_event_serializer_registry) return agent_event_serializer_registry @@ -259,7 +259,7 @@ def _subscribe_events( event_queue: IAgentEventQueue, propagation_credentials_repository: IPropagationCredentialsRepository, server_address: str, - agent_event_serializer_registry: EventSerializerRegistry, + agent_event_serializer_registry: AgentEventSerializerRegistry, ): event_queue.subscribe_type( CredentialsStolenEvent, From 4a23f5a95b9b5fe6ea18bbb2ea2c20e1d1b7616e Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 12:28:18 +0530 Subject: [PATCH 07/42] Island: Use AgentEventSerializerRegistry in place of EventSerializerRegistry --- .../monkey_island/cc/repository/mongo_event_repository.py | 6 ++++-- monkey/monkey_island/cc/resources/events.py | 4 ++-- monkey/monkey_island/cc/server_setup.py | 6 +++--- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/monkey/monkey_island/cc/repository/mongo_event_repository.py b/monkey/monkey_island/cc/repository/mongo_event_repository.py index 6614e165b54..c9f1f33d74c 100644 --- a/monkey/monkey_island/cc/repository/mongo_event_repository.py +++ b/monkey/monkey_island/cc/repository/mongo_event_repository.py @@ -2,7 +2,7 @@ from pymongo import MongoClient -from common.event_serializers import EVENT_TYPE_FIELD, EventSerializerRegistry +from common.event_serializers import EVENT_TYPE_FIELD, AgentEventSerializerRegistry from common.events import AbstractAgentEvent from common.types import AgentID from monkey_island.cc.repository import IEventRepository @@ -14,7 +14,9 @@ class MongoEventRepository(IEventRepository): """A repository for storing and retrieving events in MongoDB""" - def __init__(self, mongo_client: MongoClient, serializer_registry: EventSerializerRegistry): + def __init__( + self, mongo_client: MongoClient, serializer_registry: AgentEventSerializerRegistry + ): self._events_collection = mongo_client.monkey_island.events self._serializers = serializer_registry diff --git a/monkey/monkey_island/cc/resources/events.py b/monkey/monkey_island/cc/resources/events.py index d1dedeb9045..97c42c252d3 100644 --- a/monkey/monkey_island/cc/resources/events.py +++ b/monkey/monkey_island/cc/resources/events.py @@ -4,7 +4,7 @@ from flask import request from common.event_queue import IAgentEventQueue -from common.event_serializers import EVENT_TYPE_FIELD, EventSerializerRegistry +from common.event_serializers import EVENT_TYPE_FIELD, AgentEventSerializerRegistry from monkey_island.cc.resources.AbstractResource import AbstractResource logger = logging.getLogger(__name__) @@ -16,7 +16,7 @@ class Events(AbstractResource): def __init__( self, agent_event_queue: IAgentEventQueue, - event_serializer_registry: EventSerializerRegistry, + event_serializer_registry: AgentEventSerializerRegistry, ): self._agent_event_queue = agent_event_queue self._event_serializer_registry = event_serializer_registry diff --git a/monkey/monkey_island/cc/server_setup.py b/monkey/monkey_island/cc/server_setup.py index 4f6989e12de..27fc3bd77e1 100644 --- a/monkey/monkey_island/cc/server_setup.py +++ b/monkey/monkey_island/cc/server_setup.py @@ -24,7 +24,7 @@ from common import DIContainer # noqa: E402 from common.event_serializers import ( # noqa: E402 - EventSerializerRegistry, + AgentEventSerializerRegistry, register_common_agent_event_serializers, ) from common.version import get_version # noqa: E402 @@ -140,10 +140,10 @@ def _initialize_di_container( def _setup_agent_event_serializers(container: DIContainer): - agent_event_serializer_registry = EventSerializerRegistry() + agent_event_serializer_registry = AgentEventSerializerRegistry() register_common_agent_event_serializers(agent_event_serializer_registry) - container.register_instance(EventSerializerRegistry, agent_event_serializer_registry) + container.register_instance(AgentEventSerializerRegistry, agent_event_serializer_registry) def _initialize_mongodb_connection(start_mongodb: bool, data_dir: Path): From 2568d527309f76e3b919356d5dc67840f772385d Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 12:28:47 +0530 Subject: [PATCH 08/42] UT: Use AgentEventSerializerRegistry in place of EventSerializerRegistry --- .../event_serializers/test_event_serializer_registry.py | 4 ++-- .../cc/repository/test_mongo_event_repository.py | 6 +++--- .../unit_tests/monkey_island/cc/resources/test_events.py | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/monkey/tests/unit_tests/common/event_serializers/test_event_serializer_registry.py b/monkey/tests/unit_tests/common/event_serializers/test_event_serializer_registry.py index f918ede2371..9e500f419e9 100644 --- a/monkey/tests/unit_tests/common/event_serializers/test_event_serializer_registry.py +++ b/monkey/tests/unit_tests/common/event_serializers/test_event_serializer_registry.py @@ -3,7 +3,7 @@ import pytest from pydantic import Field -from common.event_serializers import EventSerializerRegistry, IAgentEventSerializer +from common.event_serializers import AgentEventSerializerRegistry, IAgentEventSerializer from common.events import AbstractAgentEvent @@ -25,7 +25,7 @@ class NoneEvent(AbstractAgentEvent): @pytest.fixture def event_serializer_registry(): - event_serializer_registry = EventSerializerRegistry() + event_serializer_registry = AgentEventSerializerRegistry() event_serializer_registry[SomeEvent] = SOME_SERIALIZER event_serializer_registry[OtherEvent] = OTHER_SERIALIZER diff --git a/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_event_repository.py b/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_event_repository.py index 64de7edc8a3..1da6366e2b3 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_event_repository.py +++ b/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_event_repository.py @@ -6,7 +6,7 @@ import pytest from pydantic import Field -from common.event_serializers import EventSerializerRegistry, PydanticEventSerializer +from common.event_serializers import AgentEventSerializerRegistry, PydanticEventSerializer from common.events import AbstractAgentEvent from monkey_island.cc.repository import ( IEventRepository, @@ -34,8 +34,8 @@ class FakeAgentItemEvent(AbstractAgentEvent): @pytest.fixture -def event_serializer_registry() -> EventSerializerRegistry: - registry = EventSerializerRegistry() +def event_serializer_registry() -> AgentEventSerializerRegistry: + registry = AgentEventSerializerRegistry() registry[FakeAgentEvent] = PydanticEventSerializer(FakeAgentEvent) registry[FakeAgentItemEvent] = PydanticEventSerializer(FakeAgentItemEvent) return registry diff --git a/monkey/tests/unit_tests/monkey_island/cc/resources/test_events.py b/monkey/tests/unit_tests/monkey_island/cc/resources/test_events.py index 5d8db24f5af..0a9ea7777ff 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/resources/test_events.py +++ b/monkey/tests/unit_tests/monkey_island/cc/resources/test_events.py @@ -6,7 +6,7 @@ from tests.common import StubDIContainer from common.event_queue import IAgentEventQueue -from common.event_serializers import EventSerializerRegistry, PydanticEventSerializer +from common.event_serializers import AgentEventSerializerRegistry, PydanticEventSerializer from common.events import AbstractAgentEvent from monkey_island.cc.resources import Events @@ -88,8 +88,8 @@ def mock_agent_event_queue(): @pytest.fixture -def event_serializer_registry() -> EventSerializerRegistry: - event_serializer_registry = EventSerializerRegistry() +def event_serializer_registry() -> AgentEventSerializerRegistry: + event_serializer_registry = AgentEventSerializerRegistry() event_serializer_registry[SomeAgentEvent] = PydanticEventSerializer(SomeAgentEvent) event_serializer_registry[OtherAgentEvent] = PydanticEventSerializer(OtherAgentEvent) event_serializer_registry[DifferentAgentEvent] = PydanticEventSerializer(DifferentAgentEvent) @@ -102,7 +102,7 @@ def flask_client(build_flask_client, mock_agent_event_queue, event_serializer_re container = StubDIContainer() container.register_instance(IAgentEventQueue, mock_agent_event_queue) - container.register_instance(EventSerializerRegistry, event_serializer_registry) + container.register_instance(AgentEventSerializerRegistry, event_serializer_registry) with build_flask_client(container) as flask_client: yield flask_client From 940b479ca2dcbf2823299d5976049707c48d110f Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 12:30:13 +0530 Subject: [PATCH 09/42] Common: Rename event_serializer_registry.py -> agent_event_serializer_registry.py --- monkey/common/event_serializers/__init__.py | 2 +- ...erializer_registry.py => agent_event_serializer_registry.py} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename monkey/common/event_serializers/{event_serializer_registry.py => agent_event_serializer_registry.py} (100%) diff --git a/monkey/common/event_serializers/__init__.py b/monkey/common/event_serializers/__init__.py index e8c35607df9..10499e3d063 100644 --- a/monkey/common/event_serializers/__init__.py +++ b/monkey/common/event_serializers/__init__.py @@ -1,5 +1,5 @@ from .consts import EVENT_TYPE_FIELD from .i_agent_event_serializer import IAgentEventSerializer, JSONSerializable -from .event_serializer_registry import AgentEventSerializerRegistry +from .agent_event_serializer_registry import AgentEventSerializerRegistry from .pydantic_event_serializer import PydanticEventSerializer from .register import register_common_agent_event_serializers diff --git a/monkey/common/event_serializers/event_serializer_registry.py b/monkey/common/event_serializers/agent_event_serializer_registry.py similarity index 100% rename from monkey/common/event_serializers/event_serializer_registry.py rename to monkey/common/event_serializers/agent_event_serializer_registry.py From bf5e32a2523454f7671648662ea81b165b6bf0f5 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 12:31:06 +0530 Subject: [PATCH 10/42] Project: Remove irrelevant Vulture allowlist entries --- vulture_allowlist.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/vulture_allowlist.py b/vulture_allowlist.py index 23ec0f252ff..049dcfb6520 100644 --- a/vulture_allowlist.py +++ b/vulture_allowlist.py @@ -299,14 +299,6 @@ subscribe_all_events # common\event_queue\pypubsub_agent_event_queue.py -# TODO: Remove once #2179 is closed -EventSerializerRegistry -serialize -event -deserialize -serialized_event -PydanticEventSerializer - # TODO: Remove once #2180 is closed IEventRepository.save_event IEventRepository.get_events_by_type From 43fd978ce4276ee9544461f51724045b0fa596cf Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 12:39:02 +0530 Subject: [PATCH 11/42] Common: Rename PydanticEventSerializer -> PydanticAgentEventSerializer --- monkey/common/event_serializers/__init__.py | 2 +- monkey/common/event_serializers/pydantic_event_serializer.py | 2 +- monkey/common/event_serializers/register.py | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/monkey/common/event_serializers/__init__.py b/monkey/common/event_serializers/__init__.py index 10499e3d063..e3174681532 100644 --- a/monkey/common/event_serializers/__init__.py +++ b/monkey/common/event_serializers/__init__.py @@ -1,5 +1,5 @@ from .consts import EVENT_TYPE_FIELD from .i_agent_event_serializer import IAgentEventSerializer, JSONSerializable from .agent_event_serializer_registry import AgentEventSerializerRegistry -from .pydantic_event_serializer import PydanticEventSerializer +from .pydantic_event_serializer import PydanticAgentEventSerializer from .register import register_common_agent_event_serializers diff --git a/monkey/common/event_serializers/pydantic_event_serializer.py b/monkey/common/event_serializers/pydantic_event_serializer.py index dfc26586574..3ecf12de8bf 100644 --- a/monkey/common/event_serializers/pydantic_event_serializer.py +++ b/monkey/common/event_serializers/pydantic_event_serializer.py @@ -11,7 +11,7 @@ T = TypeVar("T", bound=AbstractAgentEvent) -class PydanticEventSerializer(IAgentEventSerializer, Generic[T]): +class PydanticAgentEventSerializer(IAgentEventSerializer, Generic[T]): def __init__(self, event_class: Type[T]): self._event_class = event_class diff --git a/monkey/common/event_serializers/register.py b/monkey/common/event_serializers/register.py index ddf6a288ded..881a06f5b8c 100644 --- a/monkey/common/event_serializers/register.py +++ b/monkey/common/event_serializers/register.py @@ -1,11 +1,11 @@ from common.events import CredentialsStolenEvent -from . import AgentEventSerializerRegistry, PydanticEventSerializer +from . import AgentEventSerializerRegistry, PydanticAgentEventSerializer def register_common_agent_event_serializers( event_serializer_registry: AgentEventSerializerRegistry, ): - event_serializer_registry[CredentialsStolenEvent] = PydanticEventSerializer( + event_serializer_registry[CredentialsStolenEvent] = PydanticAgentEventSerializer( CredentialsStolenEvent ) From d729b514e53abf1d010297e2426a074aaccb34c8 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 12:43:56 +0530 Subject: [PATCH 12/42] UT: Use PydanticAgentEventSerializer in place of PydanticEventSerializer --- .../test_pydantic_event_serializer.py | 4 ++-- .../cc/repository/test_mongo_event_repository.py | 6 +++--- .../monkey_island/cc/resources/test_events.py | 10 ++++++---- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/monkey/tests/unit_tests/common/event_serializers/test_pydantic_event_serializer.py b/monkey/tests/unit_tests/common/event_serializers/test_pydantic_event_serializer.py index cd545e43236..eaa54eb02fe 100644 --- a/monkey/tests/unit_tests/common/event_serializers/test_pydantic_event_serializer.py +++ b/monkey/tests/unit_tests/common/event_serializers/test_pydantic_event_serializer.py @@ -8,7 +8,7 @@ from common.event_serializers import ( EVENT_TYPE_FIELD, IAgentEventSerializer, - PydanticEventSerializer, + PydanticAgentEventSerializer, ) from common.events import AbstractAgentEvent @@ -31,7 +31,7 @@ class PydanticEvent(AbstractAgentEvent): @pytest.fixture def pydantic_event_serializer() -> IAgentEventSerializer: - return PydanticEventSerializer(PydanticEvent) + return PydanticAgentEventSerializer(PydanticEvent) @pytest.mark.parametrize( diff --git a/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_event_repository.py b/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_event_repository.py index 1da6366e2b3..6a1f232ef1b 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_event_repository.py +++ b/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_event_repository.py @@ -6,7 +6,7 @@ import pytest from pydantic import Field -from common.event_serializers import AgentEventSerializerRegistry, PydanticEventSerializer +from common.event_serializers import AgentEventSerializerRegistry, PydanticAgentEventSerializer from common.events import AbstractAgentEvent from monkey_island.cc.repository import ( IEventRepository, @@ -36,8 +36,8 @@ class FakeAgentItemEvent(AbstractAgentEvent): @pytest.fixture def event_serializer_registry() -> AgentEventSerializerRegistry: registry = AgentEventSerializerRegistry() - registry[FakeAgentEvent] = PydanticEventSerializer(FakeAgentEvent) - registry[FakeAgentItemEvent] = PydanticEventSerializer(FakeAgentItemEvent) + registry[FakeAgentEvent] = PydanticAgentEventSerializer(FakeAgentEvent) + registry[FakeAgentItemEvent] = PydanticAgentEventSerializer(FakeAgentItemEvent) return registry diff --git a/monkey/tests/unit_tests/monkey_island/cc/resources/test_events.py b/monkey/tests/unit_tests/monkey_island/cc/resources/test_events.py index 0a9ea7777ff..74a69a6dacb 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/resources/test_events.py +++ b/monkey/tests/unit_tests/monkey_island/cc/resources/test_events.py @@ -6,7 +6,7 @@ from tests.common import StubDIContainer from common.event_queue import IAgentEventQueue -from common.event_serializers import AgentEventSerializerRegistry, PydanticEventSerializer +from common.event_serializers import AgentEventSerializerRegistry, PydanticAgentEventSerializer from common.events import AbstractAgentEvent from monkey_island.cc.resources import Events @@ -90,9 +90,11 @@ def mock_agent_event_queue(): @pytest.fixture def event_serializer_registry() -> AgentEventSerializerRegistry: event_serializer_registry = AgentEventSerializerRegistry() - event_serializer_registry[SomeAgentEvent] = PydanticEventSerializer(SomeAgentEvent) - event_serializer_registry[OtherAgentEvent] = PydanticEventSerializer(OtherAgentEvent) - event_serializer_registry[DifferentAgentEvent] = PydanticEventSerializer(DifferentAgentEvent) + event_serializer_registry[SomeAgentEvent] = PydanticAgentEventSerializer(SomeAgentEvent) + event_serializer_registry[OtherAgentEvent] = PydanticAgentEventSerializer(OtherAgentEvent) + event_serializer_registry[DifferentAgentEvent] = PydanticAgentEventSerializer( + DifferentAgentEvent + ) return event_serializer_registry From f5a0b423194fcbaeeb462b1054a4af58e88bc389 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 12:45:02 +0530 Subject: [PATCH 13/42] Common: Rename pydantic_event_serializer.py -> pydantic_agent_event_serializer.py --- monkey/common/event_serializers/__init__.py | 2 +- ...c_event_serializer.py => pydantic_agent_event_serializer.py} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename monkey/common/event_serializers/{pydantic_event_serializer.py => pydantic_agent_event_serializer.py} (100%) diff --git a/monkey/common/event_serializers/__init__.py b/monkey/common/event_serializers/__init__.py index e3174681532..d42a47cb117 100644 --- a/monkey/common/event_serializers/__init__.py +++ b/monkey/common/event_serializers/__init__.py @@ -1,5 +1,5 @@ from .consts import EVENT_TYPE_FIELD from .i_agent_event_serializer import IAgentEventSerializer, JSONSerializable from .agent_event_serializer_registry import AgentEventSerializerRegistry -from .pydantic_event_serializer import PydanticAgentEventSerializer +from .pydantic_agent_event_serializer import PydanticAgentEventSerializer from .register import register_common_agent_event_serializers diff --git a/monkey/common/event_serializers/pydantic_event_serializer.py b/monkey/common/event_serializers/pydantic_agent_event_serializer.py similarity index 100% rename from monkey/common/event_serializers/pydantic_event_serializer.py rename to monkey/common/event_serializers/pydantic_agent_event_serializer.py From d694591d1e8d2f754175c221e406ada121349939 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 12:45:55 +0530 Subject: [PATCH 14/42] UT: Rename test_pydantic_event_serializer.py -> test_pydantic_agent_event_serializer.py --- ...vent_serializer.py => test_pydantic_agent_event_serializer.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename monkey/tests/unit_tests/common/event_serializers/{test_pydantic_event_serializer.py => test_pydantic_agent_event_serializer.py} (100%) diff --git a/monkey/tests/unit_tests/common/event_serializers/test_pydantic_event_serializer.py b/monkey/tests/unit_tests/common/event_serializers/test_pydantic_agent_event_serializer.py similarity index 100% rename from monkey/tests/unit_tests/common/event_serializers/test_pydantic_event_serializer.py rename to monkey/tests/unit_tests/common/event_serializers/test_pydantic_agent_event_serializer.py From 2f748516a827a5f9471d7c540d9b5c187422dc0f Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 12:48:01 +0530 Subject: [PATCH 15/42] UT: Change pydantic_event_serializer -> pydantic_agent_event_serializer in test_pydantic_agent_event_serializer.py --- .../test_pydantic_agent_event_serializer.py | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/monkey/tests/unit_tests/common/event_serializers/test_pydantic_agent_event_serializer.py b/monkey/tests/unit_tests/common/event_serializers/test_pydantic_agent_event_serializer.py index eaa54eb02fe..31c3080ee9b 100644 --- a/monkey/tests/unit_tests/common/event_serializers/test_pydantic_agent_event_serializer.py +++ b/monkey/tests/unit_tests/common/event_serializers/test_pydantic_agent_event_serializer.py @@ -30,7 +30,7 @@ class PydanticEvent(AbstractAgentEvent): @pytest.fixture -def pydantic_event_serializer() -> IAgentEventSerializer: +def pydantic_agent_event_serializer() -> IAgentEventSerializer: return PydanticAgentEventSerializer(PydanticEvent) @@ -38,28 +38,30 @@ def pydantic_event_serializer() -> IAgentEventSerializer: "event", [NotAgentEvent(some_field=1, other_field=2.0), SomeAgentEvent(source=AGENT_ID, bogus=2)], ) -def test_pydantic_event_serializer__serialize_wrong_type(pydantic_event_serializer, event): +def test_pydantic_agent_event_serializer__serialize_wrong_type( + pydantic_agent_event_serializer, event +): with pytest.raises(TypeError): - pydantic_event_serializer.serialize(event) + pydantic_agent_event_serializer.serialize(event) -def test_pydantic_event_serializer__deserialize_wrong_type(pydantic_event_serializer): +def test_pydantic_agent_event_serializer__deserialize_wrong_type(pydantic_agent_event_serializer): with pytest.raises(TypeError): - pydantic_event_serializer.deserialize("bla") + pydantic_agent_event_serializer.deserialize("bla") -def test_pydanitc_event_serializer__de_serialize(pydantic_event_serializer): +def test_pydantic_agent_event_serializer__de_serialize(pydantic_agent_event_serializer): pydantic_event = PydanticEvent(source=AGENT_ID, some_field="some_field") - serialized_event = pydantic_event_serializer.serialize(pydantic_event) - deserialized_object = pydantic_event_serializer.deserialize(serialized_event) + serialized_event = pydantic_agent_event_serializer.serialize(pydantic_event) + deserialized_object = pydantic_agent_event_serializer.deserialize(serialized_event) assert type(serialized_event) != type(deserialized_object) assert deserialized_object == pydantic_event -def test_pydanitc_event_serializer__serialize_inclued_type(pydantic_event_serializer): +def test_pydantic_event_serializer__serialize_inclued_type(pydantic_agent_event_serializer): pydantic_event = PydanticEvent(source=AGENT_ID, some_field="some_field") - serialized_event = pydantic_event_serializer.serialize(pydantic_event) + serialized_event = pydantic_agent_event_serializer.serialize(pydantic_event) assert serialized_event[EVENT_TYPE_FIELD] == PydanticEvent.__name__ From d7327272a08016c222e5e452a2f9a902e2016c72 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 12:52:55 +0530 Subject: [PATCH 16/42] Common: Rename event_serializers/ -> agent_event_serializers/ --- .../{event_serializers => agent_event_serializers}/__init__.py | 0 .../agent_event_serializer_registry.py | 2 +- .../{event_serializers => agent_event_serializers}/consts.py | 0 .../i_agent_event_serializer.py | 0 .../pydantic_agent_event_serializer.py | 0 .../{event_serializers => agent_event_serializers}/register.py | 0 6 files changed, 1 insertion(+), 1 deletion(-) rename monkey/common/{event_serializers => agent_event_serializers}/__init__.py (100%) rename monkey/common/{event_serializers => agent_event_serializers}/agent_event_serializer_registry.py (96%) rename monkey/common/{event_serializers => agent_event_serializers}/consts.py (100%) rename monkey/common/{event_serializers => agent_event_serializers}/i_agent_event_serializer.py (100%) rename monkey/common/{event_serializers => agent_event_serializers}/pydantic_agent_event_serializer.py (100%) rename monkey/common/{event_serializers => agent_event_serializers}/register.py (100%) diff --git a/monkey/common/event_serializers/__init__.py b/monkey/common/agent_event_serializers/__init__.py similarity index 100% rename from monkey/common/event_serializers/__init__.py rename to monkey/common/agent_event_serializers/__init__.py diff --git a/monkey/common/event_serializers/agent_event_serializer_registry.py b/monkey/common/agent_event_serializers/agent_event_serializer_registry.py similarity index 96% rename from monkey/common/event_serializers/agent_event_serializer_registry.py rename to monkey/common/agent_event_serializers/agent_event_serializer_registry.py index c750a1e8930..6e5dcee1010 100644 --- a/monkey/common/event_serializers/agent_event_serializer_registry.py +++ b/monkey/common/agent_event_serializers/agent_event_serializer_registry.py @@ -1,6 +1,6 @@ from typing import Type, Union -from common.event_serializers import IAgentEventSerializer +from common.agent_event_serializers import IAgentEventSerializer from common.events import AbstractAgentEvent diff --git a/monkey/common/event_serializers/consts.py b/monkey/common/agent_event_serializers/consts.py similarity index 100% rename from monkey/common/event_serializers/consts.py rename to monkey/common/agent_event_serializers/consts.py diff --git a/monkey/common/event_serializers/i_agent_event_serializer.py b/monkey/common/agent_event_serializers/i_agent_event_serializer.py similarity index 100% rename from monkey/common/event_serializers/i_agent_event_serializer.py rename to monkey/common/agent_event_serializers/i_agent_event_serializer.py diff --git a/monkey/common/event_serializers/pydantic_agent_event_serializer.py b/monkey/common/agent_event_serializers/pydantic_agent_event_serializer.py similarity index 100% rename from monkey/common/event_serializers/pydantic_agent_event_serializer.py rename to monkey/common/agent_event_serializers/pydantic_agent_event_serializer.py diff --git a/monkey/common/event_serializers/register.py b/monkey/common/agent_event_serializers/register.py similarity index 100% rename from monkey/common/event_serializers/register.py rename to monkey/common/agent_event_serializers/register.py From 922db9c58326bad6f6b07172fc2d15cce03705cb Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 12:53:50 +0530 Subject: [PATCH 17/42] Agent: Fix import common.event_serializers -> common.agent_event_serializers --- monkey/infection_monkey/agent_event_forwarder.py | 2 +- monkey/infection_monkey/monkey.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/monkey/infection_monkey/agent_event_forwarder.py b/monkey/infection_monkey/agent_event_forwarder.py index c0a731fcfd1..2458b7a6db8 100644 --- a/monkey/infection_monkey/agent_event_forwarder.py +++ b/monkey/infection_monkey/agent_event_forwarder.py @@ -5,8 +5,8 @@ import requests +from common.agent_event_serializers import AgentEventSerializerRegistry, JSONSerializable from common.common_consts.timeouts import MEDIUM_REQUEST_TIMEOUT -from common.event_serializers import AgentEventSerializerRegistry, JSONSerializable from common.events import AbstractAgentEvent from infection_monkey.utils.threading import create_daemon_thread diff --git a/monkey/infection_monkey/monkey.py b/monkey/infection_monkey/monkey.py index ccc03adf9b8..c1594efa8d3 100644 --- a/monkey/infection_monkey/monkey.py +++ b/monkey/infection_monkey/monkey.py @@ -9,11 +9,11 @@ from pubsub.core import Publisher -from common.event_queue import IAgentEventQueue, PyPubSubAgentEventQueue -from common.event_serializers import ( +from common.agent_event_serializers import ( AgentEventSerializerRegistry, register_common_agent_event_serializers, ) +from common.event_queue import IAgentEventQueue, PyPubSubAgentEventQueue from common.events import CredentialsStolenEvent from common.network.network_utils import address_to_ip_port from common.utils.argparse_types import positive_int From 91b973cfe3b3586192429c3679f6d44ff79b1004 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 12:54:37 +0530 Subject: [PATCH 18/42] Island: Fix import common.event_serializers -> common.agent_event_serializers --- monkey/monkey_island/cc/repository/mongo_event_repository.py | 2 +- monkey/monkey_island/cc/resources/events.py | 2 +- monkey/monkey_island/cc/server_setup.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/monkey/monkey_island/cc/repository/mongo_event_repository.py b/monkey/monkey_island/cc/repository/mongo_event_repository.py index c9f1f33d74c..5e8b4f21ed7 100644 --- a/monkey/monkey_island/cc/repository/mongo_event_repository.py +++ b/monkey/monkey_island/cc/repository/mongo_event_repository.py @@ -2,7 +2,7 @@ from pymongo import MongoClient -from common.event_serializers import EVENT_TYPE_FIELD, AgentEventSerializerRegistry +from common.agent_event_serializers import EVENT_TYPE_FIELD, AgentEventSerializerRegistry from common.events import AbstractAgentEvent from common.types import AgentID from monkey_island.cc.repository import IEventRepository diff --git a/monkey/monkey_island/cc/resources/events.py b/monkey/monkey_island/cc/resources/events.py index 97c42c252d3..42612ec6f60 100644 --- a/monkey/monkey_island/cc/resources/events.py +++ b/monkey/monkey_island/cc/resources/events.py @@ -3,8 +3,8 @@ from flask import request +from common.agent_event_serializers import EVENT_TYPE_FIELD, AgentEventSerializerRegistry from common.event_queue import IAgentEventQueue -from common.event_serializers import EVENT_TYPE_FIELD, AgentEventSerializerRegistry from monkey_island.cc.resources.AbstractResource import AbstractResource logger = logging.getLogger(__name__) diff --git a/monkey/monkey_island/cc/server_setup.py b/monkey/monkey_island/cc/server_setup.py index 27fc3bd77e1..6e04e612db5 100644 --- a/monkey/monkey_island/cc/server_setup.py +++ b/monkey/monkey_island/cc/server_setup.py @@ -23,7 +23,7 @@ sys.path.insert(0, MONKEY_ISLAND_DIR_BASE_PATH) from common import DIContainer # noqa: E402 -from common.event_serializers import ( # noqa: E402 +from common.agent_event_serializers import ( # noqa: E402 AgentEventSerializerRegistry, register_common_agent_event_serializers, ) From 7bfc2da5390ff32d766014dcba5c664edfab81ec Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 12:55:49 +0530 Subject: [PATCH 19/42] UT: Fix import common.event_serializers -> common.agent_event_serializers --- .../cc/repository/test_mongo_event_repository.py | 5 ++++- .../unit_tests/monkey_island/cc/resources/test_events.py | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_event_repository.py b/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_event_repository.py index 6a1f232ef1b..6c90e239629 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_event_repository.py +++ b/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_event_repository.py @@ -6,7 +6,10 @@ import pytest from pydantic import Field -from common.event_serializers import AgentEventSerializerRegistry, PydanticAgentEventSerializer +from common.agent_event_serializers import ( + AgentEventSerializerRegistry, + PydanticAgentEventSerializer, +) from common.events import AbstractAgentEvent from monkey_island.cc.repository import ( IEventRepository, diff --git a/monkey/tests/unit_tests/monkey_island/cc/resources/test_events.py b/monkey/tests/unit_tests/monkey_island/cc/resources/test_events.py index 74a69a6dacb..20c43f18761 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/resources/test_events.py +++ b/monkey/tests/unit_tests/monkey_island/cc/resources/test_events.py @@ -5,8 +5,11 @@ import pytest from tests.common import StubDIContainer +from common.agent_event_serializers import ( + AgentEventSerializerRegistry, + PydanticAgentEventSerializer, +) from common.event_queue import IAgentEventQueue -from common.event_serializers import AgentEventSerializerRegistry, PydanticAgentEventSerializer from common.events import AbstractAgentEvent from monkey_island.cc.resources import Events From edb1c97e0a43e173c8ea67dce5006b9cd24ba805 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 12:56:23 +0530 Subject: [PATCH 20/42] UT: Rename event_serializers/ -> agent_event_serializers/ --- .../test_event_serializer_registry.py | 2 +- .../test_pydantic_agent_event_serializer.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename monkey/tests/unit_tests/common/{event_serializers => agent_event_serializers}/test_event_serializer_registry.py (95%) rename monkey/tests/unit_tests/common/{event_serializers => agent_event_serializers}/test_pydantic_agent_event_serializer.py (97%) diff --git a/monkey/tests/unit_tests/common/event_serializers/test_event_serializer_registry.py b/monkey/tests/unit_tests/common/agent_event_serializers/test_event_serializer_registry.py similarity index 95% rename from monkey/tests/unit_tests/common/event_serializers/test_event_serializer_registry.py rename to monkey/tests/unit_tests/common/agent_event_serializers/test_event_serializer_registry.py index 9e500f419e9..ae9b26199ea 100644 --- a/monkey/tests/unit_tests/common/event_serializers/test_event_serializer_registry.py +++ b/monkey/tests/unit_tests/common/agent_event_serializers/test_event_serializer_registry.py @@ -3,7 +3,7 @@ import pytest from pydantic import Field -from common.event_serializers import AgentEventSerializerRegistry, IAgentEventSerializer +from common.agent_event_serializers import AgentEventSerializerRegistry, IAgentEventSerializer from common.events import AbstractAgentEvent diff --git a/monkey/tests/unit_tests/common/event_serializers/test_pydantic_agent_event_serializer.py b/monkey/tests/unit_tests/common/agent_event_serializers/test_pydantic_agent_event_serializer.py similarity index 97% rename from monkey/tests/unit_tests/common/event_serializers/test_pydantic_agent_event_serializer.py rename to monkey/tests/unit_tests/common/agent_event_serializers/test_pydantic_agent_event_serializer.py index 31c3080ee9b..31e75ab463b 100644 --- a/monkey/tests/unit_tests/common/event_serializers/test_pydantic_agent_event_serializer.py +++ b/monkey/tests/unit_tests/common/agent_event_serializers/test_pydantic_agent_event_serializer.py @@ -5,7 +5,7 @@ import pytest from pydantic import Field -from common.event_serializers import ( +from common.agent_event_serializers import ( EVENT_TYPE_FIELD, IAgentEventSerializer, PydanticAgentEventSerializer, From e6db64bf88331d461d1b90bde54beb27034d59cc Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 12:57:13 +0530 Subject: [PATCH 21/42] UT: Rename test_event_serializer_registry.py -> test_agent_event_serializer_registry.py --- ...alizer_registry.py => test_agent_event_serializer_registry.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename monkey/tests/unit_tests/common/agent_event_serializers/{test_event_serializer_registry.py => test_agent_event_serializer_registry.py} (100%) diff --git a/monkey/tests/unit_tests/common/agent_event_serializers/test_event_serializer_registry.py b/monkey/tests/unit_tests/common/agent_event_serializers/test_agent_event_serializer_registry.py similarity index 100% rename from monkey/tests/unit_tests/common/agent_event_serializers/test_event_serializer_registry.py rename to monkey/tests/unit_tests/common/agent_event_serializers/test_agent_event_serializer_registry.py From 5ad5de9c4a72c486e12ecbc68fb41995a921ffd7 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 12:59:23 +0530 Subject: [PATCH 22/42] UT: Change event_serializer_registry -> agent_event_serializer_registry in test_agent_event_serializer_registry.py --- .../test_agent_event_serializer_registry.py | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/monkey/tests/unit_tests/common/agent_event_serializers/test_agent_event_serializer_registry.py b/monkey/tests/unit_tests/common/agent_event_serializers/test_agent_event_serializer_registry.py index ae9b26199ea..892a8ccb8d6 100644 --- a/monkey/tests/unit_tests/common/agent_event_serializers/test_agent_event_serializer_registry.py +++ b/monkey/tests/unit_tests/common/agent_event_serializers/test_agent_event_serializer_registry.py @@ -24,45 +24,45 @@ class NoneEvent(AbstractAgentEvent): @pytest.fixture -def event_serializer_registry(): - event_serializer_registry = AgentEventSerializerRegistry() +def agent_event_serializer_registry(): + agent_event_serializer_registry = AgentEventSerializerRegistry() - event_serializer_registry[SomeEvent] = SOME_SERIALIZER - event_serializer_registry[OtherEvent] = OTHER_SERIALIZER + agent_event_serializer_registry[SomeEvent] = SOME_SERIALIZER + agent_event_serializer_registry[OtherEvent] = OTHER_SERIALIZER - return event_serializer_registry + return agent_event_serializer_registry -def test_event_serializer_registry_event(event_serializer_registry): - assert event_serializer_registry[SomeEvent] == SOME_SERIALIZER - assert event_serializer_registry[OtherEvent] == OTHER_SERIALIZER +def test_agent_event_serializer_registry_event(agent_event_serializer_registry): + assert agent_event_serializer_registry[SomeEvent] == SOME_SERIALIZER + assert agent_event_serializer_registry[OtherEvent] == OTHER_SERIALIZER -def test_event_serializer_registry_string(event_serializer_registry): - assert event_serializer_registry[SomeEvent.__name__] == SOME_SERIALIZER - assert event_serializer_registry[OtherEvent.__name__] == OTHER_SERIALIZER +def test_agent_event_serializer_registry_string(agent_event_serializer_registry): + assert agent_event_serializer_registry[SomeEvent.__name__] == SOME_SERIALIZER + assert agent_event_serializer_registry[OtherEvent.__name__] == OTHER_SERIALIZER -def test_event_serializer_registry_set_unsupported_type(event_serializer_registry): +def test_agent_event_serializer_registry_set_unsupported_type(agent_event_serializer_registry): with pytest.raises(TypeError): - event_serializer_registry[SomeEvent] = "SomethingBogusVogus" + agent_event_serializer_registry[SomeEvent] = "SomethingBogusVogus" -def test_event_serializer_registry_set_unsupported_type_key(event_serializer_registry): +def test_agent_event_serializer_registry_set_unsupported_type_key(agent_event_serializer_registry): with pytest.raises(TypeError): - event_serializer_registry["BogusKey"] = MagicMock(spec=IAgentEventSerializer) + agent_event_serializer_registry["BogusKey"] = MagicMock(spec=IAgentEventSerializer) -def test_event_serializer_registry_get_unsuported_type(event_serializer_registry): +def test_agent_event_serializer_registry_get_unsuported_type(agent_event_serializer_registry): with pytest.raises(TypeError): - event_serializer_registry[1] + agent_event_serializer_registry[1] -def test_event_serializer_registry_get_unexisting_type(event_serializer_registry): +def test_agent_event_serializer_registry_get_unexisting_type(agent_event_serializer_registry): with pytest.raises(KeyError): - event_serializer_registry[NoneEvent] + agent_event_serializer_registry[NoneEvent] -def test_event_serializer_registry_get_unexisting_string(event_serializer_registry): +def test_agent_event_serializer_registry_get_unexisting_string(agent_event_serializer_registry): with pytest.raises(KeyError): - event_serializer_registry[NoneEvent.__name__] + agent_event_serializer_registry[NoneEvent.__name__] From edce35efefab17ca239f83979f0c82b898173902 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 17:57:11 +0530 Subject: [PATCH 23/42] Agent: Make log message more specific in agent_event_forwarder.py --- monkey/infection_monkey/agent_event_forwarder.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monkey/infection_monkey/agent_event_forwarder.py b/monkey/infection_monkey/agent_event_forwarder.py index 2458b7a6db8..f70a590cb69 100644 --- a/monkey/infection_monkey/agent_event_forwarder.py +++ b/monkey/infection_monkey/agent_event_forwarder.py @@ -84,7 +84,7 @@ def _send_events_to_island(self): events.append(self._queue.get(block=False)) try: - logger.debug(f"Sending events to Island at {self._server_address}: {events}") + logger.debug(f"Sending Agent events to Island at {self._server_address}: {events}") requests.post( # noqa: DUO123 EVENTS_API_URL % (self._server_address,), json=events, From c37876cb973e1f841a601bf404027c3c27ab26a3 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 18:00:05 +0530 Subject: [PATCH 24/42] Island: Rename resource Events -> AgentEvents --- monkey/monkey_island/cc/app.py | 4 ++-- monkey/monkey_island/cc/resources/__init__.py | 2 +- monkey/monkey_island/cc/resources/events.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/monkey/monkey_island/cc/app.py b/monkey/monkey_island/cc/app.py index a678ddd9b20..22a8497e9d5 100644 --- a/monkey/monkey_island/cc/app.py +++ b/monkey/monkey_island/cc/app.py @@ -13,9 +13,9 @@ from monkey_island.cc.resources import ( AgentBinaries, AgentConfiguration, + AgentEvents, Agents, ClearSimulationData, - Events, IPAddresses, IslandLog, PBAFileDownload, @@ -187,7 +187,7 @@ def init_restful_endpoints(api: FlaskDIWrapper): api.add_resource(IslandLog) api.add_resource(IPAddresses) - api.add_resource(Events) + api.add_resource(AgentEvents) # API Spec: These two should be the same resource, GET for download and POST for upload api.add_resource(PBAFileDownload) diff --git a/monkey/monkey_island/cc/resources/__init__.py b/monkey/monkey_island/cc/resources/__init__.py index 980414a8f1d..822180ebe26 100644 --- a/monkey/monkey_island/cc/resources/__init__.py +++ b/monkey/monkey_island/cc/resources/__init__.py @@ -8,5 +8,5 @@ from .agent_configuration import AgentConfiguration from .pba_file_upload import PBAFileUpload, LINUX_PBA_TYPE, WINDOWS_PBA_TYPE from .pba_file_download import PBAFileDownload -from .events import Events +from .events import AgentEvents from .agents import Agents diff --git a/monkey/monkey_island/cc/resources/events.py b/monkey/monkey_island/cc/resources/events.py index 42612ec6f60..b24ff3ad3dd 100644 --- a/monkey/monkey_island/cc/resources/events.py +++ b/monkey/monkey_island/cc/resources/events.py @@ -10,7 +10,7 @@ logger = logging.getLogger(__name__) -class Events(AbstractResource): +class AgentEvents(AbstractResource): urls = ["/api/events"] def __init__( From 354d6624d3a860f058ec01bc9930e0045b3e7d53 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 18:00:25 +0530 Subject: [PATCH 25/42] UT: Use AgentEvents in place of Events in test_events.py --- .../unit_tests/monkey_island/cc/resources/test_events.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monkey/tests/unit_tests/monkey_island/cc/resources/test_events.py b/monkey/tests/unit_tests/monkey_island/cc/resources/test_events.py index 20c43f18761..ce8b27bb928 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/resources/test_events.py +++ b/monkey/tests/unit_tests/monkey_island/cc/resources/test_events.py @@ -11,9 +11,9 @@ ) from common.event_queue import IAgentEventQueue from common.events import AbstractAgentEvent -from monkey_island.cc.resources import Events +from monkey_island.cc.resources import AgentEvents -EVENTS_URL = Events.urls[0] +EVENTS_URL = AgentEvents.urls[0] class SomeAgentEvent(AbstractAgentEvent): From cfc0905d0cee8f15b119fba1e100a53f1b3890a3 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 18:02:22 +0530 Subject: [PATCH 26/42] Island: Rename `/api/events` to `/api/agent-events` --- monkey/monkey_island/cc/resources/events.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monkey/monkey_island/cc/resources/events.py b/monkey/monkey_island/cc/resources/events.py index b24ff3ad3dd..ec311f6e17b 100644 --- a/monkey/monkey_island/cc/resources/events.py +++ b/monkey/monkey_island/cc/resources/events.py @@ -11,7 +11,7 @@ class AgentEvents(AbstractResource): - urls = ["/api/events"] + urls = ["/api/agent-events"] def __init__( self, From 66eaccb69821fb0a519c0fb6a3e4f90de4d19842 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 18:03:10 +0530 Subject: [PATCH 27/42] Agent: `/api/agent-events` in place of `/api/events` --- monkey/infection_monkey/agent_event_forwarder.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monkey/infection_monkey/agent_event_forwarder.py b/monkey/infection_monkey/agent_event_forwarder.py index f70a590cb69..3b610ea95b4 100644 --- a/monkey/infection_monkey/agent_event_forwarder.py +++ b/monkey/infection_monkey/agent_event_forwarder.py @@ -14,7 +14,7 @@ DEFAULT_TIME_PERIOD_SECONDS = 5 -EVENTS_API_URL = "https://%s/api/events" +EVENTS_API_URL = "https://%s/api/agent-events" class AgentEventForwarder: From ad30efcedd6a46ba9070679fea088d048ca5b358 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 18:03:52 +0530 Subject: [PATCH 28/42] Chaneglog: Add entry for endpoint rename --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7827e5c5cdb..f873fa9cf46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,6 +64,7 @@ Changelog](https://keepachangelog.com/en/1.0.0/). - Tunneling to relays to provide better firewall evasion, faster Island connection times, unlimited hops, and a more resilient way for agents to call home. #2216, #1583 +- `/api/events` to `/api/agent-events`. #2300 ### Removed - VSFTPD exploiter. #1533 From f0d5545f285a900fadb15c562622cace9e22b0eb Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 18:05:03 +0530 Subject: [PATCH 29/42] Island: Rename cc/resources/events.py -> cc/resources/agent_events.py --- monkey/monkey_island/cc/resources/__init__.py | 2 +- .../monkey_island/cc/resources/{events.py => agent_events.py} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename monkey/monkey_island/cc/resources/{events.py => agent_events.py} (100%) diff --git a/monkey/monkey_island/cc/resources/__init__.py b/monkey/monkey_island/cc/resources/__init__.py index 822180ebe26..b13c6cc0677 100644 --- a/monkey/monkey_island/cc/resources/__init__.py +++ b/monkey/monkey_island/cc/resources/__init__.py @@ -8,5 +8,5 @@ from .agent_configuration import AgentConfiguration from .pba_file_upload import PBAFileUpload, LINUX_PBA_TYPE, WINDOWS_PBA_TYPE from .pba_file_download import PBAFileDownload -from .events import AgentEvents +from .agent_events import AgentEvents from .agents import Agents diff --git a/monkey/monkey_island/cc/resources/events.py b/monkey/monkey_island/cc/resources/agent_events.py similarity index 100% rename from monkey/monkey_island/cc/resources/events.py rename to monkey/monkey_island/cc/resources/agent_events.py From 0dccbd32b67791344cdf3beb60bf55e589bd51bc Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 18:11:08 +0530 Subject: [PATCH 30/42] Island: Rename IEventRepository -> IAgentEventRepository --- .../cc/agent_event_handlers/save_event_to_event_repository.py | 4 ++-- monkey/monkey_island/cc/repository/__init__.py | 2 +- monkey/monkey_island/cc/repository/i_event_repository.py | 2 +- monkey/monkey_island/cc/repository/mongo_event_repository.py | 4 ++-- .../monkey_island/cc/repository/stubbed_event_repository.py | 4 ++-- .../cc/repository/zero_trust/IEventRepository.py | 2 +- monkey/monkey_island/cc/services/initialize.py | 4 ++-- monkey/monkey_island/cc/setup/agent_event_handlers.py | 4 ++-- 8 files changed, 13 insertions(+), 13 deletions(-) diff --git a/monkey/monkey_island/cc/agent_event_handlers/save_event_to_event_repository.py b/monkey/monkey_island/cc/agent_event_handlers/save_event_to_event_repository.py index 4d69b628375..5558ae83b71 100644 --- a/monkey/monkey_island/cc/agent_event_handlers/save_event_to_event_repository.py +++ b/monkey/monkey_island/cc/agent_event_handlers/save_event_to_event_repository.py @@ -1,13 +1,13 @@ import logging from common.events import AbstractAgentEvent -from monkey_island.cc.repository import IEventRepository, StorageError +from monkey_island.cc.repository import IAgentEventRepository, StorageError logger = logging.getLogger(__name__) class save_event_to_event_repository: - def __init__(self, event_repository: IEventRepository): + def __init__(self, event_repository: IAgentEventRepository): self._event_repository = event_repository def __call__(self, event: AbstractAgentEvent): diff --git a/monkey/monkey_island/cc/repository/__init__.py b/monkey/monkey_island/cc/repository/__init__.py index 63e9ac21441..1ac6d99e6e0 100644 --- a/monkey/monkey_island/cc/repository/__init__.py +++ b/monkey/monkey_island/cc/repository/__init__.py @@ -10,7 +10,7 @@ from .i_machine_repository import IMachineRepository from .i_agent_repository import IAgentRepository from .i_node_repository import INodeRepository -from .i_event_repository import IEventRepository +from .i_event_repository import IAgentEventRepository from .local_storage_file_repository import LocalStorageFileRepository diff --git a/monkey/monkey_island/cc/repository/i_event_repository.py b/monkey/monkey_island/cc/repository/i_event_repository.py index 94bc895dc5b..b119e819164 100644 --- a/monkey/monkey_island/cc/repository/i_event_repository.py +++ b/monkey/monkey_island/cc/repository/i_event_repository.py @@ -7,7 +7,7 @@ T = TypeVar("T", bound=AbstractAgentEvent) -class IEventRepository(ABC): +class IAgentEventRepository(ABC): """A repository used to store and retrieve event objects""" @abstractmethod diff --git a/monkey/monkey_island/cc/repository/mongo_event_repository.py b/monkey/monkey_island/cc/repository/mongo_event_repository.py index 5e8b4f21ed7..14525ac3326 100644 --- a/monkey/monkey_island/cc/repository/mongo_event_repository.py +++ b/monkey/monkey_island/cc/repository/mongo_event_repository.py @@ -5,13 +5,13 @@ from common.agent_event_serializers import EVENT_TYPE_FIELD, AgentEventSerializerRegistry from common.events import AbstractAgentEvent from common.types import AgentID -from monkey_island.cc.repository import IEventRepository +from monkey_island.cc.repository import IAgentEventRepository from . import RemovalError, RetrievalError, StorageError from .consts import MONGO_OBJECT_ID_KEY -class MongoEventRepository(IEventRepository): +class MongoEventRepository(IAgentEventRepository): """A repository for storing and retrieving events in MongoDB""" def __init__( diff --git a/monkey/monkey_island/cc/repository/stubbed_event_repository.py b/monkey/monkey_island/cc/repository/stubbed_event_repository.py index d5260b6afd3..c507cf92c66 100644 --- a/monkey/monkey_island/cc/repository/stubbed_event_repository.py +++ b/monkey/monkey_island/cc/repository/stubbed_event_repository.py @@ -3,13 +3,13 @@ from common.events import AbstractAgentEvent from common.types import AgentID -from . import IEventRepository +from . import IAgentEventRepository T = TypeVar("T", bound=AbstractAgentEvent) # TODO: Remove this class after #2180 is complete -class StubbedEventRepository(IEventRepository): +class StubbedEventRepository(IAgentEventRepository): def save_event(self, event: AbstractAgentEvent): return diff --git a/monkey/monkey_island/cc/repository/zero_trust/IEventRepository.py b/monkey/monkey_island/cc/repository/zero_trust/IEventRepository.py index 3f4af68c528..28e0c6ff8eb 100644 --- a/monkey/monkey_island/cc/repository/zero_trust/IEventRepository.py +++ b/monkey/monkey_island/cc/repository/zero_trust/IEventRepository.py @@ -4,7 +4,7 @@ from monkey_island.cc.models.zero_trust.event import Event -class IEventRepository(ABC): +class IAgentEventRepository(ABC): def get_events(self, finding_id: Optional[str] = None) -> Sequence[Event]: pass diff --git a/monkey/monkey_island/cc/services/initialize.py b/monkey/monkey_island/cc/services/initialize.py index bba100c8303..df4f92d6bf2 100644 --- a/monkey/monkey_island/cc/services/initialize.py +++ b/monkey/monkey_island/cc/services/initialize.py @@ -23,8 +23,8 @@ FileSimulationRepository, IAgentBinaryRepository, IAgentConfigurationRepository, + IAgentEventRepository, ICredentialsRepository, - IEventRepository, IFileRepository, ISimulationRepository, IUserRepository, @@ -102,7 +102,7 @@ def _register_repositories(container: DIContainer, data_dir: Path): container.register_instance(IUserRepository, container.resolve(JSONFileUserRepository)) # TODO: Replace with MongoEventRepository - container.register_instance(IEventRepository, StubbedEventRepository()) + container.register_instance(IAgentEventRepository, StubbedEventRepository()) def _decorate_file_repository(file_repository: IFileRepository) -> IFileRepository: diff --git a/monkey/monkey_island/cc/setup/agent_event_handlers.py b/monkey/monkey_island/cc/setup/agent_event_handlers.py index d3df0ce4f26..989363c4347 100644 --- a/monkey/monkey_island/cc/setup/agent_event_handlers.py +++ b/monkey/monkey_island/cc/setup/agent_event_handlers.py @@ -5,7 +5,7 @@ save_event_to_event_repository, save_stolen_credentials_to_repository, ) -from monkey_island.cc.repository import ICredentialsRepository, IEventRepository +from monkey_island.cc.repository import IAgentEventRepository, ICredentialsRepository def setup_agent_event_handlers(container: DIContainer): @@ -15,7 +15,7 @@ def setup_agent_event_handlers(container: DIContainer): def _subscribe_and_store_to_event_repository(container: DIContainer): agent_event_queue = container.resolve(IAgentEventQueue) - save_event_subscriber = save_event_to_event_repository(container.resolve(IEventRepository)) + save_event_subscriber = save_event_to_event_repository(container.resolve(IAgentEventRepository)) agent_event_queue.subscribe_all_events(save_event_subscriber) save_stolen_credentials_subscriber = save_stolen_credentials_to_repository( From be612abb235bcb55f3c1ff361b9a1ff286c9100a Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 18:11:43 +0530 Subject: [PATCH 31/42] UT: Change IEventRepository -> IAgentEventRepository --- .../repository/test_mongo_event_repository.py | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_event_repository.py b/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_event_repository.py index 6c90e239629..7737e3cd78a 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_event_repository.py +++ b/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_event_repository.py @@ -12,7 +12,7 @@ ) from common.events import AbstractAgentEvent from monkey_island.cc.repository import ( - IEventRepository, + IAgentEventRepository, MongoEventRepository, RemovalError, RetrievalError, @@ -54,7 +54,7 @@ def mongo_client(event_serializer_registry): @pytest.fixture -def mongo_repository(mongo_client, event_serializer_registry) -> IEventRepository: +def mongo_repository(mongo_client, event_serializer_registry) -> IAgentEventRepository: return MongoEventRepository(mongo_client, event_serializer_registry) @@ -76,7 +76,7 @@ def error_raising_mongo_client(mongo_client) -> mongomock.MongoClient: @pytest.fixture def error_raising_mongo_repository( error_raising_mongo_client, event_serializer_registry -) -> IEventRepository: +) -> IAgentEventRepository: return MongoEventRepository(error_raising_mongo_client, event_serializer_registry) @@ -86,7 +86,7 @@ def assert_same_contents(a, b): assert item in b -def test_mongo_event_repository__save_event(mongo_repository: IEventRepository): +def test_mongo_event_repository__save_event(mongo_repository: IAgentEventRepository): event = FakeAgentEvent(source=uuid.uuid4()) mongo_repository.save_event(event) events = mongo_repository.get_events() @@ -95,7 +95,7 @@ def test_mongo_event_repository__save_event(mongo_repository: IEventRepository): def test_mongo_event_repository__save_event_raises( - error_raising_mongo_repository: IEventRepository, + error_raising_mongo_repository: IAgentEventRepository, ): event = FakeAgentEvent(source=uuid.uuid4()) @@ -103,20 +103,20 @@ def test_mongo_event_repository__save_event_raises( error_raising_mongo_repository.save_event(event) -def test_mongo_event_repository__get_events(mongo_repository: IEventRepository): +def test_mongo_event_repository__get_events(mongo_repository: IAgentEventRepository): events = mongo_repository.get_events() assert_same_contents(events, EVENTS) def test_mongo_event_repository__get_events_raises( - error_raising_mongo_repository: IEventRepository, + error_raising_mongo_repository: IAgentEventRepository, ): with pytest.raises(RetrievalError): error_raising_mongo_repository.get_events() -def test_mongo_event_repository__get_events_by_type(mongo_repository: IEventRepository): +def test_mongo_event_repository__get_events_by_type(mongo_repository: IAgentEventRepository): events = mongo_repository.get_events_by_type(FakeAgentItemEvent) expected_events = [EVENTS[3]] @@ -124,13 +124,13 @@ def test_mongo_event_repository__get_events_by_type(mongo_repository: IEventRepo def test_mongo_event_repository__get_events_by_type_raises( - error_raising_mongo_repository: IEventRepository, + error_raising_mongo_repository: IAgentEventRepository, ): with pytest.raises(RetrievalError): error_raising_mongo_repository.get_events_by_type(FakeAgentItemEvent) -def test_mongo_event_repository__get_events_by_tag(mongo_repository: IEventRepository): +def test_mongo_event_repository__get_events_by_tag(mongo_repository: IAgentEventRepository): events = mongo_repository.get_events_by_tag("bar") expected_events = [EVENTS[1], EVENTS[2]] @@ -138,13 +138,13 @@ def test_mongo_event_repository__get_events_by_tag(mongo_repository: IEventRepos def test_mongo_event_repository__get_events_by_tag_raises( - error_raising_mongo_repository: IEventRepository, + error_raising_mongo_repository: IAgentEventRepository, ): with pytest.raises(RetrievalError): error_raising_mongo_repository.get_events_by_tag("bar") -def test_mongo_event_repository__get_events_by_source(mongo_repository: IEventRepository): +def test_mongo_event_repository__get_events_by_source(mongo_repository: IAgentEventRepository): source_event = EVENTS[2] events = mongo_repository.get_events_by_source(source_event.source) @@ -153,14 +153,14 @@ def test_mongo_event_repository__get_events_by_source(mongo_repository: IEventRe def test_mongo_event_repository__get_events_by_source_raises( - error_raising_mongo_repository: IEventRepository, + error_raising_mongo_repository: IAgentEventRepository, ): with pytest.raises(RetrievalError): source_event = EVENTS[2] error_raising_mongo_repository.get_events_by_source(source_event.source) -def test_mongo_event_repository__reset(mongo_repository: IEventRepository): +def test_mongo_event_repository__reset(mongo_repository: IAgentEventRepository): initial_events = mongo_repository.get_events() assert initial_events @@ -170,6 +170,8 @@ def test_mongo_event_repository__reset(mongo_repository: IEventRepository): assert not events -def test_mongo_event_repository__reset_raises(error_raising_mongo_repository: IEventRepository): +def test_mongo_event_repository__reset_raises( + error_raising_mongo_repository: IAgentEventRepository, +): with pytest.raises(RemovalError): error_raising_mongo_repository.reset() From c7fef90cd5229dc98c581687b4e05ad5b65ddfc4 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 18:12:15 +0530 Subject: [PATCH 32/42] Project: Fix Vulture allowlist entries --- vulture_allowlist.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/vulture_allowlist.py b/vulture_allowlist.py index 049dcfb6520..7df240e0b2a 100644 --- a/vulture_allowlist.py +++ b/vulture_allowlist.py @@ -21,13 +21,13 @@ from monkey_island.cc.repository.i_agent_repository import IAgentRepository from monkey_island.cc.repository.i_attack_repository import IAttackRepository from monkey_island.cc.repository.i_config_repository import IConfigRepository -from monkey_island.cc.repository.i_event_repository import IEventRepository +from monkey_island.cc.repository.i_event_repository import IAgentEventRepository from monkey_island.cc.repository.i_log_repository import ILogRepository from monkey_island.cc.repository.i_machine_repository import IMachineRepository from monkey_island.cc.repository.i_report_repository import IReportRepository from monkey_island.cc.repository.i_simulation_repository import ISimulationRepository from monkey_island.cc.repository.ICredentials import ICredentialsRepository -from monkey_island.cc.repository.zero_trust.IEventRepository import IEventRepository +from monkey_island.cc.repository.zero_trust.IAgentEventRepository import IAgentEventRepository from monkey_island.cc.repository.zero_trust.IFindingRepository import IFindingRepository fake_monkey_dir_path # unused variable (monkey/tests/infection_monkey/post_breach/actions/test_users_custom_pba.py:37) @@ -279,7 +279,7 @@ ICredentialsRepository.remove_all_credentials ICredentialsRepository.save_stolen_credentials ICredentialsRepository.save_configured_credentials -IEventRepository.get_events +IAgentEventRepository.get_events IFindingRepository.get_findings MongoAgentRepository MongoMachineRepository @@ -300,10 +300,10 @@ # TODO: Remove once #2180 is closed -IEventRepository.save_event -IEventRepository.get_events_by_type -IEventRepository.get_events_by_tag -IEventRepository.get_events_by_source +IAgentEventRepository.save_event +IAgentEventRepository.get_events_by_type +IAgentEventRepository.get_events_by_tag +IAgentEventRepository.get_events_by_source MongoEventRepository From 89a8ed7a5ef761be12e74c574d98b7f178633373 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 18:13:27 +0530 Subject: [PATCH 33/42] Island: Rename i_event_repository.py -> i_agent_event_repository.py --- monkey/monkey_island/cc/repository/__init__.py | 2 +- .../{i_event_repository.py => i_agent_event_repository.py} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename monkey/monkey_island/cc/repository/{i_event_repository.py => i_agent_event_repository.py} (100%) diff --git a/monkey/monkey_island/cc/repository/__init__.py b/monkey/monkey_island/cc/repository/__init__.py index 1ac6d99e6e0..f299ec50390 100644 --- a/monkey/monkey_island/cc/repository/__init__.py +++ b/monkey/monkey_island/cc/repository/__init__.py @@ -10,7 +10,7 @@ from .i_machine_repository import IMachineRepository from .i_agent_repository import IAgentRepository from .i_node_repository import INodeRepository -from .i_event_repository import IAgentEventRepository +from .i_agent_event_repository import IAgentEventRepository from .local_storage_file_repository import LocalStorageFileRepository diff --git a/monkey/monkey_island/cc/repository/i_event_repository.py b/monkey/monkey_island/cc/repository/i_agent_event_repository.py similarity index 100% rename from monkey/monkey_island/cc/repository/i_event_repository.py rename to monkey/monkey_island/cc/repository/i_agent_event_repository.py From e2fed8498545f77a45070af2cc88f49334b63982 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 18:14:10 +0530 Subject: [PATCH 34/42] Project: Fix import in Vulture allowlist --- vulture_allowlist.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vulture_allowlist.py b/vulture_allowlist.py index 7df240e0b2a..5abcf44528f 100644 --- a/vulture_allowlist.py +++ b/vulture_allowlist.py @@ -18,10 +18,10 @@ StubbedEventRepository, ) from monkey_island.cc.repository.attack.IMitigationsRepository import IMitigationsRepository +from monkey_island.cc.repository.i_agent_event_repository import IAgentEventRepository from monkey_island.cc.repository.i_agent_repository import IAgentRepository from monkey_island.cc.repository.i_attack_repository import IAttackRepository from monkey_island.cc.repository.i_config_repository import IConfigRepository -from monkey_island.cc.repository.i_event_repository import IAgentEventRepository from monkey_island.cc.repository.i_log_repository import ILogRepository from monkey_island.cc.repository.i_machine_repository import IMachineRepository from monkey_island.cc.repository.i_report_repository import IReportRepository From b5736fdd3e3c33edb80d15bbf4bd2eaef510dad5 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 18:16:20 +0530 Subject: [PATCH 35/42] Common: Rename common/events/ -> common/agent_events/ --- .../agent_event_serializers/agent_event_serializer_registry.py | 2 +- .../common/agent_event_serializers/i_agent_event_serializer.py | 2 +- .../agent_event_serializers/pydantic_agent_event_serializer.py | 2 +- monkey/common/agent_event_serializers/register.py | 2 +- monkey/common/{events => agent_events}/__init__.py | 0 monkey/common/{events => agent_events}/abstract_agent_event.py | 0 .../{events => agent_events}/credentials_stolen_events.py | 0 monkey/common/event_queue/i_agent_event_queue.py | 2 +- monkey/common/event_queue/pypubsub_agent_event_queue.py | 2 +- monkey/common/event_queue/types.py | 2 +- 10 files changed, 7 insertions(+), 7 deletions(-) rename monkey/common/{events => agent_events}/__init__.py (100%) rename monkey/common/{events => agent_events}/abstract_agent_event.py (100%) rename monkey/common/{events => agent_events}/credentials_stolen_events.py (100%) diff --git a/monkey/common/agent_event_serializers/agent_event_serializer_registry.py b/monkey/common/agent_event_serializers/agent_event_serializer_registry.py index 6e5dcee1010..38683c21f07 100644 --- a/monkey/common/agent_event_serializers/agent_event_serializer_registry.py +++ b/monkey/common/agent_event_serializers/agent_event_serializer_registry.py @@ -1,7 +1,7 @@ from typing import Type, Union from common.agent_event_serializers import IAgentEventSerializer -from common.events import AbstractAgentEvent +from common.agent_events import AbstractAgentEvent class AgentEventSerializerRegistry: diff --git a/monkey/common/agent_event_serializers/i_agent_event_serializer.py b/monkey/common/agent_event_serializers/i_agent_event_serializer.py index 9a56fcc65a5..348f81e045b 100644 --- a/monkey/common/agent_event_serializers/i_agent_event_serializer.py +++ b/monkey/common/agent_event_serializers/i_agent_event_serializer.py @@ -1,7 +1,7 @@ from abc import ABC, abstractmethod from typing import Dict, List, Union -from common.events import AbstractAgentEvent +from common.agent_events import AbstractAgentEvent JSONSerializable = Union[ # type: ignore[misc] Dict[str, "JSONSerializable"], # type: ignore[misc] diff --git a/monkey/common/agent_event_serializers/pydantic_agent_event_serializer.py b/monkey/common/agent_event_serializers/pydantic_agent_event_serializer.py index 3ecf12de8bf..f597cfb8703 100644 --- a/monkey/common/agent_event_serializers/pydantic_agent_event_serializer.py +++ b/monkey/common/agent_event_serializers/pydantic_agent_event_serializer.py @@ -1,7 +1,7 @@ import logging from typing import Generic, Type, TypeVar -from common.events import AbstractAgentEvent +from common.agent_events import AbstractAgentEvent from common.utils.code_utils import del_key from . import EVENT_TYPE_FIELD, IAgentEventSerializer, JSONSerializable diff --git a/monkey/common/agent_event_serializers/register.py b/monkey/common/agent_event_serializers/register.py index 881a06f5b8c..fbe353db934 100644 --- a/monkey/common/agent_event_serializers/register.py +++ b/monkey/common/agent_event_serializers/register.py @@ -1,4 +1,4 @@ -from common.events import CredentialsStolenEvent +from common.agent_events import CredentialsStolenEvent from . import AgentEventSerializerRegistry, PydanticAgentEventSerializer diff --git a/monkey/common/events/__init__.py b/monkey/common/agent_events/__init__.py similarity index 100% rename from monkey/common/events/__init__.py rename to monkey/common/agent_events/__init__.py diff --git a/monkey/common/events/abstract_agent_event.py b/monkey/common/agent_events/abstract_agent_event.py similarity index 100% rename from monkey/common/events/abstract_agent_event.py rename to monkey/common/agent_events/abstract_agent_event.py diff --git a/monkey/common/events/credentials_stolen_events.py b/monkey/common/agent_events/credentials_stolen_events.py similarity index 100% rename from monkey/common/events/credentials_stolen_events.py rename to monkey/common/agent_events/credentials_stolen_events.py diff --git a/monkey/common/event_queue/i_agent_event_queue.py b/monkey/common/event_queue/i_agent_event_queue.py index 19ef65e2bd5..aadcd1f95f3 100644 --- a/monkey/common/event_queue/i_agent_event_queue.py +++ b/monkey/common/event_queue/i_agent_event_queue.py @@ -1,7 +1,7 @@ from abc import ABC, abstractmethod from typing import Type -from common.events import AbstractAgentEvent +from common.agent_events import AbstractAgentEvent from . import AgentEventSubscriber diff --git a/monkey/common/event_queue/pypubsub_agent_event_queue.py b/monkey/common/event_queue/pypubsub_agent_event_queue.py index 246b743d10c..ae8525db42d 100644 --- a/monkey/common/event_queue/pypubsub_agent_event_queue.py +++ b/monkey/common/event_queue/pypubsub_agent_event_queue.py @@ -3,8 +3,8 @@ from pubsub.core import Publisher +from common.agent_events import AbstractAgentEvent from common.event_queue import PyPubSubPublisherWrapper -from common.events import AbstractAgentEvent from . import AgentEventSubscriber, IAgentEventQueue diff --git a/monkey/common/event_queue/types.py b/monkey/common/event_queue/types.py index 6b57ae712df..f6554c0ba92 100644 --- a/monkey/common/event_queue/types.py +++ b/monkey/common/event_queue/types.py @@ -1,5 +1,5 @@ from typing import Callable -from common.events import AbstractAgentEvent +from common.agent_events import AbstractAgentEvent AgentEventSubscriber = Callable[[AbstractAgentEvent], None] From c8d11cdfe784fdb29d2954409110ef3d90314524 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 18:17:35 +0530 Subject: [PATCH 36/42] Agent: Fix imports common.events -> common.agent_events --- monkey/infection_monkey/agent_event_forwarder.py | 2 +- .../mimikatz_collector/mimikatz_credential_collector.py | 2 +- .../credential_collectors/ssh_collector/ssh_handler.py | 2 +- .../credential_repository/add_credentials_from_event.py | 2 +- monkey/infection_monkey/exploit/zerologon.py | 2 +- monkey/infection_monkey/monkey.py | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/monkey/infection_monkey/agent_event_forwarder.py b/monkey/infection_monkey/agent_event_forwarder.py index 3b610ea95b4..1be153fa356 100644 --- a/monkey/infection_monkey/agent_event_forwarder.py +++ b/monkey/infection_monkey/agent_event_forwarder.py @@ -6,8 +6,8 @@ import requests from common.agent_event_serializers import AgentEventSerializerRegistry, JSONSerializable +from common.agent_events import AbstractAgentEvent from common.common_consts.timeouts import MEDIUM_REQUEST_TIMEOUT -from common.events import AbstractAgentEvent from infection_monkey.utils.threading import create_daemon_thread logger = logging.getLogger(__name__) diff --git a/monkey/infection_monkey/credential_collectors/mimikatz_collector/mimikatz_credential_collector.py b/monkey/infection_monkey/credential_collectors/mimikatz_collector/mimikatz_credential_collector.py index 6a9995692cc..b4bf4135ec5 100644 --- a/monkey/infection_monkey/credential_collectors/mimikatz_collector/mimikatz_credential_collector.py +++ b/monkey/infection_monkey/credential_collectors/mimikatz_collector/mimikatz_credential_collector.py @@ -1,9 +1,9 @@ import logging from typing import Sequence +from common.agent_events import CredentialsStolenEvent from common.credentials import Credentials, LMHash, NTHash, Password, Username from common.event_queue import IAgentEventQueue -from common.events import CredentialsStolenEvent from infection_monkey.i_puppet import ICredentialCollector from infection_monkey.model import USERNAME_PREFIX from infection_monkey.utils.ids import get_agent_id diff --git a/monkey/infection_monkey/credential_collectors/ssh_collector/ssh_handler.py b/monkey/infection_monkey/credential_collectors/ssh_collector/ssh_handler.py index 0582cac77c9..e6add5589ec 100644 --- a/monkey/infection_monkey/credential_collectors/ssh_collector/ssh_handler.py +++ b/monkey/infection_monkey/credential_collectors/ssh_collector/ssh_handler.py @@ -3,9 +3,9 @@ import os from typing import Dict, Iterable, Sequence +from common.agent_events import CredentialsStolenEvent from common.credentials import Credentials, SSHKeypair, Username from common.event_queue import IAgentEventQueue -from common.events import CredentialsStolenEvent from common.utils.attack_utils import ScanStatus from infection_monkey.telemetry.attack.t1005_telem import T1005Telem from infection_monkey.telemetry.attack.t1145_telem import T1145Telem diff --git a/monkey/infection_monkey/credential_repository/add_credentials_from_event.py b/monkey/infection_monkey/credential_repository/add_credentials_from_event.py index 77efc39ab17..639a26bcf63 100644 --- a/monkey/infection_monkey/credential_repository/add_credentials_from_event.py +++ b/monkey/infection_monkey/credential_repository/add_credentials_from_event.py @@ -1,6 +1,6 @@ import logging -from common.events import CredentialsStolenEvent +from common.agent_events import CredentialsStolenEvent from . import IPropagationCredentialsRepository diff --git a/monkey/infection_monkey/exploit/zerologon.py b/monkey/infection_monkey/exploit/zerologon.py index d5a58af302c..2c042796aaf 100644 --- a/monkey/infection_monkey/exploit/zerologon.py +++ b/monkey/infection_monkey/exploit/zerologon.py @@ -15,9 +15,9 @@ from impacket.dcerpc.v5 import epm, nrpc, rpcrt, transport from impacket.dcerpc.v5.dtypes import NULL +from common.agent_events import CredentialsStolenEvent from common.common_consts.timeouts import LONG_REQUEST_TIMEOUT from common.credentials import Credentials, LMHash, NTHash, Username -from common.events import CredentialsStolenEvent from infection_monkey.exploit.HostExploiter import HostExploiter from infection_monkey.exploit.tools.wmi_tools import WmiTools from infection_monkey.exploit.zerologon_utils.dump_secrets import DumpSecrets diff --git a/monkey/infection_monkey/monkey.py b/monkey/infection_monkey/monkey.py index c1594efa8d3..eaf8d5ab8df 100644 --- a/monkey/infection_monkey/monkey.py +++ b/monkey/infection_monkey/monkey.py @@ -13,8 +13,8 @@ AgentEventSerializerRegistry, register_common_agent_event_serializers, ) +from common.agent_events import CredentialsStolenEvent from common.event_queue import IAgentEventQueue, PyPubSubAgentEventQueue -from common.events import CredentialsStolenEvent from common.network.network_utils import address_to_ip_port from common.utils.argparse_types import positive_int from common.utils.attack_utils import ScanStatus, UsageEnum From d93eb4565b78af287eed3adbd2d3c0d98f43d5de Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 18:18:09 +0530 Subject: [PATCH 37/42] Island: Fix imports common.events -> common.agent_events --- .../cc/agent_event_handlers/save_event_to_event_repository.py | 2 +- .../save_stolen_credentials_to_repository.py | 2 +- monkey/monkey_island/cc/repository/i_agent_event_repository.py | 2 +- monkey/monkey_island/cc/repository/mongo_event_repository.py | 2 +- monkey/monkey_island/cc/repository/stubbed_event_repository.py | 2 +- monkey/monkey_island/cc/setup/agent_event_handlers.py | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/monkey/monkey_island/cc/agent_event_handlers/save_event_to_event_repository.py b/monkey/monkey_island/cc/agent_event_handlers/save_event_to_event_repository.py index 5558ae83b71..027aa042a5c 100644 --- a/monkey/monkey_island/cc/agent_event_handlers/save_event_to_event_repository.py +++ b/monkey/monkey_island/cc/agent_event_handlers/save_event_to_event_repository.py @@ -1,6 +1,6 @@ import logging -from common.events import AbstractAgentEvent +from common.agent_events import AbstractAgentEvent from monkey_island.cc.repository import IAgentEventRepository, StorageError logger = logging.getLogger(__name__) diff --git a/monkey/monkey_island/cc/agent_event_handlers/save_stolen_credentials_to_repository.py b/monkey/monkey_island/cc/agent_event_handlers/save_stolen_credentials_to_repository.py index 3ac80bb10f7..dd23cef9452 100644 --- a/monkey/monkey_island/cc/agent_event_handlers/save_stolen_credentials_to_repository.py +++ b/monkey/monkey_island/cc/agent_event_handlers/save_stolen_credentials_to_repository.py @@ -1,6 +1,6 @@ import logging -from common.events import CredentialsStolenEvent +from common.agent_events import CredentialsStolenEvent from monkey_island.cc.repository import ICredentialsRepository, StorageError logger = logging.getLogger(__name__) diff --git a/monkey/monkey_island/cc/repository/i_agent_event_repository.py b/monkey/monkey_island/cc/repository/i_agent_event_repository.py index b119e819164..d3b766bf6c5 100644 --- a/monkey/monkey_island/cc/repository/i_agent_event_repository.py +++ b/monkey/monkey_island/cc/repository/i_agent_event_repository.py @@ -1,7 +1,7 @@ from abc import ABC, abstractmethod from typing import Sequence, Type, TypeVar -from common.events import AbstractAgentEvent +from common.agent_events import AbstractAgentEvent from common.types import AgentID T = TypeVar("T", bound=AbstractAgentEvent) diff --git a/monkey/monkey_island/cc/repository/mongo_event_repository.py b/monkey/monkey_island/cc/repository/mongo_event_repository.py index 14525ac3326..8a053835748 100644 --- a/monkey/monkey_island/cc/repository/mongo_event_repository.py +++ b/monkey/monkey_island/cc/repository/mongo_event_repository.py @@ -3,7 +3,7 @@ from pymongo import MongoClient from common.agent_event_serializers import EVENT_TYPE_FIELD, AgentEventSerializerRegistry -from common.events import AbstractAgentEvent +from common.agent_events import AbstractAgentEvent from common.types import AgentID from monkey_island.cc.repository import IAgentEventRepository diff --git a/monkey/monkey_island/cc/repository/stubbed_event_repository.py b/monkey/monkey_island/cc/repository/stubbed_event_repository.py index c507cf92c66..c4be4d058b7 100644 --- a/monkey/monkey_island/cc/repository/stubbed_event_repository.py +++ b/monkey/monkey_island/cc/repository/stubbed_event_repository.py @@ -1,6 +1,6 @@ from typing import Sequence, Type, TypeVar -from common.events import AbstractAgentEvent +from common.agent_events import AbstractAgentEvent from common.types import AgentID from . import IAgentEventRepository diff --git a/monkey/monkey_island/cc/setup/agent_event_handlers.py b/monkey/monkey_island/cc/setup/agent_event_handlers.py index 989363c4347..b0139236152 100644 --- a/monkey/monkey_island/cc/setup/agent_event_handlers.py +++ b/monkey/monkey_island/cc/setup/agent_event_handlers.py @@ -1,6 +1,6 @@ from common import DIContainer +from common.agent_events import CredentialsStolenEvent from common.event_queue import IAgentEventQueue -from common.events import CredentialsStolenEvent from monkey_island.cc.agent_event_handlers import ( save_event_to_event_repository, save_stolen_credentials_to_repository, From f8134e505e84c03052a48b7186218774593701e8 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 16 Sep 2022 18:18:40 +0530 Subject: [PATCH 38/42] UT: Fix imports common.events -> common.agent_events --- .../test_agent_event_serializer_registry.py | 2 +- .../test_pydantic_agent_event_serializer.py | 2 +- .../common/event_queue/test_pypubsub_agent_event_queue.py | 2 +- .../credential_collectors/test_mimikatz_collector.py | 2 +- .../credential_store/test_add_credentials_from_event.py | 2 +- .../monkey_island/cc/repository/test_mongo_event_repository.py | 2 +- .../tests/unit_tests/monkey_island/cc/resources/test_events.py | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/monkey/tests/unit_tests/common/agent_event_serializers/test_agent_event_serializer_registry.py b/monkey/tests/unit_tests/common/agent_event_serializers/test_agent_event_serializer_registry.py index 892a8ccb8d6..b45385ed451 100644 --- a/monkey/tests/unit_tests/common/agent_event_serializers/test_agent_event_serializer_registry.py +++ b/monkey/tests/unit_tests/common/agent_event_serializers/test_agent_event_serializer_registry.py @@ -4,7 +4,7 @@ from pydantic import Field from common.agent_event_serializers import AgentEventSerializerRegistry, IAgentEventSerializer -from common.events import AbstractAgentEvent +from common.agent_events import AbstractAgentEvent class SomeEvent(AbstractAgentEvent): diff --git a/monkey/tests/unit_tests/common/agent_event_serializers/test_pydantic_agent_event_serializer.py b/monkey/tests/unit_tests/common/agent_event_serializers/test_pydantic_agent_event_serializer.py index 31e75ab463b..f06f7b182dd 100644 --- a/monkey/tests/unit_tests/common/agent_event_serializers/test_pydantic_agent_event_serializer.py +++ b/monkey/tests/unit_tests/common/agent_event_serializers/test_pydantic_agent_event_serializer.py @@ -10,7 +10,7 @@ IAgentEventSerializer, PydanticAgentEventSerializer, ) -from common.events import AbstractAgentEvent +from common.agent_events import AbstractAgentEvent AGENT_ID = UUID("f811ad00-5a68-4437-bd51-7b5cc1768ad5") diff --git a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_agent_event_queue.py b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_agent_event_queue.py index 891fa445fe4..bad50892e88 100644 --- a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_agent_event_queue.py +++ b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_agent_event_queue.py @@ -5,8 +5,8 @@ import pytest from pubsub.core import Publisher +from common.agent_events import AbstractAgentEvent from common.event_queue import AgentEventSubscriber, IAgentEventQueue, PyPubSubAgentEventQueue -from common.events import AbstractAgentEvent EVENT_TAG_1 = "event tag 1" EVENT_TAG_2 = "event tag 2" diff --git a/monkey/tests/unit_tests/infection_monkey/credential_collectors/test_mimikatz_collector.py b/monkey/tests/unit_tests/infection_monkey/credential_collectors/test_mimikatz_collector.py index 11c6d695b57..38292ea9ba9 100644 --- a/monkey/tests/unit_tests/infection_monkey/credential_collectors/test_mimikatz_collector.py +++ b/monkey/tests/unit_tests/infection_monkey/credential_collectors/test_mimikatz_collector.py @@ -3,9 +3,9 @@ import pytest +from common.agent_events import CredentialsStolenEvent from common.credentials import Credentials, LMHash, NTHash, Password, Username from common.event_queue import IAgentEventQueue -from common.events import CredentialsStolenEvent from infection_monkey.credential_collectors import MimikatzCredentialCollector from infection_monkey.credential_collectors.mimikatz_collector.mimikatz_credential_collector import ( # noqa: E501 MIMIKATZ_EVENT_TAGS, diff --git a/monkey/tests/unit_tests/infection_monkey/credential_store/test_add_credentials_from_event.py b/monkey/tests/unit_tests/infection_monkey/credential_store/test_add_credentials_from_event.py index 26f4f6a508d..5327603fe8b 100644 --- a/monkey/tests/unit_tests/infection_monkey/credential_store/test_add_credentials_from_event.py +++ b/monkey/tests/unit_tests/infection_monkey/credential_store/test_add_credentials_from_event.py @@ -1,8 +1,8 @@ from unittest.mock import MagicMock from uuid import UUID +from common.agent_events import CredentialsStolenEvent from common.credentials import Credentials, Password, Username -from common.events import CredentialsStolenEvent from infection_monkey.credential_repository import ( IPropagationCredentialsRepository, add_credentials_from_event_to_propagation_credentials_repository, diff --git a/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_event_repository.py b/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_event_repository.py index 7737e3cd78a..99a780b3d9d 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_event_repository.py +++ b/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_event_repository.py @@ -10,7 +10,7 @@ AgentEventSerializerRegistry, PydanticAgentEventSerializer, ) -from common.events import AbstractAgentEvent +from common.agent_events import AbstractAgentEvent from monkey_island.cc.repository import ( IAgentEventRepository, MongoEventRepository, diff --git a/monkey/tests/unit_tests/monkey_island/cc/resources/test_events.py b/monkey/tests/unit_tests/monkey_island/cc/resources/test_events.py index ce8b27bb928..2ae8315fa52 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/resources/test_events.py +++ b/monkey/tests/unit_tests/monkey_island/cc/resources/test_events.py @@ -9,8 +9,8 @@ AgentEventSerializerRegistry, PydanticAgentEventSerializer, ) +from common.agent_events import AbstractAgentEvent from common.event_queue import IAgentEventQueue -from common.events import AbstractAgentEvent from monkey_island.cc.resources import AgentEvents EVENTS_URL = AgentEvents.urls[0] From 83279cb1b7189694ac3f391fa3856447af6ccf48 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Fri, 16 Sep 2022 08:59:28 -0400 Subject: [PATCH 39/42] UT: Fix CredentialsStolenEvent event import --- .../unit_tests/common/events/test_credentials_stolen_events.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monkey/tests/unit_tests/common/events/test_credentials_stolen_events.py b/monkey/tests/unit_tests/common/events/test_credentials_stolen_events.py index df200f5500f..33273f62d8e 100644 --- a/monkey/tests/unit_tests/common/events/test_credentials_stolen_events.py +++ b/monkey/tests/unit_tests/common/events/test_credentials_stolen_events.py @@ -7,7 +7,7 @@ ) from tests.unit_tests.monkey_island.cc.models.test_agent import AGENT_ID -from common.events import CredentialsStolenEvent +from common.agent_events import CredentialsStolenEvent TEST_EVENT = CredentialsStolenEvent(stolen_credentials=CREDENTIALS, source=AGENT_ID) From f0e8691bc4109ea3cdee861c810d569c3b1a4c1a Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Fri, 16 Sep 2022 09:02:09 -0400 Subject: [PATCH 40/42] UT: Rename common/events -> common/agent_events --- .../tests/unit_tests/common/{events => agent_events}/__init__.py | 0 .../{events => agent_events}/test_credentials_stolen_events.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename monkey/tests/unit_tests/common/{events => agent_events}/__init__.py (100%) rename monkey/tests/unit_tests/common/{events => agent_events}/test_credentials_stolen_events.py (100%) diff --git a/monkey/tests/unit_tests/common/events/__init__.py b/monkey/tests/unit_tests/common/agent_events/__init__.py similarity index 100% rename from monkey/tests/unit_tests/common/events/__init__.py rename to monkey/tests/unit_tests/common/agent_events/__init__.py diff --git a/monkey/tests/unit_tests/common/events/test_credentials_stolen_events.py b/monkey/tests/unit_tests/common/agent_events/test_credentials_stolen_events.py similarity index 100% rename from monkey/tests/unit_tests/common/events/test_credentials_stolen_events.py rename to monkey/tests/unit_tests/common/agent_events/test_credentials_stolen_events.py From dd2ed506095570bdc22b03ab2ed95baff3497099 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Fri, 16 Sep 2022 09:08:27 -0400 Subject: [PATCH 41/42] CHANGELOG: Fix `/api/agent-events` entry --- CHANGELOG.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f873fa9cf46..a01568e863f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,7 +20,7 @@ Changelog](https://keepachangelog.com/en/1.0.0/). - `/api/clear-simulation-data` endpoint. #2036 - `/api/registration-status` endpoint. #2149 - authentication to `/api/island/version`. #2109 -- `/api/events` endpoint. #2155 +- `/api/agent-events` endpoint. #2155, #2300 - The ability to customize the file extension used by ransomware when encrypting files. #1242 - `/api/agents` endpoint. @@ -64,7 +64,6 @@ Changelog](https://keepachangelog.com/en/1.0.0/). - Tunneling to relays to provide better firewall evasion, faster Island connection times, unlimited hops, and a more resilient way for agents to call home. #2216, #1583 -- `/api/events` to `/api/agent-events`. #2300 ### Removed - VSFTPD exploiter. #1533 From 998c1d7a4f3ecd8d353ceaf353d1ad8c8e9f23df Mon Sep 17 00:00:00 2001 From: vakarisz Date: Fri, 16 Sep 2022 16:20:58 +0300 Subject: [PATCH 42/42] Agent: Rename EVENTS_API_URL to AGENT_EVENTS_API_URL --- monkey/infection_monkey/agent_event_forwarder.py | 4 ++-- .../infection_monkey/test_agent_event_forwarder.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/monkey/infection_monkey/agent_event_forwarder.py b/monkey/infection_monkey/agent_event_forwarder.py index 1be153fa356..f280ad4f35a 100644 --- a/monkey/infection_monkey/agent_event_forwarder.py +++ b/monkey/infection_monkey/agent_event_forwarder.py @@ -14,7 +14,7 @@ DEFAULT_TIME_PERIOD_SECONDS = 5 -EVENTS_API_URL = "https://%s/api/agent-events" +AGENT_EVENTS_API_URL = "https://%s/api/agent-events" class AgentEventForwarder: @@ -86,7 +86,7 @@ def _send_events_to_island(self): try: logger.debug(f"Sending Agent events to Island at {self._server_address}: {events}") requests.post( # noqa: DUO123 - EVENTS_API_URL % (self._server_address,), + AGENT_EVENTS_API_URL % (self._server_address,), json=events, verify=False, timeout=MEDIUM_REQUEST_TIMEOUT, diff --git a/monkey/tests/unit_tests/infection_monkey/test_agent_event_forwarder.py b/monkey/tests/unit_tests/infection_monkey/test_agent_event_forwarder.py index 7e7009c1cce..718f11de9b0 100644 --- a/monkey/tests/unit_tests/infection_monkey/test_agent_event_forwarder.py +++ b/monkey/tests/unit_tests/infection_monkey/test_agent_event_forwarder.py @@ -3,7 +3,7 @@ import pytest import requests_mock -from infection_monkey.agent_event_forwarder import EVENTS_API_URL, BatchingAgentEventForwarder +from infection_monkey.agent_event_forwarder import AGENT_EVENTS_API_URL, BatchingAgentEventForwarder SERVER = "1.1.1.1:9999" @@ -20,7 +20,7 @@ def event_sender(): def test_send_events(event_sender): with requests_mock.Mocker() as mock: - mock.post(EVENTS_API_URL % SERVER) + mock.post(AGENT_EVENTS_API_URL % SERVER) event_sender.start() @@ -38,7 +38,7 @@ def test_send_events(event_sender): def test_send_remaining_events(event_sender): with requests_mock.Mocker() as mock: - mock.post(EVENTS_API_URL % SERVER) + mock.post(AGENT_EVENTS_API_URL % SERVER) event_sender.start()