Skip to content

Commit

Permalink
Added documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
bo156 committed Jul 10, 2023
1 parent e4bf7f7 commit b9c8999
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions checkov/kustomize/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def mutate_kubernetes_results(

def _get_caller_file_info(self, entity_context: _EntityContext, k8_file: str, k8_file_path: str, resource_id: str,
root_folder: str | None) -> tuple[tuple[int, int] | None, str | None]:
origin_relative_path = entity_context['origin_relative_path']
origin_relative_path = entity_context.get('origin_relative_path')
if origin_relative_path is None:
return None, None
k8s_file_dir = pathlib.Path(k8_file_path).parent
Expand All @@ -169,6 +169,14 @@ def _get_caller_file_info(self, entity_context: _EntityContext, k8_file: str, k8
@staticmethod
def _get_caller_file_path(k8s_file_dir: pathlib.Path, origin_relative_path: str, raw_file_path: pathlib.Path)\
-> str:
"""
Creates the correct file path based on the collection of metadata locations we have.
Example for expected input:
- k8s_fil_dir - Path('/resources/image_referencer/overlays/prod')
- origin_relative_path - '../../base/deployment.yaml'
- raw_file_path - Path('/resources/image_referencer/overlays/prod/../../base/deployment.yaml')
"""
amount_of_parents = str.count(origin_relative_path, '..')
directory_prefix_path = k8s_file_dir
if amount_of_parents:
Expand All @@ -178,25 +186,30 @@ def _get_caller_file_path(k8s_file_dir: pathlib.Path, origin_relative_path: str,
resolved_path = str(raw_file_path.resolve())
# Make sure the resolved path starts with the root folder, as pathlib.Path.resolve() might change it
if directory_prefix in resolved_path and not resolved_path.startswith(directory_prefix):
resolved_path_parts = resolved_path.split(directory_prefix)
if len(resolved_path_parts) > 1:
resolved_path = f'{directory_prefix}{"".join(resolved_path_parts[1:])}'
else:
resolved_path = f'{directory_prefix}{"".join(resolved_path_parts)}'
resolved_path = K8sKustomizeRunner._remove_extra_path_parts(resolved_path, directory_prefix)

return resolved_path[len(str(directory_prefix)):]

@staticmethod
def _remove_extra_path_parts(resolved_path: str, prefix: str):
"""
Some pathlib paths can "add" extra arguments at the beginning after running `Path.resolve()`.
For example, running `Path('/var/example.txt').resolve` might result in `/<not-existent-dir>/var/example.txt`.
The purpose of this function is to remove any unintentional additions like this one.
"""
resolved_path_parts = resolved_path.split(prefix)
if len(resolved_path_parts) > 1:
resolved_path = f'{prefix}{"".join(resolved_path_parts[1:])}'
else:
resolved_path = f'{prefix}{"".join(resolved_path_parts)}'
return resolved_path

def _get_caller_line_range(self, root_folder: str, k8_file: str, origin_relative_path: str,
resource_id: str) -> tuple[int, int] | None:
raw_caller_directory = (pathlib.Path(k8_file.lstrip(os.path.sep)).parent /
pathlib.Path(origin_relative_path.lstrip(os.path.sep)).parent)
caller_directory = str(pathlib.Path(f'{os.path.sep}{raw_caller_directory}').resolve())
splitted_dir = caller_directory.split(root_folder)
if len(splitted_dir) > 1:
# Removes any unnecessary additions by `Path.resolve`
caller_directory = root_folder + ''.join(splitted_dir[1:])
else:
caller_directory = root_folder.join(splitted_dir)
caller_directory = K8sKustomizeRunner._remove_extra_path_parts(caller_directory, root_folder)
file_ending = pathlib.Path(origin_relative_path).suffix
caller_file_path = f'{str(pathlib.Path(caller_directory) / resource_id.replace(".", "-"))}{file_ending}'

Expand Down

0 comments on commit b9c8999

Please sign in to comment.