Skip to content

Commit 06eed78

Browse files
authored
Merge pull request #147 from huntresslabs/feature/sc-169744-linux-install-script-silently-fails-if-tmp
don't fail fast (-e) handle errors
2 parents 1c32e37 + a662a31 commit 06eed78

1 file changed

Lines changed: 40 additions & 15 deletions

File tree

Bash/linux/huntress-linux-install.sh

100644100755
Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Unauthorized copying of this file, via any medium is strictly prohibited
55
# without the express written consent of Huntress Labs, Inc.
66

7-
set -euo pipefail
7+
set -uo pipefail
88

99
declare ACCOUNT_KEY=
1010
declare ORG_KEY=
@@ -104,27 +104,38 @@ get_user_creds() {
104104

105105
# download latest Huntress package from S3
106106
download_latest() {
107-
status_code="0"
107+
local url="${PORTAL_URL}/download/linux/${ACCOUNT_KEY}?arch=${ARCH}"
108+
local status_code="0"
109+
local exit_code=0
110+
108111
# If neither wget or curl exists on the system, then we fail at the previous validation step
109-
if [ "$WGET_INSTALLED" = true ]; then
110-
status_code=$(wget -S -pO "${HUNTRESS_PKG}" "${PORTAL_URL}/download/linux/${ACCOUNT_KEY}?arch=${ARCH}" 2>&1 \
111-
| grep "HTTP/" | awk '{print $2}')
112-
else
113-
status_code=$(curl -f -L -o "${HUNTRESS_PKG}" -w "%{http_code}" \
114-
"${PORTAL_URL}/download/linux/${ACCOUNT_KEY}?arch=${ARCH}")
112+
if [ "$CURL_INSTALLED" = true ]; then
113+
status_code=$(curl -f -L -o "${HUNTRESS_PKG}" -w "%{http_code}" "${url}")
114+
exit_code=$?
115+
elif [ "$WGET_INSTALLED" = true ]; then
116+
# if there is a redirect, get the last http response code
117+
status_code=$(wget -S -pO "${HUNTRESS_PKG}" "${url}" 2>&1 | grep "HTTP/" | awk '{print $2}' | tail -n 1 | tr -d '[:space:]' )
118+
exit_code=$?
115119
fi
116120

117-
if [ $? != 0 ]; then
121+
if [ "$exit_code" != 0 ] || [ "$status_code" != "200" ]; then
122+
# provide helpful failure message
118123
if [ "$status_code" = "400" ]; then
119124
die "Account Key not valid."
120125
elif [ "$status_code" = "404" ]; then
121126
die "File not found on S3."
122127
elif [ "$status_code" = "409" ]; then
123128
die "The Linux Beta has not been enabled for this account."
124129
fi
125-
elif ! [ -f "$HUNTRESS_PKG" ]; then
130+
131+
die "Failed to download installation package"
132+
fi
133+
134+
if ! [ -f "$HUNTRESS_PKG" ]; then
126135
die "File download failed."
127136
fi
137+
138+
return 0
128139
}
129140

130141
# determine arch type
@@ -158,6 +169,25 @@ validate_package() {
158169
fi
159170
}
160171

172+
test_url() {
173+
# use curl if installed
174+
if [ "$CURL_INSTALLED" = true ]; then
175+
if curl -s -o /dev/null "$1"; then
176+
return 0 # success
177+
fi
178+
elif [ "$WGET_INSTALLED" = true ]; then
179+
wget --spider --quiet "$1" || local exit_code=$?
180+
exit_code=${exit_code:-0}
181+
182+
# return code 8 connection succeeded, but the server returned a non-200 status
183+
if [ "$exit_code" -eq 0 ] || [ "$exit_code" -eq 8 ]; then
184+
return 0 # success
185+
fi
186+
fi
187+
188+
die "CONNECTION FAILURE: Unable to reach $1"
189+
}
190+
161191
# Check minimum requirements
162192
validate_requirements() {
163193
log_info "[+] Validating requirements"
@@ -188,11 +218,6 @@ validate_requirements() {
188218
die "REQUIREMENT FAILURE: curl or wget needs to be installed"
189219
fi
190220

191-
test_url() {
192-
if ! curl -s -o /dev/null "$1"; then
193-
die "CONNECTION FAILURE: Unable to reach $1"
194-
fi
195-
}
196221
test_url "https://huntress.io"
197222
test_url "https://s3.amazonaws.com"
198223
test_url "https://huntresscdn.com"

0 commit comments

Comments
 (0)