|
4 | 4 | # Unauthorized copying of this file, via any medium is strictly prohibited |
5 | 5 | # without the express written consent of Huntress Labs, Inc. |
6 | 6 |
|
7 | | -set -euo pipefail |
| 7 | +set -uo pipefail |
8 | 8 |
|
9 | 9 | declare ACCOUNT_KEY= |
10 | 10 | declare ORG_KEY= |
@@ -104,27 +104,38 @@ get_user_creds() { |
104 | 104 |
|
105 | 105 | # download latest Huntress package from S3 |
106 | 106 | 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 | + |
108 | 111 | # 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=$? |
115 | 119 | fi |
116 | 120 |
|
117 | | - if [ $? != 0 ]; then |
| 121 | + if [ "$exit_code" != 0 ] || [ "$status_code" != "200" ]; then |
| 122 | + # provide helpful failure message |
118 | 123 | if [ "$status_code" = "400" ]; then |
119 | 124 | die "Account Key not valid." |
120 | 125 | elif [ "$status_code" = "404" ]; then |
121 | 126 | die "File not found on S3." |
122 | 127 | elif [ "$status_code" = "409" ]; then |
123 | 128 | die "The Linux Beta has not been enabled for this account." |
124 | 129 | fi |
125 | | - elif ! [ -f "$HUNTRESS_PKG" ]; then |
| 130 | + |
| 131 | + die "Failed to download installation package" |
| 132 | + fi |
| 133 | + |
| 134 | + if ! [ -f "$HUNTRESS_PKG" ]; then |
126 | 135 | die "File download failed." |
127 | 136 | fi |
| 137 | + |
| 138 | + return 0 |
128 | 139 | } |
129 | 140 |
|
130 | 141 | # determine arch type |
@@ -158,6 +169,25 @@ validate_package() { |
158 | 169 | fi |
159 | 170 | } |
160 | 171 |
|
| 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 | + |
161 | 191 | # Check minimum requirements |
162 | 192 | validate_requirements() { |
163 | 193 | log_info "[+] Validating requirements" |
@@ -188,11 +218,6 @@ validate_requirements() { |
188 | 218 | die "REQUIREMENT FAILURE: curl or wget needs to be installed" |
189 | 219 | fi |
190 | 220 |
|
191 | | - test_url() { |
192 | | - if ! curl -s -o /dev/null "$1"; then |
193 | | - die "CONNECTION FAILURE: Unable to reach $1" |
194 | | - fi |
195 | | - } |
196 | 221 | test_url "https://huntress.io" |
197 | 222 | test_url "https://s3.amazonaws.com" |
198 | 223 | test_url "https://huntresscdn.com" |
|
0 commit comments