Skip to content

Commit

Permalink
fix(gcp): Handle values in check_report and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jfagoagas committed Jan 30, 2025
1 parent 4460bc2 commit d7e24fc
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 12 deletions.
30 changes: 18 additions & 12 deletions prowler/lib/check/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -519,19 +519,25 @@ def __init__(
) -> 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
Expand Down
88 changes: 88 additions & 0 deletions tests/lib/check/models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from prowler.lib.check.models import (
Check_Report_AWS,
Check_Report_Azure,
Check_Report_GCP,
CheckMetadata,
CheckReport,
)
Expand Down Expand Up @@ -473,3 +474,90 @@ def test_check_report_azure_no_location(self):
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"

0 comments on commit d7e24fc

Please sign in to comment.