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: Check all directories with changes and pass all args in terrascan hook #305

Merged
merged 15 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,4 @@
description: Runs terrascan on Terraform templates.
language: script
entry: terrascan.sh
files: \.tf$
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
58 changes: 38 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,28 @@ If you are using `pre-commit-terraform` already or want to support its developme

## Table of content

* [Sponsors](#sponsors)
* [Table of content](#table-of-content)
* [How to install](#how-to-install)
* [1. Install dependencies](#1-install-dependencies)
* [2. Install the pre-commit hook globally](#2-install-the-pre-commit-hook-globally)
* [3. Add configs and hooks](#3-add-configs-and-hooks)
* [4. Run](#4-run)
* [Available Hooks](#available-hooks)
* [Hooks usage notes and examples](#hooks-usage-notes-and-examples)
* [checkov](#checkov)
* [infracost_breakdown](#infracost_breakdown)
* [terraform_docs](#terraform_docs)
* [terraform_docs_replace (deprecated)](#terraform_docs_replace-deprecated)
* [terraform_fmt](#terraform_fmt)
* [terraform_providers_lock](#terraform_providers_lock)
* [terraform_tflint](#terraform_tflint)
* [terraform_tfsec](#terraform_tfsec)
* [terraform_validate](#terraform_validate)
* [Authors](#authors)
* [License](#license)
- [Collection of git hooks for Terraform to be used with pre-commit framework](#collection-of-git-hooks-for-terraform-to-be-used-with-pre-commit-framework)
- [Sponsors](#sponsors)
- [Table of content](#table-of-content)
- [How to install](#how-to-install)
- [1. Install dependencies](#1-install-dependencies)
- [2. Install the pre-commit hook globally](#2-install-the-pre-commit-hook-globally)
- [3. Add configs and hooks](#3-add-configs-and-hooks)
- [4. Run](#4-run)
- [Available Hooks](#available-hooks)
- [Hooks usage notes and examples](#hooks-usage-notes-and-examples)
- [checkov](#checkov)
- [infracost_breakdown](#infracost_breakdown)
- [terraform_docs](#terraform_docs)
- [terraform_docs_replace (deprecated)](#terraform_docs_replace-deprecated)
- [terraform_fmt](#terraform_fmt)
- [terraform_providers_lock](#terraform_providers_lock)
- [terraform_tflint](#terraform_tflint)
- [terraform_tfsec](#terraform_tfsec)
- [terraform_validate](#terraform_validate)
- [terrascan](#terrascan)
- [Authors](#authors)
- [License](#license)
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved

## How to install

Expand Down Expand Up @@ -550,6 +552,22 @@ Example:

**Warning:** If you use Terraform workspaces, DO NOT use this workaround ([details](https://github.com/antonbabenko/pre-commit-terraform/issues/203#issuecomment-918791847)). Wait to [`force-init`](https://github.com/antonbabenko/pre-commit-terraform/issues/224) option implementation.

### terrascan

1. `terrascan` supports custom arguments so you can pass supported flags like `--non-recursive` and `--policy-type` to disable recursive inspection and set the policy type respectively::

```yaml
- id: terrascan
args:
- --args=--non-recursive # avoids scan errors on subdirectories without Terraform config files
- --args=--policy-type azure
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
```

See the `terrascan run -h` command line help for available options.

2. Use the `--args=--verbose` parameter to see the rule ID in the scaning output. Usuful to skip validations.
3. Use `--skip-rules strings` parameter to skip one or more rules globally while scanning (e.g.: `--args=--skip-rules="ruleID1,ruleID2"`).
carlosbustillordguez marked this conversation as resolved.
Show resolved Hide resolved
4. Use the syntax `#ts:skip=RuleID optional_comment` inside a resource to skip the rule for that resource.

## Authors

Expand Down
29 changes: 22 additions & 7 deletions terrascan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,42 @@ set -eo pipefail
main() {
initialize_
parse_cmdline_ "$@"

# propagate $FILES to custom function
terrascan_ "$ARGS" "$FILES"
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
terrascan_
}

terrascan_() {
# consume modified files passed from pre-commit so that
# terrascan runs against only those relevant directories
for file_with_path in $FILES; do
for file_with_path in "${FILES[@]}"; do
file_with_path="${file_with_path// /__REPLACED__SPACE__}"
paths[index]=$(dirname "$file_with_path")

let "index+=1"
((index++)) || true
carlosbustillordguez marked this conversation as resolved.
Show resolved Hide resolved
done

# allow terrascan continue if exit_code greater than 0
carlosbustillordguez marked this conversation as resolved.
Show resolved Hide resolved
set +e
carlosbustillordguez marked this conversation as resolved.
Show resolved Hide resolved
terrascan_final_exit_code=0

# for each path run terrascan
for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do
path_uniq="${path_uniq//__REPLACED__SPACE__/ }"
pushd "$path_uniq" > /dev/null
terrascan scan -i terraform $ARGS

# pass the arguments to terrascan
# shellcheck disable=SC2068 # terrascan fails when is used "${ARGS[@]}"
carlosbustillordguez marked this conversation as resolved.
Show resolved Hide resolved
terrascan scan -i terraform ${ARGS[@]}

local exit_code=$?
if [ $exit_code != 0 ]; then
terrascan_final_exit_code=$exit_code
fi

popd > /dev/null
done

# return the terrascan final exit_code
set -e
exit $terrascan_final_exit_code
carlosbustillordguez marked this conversation as resolved.
Show resolved Hide resolved
}

initialize_() {
Expand Down