forked from paketo-buildpacks/jammy-tiny-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
receipts.sh
executable file
·189 lines (150 loc) · 5.16 KB
/
receipts.sh
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env bash
set -eu
set -o pipefail
readonly PROG_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly STACK_DIR="$(cd "${PROG_DIR}/.." && pwd)"
readonly BIN_DIR="${STACK_DIR}/.bin"
readonly BUILD_DIR="${STACK_DIR}/build"
# shellcheck source=SCRIPTDIR/.util/tools.sh
source "${PROG_DIR}/.util/tools.sh"
# shellcheck source=SCRIPTDIR/.util/print.sh
source "${PROG_DIR}/.util/print.sh"
function main() {
local build run receiptFilename buildReceipt runReceipt receipts
build="${BUILD_DIR}/build.oci"
run="${BUILD_DIR}/run.oci"
receiptFilename="receipt.cyclonedx.json"
buildReceipt="${BUILD_DIR}/build-${receiptFilename}"
runReceipt="${BUILD_DIR}/run-${receiptFilename}"
while [[ "${#}" != 0 ]]; do
case "${1}" in
--help|-h)
shift 1
usage
exit 0
;;
--build-image|-b)
build="${2}"
shift 2
;;
--run-image|-r)
run="${2}"
shift 2
;;
--build-receipt|-B)
buildReceipt="${2}"
shift 2
;;
--run-receipt|-R)
runReceipt="${2}"
shift 2
;;
"")
# skip if the argument is empty
shift 1
;;
*)
util::print::error "unknown argument \"${1}\""
esac
done
tools::install
# We are generating receipts for all platforms
receipts::generate::multi::arch "${build}" "${run}" "${buildReceipt}" "${runReceipt}"
util::print::success "Success! Receipts are:\n ${buildReceipt}\n ${runReceipt}\n"
}
function usage() {
cat <<-USAGE
receipts.sh [OPTIONS]
Generates receipts listing packages installed on build and run images of the
stack.
OPTIONS
--help -h prints the command usage
--build-image -b path to OCI image of build image. Defaults to
${BUILD_DIR}/build.oci
--run-image -r path to OCI image of build image
${BUILD_DIR}/run.oci
--build-receipt -B path to output build image package receipt. Defaults to
${BUILD_DIR}/build-receipt.cyclonedx.json
--run-receipt -R path to output run image package receipt. Defaults to
${BUILD_DIR}/run-receipt.cyclonedx.json
USAGE
}
function tools::install() {
util::tools::crane::install \
--directory "${BIN_DIR}"
util::tools::jam::install \
--directory "${BIN_DIR}"
util::tools::syft::install \
--directory "${BIN_DIR}"
}
# Generates syft receipts for each architecture for given oci archives
function receipts::generate::multi::arch() {
local buildArchive runArchive registryPort registryPid localRegistry imageType archiveName imageReceipt
buildArchive="${1}"
runArchive="${2}"
buildOutput="${3}"
runOutput="${4}"
registryPort=$(get::random::port)
registryPid=$(local::registry::start $registryPort)
localRegistry="127.0.0.1:$registryPort"
# Push the oci archives to the local registry
jam publish-stack \
--build-ref "$localRegistry/build" \
--build-archive $buildArchive \
--run-ref "$localRegistry/run" \
--run-archive $runArchive
# Ensure we can write to the BUILD_DIR
if [ $(stat -c %u build) = "0" ]; then
sudo chown -R "$(id -u):$(id -g)" "$BUILD_DIR"
fi
for archivePath in "${buildArchive}" "${runArchive}" ; do
archiveName=$(basename "${archivePath}") # either 'build.oci' or 'run.oci'
imageType=$(basename -s .oci "${archivePath}") # either 'build' or 'run'
util::print::title "Generating package SBOM for ${archiveName}"
for imageArch in $(crane manifest "$localRegistry/$imageType" | jq -r '.manifests[].platform.architecture'); do
if [[ "$imageType" = "build" ]]; then
dir=$(dirname ${buildOutput})
fileName=$(basename ${buildOutput})
imageReceipt="${dir}/${imageArch}-${fileName}"
elif [[ "$imageType" = "run" ]]; then
dir=$(dirname ${runOutput})
fileName=$(basename ${runOutput})
imageReceipt="${dir}/${imageArch}-${fileName}"
fi
util::print::info "Generating CycloneDX package SBOM using syft for $archiveName on platform linux/$imageArch saved as $imageReceipt"
# Generate the architecture-specific SBOM from image in the local registry
syft packages "registry:$localRegistry/$imageType" \
--output cyclonedx-json \
--file "$imageReceipt" \
--platform "linux/$imageArch"
done
done
kill $registryPid
}
# Returns a random unused port
function get::random::port() {
local port=$(shuf -i 50000-65000 -n 1)
netstat -lat | grep $port > /dev/null
if [[ $? == 1 ]] ; then
echo $port
else
echo get::random::port
fi
}
# Starts a local registry on the given port and returns the pid
function local::registry::start() {
local registryPort registryPid localRegistry
registryPort="$1"
localRegistry="127.0.0.1:$registryPort"
# Start a local in-memory registry so we can work with oci archives
PORT=$registryPort crane registry serve --insecure > /dev/null 2>&1 &
registryPid=$!
# Stop the registry if execution is interrupted
trap "kill $registryPid" 1 2 3 6
# Wait for the registry to be available
until crane catalog $localRegistry > /dev/null 2>&1; do
sleep 1
done
echo $registryPid
}
main "${@:-}"