-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrivy-report.sh
executable file
·243 lines (191 loc) · 9.14 KB
/
trivy-report.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/bin/bash
set -e
# Colorize terminal
red='\e[0;31m'
no_color='\033[0m'
# Console step increment
i=1
# Declare script helper
TEXT_HELPER="\nThis script aims to build a markdown style vulnerability report for the trivy json scan results. It will read the files and then build a markdown summary (compatible with Github issue) summarising the vulnerabilities for each image and configuration file for the given input folder.
Following flags are available:
-i Input folder that contain trivy json scan result files into subfolders 'images/' and 'configs/'.
Please provide the path without last slash (i.e not like this 'path/to/folder/').
-o Output file for the generated markdown report.
Please provide the path with the markdown file extension (i.e '.md').
-p Github project (format as '<owner>/<repo_name>').
It can be set in GitHub Actions as '\${{ github.repository }}'.
-r Github actions run ID.
It can be set in GitHub Actions as '\${{ github.run_id }}'.
-h Print script help.\n\n"
print_help() {
printf "$TEXT_HELPER"
}
# Parse options
while getopts hi:o:p:r: flag; do
case "${flag}" in
i)
INPUT="${OPTARG}";;
o)
OUTPUT="${OPTARG}";;
p)
REPO="${OPTARG}";;
r)
RUN_ID="${OPTARG}";;
h | *)
print_help
exit 0;;
esac
done
NOW="$(date +'%Y-%m-%d at %H:%M')"
ARTIFACT_DOWNLOAD_URL="https://github.com/${REPO}/actions/runs/${RUN_ID}"
if [ -z "$INPUT" ] || [ -z "$OUTPUT" ] || [ -z "$REPO" ] || [ -z "$RUN_ID" ]; then
echo "Argument(s) missing, you should set all paramters."
print_help
exit 0
fi
_jq() {
echo ${1} | base64 --decode | jq -r ${2}
}
printf "\n${red}${i}.${no_color} Build vulnerability report\n"
i=$(($i + 1))
VULNERABILITY_REPORT_BODY="# Security report
:robot: This report was generated automatically the $NOW.
It summarizes the result of a vulnerability scan of Trivy for each docker image and configuration file.
You should take a look at it and possibly modify some dependencies to fix vulnerabilities or update your configuration files."
#--------------------#
# Images scan report #
#--------------------#
if [[ -n $(find $INPUT/images/*) ]]; then
FORMATED_REPORT=$(jq -s '.' $INPUT/images/* | jq '[ .[] | {
name: .ArtifactName,
type: .ArtifactType,
results: [
.Results[] | select(.Vulnerabilities != null) | {
class: .Class,
target: .Target,
type: .Type,
length: .Vulnerabilities | (if length == 0 then "-" else length end),
vulnerabilityType: {
critical: [.Vulnerabilities[] | select(.Severity == "CRITICAL")] | (if length == 0 then "-" else length end),
high: [.Vulnerabilities[] | select(.Severity == "HIGH")] | (if length == 0 then "-" else length end),
medium: [.Vulnerabilities[] | select(.Severity == "MEDIUM")] | (if length == 0 then "-" else length end),
low: [.Vulnerabilities[] | select(.Severity == "LOW")] | (if length == 0 then "-" else length end),
unknown: [.Vulnerabilities[] | select(.Severity == "UNKNOWN")] | (if length == 0 then "-" else length end)
}
}
]
} ]')
VULNERABILITY_REPORT_BODY="$VULNERABILITY_REPORT_BODY
## Images"
for row in $(echo "${FORMATED_REPORT}" | jq -r '.[] | @base64'); do
IMAGE_NAME=$(echo "$(_jq "${row}" '.')" | jq -r '.name')
OS_TOTAL_COUNT=$(echo "$(_jq "${row}" '.')" | jq -r '[ .results[] | select(.class == "os-pkgs") | .length ] | add')
OS_CRITICAL_COUNT=$(echo "$(_jq "${row}" '.')" | jq -r '[ .results[] | select(.class == "os-pkgs") | .vulnerabilityType.critical ] | add')
OS_HIGH_COUNT=$(echo "$(_jq "${row}" '.')" | jq -r '[ .results[] | select(.class == "os-pkgs") | .vulnerabilityType.high ] | add')
OS_MEDIUM_COUNT=$(echo "$(_jq "${row}" '.')" | jq -r '[ .results[] | select(.class == "os-pkgs") | .vulnerabilityType.medium ] | add')
OS_LOW_COUNT=$(echo "$(_jq "${row}" '.')" | jq -r '[ .results[] | select(.class == "os-pkgs") | .vulnerabilityType.low ] | add')
OS_UNKNOWN_COUNT=$(echo "$(_jq "${row}" '.')" | jq -r '[ .results[] | select(.class == "os-pkgs") | .vulnerabilityType.unknown ] | add')
if [[ $OS_TOTAL_COUNT = 'null' ]]; then
IMAGE_SCAN_SUMMARY="
:white_check_mark: No security issues were detected.
"
else
IMAGE_SCAN_SUMMARY="
| Critical | High | Medium | Low | Unknown | Total |
|:---------------:|:-----------:|:-------------:|:----------:|:--------------:|:-----------:|
| $OS_CRITICAL_COUNT | $OS_HIGH_COUNT | $OS_MEDIUM_COUNT | $OS_LOW_COUNT | $OS_UNKNOWN_COUNT | $OS_TOTAL_COUNT|
"
fi
VULNERABILITY_REPORT_BODY="$VULNERABILITY_REPORT_BODY
### \`$IMAGE_NAME\`
**OS Packages**
$IMAGE_SCAN_SUMMARY"
DEP_TOTAL_COUNT=$(echo "$(_jq "${row}" '.')" | jq -r '[ .results[] | select(.class == "lang-pkgs") | .length ] | add')
if [[ $DEP_TOTAL_COUNT = 'null' ]]; then
SCAN_SUMMARY="
:white_check_mark: No security issues were detected.
"
else
SCAN_SUMMARY="
| Target | Critical | High | Medium | Low | Unknown | Total |
|:-----------|:---------------:|:-----------:|:-------------:|:----------:|:--------------:|:-----------:|"
for row2 in $(echo "$(_jq ${row} '.')" | jq -r '.results[] | select(.class == "lang-pkgs") | @base64'); do
FULL_NAME=$(echo "$(_jq ${row2} '.')" | jq -r '.target')
TOTAL_COUNT=$(echo "$(_jq ${row2} '.')" | jq -r '.length')
CRITICAL_COUNT=$(echo "$(_jq ${row2} '.')" | jq -r '[ .vulnerabilityType.critical ] | add')
HIGH_COUNT=$(echo "$(_jq ${row2} '.')" | jq -r '[ .vulnerabilityType.high ] | add')
MEDIUM_COUNT=$(echo "$(_jq ${row2} '.')" | jq -r '[ .vulnerabilityType.medium ] | add')
LOW_COUNT=$(echo "$(_jq ${row2} '.')" | jq -r '[ .vulnerabilityType.low ] | add')
UNKNOWN_COUNT=$(echo "$(_jq ${row2} '.')" | jq -r '[ .vulnerabilityType.unknown ] | add')
SCAN_SUMMARY="$SCAN_SUMMARY
| $FULL_NAME | $CRITICAL_COUNT | $HIGH_COUNT | $MEDIUM_COUNT | $LOW_COUNT | $UNKNOWN_COUNT | $TOTAL_COUNT |"
done
fi
VULNERABILITY_REPORT_BODY="$VULNERABILITY_REPORT_BODY
**Language-specific packages**
$SCAN_SUMMARY"
done
SCAN_IMAGES=true
fi
#--------------------#
# Config scan report #
#--------------------#
if [[ -n $(find $INPUT/configs/*) ]]; then
FORMATED_REPORT=$(jq -s '.' $INPUT/configs/* | jq '.[] | {
name: .ArtifactName,
type: .ArtifactType,
results: [
.Results[] | select(.Misconfigurations != null) | {
class: .Class,
target: .Target,
type: .Type,
length: .Misconfigurations | (if length == 0 then "-" else length end),
vulnerabilityType: {
critical: [.Misconfigurations[] | select(.Severity == "CRITICAL")] | (if length == 0 then "-" else length end),
high: [.Misconfigurations[] | select(.Severity == "HIGH")] | (if length == 0 then "-" else length end),
medium: [.Misconfigurations[] | select(.Severity == "MEDIUM")] | (if length == 0 then "-" else length end),
low: [.Misconfigurations[] | select(.Severity == "LOW")] | (if length == 0 then "-" else length end),
unknown: [.Misconfigurations[] | select(.Severity == "UNKNOWN")] | (if length == 0 then "-" else length end)
}
}
]
}')
VULNERABILITY_REPORT_BODY="$VULNERABILITY_REPORT_BODY
## Configs"
TOTAL_CONFIG=$(echo $FORMATED_REPORT | jq -r '[ .results[].length ] | add')
if [[ $TOTAL_CONFIG = 'null' ]]; then
SCAN_SUMMARY="
:white_check_mark: No security issues were detected.
"
else
SCAN_SUMMARY="
| Target | Critical | High | Medium | Low | Unknown | Total |
|:-----------|:---------------:|:-----------:|:-------------:|:----------:|:--------------:|:-----------:|"
fi
for row in $(echo "${FORMATED_REPORT}" | jq -r '.results[] | @base64'); do
FULL_NAME=$(echo "$(_jq "${row}" '.')" | jq -r '.target')
TOTAL_COUNT=$(echo "$(_jq "${row}" '.')" | jq -r '.length')
CRITICAL_COUNT=$(echo "$(_jq "${row}" '.')" | jq -r '[ .vulnerabilityType.critical ] | add')
HIGH_COUNT=$(echo "$(_jq "${row}" '.')" | jq -r '[ .vulnerabilityType.high ] | add')
MEDIUM_COUNT=$(echo "$(_jq "${row}" '.')" | jq -r '[ .vulnerabilityType.medium ] | add')
LOW_COUNT=$(echo "$(_jq "${row}" '.')" | jq -r '[ .vulnerabilityType.low ] | add')
UNKNOWN_COUNT=$(echo "$(_jq "${row}" '.')" | jq -r '[ .vulnerabilityType.unknown ] | add')
SCAN_SUMMARY="$SCAN_SUMMARY
| $FULL_NAME | $CRITICAL_COUNT | $HIGH_COUNT | $MEDIUM_COUNT | $LOW_COUNT | $UNKNOWN_COUNT | $TOTAL_COUNT|"
done
VULNERABILITY_REPORT_BODY="$VULNERABILITY_REPORT_BODY
$SCAN_SUMMARY"
SCAN_CONFIG=true
fi
if [ "$SCAN_DEPENDENCIES" = true ] || [ "$SCAN_CONFIG" = true ] || [ "$SCAN_IMAGES" = true ]; then
printf "\n${red}${i}.${no_color} Add artifacts download url to the vulnerability report body\n"
i=$(($i + 1))
VULNERABILITY_REPORT_BODY=$(cat <<EOF
$VULNERABILITY_REPORT_BODY
## Download report
Click [here]($ARTIFACT_DOWNLOAD_URL) to download the full vulnerability report with all (detailed) json files.
EOF
)
printf "\nVulnerability report preview :\n\n$VULNERABILITY_REPORT_BODY\n"
echo "$VULNERABILITY_REPORT_BODY" > "$OUTPUT"
fi