Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
name: "TFSec Scan"
description: "Scan HCL using TFSec"
name: "Trivy Scan"
runs:
using: "composite"
steps:
- name: "TFSec Scan - Components"
- name: "Trivy Terraform IAC Scan"
shell: bash
run: |
components_exit_code=0
modules_exit_code=0

./scripts/terraform/tfsec.sh ./infrastructure/terraform/components || components_exit_code=$?
./scripts/terraform/tfsec.sh ./infrastructure/terraform/modules || modules_exit_code=$?
./scripts/terraform/trivy.sh ./infrastructure/terraform/components || components_exit_code=$?
./scripts/terraform/trivy.sh ./infrastructure/terraform/modules || modules_exit_code=$?

if [ $components_exit_code -ne 0 ] || [ $modules_exit_code -ne 0 ]; then
echo "One or more TFSec scans failed."
echo "Trivy misconfigurations detected."
exit 1
fi
8 changes: 4 additions & 4 deletions .github/workflows/stage-1-commit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ jobs:
uses: actions/checkout@v4
- name: "Lint Terraform"
uses: ./.github/actions/lint-terraform
tfsec:
name: "TFSec Scan"
trivy:
name: "Trivy Scan"
runs-on: ubuntu-latest
timeout-minutes: 5
needs: detect-terraform-changes
Expand All @@ -148,8 +148,8 @@ jobs:
uses: asdf-vm/actions/setup@v3
- name: "Perform Setup"
uses: ./.github/actions/setup
- name: "TFSec Scan"
uses: ./.github/actions/tfsec
- name: "Trivy Scan"
uses: ./.github/actions/trivy
count-lines-of-code:
name: "Count lines of code"
runs-on: ubuntu-latest
Expand Down
6 changes: 3 additions & 3 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
act 0.2.64
gitleaks 8.24.0
jq 1.6
nodejs 20.18.2
pre-commit 3.6.0
terraform 1.9.2
terraform-docs 0.19.0
trivy 0.61.0
vale 3.6.0
tfsec 1.28.10
nodejs 20.18.2
jq 1.6

# ==============================================================================
# The section below is reserved for Docker image versions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ resource "aws_iam_role_policy_attachment" "github_deploy_overload" {
policy_arn = aws_iam_policy.github_deploy_overload.arn
}

#tfsec:ignore:aws-iam-no-policy-wildcards Policy voilation expected for CI user role
#trivy:ignore:aws-iam-no-policy-wildcards Policy voilation expected for CI user role
data "aws_iam_policy_document" "github_deploy" {
statement {
effect = "Allow"
Expand Down
4 changes: 2 additions & 2 deletions infrastructure/terraform/components/app/iam_role_amplify.tf
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ data "aws_iam_policy_document" "amplify" {
"logs:PutLogEvents",
]

#tfsec:ignore:aws-iam-no-policy-wildcards
#trivy:ignore:aws-iam-no-policy-wildcards
resources = [
"${aws_cloudwatch_log_group.amplify.arn}:*",
"${aws_cloudwatch_log_group.amplify.arn}:log-stream:*",
Expand All @@ -59,7 +59,7 @@ data "aws_iam_policy_document" "amplify" {
"logs:DescribeLogGroups",
]

#tfsec:ignore:aws-iam-no-policy-wildcards
#trivy:ignore:aws-iam-no-policy-wildcards
resources = [
"arn:aws:logs:${var.region}:${var.aws_account_id}:*"
]
Expand Down
2 changes: 0 additions & 2 deletions scripts/config/tfsec.yaml

This file was deleted.

6 changes: 6 additions & 0 deletions scripts/config/trivy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
severity: MEDIUM # Minimum reported findings
exit-code: 1 # When issues are found
scan:
skip-files:
- "**/.terraform/**/*"
55 changes: 25 additions & 30 deletions scripts/terraform/tfsec.sh → scripts/terraform/trivy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ set -euo pipefail
# Run tfsec for security checks on Terraform code.
#
# Usage:
# $ ./tfsec.sh [directory]
# $ ./trivy.sh [directory]
# ==============================================================================

function main() {
Expand All @@ -18,68 +18,63 @@ function main() {

local dir_to_scan=${1:-.}

if command -v tfsec > /dev/null 2>&1 && ! is-arg-true "${FORCE_USE_DOCKER:-false}"; then
if command -v trivy > /dev/null 2>&1 && ! is-arg-true "${FORCE_USE_DOCKER:-false}"; then
# shellcheck disable=SC2154
run-tfsec-natively "$dir_to_scan"
run-trivy-natively "$dir_to_scan"
else
run-tfsec-in-docker "$dir_to_scan"
run-trivy-in-docker "$dir_to_scan"
fi
}

# Run tfsec on the specified directory.
# Run trivy on the specified directory.
# Arguments:
# $1 - Directory to scan
function run-tfsec-natively() {
function run-trivy-natively() {

local dir_to_scan="$1"

echo "TFSec found locally, running natively"
echo "Trivy found locally, running natively"

echo "Running TFSec on directory: $dir_to_scan"
tfsec \
--force-all-dirs \
--exclude-downloaded-modules \
--config-file scripts/config/tfsec.yaml \
--format text \
"$dir_to_scan"
echo "Running Trivy on directory: $dir_to_scan"
trivy config \
--config scripts/config/trivy.yaml \
--tf-exclude-downloaded-modules \
"${dir_to_scan}"

check-tfsec-status
check-trivy-status
}

# Check the exit status of tfsec.
function check-tfsec-status() {
function check-trivy-status() {

if [ $? -eq 0 ]; then
echo "TFSec completed successfully."
echo "Trivy completed successfully."
else
echo "TFSec found issues."
echo "Trivy found issues."
exit 1
fi
}

function run-tfsec-in-docker() {
function run-trivy-in-docker() {

# shellcheck disable=SC1091
source ./scripts/docker/docker.lib.sh
local dir_to_scan="$1"

# shellcheck disable=SC2155
local image=$(name=aquasec/tfsec docker-get-image-version-and-pull)
local image=$(name=aquasec/trivy docker-get-image-version-and-pull)
# shellcheck disable=SC2086
echo "TFSec not found locally, running in Docker Container"
echo "Running TFSec on directory: $dir_to_scan"
echo "Trivy not found locally, running in Docker Container"
echo "Running Trivy on directory: $dir_to_scan"
docker run --rm --platform linux/amd64 \
--volume "$PWD":/workdir \
--workdir /workdir \
"$image" \
--concise-output \
--force-all-dirs \
--exclude-downloaded-modules \
--config-file scripts/config/tfsec.yaml \
--format text \
--soft-fail \
"$dir_to_scan"
check-tfsec-status
config \
--config scripts/config/trivy.yaml \
--tf-exclude-downloaded-modules \
"${dir_to_scan}"
check-trivy-status
}
# ==============================================================================

Expand Down
Loading