Fixed bug in report generation, added lock release for exceptions in report generation

This commit is contained in:
Shay Nehmad 2020-01-20 17:11:30 +02:00
parent f8aff44e8b
commit 04b7370575
2 changed files with 19 additions and 12 deletions

View File

@ -386,10 +386,11 @@ class ReportService:
@staticmethod @staticmethod
def get_monkey_subnets(monkey_guid): def get_monkey_subnets(monkey_guid):
network_info = mongo.db.telemetry.find_one( network_info = mongo.db.telemetry.find_one(
{'telem_category': 'system_info', 'monkey_guid': monkey_guid}, {'telem_category': 'system_info',
'monkey_guid': monkey_guid},
{'data.network_info.networks': 1} {'data.network_info.networks': 1}
) )
if network_info is None: if network_info is None or not network_info["data"]:
return [] return []
return \ return \

View File

@ -15,28 +15,34 @@ __regular_report_generating_lock = threading.Semaphore()
def safe_generate_reports(): def safe_generate_reports():
# Entering the critical section; Wait until report generation is available. # Entering the critical section; Wait until report generation is available.
__report_generating_lock.acquire() __report_generating_lock.acquire()
report = safe_generate_regular_report() try:
attack_report = safe_generate_attack_report() report = safe_generate_regular_report()
# Leaving the critical section. attack_report = safe_generate_attack_report()
__report_generating_lock.release() finally:
# Leaving the critical section.
__report_generating_lock.release()
return report, attack_report return report, attack_report
def safe_generate_regular_report(): def safe_generate_regular_report():
# Local import to avoid circular imports # Local import to avoid circular imports
from monkey_island.cc.services.reporting.report import ReportService from monkey_island.cc.services.reporting.report import ReportService
__regular_report_generating_lock.acquire() try:
report = ReportService.generate_report() __regular_report_generating_lock.acquire()
__regular_report_generating_lock.release() report = ReportService.generate_report()
finally:
__regular_report_generating_lock.release()
return report return report
def safe_generate_attack_report(): def safe_generate_attack_report():
# Local import to avoid circular imports # Local import to avoid circular imports
from monkey_island.cc.services.attack.attack_report import AttackReportService from monkey_island.cc.services.attack.attack_report import AttackReportService
__attack_report_generating_lock.acquire() try:
attack_report = AttackReportService.generate_new_report() __attack_report_generating_lock.acquire()
__attack_report_generating_lock.release() attack_report = AttackReportService.generate_new_report()
finally:
__attack_report_generating_lock.release()
return attack_report return attack_report