Skip to content

Commit

Permalink
CCM-6245: TFSec Scanning
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesthompson26-nhs committed Aug 16, 2024
1 parent d9f11fd commit d594738
Showing 1 changed file with 48 additions and 22 deletions.
70 changes: 48 additions & 22 deletions scripts/terraform/tfsec.sh
Original file line number Diff line number Diff line change
@@ -1,33 +1,59 @@
#!/usr/bin/env bash

# Check if tfsec is installed
if ! command -v tfsec &> /dev/null; then
echo "tfsec could not be found, please install it first."
exit 1
fi
# WARNING: Please DO NOT edit this file! It is maintained in the Repository Template (https://github.com/nhs-england-tools/repository-template). Raise a PR instead.

set -euo pipefail

# Run tfsec for security checks on Terraform code.
#
# Usage:
# $ ./tfsec.sh [directory]
# ==============================================================================

function main() {

cd "$(git rev-parse --show-toplevel)"

local dir_to_scan=${1:-.}
run-tfsec "$dir_to_scan"
}

# Check if a directory was passed as an argument
if [ "$#" -eq 1 ]; then
DIR_TO_SCAN="$1"
elif [ "$#" -gt 1 ]; then
echo "Usage: $0 [directory]"
# Run tfsec on the specified directory.
# Arguments:
# $1 - Directory to scan
function run-tfsec() {

local dir_to_scan="$1"

if ! command -v tfsec &> /dev/null; then
echo "tfsec could not be found, please install it first."
exit 1
fi
fi

# Run tfsec
echo "Running tfsec on directory: $DIR_TO_SCAN"
tfsec \
echo "Running tfsec on directory: $dir_to_scan"
tfsec \
--concise-output \
--force-all-dirs \
--exclude-downloaded-modules \
--config-file ../config/tfsec.yaml
"$DIR_TO_SCAN"
--config-file ../config/tfsec.yaml \
"$dir_to_scan"

check-tfsec-status
}

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

# Check the exit status of tfsec
if [ $? -eq 0 ]; then
echo "tfsec completed successfully."
else
echo "tfsec found issues."
if [ $? -eq 0 ]; then
echo "TFSec completed successfully."
else
echo "TFSec found issues."
exit 1
fi
fi
}

# ==============================================================================

main "$@"

exit 0

0 comments on commit d594738

Please sign in to comment.