This repository has been archived by the owner on Nov 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Add debug level log messages #352
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1e46ef0
Add more log debug
jjnicola e0c0993
Set end_time if the scan was interrupted
jjnicola 9e5751f
Update changelog
jjnicola a2a583e
Use variable instead of calling the method again
jjnicola f6a75a9
Revert small code improvement.
jjnicola 302aae9
Improve docstrings and fix log messages
jjnicola File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -27,6 +27,7 @@ | |||||||
import time | ||||||||
import os | ||||||||
|
||||||||
from pprint import pformat | ||||||||
from typing import ( | ||||||||
List, | ||||||||
Any, | ||||||||
|
@@ -393,6 +394,9 @@ def stop_scan(self, scan_id: str) -> None: | |||||||
logger.debug('%s: The scanner task stopped unexpectedly.', scan_id) | ||||||||
|
||||||||
try: | ||||||||
logger.debug( | ||||||||
'%s: Terminating process group after stopping.', scan_id | ||||||||
) | ||||||||
_terminate_process_group(scan_process) | ||||||||
except ProcessLookupError: | ||||||||
logger.info( | ||||||||
|
@@ -443,6 +447,7 @@ def daemon_exit_cleanup(self) -> None: | |||||||
and status != ScanStatus.FINISHED | ||||||||
and status != ScanStatus.INTERRUPTED | ||||||||
): | ||||||||
logger.debug("%s: Stopping scan before daemon exit.", scan_id) | ||||||||
self.stop_scan(scan_id) | ||||||||
|
||||||||
# Wait for scans to be in some stopped state. | ||||||||
|
@@ -458,8 +463,12 @@ def daemon_exit_cleanup(self) -> None: | |||||||
all_stopped = False | ||||||||
|
||||||||
if all_stopped: | ||||||||
logger.debug( | ||||||||
"All scans stopped and daemon clean and ready to exit" | ||||||||
) | ||||||||
return | ||||||||
|
||||||||
logger.debug("Waiting for running scans before daemon exit. ") | ||||||||
time.sleep(1) | ||||||||
|
||||||||
def get_daemon_name(self) -> str: | ||||||||
|
@@ -574,19 +583,28 @@ def start_scan(self, scan_id: str) -> None: | |||||||
host=self.get_scan_host(scan_id), | ||||||||
value='Host process failure (%s).' % e, | ||||||||
) | ||||||||
logger.exception('While scanning: %s', scan_id) | ||||||||
logger.exception('%s: Exception %s while scanning', scan_id, e) | ||||||||
else: | ||||||||
logger.info("%s: Host scan finished.", scan_id) | ||||||||
|
||||||||
is_stopped = self.get_scan_status(scan_id) == ScanStatus.STOPPED | ||||||||
status = self.get_scan_status(scan_id) | ||||||||
is_stopped = status == ScanStatus.STOPPED | ||||||||
self.set_scan_progress(scan_id) | ||||||||
progress = self.get_scan_progress(scan_id) | ||||||||
|
||||||||
if not is_stopped and progress == ScanProgress.FINISHED: | ||||||||
self.finish_scan(scan_id) | ||||||||
elif not is_stopped: | ||||||||
logger.info( | ||||||||
"%s: Host scan finished. Progress: %d, " "Status: %s", | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
scan_id, | ||||||||
progress, | ||||||||
status.name, | ||||||||
) | ||||||||
self.interrupt_scan(scan_id) | ||||||||
|
||||||||
# For debug purposes | ||||||||
self._get_scan_progress_raw(scan_id) | ||||||||
|
||||||||
def dry_run_scan(self, scan_id: str, target: Dict) -> None: | ||||||||
""" Dry runs a scan. """ | ||||||||
|
||||||||
|
@@ -680,11 +698,14 @@ def set_scan_host_progress( | |||||||
|
||||||||
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) | ||||||||
self.scan_collection.set_status(scan_id, status) | ||||||||
|
||||||||
def get_scan_status(self, scan_id: str) -> ScanStatus: | ||||||||
""" Get scan_id scans's status.""" | ||||||||
return self.scan_collection.get_status(scan_id) | ||||||||
status = self.scan_collection.get_status(scan_id) | ||||||||
logger.debug('%s: Current scan status: %s,', scan_id, status.name) | ||||||||
return status | ||||||||
|
||||||||
def scan_exists(self, scan_id: str) -> bool: | ||||||||
"""Checks if a scan with ID scan_id is in collection. | ||||||||
|
@@ -766,11 +787,8 @@ def get_scan_results_xml( | |||||||
logger.debug('Returning %d results', len(results)) | ||||||||
return results | ||||||||
|
||||||||
def _get_scan_progress_xml(self, scan_id: str): | ||||||||
"""Gets scan_id scan's progress in XML format. | ||||||||
|
||||||||
@return: String of scan progress in xml. | ||||||||
""" | ||||||||
def _get_scan_progress_raw(self, scan_id: str) -> Dict: | ||||||||
"""Returns a dictionary with scan_id scan's progress information.""" | ||||||||
current_progress = dict() | ||||||||
|
||||||||
current_progress[ | ||||||||
|
@@ -790,6 +808,19 @@ def _get_scan_progress_xml(self, scan_id: str): | |||||||
scan_id | ||||||||
) | ||||||||
|
||||||||
logging.debug( | ||||||||
"%s: Current progress: \n%s", | ||||||||
scan_id, | ||||||||
pformat(current_progress), | ||||||||
) | ||||||||
return current_progress | ||||||||
|
||||||||
def _get_scan_progress_xml(self, scan_id: str): | ||||||||
"""Gets scan_id scan's progress in XML format. | ||||||||
|
||||||||
@return: String of scan progress in xml. | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be
Suggested change
|
||||||||
""" | ||||||||
current_progress = self._get_scan_progress_raw(scan_id) | ||||||||
return get_progress_xml(current_progress) | ||||||||
|
||||||||
@deprecated( | ||||||||
|
@@ -1242,8 +1273,18 @@ def handle_command(self, data: bytes, stream: Stream) -> None: | |||||||
scan_id = tree.get('scan_id') | ||||||||
if self.scan_exists(scan_id) and command_name == "get_scans": | ||||||||
if write_success: | ||||||||
logger.debug( | ||||||||
'%s: Results sent succesfully to the client. Cleaning ' | ||||||||
'temporary result list.', | ||||||||
scan_id, | ||||||||
) | ||||||||
self.scan_collection.clean_temp_result_list(scan_id) | ||||||||
else: | ||||||||
logger.debug( | ||||||||
'%s: Failed sending results to the client. Restoring ' | ||||||||
'result list into the cache.', | ||||||||
scan_id, | ||||||||
) | ||||||||
self.scan_collection.restore_temp_result_list(scan_id) | ||||||||
|
||||||||
def check(self): | ||||||||
|
@@ -1426,7 +1467,8 @@ def clean_forgotten_scans(self) -> None: | |||||||
|
||||||||
def check_scan_process(self, scan_id: str) -> None: | ||||||||
""" Check the scan's process, and terminate the scan if not alive. """ | ||||||||
if self.get_scan_status(scan_id) == ScanStatus.QUEUED: | ||||||||
status = self.get_scan_status(scan_id) | ||||||||
if status == ScanStatus.QUEUED: | ||||||||
return | ||||||||
|
||||||||
scan_process = self.scan_processes.get(scan_id) | ||||||||
|
@@ -1437,18 +1479,28 @@ def check_scan_process(self, scan_id: str) -> None: | |||||||
and scan_process | ||||||||
and not scan_process.is_alive() | ||||||||
): | ||||||||
if not self.get_scan_status(scan_id) == ScanStatus.STOPPED: | ||||||||
self.set_scan_status(scan_id, ScanStatus.STOPPED) | ||||||||
if not status == ScanStatus.STOPPED: | ||||||||
self.add_scan_error( | ||||||||
scan_id, name="", host="", value="Scan process failure." | ||||||||
scan_id, name="", host="", value="Scan process Failure" | ||||||||
) | ||||||||
|
||||||||
logger.info("%s: Scan stopped with errors.", scan_id) | ||||||||
logger.info( | ||||||||
"%s: Scan process is dead and its progress is %d", | ||||||||
scan_id, | ||||||||
progress, | ||||||||
) | ||||||||
self.interrupt_scan(scan_id) | ||||||||
|
||||||||
elif progress == ScanProgress.FINISHED: | ||||||||
scan_process.join(0) | ||||||||
|
||||||||
logger.debug( | ||||||||
"%s: Check scan process: \n\tProgress %d\n\t Status: %s", | ||||||||
scan_id, | ||||||||
progress, | ||||||||
status.name, | ||||||||
) | ||||||||
|
||||||||
def get_count_queued_scans(self) -> int: | ||||||||
""" Get the amount of scans with queued status """ | ||||||||
count = 0 | ||||||||
|
@@ -1459,7 +1511,9 @@ def get_count_queued_scans(self) -> int: | |||||||
|
||||||||
def get_scan_progress(self, scan_id: str) -> int: | ||||||||
""" Gives a scan's current progress value. """ | ||||||||
return self.scan_collection.get_progress(scan_id) | ||||||||
progress = self.scan_collection.get_progress(scan_id) | ||||||||
logger.debug('%s: Current scan progress: %s,', scan_id, progress) | ||||||||
return progress | ||||||||
|
||||||||
def get_scan_host(self, scan_id: str) -> str: | ||||||||
""" Gives a scan's target. """ | ||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this part be put into another separated PR?