|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +VERSION=${VERSION:-"3.2.0"} |
| 5 | +# Expected SHA256 checksums taken from https://github.com/aws-cloudformation/cloudformation-guard/releases/tag/3.2.0 |
| 6 | +# When we change gitleaks versions, these must be changed |
| 7 | +sha256sum_expected_arm="sha256:d562e14831794a4859782f5609186970373e8e0a049fbded2c01612d2dcdb087" |
| 8 | +sha256sum_expected_amd64="sha256:9f8c4d9f15f7dd54a37ea70a5237ba00aba682fb1e6521a744d12259961dfc13" |
| 9 | + |
| 10 | + |
| 11 | +# Checks if packages are installed and installs them if not |
| 12 | +check_packages() { |
| 13 | + if ! dpkg -s "$@" > /dev/null 2>&1; then |
| 14 | + sudo apt-get -y install --no-install-recommends "$@" |
| 15 | + fi |
| 16 | +} |
| 17 | + |
| 18 | +check_packages curl ca-certificates tar |
| 19 | + |
| 20 | +install() { |
| 21 | + tmp_dir="$(mktemp -d)" |
| 22 | + trap 'rm -rf "${tmp_dir}"' EXIT |
| 23 | + |
| 24 | + download_file="${tmp_dir}/gitleaks.tar.gz" |
| 25 | + |
| 26 | + if [ "$TARGETARCH" = "arm64" ] || [ "$TARGETARCH" == "aarch64" ]; then |
| 27 | + download_url="https://github.com/aws-cloudformation/cloudformation-guard/releases/download/${VERSION}/cfn-guard-v3-aarch64-ubuntu-latest.tar.gz" |
| 28 | + arch_type="aarch64" |
| 29 | + sha256sum_expected="${sha256sum_expected_arm}" |
| 30 | + else |
| 31 | + download_url="https://github.com/aws-cloudformation/cloudformation-guard/releases/download/${VERSION}/cfn-guard-v3-x86_64-ubuntu-latest.tar.gz" |
| 32 | + arch_type="x86_64" |
| 33 | + sha256sum_expected="${sha256sum_expected_amd64}" |
| 34 | + fi |
| 35 | + echo "Downloading cfn-guard from ${download_url}..." |
| 36 | + curl -fsSL "${download_url}" -o "${download_file}" |
| 37 | + |
| 38 | + download_file_sha256sum=$(sha256sum "${download_file}" | awk '{print $1}') |
| 39 | + if [ "${download_file_sha256sum}" != "${sha256sum_expected#sha256:}" ]; then |
| 40 | + echo "SHA256 checksum mismatch for downloaded cfn-guard archive" |
| 41 | + echo "Expected: ${sha256sum_expected}" |
| 42 | + echo "Actual: sha256:${download_file_sha256sum}" |
| 43 | + exit 1 |
| 44 | + fi |
| 45 | + |
| 46 | + tar -xzf "${download_file}" -C "${tmp_dir}" |
| 47 | + mkdir -p ~/.guard/bin |
| 48 | + mv "${tmp_dir}/cfn-guard-v3-${arch_type}-ubuntu-latest/cfn-guard" ~/.guard/bin/cfn-guard |
| 49 | + chmod +x ~/.guard/bin/cfn-guard |
| 50 | +} |
| 51 | +echo "(*) Installing cfn-guard..." |
| 52 | + |
| 53 | +install |
| 54 | + |
| 55 | +echo "Done!" |
0 commit comments