Common: Rename AbstractEvent -> AbstractAgentEvent

This commit is contained in:
Shreya Malviya 2022-09-05 19:00:20 +05:30
parent de5da88c22
commit 76cf34b5f0
8 changed files with 31 additions and 29 deletions

View File

@ -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

View File

@ -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

View File

@ -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]

View File

@ -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__}"
)

View File

@ -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

View File

@ -1,2 +1,2 @@
from .abstract_event import AbstractEvent
from .abstract_event import AbstractAgentEvent
from .credentials_stolen_events import CredentialsStolenEvent

View File

@ -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

View File

@ -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