Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(general): skip scanning VCS configuration if only files are passed in #3729

Merged
merged 1 commit into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions checkov/bitbucket/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ def run(

self.prepare_data()

report = super().run(root_folder=self.bitbucket.bitbucket_conf_dir_path, external_checks_dir=external_checks_dir,
files=files,
runner_filter=runner_filter, collect_skip_comments=collect_skip_comments)
# JsonRunner._change_files_path_to_relative(report)
report = super().run(
root_folder=self.bitbucket.bitbucket_conf_dir_path,
external_checks_dir=external_checks_dir,
files=None, # ignore file scans
runner_filter=runner_filter,
collect_skip_comments=collect_skip_comments,
)

return report

Expand Down
10 changes: 7 additions & 3 deletions checkov/github/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ def run(

self.prepare_data()

report = super().run(root_folder=self.github.github_conf_dir_path, external_checks_dir=external_checks_dir,
files=files,
runner_filter=runner_filter, collect_skip_comments=collect_skip_comments)
report = super().run(
root_folder=self.github.github_conf_dir_path,
external_checks_dir=external_checks_dir,
files=None, # ignore file scans
runner_filter=runner_filter,
collect_skip_comments=collect_skip_comments,
)
JsonRunner._change_files_path_to_relative(report) # type:ignore[arg-type] # report can only be of type Report, not a list
return report

Expand Down
2 changes: 1 addition & 1 deletion checkov/gitlab/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def run(
report = super().run(
root_folder=self.gitlab.gitlab_conf_dir_path,
external_checks_dir=external_checks_dir,
files=files,
files=None, # ignore file scans
runner_filter=runner_filter,
collect_skip_comments=collect_skip_comments,
)
Expand Down
20 changes: 20 additions & 0 deletions tests/bitbucket/test_runner.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import unittest
from pathlib import Path
from unittest import mock

from checkov.bitbucket.runner import Runner
Expand Down Expand Up @@ -70,6 +71,25 @@ def test_runner_object_passing_check(self):
self.assertEqual(len(report.passed_checks), 1)
self.assertEqual(report.skipped_checks, [])

@mock.patch.dict(os.environ, {"CKV_BITBUCKET_CONFIG_FETCH_DATA": "False", "PYCHARM_HOSTED": "1"}, clear=True)
def test_runner_files_ignore(self):
# given
test_file = Path(__file__).parent / "resources/bitbucket_conf/pass/branch_restrictions.json"
checks = ["CKV_BITBUCKET_1"]

# when
report = Runner().run(
files=[str(test_file)],
runner_filter=RunnerFilter(checks=checks)
)

# then
# even it points to a file with scannable content, it should skip it
self.assertEqual(len(report.passed_checks), 0)
self.assertEqual(len(report.failed_checks), 0)
self.assertEqual(len(report.parsing_errors), 0)
self.assertEqual(len(report.skipped_checks), 0)


if __name__ == "__main__":
unittest.main()
20 changes: 20 additions & 0 deletions tests/github/test_runner.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import unittest
from pathlib import Path
from unittest import mock

from checkov.common.bridgecrew.check_type import CheckType
Expand Down Expand Up @@ -140,6 +141,25 @@ def test_runner_object_passing_check(self):
self.assertEqual(len(report.passed_checks), 3)
self.assertEqual(report.skipped_checks, [])

@mock.patch.dict(os.environ, {"CKV_GITHUB_CONFIG_FETCH_DATA": "False", "PYCHARM_HOSTED": "1"}, clear=True)
def test_runner_files_ignore(self):
# given
test_file = Path(__file__).parent / "resources/github_conf/pass/org_security.json"
checks = ["CKV_GITHUB_1", "CKV_GITHUB_2", "CKV_GITHUB_3"]

# when
report = Runner().run(
files=[str(test_file)],
runner_filter=RunnerFilter(checks=checks)
)

# then
# even it points to a file with scannable content, it should skip it
self.assertEqual(len(report.passed_checks), 0)
self.assertEqual(len(report.failed_checks), 0)
self.assertEqual(len(report.parsing_errors), 0)
self.assertEqual(len(report.skipped_checks), 0)


if __name__ == "__main__":
unittest.main()
20 changes: 20 additions & 0 deletions tests/gitlab/test_runner.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import unittest
from pathlib import Path
from unittest import mock

from checkov.common.bridgecrew.check_type import CheckType
Expand Down Expand Up @@ -63,6 +64,25 @@ def test_runner_object_passing_check(self):
self.assertEqual(len(report.passed_checks), 2)
self.assertEqual(report.skipped_checks, [])

@mock.patch.dict(os.environ, {"CKV_GITLAB_CONFIG_FETCH_DATA": "False", "PYCHARM_HOSTED": "1"}, clear=True)
def test_runner_files_ignore(self):
# given
test_file = Path(__file__).parent / "resources/gitlab_conf/pass/merge_request_approval_conf.json"
checks = ["CKV_GITLAB_1", "CKV_GITLAB_2"]

# when
report = Runner().run(
files=[str(test_file)],
runner_filter=RunnerFilter(checks=checks)
)

# then
# even it points to a file with scannable content, it should skip it
self.assertEqual(len(report.passed_checks), 0)
self.assertEqual(len(report.failed_checks), 0)
self.assertEqual(len(report.parsing_errors), 0)
self.assertEqual(len(report.skipped_checks), 0)

def test_registry_has_type(self):
self.assertEqual(registry.report_type, CheckType.GITLAB_CONFIGURATION)

Expand Down