Skip to content

Commit c9903aa

Browse files
committed
terraform: Add support for GitHub Attestations in TFLint installation
1 parent c85af4d commit c9903aa

2 files changed

Lines changed: 55 additions & 20 deletions

File tree

src/terraform/devcontainer-feature.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@
7979
}
8080
}
8181
},
82+
"dependsOn": {
83+
"ghcr.io/devcontainers/features/github-cli:1": {
84+
"version": "latest"
85+
}
86+
},
8287
"installsAfter": [
8388
"ghcr.io/devcontainers/features/common-utils"
8489
]

src/terraform/install.sh

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,23 @@ install_tflint() {
460460
curl -sSL -o /tmp/tf-downloads/${TFLINT_FILENAME} https://github.com/terraform-linters/tflint/releases/download/v${TFLINT_VERSION}/${TFLINT_FILENAME}
461461
}
462462

463+
verify_tflint_attestations() {
464+
local checksums=$1
465+
local checksums_sha256=$(sha256sum "$checksums" | cut -d " " -f 1)
466+
467+
check_packages jq
468+
469+
curl -L -f "https://api.github.com/repos/terraform-linters/tflint/attestations/sha256:${checksums_sha256}" > attestation.json
470+
curl_exit_code=$?
471+
if [ $curl_exit_code -ne 0 ]; then
472+
echo "(*) Failed to fetch GitHub Attestations for tflint checksums"
473+
return 1
474+
fi
475+
476+
jq ".attestations[].bundle" attestation.json > bundle.jsonl
477+
gh at verify "$checksums" -R terraform-linters/tflint -b bundle.jsonl
478+
}
479+
463480
if [ "${TFLINT_VERSION}" != "none" ]; then
464481
echo "Downloading tflint..."
465482
TFLINT_FILENAME="tflint_linux_${architecture}.zip"
@@ -475,31 +492,44 @@ if [ "${TFLINT_VERSION}" != "none" ]; then
475492
else
476493
curl -sSL -o tflint_checksums.txt https://github.com/terraform-linters/tflint/releases/download/v${TFLINT_VERSION}/checksums.txt
477494

495+
# Attempt GitHub Attestation verification (0.51.1+)
478496
set +e
479-
curl -sSL -o checksums.txt.keyless.sig https://github.com/terraform-linters/tflint/releases/download/v${TFLINT_VERSION}/checksums.txt.keyless.sig
497+
verify_tflint_attestations tflint_checksums.txt
498+
verify_result=$?
480499
set -e
481500

482-
# Check that checksums.txt.keyless.sig exists and is not empty
483-
if [ -s checksums.txt.keyless.sig ]; then
484-
# Validate checksums with cosign
485-
curl -sSL -o checksums.txt.pem https://github.com/terraform-linters/tflint/releases/download/v${TFLINT_VERSION}/checksums.txt.pem
486-
ensure_cosign
487-
cosign verify-blob \
488-
--certificate=/tmp/tf-downloads/checksums.txt.pem \
489-
--signature=/tmp/tf-downloads/checksums.txt.keyless.sig \
490-
--certificate-identity-regexp="^https://github.com/terraform-linters/tflint" \
491-
--certificate-oidc-issuer=https://token.actions.githubusercontent.com \
492-
/tmp/tf-downloads/tflint_checksums.txt
493-
# Ensure that checksums.txt has $TFLINT_FILENAME
494-
grep ${TFLINT_FILENAME} /tmp/tf-downloads/tflint_checksums.txt
495-
# Validate downloaded file
501+
if [ $verify_result -eq 0 ]; then
496502
sha256sum --ignore-missing -c tflint_checksums.txt
503+
echo "(*) tflint_checksums.txt verified successfully using GitHub Attestation."
497504
else
498-
# Fallback to older, GPG-based verification (pre-0.47.0 of tflint)
499-
curl -sSL -o tflint_checksums.txt.sig https://github.com/terraform-linters/tflint/releases/download/v${TFLINT_VERSION}/checksums.txt.sig
500-
curl -sSL -o tflint_key "${TFLINT_GPG_KEY_URI}"
501-
gpg -q --import tflint_key
502-
gpg --verify tflint_checksums.txt.sig tflint_checksums.txt
505+
# Fallback to cosign verification
506+
echo "(*) GitHub Attestation verification failed or not supported for this version, falling back to Cosign verification..."
507+
set +e
508+
curl -sSL -o checksums.txt.keyless.sig https://github.com/terraform-linters/tflint/releases/download/v${TFLINT_VERSION}/checksums.txt.keyless.sig
509+
set -e
510+
511+
# Check that checksums.txt.keyless.sig exists and is not empty
512+
if [ -s checksums.txt.keyless.sig ]; then
513+
# Validate checksums with cosign
514+
curl -sSL -o checksums.txt.pem https://github.com/terraform-linters/tflint/releases/download/v${TFLINT_VERSION}/checksums.txt.pem
515+
ensure_cosign
516+
cosign verify-blob \
517+
--certificate=/tmp/tf-downloads/checksums.txt.pem \
518+
--signature=/tmp/tf-downloads/checksums.txt.keyless.sig \
519+
--certificate-identity-regexp="^https://github.com/terraform-linters/tflint" \
520+
--certificate-oidc-issuer=https://token.actions.githubusercontent.com \
521+
/tmp/tf-downloads/tflint_checksums.txt
522+
# Ensure that checksums.txt has $TFLINT_FILENAME
523+
grep ${TFLINT_FILENAME} /tmp/tf-downloads/tflint_checksums.txt
524+
# Validate downloaded file
525+
sha256sum --ignore-missing -c tflint_checksums.txt
526+
else
527+
# Fallback to older, GPG-based verification (pre-0.47.0 of tflint)
528+
curl -sSL -o tflint_checksums.txt.sig https://github.com/terraform-linters/tflint/releases/download/v${TFLINT_VERSION}/checksums.txt.sig
529+
curl -sSL -o tflint_key "${TFLINT_GPG_KEY_URI}"
530+
gpg -q --import tflint_key
531+
gpg --verify tflint_checksums.txt.sig tflint_checksums.txt
532+
fi
503533
fi
504534
fi
505535
fi

0 commit comments

Comments
 (0)