Skip to content

Commit

Permalink
Linters
Browse files Browse the repository at this point in the history
  • Loading branch information
bo156 committed Jul 6, 2023
1 parent fc51eda commit 10e9411
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions checkov/kustomize/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from checkov.common.util.data_structures_utils import pickle_deepcopy
from checkov.common.util.env_vars_config import env_vars_config
from checkov.kubernetes.kubernetes_utils import create_check_result, get_resource_id, calculate_code_lines
from checkov.kubernetes.parser import k8_yaml
from checkov.kubernetes.runner import Runner as K8sRunner
from checkov.kubernetes.runner import _get_entity_abs_path
from checkov.kustomize.image_referencer.manager import KustomizeImageReferencerManager
Expand Down Expand Up @@ -129,8 +128,8 @@ def mutate_kubernetes_results(

if env_vars_config.ALLOW_KUSTOMIZE_FILE_EDITS:
caller_file_line_range, caller_file_path = self._get_caller_file_info(entity_context, k8_file,
k8_file_path, resource_id,
root_folder)
k8_file_path, resource_id,
root_folder)

if realKustomizeEnvMetadata['filePath'].startswith(repo_dir):
file_path = realKustomizeEnvMetadata['filePath'][len(repo_dir):]
Expand All @@ -151,11 +150,13 @@ def mutate_kubernetes_results(
return report

def _get_caller_file_info(self, entity_context: dict[str, Any], k8_file: str, k8_file_path: str, resource_id: str,
root_folder: str) -> tuple[tuple[int, int], str]:
root_folder: str | None) -> tuple[tuple[int, int] | None, str]:
origin_relative_path = entity_context['origin_relative_path']
k8s_file_dir = pathlib.Path(k8_file_path).parent
raw_file_path = k8s_file_dir / origin_relative_path
caller_file_path = self._get_caller_file_path(k8s_file_dir, origin_relative_path, raw_file_path, root_folder)
caller_file_path = self._get_caller_file_path(k8s_file_dir, origin_relative_path, raw_file_path)
if root_folder is None:
return None, caller_file_path
caller_file_line_range = self._get_caller_line_range(root_folder, k8_file, origin_relative_path,
resource_id)
return caller_file_line_range, caller_file_path
Expand All @@ -164,11 +165,11 @@ def _get_caller_file_info(self, entity_context: dict[str, Any], k8_file: str, k8
def _get_caller_file_path(k8s_file_dir: pathlib.Path, origin_relative_path: str, raw_file_path: pathlib.Path)\
-> str:
amount_of_parents = str.count(origin_relative_path, '..')
directory_prefix = k8s_file_dir
directory_prefix_path = k8s_file_dir
for i in range(amount_of_parents):
directory_prefix = directory_prefix.parent
directory_prefix_path = directory_prefix_path.parent

directory_prefix = str(directory_prefix)
directory_prefix = str(directory_prefix_path)
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):
Expand Down

0 comments on commit 10e9411

Please sign in to comment.