From 866ea243ef3d8714568b4c7d8a2bbd0e3dfae9d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Jos=C3=A9=20Nicola?= Date: Wed, 13 Oct 2021 03:43:02 -0300 Subject: [PATCH 1/2] Fix: Fix get_status (#471) Avoid ospd to crash when get_status is called with a wrong scan id (cherry picked from commit a682e99b93d57f89356b127d85990140834b1eb6) # Conflicts: # ospd/scan.py --- ospd/ospd.py | 4 +++- ospd/scan.py | 21 ++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/ospd/ospd.py b/ospd/ospd.py index 17a91cd9..1bc147a9 100644 --- a/ospd/ospd.py +++ b/ospd/ospd.py @@ -655,7 +655,9 @@ def set_scan_status(self, scan_id: str, status: ScanStatus) -> None: def get_scan_status(self, scan_id: str) -> ScanStatus: """Get scan_id scans's status.""" status = self.scan_collection.get_status(scan_id) - logger.debug('%s: Current scan status: %s,', scan_id, status.name) + st = status.name if status else None + logger.debug('%s: Current scan status: %s,', scan_id, st) + return status def scan_exists(self, scan_id: str) -> bool: diff --git a/ospd/scan.py b/ospd/scan.py index 7efd904e..ed3540a5 100644 --- a/ospd/scan.py +++ b/ospd/scan.py @@ -356,9 +356,18 @@ def set_status(self, scan_id: str, status: ScanStatus) -> None: self.scans_table[scan_id]['end_time'] = int(time.time()) def get_status(self, scan_id: str) -> ScanStatus: +<<<<<<< HEAD """Get scan_id scans's status.""" +======= + """ Get scan_id scans's status.""" + status = None + try: + status = self.scans_table[scan_id].get('status') + except KeyError: + LOGGER.error("Scan ID %s not found", scan_id) +>>>>>>> a682e99 (Fix: Fix get_status (#471)) - return self.scans_table[scan_id].get('status') + return status def get_options(self, scan_id: str) -> Dict: """Get scan_id scan's options list.""" @@ -425,10 +434,20 @@ def simplify_exclude_host_count(self, scan_id: str) -> int: Count of excluded host. """ exc_hosts_list = target_str_to_list(self.get_exclude_hosts(scan_id)) + LOGGER.debug( + '%s: Excluded Hosts: %s', + scan_id, + pformat(exc_hosts_list), + ) finished_hosts_list = target_str_to_list( self.get_finished_hosts(scan_id) ) + LOGGER.debug( + '%s: Finished Hosts: %s', + scan_id, + pformat(finished_hosts_list), + ) # Remove finished hosts from excluded host list if finished_hosts_list and exc_hosts_list: From 8a365066f854852d3121306c9f1738efffd595b1 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Wed, 13 Oct 2021 02:10:03 -0500 Subject: [PATCH 2/2] Use logger instead of LOGGER. Also, update changelog --- CHANGELOG.md | 2 +- ospd/scan.py | 10 +++------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6592fc0e..e9ef3ab2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,7 +18,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Removed ### Fixed - Fix resume scan. [#464](https://github.com/greenbone/ospd/pull/464) - +- Fix get_status. Backport #471.[#472](https://github.com/greenbone/ospd/pull/472) [Unreleased]: https://github.com/greenbone/ospd/compare/v21.4.3...HEAD diff --git a/ospd/scan.py b/ospd/scan.py index ed3540a5..c966be86 100644 --- a/ospd/scan.py +++ b/ospd/scan.py @@ -356,16 +356,12 @@ def set_status(self, scan_id: str, status: ScanStatus) -> None: self.scans_table[scan_id]['end_time'] = int(time.time()) def get_status(self, scan_id: str) -> ScanStatus: -<<<<<<< HEAD """Get scan_id scans's status.""" -======= - """ Get scan_id scans's status.""" status = None try: status = self.scans_table[scan_id].get('status') except KeyError: - LOGGER.error("Scan ID %s not found", scan_id) ->>>>>>> a682e99 (Fix: Fix get_status (#471)) + logger.error("Scan ID %s not found", scan_id) return status @@ -434,7 +430,7 @@ def simplify_exclude_host_count(self, scan_id: str) -> int: Count of excluded host. """ exc_hosts_list = target_str_to_list(self.get_exclude_hosts(scan_id)) - LOGGER.debug( + logger.debug( '%s: Excluded Hosts: %s', scan_id, pformat(exc_hosts_list), @@ -443,7 +439,7 @@ def simplify_exclude_host_count(self, scan_id: str) -> int: finished_hosts_list = target_str_to_list( self.get_finished_hosts(scan_id) ) - LOGGER.debug( + logger.debug( '%s: Finished Hosts: %s', scan_id, pformat(finished_hosts_list),