Skip to content
This repository has been archived by the owner on Nov 29, 2021. It is now read-only.

Commit

Permalink
Merge pull request #367 from jjnicola/merge-master
Browse files Browse the repository at this point in the history
 Merge branch 'ospd-20.08' into master
  • Loading branch information
bjoernricks committed Mar 12, 2021
2 parents dd3ea07 + 3f70f9a commit 651e0a4
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -21,6 +21,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Allow the scanner to update total count of hosts. [#332](https://github.com/greenbone/ospd/pull/332)
- Add more debug logging. [#352](https://github.com/greenbone/ospd/pull/352)
- Set end_time for interrupted scans. [#353](https://github.com/greenbone/ospd/pull/353)
- Add method to get a single host scan progress. [#363](https://github.com/greenbone/ospd/pull/363)

### Fixed
- Fix OSP version. [#326](https://github.com/greenbone/ospd/pull/326)
Expand All @@ -29,6 +30,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Fix malformed target. [#341](https://github.com/greenbone/ospd/pull/341)
- Initialize end_time with create_scan. [#354](https://github.com/greenbone/ospd/pull/354)
- Fix get_count_total(). Accept -1 value set by the server. [#355](https://github.com/greenbone/ospd/pull/355)
- Fix get_count_total(). Consider 0 value set by the server. [#366](https://github.com/greenbone/ospd/pull/366)

[20.8.2]: https://github.com/greenbone/ospd/compare/v20.8.1...ospd-20.08

Expand Down
25 changes: 24 additions & 1 deletion ospd/ospd.py
Expand Up @@ -611,8 +611,16 @@ def sort_host_finished(
progress = current_hosts.get(finished_host)
if progress == ScanProgress.FINISHED:
alive_hosts.append(finished_host)
if progress == ScanProgress.DEAD_HOST:
elif progress == ScanProgress.DEAD_HOST:
dead_hosts.append(finished_host)
else:
logger.debug(
'The host %s is considered dead or finished, but '
'its progress is still %d. This can lead to '
'interrupted scan.',
finished_host,
progress,
)

self.scan_collection.set_host_dead(scan_id, dead_hosts)

Expand All @@ -625,6 +633,10 @@ def sort_host_finished(
def set_scan_progress(self, scan_id: str):
"""Calculate the target progress with the current host states
and stores in the scan table."""
# Get current scan progress for debugging purposes
logger.debug("Calculating scan progress with the following data:")
self._get_scan_progress_raw(scan_id)

scan_progress = self.scan_collection.calculate_target_progress(scan_id)
self.scan_collection.set_progress(scan_id, scan_progress)

Expand Down Expand Up @@ -656,6 +668,17 @@ def set_scan_host_progress(
host_progress = {host: progress}
self.set_scan_progress_batch(scan_id, host_progress)

def get_scan_host_progress(
self,
scan_id: str,
host: str = None,
) -> int:
""" Get host's progress which is part of target."""
current_progress = self.scan_collection.get_current_target_progress(
scan_id
)
return current_progress.get(host)

def set_scan_status(self, scan_id: str, status: ScanStatus) -> None:
""" Set the scan's status."""
logger.debug('%s: Set scan status %s,', scan_id, status.name)
Expand Down
18 changes: 2 additions & 16 deletions ospd/scan.py
Expand Up @@ -287,7 +287,7 @@ def unpickle_scan_info(self, scan_id: str) -> None:
scan_info['target_progress'] = dict()
scan_info['count_alive'] = 0
scan_info['count_dead'] = 0
scan_info['count_total'] = 0
scan_info['count_total'] = None
scan_info['target'] = unpickled_scan_info.pop('target')
scan_info['vts'] = unpickled_scan_info.pop('vts')
scan_info['options'] = unpickled_scan_info.pop('options')
Expand Down Expand Up @@ -404,7 +404,7 @@ def get_count_total(self, scan_id: str) -> int:
count_total = 0
# If the server does not set the total host count
# ospd set the amount of host from the original host list.
elif not count_total:
elif count_total is None:
count_total = self.get_host_count(scan_id)
self.update_count_total(scan_id, count_total)

Expand Down Expand Up @@ -448,20 +448,6 @@ def calculate_target_progress(self, scan_id: str) -> int:
count_dead = self.get_count_dead(scan_id)
host_progresses = self.get_current_target_progress(scan_id)

LOGGER.debug(
"Calculating scan progress with the following data:\n"
"\ttotal_hosts: %d\n\t"
"\texc_hosts: %d\n\t"
"\tcount_alive: %d\n\t"
"\tcount_dead: %d\n\t"
"\thost_prgresses: %d\n\t",
total_hosts,
exc_hosts,
count_alive,
count_dead,
sum(host_progresses.values()),
)

try:
t_prog = int(
(sum(host_progresses.values()) + 100 * count_alive)
Expand Down
50 changes: 50 additions & 0 deletions tests/test_scan_and_result.py
Expand Up @@ -1190,6 +1190,27 @@ def test_calculate_progress_without_current_hosts(self):
progress = self.daemon.get_scan_progress(scan_id)
self.assertEqual(progress, 33)

def test_get_scan_host_progress(self):
fs = FakeStream()
self.daemon.handle_command(
'<start_scan parallel="2">'
'<scanner_params />'
'<targets><target>'
'<hosts>localhost</hosts>'
'<ports>22</ports>'
'</target></targets>'
'</start_scan>',
fs,
)
self.daemon.start_queued_scans()
response = fs.get_response()

scan_id = response.findtext('id')
self.daemon.set_scan_host_progress(scan_id, 'localhost', 45)
self.assertEqual(
self.daemon.get_scan_host_progress(scan_id, 'localhost'), 45
)

def test_get_scan_without_scanid(self):

fs = FakeStream()
Expand Down Expand Up @@ -1238,6 +1259,35 @@ def test_set_scan_total_hosts(self):
count = self.daemon.scan_collection.get_count_total(scan_id)
self.assertEqual(count, 3)

def test_set_scan_total_hosts_zero(self):

fs = FakeStream()
self.daemon.handle_command(
'<start_scan parallel="2">'
'<scanner_params />'
'<targets><target>'
'<hosts>localhost1, localhost2, localhost3, localhost4</hosts>'
'<ports>22</ports>'
'</target></targets>'
'</start_scan>',
fs,
)
self.daemon.start_queued_scans()

response = fs.get_response()
scan_id = response.findtext('id')

# Default calculated by ospd with the hosts in the target
count = self.daemon.scan_collection.get_count_total(scan_id)
self.assertEqual(count, 4)

# Set to 0 (all hosts unresolved, dead, invalid target) via
# the server. This one has priority and must be still 0 and
# never overwritten with the calculation from host list
self.daemon.set_scan_total_hosts(scan_id, 0)
count = self.daemon.scan_collection.get_count_total(scan_id)
self.assertEqual(count, 0)

def test_set_scan_total_hosts_invalid_target(self):

fs = FakeStream()
Expand Down

0 comments on commit 651e0a4

Please sign in to comment.