Skip to content
Snippets Groups Projects
Commit b7667882 authored by penenadpi's avatar penenadpi Committed by Anze Luzar
Browse files

Fixed output handling in case of HTML response output

parent d890ca9c
Branches
No related tags found
No related merge requests found
......@@ -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)
......
......@@ -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,22 +144,15 @@ 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 = ""
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"
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)
......@@ -173,18 +168,18 @@ class ScanRunner:
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"
)
# 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:
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment