Skip to content

Commit 66401d9

Browse files
fix: Check all directories with changes and pass all args in terrascan hook (antonbabenko#305)
1 parent 04ecd10 commit 66401d9

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

.pre-commit-hooks.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,6 @@
108108
description: Runs terrascan on Terraform templates.
109109
language: script
110110
entry: terrascan.sh
111+
files: \.tf$
112+
exclude: \.terraform\/.*$
111113
require_serial: true

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ If you are using `pre-commit-terraform` already or want to support its developme
4343
* [terraform_tflint](#terraform_tflint)
4444
* [terraform_tfsec](#terraform_tfsec)
4545
* [terraform_validate](#terraform_validate)
46+
* [terrascan](#terrascan)
4647
* [Authors](#authors)
4748
* [License](#license)
4849

@@ -223,7 +224,7 @@ There are several [pre-commit](https://pre-commit.com/) hooks to keep Terraform
223224
| `terraform_validate` | Validates all Terraform configuration files. [Hook notes](#terraform_validate) | - |
224225
| `terragrunt_fmt` | Reformat all [Terragrunt](https://github.com/gruntwork-io/terragrunt) configuration files (`*.hcl`) to a canonical format. | `terragrunt` |
225226
| `terragrunt_validate` | Validates all [Terragrunt](https://github.com/gruntwork-io/terragrunt) configuration files (`*.hcl`) | `terragrunt` |
226-
| `terrascan` | [terrascan](https://github.com/accurics/terrascan) Detect compliance and security violations. | `terrascan` |
227+
| `terrascan` | [terrascan](https://github.com/accurics/terrascan) Detect compliance and security violations. [Hook notes](#terrascan) | `terrascan` |
227228
<!-- markdownlint-enable no-inline-html -->
228229

229230
Check the [source file](https://github.com/antonbabenko/pre-commit-terraform/blob/master/.pre-commit-hooks.yaml) to know arguments used for each hook.
@@ -550,6 +551,22 @@ Example:
550551

551552
**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.
552553

554+
### terrascan
555+
556+
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:
557+
558+
```yaml
559+
- id: terrascan
560+
args:
561+
- --args=--non-recursive # avoids scan errors on subdirectories without Terraform config files
562+
- --args=--policy-type=azure
563+
```
564+
565+
See the `terrascan run -h` command line help for available options.
566+
567+
2. Use the `--args=--verbose` parameter to see the rule ID in the scaning output. Usuful to skip validations.
568+
3. Use `--skip-rules="ruleID1,ruleID2"` parameter to skip one or more rules globally while scanning (e.g.: `--args=--skip-rules="ruleID1,ruleID2"`).
569+
4. Use the syntax `#ts:skip=RuleID optional_comment` inside a resource to skip the rule for that resource.
553570

554571
## Authors
555572

terrascan.sh

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,49 @@ set -eo pipefail
44
main() {
55
initialize_
66
parse_cmdline_ "$@"
7-
8-
# propagate $FILES to custom function
9-
terrascan_ "$ARGS" "$FILES"
7+
terrascan_ "${ARGS[*]}" "${FILES[@]}"
108
}
119

1210
terrascan_() {
11+
local -r args="${1}"
12+
shift 1
13+
local -a -r files=("$@")
14+
1315
# consume modified files passed from pre-commit so that
1416
# terrascan runs against only those relevant directories
15-
for file_with_path in $FILES; do
17+
for file_with_path in "${files[@]}"; do
1618
file_with_path="${file_with_path// /__REPLACED__SPACE__}"
1719
paths[index]=$(dirname "$file_with_path")
18-
19-
let "index+=1"
20+
index=$((index + 1))
2021
done
2122

23+
# allow terrascan to continue if exit_code is greater than 0
24+
# preserve errexit status
25+
shopt -qo errexit && ERREXIT_IS_SET=true
26+
set +e
27+
terrascan_final_exit_code=0
28+
29+
# for each path run terrascan
2230
for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do
2331
path_uniq="${path_uniq//__REPLACED__SPACE__/ }"
2432
pushd "$path_uniq" > /dev/null
25-
terrascan scan -i terraform $ARGS
33+
34+
# pass the arguments to terrascan
35+
# shellcheck disable=SC2086 # terrascan fails when quoting is used ("$arg" vs $arg)
36+
terrascan scan -i terraform $args
37+
38+
local exit_code=$?
39+
if [ $exit_code != 0 ]; then
40+
terrascan_final_exit_code=$exit_code
41+
fi
42+
2643
popd > /dev/null
2744
done
45+
46+
# restore errexit if it was set before the "for" loop
47+
[[ $ERREXIT_IS_SET ]] && set -e
48+
# return the terrascan final exit_code
49+
exit $terrascan_final_exit_code
2850
}
2951

3052
initialize_() {

0 commit comments

Comments
 (0)