Skip to content

Commit 92698c5

Browse files
authored
Merge pull request #39 from mbainter/feature/verify_downloads
add support for verifying downloads of terraform
2 parents 3ef5384 + 1f1ccd3 commit 92698c5

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ $ tfenv install latest
4747
$ tfenv install latest:^0.8
4848
```
4949

50+
If shasum is present in the path, tfenv will verify the download against Hashicorp's published sha256 hash. If [keybase](https://keybase.io/) is available in the path it will also verify the signature for those published hashes using hashicorp's published public key.
51+
5052
If you use [.terraform-version](#terraform-version), `tfenv install` (no argument) will install the version written in it.
5153

5254
### tfenv use

libexec/tfenv-install

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ declare version_requested version regex
1313

1414
if [ -z "${1}" ]; then
1515
version_file="$(tfenv-version-file)"
16-
if [ "${version_file}" != "${TFENV_ROOT}/version" ];then
16+
if [ "${version_file}" != "${TFENV_ROOT}/version" ]; then
1717
version_requested="$(cat ${version_file} || true)"
1818
fi
1919
else
@@ -36,7 +36,7 @@ version="$(tfenv-list-remote | grep -e "${regex}" | head -n 1)"
3636
[ -n "${version}" ] || error_and_die "No versions matching '${1}' found in remote"
3737

3838
dst_path="${TFENV_ROOT}/versions/${version}"
39-
if [ -f ${dst_path}/terraform ];then
39+
if [ -f ${dst_path}/terraform ]; then
4040
echo "Terraform v${version} is already installed"
4141
exit 0
4242
fi
@@ -52,11 +52,40 @@ MINGW64* )
5252
os="linux_amd64"
5353
esac
5454

55+
keybase=$(which keybase)
56+
shasum=$(which shasum)
57+
58+
if [[ -n $keybase && -x "$keybase" ]]; then
59+
if ! $keybase list-following | fgrep -q hashicorp; then
60+
echo "NOTICE: Following 'hashicorp' with keybase will make this process smoother."
61+
fi
62+
fi
63+
64+
version_url="https://releases.hashicorp.com/terraform/${version}"
5565
tarball_name="terraform_${version}_${os}.zip"
56-
tarball_url="https://releases.hashicorp.com/terraform/${version}/${tarball_name}"
66+
shasums_name="terraform_${version}_SHA256SUMS"
5767
echo "Installing Terraform v${version}"
58-
echo "Downloading release tarball from ${tarball_url}"
59-
curl --tlsv1.2 -f -o /tmp/${tarball_name} "${tarball_url}" || error_and_die "Tarball download failed"
68+
echo "Downloading release tarball from ${version_url}/${tarball_name}"
69+
curl --tlsv1.2 -f -o /tmp/${tarball_name} "${version_url}/${tarball_name}" || error_and_die "Tarball download failed"
70+
echo "Downloading SHA hash file from ${version_url}/${sha256sums}"
71+
curl -s --tlsv1.2 -f -o /tmp/${shasums_name} "${version_url}/${shasums_name}" || error_and_die "SHA256 hashes download failed"
72+
73+
if [[ -n $keybase && -x "$keybase" ]]; then
74+
echo "Downloading SHA hash signature file from ${version_url}/${sha256sums}.sig"
75+
curl -s --tlsv1.2 -f -o /tmp/${shasums_name}.sig "${version_url}/${shasums_name}.sig" || error_and_die "SHA256SUMS signature download failed"
76+
${keybase} pgp verify -S hashicorp -d "/tmp/${shasums_name}.sig" -i "/tmp/${shasums_name}" || error_and_die "SHA256SUMS signature does not match!"
77+
else
78+
echo "No keybase install found, skipping SHA hash file validation..."
79+
fi
80+
81+
if [[ -n $shasum && -x $shasum ]]; then
82+
pushd /tmp >/dev/null
83+
${shasum} -a 256 -s -c <(fgrep ${tarball_name} /tmp/${shasums_name}) || error_and_die "SHA256 hash does not match!"
84+
popd >/dev/null
85+
else
86+
echo "No shasum tool for validating the SHA256 hash was found, skipping download validation..."
87+
fi
88+
6089
mkdir -p ${dst_path} || error_and_die "Failed to make directory ${dst_path}"
6190
unzip /tmp/${tarball_name} -d ${dst_path} || error_and_die "Tarball unzip failed"
6291
echo -e "\033[0;32mInstallation of terraform v${version} successful\033[0;39m"

0 commit comments

Comments
 (0)