From c54d1b89ab9e7cb6127dda6962d50ec2b8aac75e Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Wed, 14 Sep 2022 12:34:29 +0200 Subject: [PATCH 1/2] Island: Add IEventRepository --- .../monkey_island/cc/repository/__init__.py | 1 + .../cc/repository/i_event_repository.py | 68 +++++++++++++++++++ vulture_allowlist.py | 7 ++ 3 files changed, 76 insertions(+) create mode 100644 monkey/monkey_island/cc/repository/i_event_repository.py diff --git a/monkey/monkey_island/cc/repository/__init__.py b/monkey/monkey_island/cc/repository/__init__.py index 6bda93bbf..a6898d1b3 100644 --- a/monkey/monkey_island/cc/repository/__init__.py +++ b/monkey/monkey_island/cc/repository/__init__.py @@ -10,6 +10,7 @@ from .i_user_repository import IUserRepository 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 .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 new file mode 100644 index 000000000..9354afc59 --- /dev/null +++ b/monkey/monkey_island/cc/repository/i_event_repository.py @@ -0,0 +1,68 @@ +from abc import ABC, abstractmethod +from typing import Sequence, Type + +from common.events import AbstractAgentEvent +from common.types import AgentID + + +class IEventRepository(ABC): + """A repository used to store and retrieve event objects""" + + @abstractmethod + def save_event(self, event: AbstractAgentEvent): + """ + Save an event to the repository + + :param event: The event to store in the repository + :raises StorageError: If an error occured while attempting to save an event + """ + + @abstractmethod + def get_events(self) -> Sequence[AbstractAgentEvent]: + """ + Retrieve all events stored in the repository + + :return: All stored events + :raises RetrievalError: If an error occured while attempting to retrieve the events + """ + + @abstractmethod + def get_events_by_type( + self, event_type: Type[AbstractAgentEvent] + ) -> Sequence[AbstractAgentEvent]: + """ + Retrieve all events with same type + + :param event_type: Type of event + :return: Stored events that have same type + :raises RetrievalError: If an error occured while attempting to retrieve the event + """ + + @abstractmethod + def get_events_by_tag(self, tag: str) -> Sequence[AbstractAgentEvent]: + """ + Retrieve all events with same tag + + :param tag: Tag of event + :return: Stored events that have same tag + :raises RetrievalError: If an error occured while attempting to retrieve the event + """ + + @abstractmethod + def get_events_by_source(self, source: AgentID) -> Sequence[AbstractAgentEvent]: + """ + Retrieve all events from the same source + + :param source: The ID of the agent that observed the events + :return: Stored events that have same source + :raises RetrievalError: If an error occured while attempting to retrieve the event + """ + + @abstractmethod + def reset(self): + """ + Remove all data from the repository + + :raises RemovalError: If an error occurred while attempting to remove all events from the + repository + """ diff --git a/vulture_allowlist.py b/vulture_allowlist.py index c77390784..3113a44d9 100644 --- a/vulture_allowlist.py +++ b/vulture_allowlist.py @@ -16,6 +16,7 @@ from monkey_island.cc.repository.attack.IMitigationsRepository import IMitigatio 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_log_repository import ILogRepository from monkey_island.cc.repository.i_machine_repository import IMachineRepository from monkey_island.cc.repository.i_report_repository import IReportRepository @@ -300,6 +301,12 @@ deserialize serialized_event PydanticEventSerializer +# TODO: Remove once #2180 is closed +IEventRepository.save_event +IEventRepository.get_events_by_type +IEventRepository.get_events_by_tag +IEventRepository.get_events_by_source + # pydantic base models underscore_attrs_are_private extra From c977d8c212d131a0a05e7c2d9376ffaf715f7239 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 14 Sep 2022 09:41:58 -0400 Subject: [PATCH 2/2] Island: Use TypeVar for IEventRepository.get_events_by_type() --- monkey/monkey_island/cc/repository/i_event_repository.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/monkey/monkey_island/cc/repository/i_event_repository.py b/monkey/monkey_island/cc/repository/i_event_repository.py index 9354afc59..94bc895dc 100644 --- a/monkey/monkey_island/cc/repository/i_event_repository.py +++ b/monkey/monkey_island/cc/repository/i_event_repository.py @@ -1,9 +1,11 @@ from abc import ABC, abstractmethod -from typing import Sequence, Type +from typing import Sequence, Type, TypeVar from common.events import AbstractAgentEvent from common.types import AgentID +T = TypeVar("T", bound=AbstractAgentEvent) + class IEventRepository(ABC): """A repository used to store and retrieve event objects""" @@ -27,9 +29,7 @@ class IEventRepository(ABC): """ @abstractmethod - def get_events_by_type( - self, event_type: Type[AbstractAgentEvent] - ) -> Sequence[AbstractAgentEvent]: + def get_events_by_type(self, event_type: Type[T]) -> Sequence[T]: """ Retrieve all events with same type