Repository: bridgecrewio/checkov

Issue:
kustomize fails on empty kustomization.yaml file without meaningful message
After applying `checkov -d . --framework kustomize` onto existing project I got generic error.
```checkov -d . --framework kustomize 
2023-05-23 14:08:01,563 [MainThread  ] [ERROR]  Exception traceback:
Traceback (most recent call last):
  File "/home/marek/.local/lib/python3.10/site-packages/checkov/main.py", line 463, in run
    self.scan_reports = runner_registry.run(
  File "/home/marek/.local/lib/python3.10/site-packages/checkov/common/runners/runner_registry.py", line 102, in run
    self.runners[0].run(root_folder, external_checks_dir=external_checks_dir, files=files,
  File "/home/marek/.local/lib/python3.10/site-packages/checkov/kustomize/runner.py", line 557, in run
    self.run_kustomize_to_k8s(root_folder, files, runner_filter)
  File "/home/marek/.local/lib/python3.10/site-packages/checkov/kustomize/runner.py", line 498, in run_kustomize_to_k8s
    self.kustomizeProcessedFolderAndMeta[kustomize_dir] = self._parseKustomization(kustomize_dir)
  File "/home/marek/.local/lib/python3.10/site-packages/checkov/kustomize/runner.py", line 271, in _parseKustomization
    if 'resources' in fileContent:
TypeError: argument of type 'NoneType' is not iterable
Traceback (most recent call last):
  File "/home/marek/.local/bin/checkov", line 9, in <module>
    sys.exit(Checkov().run())
  File "/home/marek/.local/lib/python3.10/site-packages/checkov/main.py", line 463, in run
    self.scan_reports = runner_registry.run(
  File "/home/marek/.local/lib/python3.10/site-packages/checkov/common/runners/runner_registry.py", line 102, in run
    self.runners[0].run(root_folder, external_checks_dir=external_checks_dir, files=files,
  File "/home/marek/.local/lib/python3.10/site-packages/checkov/kustomize/runner.py", line 557, in run
    self.run_kustomize_to_k8s(root_folder, files, runner_filter)
  File "/home/marek/.local/lib/python3.10/site-packages/checkov/kustomize/runner.py", line 498, in run_kustomize_to_k8s
    self.kustomizeProcessedFolderAndMeta[kustomize_dir] = self._parseKustomization(kustomize_dir)
  File "/home/marek/.local/lib/python3.10/site-packages/checkov/kustomize/runner.py", line 271, in _parseKustomization
    if 'resources' in fileContent:
TypeError: argument of type 'NoneType' is not iterable
```

After some digging inside _parseKustomization at kustomize/runner.py:247 and applying try block loop body I found out file that was causing me the issue. I had following folders in kubernetess directory: base, prod,stage,dev. But dev contained totally empty kustomization.yaml file. Filling this file with yaml content solves the issue.

However maybe its worth to add some checks on `fileContent` object before we attempt to examine it for its contents.

```
checkov -v
2.3.234
```


Discussion:
Alternatively, this could be turned into a failing policy saying that an empty kustomization file is confusing/undefined behavior/something else and should be avoided. Either case, it shouldn't crash the scanner.

Resolving patch:
diff --git a/checkov/kustomize/runner.py b/checkov/kustomize/runner.py
--- a/checkov/kustomize/runner.py
+++ b/checkov/kustomize/runner.py
@@ -246,7 +246,7 @@ def get_kustomize_metadata(self) -> dict[str, dict[str, Any]]:
         return {'kustomizeMetadata': self.kustomizeProcessedFolderAndMeta,
                 'kustomizeFileMappings': self.kustomizeFileMappings}
 
-    def _parseKustomization(self, kustomize_dir: str) -> dict[str, str]:
+    def _parseKustomization(self, kustomize_dir: str) -> dict[str, Any]:
         # We may have multiple results for "kustomization.yaml" files. These could be:
         # - Base and Environment (overlay) DIR's for the same kustomize-powered deployment
         # - OR, Multiple different Kustomize-powered deployments
@@ -263,29 +263,33 @@ def _parseKustomization(self, kustomize_dir: str) -> dict[str, str]:
         else:
             return {}
 
-        with open(kustomization_path, 'r') as kustomizationFile:
-            metadata = {}
+        with open(kustomization_path, 'r') as kustomization_file:
+            metadata: dict[str, Any] = {}
             try:
-                fileContent = yaml.safe_load(kustomizationFile)
+                file_content = yaml.safe_load(kustomization_file)
             except yaml.YAMLError:
                 logging.info(f"Failed to load Kustomize metadata from {kustomization_path}.", exc_info=True)
+                return {}
 
-            if 'resources' in fileContent:
+            if not isinstance(file_content, dict):
+                return {}
+
+            if 'resources' in file_content:
                 logging.debug(f"Kustomization contains resources: section. Likley a base. {kustomization_path}")
                 metadata['type'] = "base"
 
-            elif 'patchesStrategicMerge' in fileContent:
+            elif 'patchesStrategicMerge' in file_content:
                 logging.debug(f"Kustomization contains patchesStrategicMerge: section. Likley an overlay/env. {kustomization_path}")
                 metadata['type'] = "overlay"
-                if 'bases' in fileContent:
-                    metadata['referenced_bases'] = fileContent['bases']
+                if 'bases' in file_content:
+                    metadata['referenced_bases'] = file_content['bases']
 
-            elif 'bases' in fileContent:
+            elif 'bases' in file_content:
                 logging.debug(f"Kustomization contains bases: section. Likley an overlay/env. {kustomization_path}")
                 metadata['type'] = "overlay"
-                metadata['referenced_bases'] = fileContent['bases']
+                metadata['referenced_bases'] = file_content['bases']
 
-            metadata['fileContent'] = fileContent
+            metadata['fileContent'] = file_content
             metadata['filePath'] = f"{kustomization_path}"
             if metadata.get('type') == "base":
                 self.potentialBases.append(metadata['filePath'])


Test patch:
diff --git a/tests/kustomize/runner/resources/example/overlays/empty/kustomization.yaml b/tests/kustomize/runner/resources/example/overlays/empty/kustomization.yaml
new file mode 100644
--- /dev/null
+++ b/tests/kustomize/runner/resources/example/overlays/empty/kustomization.yaml
@@ -0,0 +1 @@
+# just an empty file, shouldn't crash the run