From ef0d4fe34b5194e230460ff5332ac35ea50ffff0 Mon Sep 17 00:00:00 2001 From: Pepe Fagoaga Date: Tue, 17 Jan 2023 11:40:05 +0100 Subject: [PATCH] fix(fill_html_overview_statistics): Handle if file exists (#1718) --- prowler/lib/check/check.py | 5 ++++- prowler/lib/outputs/html.py | 36 ++++++++++++++++++++---------------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/prowler/lib/check/check.py b/prowler/lib/check/check.py index e41b3959..22b945cd 100644 --- a/prowler/lib/check/check.py +++ b/prowler/lib/check/check.py @@ -298,7 +298,10 @@ def run_check(check: Check, output_options: Provider_Output_Options) -> list: try: findings = check.execute() except Exception as error: - print(f"Something went wrong in {check.CheckID}, please use --log-level ERROR") + if not output_options.only_logs: + print( + f"Something went wrong in {check.CheckID}, please use --log-level ERROR" + ) logger.error( f"{check.CheckID} -- {error.__class__.__name__}[{error.__traceback__.tb_lineno}]: {error}" ) diff --git a/prowler/lib/outputs/html.py b/prowler/lib/outputs/html.py index ac6d2dc7..4634ca4f 100644 --- a/prowler/lib/outputs/html.py +++ b/prowler/lib/outputs/html.py @@ -1,5 +1,6 @@ import sys from os import path + from prowler.config.config import ( html_file_suffix, html_logo_img, @@ -235,23 +236,26 @@ def fill_html_overview_statistics(stats, output_filename, output_directory): try: filename = f"{output_directory}/{output_filename}{html_file_suffix}" # Read file - with open(filename, "r") as file: - filedata = file.read() + if path.isfile(filename): + with open(filename, "r") as file: + filedata = file.read() - # Replace statistics - # TOTAL_FINDINGS - filedata = filedata.replace("TOTAL_FINDINGS", str(stats.get("findings_count"))) - # TOTAL_RESOURCES - filedata = filedata.replace( - "TOTAL_RESOURCES", str(stats.get("resources_count")) - ) - # TOTAL_PASS - filedata = filedata.replace("TOTAL_PASS", str(stats.get("total_pass"))) - # TOTAL_FAIL - filedata = filedata.replace("TOTAL_FAIL", str(stats.get("total_fail"))) - # Write file - with open(filename, "w") as file: - file.write(filedata) + # Replace statistics + # TOTAL_FINDINGS + filedata = filedata.replace( + "TOTAL_FINDINGS", str(stats.get("findings_count")) + ) + # TOTAL_RESOURCES + filedata = filedata.replace( + "TOTAL_RESOURCES", str(stats.get("resources_count")) + ) + # TOTAL_PASS + filedata = filedata.replace("TOTAL_PASS", str(stats.get("total_pass"))) + # TOTAL_FAIL + filedata = filedata.replace("TOTAL_FAIL", str(stats.get("total_fail"))) + # Write file + with open(filename, "w") as file: + file.write(filedata) except Exception as error: logger.critical(