mirror of
https://github.com/ghndrx/prowler.git
synced 2026-02-10 14:55:00 +00:00
fix(check_report): Init status field and fix stats output (#1580)
This commit is contained in:
@@ -91,6 +91,7 @@ class Check_Report:
|
|||||||
resource_tags: list
|
resource_tags: list
|
||||||
|
|
||||||
def __init__(self, metadata):
|
def __init__(self, metadata):
|
||||||
|
self.status = ""
|
||||||
self.check_metadata = Check_Metadata_Model.parse_raw(metadata)
|
self.check_metadata = Check_Metadata_Model.parse_raw(metadata)
|
||||||
self.status_extended = ""
|
self.status_extended = ""
|
||||||
self.resource_details = ""
|
self.resource_details = ""
|
||||||
|
|||||||
@@ -202,6 +202,16 @@ def send_to_s3_bucket(
|
|||||||
|
|
||||||
|
|
||||||
def extract_findings_statistics(findings: list) -> dict:
|
def extract_findings_statistics(findings: list) -> dict:
|
||||||
|
"""
|
||||||
|
extract_findings_statistics takes a list of findings and returns the following dict with the aggregated statistics
|
||||||
|
{
|
||||||
|
"total_pass": 0,
|
||||||
|
"total_fail": 0,
|
||||||
|
"resources_count": 0,
|
||||||
|
"findings_count": 0,
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
logger.info("Extracting audit statistics...")
|
||||||
stats = {}
|
stats = {}
|
||||||
total_pass = 0
|
total_pass = 0
|
||||||
total_fail = 0
|
total_fail = 0
|
||||||
@@ -211,12 +221,12 @@ def extract_findings_statistics(findings: list) -> dict:
|
|||||||
for finding in findings:
|
for finding in findings:
|
||||||
# Save the resource_id
|
# Save the resource_id
|
||||||
resources.add(finding.resource_id)
|
resources.add(finding.resource_id)
|
||||||
# Increment findings
|
|
||||||
findings_count += 1
|
|
||||||
if finding.status == "PASS":
|
if finding.status == "PASS":
|
||||||
total_pass += 1
|
total_pass += 1
|
||||||
|
findings_count += 1
|
||||||
if finding.status == "FAIL":
|
if finding.status == "FAIL":
|
||||||
total_fail += 1
|
total_fail += 1
|
||||||
|
findings_count += 1
|
||||||
|
|
||||||
stats["total_pass"] = total_pass
|
stats["total_pass"] = total_pass
|
||||||
stats["total_fail"] = total_fail
|
stats["total_fail"] = total_fail
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import os
|
import os
|
||||||
from os import path, remove
|
from os import path, remove
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
import boto3
|
import boto3
|
||||||
import pytest
|
import pytest
|
||||||
@@ -19,15 +20,16 @@ from prowler.lib.check.models import Check_Report, load_check_metadata
|
|||||||
from prowler.lib.outputs.file_descriptors import fill_file_descriptors
|
from prowler.lib.outputs.file_descriptors import fill_file_descriptors
|
||||||
from prowler.lib.outputs.json import fill_json_asff
|
from prowler.lib.outputs.json import fill_json_asff
|
||||||
from prowler.lib.outputs.models import (
|
from prowler.lib.outputs.models import (
|
||||||
generate_csv_fields,
|
|
||||||
Check_Output_CSV,
|
Check_Output_CSV,
|
||||||
Check_Output_JSON_ASFF,
|
Check_Output_JSON_ASFF,
|
||||||
Compliance,
|
Compliance,
|
||||||
ProductFields,
|
ProductFields,
|
||||||
Resource,
|
Resource,
|
||||||
Severity,
|
Severity,
|
||||||
|
generate_csv_fields,
|
||||||
)
|
)
|
||||||
from prowler.lib.outputs.outputs import (
|
from prowler.lib.outputs.outputs import (
|
||||||
|
extract_findings_statistics,
|
||||||
send_to_s3_bucket,
|
send_to_s3_bucket,
|
||||||
set_report_color,
|
set_report_color,
|
||||||
)
|
)
|
||||||
@@ -328,3 +330,57 @@ class Test_Outputs:
|
|||||||
)["ContentType"]
|
)["ContentType"]
|
||||||
== "binary/octet-stream"
|
== "binary/octet-stream"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_extract_findings_statistics_different_resources(self):
|
||||||
|
finding_1 = mock.MagicMock()
|
||||||
|
finding_1.status = "PASS"
|
||||||
|
finding_1.resource_id = "test_resource_1"
|
||||||
|
finding_2 = mock.MagicMock()
|
||||||
|
finding_2.status = "FAIL"
|
||||||
|
finding_2.resource_id = "test_resource_2"
|
||||||
|
findings = [finding_1, finding_2]
|
||||||
|
|
||||||
|
stats = extract_findings_statistics(findings)
|
||||||
|
assert stats["total_pass"] == 1
|
||||||
|
assert stats["total_fail"] == 1
|
||||||
|
assert stats["resources_count"] == 2
|
||||||
|
assert stats["findings_count"] == 2
|
||||||
|
|
||||||
|
def test_extract_findings_statistics_same_resources(self):
|
||||||
|
finding_1 = mock.MagicMock()
|
||||||
|
finding_1.status = "PASS"
|
||||||
|
finding_1.resource_id = "test_resource_1"
|
||||||
|
finding_2 = mock.MagicMock()
|
||||||
|
finding_2.status = "PASS"
|
||||||
|
finding_2.resource_id = "test_resource_1"
|
||||||
|
findings = [finding_1, finding_2]
|
||||||
|
|
||||||
|
stats = extract_findings_statistics(findings)
|
||||||
|
assert stats["total_pass"] == 2
|
||||||
|
assert stats["total_fail"] == 0
|
||||||
|
assert stats["resources_count"] == 1
|
||||||
|
assert stats["findings_count"] == 2
|
||||||
|
|
||||||
|
def test_extract_findings_statistics_info_resources(self):
|
||||||
|
finding_1 = mock.MagicMock()
|
||||||
|
finding_1.status = "INFO"
|
||||||
|
finding_1.resource_id = "test_resource_1"
|
||||||
|
finding_2 = mock.MagicMock()
|
||||||
|
finding_2.status = "PASS"
|
||||||
|
finding_2.resource_id = "test_resource_1"
|
||||||
|
findings = [finding_1, finding_2]
|
||||||
|
|
||||||
|
stats = extract_findings_statistics(findings)
|
||||||
|
assert stats["total_pass"] == 1
|
||||||
|
assert stats["total_fail"] == 0
|
||||||
|
assert stats["resources_count"] == 1
|
||||||
|
assert stats["findings_count"] == 1
|
||||||
|
|
||||||
|
def test_extract_findings_statistics_no_findings(self):
|
||||||
|
findings = []
|
||||||
|
|
||||||
|
stats = extract_findings_statistics(findings)
|
||||||
|
assert stats["total_pass"] == 0
|
||||||
|
assert stats["total_fail"] == 0
|
||||||
|
assert stats["resources_count"] == 0
|
||||||
|
assert stats["findings_count"] == 0
|
||||||
|
|||||||
Reference in New Issue
Block a user