-
Notifications
You must be signed in to change notification settings - Fork 3
/
action.yaml
199 lines (174 loc) · 7.86 KB
/
action.yaml
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
name: "pharos-action"
branding:
icon: "compass"
color: "blue"
description: "GitHub action for running different Security Scans, that should be run before deploying to SKIP"
inputs:
image_url:
description: "An image_url of the form registry/repository:tag or registry/repository@digest"
required: false
trivy:
description: "A boolean which determines whether or not Trivy vulnerability scan will be run. Defaults to true"
required: false
default: "true"
tfsec:
description: "A boolean which determines whether or not TFSec security scan will be run. Defaults to true"
required: false
default: "true"
allow_severity_level:
description: "A string which determines the highest level of severity the security scans can find while still succeeding workflows. Only `medium`, `high` and `critical` are allowed as input strings. Note that these values are case sensitive."
required: false
default: medium
disable_severity_check:
description: "Skip checking severity of scan results. Useful when running the action ad-hoc to update the Github security status."
required: false
default: "false"
trivy_category:
description: "A category for describing the Trivy action. Useful for differentiating between different runs of different images."
required: false
default: "Trivy"
runs:
using: "composite"
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Check If Both Scans Disabled
if: (inputs.trivy == 'false' || inputs.image_url == '') && inputs.tfsec == 'false'
shell: bash
run: |
echo "Error: TFSec is disabled and Trivy was either disabled or an image url was not set. Exiting."
exit 1;
- name: Check If Allowed Severity Level
if: inputs.allow_severity_level != 'medium' && inputs.allow_severity_level != 'high' && inputs.allow_severity_level != 'critical'
shell: bash
env:
ALLOW_SEVERITY_LEVEL: ${{ inputs.allow_severity_level }}
run: |
echo "Error: The input 'allow_severity_level' was not one of the allowed strings, 'high', 'critical' or 'medium'. Found: "$ALLOW_SEVERITY_LEVEL".";
exit 1;
#
# TFSec
#
- name: Run tfsec
id: tfsec
if: inputs.tfsec == 'true'
uses: aquasecurity/tfsec-sarif-action@21ded20e8ca120cd9d3d6ab04ef746477542a608
with:
sarif_file: tfsec.sarif
- name: Upload SARIF file
if: inputs.tfsec == 'true'
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: tfsec.sarif
#
# Trivy
#
- name: Log Into GHCR Registry with Token
if: inputs.trivy == 'true' && inputs.image_url != ''
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ github.token }}
- name: Pull Image to Scan from Docker
if: inputs.trivy == 'true' && inputs.image_url != ''
env:
IMAGE_URL: ${{ inputs.image_url }}
shell: bash
run: docker pull $IMAGE_URL
- name: Run Trivy Vulnerability Scanner on Image
if: inputs.trivy == 'true' && inputs.image_url != ''
uses: aquasecurity/trivy-action@915b19bbe73b92a6cf82a1bc12b087c9a19a5fe2
with:
image-ref: ${{ inputs.image_url }}
format: sarif
output: trivy-results.sarif
severity: UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL
timeout: 15m
- name: Upload Trivy Scan Results to GitHub Security Tab
if: inputs.trivy == 'true' && inputs.image_url != ''
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: trivy-results.sarif
category: ${{ inputs.trivy_category }}
#
# Check results
#
- name: Set high and critical severity outputs
if: inputs.disable_severity_check != 'true'
id: severity_check_outputs
shell: bash
env:
GITHUB_REPOSITORY: ${{ github.repository }}
GITHUB_REF: ${{ github.ref }}
GITHUB_TOKEN: ${{ github.token }}
run: |
content_header="Accept: application/vnd.github+json"
auth_header="Authorization: Bearer $GITHUB_TOKEN"
base_url="https://api.github.com/repos/$GITHUB_REPOSITORY/code-scanning/alerts?ref=$GITHUB_REF&state=open"
result_severity_check_high=$(curl -s -H "$content_header" -H "$auth_header" "${base_url}&severity=high")
result_severity_check_critical=$(curl -s -H "$content_header" -H "$auth_header" "${base_url}&severity=critical")
result_severity_check_error=$(curl -s -H "$content_header" -H "$auth_header" "${base_url}&severity=error")
is_high_vuln_present=$([[ "$result_severity_check_high" =~ [a-zA-Z] ]] && echo "true" || echo "false")
is_critical_vuln_present=$([[ "$result_severity_check_critical" =~ [a-zA-Z] || "$result_severity_check_error" =~ [a-zA-Z] ]] && echo "true" || echo "false")
echo "is_high_vuln_present=$is_high_vuln_present" >> $GITHUB_OUTPUT
echo "is_critical_vuln_present=$is_critical_vuln_present" >> $GITHUB_OUTPUT
- name: Succeed or fail based on severity
if: inputs.disable_severity_check != 'true'
id: severity_check
shell: bash
env:
REF_NAME: ${{ github.ref_name }}
ALLOW_SEVERITY_LEVEL: ${{ inputs.allow_severity_level }}
run: |
pr_number=$( echo $REF_NAME | sed 's/\/.*//');
is_high_vuln_present=${{ steps.severity_check_outputs.outputs.is_high_vuln_present }}
is_critical_vuln_present=${{ steps.severity_check_outputs.outputs.is_critical_vuln_present }}
error_start_message="Error: Vulnerabilities were found of level"
error_end_message="Go to the Code Scanning section of the GitHub Security tab to review these vulnerabilities."
error_search_pr_message="Search for is:open pr:"$pr_number" to find PR related vulnerabilities."
_message="Undefined. Bug in this action. Fix."
_exit_code=0
if [[ $is_high_vuln_present == 'false' && $is_critical_vuln_present == 'false' ]]
then
_message="Success! No high or critical code scanning alerts."
_exit_code=0;
else
if [[ $ALLOW_SEVERITY_LEVEL == 'medium' ]]
then
if [[ ${{ github.event_name }} == 'pull_request' ]]
then
_message="$error_start_message high or critical. $error_end_message $error_search_pr_message";
_exit_code=1
else
_message="$error_start_message high or critical found on branch '$REF_NAME'. $error_end_message";
_exit_code=1;
fi
elif [[ $ALLOW_SEVERITY_LEVEL == 'high' ]]
then
if [[ $is_critical_vuln_present == 'false' ]]
then
_message="Only high vulnerabilities detected! Allowing due to input ALLOW_SEVERITY_LEVEL being set to high.";
_exit_code=0;
fi
if [[ ${{ github.event_name }} == 'pull_request' ]]
then
_message="$error_start_message critical. $error_end_message $error_search_pr_message";
_exit_code=1
else
_message="$error_start_message critical found on '$REF_NAME' branch. $error_end_message";
_exit_code=1
fi
elif [[ $ALLOW_SEVERITY_LEVEL == 'critical' ]]
then
_message="High or critical vulnerabilities detected! Allowing due to input ALLOW_SEVERITY_LEVEL being set to critical.";
_exit_code=0;
else
_message="Input 'ALLOW_SEVERITY_LEVEL' was not one of the known values, found '$ALLOW_SEVERITY_LEVEL'. If you see this message, please contact SKIP.";
_exit_code=1
fi
fi
[[ ${_exit_code} -gt 0 ]] && _summary_prefix=":x: " || _summary_prefix=""
echo "${_message}";
echo "${_summary_prefix}${_message}" >> $GITHUB_STEP_SUMMARY;
exit ${_exit_code};