From 965973a32fc969fbee47f051db061f1c9e80415e Mon Sep 17 00:00:00 2001 From: "R. Richard" Date: Mon, 26 Aug 2019 11:10:39 +0200 Subject: [PATCH] Fix issues in PR #64 --- tfw/components/fsm/fsm_handler.py | 4 ++-- tfw/components/fsm/fsm_updater.py | 2 +- tfw/event_handlers.py | 2 +- tfw/internals/event_handling/__init__.py | 1 + .../event_handling/control_event_handler.py | 5 ++--- tfw/internals/event_handling/event_handler.py | 6 +++++- .../event_handling/signed_control_event_handler.py | 9 +++++++++ tfw/internals/event_handling/signed_event_handler.py | 12 ++++++------ tfw/internals/networking/zmq_connector.py | 2 +- 9 files changed, 28 insertions(+), 15 deletions(-) create mode 100644 tfw/internals/event_handling/signed_control_event_handler.py diff --git a/tfw/components/fsm/fsm_handler.py b/tfw/components/fsm/fsm_handler.py index 9ea1374..ae9a171 100644 --- a/tfw/components/fsm/fsm_handler.py +++ b/tfw/components/fsm/fsm_handler.py @@ -27,13 +27,13 @@ class FSMHandler: def start(self): self.connector.subscribe('frontend.ready') - def handle_event(self, message, connector): + def handle_event(self, message, _): try: message = self.command_handlers[message['key']](message) if message: fsm_update_message = self._fsm_updater.fsm_update sign_message(self.auth_key, fsm_update_message) - connector.send_message(fsm_update_message, Scope.BROADCAST, Intent.EVENT) + self.connector.send_message(fsm_update_message, Scope.BROADCAST, Intent.EVENT) except KeyError: LOG.error('IGNORING MESSAGE: Invalid message received: %s', message) diff --git a/tfw/components/fsm/fsm_updater.py b/tfw/components/fsm/fsm_updater.py index 1c671ad..8ab488f 100644 --- a/tfw/components/fsm/fsm_updater.py +++ b/tfw/components/fsm/fsm_updater.py @@ -16,7 +16,7 @@ class FSMUpdater: for trigger in self.fsm.get_triggers(self.fsm.state) ] if not self.fsm.event_log: - return {} + return None last_fsm_event = self.fsm.event_log[-1] return { 'current_state': self.fsm.state, diff --git a/tfw/event_handlers.py b/tfw/event_handlers.py index 3cd92f5..a804bc4 100644 --- a/tfw/event_handlers.py +++ b/tfw/event_handlers.py @@ -1,2 +1,2 @@ # pylint: disable=unused-import -from tfw.internals.event_handling import EventHandler, ControlEventHandler, FSMAwareEventHandler, SignedEventHandler +from tfw.internals.event_handling import EventHandler, ControlEventHandler, FSMAwareEventHandler, SignedEventHandler, SignedControlEventHandler diff --git a/tfw/internals/event_handling/__init__.py b/tfw/internals/event_handling/__init__.py index 0803cc5..bd5e14f 100644 --- a/tfw/internals/event_handling/__init__.py +++ b/tfw/internals/event_handling/__init__.py @@ -3,3 +3,4 @@ from .event_handler_factory_base import EventHandlerFactoryBase from .control_event_handler import ControlEventHandler from .fsm_aware_event_handler import FSMAwareEventHandler from .signed_event_handler import SignedEventHandler +from .signed_control_event_handler import SignedControlEventHandler diff --git a/tfw/internals/event_handling/control_event_handler.py b/tfw/internals/event_handling/control_event_handler.py index dda4dae..381963e 100644 --- a/tfw/internals/event_handling/control_event_handler.py +++ b/tfw/internals/event_handling/control_event_handler.py @@ -4,6 +4,5 @@ from .event_handler import EventHandler class ControlEventHandler(EventHandler): # pylint: disable=abstract-method - def _event_callback(self, message): - if message.get('intent') != Intent.EVENT.value: - self.handle_event(message, self.connector) + def _validate_message(self, message): + return message.get('intent') != Intent.EVENT.value diff --git a/tfw/internals/event_handling/event_handler.py b/tfw/internals/event_handling/event_handler.py index f288042..fd7c69c 100644 --- a/tfw/internals/event_handling/event_handler.py +++ b/tfw/internals/event_handling/event_handler.py @@ -9,7 +9,11 @@ class EventHandler: self.connector.register_callback(self._event_callback) def _event_callback(self, message): - self.handle_event(message, self.connector) + if self._validate_message(message): + self.handle_event(message, self.connector) + + def _validate_message(self, message): + return True def handle_event(self, message, connector): raise NotImplementedError() diff --git a/tfw/internals/event_handling/signed_control_event_handler.py b/tfw/internals/event_handling/signed_control_event_handler.py new file mode 100644 index 0000000..5375280 --- /dev/null +++ b/tfw/internals/event_handling/signed_control_event_handler.py @@ -0,0 +1,9 @@ +from .control_event_handler import ControlEventHandler +from .signed_event_handler import SignedEventHandler + +class SignedControlEventHandler(ControlEventHandler, SignedEventHandler): + def _validate_message(self, message): + return ( + ControlEventHandler._validate_message(self, message) and + SignedEventHandler._validate_message(self, message) + ) diff --git a/tfw/internals/event_handling/signed_event_handler.py b/tfw/internals/event_handling/signed_event_handler.py index 581035f..15f4439 100644 --- a/tfw/internals/event_handling/signed_event_handler.py +++ b/tfw/internals/event_handling/signed_event_handler.py @@ -2,19 +2,19 @@ import logging from tfw.internals.crypto import KeyManager, verify_message -from .control_event_handler import ControlEventHandler +from .event_handler import EventHandler LOG = logging.getLogger(__name__) # pylint: disable=abstract-method -class SignedEventHandler(ControlEventHandler): +class SignedEventHandler(EventHandler): def __init__(self, connector): self._auth_key = KeyManager().auth_key super().__init__(connector) - def _event_callback(self, message): - if verify_message(self._auth_key, message): - self.handle_event(message, self.connector) - else: + def _validate_message(self, message): + is_valid = verify_message(self._auth_key, message) + if not is_valid: LOG.error('Message does not have valid signature: %s', message) + return is_valid diff --git a/tfw/internals/networking/zmq_connector.py b/tfw/internals/networking/zmq_connector.py index 10c5114..ff82758 100644 --- a/tfw/internals/networking/zmq_connector.py +++ b/tfw/internals/networking/zmq_connector.py @@ -54,7 +54,7 @@ class ZMQUplinkConnector: def send_message(self, message, scope=Scope.ZMQ, intent=None): message['scope'] = scope.value - if isinstance(intent, Intent): + if intent is not None: message['intent'] = intent.value self._zmq_push_socket.send_multipart(serialize_tfw_msg(message))