|
| 1 | +# Copyright 2022 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +name: 'generate terraform checksum' |
| 16 | + |
| 17 | +on: |
| 18 | + workflow_dispatch: |
| 19 | + inputs: |
| 20 | + terraform_version: |
| 21 | + type: string |
| 22 | + description: Terraform version (e.g 1.3.2) |
| 23 | + required: false |
| 24 | + default: '1.3.2' |
| 25 | + |
| 26 | +jobs: |
| 27 | + get_and_verify_checksum: |
| 28 | + runs-on: 'ubuntu-latest' |
| 29 | + env: |
| 30 | + VERSION: ${{ inputs.terraform_version }} |
| 31 | + outputs: |
| 32 | + binary_checksum: '${{ steps.verify-checksum.outputs.binary_checksum }}' |
| 33 | + archive_checksum: '${{ steps.verify-checksum.outputs.archive_checksum }}' |
| 34 | + steps: |
| 35 | + - |
| 36 | + name: 'verify-checksum' |
| 37 | + run: |- |
| 38 | + # Set a local gnupg home so as not to pollute the environment |
| 39 | + export GNUPGHOME=./.gnupg |
| 40 | +
|
| 41 | + # Terraform variables |
| 42 | + export ARCH=darwin_amd64 |
| 43 | + export RELEASE_URL=https://releases.hashicorp.com/terraform/${VERSION} |
| 44 | + export BIN_FILE=terraform_${VERSION}_${ARCH}.zip |
| 45 | + export SHA_FILE=terraform_${VERSION}_SHA256SUMS |
| 46 | + export SIG_FILE=terraform_${VERSION}_SHA256SUMS.sig |
| 47 | +
|
| 48 | + # Generate a temporary key to use for verification |
| 49 | + gpg --batch --quick-generate-key --batch --passphrase "" [email protected] |
| 50 | +
|
| 51 | + # Retrieve the hashicorp key |
| 52 | + curl --remote-name https://keybase.io/hashicorp/pgp_keys.asc |
| 53 | +
|
| 54 | + # Import the key from hashicorp |
| 55 | + echo "importing key" |
| 56 | + gpg --batch --import pgp_keys.asc |
| 57 | +
|
| 58 | + # Sign the hashicorp key with our key |
| 59 | + echo "signing key" |
| 60 | + gpg --batch --yes --trust-model always --sign-key 34365D9472D7468F |
| 61 | +
|
| 62 | + # Download the archive, sha file and signature |
| 63 | + curl --remote-name ${RELEASE_URL}/${BIN_FILE} |
| 64 | + curl --remote-name ${RELEASE_URL}/${SHA_FILE} |
| 65 | + curl --remote-name ${RELEASE_URL}/${SIG_FILE} |
| 66 | +
|
| 67 | + # Verify the signature against the sha file |
| 68 | + echo "verifying shas" |
| 69 | + gpg --batch --verify ${SIG_FILE} ${SHA_FILE} |
| 70 | +
|
| 71 | + # Verify the archive's checksum |
| 72 | + shasum --algorithm 256 --check --ignore-missing ${SHA_FILE} |
| 73 | +
|
| 74 | + # Extract the binary from the archive |
| 75 | + unzip -o ${BIN_FILE} |
| 76 | +
|
| 77 | + # Extract only the shasum for the archive we care about |
| 78 | + ARCH_SUM=$(grep ${BIN_FILE} ${SHA_FILE} | cut -d' ' -f1) |
| 79 | +
|
| 80 | + # Produce a checksum of the binary |
| 81 | + BIN_SUM=$(shasum -a 256 terraform | cut -d' ' -f1) |
| 82 | +
|
| 83 | + # Store the binary and the archive checksums as outputs |
| 84 | + echo "archive_checksum=${ARCH_SUM}" |
| 85 | + echo "binary_checksum=${BIN_SUM}" |
| 86 | +
|
| 87 | + unset GNUPGHOME |
0 commit comments