mirror of
https://github.com/prowler-cloud/prowler.git
synced 2026-10-09 21:14:22 +00:00
fix(inspector2): add more efficient way to check if any active findings (#4505)
This commit is contained in:
+7
-93
@@ -1,9 +1,6 @@
|
||||
from unittest import mock
|
||||
|
||||
from prowler.providers.aws.services.inspector2.inspector2_service import (
|
||||
Inspector,
|
||||
InspectorFinding,
|
||||
)
|
||||
from prowler.providers.aws.services.inspector2.inspector2_service import Inspector
|
||||
from tests.providers.aws.utils import (
|
||||
AWS_ACCOUNT_NUMBER,
|
||||
AWS_REGION_EU_WEST_1,
|
||||
@@ -32,7 +29,7 @@ class Test_inspector2_active_findings_exist:
|
||||
arn=f"arn:aws:inspector2:{AWS_REGION_EU_WEST_1}:{AWS_ACCOUNT_NUMBER}:inspector2",
|
||||
status="ENABLED",
|
||||
region=AWS_REGION_EU_WEST_1,
|
||||
findings=[],
|
||||
active_findings=False,
|
||||
)
|
||||
]
|
||||
aws_provider = set_mocked_aws_provider([AWS_REGION_EU_WEST_1])
|
||||
@@ -58,7 +55,7 @@ class Test_inspector2_active_findings_exist:
|
||||
assert result[0].status == "PASS"
|
||||
assert (
|
||||
result[0].status_extended
|
||||
== "Inspector2 is enabled with no findings."
|
||||
== "Inspector2 is enabled with no active findings."
|
||||
)
|
||||
assert result[0].resource_id == AWS_ACCOUNT_NUMBER
|
||||
assert (
|
||||
@@ -83,15 +80,7 @@ class Test_inspector2_active_findings_exist:
|
||||
arn=f"arn:aws:inspector2:{AWS_REGION_EU_WEST_1}:{AWS_ACCOUNT_NUMBER}:inspector2",
|
||||
region=AWS_REGION_EU_WEST_1,
|
||||
status="ENABLED",
|
||||
findings=[
|
||||
InspectorFinding(
|
||||
arn=FINDING_ARN,
|
||||
region=AWS_REGION_EU_WEST_1,
|
||||
severity="MEDIUM",
|
||||
status="NOT_ACTIVE",
|
||||
title="CVE-2022-40897 - setuptools",
|
||||
)
|
||||
],
|
||||
active_findings=False,
|
||||
)
|
||||
]
|
||||
aws_provider = set_mocked_aws_provider([AWS_REGION_EU_WEST_1])
|
||||
@@ -142,15 +131,7 @@ class Test_inspector2_active_findings_exist:
|
||||
arn=f"arn:aws:inspector2:{AWS_REGION_EU_WEST_1}:{AWS_ACCOUNT_NUMBER}:inspector2",
|
||||
region=AWS_REGION_EU_WEST_1,
|
||||
status="ENABLED",
|
||||
findings=[
|
||||
InspectorFinding(
|
||||
arn=FINDING_ARN,
|
||||
region=AWS_REGION_EU_WEST_1,
|
||||
severity="MEDIUM",
|
||||
status="ACTIVE",
|
||||
title="CVE-2022-40897 - setuptools",
|
||||
)
|
||||
],
|
||||
active_findings=True,
|
||||
)
|
||||
]
|
||||
aws_provider = set_mocked_aws_provider([AWS_REGION_EU_WEST_1])
|
||||
@@ -175,74 +156,7 @@ class Test_inspector2_active_findings_exist:
|
||||
assert len(result) == 1
|
||||
assert result[0].status == "FAIL"
|
||||
assert (
|
||||
result[0].status_extended
|
||||
== "There are 1 active Inspector2 findings."
|
||||
)
|
||||
assert result[0].resource_id == AWS_ACCOUNT_NUMBER
|
||||
assert (
|
||||
result[0].resource_arn
|
||||
== f"arn:aws:inspector2:{AWS_REGION_EU_WEST_1}:{AWS_ACCOUNT_NUMBER}:inspector2"
|
||||
)
|
||||
assert result[0].region == AWS_REGION_EU_WEST_1
|
||||
|
||||
def test_enabled_with_active_and_closed_findings(self):
|
||||
# Mock the inspector2 client
|
||||
inspector2_client = mock.MagicMock
|
||||
|
||||
inspector2_client.provider = set_mocked_aws_provider([AWS_REGION_EU_WEST_1])
|
||||
inspector2_client.audited_account = AWS_ACCOUNT_NUMBER
|
||||
inspector2_client.audited_account_arn = (
|
||||
f"arn:aws:iam::{AWS_ACCOUNT_NUMBER}:root"
|
||||
)
|
||||
inspector2_client.region = AWS_REGION_EU_WEST_1
|
||||
inspector2_client.inspectors = [
|
||||
Inspector(
|
||||
id=AWS_ACCOUNT_NUMBER,
|
||||
arn=f"arn:aws:inspector2:{AWS_REGION_EU_WEST_1}:{AWS_ACCOUNT_NUMBER}:inspector2",
|
||||
region=AWS_REGION_EU_WEST_1,
|
||||
status="ENABLED",
|
||||
findings=[
|
||||
InspectorFinding(
|
||||
arn=FINDING_ARN,
|
||||
region=AWS_REGION_EU_WEST_1,
|
||||
severity="MEDIUM",
|
||||
status="ACTIVE",
|
||||
title="CVE-2022-40897 - setuptools",
|
||||
),
|
||||
InspectorFinding(
|
||||
arn=FINDING_ARN,
|
||||
region=AWS_REGION_EU_WEST_1,
|
||||
severity="MEDIUM",
|
||||
status="CLOSED",
|
||||
title="CVE-2022-27404 - freetype",
|
||||
),
|
||||
],
|
||||
)
|
||||
]
|
||||
aws_provider = set_mocked_aws_provider([AWS_REGION_EU_WEST_1])
|
||||
|
||||
with mock.patch(
|
||||
"prowler.providers.common.provider.Provider.get_global_provider",
|
||||
return_value=aws_provider,
|
||||
):
|
||||
with mock.patch(
|
||||
"prowler.providers.aws.services.inspector2.inspector2_active_findings_exist.inspector2_active_findings_exist.inspector2_client",
|
||||
new=inspector2_client,
|
||||
):
|
||||
|
||||
# Test Check
|
||||
from prowler.providers.aws.services.inspector2.inspector2_active_findings_exist.inspector2_active_findings_exist import (
|
||||
inspector2_active_findings_exist,
|
||||
)
|
||||
|
||||
check = inspector2_active_findings_exist()
|
||||
result = check.execute()
|
||||
|
||||
assert len(result) == 1
|
||||
assert result[0].status == "FAIL"
|
||||
assert (
|
||||
result[0].status_extended
|
||||
== "There are 1 active Inspector2 findings."
|
||||
result[0].status_extended == "There are active Inspector2 findings."
|
||||
)
|
||||
assert result[0].resource_id == AWS_ACCOUNT_NUMBER
|
||||
assert (
|
||||
@@ -278,7 +192,7 @@ class Test_inspector2_active_findings_exist:
|
||||
arn=f"arn:aws:inspector2:{AWS_REGION_EU_WEST_1}:{AWS_ACCOUNT_NUMBER}:inspector2",
|
||||
status="DISABLED",
|
||||
region=AWS_REGION_EU_WEST_1,
|
||||
findings=[],
|
||||
active_findings=False,
|
||||
)
|
||||
]
|
||||
aws_provider = set_mocked_aws_provider([AWS_REGION_EU_WEST_1])
|
||||
|
||||
@@ -105,14 +105,7 @@ class Test_Inspector2_Service:
|
||||
assert inspector2.inspectors[0].region == AWS_REGION_EU_WEST_1
|
||||
assert inspector2.inspectors[0].status == "ENABLED"
|
||||
|
||||
def test__list_findings__(self):
|
||||
def test__list_active_findings__(self):
|
||||
aws_provider = set_mocked_aws_provider([AWS_REGION_EU_WEST_1])
|
||||
inspector2 = Inspector2(aws_provider)
|
||||
assert len(inspector2.inspectors[0].findings) == 1
|
||||
assert inspector2.inspectors[0].findings[0].arn == FINDING_ARN
|
||||
assert inspector2.inspectors[0].findings[0].region == AWS_REGION_EU_WEST_1
|
||||
assert inspector2.inspectors[0].findings[0].severity == "MEDIUM"
|
||||
assert inspector2.inspectors[0].findings[0].status == "ACTIVE"
|
||||
assert (
|
||||
inspector2.inspectors[0].findings[0].title == "CVE-2022-40897 - setuptools"
|
||||
)
|
||||
assert inspector2.inspectors[0].active_findings
|
||||
|
||||
Reference in New Issue
Block a user