diff --git a/src/iac_scan_runner/compatibility.py b/src/iac_scan_runner/compatibility.py index 58176950aa6357f293cb49bb512f97973886ce29..bfd4c9cddac6628d16d8aad98be15c2ecc480fc2 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 48387fb0d53b99665ab5e146564ca3ffc4593b2a..199de7dea3467fdb397142709ac3e22e39726a76 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 6c90b0573bd525e17c7e1f64709848baf0882d69..b93cc2a28899b765b0950a54560407e6b1dc5a7c 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