From 88455353ad4af404802c2aed3bff2d32da32cc93 Mon Sep 17 00:00:00 2001 From: Pepe Fagoaga Date: Thu, 30 Jan 2025 12:53:47 +0100 Subject: [PATCH] fix(kubernetes): Handle check_report values and add tests --- prowler/lib/check/models.py | 4 ++-- tests/lib/check/models_test.py | 43 ++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/prowler/lib/check/models.py b/prowler/lib/check/models.py index 77e0ae16a0..af6cdc1b1d 100644 --- a/prowler/lib/check/models.py +++ b/prowler/lib/check/models.py @@ -551,12 +551,12 @@ class Check_Report_Kubernetes(CheckReport): def __init__(self, metadata: Dict, resource: Any) -> None: super().__init__(metadata, resource) + self.resource_name = getattr(resource, "name") try: self.resource_id = getattr(resource, "uid") except AttributeError: - self.resource_id = getattr(resource, "name") + self.resource_id = self.resource_name - self.resource_name = getattr(resource, "name") self.namespace = getattr(resource, "namespace", "cluster-wide") diff --git a/tests/lib/check/models_test.py b/tests/lib/check/models_test.py index 4243fcec4f..671dad79ee 100644 --- a/tests/lib/check/models_test.py +++ b/tests/lib/check/models_test.py @@ -6,6 +6,7 @@ from prowler.lib.check.models import ( Check_Report_AWS, Check_Report_Azure, Check_Report_GCP, + Check_Report_Kubernetes, CheckMetadata, CheckReport, ) @@ -561,3 +562,45 @@ class TestCheckReportGCP: assert report.resource_name == "test_name" assert report.location == "test_region" assert report.project_id == "test_project" + + +class TestCheckReportKubernetes: + def test_check_report_kubernetes(self): + resource = mock.Mock + resource.uid = "test_uid" + resource.name = "test_name" + resource.namespace = "test_namespace" + report = Check_Report_Kubernetes( + metadata=mock_metadata.json(), resource=resource + ) + assert report.resource_id == "test_uid" + assert report.resource_name == "test_name" + assert report.namespace == "test_namespace" + + def test_check_report_kubernetes_no_name(self): + resource = mock.Mock + resource.uid = "test_uid" + resource.namespace = "test_namespace" + with pytest.raises(AttributeError): + Check_Report_Kubernetes(metadata=mock_metadata.json(), resource=resource) + + def test_check_report_kubernetes_no_uid(self): + resource = mock.Mock + resource.name = "test_name" + resource.namespace = "test_namespace" + report = Check_Report_Kubernetes( + metadata=mock_metadata.json(), resource=resource + ) + assert report.resource_id == "test_name" + assert report.resource_name == "test_name" + assert report.namespace == "test_namespace" + + def test_check_report_kubernetes_no_namespace(self): + resource = mock.Mock + resource.name = "test_name" + report = Check_Report_Kubernetes( + metadata=mock_metadata.json(), resource=resource + ) + assert report.resource_id == "test_name" + assert report.resource_name == "test_name" + assert report.namespace == "cluster-wide"