Skip to content

Commit

Permalink
refactor(stats): Use Finding instead of Check_Report (#7053)
Browse files Browse the repository at this point in the history
Co-authored-by: pedrooot <[email protected]>
  • Loading branch information
jfagoagas and pedrooot authored Feb 28, 2025
1 parent 7e7e2c8 commit 9c33b3f
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 115 deletions.
26 changes: 13 additions & 13 deletions prowler/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,20 @@ def prowler():
print(f"{Style.BRIGHT}{Fore.GREEN}\nNo findings to fix!{Style.RESET_ALL}\n")
sys.exit()

# Outputs
# TODO: this part is needed since the checks generates a Check_Report_XXX and the output uses Finding
# This will be refactored for the outputs generate directly the Finding
finding_outputs = []
for finding in findings:
try:
finding_outputs.append(
Finding.generate_output(global_provider, finding, output_options)
)
except Exception:
continue

# Extract findings stats
stats = extract_findings_statistics(findings)
stats = extract_findings_statistics(finding_outputs)

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

# Outputs
# TODO: this part is needed since the checks generates a Check_Report_XXX and the output uses Finding
# This will be refactored for the outputs generate directly the Finding
finding_outputs = []
for finding in findings:
try:
finding_outputs.append(
Finding.generate_output(global_provider, finding, output_options)
)
except Exception:
continue

generated_outputs = {"regular": [], "compliance": []}

if args.output_formats:
Expand Down
51 changes: 32 additions & 19 deletions prowler/lib/outputs/outputs.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from colorama import Fore, Style

from prowler.config.config import orange_color
from prowler.lib.check.models import Severity
from prowler.lib.logger import logger
from prowler.lib.outputs.common import Status
from prowler.lib.outputs.finding import Finding


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


def extract_findings_statistics(findings: list) -> dict:
def extract_findings_statistics(findings: list[Finding]) -> dict:
"""
extract_findings_statistics takes a list of findings and returns the following dict with the aggregated statistics
{
Expand Down Expand Up @@ -121,38 +124,46 @@ def extract_findings_statistics(findings: list) -> dict:
medium_severity_fail = 0
low_severity_pass = 0
low_severity_fail = 0
informational_severity_pass = 0
informational_severity_fail = 0

for finding in findings:
# Save the resource_id
resources.add(finding.resource_id)
resources.add(finding.resource_uid)

if finding.status == "PASS":
if finding.check_metadata.Severity == "critical":
if finding.status == Status.PASS:
findings_count += 1
total_pass += 1
if finding.metadata.Severity == Severity.critical:
critical_severity_pass += 1
if finding.check_metadata.Severity == "high":
if finding.metadata.Severity == Severity.high:
high_severity_pass += 1
if finding.check_metadata.Severity == "medium":
if finding.metadata.Severity == Severity.medium:
medium_severity_pass += 1
if finding.check_metadata.Severity == "low":
if finding.metadata.Severity == Severity.low:
low_severity_pass += 1
total_pass += 1
findings_count += 1
if finding.metadata.Severity == Severity.informational:
informational_severity_pass += 1

if finding.muted is True:
muted_pass += 1

if finding.status == "FAIL":
if finding.check_metadata.Severity == "critical":
if finding.status == Status.FAIL:
findings_count += 1
total_fail += 1
if finding.metadata.Severity == Severity.critical:
critical_severity_fail += 1
if finding.check_metadata.Severity == "high":
if finding.metadata.Severity == Severity.high:
high_severity_fail += 1
if finding.check_metadata.Severity == "medium":
if finding.metadata.Severity == Severity.medium:
medium_severity_fail += 1
if finding.check_metadata.Severity == "low":
if finding.metadata.Severity == Severity.low:
low_severity_fail += 1
total_fail += 1
findings_count += 1
if finding.metadata.Severity == Severity.informational:
informational_severity_fail += 1

if finding.muted is True:
muted_fail += 1

if not finding.muted and all_fails_are_muted:
all_fails_are_muted = False

Expand All @@ -168,8 +179,10 @@ def extract_findings_statistics(findings: list) -> dict:
stats["total_high_severity_pass"] = high_severity_pass
stats["total_medium_severity_fail"] = medium_severity_fail
stats["total_medium_severity_pass"] = medium_severity_pass
stats["total_low_severity_fail"] = medium_severity_fail
stats["total_low_severity_pass"] = medium_severity_pass
stats["total_low_severity_fail"] = low_severity_fail
stats["total_low_severity_pass"] = low_severity_pass
stats["total_informational_severity_pass"] = informational_severity_pass
stats["total_informational_severity_fail"] = informational_severity_fail
stats["all_fails_are_muted"] = all_fails_are_muted

return stats
Loading

0 comments on commit 9c33b3f

Please sign in to comment.