Skip to content

Commit 396b463

Browse files
fix(sbom): handle different installation scenarios
1 parent 5c8e0cc commit 396b463

File tree

4 files changed

+41
-30
lines changed

4 files changed

+41
-30
lines changed

src/sbom/install.sh

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,46 @@ check_packages() {
2424
fi
2525
}
2626

27-
# Figure out correct version of a three part version number is not passed
27+
# Resolve "latest" version by following the GitHub redirect (no API rate limit)
28+
resolve_latest_version() {
29+
local latest_url="https://github.com/microsoft/sbom-tool/releases/latest"
30+
local redirect_url
31+
redirect_url=$(curl -sIL -o /dev/null -w '%{url_effective}' "${latest_url}")
32+
if [ -z "${redirect_url}" ] || [ "${redirect_url}" = "${latest_url}" ]; then
33+
echo "ERROR: Failed to resolve latest sbom-tool version from GitHub." >&2
34+
exit 1
35+
fi
36+
# Extract tag from redirect URL (e.g. .../releases/tag/v4.1.5 -> v4.1.5)
37+
echo "${redirect_url##*/}"
38+
}
39+
40+
# Validate that a given version/tag exists by checking the download URL returns 200
2841
validate_version_exists() {
2942
local variable_name=$1
30-
local requested_version=$2
31-
if [ "${requested_version}" = "latest" ]; then requested_version=$(curl -sL https://api.github.com/repos/microsoft/sbom-tool/releases/latest | jq -r ".tag_name"); fi
32-
local version_list
33-
version_list=$(curl -sL https://api.github.com/repos/microsoft/sbom-tool/releases | jq -r ".[].tag_name")
34-
if [ -z "${variable_name}" ] || ! echo "${version_list}" | grep "${requested_version}" >/dev/null 2>&1; then
35-
echo -e "Invalid ${variable_name} value: ${requested_version}\nValid values:\n${version_list}" >&2
43+
local requested_version=$2
44+
local check_url="https://github.com/microsoft/sbom-tool/releases/tag/${requested_version}"
45+
local http_code
46+
http_code=$(curl -sIL -o /dev/null -w '%{http_code}' "${check_url}")
47+
if [ "${http_code}" != "200" ]; then
48+
echo "ERROR: ${variable_name} value '${requested_version}' not found (HTTP ${http_code})." >&2
49+
echo "Check available versions at: https://github.com/microsoft/sbom-tool/releases" >&2
3650
exit 1
3751
fi
3852
echo "${variable_name}=${requested_version}"
3953
}
4054

4155
# make sure we have curl
42-
check_packages curl jq ca-certificates libicu-dev
56+
check_packages curl ca-certificates libicu-dev
4357

4458
# Normalize version: add 'v' prefix if missing
4559
if [ "${SBOM_TOOL_VERSION}" != "latest" ] && [[ "${SBOM_TOOL_VERSION}" != v* ]]; then
4660
SBOM_TOOL_VERSION="v${SBOM_TOOL_VERSION}"
4761
fi
4862

49-
# make sure version is available
50-
if [ "${SBOM_TOOL_VERSION}" = "latest" ]; then SBOM_TOOL_VERSION=$(curl -sL https://api.github.com/repos/microsoft/sbom-tool/releases/latest | jq -r ".tag_name"); fi
63+
# Resolve latest or validate the requested version
64+
if [ "${SBOM_TOOL_VERSION}" = "latest" ]; then
65+
SBOM_TOOL_VERSION=$(resolve_latest_version)
66+
fi
5167
validate_version_exists SBOM_TOOL_VERSION "${SBOM_TOOL_VERSION}"
5268

5369
# download and install binary

test/sbom/install_sbom_specific_version.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
22

33
# Scenario: install_sbom_specific_version
4-
# Verifies that a specific version of sbom-tool (2.2.5) installs correctly
4+
# Verifies that a specific version of sbom-tool (3.0.1) installs correctly
55

66
set -e
77

@@ -10,8 +10,8 @@ source dev-container-features-test-lib
1010
# Verify sbom-tool is installed
1111
check "sbom-tool is installed" bash -c "which sbom-tool"
1212

13-
# Verify sbom-tool can run
14-
check "sbom-tool version runs" bash -c "sbom-tool version"
13+
# Verify the exact version is installed
14+
check "sbom-tool version is 3.0.1" bash -c "sbom-tool version | grep '3.0.1'"
1515

1616
# Verify sbom-tool binary is in the expected location
1717
check "sbom-tool in /usr/local/bin" bash -c "ls /usr/local/bin/sbom-tool"

test/sbom/invalid_version.sh

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,24 @@ source dev-container-features-test-lib
1313
# sbom-tool should be installed (the scenario uses "latest")
1414
check "sbom-tool is installed" bash -c "which sbom-tool"
1515

16-
# Verify that a completely fake version does NOT appear in the releases list
17-
check "fake version not in releases" bash -c \
18-
"! curl -sL https://api.github.com/repos/microsoft/sbom-tool/releases | jq -r '.[].tag_name' | grep -qx 'v99.99.99'"
19-
20-
# Simulate the install script's validation: attempt to match a non-existent
21-
# version against the release list and confirm it fails
22-
check "validation rejects non-existent version" bash -c '
23-
version_list=$(curl -sL https://api.github.com/repos/microsoft/sbom-tool/releases | jq -r ".[].tag_name")
24-
if echo "${version_list}" | grep -qx "v99.99.99"; then
25-
echo "ERROR: fake version was found in release list"
16+
# Verify that a fake version returns non-200 from GitHub releases
17+
check "fake version not in releases" bash -c '
18+
http_code=$(curl -sIL -o /dev/null -w "%{http_code}" "https://github.com/microsoft/sbom-tool/releases/tag/v99.99.99")
19+
if [ "${http_code}" = "200" ]; then
20+
echo "ERROR: fake version v99.99.99 returned HTTP 200"
2621
exit 1
2722
fi
28-
echo "Correctly rejected non-existent version v99.99.99"
23+
echo "Correctly rejected non-existent version v99.99.99 (HTTP ${http_code})"
2924
'
3025

31-
# Also verify that a valid version IS accepted by the same logic
26+
# Verify that a valid version IS accepted (returns HTTP 200)
3227
check "validation accepts a real version" bash -c '
33-
version_list=$(curl -sL https://api.github.com/repos/microsoft/sbom-tool/releases | jq -r ".[].tag_name")
34-
if ! echo "${version_list}" | grep -q "0.3.3"; then
35-
echo "ERROR: valid version 0.3.3 was not found in release list"
28+
http_code=$(curl -sIL -o /dev/null -w "%{http_code}" "https://github.com/microsoft/sbom-tool/releases/tag/v3.0.1")
29+
if [ "${http_code}" != "200" ]; then
30+
echo "ERROR: valid version v3.0.1 returned HTTP ${http_code}"
3631
exit 1
3732
fi
38-
echo "Correctly accepted valid version 0.3.3"
33+
echo "Correctly accepted valid version v3.0.1 (HTTP ${http_code})"
3934
'
4035

4136
reportResults

test/sbom/scenarios.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
1212
"features": {
1313
"sbom": {
14-
"version": "2.2.5"
14+
"version": "3.0.1"
1515
}
1616
}
1717
},

0 commit comments

Comments
 (0)