Skip to content

Commit bbd681a

Browse files
add kustomizegoat to integration tests (bridgecrewio#2561)
* add kustomizegoat to integration tests
1 parent b22e413 commit bbd681a

File tree

5 files changed

+28
-17
lines changed

5 files changed

+28
-17
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ jobs:
4848
run: git clone https://github.com/bridgecrewio/cfngoat
4949
- name: Clone Kubernetes-goat - vulnerable kubernetes
5050
run: git clone https://github.com/madhuakula/kubernetes-goat
51+
- name: Clone kustomize-goat - vulnerable kustomize
52+
run: git clone https://github.com/bridgecrewio/kustomizegoat
5153
- name: Create checkov reports
5254
run: |
5355
# Just making sure the API key tests don't run on PRs
@@ -102,6 +104,7 @@ jobs:
102104
pipenv --python 3.7
103105
pipenv install --dev
104106
- uses: imranismail/setup-kustomize@v1
107+
if: ${{ runner.os != 'windows' }}
105108
- name: Test with pytest
106109
env:
107110
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/pr-test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ jobs:
8686
run: git clone https://github.com/bridgecrewio/cfngoat
8787
- name: Clone Kubernetes-goat - vulnerable kubernetes
8888
run: git clone https://github.com/madhuakula/kubernetes-goat
89+
- name: Clone kustomize-goat - vulnerable kustomize
90+
run: git clone https://github.com/bridgecrewio/kustomizegoat
8991
- name: Create checkov reports
9092
env:
9193
LOG_LEVEL: INFO

checkov/kustomize/runner.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,28 @@ def mutateKubernetesResults(self, results, report, k8_file=None, k8_file_path=No
3939
else:
4040
kustomizeResourceID = "Unknown error. This is a bug."
4141

42+
code_lines = entity_context.get("code_lines")
43+
file_line_range = self.line_range(code_lines)
4244
record = Record(
4345
check_id=check.id, bc_check_id=check.bc_id, check_name=check.name,
44-
check_result=check_result, code_block=entity_context.get("code_lines"), file_path=realKustomizeEnvMetadata['filePath'],
45-
file_line_range=[0,0],
46+
check_result=check_result, code_block=code_lines, file_path=realKustomizeEnvMetadata['filePath'],
47+
file_line_range=file_line_range,
4648
resource=kustomizeResourceID, evaluations=variable_evaluations,
4749
check_class=check.__class__.__module__, file_abs_path=realKustomizeEnvMetadata['filePath'], severity=check.bc_severity)
4850
record.set_guideline(check.guideline)
4951
report.add_record(record=record)
5052

5153
return report
5254

55+
def line_range(self, code_lines):
56+
num_of_lines = len(code_lines)
57+
file_line_range = [0, 0]
58+
if num_of_lines > 0:
59+
first_line, code = code_lines[0]
60+
last_line, code = code_lines[num_of_lines - 1]
61+
file_line_range = [first_line, last_line]
62+
return file_line_range
63+
5364
def mutateKubernetesGraphResults(self, root_folder: str, runner_filter: RunnerFilter, report: Report, checks_results, reportMutatorData=None) -> Report:
5465
# Moves report generation logic out of run() method in Runner class.
5566
# Allows function overriding of a much smaller function than run() for other "child" frameworks such as Kustomize, Helm
@@ -73,14 +84,16 @@ def mutateKubernetesGraphResults(self, root_folder: str, runner_filter: RunnerFi
7384
kustomizeResourceID = f'{realKustomizeEnvMetadata["type"]}:{entity_id}'
7485
else:
7586
kustomizeResourceID = "Unknown error. This is a bug."
87+
code_lines = entity_context.get("code_lines")
88+
file_line_range = self.line_range(code_lines)
7689

7790
record = Record(
7891
check_id=check.id,
7992
check_name=check.name,
8093
check_result=check_result,
8194
code_block=entity_context.get("code_lines"),
8295
file_path=realKustomizeEnvMetadata['filePath'],
83-
file_line_range=[0,0],
96+
file_line_range=file_line_range,
8497
resource=kustomizeResourceID, # entity.get(CustomAttributes.ID),
8598
evaluations={},
8699
check_class=check.__class__.__module__,
@@ -408,17 +421,3 @@ def _curWriterValidateStoreMapAndClose(self, cur_writer, FilePath):
408421

409422
except IsADirectoryError:
410423
pass
411-
412-
def find_lines(node, kv):
413-
if isinstance(node, str):
414-
return node
415-
if isinstance(node, list):
416-
for i in node:
417-
for x in find_lines(i, kv):
418-
yield x
419-
elif isinstance(node, dict):
420-
if kv in node:
421-
yield node[kv]
422-
for j in node.values():
423-
for x in find_lines(j, kv):
424-
yield x

integration_tests/prepare_data.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ else
2222
pipenv run checkov -s -d cfngoat/ -o json --external-checks-dir ./checkov/cloudformation/checks/graph_checks/aws > checkov_report_cfngoat.json
2323
pipenv run checkov -s -d kubernetes-goat/ --framework kubernetes -o json > checkov_report_kubernetes-goat.json
2424
pipenv run checkov -s -d kubernetes-goat/ --framework helm -o json > checkov_report_kubernetes-goat-helm.json
25+
pipenv run checkov -s -d kustomizegoat/ --framework kustomize -o json > checkov_report_kustomizegoat.json
2526
pipenv run checkov -s --framework terraform --skip-check CKV_AWS_33,CKV_AWS_41 -d terragoat/terraform/ -o json > checkov_report_terragoat_with_skip.json
2627
pipenv run checkov -s -d cfngoat/ -o json --quiet > checkov_report_cfngoat_quiet.json
2728
pipenv run checkov -s -d terragoat/terraform/ --config-file integration_tests/example_config_files/config.yaml -o json > checkov_config_report_terragoat.json

integration_tests/test_checkov_json_report.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ def test_terragoat_report(self):
1313
report_path = os.path.join(os.path.dirname(current_dir), 'checkov_report_terragoat.json')
1414
self.validate_report(os.path.abspath(report_path))
1515

16+
def test_kustomizegoat_report(self):
17+
if not sys.platform.startswith('win'):
18+
report_path = os.path.join(os.path.dirname(current_dir), 'checkov_report_kustomizegoat.json')
19+
self.validate_report(os.path.abspath(report_path))
20+
1621
def test_cfngoat_report(self):
1722
report_path = os.path.join(os.path.dirname(current_dir), 'checkov_report_cfngoat.json')
1823
self.validate_report(os.path.abspath(report_path))
@@ -53,6 +58,7 @@ def validate_report_not_empty(self, report):
5358
f"expecting 0 parsing errors but got: {report['results']['parsing_errors']}")
5459
self.assertGreater(report["summary"]["failed"], 1,
5560
f"expecting more than 1 failed checks, got: {report['summary']['failed']}")
61+
self.assertGreater(report['results']['failed_checks'][0]['file_line_range'][1], 0)
5662

5763
def validate_json_quiet(self):
5864
report_path = os.path.join(os.path.dirname(current_dir), 'checkov_report_cfngoat_quiet.json')

0 commit comments

Comments
 (0)