diff --git a/prowler/lib/check/models.py b/prowler/lib/check/models.py index d4db7e0a4e..77e0ae16a0 100644 --- a/prowler/lib/check/models.py +++ b/prowler/lib/check/models.py @@ -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 diff --git a/tests/lib/check/models_test.py b/tests/lib/check/models_test.py index bfba438b08..4243fcec4f 100644 --- a/tests/lib/check/models_test.py +++ b/tests/lib/check/models_test.py @@ -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"