Skip to content

Commit fee2387

Browse files
author
Cesar Rodriguez
authored
feat: Adds support for Terrascan (antonbabenko#195)
1 parent 96346e7 commit fee2387

File tree

3 files changed

+85
-3
lines changed

3 files changed

+85
-3
lines changed

.pre-commit-hooks.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,9 @@
8282
files: \.tf$
8383
exclude: \.+.terraform\/.*$
8484
require_serial: true
85+
86+
- id: terrascan
87+
name: terrascan
88+
description: Runs terrascan on Terraform templates.
89+
language: script
90+
entry: terrascan.sh

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
* [`TFSec`](https://github.com/liamg/tfsec) required for `terraform_tfsec` hook.
1313
* [`coreutils`](https://formulae.brew.sh/formula/coreutils) required for `terraform_validate` hook on macOS (due to use of `realpath`).
1414
* [`checkov`](https://github.com/bridgecrewio/checkov) required for `checkov` hook.
15+
* [`terrascan`](https://github.com/accurics/terrascan) required for `terrascan` hook.
1516

1617
or build and use the Docker image locally as mentioned below in the `Run` section.
1718

1819
##### MacOS
1920

2021
```bash
21-
brew install pre-commit gawk terraform-docs tflint tfsec coreutils checkov
22+
brew install pre-commit gawk terraform-docs tflint tfsec coreutils checkov terrascan
2223
```
2324

2425
##### Ubuntu 18.04
@@ -32,6 +33,7 @@ pip3 install pre-commit
3233
curl -L "$(curl -s https://api.github.com/repos/terraform-docs/terraform-docs/releases/latest | grep -o -E "https://.+?-linux-amd64.tar.gz")" > terraform-docs.tgz && tar xzf terraform-docs.tgz && chmod +x terraform-docs && sudo mv terraform-docs /usr/bin/
3334
curl -L "$(curl -s https://api.github.com/repos/terraform-linters/tflint/releases/latest | grep -o -E "https://.+?_linux_amd64.zip")" > tflint.zip && unzip tflint.zip && rm tflint.zip && sudo mv tflint /usr/bin/
3435
curl -L "$(curl -s https://api.github.com/repos/tfsec/tfsec/releases/latest | grep -o -E "https://.+?tfsec-linux-amd64")" > tfsec && chmod +x tfsec && mv tfsec /usr/bin/
36+
curl -L "$(curl -s https://api.github.com/repos/accurics/terrascan/releases/latest | grep -o -E "https://.+?_Linux_x86_64.tar.gz")" > terrascan.tar.gz && tar -xf terrascan.tar.gz terrascan && rm terrascan.tar.gz && sudo mv terrascan /usr/bin/
3537
python3.7 -m pip install -U checkov
3638
```
3739

@@ -72,9 +74,9 @@ or you can also build and use the provided Docker container, which wraps all dep
7274
```bash
7375
# first building it
7476
docker build -t pre-commit .
75-
# and then running it in the folder
77+
# and then running it in the folder
7678
# with the terraform code you want to check by executing
77-
docker run -v $(pwd):/lint -w /lint pre-commit run -a
79+
docker run -v $(pwd):/lint -w /lint pre-commit run -a
7880
```
7981

8082
## Available Hooks
@@ -93,6 +95,7 @@ There are several [pre-commit](https://pre-commit.com/) hooks to keep Terraform
9395
| `terragrunt_validate` | Validates all [Terragrunt](https://github.com/gruntwork-io/terragrunt) configuration files (`*.hcl`) |
9496
| `terraform_tfsec` | [TFSec](https://github.com/liamg/tfsec) static analysis of terraform templates to spot potential security issues. |
9597
| `checkov` | [checkov](https://github.com/bridgecrewio/checkov) static analysis of terraform templates to spot potential security issues. |
98+
| `terrascan` | [terrascan](https://github.com/accurics/terrascan) Detect compliance and security violations. |
9699

97100
Check the [source file](https://github.com/antonbabenko/pre-commit-terraform/blob/master/.pre-commit-hooks.yaml) to know arguments used for each hook.
98101

terrascan.sh

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env bash
2+
set -eo pipefail
3+
4+
main() {
5+
initialize_
6+
parse_cmdline_ "$@"
7+
8+
# propagate $FILES to custom function
9+
terrascan_ "$ARGS" "$FILES"
10+
}
11+
12+
terrascan_() {
13+
# consume modified files passed from pre-commit so that
14+
# terrascan runs against only those relevant directories
15+
for file_with_path in $FILES; do
16+
file_with_path="${file_with_path// /__REPLACED__SPACE__}"
17+
paths[index]=$(dirname "$file_with_path")
18+
19+
let "index+=1"
20+
done
21+
22+
for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do
23+
path_uniq="${path_uniq//__REPLACED__SPACE__/ }"
24+
pushd "$path_uniq" > /dev/null
25+
terrascan scan -i terraform $ARGS
26+
popd > /dev/null
27+
done
28+
}
29+
30+
initialize_() {
31+
# get directory containing this script
32+
local dir
33+
local source
34+
source="${BASH_SOURCE[0]}"
35+
while [[ -L $source ]]; do # resolve $source until the file is no longer a symlink
36+
dir="$(cd -P "$(dirname "$source")" > /dev/null && pwd)"
37+
source="$(readlink "$source")"
38+
# if $source was a relative symlink, we need to resolve it relative to the path where the symlink file was located
39+
[[ $source != /* ]] && source="$dir/$source"
40+
done
41+
_SCRIPT_DIR="$(dirname "$source")"
42+
43+
# source getopt function
44+
# shellcheck source=lib_getopt
45+
. "$_SCRIPT_DIR/lib_getopt"
46+
}
47+
48+
parse_cmdline_() {
49+
declare argv
50+
argv=$(getopt -o a: --long args: -- "$@") || return
51+
eval "set -- $argv"
52+
53+
for argv; do
54+
case $argv in
55+
-a | --args)
56+
shift
57+
ARGS+=("$1")
58+
shift
59+
;;
60+
--)
61+
shift
62+
FILES+=("$@")
63+
break
64+
;;
65+
esac
66+
done
67+
}
68+
69+
# global arrays
70+
declare -a ARGS=()
71+
declare -a FILES=()
72+
73+
[[ ${BASH_SOURCE[0]} != "$0" ]] || main "$@"

0 commit comments

Comments
 (0)