From 2aa79331e3d2bda61b62861d10abd9f3c6db8a7c Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 29 Sep 2022 15:04:29 -0400 Subject: [PATCH] Island: Extract method to improve readablility --- .../handle_ping_scan_event.py | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/monkey/monkey_island/cc/agent_event_handlers/handle_ping_scan_event.py b/monkey/monkey_island/cc/agent_event_handlers/handle_ping_scan_event.py index f0a98757b..f73f461e3 100644 --- a/monkey/monkey_island/cc/agent_event_handlers/handle_ping_scan_event.py +++ b/monkey/monkey_island/cc/agent_event_handlers/handle_ping_scan_event.py @@ -38,13 +38,9 @@ class handle_ping_scan_event: try: dest_machine = self._get_destination_machine(event) - self._update_destination_machine(dest_machine, event) - src_machine = self._get_source_machine(event) - # Update or create the node - self._node_repository.upsert_communication( - src_machine.id, dest_machine.id, CommunicationType.SCANNED - ) + self._update_destination_machine_os(dest_machine, event) + self._update_nodes(dest_machine, event) except (RetrievalError, StorageError, TypeError, UnknownRecordError): logger.exception("Unable to process ping scan data") @@ -60,11 +56,19 @@ class handle_ping_scan_event: self._machine_repository.upsert_machine(machine) return machine - def _get_source_machine(self, event: PingScanEvent) -> Machine: - agent = self._agent_repository.get_agent_by_id(event.source) - return self._machine_repository.get_machine_by_id(agent.machine_id) - - def _update_destination_machine(self, machine: Machine, event: PingScanEvent): + def _update_destination_machine_os(self, machine: Machine, event: PingScanEvent): if event.os is not None and machine.operating_system is None: machine.operating_system = event.os self._machine_repository.upsert_machine(machine) + + def _update_nodes(self, dest_machine, event): + src_machine = self._get_source_machine(event) + + # Update or create the node + self._node_repository.upsert_communication( + src_machine.id, dest_machine.id, CommunicationType.SCANNED + ) + + def _get_source_machine(self, event: PingScanEvent) -> Machine: + agent = self._agent_repository.get_agent_by_id(event.source) + return self._machine_repository.get_machine_by_id(agent.machine_id)