Two families of tools, catching different problems:
- Misconfiguration scanners read
.tffiles or plan JSON and flag insecure settings: public storage, open NSGs, missing encryption, no diagnostic logs. - Secret scanners look for credentials committed to Git: keys, tokens, connection strings, private keys.
Run both in pre-commit (fast feedback) and CI (the gate nobody can skip). Context on why and what else to do: Terraform security.
Where a tool ships as a container image, running it with
podman run --rm -v "$PWD:/src:Z" ...avoids installing anything on the host. The:Zis for SELinux on Fedora.
Misconfiguration scanners¶
Checkov¶
Large built-in policy library for Terraform (and Kubernetes, Dockerfiles, CI configs), plan-JSON scanning, and custom policies in Python or YAML.
# Install (pipx keeps it out of system Python)
sudo dnf -y install pipx && pipx ensurepath
pipx install checkov
# Scan a directory of .tf files
checkov -d . --framework terraform
# Scan a plan (resolves variables, modules and computed values)
terraform plan -out=tfplan
terraform show -json tfplan > tfplan.json
checkov -f tfplan.json --framework terraform_plan
# Only fail on high/critical; output for GitLab's SAST widget
checkov -d . --soft-fail-on LOW,MEDIUM -o cli -o gitlab_sast --output-file-path console,checkov
Skip a check with a reason, inline:
resource "azurerm_storage_account" "web" {
# checkov:skip=CKV_AZURE_59: public static website by design
}
Container: podman run --rm -v "$PWD:/src:Z" docker.io/bridgecrew/checkov -d /src
Trivy (formerly tfsec)¶
tfsec has been folded into Trivy. The tfsec checks live on in trivy config, and Aqua recommends migrating. Existing tfsec . invocations still work if you have the binary, but new setups should use Trivy, which also scans container images, SBOMs and dependencies with the same tool.
# Fedora: Aqua's RPM repository
cat <<'EOF' | sudo tee /etc/yum.repos.d/trivy.repo
[trivy]
name=Trivy repository
baseurl=https://aquasecurity.github.io/trivy-repo/rpm/releases/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://aquasecurity.github.io/trivy-repo/rpm/public.key
EOF
sudo dnf -y install trivy
# Scan Terraform
trivy config .
trivy config --severity HIGH,CRITICAL --exit-code 1 .
# Use a tfvars file so variable-driven settings are evaluated
trivy config --tf-vars env/prod.tfvars .
Inline ignore: # trivy:ignore:AVD-AZU-0012 on the line above the resource or attribute. Old #tfsec:ignore: comments are still honoured.
Container: podman run --rm -v "$PWD:/src:Z" docker.io/aquasec/trivy config /src
Legacy tfsec, if you still need it:
curl -s https://raw.githubusercontent.com/aquasecurity/tfsec/master/scripts/install_linux.sh | bash
tfsec .
KICS¶
Checkmarx's IaC scanner, with queries written in Rego. Broad coverage across Terraform, CloudFormation, Bicep/ARM, Kubernetes, Ansible.
podman run --rm -v "$PWD:/src:Z" docker.io/checkmarx/kics:latest scan -p /src -o /src/kics-results
TFLint¶
Not a security scanner first, but it catches the class of bug scanners don't: invalid VM sizes, deprecated arguments, unused declarations, naming violations. The provider rulesets know each provider's schema.
curl -s https://raw.githubusercontent.com/terraform-linters/tflint/master/install_linux.sh | bash
# .tflint.hcl
config {
call_module_type = "local" # also lint local module calls
}
plugin "terraform" {
enabled = true
preset = "recommended"
}
plugin "azurerm" {
enabled = true
version = "0.28.0" # pin; check the latest release
source = "github.com/terraform-linters/tflint-ruleset-azurerm"
}
rule "terraform_naming_convention" {
enabled = true
format = "snake_case"
}
rule "terraform_documented_variables" { enabled = true }
rule "terraform_typed_variables" { enabled = true }
rule "terraform_unused_declarations" { enabled = true }
tflint --init # download plugins
tflint --recursive --format compact
More on configuring TFLint in hygiene.
Secret scanners¶
trufflehog¶
Finds secrets and can verify them against the issuing service, which kills most false positives. Note: the old Python truffleHog from pip (v2) is unmaintained; use v3, a Go binary.
# Install v3
curl -sSfL https://raw.githubusercontent.com/trufflesecurity/trufflehog/main/scripts/install.sh \
| sudo sh -s -- -b /usr/local/bin
# Working tree
trufflehog filesystem . --json
# Full Git history, only secrets confirmed live
trufflehog git file://. --only-verified --fail
Container: podman run --rm -v "$PWD:/src:Z" docker.io/trufflesecurity/trufflehog:latest filesystem /src
gitleaks¶
Fast, regex-based, easy to tune with a .gitleaks.toml. A good default for pre-commit.
sudo dnf -y install gitleaks # if your Fedora release packages it; otherwise grab the GitHub release binary
gitleaks git -v . # scan history (gitleaks 8.19+ syntax)
gitleaks dir -v . # scan files only
detect-secrets¶
Yelp's scanner with a baseline file: existing findings are recorded and reviewed once, and only new secrets fail.
pipx install detect-secrets
detect-secrets scan --all-files > .secrets.baseline
detect-secrets audit .secrets.baseline # mark true/false positives
detect-secrets-hook --baseline .secrets.baseline $(git diff --cached --name-only)
git-secrets¶
AWS Labs' Git hook that blocks commits matching patterns. Simple and AWS-focused; gitleaks covers more providers.
git clone https://github.com/awslabs/git-secrets.git && cd git-secrets && sudo make install
cd /path/to/repo
git secrets --install # adds pre-commit, commit-msg and prepare-commit-msg hooks
git secrets --register-aws # AWS key patterns
git secrets --add 'AccountKey=[A-Za-z0-9+/=]{88}' # e.g. Azure storage connection strings
git secrets --scan-history
Pre-commit: wire it together¶
# .pre-commit-config.yaml
repos:
- repo: https://github.com/antonbabenko/pre-commit-terraform
rev: v1.96.1 # pin; `pre-commit autoupdate` to bump
hooks:
- id: terraform_fmt
- id: terraform_validate
- id: terraform_tflint
args: ["--args=--config=__GIT_WORKING_DIR__/.tflint.hcl"]
- id: terraform_docs
- id: terraform_trivy
args: ["--args=--severity=HIGH,CRITICAL"]
- id: terraform_checkov
args: ["--args=--quiet --framework terraform"]
- repo: https://github.com/gitleaks/gitleaks
rev: v8.21.2
hooks:
- id: gitleaks
pipx install pre-commit
pre-commit install
pre-commit run --all-files
Pin rev values and let pre-commit autoupdate (or Renovate's pre-commit manager) bump them.
GitLab CI¶
iac-scan:
stage: validate
image: docker.io/aquasec/trivy:latest
script:
- trivy config --severity HIGH,CRITICAL --exit-code 1 --format template
--template "@/contrib/gitlab-codequality.tpl" --output gl-code-quality-report.json .
artifacts:
reports:
codequality: gl-code-quality-report.json
checkov-plan:
stage: plan
image: docker.io/bridgecrew/checkov:latest
needs: ["plan:dev"]
script:
- checkov -f tfplan.json --framework terraform_plan --soft-fail-on LOW,MEDIUM
secrets:
stage: validate
image: { name: docker.io/zricethezav/gitleaks:latest, entrypoint: [""] }
script:
- gitleaks git --redact -v .
GitLab also ships its own IaC SAST (based on KICS) and Secret Detection templates. Include Jobs/SAST-IaC.gitlab-ci.yml and Jobs/Secret-Detection.gitlab-ci.yml to get findings in the MR security widget.
Which to pick¶
| Need | Tool |
|---|---|
| One scanner, broadest Terraform coverage | Checkov |
| One tool for IaC + images + dependencies | Trivy |
| Provider-schema correctness | TFLint with provider ruleset |
| Your organisation's own rules | Conftest/OPA (policy) or Checkov custom policies |
| Secrets in pre-commit | gitleaks |
| Secrets across history, verified | trufflehog |
| Secrets with a reviewed baseline | detect-secrets |