-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
feat(general): Allow skipping multiple checks in a single line #6622
Closed
Closed
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
2ce5cef
added the possibility to allow multiple skips in a single line
shoshiGit cda4f42
Merge branch 'main' into multiple_skips
shoshiGit aebaea6
Update base_parser.py
esterKoren cfdcc5f
add enum
shoshiGit 43297bf
Merge remote-tracking branch 'origin/multiple_skips' into multiple_skips
shoshiGit a55fb47
Update enum.py
esterKoren 71e3c71
changed folder name
shoshiGit 2ed8228
Merge remote-tracking branch 'origin/multiple_skips' into multiple_skips
shoshiGit ea9da32
added the possibility to allow multiple skips in a single line
shoshiGit 5e9c9e5
Merge branch 'main' into multiple_skips
shoshiGit e1fe308
added the possibility to allow multiple skips in a single line
shoshiGit 898f628
Merge remote-tracking branch 'origin/multiple_skips' into multiple_skips
shoshiGit 2d30cb8
Merge branch 'main' into multiple_skips
shoshiGit 2ec21b9
Merge remote-tracking branch 'origin/multiple_skips' into multiple_skips
shoshiGit 6bed1fa
fixed the possibility to allow multiple skips in a single line
shoshiGit ba4d28c
fixed the possibility to allow multiple skips in a single line
shoshiGit cc4b0bb
Merge branch 'main' into multiple_skips
shoshiGit 614a82c
updated the possibility to allow multiple skips in a single line
shoshiGit 6fe5c1f
Merge remote-tracking branch 'origin/multiple_skips' into multiple_skips
shoshiGit ea8df14
updated strtobool
shoshiGit a34645f
updated strtobool 2
shoshiGit 1f28ea3
Merge branch 'main' into multiple_skips
esterKoren d91dfd5
Merge branch 'main' into multiple_skips
shoshiGit 7b20856
Resolved conflicts in enum.py
shoshiGit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
import re | ||
from typing import Pattern | ||
|
||
COMMENT_REGEX = re.compile(r'(checkov:skip=|bridgecrew:skip=) *([A-Za-z_\d]+)(:[^\n]+)?') | ||
# Default regex pattern | ||
COMMENT_REGEX = re.compile(r'(checkov:skip=|bridgecrew:skip=|cortex:skip=) *([A-Za-z_\d]+)(:[^\n]*)?') | ||
# Custom regex pattern if needed | ||
MULTIPLE_CHECKS_SKIP_REGEX = re.compile(r'(checkov:skip=|bridgecrew:skip=|cortex:skip=) *([A-Za-z_\d]+(?:,[A-Za-z_\d]+)*)?(:[^\n]*)?') | ||
|
||
def get_comment_regex(allow_multiple_skips: bool) -> Pattern[str]: | ||
""" | ||
Returns the appropriate regex pattern based on the environment variable. | ||
""" | ||
return MULTIPLE_CHECKS_SKIP_REGEX if allow_multiple_skips else COMMENT_REGEX |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
resource "azurerm_storage_account" "default" { | ||
#checkov:skip=CKV_AZURE_33 | ||
name = "storageaccountname" | ||
resource_group_name = "azurerm_resource_group.example.name" | ||
location = "azurerm_resource_group.example.location" | ||
account_tier = "Standard" | ||
account_replication_type = "GRS" | ||
} | ||
|
||
resource "azurerm_storage_account" "skip_more_than_one" { | ||
#checkov:skip=CKV_AZURE_33,CKV_AZURE_59: Skipped by user | ||
name = "storageaccountname" | ||
resource_group_name = "azurerm_resource_group.example.name" | ||
location = "azurerm_resource_group.example.location" | ||
account_tier = "Standard" | ||
account_replication_type = "GRS" | ||
} | ||
|
||
resource "azurerm_storage_account" "skip_invalid" { | ||
#checkov:skip=CKV_AZURE_33,bla bla bla: Skipped by user | ||
name = "storageaccountname" | ||
resource_group_name = "azurerm_resource_group.example.name" | ||
location = "azurerm_resource_group.example.location" | ||
account_tier = "Standard" | ||
account_replication_type = "GRS" | ||
} | ||
|
||
resource "azurerm_storage_account" "skip_all_checks" { | ||
#checkov:skip=CKV_AZURE_33,CKV_AZURE_59,CKV_AZURE_999,CKV_AZURE_190: Skipping multiple checks | ||
name = "storageaccountname" | ||
resource_group_name = "azurerm_resource_group.example.name" | ||
location = "azurerm_resource_group.example.location" | ||
account_tier = "Standard" | ||
account_replication_type = "GRS" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import os | ||
import unittest | ||
from pathlib import Path | ||
from unittest.mock import patch | ||
|
||
from checkov.runner_filter import RunnerFilter | ||
from checkov.terraform.runner import Runner | ||
|
||
|
||
class TestMultipleSkips(unittest.TestCase): | ||
@patch.dict(os.environ, {'CHECKOV_ALLOW_SKIP_MULTIPLE_ONE_LINE': 'True'}) | ||
def test(self) -> None: | ||
# given | ||
test_files_dir = Path(__file__).parent / "a_example_skip" | ||
|
||
# when | ||
report = Runner().run(root_folder=str(test_files_dir), runner_filter=RunnerFilter(checks=[])) | ||
|
||
# then | ||
summary = report.get_summary() | ||
|
||
skipped_resources = { | ||
"azurerm_storage_account.default": 1, | ||
"azurerm_storage_account.skip_invalid": 1, | ||
"azurerm_storage_account.skip_more_than_one": 2, | ||
"azurerm_storage_account.skip_all_checks": 3, | ||
} | ||
|
||
for skipped_check in report.skipped_checks: | ||
resource = skipped_check.resource # Access resource attribute directly | ||
if resource in skipped_resources: | ||
skipped_resources[resource] -= 1 | ||
|
||
for resource, count in skipped_resources.items(): | ||
self.assertEqual(count, 0, f"{resource} did not skip the expected number of checks") | ||
|
||
self.assertEqual(summary["passed"], 16) | ||
self.assertEqual(summary["failed"], 33) | ||
self.assertEqual(summary["skipped"], 7) | ||
self.assertEqual(summary["parsing_errors"], 0) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please assert the skipped part in the reports by resource -
We want to be sure
default
andskip_invalid
are with one skip, andskip_more_than_one
is with 2 skips.