-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathgenerate-habitat-pkg-deps.sh
More file actions
122 lines (110 loc) · 4.06 KB
/
Copy pathgenerate-habitat-pkg-deps.sh
File metadata and controls
122 lines (110 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env bash
# generate-habitat-pkg-deps.sh
#
# Queries the public Builder API for the full transitive dependency trees
# (tdeps) of this repo's top-level, deployable Habitat packages and emits a
# CycloneDX fragment covering their core-origin runtime dependencies.
#
# Motivation: only components/builder-api is a Rust project (its SBOM is
# generated separately via cargo-cyclonedx). The other deployable components
# (builder-api-proxy, builder-memcached, builder-minio, builder-datastore)
# wrap third-party binaries packaged as Habitat "core" packages, which are
# invisible to cargo tooling. This script captures those dependencies by
# asking Builder directly for each top-level package's dependency tree, the
# same way /habitat's support/sbom scripts derive its core-origin fragment.
#
# All top-level packages here are built for x86_64-linux only, so unlike
# /habitat's multi-platform SBOM job, only a single target is queried.
#
# Usage:
# bash support/sbom/generate-habitat-pkg-deps.sh > habitat-pkg-deps.cdx.json
#
# Environment:
# BLDR_URL Builder base URL (default: https://bldr.habitat.sh)
# CHANNEL Channel to inspect (default: on-prem-base)
# TARGET Habitat package target (default: x86_64-linux)
#
# Requires: curl, jq
set -euo pipefail
BLDR_URL="${BLDR_URL:-https://bldr.habitat.sh}"
CHANNEL="${CHANNEL:-on-prem-base}"
TARGET="${TARGET:-x86_64-linux}"
# The top-level, deployable Habitat packages that make up this product.
TOP_LEVEL_PACKAGES=(
"habitat/builder-api-proxy"
"habitat/builder-api"
"habitat/builder-memcached"
"habitat/builder-minio"
"habitat/builder-datastore"
)
echo "Builder URL: $BLDR_URL" >&2
echo "Channel: $CHANNEL" >&2
echo "Target: $TARGET" >&2
echo "Top-level pkgs: ${TOP_LEVEL_PACKAGES[*]}" >&2
echo "" >&2
# CORE_DEPS is a set keyed by "name@version" to deduplicate across all
# top-level packages' dependency trees.
declare -A CORE_DEPS # key: "name@version" -> "1"
declare -A CORE_DEP_META # key: "name@version" -> "name version"
add_dep() {
local name="$1" version="$2"
local key="${name}@${version}"
if [ -z "${CORE_DEPS[$key]+_}" ]; then
CORE_DEPS["$key"]="1"
CORE_DEP_META["$key"]="${name} ${version}"
fi
}
for pkg in "${TOP_LEVEL_PACKAGES[@]}"; do
origin="${pkg%%/*}"
pkg_name="${pkg##*/}"
url="${BLDR_URL}/v1/depot/channels/${origin}/${CHANNEL}/pkgs/${pkg_name}/latest?target=${TARGET}"
printf " Fetching %-40s [%-16s] ... " "${pkg}" "${TARGET}" >&2
if response=$(curl -sSf "$url" 2>/dev/null); then
mapfile -t deps < <(
echo "$response" \
| jq -r '.tdeps[]? | select(.origin == "core") | "\(.name)/\(.version)"' \
2>/dev/null \
|| true
)
printf "%d core deps\n" "${#deps[@]}" >&2
for dep in "${deps[@]}"; do
[ -z "$dep" ] && continue
add_dep "${dep%%/*}" "${dep##*/}"
done
else
echo "ERROR: failed to fetch ${pkg} from channel ${CHANNEL} (target ${TARGET})" >&2
exit 1
fi
done
echo "" >&2
echo "Unique core {name, version} pairs found: ${#CORE_DEPS[@]}" >&2
echo "" >&2
if [ "${#CORE_DEPS[@]}" -eq 0 ]; then
echo "ERROR: No core-origin packages found. Check BLDR_URL, CHANNEL, and TOP_LEVEL_PACKAGES." >&2
exit 1
fi
# Build one CycloneDX component per unique {name, version}, using the same
# naming/purl convention as /habitat's habitat-core-deps.cyclonedx.json so
# BlackDuck maps them to the same KB entries rather than creating duplicates.
component_jsons=()
for key in $(printf '%s\n' "${!CORE_DEP_META[@]}" | sort); do
read -r pkg_name version <<< "${CORE_DEP_META[$key]}"
display_name="Habitat core_${pkg_name}"
purl="pkg:generic/${pkg_name}@${version}"
component_jsons+=(
"$(jq -cn \
--arg name "$display_name" \
--arg version "$version" \
--arg purl "$purl" \
'{type: "library", name: $name, version: $version, purl: $purl}')"
)
done
components_json=$(printf '%s\n' "${component_jsons[@]}" | jq -s '.')
jq -n \
--argjson components "$components_json" \
'{
bomFormat: "CycloneDX",
specVersion: "1.4",
version: 1,
components: $components
}'