Skip to content

Commit 171efa7

Browse files
OfekShimkooshimko
and
oshimko
authored
fix(kustomize): handle kustomize file with empty resources section (#7109)
* fix error when resources section is empty * add tests --------- Co-authored-by: oshimko <[email protected]>
1 parent 79e8fb8 commit 171efa7

File tree

4 files changed

+168
-5
lines changed

4 files changed

+168
-5
lines changed

checkov/kustomize/runner.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ def _parseKustomization(self, kustomize_dir: str) -> dict[str, Any]:
408408
if not isinstance(file_content, dict):
409409
return {}
410410

411-
if 'resources' in file_content:
411+
if 'resources' in file_content and file_content['resources'] is not None:
412412
resources = file_content['resources']
413413

414414
# We can differentiate between "overlays" and "bases" based on if the `resources` refers to a directory,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: internal-proxy-deployment
5+
labels:
6+
app: internal-proxy
7+
spec:
8+
selector:
9+
matchLabels:
10+
app: internal-proxy
11+
template:
12+
metadata:
13+
labels:
14+
app: internal-proxy
15+
spec:
16+
containers:
17+
- name: internal-api
18+
image: madhuakula/k8s-goat-internal-api
19+
resources:
20+
limits:
21+
cpu: 30m
22+
memory: 40Mi
23+
requests:
24+
cpu: 30m
25+
memory: 40Mi
26+
ports:
27+
- containerPort: 3000
28+
- name: info-app
29+
image: madhuakula/k8s-goat-info-app
30+
resources:
31+
limits:
32+
cpu: 30m
33+
memory: 40Mi
34+
requests:
35+
cpu: 30m
36+
memory: 40Mi
37+
ports:
38+
- containerPort: 5000
39+
---
40+
apiVersion: apps/v1
41+
kind: Deployment
42+
metadata:
43+
name: external-proxy-deployment
44+
labels:
45+
app: external-proxy
46+
spec:
47+
selector:
48+
matchLabels:
49+
app: external-proxy
50+
template:
51+
metadata:
52+
labels:
53+
app: external-proxy
54+
spec:
55+
containers:
56+
- name: internal-api
57+
image: madhuakula/k8s-goat-internal-api
58+
resources:
59+
limits:
60+
cpu: 30m
61+
memory: 40Mi
62+
requests:
63+
cpu: 30m
64+
memory: 40Mi
65+
ports:
66+
- containerPort: 3000
67+
- name: info-app
68+
image: madhuakula/k8s-goat-info-app
69+
resources:
70+
limits:
71+
cpu: 30m
72+
memory: 40Mi
73+
requests:
74+
cpu: 30m
75+
memory: 40Mi
76+
ports:
77+
- containerPort: 5000
78+
---
79+
apiVersion: networking.k8s.io/v1
80+
kind: NetworkPolicy
81+
metadata:
82+
name: test-network-policy
83+
namespace: default
84+
spec:
85+
podSelector:
86+
matchLabels:
87+
app: internal-proxy
88+
policyTypes:
89+
- Ingress
90+
- Egress
91+
ingress:
92+
- from:
93+
- ipBlock:
94+
cidr: 172.17.0.0/16
95+
except:
96+
- 172.17.1.0/24
97+
- podSelector:
98+
matchLabels:
99+
app: internal-proxy
100+
ports:
101+
- protocol: TCP
102+
port: 6379
103+
egress:
104+
- to:
105+
- ipBlock:
106+
cidr: 10.0.0.0/24
107+
ports:
108+
- protocol: TCP
109+
port: 5978
110+
---
111+
apiVersion: apps/v1
112+
kind: Deployment
113+
metadata:
114+
name: skipdeployment
115+
annotations:
116+
"checkov.io/skip": "CKV2_K8S_6=skip it"
117+
labels:
118+
app: skip
119+
spec:
120+
selector:
121+
matchLabels:
122+
app: skip
123+
template:
124+
metadata:
125+
labels:
126+
app: skip
127+
spec:
128+
containers:
129+
- name: info-app
130+
image: madhuakula/k8s-goat-info-app
131+
resources:
132+
limits:
133+
cpu: 30m
134+
memory: 40Mi
135+
requests:
136+
cpu: 30m
137+
memory: 40Mi
138+
ports:
139+
- containerPort: 5000
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
apiVersion: kustomize.config.k8s.io/v1beta1
2+
kind: Kustomization
3+
4+
resources:
5+
# - graph_check.yaml

tests/kustomize/graph/test_running_graph_checks.py

+23-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010
from tests.kustomize.utils import kustomize_exists
1111

1212

13-
@pytest.mark.skipif(not kustomize_exists(), reason="kustomize not installed")
14-
@pytest.mark.parametrize("graph_framework", GRAPH_FRAMEWORKS)
15-
def test_runner(mocker: MockerFixture, graph_framework):
16-
scan_dir_path = Path(__file__).parent / "resources" / "example_checks"
13+
def get_kustomize_summary(mocker: MockerFixture, graph_framework, scan_dir_path):
1714
dir_rel_path = os.path.realpath(scan_dir_path).replace('\\', '/')
1815

1916
runner_filter = RunnerFilter(framework=["kustomize"], checks=["CKV2_K8S_6"])
@@ -28,7 +25,29 @@ def test_runner(mocker: MockerFixture, graph_framework):
2825

2926
summary = report.get_summary()
3027

28+
return summary
29+
30+
31+
@pytest.mark.skipif(not kustomize_exists(), reason="kustomize not installed")
32+
@pytest.mark.parametrize("graph_framework", GRAPH_FRAMEWORKS)
33+
def test_runner(mocker: MockerFixture, graph_framework):
34+
scan_dir_path = Path(__file__).parent / "resources" / "example_checks"
35+
summary = get_kustomize_summary(mocker=mocker, graph_framework=graph_framework, scan_dir_path=scan_dir_path)
36+
3137
assert summary["passed"] == 1
3238
assert summary["failed"] == 1
3339
assert summary["skipped"] == 1
3440
assert summary["parsing_errors"] == 0
41+
42+
43+
@pytest.mark.skipif(not kustomize_exists(), reason="kustomize not installed")
44+
@pytest.mark.parametrize("graph_framework", GRAPH_FRAMEWORKS)
45+
def test_empty_resources(mocker: MockerFixture, graph_framework):
46+
scan_dir_path = Path(__file__).parent / "resources" / "empty_resources"
47+
48+
summary = get_kustomize_summary(mocker=mocker, graph_framework=graph_framework, scan_dir_path=scan_dir_path)
49+
50+
assert summary["passed"] == 0
51+
assert summary["failed"] == 0
52+
assert summary["skipped"] == 0
53+
assert summary["parsing_errors"] == 0

0 commit comments

Comments
 (0)