Skip to content

Commit 6a98903

Browse files
authored
[Ubuntu] Add checksum validation for Aliyun CLI, CMake, Docker Compose v2 (#8612)
* Add checksums for Aliyun, CMake, Docker Compose v2 * After review changes * Download Aliyun CLI from github release
1 parent 62cd627 commit 6a98903

File tree

4 files changed

+68
-10
lines changed

4 files changed

+68
-10
lines changed

images/linux/scripts/helpers/install.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,42 @@ get_github_package_hash() {
152152
echo "$result"
153153
}
154154

155+
get_hash_from_remote_file() {
156+
local url=$1
157+
local keywords=("$2" "$3")
158+
local delimiter=${4:-' '}
159+
local word_number=${5:-1}
160+
161+
if [[ -z "${keywords[0]}" || -z "$url" ]]; then
162+
echo "File name and/or URL is not specified."
163+
exit 1
164+
fi
165+
166+
matching_line=$(curl -fsSL "$url" | tr -d '`')
167+
for keyword in "${keywords[@]}"; do
168+
matching_line=$(echo "$matching_line" | grep "$keyword")
169+
done
170+
171+
if [[ "$(echo "$matching_line" | wc -l)" -gt 1 ]]; then
172+
echo "Multiple lines found including the words: ${keywords[*]}. Please use a more specific filter."
173+
exit 1
174+
fi
175+
176+
if [[ -z "$matching_line" ]]; then
177+
echo "Keywords (${keywords[*]}) not found in the file with hashes."
178+
exit 1
179+
fi
180+
181+
result=$(echo "$matching_line" | cut -d "$delimiter" -f "$word_number" | tr -d -c '[:alnum:]')
182+
if [[ ${#result} -ne 64 && ${#result} -ne 128 ]]; then
183+
echo "Invalid result length. Expected 64 or 128 characters. Please check delimiter and/or word_number parameters."
184+
echo "Result: $result"
185+
exit 1
186+
fi
187+
188+
echo "$result"
189+
}
190+
155191
use_checksum_comparison() {
156192
local file_path=$1
157193
local checksum=$2

images/linux/scripts/installers/aliyun-cli.sh

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
################################################################################
33
## File: aliyun-cli.sh
44
## Desc: Installs Alibaba Cloud CLI
5+
## Supply chain security: Alibaba Cloud CLI - checksum validation
56
################################################################################
67

78
# Source the helpers for use with the script
@@ -11,14 +12,22 @@ source $HELPER_SCRIPTS/install.sh
1112
# Install Alibaba Cloud CLI
1213
# Pin tool version on ubuntu20 due to issues with GLIBC_2.32 not available
1314
if isUbuntu20; then
14-
toolsetVersion=$(get_toolset_value '.aliyunCli.version')
15-
downloadUrl="https://github.com/aliyun/aliyun-cli/releases/download/v$toolsetVersion/aliyun-cli-linux-$toolsetVersion-amd64.tgz"
15+
toolset_version=$(get_toolset_value '.aliyunCli.version')
16+
download_url="https://github.com/aliyun/aliyun-cli/releases/download/v$toolset_version/aliyun-cli-linux-$toolset_version-amd64.tgz"
17+
hash_url="https://github.com/aliyun/aliyun-cli/releases/download/v$toolset_version/SHASUMS256.txt"
1618
else
17-
downloadUrl="https://aliyuncli.alicdn.com/aliyun-cli-linux-latest-amd64.tgz"
19+
download_url=$(get_github_package_download_url "aliyun/aliyun-cli" "contains(\"aliyun-cli-linux\") and endswith(\"amd64.tgz\")")
20+
hash_url="https://github.com/aliyun/aliyun-cli/releases/latest/download/SHASUMS256.txt"
1821
fi
1922

20-
download_with_retries $downloadUrl "/tmp"
21-
tar xzf /tmp/aliyun-cli-linux-*-amd64.tgz
23+
package_name="aliyun-cli-linux-amd64.tgz"
24+
download_with_retries "$download_url" "/tmp" "$package_name"
25+
26+
# Supply chain security - Alibaba Cloud CLI
27+
external_hash=$(get_hash_from_remote_file "$hash_url" "aliyun-cli-linux" "amd64.tgz")
28+
use_checksum_comparison "/tmp/$package_name" "$external_hash"
29+
30+
tar xzf "/tmp/$package_name"
2231
mv aliyun /usr/local/bin
2332

2433
invoke_tests "CLI.Tools" "Aliyun CLI"

images/linux/scripts/installers/cmake.sh

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
################################################################################
33
## File: cmake.sh
44
## Desc: Installs CMake
5+
## Supply chain security: CMake - checksum validation
56
################################################################################
67

78
# Source the helpers for use with the script
@@ -12,9 +13,15 @@ echo "Checking to see if the installer script has already been run"
1213
if command -v cmake; then
1314
echo "cmake is already installed"
1415
else
15-
downloadUrl=$(get_github_package_download_url "Kitware/CMake" "endswith(\"inux-x86_64.sh\")")
16-
curl -fsSL ${downloadUrl} -o cmakeinstall.sh \
17-
&& chmod +x cmakeinstall.sh \
16+
# Download script to install CMake
17+
download_url=$(get_github_package_download_url "Kitware/CMake" "endswith(\"inux-x86_64.sh\")")
18+
curl -fsSL "${download_url}" -o cmakeinstall.sh
19+
# Supply chain security - CMake
20+
hash_url=$(get_github_package_download_url "Kitware/CMake" "endswith(\"SHA-256.txt\")")
21+
external_hash=$(get_hash_from_remote_file "$hash_url" "linux-x86_64.sh")
22+
use_checksum_comparison "cmakeinstall.sh" "$external_hash"
23+
# Install CMake and remove the install script
24+
chmod +x cmakeinstall.sh \
1825
&& ./cmakeinstall.sh --prefix=/usr/local --exclude-subdir \
1926
&& rm cmakeinstall.sh
2027
fi

images/linux/scripts/installers/docker.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
################################################################################
33
## File: docker.sh
44
## Desc: Installs docker onto the image
5+
## Supply chain security: Docker Compose v2 - checksum validation
56
################################################################################
67

78
# Source the helpers for use with the script
@@ -19,8 +20,13 @@ apt-get install --no-install-recommends docker-ce docker-ce-cli containerd.io do
1920

2021
# Install docker compose v2 from releases
2122
URL=$(get_github_package_download_url "docker/compose" "contains(\"compose-linux-x86_64\")")
22-
curl -fsSL $URL -o /usr/libexec/docker/cli-plugins/docker-compose
23-
chmod +x /usr/libexec/docker/cli-plugins/docker-compose
23+
curl -fsSL "${URL}" -o /tmp/docker-compose
24+
# Supply chain security - CMake
25+
hash_url=$(get_github_package_download_url "docker/compose" "contains(\"checksums.txt\")")
26+
external_hash=$(get_hash_from_remote_file "$hash_url" "compose-linux-x86_64")
27+
use_checksum_comparison "/tmp/docker-compose" "$external_hash"
28+
install /tmp/docker-compose /usr/libexec/docker/cli-plugins/docker-compose
29+
2430

2531
# docker from official repo introduced different GID generation: https://github.com/actions/runner-images/issues/8157
2632
gid=$(cut -d ":" -f 3 /etc/group | grep "^1..$" | sort -n | tail -n 1 | awk '{ print $1+1 }')

0 commit comments

Comments
 (0)