From 258216d6512cb40a7287a58d71dfb4908595d7fe Mon Sep 17 00:00:00 2001 From: angelolmn Date: Tue, 9 Sep 2025 13:58:02 +0200 Subject: [PATCH] feat: Verify that the CheckID is the same as the filename and classname in the Check class --- prowler/lib/check/models.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/prowler/lib/check/models.py b/prowler/lib/check/models.py index 02813edc76..6d3df77658 100644 --- a/prowler/lib/check/models.py +++ b/prowler/lib/check/models.py @@ -436,17 +436,31 @@ class Check(ABC, CheckMetadata): def __init__(self, **data): """Check's init function. Calls the CheckMetadataModel init.""" + file_path = os.path.abspath(sys.modules[self.__module__].__file__)[:-3] + # Parse the Check's metadata file - metadata_file = ( - os.path.abspath(sys.modules[self.__module__].__file__)[:-3] - + ".metadata.json" - ) + metadata_file = file_path + ".metadata.json" # Store it to validate them with Pydantic data = CheckMetadata.parse_file(metadata_file).dict() # Calls parents init function super().__init__(**data) - # TODO: verify that the CheckID is the same as the filename and classname - # to mimic the test done at test__checks_metadata_is_valid + + # Verify names consistency + check_id = self.CheckID + class_name = self.__class__.__name__ + file_name = file_path.split(sep="/")[-1] + + errors = [] + if check_id != class_name: + errors.append(f"CheckID '{check_id}' != class name '{class_name}'") + if check_id != file_name: + errors.append(f"CheckID '{check_id}' != file name '{file_name}'") + + if errors: + logger.error( + "Name consistency error. The CheckID in the metadata, the class name " + "and the file name must all match. -- " + " | ".join(errors) + ) def metadata(self) -> dict: """Return the JSON representation of the check's metadata"""