cmd/vendor-install: handle broken shasum.

If `shasum` is broken and doesn't produce a valid checksum: we were
just doing the checksum comparison with an empty checksum.

Instead, let's treat an empty checksum as a failure condition, rather
than just the executable bit being present for the relevant binary, and
iterate through the available tools to try and get a non-empty checksum.

If they all produce an empty checksum: provide a different error to make
it a bit more obvious what's happening here.
This commit is contained in:
Mike McQuaid 2024-06-04 13:49:32 +01:00
parent 93e58fb41c
commit 2c1bf79c67
No known key found for this signature in database

View File

@ -186,21 +186,33 @@ EOS
if [[ -x "/usr/bin/shasum" ]] if [[ -x "/usr/bin/shasum" ]]
then then
sha="$(/usr/bin/shasum -a 256 "${CACHED_LOCATION}" | cut -d' ' -f1)" sha="$(/usr/bin/shasum -a 256 "${CACHED_LOCATION}" | cut -d' ' -f1)"
elif [[ -x "$(type -P sha256sum)" ]] fi
if [[ -z "${sha}" && -x "$(type -P sha256sum)" ]]
then then
sha="$(sha256sum "${CACHED_LOCATION}" | cut -d' ' -f1)" sha="$(sha256sum "${CACHED_LOCATION}" | cut -d' ' -f1)"
elif [[ -x "$(type -P ruby)" ]] fi
if [[ -z "${sha}" ]]
then then
sha="$( if [[ -x "$(type -P ruby)" ]]
ruby <<EOSCRIPT then
sha="$(
ruby <<EOSCRIPT
require 'digest/sha2' require 'digest/sha2'
digest = Digest::SHA256.new digest = Digest::SHA256.new
File.open('${CACHED_LOCATION}', 'rb') { |f| digest.update(f.read) } File.open('${CACHED_LOCATION}', 'rb') { |f| digest.update(f.read) }
puts digest.hexdigest puts digest.hexdigest
EOSCRIPT EOSCRIPT
)" )"
else else
odie "Cannot verify checksum ('shasum' or 'sha256sum' not found)!" odie "Cannot verify checksum ('shasum', 'sha256sum' and 'ruby' not found)!"
fi
fi
if [[ -z "${sha}" ]]
then
odie "Could not get checksum ('shasum', 'sha256sum' and 'ruby' produced no output)!"
fi fi
if [[ "${sha}" != "${VENDOR_SHA}" ]] if [[ "${sha}" != "${VENDOR_SHA}" ]]