2020
2121# IMPORTS ------------------------------------------------
2222import hashlib
23- import logging
2423import os
2524import shutil
2625import tempfile
3433
3534import iris_interface .IrisInterfaceStatus as InterfaceStatus
3635
37- log = logging .getLogger ('iris' )
38-
3936
4037# CONTENT ------------------------------------------------
4138def decompress_7z (filename , output_dir ):
@@ -50,7 +47,7 @@ def decompress_7z(filename, output_dir):
5047 a .extractall (directory = output_dir , auto_create_dir = True )
5148
5249 except Exception as e :
53- log . warning (e )
50+ print (e )
5451 return False
5552
5653 return True
@@ -61,15 +58,11 @@ class ImportDispatcher(object):
6158 Allows to dispatch files to each related importers
6259 """
6360
64- def __init__ (self , task_self , task_args = None , evidence_storage = None , configuration = None ):
61+ def __init__ (self , task_self , task_args = None , evidence_storage = None , configuration = None , log = None ):
6562 self .task = task_self
6663 self .evidence_storage = evidence_storage
6764 self .configuration = configuration
68- self .message_queue = []
69- handler = InterfaceStatus .QueuingHandler (message_queue = self .message_queue ,
70- level = logging .INFO ,
71- celery_task = task_self )
72- log .addHandler (handler )
65+ self .log = log
7366
7467 self .index = task_args ['pipeline_args' ]['index_evtx' ]
7568 self .user = task_args ['user' ]
@@ -85,34 +78,22 @@ def _ret_task_success(self):
8578 Return a task compatible success object to be passed to the next task
8679 :return:
8780 """
88- return InterfaceStatus .iit_report_task_success (
89- user = self .user ,
90- initial = self .task .request .id ,
91- case_name = self .case_name ,
92- logs = list (self .message_queue ),
93- data = {}
94- )
81+ return InterfaceStatus .I2Success
9582
9683 def _ret_task_failure (self ):
9784 """
9885 Return a task compatible failure object to be passed to the next task
9986 :return:
10087 """
101- return InterfaceStatus .iit_report_task_failure (
102- user = self .user ,
103- initial = self .task .request .id ,
104- case_name = self .case_name ,
105- logs = list (self .message_queue ),
106- data = {}
107- )
88+ return InterfaceStatus .I2Error
10889
10990 def import_files (self ):
11091 """
11192 Check every uploaded files and dispatch to handlers
11293 :return:
11394 """
11495
115- log .info ("Received new evtx import signal for {}" .format (self .case_name ))
96+ self . log .info ("Received new evtx import signal for {}" .format (self .case_name ))
11697
11798 temp_zippath = tempfile .TemporaryDirectory ()
11899 shutil .move (str (self .path ), temp_zippath .name )
@@ -121,41 +102,25 @@ def import_files(self):
121102
122103 import_list = self ._create_import_list (path = self .path )
123104
124- ret = self . _ret_task_success ()
105+ ret = None
125106 if import_list :
126107
127108 for data_type in import_list :
128109
129110 ret_t = self .inner_import_files (import_list [data_type ], data_type )
130111
131112 # Merge the result with the current caller
132- ret . merge_task_results ( ret_t , is_update = self . is_update )
113+ ret = InterfaceStatus . merge_status ( ret , ret_t )
133114
134115 else :
135116
136- log .error ("Import list was empty. Please check previous errors." )
137- log .error ("Either internal error, either the files could not be uploaded successfully." )
138- log .error ("Nothing to import" )
117+ self . log .error ("Import list was empty. Please check previous errors." )
118+ self . log .error ("Either internal error, either the files could not be uploaded successfully." )
119+ self . log .error ("Nothing to import" )
139120 ret = self ._ret_task_failure ()
140121
141122 return ret
142123
143- def _merge_task_results (self , base_ret , new_ret , type ):
144- """
145- Merge the result of multiple tasks
146- :param base_ret: Task return to merge
147- :return:
148- """
149- # Set the overall task success at false if any of the task failed
150- base_ret ['success' ] = new_ret ['success' ] and base_ret ['success' ]
151-
152- # Concatenate the tasks logs to display everything at the end
153- base_ret ['logs' ] += new_ret ['logs' ]
154-
155- base_ret ['data' ]['is_update' ] = self .is_update
156-
157- return base_ret
158-
159124 def _create_import_list (self , path = None ):
160125 """
161126 Create the list for every files
@@ -165,8 +130,8 @@ def _create_import_list(self, path=None):
165130 import_list = {
166131 }
167132
168- log .info ("Checking input files" )
169- log .info ("Path is {}" .format (path ))
133+ self . log .info ("Checking input files" )
134+ self . log .info ("Path is {}" .format (path ))
170135
171136 if path .is_dir ():
172137 for entry in path .iterdir ():
@@ -209,20 +174,20 @@ def _create_import_list(self, path=None):
209174 if not is_valid :
210175 try :
211176 entry .unlink ()
212- log .debug (entry )
177+ self . log .debug (entry )
213178 except Exception :
214179 pass
215- log .info ("File has been deleted from the server" )
180+ self . log .info ("File has been deleted from the server" )
216181
217182 else :
218183 entry .unlink ()
219- log .warning ("{} was already imported" .format (entry ))
184+ self . log .warning ("{} was already imported" .format (entry ))
220185
221186 # log.info("Detected {} valid files".format(len(import_list)))
222187 return import_list
223188
224189 else :
225- log .error ("Internal error. Provided path is not a path" )
190+ self . log .error ("Internal error. Provided path is not a path" )
226191 return None
227192
228193 def inner_import_files (self , import_list : list , files_type ):
@@ -233,10 +198,10 @@ def inner_import_files(self, import_list: list, files_type):
233198 :return: True if imported, false if not + list of errors
234199 """
235200
236- log .info ("New imports for {} on behalf of {}" .format (self .case_name , self .user ))
237- log .info ("{} files of type {} to import into {}" .format (len (import_list ), files_type , self .index ))
201+ self . log .info ("New imports for {} on behalf of {}" .format (self .case_name , self .user ))
202+ self . log .info ("{} files of type {} to import into {}" .format (len (import_list ), files_type , self .index ))
238203
239- log .info ("Starting processing of files" )
204+ self . log .info ("Starting processing of files" )
240205
241206 in_path = import_list [0 ].parent
242207 # Temporary files are placed in the same directory, not in tmp as there is a
@@ -253,12 +218,13 @@ def inner_import_files(self, import_list: list, files_type):
253218 elif files_type == "evtx" :
254219 in_path_evtx = in_path
255220 else :
256- log .error ("Unexpected file type, aborting..." )
221+ self . log .error ("Unexpected file type, aborting..." )
257222 return self ._ret_task_failure ()
258223
259224 start_time = time .time ()
260225
261226 e2s = Evtx2Splunk ()
227+
262228 # We could just pass on self.configuration, but we prefer to format the dict in such way that
263229 # field names in evtx2splunk will not depend on IrisEVTXModule
264230 proxies = {
@@ -288,7 +254,7 @@ def inner_import_files(self, import_list: list, files_type):
288254
289255 end_time = time .time ()
290256
291- log .info ("Finished in {time}" .format (time = end_time - start_time ))
257+ self . log .info ("Finished in {time}" .format (time = end_time - start_time ))
292258
293259 if ret_t is False :
294260 return self ._ret_task_failure ()
0 commit comments