From b7667882e0adc2354b1a4f03e625fd655aa8d309 Mon Sep 17 00:00:00 2001 From: penenadpi <penenadpi@gmail.com> Date: Thu, 1 Sep 2022 04:48:29 -0400 Subject: [PATCH] Fixed output handling in case of HTML response output --- src/iac_scan_runner/compatibility.py | 1 - src/iac_scan_runner/scan_runner.py | 33 ++++++++++++---------------- src/iac_scan_runner/utils.py | 15 +++++++++++++ 3 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/iac_scan_runner/compatibility.py b/src/iac_scan_runner/compatibility.py index 5817695..bfd4c9c 100644 --- a/src/iac_scan_runner/compatibility.py +++ b/src/iac_scan_runner/compatibility.py @@ -54,7 +54,6 @@ class Compatibility: try: for root, folders, names in os.walk(iac_directory): for f in names: - print(f) if (f.find(".tf") or f.find(".tftpl")) > -1: types.append("terraform") scanned_terraform.append(f) diff --git a/src/iac_scan_runner/scan_runner.py b/src/iac_scan_runner/scan_runner.py index 48387fb..199de7d 100644 --- a/src/iac_scan_runner/scan_runner.py +++ b/src/iac_scan_runner/scan_runner.py @@ -37,10 +37,12 @@ from iac_scan_runner.utils import ( generate_random_pathname, unpack_archive_to_dir, write_string_to_file, + file_to_string ) from pydantic import SecretStr import uuid import os +import json class ScanRunner: def __init__(self): @@ -142,25 +144,18 @@ class ScanRunner: compatible_checks = self.compatibility_matrix.get_all_compatible_checks(self.iac_dir) non_compatible_checks = [] - if scan_response_type == ScanResponseType.json: - scan_output = {} - else: - scan_output = "" + scan_output = {} + if selected_checks: for selected_check in selected_checks: check = self.iac_checks[selected_check] if check.enabled: if selected_check in compatible_checks: check_output = check.run(self.iac_dir) - if scan_response_type == ScanResponseType.json: - scan_output[selected_check] = check_output.to_dict() - else: - # TODO: Discuss the format of this output - scan_output += f"### {selected_check} ###\n{check_output.to_string()}\n\n" - + scan_output[selected_check] = check_output.to_dict() write_string_to_file(check.name, dir_name, scan_output[check.name]["output"]) self.results_summary.summarize_outcome(selected_check, scan_output[check.name]["output"], self.compatibility_matrix.scanned_files, Compatibility.compatibility_matrix) - + else: non_compatible_checks.append(check.name) write_string_to_file(check.name, dir_name, "No files to scan") @@ -168,23 +163,23 @@ class ScanRunner: self.results_summary.dump_outcomes(random_uuid) self.results_summary.generate_html_prioritized(random_uuid) - + else: for iac_check in self.iac_checks.values(): if iac_check.enabled: check_output = iac_check.run(self.iac_dir) - if scan_response_type == ScanResponseType.json: - scan_output[iac_check.name] = check_output.to_dict() - else: - # TODO: Discuss the format of this output - scan_output += ( - f"### {iac_check.name} ###\n{check_output.to_string()}\n\n" - ) + scan_output[iac_check.name] = check_output.to_dict() + # TODO: Discuss the format of this output write_string_to_file( iac_check.name, dir_name, scan_output[iac_check.name]["output"] ) + if scan_response_type == ScanResponseType.json: + scan_output = json.loads(file_to_string(f"../outputs/json_dumps/{random_uuid}.json")) + else: + scan_output = file_to_string(f"../outputs/generated_html/{random_uuid}.html") + return scan_output def enable_check(self, check_name: str) -> str: diff --git a/src/iac_scan_runner/utils.py b/src/iac_scan_runner/utils.py index 6c90b05..b93cc2a 100644 --- a/src/iac_scan_runner/utils.py +++ b/src/iac_scan_runner/utils.py @@ -97,3 +97,18 @@ def write_html_to_file(file_name: str, output_value: str): text_file.write(output_value) except Exception as e: raise Exception(f"Error storing HTML to file: {str(e)}.") + + +def file_to_string(file_path: str): + """ + Reads the file given by path and returns its contents as string output + :param file_path: Path of the file + :return output: Content read from file in form of a string + """ + output = "" + try: + with open(file_path, "r") as text_file: + output = str(text_file.read()) + except Exception as e: + raise Exception(f"Error while reading file: {str(e)}.") + return output -- GitLab