forked from triat/terraform-security-scan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
executable file
·76 lines (63 loc) · 2.18 KB
/
entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/bash
# determine if failing quietly in case of findings
if [ "${INPUT_TFSEC_SOFT_FAIL}" == "true" ]; then
TFSEC_SOFT_FAIL="--soft-fail"
else
TFSEC_SOFT_FAIL=""
fi
#Select the output format
if [ "${TFSEC_OUTPUT_FILE}" != "" ]; then
TFSEC_FORMAT="-f ${TFSEC_OUTPUT_FILE}"
else
TFSEC_FORMAT=""
fi
#select the output file
if [ "${TFSEC_OUTPUT_FILE}" != "" ]; then
TFSEC_FILE="--out ${TFSEC_OUTPUT_FILE}"
else
TFSEC_FILE=""
fi
# Comment on the pull request if necessary.
if [ "${INPUT_TFSEC_ACTIONS_WORKING_DIR}" != "" ] && [ "${INPUT_TFSEC_ACTIONS_WORKING_DIR}" != "." ]; then
TFSEC_WORKING_DIR="/github/workspace/${INPUT_TFSEC_ACTIONS_WORKING_DIR}"
else
TFSEC_WORKING_DIR="/github/workspace/"
fi
# grab tfsec from GitHub (taken from README.md)
if [[ -n "$INPUT_TFSEC_VERSION" ]]; then
env GO111MODULE=on go install github.com/tfsec/tfsec/cmd/tfsec@"${INPUT_TFSEC_VERSION}"
else
env GO111MODULE=on go get -u github.com/tfsec/tfsec/cmd/tfsec
fi
if [[ -n "$INPUT_TFSEC_EXCLUDE" ]]; then
TFSEC_OUTPUT=$(/go/bin/tfsec ${TFSEC_WORKING_DIR} --no-colour "${TFSEC_SOFT_FAIL}" -e "${INPUT_TFSEC_EXCLUDE}" "${TFSEC_FORMAT}" "${TFSEC_FILE}")
else
TFSEC_OUTPUT=$(/go/bin/tfsec ${TFSEC_WORKING_DIR} --no-colour "${TFSEC_SOFT_FAIL}" "${TFSEC_FORMAT}" "${TFSEC_FILE}")
fi
TFSEC_EXITCODE=${?}
# Exit code of 0 indicates success.
if [ ${TFSEC_EXITCODE} -eq 0 ]; then
TFSEC_STATUS="Success"
else
TFSEC_STATUS="Failed"
fi
# Print output.
echo "${TFSEC_OUTPUT}"
# Comment on the pull request if necessary.
if [ "${INPUT_TFSEC_ACTIONS_COMMENT}" == "1" ] || [ "${INPUT_TFSEC_ACTIONS_COMMENT}" == "true" ]; then
TFSEC_COMMENT=1
else
TFSEC_COMMENT=0
fi
if [ "${GITHUB_EVENT_NAME}" == "pull_request" ] && [ -n "${GITHUB_TOKEN}" ] && [ "${TFSEC_COMMENT}" == "1" ]; then
COMMENT="#### \`Terraform Security Scan\` ${TFSEC_STATUS}
<details><summary>Show Output</summary>
\`\`\`hcl
${TFSEC_OUTPUT}
\`\`\`
</details>"
PAYLOAD=$(echo "${COMMENT}" | jq -R --slurp '{body: .}')
URL=$(jq -r .pull_request.comments_url "${GITHUB_EVENT_PATH}")
echo "${PAYLOAD}" | curl -s -S -H "Authorization: token ${GITHUB_TOKEN}" --header "Content-Type: application/json" --data @- "${URL}" > /dev/null
fi
exit $TFSEC_EXITCODE