Skip to content

Commit f3c819a

Browse files
authored
feat(trivy): Add terraform_trivy hook and deprecate terraform_tfsec (antonbabenko#606)
1 parent cf0f316 commit f3c819a

File tree

6 files changed

+152
-4
lines changed

6 files changed

+152
-4
lines changed

.github/.container-structure-test-config.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ commandTests:
5050
args: [ "--version" ]
5151
expectedOutput: [ "([0-9]+\\.){2}[0-9]+\\n$" ]
5252

53+
- name: "trivy"
54+
command: "trivy"
55+
args: [ "--version" ]
56+
expectedOutput: [ "Version: ([0-9]+\\.){2}[0-9]+\\n" ]
57+
5358
- name: "tfupdate"
5459
command: "tfupdate"
5560
args: [ "--version" ]

.pre-commit-hooks.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,21 @@
8686
exclude: \.terraform\/.*$
8787

8888
- id: terraform_tfsec
89-
name: Terraform validate with tfsec
89+
name: Terraform validate with tfsec (deprecated, use "terraform_trivy")
9090
description: Static analysis of Terraform templates to spot potential security issues.
9191
require_serial: true
9292
entry: hooks/terraform_tfsec.sh
9393
files: \.tf(vars)?$
9494
language: script
9595

96+
- id: terraform_trivy
97+
name: Terraform validate with trivy
98+
description: Static analysis of Terraform templates to spot potential security issues.
99+
require_serial: true
100+
entry: hooks/terraform_trivy.sh
101+
files: \.tf(vars)?$
102+
language: script
103+
96104
- id: checkov
97105
name: checkov (deprecated, use "terraform_checkov")
98106
description: Runs checkov on Terraform templates.

Dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ ARG TERRAGRUNT_VERSION=${TERRAGRUNT_VERSION:-false}
3737
ARG TERRASCAN_VERSION=${TERRASCAN_VERSION:-false}
3838
ARG TFLINT_VERSION=${TFLINT_VERSION:-false}
3939
ARG TFSEC_VERSION=${TFSEC_VERSION:-false}
40+
ARG TRIVY_VERSION=${TRIVY_VERSION:-false}
4041
ARG TFUPDATE_VERSION=${TFUPDATE_VERSION:-false}
4142
ARG HCLEDIT_VERSION=${HCLEDIT_VERSION:-false}
4243

@@ -53,6 +54,7 @@ RUN if [ "$INSTALL_ALL" != "false" ]; then \
5354
echo "export TERRASCAN_VERSION=latest" >> /.env && \
5455
echo "export TFLINT_VERSION=latest" >> /.env && \
5556
echo "export TFSEC_VERSION=latest" >> /.env && \
57+
echo "export TRIVY_VERSION=latest" >> /.env && \
5658
echo "export TFUPDATE_VERSION=latest" >> /.env && \
5759
echo "export HCLEDIT_VERSION=latest" >> /.env \
5860
; else \
@@ -136,6 +138,17 @@ RUN . /.env && \
136138
) && chmod +x tfsec \
137139
; fi
138140

141+
# Trivy
142+
RUN . /.env && \
143+
if [ "$TRIVY_VERSION" != "false" ]; then \
144+
if [ "$TARGETARCH" != "amd64" ]; then ARCH="$TARGETARCH"; else ARCH="64bit"; fi; \
145+
( \
146+
TRIVY_RELEASES="https://api.github.com/repos/aquasecurity/trivy/releases" && \
147+
[ "$TRIVY_VERSION" = "latest" ] && curl -L "$(curl -s ${TRIVY_RELEASES}/latest | grep -o -E -i -m 1 "https://.+?/trivy_.+?_${TARGETOS}-${ARCH}.tar.gz")" > trivy.tar.gz \
148+
|| curl -L "$(curl -s ${TRIVY_RELEASES} | grep -o -E -i -m 1 "https://.+?/v${TRIVY_VERSION}/trivy_.+?_${TARGETOS}-${ARCH}.tar.gz")" > trivy.tar.gz \
149+
) && tar -xzf trivy.tar.gz trivy && rm trivy.tar.gz \
150+
; fi
151+
139152
# TFUpdate
140153
RUN . /.env && \
141154
if [ "$TFUPDATE_VERSION" != "false" ]; then \

README.md

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ If you are using `pre-commit-terraform` already or want to support its developme
4747
* [terraform\_fmt](#terraform_fmt)
4848
* [terraform\_providers\_lock](#terraform_providers_lock)
4949
* [terraform\_tflint](#terraform_tflint)
50-
* [terraform\_tfsec](#terraform_tfsec)
50+
* [terraform\_tfsec (deprecated)](#terraform_tfsec-deprecated)
51+
* [terraform\_trivy](#terraform_trivy)
5152
* [terraform\_validate](#terraform_validate)
5253
* [terraform\_wrapper\_module\_for\_each](#terraform_wrapper_module_for_each)
5354
* [terrascan](#terrascan)
@@ -82,6 +83,7 @@ If you are using `pre-commit-terraform` already or want to support its developme
8283
* [`terrascan`](https://github.com/tenable/terrascan) required for `terrascan` hook.
8384
* [`TFLint`](https://github.com/terraform-linters/tflint) required for `terraform_tflint` hook.
8485
* [`TFSec`](https://github.com/liamg/tfsec) required for `terraform_tfsec` hook.
86+
* [`Trivy`](https://github.com/aquasecurity/trivy) required for `terraform_trivy` hook.
8587
* [`infracost`](https://github.com/infracost/infracost) required for `infracost_breakdown` hook.
8688
* [`jq`](https://github.com/stedolan/jq) required for `terraform_validate` with `--retry-once-with-cleanup` flag, and for `infracost_breakdown` hook.
8789
* [`tfupdate`](https://github.com/minamijoyo/tfupdate) required for `tfupdate` hook.
@@ -125,6 +127,7 @@ docker build -t pre-commit-terraform \
125127
--build-arg TERRASCAN_VERSION=1.10.0 \
126128
--build-arg TFLINT_VERSION=0.31.0 \
127129
--build-arg TFSEC_VERSION=latest \
130+
--build-arg TRIVY_VERSION=latest \
128131
--build-arg TFUPDATE_VERSION=latest \
129132
--build-arg HCLEDIT_VERSION=latest \
130133
.
@@ -138,7 +141,7 @@ Set `-e PRE_COMMIT_COLOR=never` to disable the color output in `pre-commit`.
138141
<details><summary><b>MacOS</b></summary><br>
139142

140143
```bash
141-
brew install pre-commit terraform-docs tflint tfsec checkov terrascan infracost tfupdate minamijoyo/hcledit/hcledit jq
144+
brew install pre-commit terraform-docs tflint tfsec trivy checkov terrascan infracost tfupdate minamijoyo/hcledit/hcledit jq
142145
```
143146

144147
</details>
@@ -156,6 +159,7 @@ python3.7 -m pip install -U checkov
156159
curl -L "$(curl -s https://api.github.com/repos/terraform-docs/terraform-docs/releases/latest | grep -o -E -m 1 "https://.+?-linux-amd64.tar.gz")" > terraform-docs.tgz && tar -xzf terraform-docs.tgz && rm terraform-docs.tgz && chmod +x terraform-docs && sudo mv terraform-docs /usr/bin/
157160
curl -L "$(curl -s https://api.github.com/repos/terraform-linters/tflint/releases/latest | grep -o -E -m 1 "https://.+?_linux_amd64.zip")" > tflint.zip && unzip tflint.zip && rm tflint.zip && sudo mv tflint /usr/bin/
158161
curl -L "$(curl -s https://api.github.com/repos/aquasecurity/tfsec/releases/latest | grep -o -E -m 1 "https://.+?tfsec-linux-amd64")" > tfsec && chmod +x tfsec && sudo mv tfsec /usr/bin/
162+
curl -L "$(curl -s https://api.github.com/repos/aquasecurity/trivy/releases/latest | grep -o -E -i -m 1 "https://.+?/trivy_.+?_Linux-64bit.tar.gz")" > trivy.tar.gz && tar -xzf trivy.tar.gz trivy && rm trivy.tar.gz && sudo mv trivy /usr/bin
159163
curl -L "$(curl -s https://api.github.com/repos/tenable/terrascan/releases/latest | grep -o -E -m 1 "https://.+?_Linux_x86_64.tar.gz")" > terrascan.tar.gz && tar -xzf terrascan.tar.gz terrascan && rm terrascan.tar.gz && sudo mv terrascan /usr/bin/ && terrascan init
160164
sudo apt install -y jq && \
161165
curl -L "$(curl -s https://api.github.com/repos/infracost/infracost/releases/latest | grep -o -E -m 1 "https://.+?-linux-amd64.tar.gz")" > infracost.tgz && tar -xzf infracost.tgz && rm infracost.tgz && sudo mv infracost-linux-amd64 /usr/bin/infracost && infracost register
@@ -178,6 +182,7 @@ curl -L "$(curl -s https://api.github.com/repos/terraform-docs/terraform-docs/re
178182
curl -L "$(curl -s https://api.github.com/repos/tenable/terrascan/releases/latest | grep -o -E -m 1 "https://.+?_Linux_x86_64.tar.gz")" > terrascan.tar.gz && tar -xzf terrascan.tar.gz terrascan && rm terrascan.tar.gz && sudo mv terrascan /usr/bin/ && terrascan init
179183
curl -L "$(curl -s https://api.github.com/repos/terraform-linters/tflint/releases/latest | grep -o -E -m 1 "https://.+?_linux_amd64.zip")" > tflint.zip && unzip tflint.zip && rm tflint.zip && sudo mv tflint /usr/bin/
180184
curl -L "$(curl -s https://api.github.com/repos/aquasecurity/tfsec/releases/latest | grep -o -E -m 1 "https://.+?tfsec-linux-amd64")" > tfsec && chmod +x tfsec && sudo mv tfsec /usr/bin/
185+
curl -L "$(curl -s https://api.github.com/repos/aquasecurity/trivy/releases/latest | grep -o -E -i -m 1 "https://.+?/trivy_.+?_Linux-64bit.tar.gz")" > trivy.tar.gz && tar -xzf trivy.tar.gz trivy && rm trivy.tar.gz && sudo mv trivy /usr/bin
181186
sudo apt install -y jq && \
182187
curl -L "$(curl -s https://api.github.com/repos/infracost/infracost/releases/latest | grep -o -E -m 1 "https://.+?-linux-amd64.tar.gz")" > infracost.tgz && tar -xzf infracost.tgz && rm infracost.tgz && sudo mv infracost-linux-amd64 /usr/bin/infracost && infracost register
183188
curl -L "$(curl -s https://api.github.com/repos/minamijoyo/tfupdate/releases/latest | grep -o -E -m 1 "https://.+?_linux_amd64.tar.gz")" > tfupdate.tar.gz && tar -xzf tfupdate.tar.gz tfupdate && rm tfupdate.tar.gz && sudo mv tfupdate /usr/bin/
@@ -274,6 +279,7 @@ There are several [pre-commit](https://pre-commit.com/) hooks to keep Terraform
274279
| `terraform_providers_lock` | Updates provider signatures in [dependency lock files](https://www.terraform.io/docs/cli/commands/providers/lock.html). [Hook notes](#terraform_providers_lock) | - |
275280
| `terraform_tflint` | Validates all Terraform configuration files with [TFLint](https://github.com/terraform-linters/tflint). [Available TFLint rules](https://github.com/terraform-linters/tflint/tree/master/docs/rules#rules). [Hook notes](#terraform_tflint). | `tflint` |
276281
| `terraform_tfsec` | [TFSec](https://github.com/aquasecurity/tfsec) static analysis of terraform templates to spot potential security issues. [Hook notes](#terraform_tfsec) | `tfsec` |
282+
| `terraform_trivy` | [Trivy](https://github.com/aquasecurity/trivy) static analysis of terraform templates to spot potential security issues. [Hook notes](#terraform_trivy) | `trivy` |
277283
| `terraform_validate` | Validates all Terraform configuration files. [Hook notes](#terraform_validate) | `jq`, only for `--retry-once-with-cleanup` flag |
278284
| `terragrunt_fmt` | Reformat all [Terragrunt](https://github.com/gruntwork-io/terragrunt) configuration files (`*.hcl`) to a canonical format. | `terragrunt` |
279285
| `terragrunt_validate` | Validates all [Terragrunt](https://github.com/gruntwork-io/terragrunt) configuration files (`*.hcl`) | `terragrunt` |
@@ -681,7 +687,9 @@ To replicate functionality in `terraform_docs` hook:
681687
```
682688

683689

684-
### terraform_tfsec
690+
### terraform_tfsec (deprecated)
691+
692+
**DEPRECATED**. [tfsec was replaced by trivy](https://github.com/aquasecurity/tfsec/discussions/1994), so please use [`terraform_trivy`](#terraform_trivy).
685693

686694
1. `terraform_tfsec` will consume modified files that pre-commit
687695
passes to it, so you can perform whitelisting of directories
@@ -738,6 +746,48 @@ To replicate functionality in `terraform_docs` hook:
738746
- --args=--config-file=.tfsec.json
739747
```
740748

749+
### terraform_trivy
750+
751+
1. `terraform_trivy` will consume modified files that pre-commit
752+
passes to it, so you can perform whitelisting of directories
753+
or files to run against via [files](https://pre-commit.com/#config-files)
754+
pre-commit flag
755+
756+
Example:
757+
758+
```yaml
759+
- id: terraform_trivy
760+
files: ^prd-infra/
761+
```
762+
763+
The above will tell pre-commit to pass down files from the `prd-infra/` folder
764+
only such that the underlying `trivy` tool can run against changed files in this
765+
directory, ignoring any other folders at the root level
766+
767+
2. To ignore specific warnings, follow the convention from the
768+
[documentation](https://aquasecurity.github.io/trivy/latest/docs/configuration/filtering/).
769+
770+
Example:
771+
772+
```hcl
773+
#trivy:ignore:AVD-AWS-0107
774+
#trivy:ignore:AVD-AWS-0124
775+
resource "aws_security_group_rule" "my-rule" {
776+
type = "ingress"
777+
cidr_blocks = ["0.0.0.0/0"]
778+
}
779+
```
780+
781+
3. `terraform_trivy` supports custom arguments, so you can pass supported `--format` (output), `--skip-dirs` (exclude directories) and other flags:
782+
783+
```yaml
784+
- id: terraform_trivy
785+
args:
786+
- >
787+
--args=--format json
788+
--skip-dirs="**/.terragrunt-cache"
789+
```
790+
741791
### terraform_validate
742792

743793
1. `terraform_validate` supports custom arguments so you can pass supported `-no-color` or `-json` flags:

hooks/terraform_tfsec.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ function main {
2222
ARGS+=("--no-color")
2323
fi
2424

25+
common::colorify "yellow" "tfsec tool was deprecated, and replaced by trivy. You can check trivy hook here:"
26+
common::colorify "yellow" "https://github.com/antonbabenko/pre-commit-terraform/tree/master#terraform_trivy"
27+
2528
common::per_dir_hook "$HOOK_ID" "${#ARGS[@]}" "${ARGS[@]}" "${FILES[@]}"
2629
}
2730

hooks/terraform_trivy.sh

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env bash
2+
set -eo pipefail
3+
4+
# globals variables
5+
# shellcheck disable=SC2155 # No way to assign to readonly variable in separate lines
6+
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
7+
# shellcheck source=_common.sh
8+
. "$SCRIPT_DIR/_common.sh"
9+
10+
function main {
11+
common::initialize "$SCRIPT_DIR"
12+
common::parse_cmdline "$@"
13+
common::export_provided_env_vars "${ENV_VARS[@]}"
14+
common::parse_and_export_env_vars
15+
# Support for setting PATH to repo root.
16+
for i in "${!ARGS[@]}"; do
17+
ARGS[i]=${ARGS[i]/__GIT_WORKING_DIR__/$(pwd)\/}
18+
done
19+
20+
common::per_dir_hook "$HOOK_ID" "${#ARGS[@]}" "${ARGS[@]}" "${FILES[@]}"
21+
}
22+
23+
#######################################################################
24+
# Unique part of `common::per_dir_hook`. The function is executed in loop
25+
# on each provided dir path. Run wrapped tool with specified arguments
26+
# Arguments:
27+
# dir_path (string) PATH to dir relative to git repo root.
28+
# Can be used in error logging
29+
# change_dir_in_unique_part (string/false) Modifier which creates
30+
# possibilities to use non-common chdir strategies.
31+
# Availability depends on hook.
32+
# args (array) arguments that configure wrapped tool behavior
33+
# Outputs:
34+
# If failed - print out hook checks status
35+
#######################################################################
36+
function per_dir_hook_unique_part {
37+
# shellcheck disable=SC2034 # Unused var.
38+
local -r dir_path="$1"
39+
# shellcheck disable=SC2034 # Unused var.
40+
local -r change_dir_in_unique_part="$2"
41+
shift 2
42+
local -a -r args=("$@")
43+
44+
# pass the arguments to hook
45+
trivy conf "$(pwd)" --exit-code=1 "${args[@]}"
46+
47+
# return exit code to common::per_dir_hook
48+
local exit_code=$?
49+
return $exit_code
50+
}
51+
52+
#######################################################################
53+
# Unique part of `common::per_dir_hook`. The function is executed one time
54+
# in the root git repo
55+
# Arguments:
56+
# args (array) arguments that configure wrapped tool behavior
57+
#######################################################################
58+
function run_hook_on_whole_repo {
59+
local -a -r args=("$@")
60+
61+
# pass the arguments to hook
62+
trivy conf "$(pwd)" "${args[@]}"
63+
64+
# return exit code to common::per_dir_hook
65+
local exit_code=$?
66+
return $exit_code
67+
}
68+
69+
[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@"

0 commit comments

Comments
 (0)