This repository has been archived by the owner on Jan 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
assess-image-risk.sh
executable file
·161 lines (137 loc) · 4.4 KB
/
assess-image-risk.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
#!/usr/bin/env bash
ts() {
date "+%Y-%m-%d %H:%M:%S"
}
echo_if_verbose() {
if [ "1" -eq "${VERBOSE:-0}" ]; then
echo "$@"
fi
return 0
}
#
# parse command line arguments
#
VERBOSE=0
LOG_LEVEL=ERROR
CLAIR_API_PORT=6060
VULNERABILITY_WHITELIST='{"ignoreSevertiesAtOrBelow": "medium"}'
while true
do
case "$(echo "${1:-}" | tr "[:upper:]" "[:lower:]")" in
-s)
shift
VERBOSE=0
LOG_LEVEL=ERROR
;;
-v)
shift
VERBOSE=1
LOG_LEVEL=INFO
;;
-vv)
shift
VERBOSE=1
LOG_LEVEL=DEBUG
;;
-wl|--whitelist)
shift
VULNERABILITY_WHITELIST=${1:-}
shift
;;
--api-port)
shift
CLAIR_API_PORT=${1:-}
shift
;;
*)
break
;;
esac
done
if [ $# != 1 ]; then
echo "usage: $(basename "$0") [-s|-v|-vv] [--api-port <port>] <docker image>" >&2
exit 1
fi
DOCKER_IMAGE_TO_ANALYZE=${1:-}
#
# save the docker image to be analyzed to a tar archive
# and determine which layers are in the image
#
DOCKER_IMAGE_EXPLODED_TAR_DIR=$(mktemp -d 2> /dev/null || mktemp -d -t DAS)
echo_if_verbose "$(ts) saving docker image '${DOCKER_IMAGE_TO_ANALYZE}' to '${DOCKER_IMAGE_EXPLODED_TAR_DIR}'"
pushd "${DOCKER_IMAGE_EXPLODED_TAR_DIR}" > /dev/null || echo "only here to avoid shellcheck's SC2164"
docker save "${DOCKER_IMAGE_TO_ANALYZE}" | tar xv > /dev/null
popd > /dev/null || echo "only here to avoid shellcheck's SC2164"
LAYERS=$(jq ".[0].Layers[]" < "${DOCKER_IMAGE_EXPLODED_TAR_DIR}/manifest.json" | sed -e 's|"||g' | sed -e 's|/layer.tar$||g')
echo_if_verbose "$(ts) successfully saved docker image '${DOCKER_IMAGE_TO_ANALYZE}'"
#
# Iterate through each layer in the saved image and use
# Clair's RESTful API to analyze each layer.
#
# References
# -- https://coreos.com/clair/docs/latest/api_v1.html#layers
#
echo_if_verbose "$(ts) starting to create clair layers"
PREVIOUS_LAYER=""
for LAYER in $LAYERS
do
echo_if_verbose "$(ts) creating clair layer '${LAYER}'"
BODY=$(mktemp 2> /dev/null || mktemp -t DAS)
if [ "${PREVIOUS_LAYER}" == "" ]; then
echo "{\"Layer\": {\"Name\": \"${LAYER}\", \"Path\": \"${DOCKER_IMAGE_EXPLODED_TAR_DIR}/${LAYER}/layer.tar\", \"Format\": \"Docker\"}}" > "${BODY}"
else
echo "{\"Layer\": {\"Name\": \"${LAYER}\", \"Path\": \"${DOCKER_IMAGE_EXPLODED_TAR_DIR}/${LAYER}/layer.tar\", \"ParentName\": \"${PREVIOUS_LAYER}\", \"Format\": \"Docker\"}}" > "${BODY}"
fi
if ! HTTP_STATUS_CODE=$(curl \
-s \
-X POST \
-H 'Content-Type: application/json' \
--data-binary @"${BODY}" \
--write-out "%{http_code}" \
--silent \
--output /dev/null \
"http://127.0.0.1:${CLAIR_API_PORT}/v1/layers") \
|| \
[ "${HTTP_STATUS_CODE}" != "201" ];
then
echo "$(ts) error creating clair layer '${LAYER}'" >&2
exit 1
fi
echo_if_verbose "$(ts) successfully created clair layer '${LAYER}'"
PREVIOUS_LAYER=${LAYER}
done
echo_if_verbose "$(ts) done creating clair layers"
#
# Iterate through each layer in the saved image and use Clair's
# RESTful API to get the vulnerabilities for each layer.
#
# References
# -- https://coreos.com/clair/docs/latest/api_v1.html#get-layersname
#
echo_if_verbose "$(ts) starting to get vulnerabilities for clair layers"
VULNERABILTIES_DIR=$(mktemp -d 2> /dev/null || mktemp -d -t DAS)
echo_if_verbose "$(ts) saving vulnerabilities to directory '${VULNERABILTIES_DIR}'"
for LAYER in ${LAYERS}
do
echo_if_verbose "$(ts) getting vulnerabilities for layer '${LAYER}'"
if ! HTTP_STATUS_CODE=$(curl \
-s \
-o "${VULNERABILTIES_DIR}/${LAYER}.json" \
--write-out "%{http_code}" \
--silent \
--output /dev/null \
"http://127.0.0.1:${CLAIR_API_PORT}/v1/layers/${LAYER}?vulnerabilities") \
|| \
[ "${HTTP_STATUS_CODE}" != "200" ];
then
echo "$(ts) error getting vulnerabilities for layer '${LAYER}'" >&2
exit 1
fi
echo_if_verbose "$(ts) successfully got vulnerabilities for layer '${LAYER}'"
done
echo_if_verbose "$(ts) done getting vulnerabilities for clair layers"
#
# ...
#
assess-vulnerabilities-risk.py "--whitelist=${VULNERABILITY_WHITELIST}" "--log=${LOG_LEVEL}" "${VULNERABILTIES_DIR}"
exit $?