-
-
Notifications
You must be signed in to change notification settings - Fork 546
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: Make checkov more flexible #329
Closed
Miscreancy
wants to merge
1
commit into
antonbabenko:master
from
Miscreancy:add-additional-functionality-for-checkov
Closed
Changes from all commits
Commits
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
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,123 @@ | ||
#!/usr/bin/env bash | ||
set -eo pipefail | ||
|
||
####################################################################### | ||
# Wrapper function for hook. Determines run mode based on bool set by | ||
# argparse, and triggers that run mode. | ||
# Globals: | ||
# None | ||
# Arguments: | ||
# None | ||
# Returns: | ||
# 0 if successful, non-zero on error | ||
####################################################################### | ||
function main { | ||
parse_arguments "$@" | ||
# shellcheck disable=SC2153 # False positive | ||
if [[ ${CHANGE_DIRECTORY_SCAN} == true ]]; then | ||
# directories containing changed .tf files only | ||
directory_runner | ||
elif [[ ${CHANGE_FILE_SCAN} == true ]]; then | ||
# changed .tf files only | ||
file_runner | ||
else | ||
# whole repository | ||
checkov "${ARGS[@]}" -d . | ||
fi | ||
} | ||
|
||
####################################################################### | ||
# Parses arguments provided to the hook to filter out changed files and | ||
# args for the hook, set run mode (if present) and remove run mode toggles | ||
# from the args as they're not valid for checkov. Enables backwards | ||
# compatibility for those using checkov with no args, or without --args | ||
# Globals: | ||
# None | ||
# Arguments: | ||
# None | ||
# Returns: | ||
# 0 if successful, non-zero on error | ||
# Creates global arrays ARGS, FILES, may create a run mode global | ||
####################################################################### | ||
function parse_arguments { | ||
declare -a -g ARGS=() | ||
declare -a -g FILES=() | ||
|
||
argv=("$@") | ||
pattern='^.*\.tf$' | ||
|
||
for item in "${argv[@]}"; do | ||
if [[ $item =~ $pattern ]]; then | ||
FILES+=("$item") | ||
else | ||
case $item in | ||
--scan-change-directories) | ||
CHANGE_DIRECTORY_SCAN=true | ||
;; | ||
--scan-change-files) | ||
CHANGE_FILE_SCAN=true | ||
;; | ||
# -f filtered out to avoid duplicating | ||
-f | --file) | ||
: | ||
;; | ||
*) | ||
ARGS+=("$item") | ||
;; | ||
esac | ||
fi | ||
done | ||
} | ||
|
||
####################################################################### | ||
# Identifies directories containing files that have changed in them, builds | ||
# a command string from them, and then runs checkov against those dirs | ||
# with args provided to hook. | ||
# Globals: | ||
# FILES | ||
# ARGS | ||
# Arguments: | ||
# None | ||
# Returns: | ||
# 0 if successful, non-zero on error | ||
####################################################################### | ||
function directory_runner { | ||
declare -a directories=() | ||
declare -a directory_command=() | ||
|
||
for file_with_path in "${FILES[@]}"; do | ||
directory=$(dirname "$file_with_path") | ||
directories+=("$directory") | ||
done | ||
|
||
for dir in $(echo "${directories[*]}" | tr ' ' '\n' | sort -u); do | ||
directory_command+=("-d") | ||
directory_command+=("$dir") | ||
done | ||
|
||
checkov "${ARGS[@]}" "${directory_command[@]}" | ||
} | ||
|
||
####################################################################### | ||
# Builds a command string from the files provided, and runs checkov against | ||
# those files with args provided to hook. This is the fastest run mode. | ||
# Globals: | ||
# FILES | ||
# ARGS | ||
# Arguments: | ||
# None | ||
# Returns: | ||
# 0 if successful, non-zero on error | ||
####################################################################### | ||
function file_runner { | ||
declare -a file_command=() | ||
|
||
for file in "${FILES[@]}"; do | ||
file_command+=("-f") | ||
file_command+=("$file") | ||
done | ||
|
||
checkov "${ARGS[@]}" "${file_command[@]}" | ||
} | ||
|
||
[ "${BASH_SOURCE[0]}" != "$0" ] || 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.
The any_chars_from_beginning part of pattern makes no much sense and can be dropped