|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# WARNING: Please DO NOT edit this file! It is maintained in the Repository Template (https://github.com/NHSDigital/nhs-notify-repository-template). Raise a PR instead. |
| 4 | + |
| 5 | +set -euo pipefail |
| 6 | + |
| 7 | +# TFSec command wrapper. It will run the command natively if TFSec is |
| 8 | +# installed, otherwise it will run it in a Docker container. |
| 9 | +# Run tfsec for security checks on Terraform code. |
| 10 | +# |
| 11 | +# Usage: |
| 12 | +# $ ./tfsec.sh [directory] |
| 13 | +# ============================================================================== |
| 14 | + |
| 15 | +function main() { |
| 16 | + |
| 17 | + cd "$(git rev-parse --show-toplevel)" |
| 18 | + |
| 19 | + local dir_to_scan=${1:-.} |
| 20 | + |
| 21 | + if command -v tfsec > /dev/null 2>&1 && ! is-arg-true "${FORCE_USE_DOCKER:-false}"; then |
| 22 | + # shellcheck disable=SC2154 |
| 23 | + run-tfsec-natively "$dir_to_scan" |
| 24 | + else |
| 25 | + run-tfsec-in-docker "$dir_to_scan" |
| 26 | + fi |
| 27 | +} |
| 28 | + |
| 29 | +# Run tfsec on the specified directory. |
| 30 | +# Arguments: |
| 31 | +# $1 - Directory to scan |
| 32 | +function run-tfsec-natively() { |
| 33 | + |
| 34 | + local dir_to_scan="$1" |
| 35 | + |
| 36 | + echo "TFSec found locally, running natively" |
| 37 | + |
| 38 | + echo "Running TFSec on directory: $dir_to_scan" |
| 39 | + tfsec \ |
| 40 | + --concise-output \ |
| 41 | + --force-all-dirs \ |
| 42 | + --exclude-downloaded-modules \ |
| 43 | + --config-file scripts/config/tfsec.yaml \ |
| 44 | + --format text \ |
| 45 | + --soft-fail \ |
| 46 | + "$dir_to_scan" |
| 47 | + |
| 48 | + check-tfsec-status |
| 49 | +} |
| 50 | + |
| 51 | +# Check the exit status of tfsec. |
| 52 | +function check-tfsec-status() { |
| 53 | + |
| 54 | + if [ $? -eq 0 ]; then |
| 55 | + echo "TFSec completed successfully." |
| 56 | + else |
| 57 | + echo "TFSec found issues." |
| 58 | + exit 1 |
| 59 | + fi |
| 60 | +} |
| 61 | + |
| 62 | +function run-tfsec-in-docker() { |
| 63 | + |
| 64 | + # shellcheck disable=SC1091 |
| 65 | + source ./scripts/docker/docker.lib.sh |
| 66 | + local dir_to_scan="$1" |
| 67 | + |
| 68 | + # shellcheck disable=SC2155 |
| 69 | + local image=$(name=aquasec/tfsec docker-get-image-version-and-pull) |
| 70 | + # shellcheck disable=SC2086 |
| 71 | + echo "TFSec not found locally, running in Docker Container" |
| 72 | + echo "Running TFSec on directory: $dir_to_scan" |
| 73 | + docker run --rm --platform linux/amd64 \ |
| 74 | + --volume "$PWD":/workdir \ |
| 75 | + --workdir /workdir \ |
| 76 | + "$image" \ |
| 77 | + --concise-output \ |
| 78 | + --force-all-dirs \ |
| 79 | + --exclude-downloaded-modules \ |
| 80 | + --config-file scripts/config/tfsec.yaml \ |
| 81 | + --format text \ |
| 82 | + --soft-fail \ |
| 83 | + "$dir_to_scan" |
| 84 | + check-tfsec-status |
| 85 | +} |
| 86 | +# ============================================================================== |
| 87 | + |
| 88 | +function is-arg-true() { |
| 89 | + |
| 90 | + if [[ "$1" =~ ^(true|yes|y|on|1|TRUE|YES|Y|ON)$ ]]; then |
| 91 | + return 0 |
| 92 | + else |
| 93 | + return 1 |
| 94 | + fi |
| 95 | +} |
| 96 | + |
| 97 | +# ============================================================================== |
| 98 | + |
| 99 | +is-arg-true "${VERBOSE:-false}" && set -x |
| 100 | + |
| 101 | +main "$@" |
| 102 | + |
| 103 | +exit 0 |
0 commit comments