Codebase list python-faraday / upstream/3.17.1
New upstream version 3.17.1 Sophie Brun 2 years ago
7 changed file(s) with 57 addition(s) and 54 deletion(s). Raw diff Collapse all Expand all
0 * FIX bug when starting the server, creates a pool for reporting that breaks.
0 Aug 20th, 2021
00 New features in the latest update
11 =====================================
22
3
4 3.17.1 [Aug 20th, 2021]:
5 ---
6 * FIX bug when starting the server, creates a pool for reporting that breaks.
37
48 3.17.0 [Aug 10th, 2021]:
59 ---
00 New features in the latest update
11 =====================================
22
3
4 3.17.1 [Aug 20th, 2021]:
5 ---
6 * FIX bug when starting the server, creates a pool for reporting that breaks.
37
48 3.17.0 [Aug 10th, 2021]:
59 ---
11 # Copyright (C) 2013 Infobyte LLC (http://www.infobytesec.com/)
22 # See the file 'doc/LICENSE' for the license information
33
4 __version__ = '3.17.0'
4 __version__ = '3.17.1'
55 __license_version__ = __version__
138138 self.agent_registration_secret = None
139139 self.agent_token_expiration = 60 # Default as 1 min
140140 self.debug = False
141 self.reports_pool_size = 1
142141 self.delete_report_after_process = True
143142
144143
55 from queue import Queue, Empty
66 from typing import Tuple, Optional
77 import json
8 import multiprocessing
98
109 from faraday_plugins.plugins.manager import PluginsManager
1110 from faraday.server.api.modules.bulk_create import bulk_create, BulkCreateSchema
2524 def send_report_data(workspace_name: str, command_id: int, report_json: dict,
2625 user_id: Optional[int], set_end_date: bool):
2726 logger.info("Send Report data to workspace [%s]", workspace_name)
28 from faraday.server.web import get_app # pylint:disable=import-outside-toplevel
29 with get_app().app_context():
30 ws = Workspace.query.filter_by(name=workspace_name).one()
31 command = Command.query.filter_by(id=command_id).one()
32 schema = BulkCreateSchema()
33 data = schema.load(report_json)
34 if user_id:
35 user = User.query.filter_by(id=user_id).one()
36 data = add_creator(data, user)
37 bulk_create(ws, command, data, True, set_end_date)
27 ws = Workspace.query.filter_by(name=workspace_name).one()
28 command = Command.query.filter_by(id=command_id).one()
29 schema = BulkCreateSchema()
30 data = schema.load(report_json)
31 if user_id:
32 user = User.query.filter_by(id=user_id).one()
33 data = add_creator(data, user)
34 bulk_create(ws, command, data, True, set_end_date)
3835
3936
4037 def process_report(workspace_name: str, command_id: int, file_path: Path,
4138 plugin_id: Optional[int], user_id: Optional[int]):
42 if plugin_id is not None:
43 plugins_manager = PluginsManager(ReportsSettings.settings.custom_plugins_folder,
44 ignore_info=ReportsSettings.settings.ignore_info_severity)
45 logger.info(f"Reports Manager: [Custom plugins folder: "
46 f"[{ReportsSettings.settings.custom_plugins_folder}]"
47 f"[Ignore info severity: {ReportsSettings.settings.ignore_info_severity}]")
48 plugin = plugins_manager.get_plugin(plugin_id)
49 if plugin:
39 from faraday.server.web import get_app # pylint:disable=import-outside-toplevel
40 with get_app().app_context():
41 if plugin_id is not None:
42 plugins_manager = PluginsManager(ReportsSettings.settings.custom_plugins_folder,
43 ignore_info=ReportsSettings.settings.ignore_info_severity)
44 logger.info(f"Reports Manager: [Custom plugins folder: "
45 f"[{ReportsSettings.settings.custom_plugins_folder}]"
46 f"[Ignore info severity: {ReportsSettings.settings.ignore_info_severity}]")
47 plugin = plugins_manager.get_plugin(plugin_id)
48 if plugin:
49 try:
50 logger.info(f"Processing report [{file_path}] with plugin ["
51 f"{plugin.id}]")
52 plugin.processReport(str(file_path))
53 vulns_data = plugin.get_data()
54 del vulns_data['command']['duration']
55 except Exception as e:
56 logger.error("Processing Error: %s", e)
57 logger.exception(e)
58 return
59 else:
60 logger.error(f"No plugin detected for report [{file_path}]")
61 return
62 else:
5063 try:
51 logger.info(f"Processing report [{file_path}] with plugin ["
52 f"{plugin.id}]")
53 plugin.processReport(str(file_path))
54 vulns_data = plugin.get_data()
55 del vulns_data['command']['duration']
64 with file_path.open("r") as f:
65 vulns_data = json.load(f)
5666 except Exception as e:
57 logger.error("Processing Error: %s", e)
67 logger.error("Loading data from json file: %s [%s]", file_path, e)
5868 logger.exception(e)
5969 return
70 if plugin_id is None:
71 logger.debug("Removing file: %s", file_path)
72 os.remove(file_path)
6073 else:
61 logger.error(f"No plugin detected for report [{file_path}]")
62 return
63 else:
74 if faraday_server.delete_report_after_process:
75 os.remove(file_path)
76 set_end_date = True
6477 try:
65 with file_path.open("r") as f:
66 vulns_data = json.load(f)
78 send_report_data(workspace_name, command_id, vulns_data, user_id, set_end_date)
79 logger.info("Report processing finished")
6780 except Exception as e:
68 logger.error("Loading data from json file: %s [%s]", file_path, e)
6981 logger.exception(e)
70 return
71 if plugin_id is None:
72 logger.debug("Removing file: %s", file_path)
73 os.remove(file_path)
74 else:
75 if faraday_server.delete_report_after_process:
76 os.remove(file_path)
77 set_end_date = True
78 try:
79 send_report_data(workspace_name, command_id, vulns_data, user_id, set_end_date)
80 logger.info("Report processing finished")
81 except Exception as e:
82 logger.exception(e)
83 logger.error("Save Error: %s", e)
82 logger.error("Save Error: %s", e)
8483
8584
8685 class ReportsManager(Thread):
8988 super().__init__(name="ReportsManager-Thread", daemon=True, *args, **kwargs)
9089 self.upload_reports_queue = upload_reports_queue
9190 self.__event = threading.Event()
92 self.processing_pool = multiprocessing.Pool(processes=faraday_server.reports_pool_size)
9391
9492 def stop(self):
9593 logger.info("Reports Manager Thread [Stopping...]")
9694 self.__event.set()
9795
9896 def run(self):
99 logger.info(f"Reports Manager Thread [Start] with Pool Size: {faraday_server.reports_pool_size}")
97 logger.info("Reports Manager Thread [Start]")
10098 while not self.__event.is_set():
10199 try:
102100 tpl: Tuple[str, int, Path, int, int] = \
106104
107105 logger.info(f"Processing raw report {file_path}")
108106 if file_path.is_file():
109 self.processing_pool.apply_async(process_report,
110 (workspace_name, command_id, file_path, plugin_id, user_id))
107 process_report(workspace_name, command_id, file_path, plugin_id, user_id)
111108 else:
112109 logger.warning(f"Report file [{file_path}] don't exists",
113110 file_path)
121118 continue
122119 else:
123120 logger.info("Reports Manager Thread [Stop]")
124 self.processing_pool.close()
125 self.processing_pool.terminate()
126 self.processing_pool.join()