Skip to content

Commit 9c33b3f

Browse files
jfagoagaspedrooot
andauthored
refactor(stats): Use Finding instead of Check_Report (#7053)
Co-authored-by: pedrooot <[email protected]>
1 parent 7e7e2c8 commit 9c33b3f

File tree

3 files changed

+203
-115
lines changed

3 files changed

+203
-115
lines changed

prowler/__main__.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,20 @@ def prowler():
305305
print(f"{Style.BRIGHT}{Fore.GREEN}\nNo findings to fix!{Style.RESET_ALL}\n")
306306
sys.exit()
307307

308+
# Outputs
309+
# TODO: this part is needed since the checks generates a Check_Report_XXX and the output uses Finding
310+
# This will be refactored for the outputs generate directly the Finding
311+
finding_outputs = []
312+
for finding in findings:
313+
try:
314+
finding_outputs.append(
315+
Finding.generate_output(global_provider, finding, output_options)
316+
)
317+
except Exception:
318+
continue
319+
308320
# Extract findings stats
309-
stats = extract_findings_statistics(findings)
321+
stats = extract_findings_statistics(finding_outputs)
310322

311323
if args.slack:
312324
# TODO: this should be also in a config file
@@ -329,18 +341,6 @@ def prowler():
329341
)
330342
sys.exit(1)
331343

332-
# Outputs
333-
# TODO: this part is needed since the checks generates a Check_Report_XXX and the output uses Finding
334-
# This will be refactored for the outputs generate directly the Finding
335-
finding_outputs = []
336-
for finding in findings:
337-
try:
338-
finding_outputs.append(
339-
Finding.generate_output(global_provider, finding, output_options)
340-
)
341-
except Exception:
342-
continue
343-
344344
generated_outputs = {"regular": [], "compliance": []}
345345

346346
if args.output_formats:

prowler/lib/outputs/outputs.py

+32-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
from colorama import Fore, Style
22

33
from prowler.config.config import orange_color
4+
from prowler.lib.check.models import Severity
45
from prowler.lib.logger import logger
6+
from prowler.lib.outputs.common import Status
7+
from prowler.lib.outputs.finding import Finding
58

69

710
def stdout_report(finding, color, verbose, status, fix):
@@ -89,7 +92,7 @@ def set_report_color(status: str, muted: bool = False) -> str:
8992
return color
9093

9194

92-
def extract_findings_statistics(findings: list) -> dict:
95+
def extract_findings_statistics(findings: list[Finding]) -> dict:
9396
"""
9497
extract_findings_statistics takes a list of findings and returns the following dict with the aggregated statistics
9598
{
@@ -121,38 +124,46 @@ def extract_findings_statistics(findings: list) -> dict:
121124
medium_severity_fail = 0
122125
low_severity_pass = 0
123126
low_severity_fail = 0
127+
informational_severity_pass = 0
128+
informational_severity_fail = 0
124129

125130
for finding in findings:
126-
# Save the resource_id
127-
resources.add(finding.resource_id)
131+
resources.add(finding.resource_uid)
128132

129-
if finding.status == "PASS":
130-
if finding.check_metadata.Severity == "critical":
133+
if finding.status == Status.PASS:
134+
findings_count += 1
135+
total_pass += 1
136+
if finding.metadata.Severity == Severity.critical:
131137
critical_severity_pass += 1
132-
if finding.check_metadata.Severity == "high":
138+
if finding.metadata.Severity == Severity.high:
133139
high_severity_pass += 1
134-
if finding.check_metadata.Severity == "medium":
140+
if finding.metadata.Severity == Severity.medium:
135141
medium_severity_pass += 1
136-
if finding.check_metadata.Severity == "low":
142+
if finding.metadata.Severity == Severity.low:
137143
low_severity_pass += 1
138-
total_pass += 1
139-
findings_count += 1
144+
if finding.metadata.Severity == Severity.informational:
145+
informational_severity_pass += 1
146+
140147
if finding.muted is True:
141148
muted_pass += 1
142149

143-
if finding.status == "FAIL":
144-
if finding.check_metadata.Severity == "critical":
150+
if finding.status == Status.FAIL:
151+
findings_count += 1
152+
total_fail += 1
153+
if finding.metadata.Severity == Severity.critical:
145154
critical_severity_fail += 1
146-
if finding.check_metadata.Severity == "high":
155+
if finding.metadata.Severity == Severity.high:
147156
high_severity_fail += 1
148-
if finding.check_metadata.Severity == "medium":
157+
if finding.metadata.Severity == Severity.medium:
149158
medium_severity_fail += 1
150-
if finding.check_metadata.Severity == "low":
159+
if finding.metadata.Severity == Severity.low:
151160
low_severity_fail += 1
152-
total_fail += 1
153-
findings_count += 1
161+
if finding.metadata.Severity == Severity.informational:
162+
informational_severity_fail += 1
163+
154164
if finding.muted is True:
155165
muted_fail += 1
166+
156167
if not finding.muted and all_fails_are_muted:
157168
all_fails_are_muted = False
158169

@@ -168,8 +179,10 @@ def extract_findings_statistics(findings: list) -> dict:
168179
stats["total_high_severity_pass"] = high_severity_pass
169180
stats["total_medium_severity_fail"] = medium_severity_fail
170181
stats["total_medium_severity_pass"] = medium_severity_pass
171-
stats["total_low_severity_fail"] = medium_severity_fail
172-
stats["total_low_severity_pass"] = medium_severity_pass
182+
stats["total_low_severity_fail"] = low_severity_fail
183+
stats["total_low_severity_pass"] = low_severity_pass
184+
stats["total_informational_severity_pass"] = informational_severity_pass
185+
stats["total_informational_severity_fail"] = informational_severity_fail
173186
stats["all_fails_are_muted"] = all_fails_are_muted
174187

175188
return stats

0 commit comments

Comments
 (0)