fix(gcp): Handle values in check_report and add tests

This commit is contained in:
Pepe Fagoaga
2025-01-30 12:49:19 +01:00
parent 4460bc2e82
commit d7e24fce30
2 changed files with 106 additions and 12 deletions
+18 -12
View File
@@ -475,8 +475,8 @@ class Check_Report_Azure(CheckReport):
resource_name: str
resource_id: str
subscription: str = None
location: str
subscription: str = None
def __init__(self, metadata: Dict, resource: Any) -> None:
"""Initialize the Azure Check's finding information.
@@ -519,19 +519,25 @@ class Check_Report_GCP(CheckReport):
) -> None:
super().__init__(metadata, resource)
try:
self.resource_id = getattr(resource, "id")
except AttributeError:
self.resource_id = getattr(resource, "name")
self.resource_id = resource_id or getattr(
resource, "id", getattr(resource, "name")
)
self.resource_name = resource_name or getattr(resource, "name")
if resource_id:
self.resource_id = resource_id
else:
try:
self.resource_id = getattr(resource, "id")
except AttributeError:
self.resource_id = self.resource_name
self.project_id = project_id or getattr(resource, "project_id")
self.location = location or getattr(
resource, "location", getattr(resource, "region")
)
if location:
self.location = location
else:
try:
self.location = getattr(resource, "location")
except AttributeError:
self.location = getattr(resource, "region")
@dataclass
+88
View File
@@ -5,6 +5,7 @@ import pytest
from prowler.lib.check.models import (
Check_Report_AWS,
Check_Report_Azure,
Check_Report_GCP,
CheckMetadata,
CheckReport,
)
@@ -473,3 +474,90 @@ class TestCheckReportAzure:
assert report.resource_name == "test_name"
assert report.location == "global"
assert report.subscription is None
class TestCheckReportGCP:
def test_check_report_gcp(self):
resource = mock.Mock
resource.id = "test_id"
resource.name = "test_name"
resource.project_id = "test_project"
resource.location = "test_location"
report = Check_Report_GCP(metadata=mock_metadata.json(), resource=resource)
assert report.resource_id == "test_id"
assert report.resource_name == "test_name"
assert report.location == "test_location"
assert report.project_id == "test_project"
def test_check_report_gcp_resource_id(self):
resource = mock.Mock
resource.id = "test_id"
resource.name = "test_name"
resource.project_id = "test_project"
resource.location = "test_location"
report = Check_Report_GCP(
metadata=mock_metadata.json(), resource=resource, resource_id="resource_1"
)
assert report.resource_id == "resource_1"
assert report.resource_name == "test_name"
assert report.location == "test_location"
assert report.project_id == "test_project"
def test_check_report_gcp_no_resource_id(self):
resource = mock.Mock
resource.name = "test_name"
resource.project_id = "test_project"
resource.location = "test_location"
report = Check_Report_GCP(
metadata=mock_metadata.json(),
resource=resource,
)
assert report.resource_id == "test_name"
assert report.resource_name == "test_name"
assert report.location == "test_location"
assert report.project_id == "test_project"
def test_check_report_gcp_resource_name(self):
resource = mock.Mock
resource.id = "test_id"
resource.name = "test_name"
resource.project_id = "test_project"
resource.location = "test_location"
report = Check_Report_GCP(
metadata=mock_metadata.json(), resource=resource, resource_name="resource_1"
)
assert report.resource_id == "test_id"
assert report.resource_name == "resource_1"
assert report.location == "test_location"
assert report.project_id == "test_project"
def test_check_report_gcp_no_project_id(self):
resource = mock.Mock
resource.id = "test_id"
resource.name = "test_name"
resource.location = "test_location"
with pytest.raises(AttributeError):
Check_Report_GCP(metadata=mock_metadata.json(), resource=resource)
def test_check_report_gcp_no_location(self):
resource = mock.Mock
resource.id = "test_id"
resource.name = "test_name"
resource.project_id = "test_project"
with pytest.raises(AttributeError):
Check_Report_GCP(metadata=mock_metadata.json(), resource=resource)
def test_check_report_gcp_region(self):
resource = mock.Mock
resource.id = "test_id"
resource.name = "test_name"
resource.region = "test_region"
resource.project_id = "test_project"
report = Check_Report_GCP(
metadata=mock_metadata.json(),
resource=resource,
)
assert report.resource_id == "test_id"
assert report.resource_name == "test_name"
assert report.location == "test_region"
assert report.project_id == "test_project"