From 76cf34b5f0c138bcd00c6b9b17ec7ab7a49d97d8 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Mon, 5 Sep 2022 19:00:20 +0530 Subject: [PATCH] Common: Rename AbstractEvent -> AbstractAgentEvent --- monkey/common/event_queue/i_event_queue.py | 6 +++--- .../common/event_queue/pypubsub_event_queue.py | 16 ++++++++-------- monkey/common/event_queue/types.py | 4 ++-- .../event_serializer_registry.py | 16 +++++++++------- .../event_serializers/i_event_serialize.py | 6 +++--- monkey/common/events/__init__.py | 2 +- monkey/common/events/abstract_event.py | 6 +++--- .../common/events/credentials_stolen_events.py | 4 ++-- 8 files changed, 31 insertions(+), 29 deletions(-) diff --git a/monkey/common/event_queue/i_event_queue.py b/monkey/common/event_queue/i_event_queue.py index b205b8a98..128d0517b 100644 --- a/monkey/common/event_queue/i_event_queue.py +++ b/monkey/common/event_queue/i_event_queue.py @@ -1,7 +1,7 @@ from abc import ABC, abstractmethod from typing import Type -from common.events import AbstractEvent +from common.events import AbstractAgentEvent from . import EventSubscriber @@ -22,7 +22,7 @@ class IEventQueue(ABC): pass @abstractmethod - def subscribe_type(self, event_type: Type[AbstractEvent], subscriber: EventSubscriber): + def subscribe_type(self, event_type: Type[AbstractAgentEvent], subscriber: EventSubscriber): """ Subscribes a subscriber to the specified event type @@ -44,7 +44,7 @@ class IEventQueue(ABC): pass @abstractmethod - def publish(self, event: AbstractEvent): + def publish(self, event: AbstractAgentEvent): """ Publishes an event with the given data diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index 6dde760a8..9c3e4dc6d 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -3,7 +3,7 @@ from typing import Type from pubsub.core import Publisher -from common.events import AbstractEvent +from common.events import AbstractAgentEvent from . import EventSubscriber, IEventQueue @@ -20,7 +20,7 @@ class PyPubSubEventQueue(IEventQueue): def subscribe_all_events(self, subscriber: EventSubscriber): self._subscribe(_ALL_EVENTS_TOPIC, subscriber) - def subscribe_type(self, event_type: Type[AbstractEvent], subscriber: EventSubscriber): + def subscribe_type(self, event_type: Type[AbstractAgentEvent], subscriber: EventSubscriber): # pypubsub.pub.subscribe needs a string as the topic/event name event_type_topic = PyPubSubEventQueue._get_type_topic(event_type) self._subscribe(event_type_topic, subscriber) @@ -60,31 +60,31 @@ class PyPubSubEventQueue(IEventQueue): # scope. Adding subscribers to self._refs prevents them from ever going out of scope. self._refs.append(subscriber) - def publish(self, event: AbstractEvent): + def publish(self, event: AbstractAgentEvent): self._publish_to_all_events_topic(event) self._publish_to_type_topic(event) self._publish_to_tags_topics(event) - def _publish_to_all_events_topic(self, event: AbstractEvent): + def _publish_to_all_events_topic(self, event: AbstractAgentEvent): self._publish_event(_ALL_EVENTS_TOPIC, event) - def _publish_to_type_topic(self, event: AbstractEvent): + def _publish_to_type_topic(self, event: AbstractAgentEvent): event_type_topic = PyPubSubEventQueue._get_type_topic(event.__class__) self._publish_event(event_type_topic, event) - def _publish_to_tags_topics(self, event: AbstractEvent): + def _publish_to_tags_topics(self, event: AbstractAgentEvent): for tag in event.tags: tag_topic = PyPubSubEventQueue._get_tag_topic(tag) self._publish_event(tag_topic, event) - def _publish_event(self, topic: str, event: AbstractEvent): + def _publish_event(self, topic: str, event: AbstractAgentEvent): logger.debug(f"Publishing a {event.__class__.__name__} event to {topic}") self._pypubsub_publisher.sendMessage(topic, event=event) # Appending a unique string to the topics for type and tags prevents bugs caused by collisions # between type names and tag names. @staticmethod - def _get_type_topic(event_type: Type[AbstractEvent]) -> str: + def _get_type_topic(event_type: Type[AbstractAgentEvent]) -> str: return f"{event_type.__name__}-type" @staticmethod diff --git a/monkey/common/event_queue/types.py b/monkey/common/event_queue/types.py index f78651306..f3f5f4ef8 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 AbstractEvent +from common.events import AbstractAgentEvent -EventSubscriber = Callable[[AbstractEvent], None] +EventSubscriber = Callable[[AbstractAgentEvent], None] diff --git a/monkey/common/event_serializers/event_serializer_registry.py b/monkey/common/event_serializers/event_serializer_registry.py index fd02045d1..b4f43f7b0 100644 --- a/monkey/common/event_serializers/event_serializer_registry.py +++ b/monkey/common/event_serializers/event_serializer_registry.py @@ -1,7 +1,7 @@ from typing import Type, Union from common.event_serializers import IEventSerializer -from common.events import AbstractEvent +from common.events import AbstractAgentEvent class EventSerializerRegistry: @@ -21,9 +21,11 @@ class EventSerializerRegistry: def __init__(self): self._registry = {} - def __setitem__(self, event_class: Type[AbstractEvent], event_serializer: IEventSerializer): - if not issubclass(event_class, AbstractEvent): - raise TypeError(f"Event class must be of type: {AbstractEvent.__name__}") + def __setitem__( + self, event_class: Type[AbstractAgentEvent], event_serializer: IEventSerializer + ): + 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__}") @@ -31,10 +33,10 @@ class EventSerializerRegistry: self._registry[event_class] = event_serializer self._registry[event_class.__name__] = event_serializer - def __getitem__(self, event_class: Union[str, Type[AbstractEvent]]) -> IEventSerializer: - if not (isinstance(event_class, str) or issubclass(event_class, AbstractEvent)): + def __getitem__(self, event_class: Union[str, Type[AbstractAgentEvent]]) -> IEventSerializer: + if not (isinstance(event_class, str) or issubclass(event_class, AbstractAgentEvent)): raise TypeError( - f"Registry get key {event_class} must be of type: {AbstractEvent.__name__} or " + f"Registry get key {event_class} must be of type: {AbstractAgentEvent.__name__} or " f"{str.__name__}" ) diff --git a/monkey/common/event_serializers/i_event_serialize.py b/monkey/common/event_serializers/i_event_serialize.py index 5544e6ede..75d95be3c 100644 --- a/monkey/common/event_serializers/i_event_serialize.py +++ b/monkey/common/event_serializers/i_event_serialize.py @@ -1,7 +1,7 @@ from abc import ABC, abstractmethod from typing import Dict, List, Union -from common.events import AbstractEvent +from common.events import AbstractAgentEvent JSONSerializable = Union[ Dict[str, "JSONSerializable"], List["JSONSerializable"], int, str, float, bool, None @@ -14,7 +14,7 @@ class IEventSerializer(ABC): """ @abstractmethod - def serialize(self, event: AbstractEvent) -> JSONSerializable: + def serialize(self, event: AbstractAgentEvent) -> JSONSerializable: """ Serializes an event @@ -24,7 +24,7 @@ class IEventSerializer(ABC): pass @abstractmethod - def deserialize(self, serialized_event: JSONSerializable) -> AbstractEvent: + def deserialize(self, serialized_event: JSONSerializable) -> AbstractAgentEvent: """ Deserializes an event diff --git a/monkey/common/events/__init__.py b/monkey/common/events/__init__.py index da5b88234..b996452f7 100644 --- a/monkey/common/events/__init__.py +++ b/monkey/common/events/__init__.py @@ -1,2 +1,2 @@ -from .abstract_event import AbstractEvent +from .abstract_event import AbstractAgentEvent from .credentials_stolen_events import CredentialsStolenEvent diff --git a/monkey/common/events/abstract_event.py b/monkey/common/events/abstract_event.py index 4b4edbf99..89f515187 100644 --- a/monkey/common/events/abstract_event.py +++ b/monkey/common/events/abstract_event.py @@ -7,13 +7,13 @@ from uuid import UUID, getnode @dataclass(frozen=True) -class AbstractEvent(ABC): +class AbstractAgentEvent(ABC): """ An event that was initiated or observed by an agent Agents perform actions and collect data. These actions and data are represented as "events". - Subtypes of `AbstractEvent` will have additional properties that provide context and information - about the event. + Subtypes of `AbstractAgentEvent` will have additional properties that provide context and + information about the event. Attributes: :param source: The UUID of the agent that observed the event diff --git a/monkey/common/events/credentials_stolen_events.py b/monkey/common/events/credentials_stolen_events.py index ffd7707e7..184ee81c3 100644 --- a/monkey/common/events/credentials_stolen_events.py +++ b/monkey/common/events/credentials_stolen_events.py @@ -3,11 +3,11 @@ from typing import Sequence from common.credentials import Credentials -from . import AbstractEvent +from . import AbstractAgentEvent @dataclass(frozen=True) -class CredentialsStolenEvent(AbstractEvent): +class CredentialsStolenEvent(AbstractAgentEvent): """ An event that occurs when an agent collects credentials from the victim