Unverified Commit 988cdd2b authored by Gaurav Saini's avatar Gaurav Saini Committed by GitHub

[Terraform] feature fallback fix (#912)

* implementation of fallback logic for cosign, terraform

* tflint, terragrunt - wrote fallback logic for these

* misc change

* [Terraform] - fallback - apply

* changes as requested by review comments

* misc change

* misc change

* 2-step fallback implemented in install.sh, test cases yet to be updated

* install jq for ubuntu:focal, jammy & debian:11, 12

* test for terraform_docs updated

* tfsec test updated

* terraform fallback test updated

* misc change

* added tflint fallback test file

* terragrunt fallback test file added

* minor change in tfsec test file

* few more changes to cosign, terragrunt installations..

* changes to test files for terragrunt & cosign

* Changes acc. to review comments for pr

* changes requested

* changes as requested in review comments !
parent 0a4fa18e
{
"id": "terraform",
"version": "1.3.5",
"version": "1.3.6",
"name": "Terraform, tflint, and TFGrunt",
"documentationURL": "https://github.com/devcontainers/features/tree/main/src/terraform",
"description": "Installs the Terraform CLI and optionally TFLint and Terragrunt. Auto-detects latest version and installs needed dependencies.",
......
......@@ -137,6 +137,47 @@ find_version_from_git_tags() {
echo "${variable_name}=${!variable_name}"
}
# Use semver logic to decrement a version number then look for the closest match
find_prev_version_from_git_tags() {
local variable_name=$1
local current_version=${!variable_name}
local repository=$2
# Normally a "v" is used before the version number, but support alternate cases
local prefix=${3:-"tags/v"}
# Some repositories use "_" instead of "." for version number part separation, support that
local separator=${4:-"."}
# Some tools release versions that omit the last digit (e.g. go)
local last_part_optional=${5:-"false"}
# Some repositories may have tags that include a suffix (e.g. actions/node-versions)
local version_suffix_regex=$6
# Try one break fix version number less if we get a failure. Use "set +e" since "set -e" can cause failures in valid scenarios.
set +e
major="$(echo "${current_version}" | grep -oE '^[0-9]+' || echo '')"
minor="$(echo "${current_version}" | grep -oP '^[0-9]+\.\K[0-9]+' || echo '')"
breakfix="$(echo "${current_version}" | grep -oP '^[0-9]+\.[0-9]+\.\K[0-9]+' 2>/dev/null || echo '')"
if [ "${minor}" = "0" ] && [ "${breakfix}" = "0" ]; then
((major=major-1))
declare -g ${variable_name}="${major}"
# Look for latest version from previous major release
find_version_from_git_tags "${variable_name}" "${repository}" "${prefix}" "${separator}" "${last_part_optional}"
# Handle situations like Go's odd version pattern where "0" releases omit the last part
elif [ "${breakfix}" = "" ] || [ "${breakfix}" = "0" ]; then
((minor=minor-1))
declare -g ${variable_name}="${major}.${minor}"
# Look for latest version from previous minor release
find_version_from_git_tags "${variable_name}" "${repository}" "${prefix}" "${separator}" "${last_part_optional}"
else
((breakfix=breakfix-1))
if [ "${breakfix}" = "0" ] && [ "${last_part_optional}" = "true" ]; then
declare -g ${variable_name}="${major}.${minor}"
else
declare -g ${variable_name}="${major}.${minor}.${breakfix}"
fi
fi
set -e
}
find_sentinel_version_from_url() {
local variable_name=$1
local requested_version=${!variable_name}
......@@ -177,6 +218,73 @@ check_packages() {
fi
}
# Function to fetch the version released prior to the latest version
get_previous_version() {
local url=$1
local repo_url=$2
local variable_name=$3
prev_version=${!variable_name}
output=$(curl -s "$repo_url");
# install jq
check_packages jq
message=$(echo "$output" | jq -r '.message')
if [[ $message == "API rate limit exceeded"* ]]; then
echo -e "\nAn attempt to find latest version using GitHub Api Failed... \nReason: ${message}"
echo -e "\nAttempting to find latest version using GitHub tags."
find_prev_version_from_git_tags prev_version "$url" "tags/v"
declare -g ${variable_name}="${prev_version}"
else
echo -e "\nAttempting to find latest version using GitHub Api."
version=$(echo "$output" | jq -r '.tag_name')
declare -g ${variable_name}="${version#v}"
fi
echo "${variable_name}=${!variable_name}"
}
get_github_api_repo_url() {
local url=$1
echo "${url/https:\/\/github.com/https:\/\/api.github.com\/repos}/releases/latest"
}
install_previous_version() {
given_version=$1
requested_version=${!given_version}
local URL=$2
INSTALLER_FN=$3
local REPO_URL=$(get_github_api_repo_url "$URL")
local PKG_NAME=$(get_pkg_name "${given_version}")
echo -e "\n(!) Failed to fetch the latest artifacts for ${PKG_NAME} v${requested_version}..."
get_previous_version "$URL" "$REPO_URL" requested_version
echo -e "\nAttempting to install ${requested_version}"
declare -g ${given_version}="${requested_version#v}"
$INSTALLER_FN "${!given_version}"
echo "${given_version}=${!given_version}"
}
install_cosign() {
COSIGN_VERSION=$1
local URL=$2
cosign_filename="/tmp/cosign_${COSIGN_VERSION}_${architecture}.deb"
cosign_url="https://github.com/sigstore/cosign/releases/latest/download/cosign_${COSIGN_VERSION}_${architecture}.deb"
curl -L "${cosign_url}" -o $cosign_filename
if grep -q "Not Found" "$cosign_filename"; then
echo -e "\n(!) Failed to fetch the latest artifacts for cosign v${COSIGN_VERSION}..."
REPO_URL=$(get_github_api_repo_url "$URL")
get_previous_version "$URL" "$REPO_URL" COSIGN_VERSION
echo -e "\nAttempting to install ${COSIGN_VERSION}"
cosign_filename="/tmp/cosign_${COSIGN_VERSION}_${architecture}.deb"
cosign_url="https://github.com/sigstore/cosign/releases/latest/download/cosign_${COSIGN_VERSION}_${architecture}.deb"
curl -L "${cosign_url}" -o $cosign_filename
fi
dpkg -i $cosign_filename
rm $cosign_filename
echo "Installation of cosign succeeded with ${COSIGN_VERSION}."
}
# Install 'cosign' for validating signatures
# https://docs.sigstore.dev/cosign/overview/
ensure_cosign() {
......@@ -184,12 +292,10 @@ ensure_cosign() {
if ! type cosign > /dev/null 2>&1; then
echo "Installing cosign..."
LATEST_COSIGN_VERSION="latest"
find_version_from_git_tags LATEST_COSIGN_VERSION 'https://github.com/sigstore/cosign'
curl -L "https://github.com/sigstore/cosign/releases/latest/download/cosign_${LATEST_COSIGN_VERSION}_${architecture}.deb" -o /tmp/cosign_${LATEST_COSIGN_VERSION}_${architecture}.deb
dpkg -i /tmp/cosign_${LATEST_COSIGN_VERSION}_${architecture}.deb
rm /tmp/cosign_${LATEST_COSIGN_VERSION}_${architecture}.deb
COSIGN_VERSION="latest"
cosign_url='https://github.com/sigstore/cosign'
find_version_from_git_tags COSIGN_VERSION "${cosign_url}"
install_cosign "${COSIGN_VERSION}" "${cosign_url}"
fi
if ! type cosign > /dev/null 2>&1; then
echo "(!) Failed to install cosign."
......@@ -207,18 +313,30 @@ if ! type git > /dev/null 2>&1; then
check_packages git
fi
terraform_url='https://github.com/hashicorp/terraform'
tflint_url='https://github.com/terraform-linters/tflint'
terragrunt_url='https://github.com/gruntwork-io/terragrunt'
# Verify requested version is available, convert latest
find_version_from_git_tags TERRAFORM_VERSION 'https://github.com/hashicorp/terraform'
find_version_from_git_tags TFLINT_VERSION 'https://github.com/terraform-linters/tflint'
find_version_from_git_tags TERRAGRUNT_VERSION 'https://github.com/gruntwork-io/terragrunt'
find_version_from_git_tags TERRAFORM_VERSION "$terraform_url"
find_version_from_git_tags TFLINT_VERSION "$tflint_url"
find_version_from_git_tags TERRAGRUNT_VERSION "$terragrunt_url"
install_terraform() {
local TERRAFORM_VERSION=$1
terraform_filename="terraform_${TERRAFORM_VERSION}_linux_${architecture}.zip"
curl -sSL -o ${terraform_filename} "https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/${terraform_filename}"
}
mkdir -p /tmp/tf-downloads
cd /tmp/tf-downloads
# Install Terraform, tflint, Terragrunt
echo "Downloading terraform..."
terraform_filename="terraform_${TERRAFORM_VERSION}_linux_${architecture}.zip"
curl -sSL -o ${terraform_filename} "https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/${terraform_filename}"
install_terraform "$TERRAFORM_VERSION"
if grep -q "The specified key does not exist." "${terraform_filename}"; then
install_previous_version TERRAFORM_VERSION $terraform_url "install_terraform"
terraform_filename="terraform_${TERRAFORM_VERSION}_linux_${architecture}.zip"
fi
if [ "${TERRAFORM_SHA256}" != "dev-mode" ]; then
if [ "${TERRAFORM_SHA256}" = "automatic" ]; then
receive_gpg_keys TERRAFORM_GPG_KEY
......@@ -233,10 +351,18 @@ fi
unzip ${terraform_filename}
mv -f terraform /usr/local/bin/
install_tflint() {
TFLINT_VERSION=$1
curl -sSL -o /tmp/tf-downloads/${TFLINT_FILENAME} https://github.com/terraform-linters/tflint/releases/download/v${TFLINT_VERSION}/${TFLINT_FILENAME}
}
if [ "${TFLINT_VERSION}" != "none" ]; then
echo "Downloading tflint..."
TFLINT_FILENAME="tflint_linux_${architecture}.zip"
curl -sSL -o /tmp/tf-downloads/${TFLINT_FILENAME} https://github.com/terraform-linters/tflint/releases/download/v${TFLINT_VERSION}/${TFLINT_FILENAME}
install_tflint "$TFLINT_VERSION"
if grep -q "Not Found" "/tmp/tf-downloads/${TFLINT_FILENAME}"; then
install_previous_version TFLINT_VERSION "$tflint_url" "install_tflint"
fi
if [ "${TFLINT_SHA256}" != "dev-mode" ]; then
if [ "${TFLINT_SHA256}" != "automatic" ]; then
......@@ -277,10 +403,20 @@ if [ "${TFLINT_VERSION}" != "none" ]; then
unzip /tmp/tf-downloads/${TFLINT_FILENAME}
mv -f tflint /usr/local/bin/
fi
install_terragrunt() {
TERRAGRUNT_VERSION=$1
curl -sSL -o /tmp/tf-downloads/${terragrunt_filename} https://github.com/gruntwork-io/terragrunt/releases/download/v${TERRAGRUNT_VERSION}/${terragrunt_filename}
}
if [ "${TERRAGRUNT_VERSION}" != "none" ]; then
echo "Downloading Terragrunt..."
terragrunt_filename="terragrunt_linux_${architecture}"
curl -sSL -o /tmp/tf-downloads/${terragrunt_filename} https://github.com/gruntwork-io/terragrunt/releases/download/v${TERRAGRUNT_VERSION}/${terragrunt_filename}
install_terragrunt "$TERRAGRUNT_VERSION"
output=$(cat "/tmp/tf-downloads/${terragrunt_filename}")
if [[ $output == "Not Found" ]]; then
install_previous_version TERRAGRUNT_VERSION $terragrunt_url "install_terragrunt"
fi
if [ "${TERRAGRUNT_SHA256}" != "dev-mode" ]; then
if [ "${TERRAGRUNT_SHA256}" = "automatic" ]; then
curl -sSL -o terragrunt_SHA256SUMS https://github.com/gruntwork-io/terragrunt/releases/download/v${TERRAGRUNT_VERSION}/SHA256SUMS
......@@ -318,12 +454,23 @@ if [ "${INSTALL_SENTINEL}" = "true" ]; then
mv -f /tmp/tf-downloads/sentinel /usr/local/bin/sentinel
fi
install_tfsec() {
local TFSEC_VERSION=$1
tfsec_filename="tfsec_${TFSEC_VERSION}_linux_${architecture}.tar.gz"
curl -sSL -o /tmp/tf-downloads/${tfsec_filename} https://github.com/aquasecurity/tfsec/releases/download/v${TFSEC_VERSION}/${tfsec_filename}
}
if [ "${INSTALL_TFSEC}" = "true" ]; then
TFSEC_VERSION="latest"
find_version_from_git_tags TFSEC_VERSION 'https://github.com/aquasecurity/tfsec'
tfsec_url='https://github.com/aquasecurity/tfsec'
find_version_from_git_tags TFSEC_VERSION $tfsec_url
tfsec_filename="tfsec_${TFSEC_VERSION}_linux_${architecture}.tar.gz"
echo "(*) Downloading TFSec... ${tfsec_filename}"
curl -sSL -o /tmp/tf-downloads/${tfsec_filename} https://github.com/aquasecurity/tfsec/releases/download/v${TFSEC_VERSION}/${tfsec_filename}
install_tfsec "$TFSEC_VERSION"
if grep -q "Not Found" "/tmp/tf-downloads/${tfsec_filename}"; then
install_previous_version TFSEC_VERSION $tfsec_url "install_tfsec"
tfsec_filename="tfsec_${TFSEC_VERSION}_linux_${architecture}.tar.gz"
fi
if [ "${TFSEC_SHA256}" != "dev-mode" ]; then
if [ "${TFSEC_SHA256}" = "automatic" ]; then
curl -sSL -o tfsec_SHA256SUMS https://github.com/aquasecurity/tfsec/releases/download/v${TFSEC_VERSION}/tfsec_${TFSEC_VERSION}_checksums.txt
......@@ -338,12 +485,23 @@ if [ "${INSTALL_TFSEC}" = "true" ]; then
mv -f /tmp/tf-downloads/tfsec/tfsec /usr/local/bin/tfsec
fi
install_terraform_docs() {
local TERRAFORM_DOCS_VERSION=$1
tfdocs_filename="terraform-docs-v${TERRAFORM_DOCS_VERSION}-linux-${architecture}.tar.gz"
curl -sSL -o /tmp/tf-downloads/${tfdocs_filename} https://github.com/terraform-docs/terraform-docs/releases/download/v${TERRAFORM_DOCS_VERSION}/${tfdocs_filename}
}
if [ "${INSTALL_TERRAFORM_DOCS}" = "true" ]; then
TERRAFORM_DOCS_VERSION="latest"
find_version_from_git_tags TERRAFORM_DOCS_VERSION 'https://github.com/terraform-docs/terraform-docs'
terraform_docs_url='https://github.com/terraform-docs/terraform-docs'
find_version_from_git_tags TERRAFORM_DOCS_VERSION $terraform_docs_url
tfdocs_filename="terraform-docs-v${TERRAFORM_DOCS_VERSION}-linux-${architecture}.tar.gz"
echo "(*) Downloading Terraform docs... ${tfdocs_filename}"
curl -sSL -o /tmp/tf-downloads/${tfdocs_filename} https://github.com/terraform-docs/terraform-docs/releases/download/v${TERRAFORM_DOCS_VERSION}/${tfdocs_filename}
install_terraform_docs "$TERRAFORM_DOCS_VERSION"
if grep -q "Not Found" "/tmp/tf-downloads/${tfdocs_filename}"; then
install_previous_version TERRAFORM_DOCS_VERSION $terraform_docs_url "install_terraform_docs"
tfdocs_filename="terraform-docs-v${TERRAFORM_DOCS_VERSION}-linux-${architecture}.tar.gz"
fi
if [ "${TERRAFORM_DOCS_SHA256}" != "dev-mode" ]; then
if [ "${TERRAFORM_DOCS_SHA256}" = "automatic" ]; then
curl -sSL -o tfdocs_SHA256SUMS https://github.com/terraform-docs/terraform-docs/releases/download/v${TERRAFORM_DOCS_VERSION}/terraform-docs-v${TERRAFORM_DOCS_VERSION}.sha256sum
......
......@@ -15,6 +15,14 @@
}
}
},
"tfsec_fallback_test": {
"image": "mcr.microsoft.com/devcontainers/base:jammy",
"features": {
"terraform": {
"installTFsec": true
}
}
},
"install_terraform_docs": {
"image": "mcr.microsoft.com/devcontainers/base:jammy",
"features": {
......@@ -23,6 +31,38 @@
}
}
},
"terraform_docs_fallback_test": {
"image": "mcr.microsoft.com/devcontainers/base:jammy",
"features": {
"terraform": {
"installTerraformDocs": true
}
}
},
"terraform_fallback_test": {
"image": "mcr.microsoft.com/devcontainers/base:jammy",
"features": {
"terraform": {
"version": "latest"
}
}
},
"terragrunt_fallback_test": {
"image": "mcr.microsoft.com/devcontainers/base:jammy",
"features": {
"terraform": {
"terragrunt": "latest"
}
}
},
"tflint_fallback_test": {
"image": "mcr.microsoft.com/devcontainers/base:jammy",
"features": {
"terraform": {
"tflint": "latest"
}
}
},
"older_tflint": {
"image": "mcr.microsoft.com/devcontainers/base:jammy",
"features": {
......
#!/bin/bash
set -e
# Import test library for `check` command
source dev-container-features-test-lib
# Check to make sure the user is vscode
check "user is vscode" whoami | grep vscode
# Terraform Docs specific tests
check "terraform-docs version as installed by feature" terraform-docs --version
TERRAFORM_DOCS_SHA256="automatic"
set_error_handler() {
echo "Error occurred on line: $LINENO"
}
# Register the error handler function to be triggered on ERR signal
trap 'set_error_handler' ERR
architecture="$(uname -m)"
case ${architecture} in
x86_64) architecture="amd64";;
aarch64 | armv8*) architecture="arm64";;
aarch32 | armv7* | armvhf*) architecture="arm";;
i?86) architecture="386";;
*) echo "(!) Architecture ${architecture} unsupported"; exit 1 ;;
esac
# Figure out correct version of a three part version number is not passed
find_version_from_git_tags() {
local variable_name=$1
local requested_version=${!variable_name}
if [ "${requested_version}" = "none" ]; then return; fi
local repository=$2
local prefix=${3:-"tags/v"}
local separator=${4:-"."}
local last_part_optional=${5:-"false"}
if [ "$(echo "${requested_version}" | grep -o "." | wc -l)" != "2" ]; then
local escaped_separator=${separator//./\\.}
local last_part
if [ "${last_part_optional}" = "true" ]; then
last_part="(${escaped_separator}[0-9]+)?"
else
last_part="${escaped_separator}[0-9]+"
fi
local regex="${prefix}\\K[0-9]+${escaped_separator}[0-9]+${last_part}$"
local version_list="$(git ls-remote --tags ${repository} | grep -oP "${regex}" | tr -d ' ' | tr "${separator}" "." | sort -rV)"
if [ "${requested_version}" = "latest" ] || [ "${requested_version}" = "current" ] || [ "${requested_version}" = "lts" ]; then
declare -g ${variable_name}="$(echo "${version_list}" | head -n 1)"
else
set +e
declare -g ${variable_name}="$(echo "${version_list}" | grep -E -m 1 "^${requested_version//./\\.}([\\.\\s]|$)")"
set -e
fi
fi
if [ -z "${!variable_name}" ] || ! echo "${version_list}" | grep "^${!variable_name//./\\.}$" > /dev/null 2>&1; then
echo -e "Invalid ${variable_name} value: ${requested_version}\nValid values:\n${version_list}" >&2
exit 1
fi
echo "${variable_name}=${!variable_name}"
}
# Use semver logic to decrement a version number then look for the closest match
find_prev_version_from_git_tags() {
local variable_name=$1
local current_version=${!variable_name}
local repository=$2
# Normally a "v" is used before the version number, but support alternate cases
local prefix=${3:-"tags/v"}
# Some repositories use "_" instead of "." for version number part separation, support that
local separator=${4:-"."}
# Some tools release versions that omit the last digit (e.g. go)
local last_part_optional=${5:-"false"}
# Some repositories may have tags that include a suffix (e.g. actions/node-versions)
local version_suffix_regex=$6
# Try one break fix version number less if we get a failure. Use "set +e" since "set -e" can cause failures in valid scenarios.
set +e
major="$(echo "${current_version}" | grep -oE '^[0-9]+' || echo '')"
minor="$(echo "${current_version}" | grep -oP '^[0-9]+\.\K[0-9]+' || echo '')"
breakfix="$(echo "${current_version}" | grep -oP '^[0-9]+\.[0-9]+\.\K[0-9]+' 2>/dev/null || echo '')"
if [ "${minor}" = "0" ] && [ "${breakfix}" = "0" ]; then
((major=major-1))
declare -g ${variable_name}="${major}"
# Look for latest version from previous major release
find_version_from_git_tags "${variable_name}" "${repository}" "${prefix}" "${separator}" "${last_part_optional}"
# Handle situations like Go's odd version pattern where "0" releases omit the last part
elif [ "${breakfix}" = "" ] || [ "${breakfix}" = "0" ]; then
((minor=minor-1))
declare -g ${variable_name}="${major}.${minor}"
# Look for latest version from previous minor release
find_version_from_git_tags "${variable_name}" "${repository}" "${prefix}" "${separator}" "${last_part_optional}"
else
((breakfix=breakfix-1))
if [ "${breakfix}" = "0" ] && [ "${last_part_optional}" = "true" ]; then
declare -g ${variable_name}="${major}.${minor}"
else
declare -g ${variable_name}="${major}.${minor}.${breakfix}"
fi
fi
set -e
}
apt_get_update()
{
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
echo "Running apt-get update..."
apt-get update -y
fi
}
# Checks if packages are installed and installs them if not
check_packages() {
if ! dpkg -s "$@" > /dev/null 2>&1; then
apt_get_update
apt-get -y install --no-install-recommends "$@"
fi
}
# Function to fetch the version released prior to the latest version
get_previous_version() {
local url=$1
local repo_url=$2
local variable_name=$3
local mode=$4
prev_version=${!variable_name}
output=$(curl -s "$repo_url");
# install jq
check_packages jq
message=$(echo "$output" | jq -r '.message')
if [[ "$mode" == "mode1" ]]; then
message="API rate limit exceeded";
elif [[ "$mode" == "mode2" ]]; then
message=""
fi
if [[ $message == "API rate limit exceeded"* ]]; then
echo -e "\nAn attempt to find latest version using GitHub Api Failed... \nReason: ${message}"
echo -e "\nAttempting to find latest version using GitHub tags."
find_prev_version_from_git_tags prev_version "$url" "tags/v"
declare -g ${variable_name}="${prev_version}"
else
echo -e "\nAttempting to find latest version using GitHub Api."
version=$(echo "$output" | jq -r '.tag_name')
declare -g ${variable_name}="${version#v}"
fi
echo "${variable_name}=${!variable_name}"
}
get_github_api_repo_url() {
local url=$1
echo "${url/https:\/\/github.com/https:\/\/api.github.com\/repos}/releases/latest"
}
install_previous_version() {
given_version=$1
requested_version=${!given_version}
local URL=$2
local mode=$3
INSTALLER_FN=$4
local REPO_URL=$(get_github_api_repo_url "$URL")
local PKG_NAME=$(get_pkg_name "${given_version}")
echo -e "\n(!) Failed to fetch the latest artifacts for ${PKG_NAME} v${requested_version}..."
get_previous_version "$URL" "$REPO_URL" requested_version $mode
echo -e "\nAttempting to install ${requested_version}"
declare -g ${given_version}="${requested_version#v}"
$INSTALLER_FN "${!given_version}"
echo "${given_version}=${!given_version}"
}
install_terraform_docs() {
local TERRAFORM_DOCS_VERSION=$1
tfdocs_filename="terraform-docs-v${TERRAFORM_DOCS_VERSION}-linux-${architecture}.tar.gz"
curl -sSL -o /tmp/tf-downloads/${tfdocs_filename} https://github.com/terraform-docs/terraform-docs/releases/download/v${TERRAFORM_DOCS_VERSION}/${tfdocs_filename}
}
try_install_terraform_docs_dummy_version() {
mode=$1
mkdir -p /tmp/tf-downloads
cd /tmp/tf-downloads
TERRAFORM_DOCS_VERSION="0.17.xyz"
echo -e "\nInstalling TERRAFORM_DOCS dummy version.." v${TERRAFORM_DOCS_VERSION}
terraform_docs_url='https://github.com/terraform-docs/terraform-docs'
tfdocs_filename="terraform-docs-v${TERRAFORM_DOCS_VERSION}-linux-${architecture}.tar.gz"
echo "(*) Downloading Terraform docs... ${tfdocs_filename}"
install_terraform_docs "$TERRAFORM_DOCS_VERSION"
if grep -q "Not Found" "/tmp/tf-downloads/${tfdocs_filename}"; then
install_previous_version TERRAFORM_DOCS_VERSION $terraform_docs_url $mode "install_terraform_docs"
tfdocs_filename="terraform-docs-v${TERRAFORM_DOCS_VERSION}-linux-${architecture}.tar.gz"
fi
if [ "${TERRAFORM_DOCS_SHA256}" != "dev-mode" ]; then
if [ "${TERRAFORM_DOCS_SHA256}" = "automatic" ]; then
curl -sSL -o tfdocs_SHA256SUMS https://github.com/terraform-docs/terraform-docs/releases/download/v${TERRAFORM_DOCS_VERSION}/terraform-docs-v${TERRAFORM_DOCS_VERSION}.sha256sum
else
echo "${TERRAFORM_DOCS_SHA256} *${tfsec_filename}" > tfdocs_SHA256SUMS
fi
sha256sum --ignore-missing -c tfdocs_SHA256SUMS
fi
mkdir -p /tmp/tf-downloads/tfdocs
tar -xzf /tmp/tf-downloads/${tfdocs_filename} -C /tmp/tf-downloads/tfdocs
sudo chmod a+x /tmp/tf-downloads/tfdocs/terraform-docs
sudo mv -f /tmp/tf-downloads/tfdocs/terraform-docs /usr/local/bin/terraform-docs
}
try_install_terraform_docs_dummy_version "mode1"
check "terraform-docs version as installed by test (mode 1: install using find_prev_version_from_git_tags)" terraform-docs --version
try_install_terraform_docs_dummy_version "mode2"
check "terraform-docs version as installed by test (mode 2: install using GitHub Api)" terraform-docs --version
# Report result
reportResults
\ No newline at end of file
#!/bin/bash
set -e
# Import test library for `check` command
source dev-container-features-test-lib
# Check to make sure the user is vscode
check "user is vscode" whoami | grep vscode
set_error_handler() {
echo "Error occurred on line: $LINENO"
}
# Register the error handler function to be triggered on ERR signal
trap 'set_error_handler' ERR
check "terraform version as installed by feature" terraform --version
architecture="$(uname -m)"
case ${architecture} in
x86_64) architecture="amd64";;
aarch64 | armv8*) architecture="arm64";;
aarch32 | armv7* | armvhf*) architecture="arm";;
i?86) architecture="386";;
*) echo "(!) Architecture ${architecture} unsupported"; exit 1 ;;
esac
# Figure out correct version of a three part version number is not passed
find_version_from_git_tags() {
local variable_name=$1
local requested_version=${!variable_name}
if [ "${requested_version}" = "none" ]; then return; fi
local repository=$2
local prefix=${3:-"tags/v"}
local separator=${4:-"."}
local last_part_optional=${5:-"false"}
if [ "$(echo "${requested_version}" | grep -o "." | wc -l)" != "2" ]; then
local escaped_separator=${separator//./\\.}
local last_part
if [ "${last_part_optional}" = "true" ]; then
last_part="(${escaped_separator}[0-9]+)?"
else
last_part="${escaped_separator}[0-9]+"
fi
local regex="${prefix}\\K[0-9]+${escaped_separator}[0-9]+${last_part}$"
local version_list="$(git ls-remote --tags ${repository} | grep -oP "${regex}" | tr -d ' ' | tr "${separator}" "." | sort -rV)"
if [ "${requested_version}" = "latest" ] || [ "${requested_version}" = "current" ] || [ "${requested_version}" = "lts" ]; then
declare -g ${variable_name}="$(echo "${version_list}" | head -n 1)"
else
set +e
declare -g ${variable_name}="$(echo "${version_list}" | grep -E -m 1 "^${requested_version//./\\.}([\\.\\s]|$)")"
set -e
fi
fi
if [ -z "${!variable_name}" ] || ! echo "${version_list}" | grep "^${!variable_name//./\\.}$" > /dev/null 2>&1; then
echo -e "Invalid ${variable_name} value: ${requested_version}\nValid values:\n${version_list}" >&2
exit 1
fi
echo "${variable_name}=${!variable_name}"
}
# Use semver logic to decrement a version number then look for the closest match
find_prev_version_from_git_tags() {
local variable_name=$1
local current_version=${!variable_name}
local repository=$2
# Normally a "v" is used before the version number, but support alternate cases
local prefix=${3:-"tags/v"}
# Some repositories use "_" instead of "." for version number part separation, support that
local separator=${4:-"."}
# Some tools release versions that omit the last digit (e.g. go)
local last_part_optional=${5:-"false"}
# Some repositories may have tags that include a suffix (e.g. actions/node-versions)
local version_suffix_regex=$6
# Try one break fix version number less if we get a failure. Use "set +e" since "set -e" can cause failures in valid scenarios.
set +e
major="$(echo "${current_version}" | grep -oE '^[0-9]+' || echo '')"
minor="$(echo "${current_version}" | grep -oP '^[0-9]+\.\K[0-9]+' || echo '')"
breakfix="$(echo "${current_version}" | grep -oP '^[0-9]+\.[0-9]+\.\K[0-9]+' 2>/dev/null || echo '')"
if [ "${minor}" = "0" ] && [ "${breakfix}" = "0" ]; then
((major=major-1))
declare -g ${variable_name}="${major}"
# Look for latest version from previous major release
find_version_from_git_tags "${variable_name}" "${repository}" "${prefix}" "${separator}" "${last_part_optional}"
# Handle situations like Go's odd version pattern where "0" releases omit the last part
elif [ "${breakfix}" = "" ] || [ "${breakfix}" = "0" ]; then
((minor=minor-1))
declare -g ${variable_name}="${major}.${minor}"
# Look for latest version from previous minor release
find_version_from_git_tags "${variable_name}" "${repository}" "${prefix}" "${separator}" "${last_part_optional}"
else
((breakfix=breakfix-1))
if [ "${breakfix}" = "0" ] && [ "${last_part_optional}" = "true" ]; then
declare -g ${variable_name}="${major}.${minor}"
else
declare -g ${variable_name}="${major}.${minor}.${breakfix}"
fi
fi
set -e
}
apt_get_update()
{
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
echo "Running apt-get update..."
apt-get update -y
fi
}
# Checks if packages are installed and installs them if not
check_packages() {
if ! dpkg -s "$@" > /dev/null 2>&1; then
apt_get_update
apt-get -y install --no-install-recommends "$@"
fi
}
# Function to fetch the version released prior to the latest version
get_previous_version() {
local url=$1
local repo_url=$2
local variable_name=$3
local mode=$4
prev_version=${!variable_name}
output=$(curl -s "$repo_url");
# install jq
check_packages jq
message=$(echo "$output" | jq -r '.message')
if [[ "$mode" == "mode1" ]]; then
message="API rate limit exceeded";
elif [[ "$mode" == "mode2" ]]; then
message=""
fi
if [[ $message == "API rate limit exceeded"* ]]; then
echo -e "\nAn attempt to find latest version using GitHub Api Failed... \nReason: ${message}"
echo -e "\nAttempting to find latest version using GitHub tags."
find_prev_version_from_git_tags prev_version "$url" "tags/v"
declare -g ${variable_name}="${prev_version}"
else
echo -e "\nAttempting to find latest version using GitHub Api."
version=$(echo "$output" | jq -r '.tag_name')
declare -g ${variable_name}="${version#v}"
fi
echo "${variable_name}=${!variable_name}"
}
get_github_api_repo_url() {
local url=$1
echo "${url/https:\/\/github.com/https:\/\/api.github.com\/repos}/releases/latest"
}
install_previous_version() {
given_version=$1
requested_version=${!given_version}
local URL=$2
local mode=$3
INSTALLER_FN=$4
local REPO_URL=$(get_github_api_repo_url "$URL")
local PKG_NAME=$(get_pkg_name "${given_version}")
echo -e "\n(!) Failed to fetch the latest artifacts for ${PKG_NAME} v${requested_version}..."
get_previous_version "$URL" "$REPO_URL" requested_version $mode
echo -e "\nAttempting to install ${requested_version}"
declare -g ${given_version}="${requested_version#v}"
$INSTALLER_FN "${!given_version}"
echo "${given_version}=${!given_version}"
}
install_terraform() {
local TERRAFORM_VERSION=$1
terraform_filename="terraform_${TERRAFORM_VERSION}_linux_${architecture}.zip"
curl -sSL -o ${terraform_filename} "https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/${terraform_filename}"
}
try_install_dummy_terraform_version() {
mode=$1
mkdir -p /tmp/tf-downloads
cd /tmp/tf-downloads
terraform_url='https://github.com/hashicorp/terraform'
TERRAFORM_VERSION="1.7.xyz"
echo -e "\nAttempting to install dummy version for Terraform v${TERRAFORM_VERSION}..."
terraform_filename="terraform_${TERRAFORM_VERSION}_linux_${architecture}.zip"
install_terraform "$TERRAFORM_VERSION"
if grep -q "The specified key does not exist." "${terraform_filename}"; then
install_previous_version TERRAFORM_VERSION $terraform_url $mode "install_terraform"
terraform_filename="terraform_${TERRAFORM_VERSION}_linux_${architecture}.zip"
fi
unzip ${terraform_filename}
sudo mv -f terraform /usr/local/bin/
}
try_install_dummy_terraform_version "mode1"
check "terraform version as installed by test after fallbacking from the dummy version (mode 1: install using find_prev_version_from_git_tags)" terraform --version
try_install_dummy_terraform_version "mode2"
check "terraform version as installed by test after fallbacking from the dummy version (mode 2: install using GitHub Api)" terraform --version
# Report result
reportResults
#!/bin/bash
set -e
# Import test library for `check` command
source dev-container-features-test-lib
# Check to make sure the user is vscode
check "user is vscode" whoami | grep vscode
set_error_handler() {
echo "Error occurred on line: $LINENO"
}
# Register the error handler function to be triggered on ERR signal
trap 'set_error_handler' ERR
check "terragrunt version as installed by feature" terragrunt --version
TERRAGRUNT_SHA256="automatic"
architecture="$(uname -m)"
case ${architecture} in
x86_64) architecture="amd64";;
aarch64 | armv8*) architecture="arm64";;
aarch32 | armv7* | armvhf*) architecture="arm";;
i?86) architecture="386";;
*) echo "(!) Architecture ${architecture} unsupported"; exit 1 ;;
esac
# Figure out correct version of a three part version number is not passed
find_version_from_git_tags() {
local variable_name=$1
local requested_version=${!variable_name}
if [ "${requested_version}" = "none" ]; then return; fi
local repository=$2
local prefix=${3:-"tags/v"}
local separator=${4:-"."}
local last_part_optional=${5:-"false"}
if [ "$(echo "${requested_version}" | grep -o "." | wc -l)" != "2" ]; then
local escaped_separator=${separator//./\\.}
local last_part
if [ "${last_part_optional}" = "true" ]; then
last_part="(${escaped_separator}[0-9]+)?"
else
last_part="${escaped_separator}[0-9]+"
fi
local regex="${prefix}\\K[0-9]+${escaped_separator}[0-9]+${last_part}$"
local version_list="$(git ls-remote --tags ${repository} | grep -oP "${regex}" | tr -d ' ' | tr "${separator}" "." | sort -rV)"
if [ "${requested_version}" = "latest" ] || [ "${requested_version}" = "current" ] || [ "${requested_version}" = "lts" ]; then
declare -g ${variable_name}="$(echo "${version_list}" | head -n 1)"
else
set +e
declare -g ${variable_name}="$(echo "${version_list}" | grep -E -m 1 "^${requested_version//./\\.}([\\.\\s]|$)")"
set -e
fi
fi
if [ -z "${!variable_name}" ] || ! echo "${version_list}" | grep "^${!variable_name//./\\.}$" > /dev/null 2>&1; then
echo -e "Invalid ${variable_name} value: ${requested_version}\nValid values:\n${version_list}" >&2
exit 1
fi
echo "${variable_name}=${!variable_name}"
}
# Use semver logic to decrement a version number then look for the closest match
find_prev_version_from_git_tags() {
local variable_name=$1
local current_version=${!variable_name}
local repository=$2
# Normally a "v" is used before the version number, but support alternate cases
local prefix=${3:-"tags/v"}
# Some repositories use "_" instead of "." for version number part separation, support that
local separator=${4:-"."}
# Some tools release versions that omit the last digit (e.g. go)
local last_part_optional=${5:-"false"}
# Some repositories may have tags that include a suffix (e.g. actions/node-versions)
local version_suffix_regex=$6
# Try one break fix version number less if we get a failure. Use "set +e" since "set -e" can cause failures in valid scenarios.
set +e
major="$(echo "${current_version}" | grep -oE '^[0-9]+' || echo '')"
minor="$(echo "${current_version}" | grep -oP '^[0-9]+\.\K[0-9]+' || echo '')"
breakfix="$(echo "${current_version}" | grep -oP '^[0-9]+\.[0-9]+\.\K[0-9]+' 2>/dev/null || echo '')"
if [ "${minor}" = "0" ] && [ "${breakfix}" = "0" ]; then
((major=major-1))
declare -g ${variable_name}="${major}"
# Look for latest version from previous major release
find_version_from_git_tags "${variable_name}" "${repository}" "${prefix}" "${separator}" "${last_part_optional}"
# Handle situations like Go's odd version pattern where "0" releases omit the last part
elif [ "${breakfix}" = "" ] || [ "${breakfix}" = "0" ]; then
((minor=minor-1))
declare -g ${variable_name}="${major}.${minor}"
# Look for latest version from previous minor release
find_version_from_git_tags "${variable_name}" "${repository}" "${prefix}" "${separator}" "${last_part_optional}"
else
((breakfix=breakfix-1))
if [ "${breakfix}" = "0" ] && [ "${last_part_optional}" = "true" ]; then
declare -g ${variable_name}="${major}.${minor}"
else
declare -g ${variable_name}="${major}.${minor}.${breakfix}"
fi
fi
set -e
}
apt_get_update()
{
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
echo "Running apt-get update..."
apt-get update -y
fi
}
# Checks if packages are installed and installs them if not
check_packages() {
if ! dpkg -s "$@" > /dev/null 2>&1; then
apt_get_update
apt-get -y install --no-install-recommends "$@"
fi
}
# Function to fetch the version released prior to the latest version
get_previous_version() {
local url=$1
local repo_url=$2
local variable_name=$3
local mode=$4
prev_version=${!variable_name}
output=$(curl -s "$repo_url");
# install jq
check_packages jq
message=$(echo "$output" | jq -r '.message')
if [[ "$mode" == "mode1" ]]; then
message="API rate limit exceeded";
elif [[ "$mode" == "mode2" ]]; then
message=""
fi
if [[ $message == "API rate limit exceeded"* ]]; then
echo -e "\nAn attempt to find latest version using GitHub Api Failed... \nReason: ${message}"
echo -e "\nAttempting to find latest version using GitHub tags."
find_prev_version_from_git_tags prev_version "$url" "tags/v"
declare -g ${variable_name}="${prev_version}"
else
echo -e "\nAttempting to find latest version using GitHub Api."
version=$(echo "$output" | jq -r '.tag_name')
declare -g ${variable_name}="${version#v}"
fi
echo "${variable_name}=${!variable_name}"
}
get_github_api_repo_url() {
local url=$1
echo "${url/https:\/\/github.com/https:\/\/api.github.com\/repos}/releases/latest"
}
install_previous_version() {
given_version=$1
requested_version=${!given_version}
local URL=$2
local mode=$3
INSTALLER_FN=$4
local REPO_URL=$(get_github_api_repo_url "$URL")
local PKG_NAME=$(get_pkg_name "${given_version}")
echo -e "\n(!) Failed to fetch the latest artifacts for ${PKG_NAME} v${requested_version}..."
get_previous_version "$URL" "$REPO_URL" requested_version $mode
echo -e "\nAttempting to install ${requested_version}"
declare -g ${given_version}="${requested_version#v}"
$INSTALLER_FN "${!given_version}"
echo "${given_version}=${!given_version}"
}
install_terragrunt() {
TERRAGRUNT_VERSION=$1
curl -sSL -o /tmp/tf-downloads/${terragrunt_filename} https://github.com/gruntwork-io/terragrunt/releases/download/v${TERRAGRUNT_VERSION}/${terragrunt_filename}
}
try_install_dummy_terragrunt_version() {
mode=$1
mkdir -p /tmp/tf-downloads
cd /tmp/tf-downloads
terragrunt_url='https://github.com/gruntwork-io/terragrunt'
TERRAGRUNT_VERSION="0.55.xyz"
echo -e "\nAttempting to install terragrunt dummy v${TERRAGRUNT_VERSION}"
echo "Downloading Terragrunt... v${TERRAGRUNT_VERSION}"
terragrunt_filename="terragrunt_linux_${architecture}"
install_terragrunt "$TERRAGRUNT_VERSION"
output=$(cat "/tmp/tf-downloads/${terragrunt_filename}")
if [[ $output == "Not Found" ]]; then
install_previous_version TERRAGRUNT_VERSION $terragrunt_url $mode "install_terragrunt"
fi
if [ "${TERRAGRUNT_SHA256}" != "dev-mode" ]; then
if [ "${TERRAGRUNT_SHA256}" = "automatic" ]; then
curl -sSL -o terragrunt_SHA256SUMS https://github.com/gruntwork-io/terragrunt/releases/download/v${TERRAGRUNT_VERSION}/SHA256SUMS
else
echo "${TERRAGRUNT_SHA256} *${terragrunt_filename}" > terragrunt_SHA256SUMS
fi
sha256sum --ignore-missing -c terragrunt_SHA256SUMS
fi
sudo chmod a+x /tmp/tf-downloads/${terragrunt_filename}
sudo mv -f /tmp/tf-downloads/${terragrunt_filename} /usr/local/bin/terragrunt
}
try_install_dummy_terragrunt_version "mode1"
check "terragrunt version as installed by test after fallbacking from the dummy version (mode 1: install using find_prev_version_from_git_tags)" terragrunt --version
try_install_dummy_terragrunt_version "mode2"
check "terragrunt version as installed by test after fallbacking from the dummy version (mode 2: install using GitHub Api)" terragrunt --version
# Report result
reportResults
#!/bin/bash
set -e
# Import test library for `check` command
source dev-container-features-test-lib
# Check to make sure the user is vscode
check "user is vscode" whoami | grep vscode
set_error_handler() {
echo "Error occurred on line: $LINENO"
}
# Register the error handler function to be triggered on ERR signal
trap 'set_error_handler' ERR
TFLINT_SHA256="automatic"
GPG_KEY_SERVERS="keyserver hkps://keyserver.ubuntu.com
keyserver hkps://keys.openpgp.org
keyserver hkps://keyserver.pgp.com"
check "tflint version as installed by feature" tflint --version
check "cosign version as installed by feature" cosign version
architecture="$(uname -m)"
case ${architecture} in
x86_64) architecture="amd64";;
aarch64 | armv8*) architecture="arm64";;
aarch32 | armv7* | armvhf*) architecture="arm";;
i?86) architecture="386";;
*) echo "(!) Architecture ${architecture} unsupported"; exit 1 ;;
esac
# Figure out correct version of a three part version number is not passed
find_version_from_git_tags() {
local variable_name=$1
local requested_version=${!variable_name}
if [ "${requested_version}" = "none" ]; then return; fi
local repository=$2
local prefix=${3:-"tags/v"}
local separator=${4:-"."}
local last_part_optional=${5:-"false"}
if [ "$(echo "${requested_version}" | grep -o "." | wc -l)" != "2" ]; then
local escaped_separator=${separator//./\\.}
local last_part
if [ "${last_part_optional}" = "true" ]; then
last_part="(${escaped_separator}[0-9]+)?"
else
last_part="${escaped_separator}[0-9]+"
fi
local regex="${prefix}\\K[0-9]+${escaped_separator}[0-9]+${last_part}$"
local version_list="$(git ls-remote --tags ${repository} | grep -oP "${regex}" | tr -d ' ' | tr "${separator}" "." | sort -rV)"
if [ "${requested_version}" = "latest" ] || [ "${requested_version}" = "current" ] || [ "${requested_version}" = "lts" ]; then
declare -g ${variable_name}="$(echo "${version_list}" | head -n 1)"
else
set +e
declare -g ${variable_name}="$(echo "${version_list}" | grep -E -m 1 "^${requested_version//./\\.}([\\.\\s]|$)")"
set -e
fi
fi
if [ -z "${!variable_name}" ] || ! echo "${version_list}" | grep "^${!variable_name//./\\.}$" > /dev/null 2>&1; then
echo -e "Invalid ${variable_name} value: ${requested_version}\nValid values:\n${version_list}" >&2
exit 1
fi
echo "${variable_name}=${!variable_name}"
}
# Use semver logic to decrement a version number then look for the closest match
find_prev_version_from_git_tags() {
local variable_name=$1
local current_version=${!variable_name}
local repository=$2
# Normally a "v" is used before the version number, but support alternate cases
local prefix=${3:-"tags/v"}
# Some repositories use "_" instead of "." for version number part separation, support that
local separator=${4:-"."}
# Some tools release versions that omit the last digit (e.g. go)
local last_part_optional=${5:-"false"}
# Some repositories may have tags that include a suffix (e.g. actions/node-versions)
local version_suffix_regex=$6
# Try one break fix version number less if we get a failure. Use "set +e" since "set -e" can cause failures in valid scenarios.
set +e
major="$(echo "${current_version}" | grep -oE '^[0-9]+' || echo '')"
minor="$(echo "${current_version}" | grep -oP '^[0-9]+\.\K[0-9]+' || echo '')"
breakfix="$(echo "${current_version}" | grep -oP '^[0-9]+\.[0-9]+\.\K[0-9]+' 2>/dev/null || echo '')"
if [ "${minor}" = "0" ] && [ "${breakfix}" = "0" ]; then
((major=major-1))
declare -g ${variable_name}="${major}"
# Look for latest version from previous major release
find_version_from_git_tags "${variable_name}" "${repository}" "${prefix}" "${separator}" "${last_part_optional}"
# Handle situations like Go's odd version pattern where "0" releases omit the last part
elif [ "${breakfix}" = "" ] || [ "${breakfix}" = "0" ]; then
((minor=minor-1))
declare -g ${variable_name}="${major}.${minor}"
# Look for latest version from previous minor release
find_version_from_git_tags "${variable_name}" "${repository}" "${prefix}" "${separator}" "${last_part_optional}"
else
((breakfix=breakfix-1))
if [ "${breakfix}" = "0" ] && [ "${last_part_optional}" = "true" ]; then
declare -g ${variable_name}="${major}.${minor}"
else
declare -g ${variable_name}="${major}.${minor}.${breakfix}"
fi
fi
set -e
}
apt_get_update()
{
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
echo "Running apt-get update..."
apt-get update -y
fi
}
# Checks if packages are installed and installs them if not
check_packages() {
if ! dpkg -s "$@" > /dev/null 2>&1; then
apt_get_update
apt-get -y install --no-install-recommends "$@"
fi
}
# Function to fetch the version released prior to the latest version
get_previous_version() {
local url=$1
local repo_url=$2
local variable_name=$3
local mode=$4
prev_version=${!variable_name}
output=$(curl -s "$repo_url");
# install jq
check_packages jq
message=$(echo "$output" | jq -r '.message')
if [[ "$mode" == "mode1" ]]; then
message="API rate limit exceeded";
elif [[ "$mode" == "mode2" ]]; then
message=""
fi
if [[ $message == "API rate limit exceeded"* ]]; then
echo -e "\nAn attempt to find latest version using GitHub Api Failed... \nReason: ${message}"
echo -e "\nAttempting to find latest version using GitHub tags."
find_prev_version_from_git_tags prev_version "$url" "tags/v"
declare -g ${variable_name}="${prev_version}"
else
echo -e "\nAttempting to find latest version using GitHub Api."
version=$(echo "$output" | jq -r '.tag_name')
declare -g ${variable_name}="${version#v}"
fi
echo "${variable_name}=${!variable_name}"
}
get_github_api_repo_url() {
local url=$1
echo "${url/https:\/\/github.com/https:\/\/api.github.com\/repos}/releases/latest"
}
install_previous_version() {
given_version=$1
requested_version=${!given_version}
local URL=$2
local mode=$3
INSTALLER_FN=$4
local REPO_URL=$(get_github_api_repo_url "$URL")
local PKG_NAME=$(get_pkg_name "${given_version}")
echo -e "\n(!) Failed to fetch the latest artifacts for ${PKG_NAME} v${requested_version}..."
get_previous_version "$URL" "$REPO_URL" requested_version $mode
echo -e "\nAttempting to install ${requested_version}"
declare -g ${given_version}="${requested_version#v}"
$INSTALLER_FN "${!given_version}"
echo "${given_version}=${!given_version}"
}
install_cosign() {
COSIGN_VERSION=$1
local URL=$2
local mode=$3
cosign_filename="/tmp/cosign_${COSIGN_VERSION}_${architecture}.deb"
cosign_url="https://github.com/sigstore/cosign/releases/latest/download/cosign_${COSIGN_VERSION}_${architecture}.deb"
curl -L "${cosign_url}" -o $cosign_filename
if grep -q "Not Found" "$cosign_filename"; then
echo -e "\n(!) Failed to fetch the latest artifacts for cosign v${COSIGN_VERSION}..."
REPO_URL=$(get_github_api_repo_url "$URL")
get_previous_version "$URL" "$REPO_URL" COSIGN_VERSION $mode
echo -e "\nAttempting to install ${COSIGN_VERSION}"
cosign_filename="/tmp/cosign_${COSIGN_VERSION}_${architecture}.deb"
cosign_url="https://github.com/sigstore/cosign/releases/latest/download/cosign_${COSIGN_VERSION}_${architecture}.deb"
curl -L "${cosign_url}" -o $cosign_filename
fi
dpkg -i $cosign_filename
rm $cosign_filename
echo "Installation of cosign succeeded with ${COSIGN_VERSION}."
}
# Install 'cosign' for validating signatures
# https://docs.sigstore.dev/cosign/overview/
ensure_cosign() {
mode=$1
if ! type cosign > /dev/null 2>&1; then
echo -e "\nAttempting to install dummy cosign version..."
COSIGN_VERSION="2.2.xyz"
echo "Installing cosign... v${COSIGN_VERSION}"
cosign_url='https://github.com/sigstore/cosign'
install_cosign "${COSIGN_VERSION}" "${cosign_url}" $mode
fi
if ! type cosign > /dev/null 2>&1; then
echo "(!) Failed to install cosign."
exit 1
fi
cosign version
}
install_tflint() {
TFLINT_VERSION=$1
curl -sSL -o /tmp/tf-downloads/${TFLINT_FILENAME} https://github.com/terraform-linters/tflint/releases/download/v${TFLINT_VERSION}/${TFLINT_FILENAME}
}
try_install_dummy_tflint_cosign_version() {
mode=$1
tflint_url='https://github.com/terraform-linters/tflint'
mkdir -p /tmp/tf-downloads
cd /tmp/tf-downloads
echo -e "\nTrying to install dummy tflint version..."
TFLINT_VERSION="0.50.XYZ"
echo "Downloading tflint...v${TFLINT_VERSION}"
TFLINT_FILENAME="tflint_linux_${architecture}.zip"
install_tflint "$TFLINT_VERSION"
if grep -q "Not Found" "/tmp/tf-downloads/${TFLINT_FILENAME}"; then
install_previous_version TFLINT_VERSION "$tflint_url" $mode "install_tflint"
fi
if [ "${TFLINT_SHA256}" != "dev-mode" ]; then
if [ "${TFLINT_SHA256}" != "automatic" ]; then
echo "${TFLINT_SHA256} *${TFLINT_FILENAME}" > tflint_checksums.txt
sha256sum --ignore-missing -c tflint_checksums.txt
else
curl -sSL -o tflint_checksums.txt https://github.com/terraform-linters/tflint/releases/download/v${TFLINT_VERSION}/checksums.txt
set +e
curl -sSL -o checksums.txt.keyless.sig https://github.com/terraform-linters/tflint/releases/download/v${TFLINT_VERSION}/checksums.txt.keyless.sig
set -e
# Check that checksums.txt.keyless.sig exists and is not empty
if [ -s checksums.txt.keyless.sig ]; then
# Validate checksums with cosign
curl -sSL -o checksums.txt.pem https://github.com/terraform-linters/tflint/releases/download/v${TFLINT_VERSION}/checksums.txt.pem
ensure_cosign $mode
cosign verify-blob \
--certificate=/tmp/tf-downloads/checksums.txt.pem \
--signature=/tmp/tf-downloads/checksums.txt.keyless.sig \
--certificate-identity-regexp="^https://github.com/terraform-linters/tflint" \
--certificate-oidc-issuer=https://token.actions.githubusercontent.com \
/tmp/tf-downloads/tflint_checksums.txt
# Ensure that checksums.txt has $TFLINT_FILENAME
grep ${TFLINT_FILENAME} /tmp/tf-downloads/tflint_checksums.txt
# Validate downloaded file
sha256sum --ignore-missing -c tflint_checksums.txt
else
# Fallback to older, GPG-based verification (pre-0.47.0 of tflint)
curl -sSL -o tflint_checksums.txt.sig https://github.com/terraform-linters/tflint/releases/download/v${TFLINT_VERSION}/checksums.txt.sig
curl -sSL -o tflint_key "${TFLINT_GPG_KEY_URI}"
gpg -q --import tflint_key
gpg --verify tflint_checksums.txt.sig tflint_checksums.txt
fi
fi
fi
unzip /tmp/tf-downloads/${TFLINT_FILENAME}
sudo mv -f tflint /usr/local/bin/
}
try_install_dummy_tflint_cosign_version "mode1"
check "tflint version as installed when mode=1" tflint --version
check "cosign version as installed when mode=1" cosign version
try_install_dummy_tflint_cosign_version "mode2"
check "tflint version as installed when mode=2" tflint --version
check "cosign version as installed when mode=2" cosign version
\ No newline at end of file
#!/bin/bash
set -e
# Import test library for `check` command
source dev-container-features-test-lib
# Check to make sure the user is vscode
check "user is vscode" whoami | grep vscode
set_error_handler() {
echo "Error occurred on line: $LINENO"
}
# Register the error handler function to be triggered on ERR signal
trap 'set_error_handler' ERR
# Trap errors and call the error handling function
trap 'handle_error' ERR
architecture="$(uname -m)"
case ${architecture} in
x86_64) architecture="amd64";;
aarch64 | armv8*) architecture="arm64";;
aarch32 | armv7* | armvhf*) architecture="arm";;
i?86) architecture="386";;
*) echo "(!) Architecture ${architecture} unsupported"; exit 1 ;;
esac
TFSEC_SHA256="automatic"
# TFSec specific tests
check "tfsec version as installed by feature" tfsec --version
# Figure out correct version of a three part version number is not passed
find_version_from_git_tags() {
local variable_name=$1
local requested_version=${!variable_name}
if [ "${requested_version}" = "none" ]; then return; fi
local repository=$2
local prefix=${3:-"tags/v"}
local separator=${4:-"."}
local last_part_optional=${5:-"false"}
if [ "$(echo "${requested_version}" | grep -o "." | wc -l)" != "2" ]; then
local escaped_separator=${separator//./\\.}
local last_part
if [ "${last_part_optional}" = "true" ]; then
last_part="(${escaped_separator}[0-9]+)?"
else
last_part="${escaped_separator}[0-9]+"
fi
local regex="${prefix}\\K[0-9]+${escaped_separator}[0-9]+${last_part}$"
local version_list="$(git ls-remote --tags ${repository} | grep -oP "${regex}" | tr -d ' ' | tr "${separator}" "." | sort -rV)"
if [ "${requested_version}" = "latest" ] || [ "${requested_version}" = "current" ] || [ "${requested_version}" = "lts" ]; then
declare -g ${variable_name}="$(echo "${version_list}" | head -n 1)"
else
set +e
declare -g ${variable_name}="$(echo "${version_list}" | grep -E -m 1 "^${requested_version//./\\.}([\\.\\s]|$)")"
set -e
fi
fi
if [ -z "${!variable_name}" ] || ! echo "${version_list}" | grep "^${!variable_name//./\\.}$" > /dev/null 2>&1; then
echo -e "Invalid ${variable_name} value: ${requested_version}\nValid values:\n${version_list}" >&2
exit 1
fi
echo "${variable_name}=${!variable_name}"
}
# Use semver logic to decrement a version number then look for the closest match
find_prev_version_from_git_tags() {
local variable_name=$1
local current_version=${!variable_name}
local repository=$2
# Normally a "v" is used before the version number, but support alternate cases
local prefix=${3:-"tags/v"}
# Some repositories use "_" instead of "." for version number part separation, support that
local separator=${4:-"."}
# Some tools release versions that omit the last digit (e.g. go)
local last_part_optional=${5:-"false"}
# Some repositories may have tags that include a suffix (e.g. actions/node-versions)
local version_suffix_regex=$6
# Try one break fix version number less if we get a failure. Use "set +e" since "set -e" can cause failures in valid scenarios.
set +e
major="$(echo "${current_version}" | grep -oE '^[0-9]+' || echo '')"
minor="$(echo "${current_version}" | grep -oP '^[0-9]+\.\K[0-9]+' || echo '')"
breakfix="$(echo "${current_version}" | grep -oP '^[0-9]+\.[0-9]+\.\K[0-9]+' 2>/dev/null || echo '')"
if [ "${minor}" = "0" ] && [ "${breakfix}" = "0" ]; then
((major=major-1))
declare -g ${variable_name}="${major}"
# Look for latest version from previous major release
find_version_from_git_tags "${variable_name}" "${repository}" "${prefix}" "${separator}" "${last_part_optional}"
# Handle situations like Go's odd version pattern where "0" releases omit the last part
elif [ "${breakfix}" = "" ] || [ "${breakfix}" = "0" ]; then
((minor=minor-1))
declare -g ${variable_name}="${major}.${minor}"
# Look for latest version from previous minor release
find_version_from_git_tags "${variable_name}" "${repository}" "${prefix}" "${separator}" "${last_part_optional}"
else
((breakfix=breakfix-1))
if [ "${breakfix}" = "0" ] && [ "${last_part_optional}" = "true" ]; then
declare -g ${variable_name}="${major}.${minor}"
else
declare -g ${variable_name}="${major}.${minor}.${breakfix}"
fi
fi
set -e
}
apt_get_update()
{
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
echo "Running apt-get update..."
apt-get update -y
fi
}
# Checks if packages are installed and installs them if not
check_packages() {
if ! dpkg -s "$@" > /dev/null 2>&1; then
apt_get_update
apt-get -y install --no-install-recommends "$@"
fi
}
# Function to fetch the version released prior to the latest version
get_previous_version() {
local url=$1
local repo_url=$2
local variable_name=$3
local mode=$4
prev_version=${!variable_name}
output=$(curl -s "$repo_url");
# install jq
check_packages jq
message=$(echo "$output" | jq -r '.message')
if [[ "$mode" == "mode1" ]]; then
message="API rate limit exceeded";
elif [[ "$mode" == "mode2" ]]; then
message=""
fi
if [[ $message == "API rate limit exceeded"* ]]; then
echo -e "\nAn attempt to find latest version using GitHub Api Failed... \nReason: ${message}"
echo -e "\nAttempting to find latest version using GitHub tags."
find_prev_version_from_git_tags prev_version "$url" "tags/v"
declare -g ${variable_name}="${prev_version}"
else
echo -e "\nAttempting to find latest version using GitHub Api."
version=$(echo "$output" | jq -r '.tag_name')
declare -g ${variable_name}="${version#v}"
fi
echo "${variable_name}=${!variable_name}"
}
get_github_api_repo_url() {
local url=$1
echo "${url/https:\/\/github.com/https:\/\/api.github.com\/repos}/releases/latest"
}
install_previous_version() {
given_version=$1
requested_version=${!given_version}
local URL=$2
local mode=$3
INSTALLER_FN=$4
local REPO_URL=$(get_github_api_repo_url "$URL")
local PKG_NAME=$(get_pkg_name "${given_version}")
echo -e "\n(!) Failed to fetch the latest artifacts for ${PKG_NAME} v${requested_version}..."
get_previous_version "$URL" "$REPO_URL" requested_version $mode
echo -e "\nAttempting to install ${requested_version}"
declare -g ${given_version}="${requested_version#v}"
$INSTALLER_FN "${!given_version}"
echo "${given_version}=${!given_version}"
}
install_tfsec() {
local TFSEC_VERSION=$1
tfsec_filename="tfsec_${TFSEC_VERSION}_linux_${architecture}.tar.gz"
curl -sSL -o /tmp/tf-downloads/${tfsec_filename} https://github.com/aquasecurity/tfsec/releases/download/v${TFSEC_VERSION}/${tfsec_filename}
}
try_install_tfsec_dummy_version() {
mode=$1
mkdir -p /tmp/tf-downloads
cd /tmp/tf-downloads
TFSEC_VERSION="1.28.XYZ"
echo -e "\nInstalling TFSEC dummy version.." v${TFSEC_VERSION}
tfsec_url='https://github.com/aquasecurity/tfsec'
tfsec_filename="tfsec_${TFSEC_VERSION}_linux_${architecture}.tar.gz"
echo "(*) Downloading TFSec... ${tfsec_filename}"
install_tfsec "$TFSEC_VERSION"
if grep -q "Not Found" "/tmp/tf-downloads/${tfsec_filename}"; then
install_previous_version TFSEC_VERSION $tfsec_url $mode "install_tfsec"
tfsec_filename="tfsec_${TFSEC_VERSION}_linux_${architecture}.tar.gz"
fi
if [ "${TFSEC_SHA256}" != "dev-mode" ]; then
if [ "${TFSEC_SHA256}" = "automatic" ]; then
curl -sSL -o tfsec_SHA256SUMS https://github.com/aquasecurity/tfsec/releases/download/v${TFSEC_VERSION}/tfsec_${TFSEC_VERSION}_checksums.txt
else
echo "${TFSEC_SHA256} *${tfsec_filename}" > tfsec_SHA256SUMS
fi
sha256sum --ignore-missing -c tfsec_SHA256SUMS
fi
mkdir -p /tmp/tf-downloads/tfsec
tar -xzf /tmp/tf-downloads/${tfsec_filename} -C /tmp/tf-downloads/tfsec
chmod a+x /tmp/tf-downloads/tfsec/tfsec
sudo mv -f /tmp/tf-downloads/tfsec/tfsec /usr/local/bin/tfsec
}
try_install_tfsec_dummy_version "mode1"
check "tfsec version as installed by test after fallbacking from the dummy version (mode 1: install using find_prev_version_from_git_tags)" tfsec --version
try_install_tfsec_dummy_version "mode2"
check "tfsec version as installed by test after fallbacking from the dummy version (mode 2: install using GitHub Api)" tfsec --version
# Report result
reportResults
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment