def intercept_result(context, future: Future) -> None: """ Box-JS doesn't output result to standard output but to a file, using this callback function, we intercept the future object and update it's result attribute by reading the final analysis result from the saved result file before it is ready to be consumed. """ # get current result res = future.result() fname = context.get("read_result_from", "") dir_loc = safe_join("/tmp/boxjs", fname + ".results") if not fname: if res.get("returncode", -1) == 0: res["returncode"] = -1 raise Exception("No file specified to read result from") try: res["report"] = read_files_and_make_report(dir_loc) except Exception as e: res["error"] += str(e) finally: # set final result future._result = res # Remove the directory shutil.rmtree(dir_loc, ignore_errors=True)
def intercept_result(context, future: Future) -> None: """ Floss doesn't output result to standard output but to a file, using this callback function, we intercept the future object and update it's result attribute by reading the final analysis result from the saved result file before it is ready to be consumed. """ # 1. get current result object res = future.result() # 2. dir from which we will read final analysis result f_loc = context.get("read_result_from", None) if not f_loc: res["error"] += ", No specified file to read result from" if res.get("returncode", -1) == 0: res["returncode"] = -1 else: try: with open(f"/tmp/{f_loc}.json", "r") as fp: try: res["report"] = json.load(fp) except json.JSONDecodeError: res["report"] = fp.read() except FileNotFoundError: res["error"] += ", Output File Not Found." # 4. set final result after modifications future._result = res # 5. file can be removed now os.remove(f_loc)
def intercept_result(context, future: Future): """ Will be invoked on every process completion """ data = None if future.done(): fname = context.get("read_result_from", None) with open(fname) as f: data = f.read() # 1. get current result object res = future.result() # this returns a dictionary # 2. update the report variable, # you may update only these: report,error,status res["report"] = data if context.get("force_success", False): res["status"] = "success" # 3. set new result future._result = res
def intercept_thug_result(context, future: Future) -> None: """ Thug doesn't output result to standard output but to a file, using this callback function, we intercept the future object and update its result attribute by reading the final analysis result from the saved result file before it is ready to be consumed. """ dir_loc = None # 1. get current result object res = future.result() # 2. dir from which we will read final analysis result dir_name = context.get("read_result_from", None) if not dir_name: res["error"] += ", No specified file to read result from" if res.get("returncode", -1) == 0: res["returncode"] = -1 else: # 3. read saved result file, if it exists dir_loc = safe_join("/opt/deploy/thug", dir_name) f_loc = dir_loc + "/analysis/json/analysis.json" if not os.path.exists(f_loc): res["error"] += f", result file {f_loc} does not exists." if res.get("returncode", -1) == 0: res["returncode"] = -1 else: with open(f_loc, "r") as fp: try: res["report"] = json.load(fp) except json.JSONDecodeError: res["report"] = fp.read() # 4. set final result after modifications future._result = res # 5. directory can be removed now if dir_loc: shutil.rmtree(dir_loc, ignore_errors=True)