"""Checkov results filtering module.""" import os def remove_recursive_findings(checks, dirs): """Remove findings from nested directories. The filter removes findings that have source or caller files located in the directories other than in the specified list. Args: checks (list[dict]): List of checks. dirs (list[string]): List of allowed directories. Returns: list[dict]: Filtered list of checks. """ return list(filter( lambda check: _is_check_file_in_the_list(check, dirs), checks)) def prioritize_module_update(checks): """Prioritize custom policy that requires terraform module versions update. Custom policy ORCD_AWS_2 ensures in use of the latest terraform modules versions. All other checks, that are triggered on the same module or its resources will not be shown. Args: checks (list[dict]): List of checks. Returns: list[dict]: Filtered list of checks. """ prioritized_checks = list(filter( lambda check: check['check_id'] == 'ORCD_AWS_2', checks)) if not prioritized_checks: return checks prioritized_resources = list(map( lambda check: check['resource'], prioritized_checks)) valuable_checks = list(filter( lambda check: not _is_check_resource_in_the_list( check, prioritized_resources), checks)) # Filter out the broken CKV2_AWS_20 check, as its resource doesn't have # the full name with module prefix. valuable_checks = list(filter( lambda check: check['check_id'] != 'CKV2_AWS_20', valuable_checks)) return prioritized_checks + valuable_checks def _is_check_file_in_the_list(check, dirs): """Verify if the check's file is in the directory list. If the check's source file or caller file is in one of the directories specified in the list, the function will return True. If the source or caller file is inside the directory named `.terraform-modules-*` and this directory is nested to one of the directories in the list, the function will also return True. Args: check (dict): Failed check. dirs (list[string]): List of directories. Returns: bool: True if the check's file is in the directory list. """ check_dir = '' if check['file_path']: check_dir = os.path.dirname(check['file_path']) caller_dir = '' if check['caller_file_path']: caller_dir = os.path.dirname(check['caller_file_path']) for dir in dirs: if check_dir == dir or caller_dir == dir: return True # Avoid filtering out the broken CKV2_AWS_20 check, as it doesn't # have `caller_file_path`. if not caller_dir and check_dir.startswith( os.path.join(dir, '.terraform-modules')): return True return False def _is_check_resource_in_the_list(check, resources): """Verify if the check's resource is in the list. If the check's resource is in the resources list, or it is a child item of any of the resources in the list, then this function will return True. Args: check (dict): Failed check. resources (list[string]): Resources names. Returns: bool: True if the check's resource is in the list. """ for resource in resources: if (check['resource'] == resource or check['resource'].startswith(resource+'.')): return True return False