Skip to content

Commit e2c40a4

Browse files
committed
guix-attest: Error out if SHA256SUMS is unexpected
1 parent 4cc35da commit e2c40a4

File tree

1 file changed

+67
-27
lines changed

1 file changed

+67
-27
lines changed

contrib/guix/guix-attest

Lines changed: 67 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,15 @@ fi
102102
echo "Looking for build output SHA256SUMS fragments in ${OUTDIR_BASE}"
103103

104104
shopt -s nullglob
105-
OUTDIRS=( "$OUTDIR_BASE"/*/SHA256SUMS.part ) # This expands to an array of directories...
105+
sha256sum_fragments=( "$OUTDIR_BASE"/*/SHA256SUMS.part ) # This expands to an array of directories...
106106
shopt -u nullglob
107107

108108
noncodesigned_fragments=()
109109
codesigned_fragments=()
110110

111-
if (( ${#OUTDIRS[@]} )); then
111+
if (( ${#sha256sum_fragments[@]} )); then
112112
echo "Found build output SHA256SUMS fragments:"
113-
for outdir in "${OUTDIRS[@]}"; do
113+
for outdir in "${sha256sum_fragments[@]}"; do
114114
echo " '$outdir'"
115115
case "$outdir" in
116116
"$OUTDIR_BASE"/*-codesigned/SHA256SUMS.part)
@@ -139,6 +139,26 @@ out_name() {
139139
basename "$(dirname "$1")"
140140
}
141141

142+
shasum_already_exists() {
143+
cat <<EOF
144+
--
145+
146+
ERR: An ${1} file already exists for '${VERSION}' and attests
147+
differently. You likely previously attested to a partial build (e.g. one
148+
where you specified the HOST environment variable).
149+
150+
See the diff above for more context.
151+
152+
Hint: You may wish to remove the existing attestations and their signatures by
153+
invoking:
154+
155+
rm '${PWD}/${1}'{,.asc}
156+
157+
Then try running this script again.
158+
159+
EOF
160+
}
161+
142162
echo "Attesting to build outputs for version: '${VERSION}'"
143163
echo ""
144164

@@ -147,40 +167,60 @@ mkdir -p "$outsigdir"
147167
(
148168
cd "$outsigdir"
149169

150-
if [ -e "noncodesigned.SHA256SUMS" ]; then
151-
echo "noncodesigned.SHA256SUMS already exists, using..."
152-
elif (( ${#noncodesigned_fragments[@]} )); then
170+
temp_noncodesigned="$(mktemp)"
171+
trap 'rm -rf -- "$temp_noncodesigned"' EXIT
172+
173+
if (( ${#noncodesigned_fragments[@]} )); then
153174
cat "${noncodesigned_fragments[@]}" \
154175
| sort -u \
155176
| sort -k2 \
156-
> noncodesigned.SHA256SUMS
177+
> "$temp_noncodesigned"
178+
if [ -e noncodesigned.SHA256SUMS ]; then
179+
# The SHA256SUMS already exists, make sure it's exactly what we
180+
# expect, error out if not
181+
if diff -u noncodesigned.SHA256SUMS "$temp_noncodesigned"; then
182+
echo "A noncodesigned.SHA256SUMS file already exists for '${VERSION}' and is up-to-date."
183+
else
184+
shasum_already_exists noncodesigned.SHA256SUMS
185+
exit 1
186+
fi
187+
else
188+
mv "$temp_noncodesigned" noncodesigned.SHA256SUMS
189+
fi
157190
else
158-
echo "no noncodesigned outputs found"
191+
echo "ERR: No noncodesigned outputs found for '${VERSION}', exiting..."
192+
exit 1
159193
fi
160194

161-
if [ -e noncodesigned.SHA256SUMS ]; then
162-
# noncodesigned.SHA256SUMS already exists, or was produced, let's sanity
163-
# check it.
164-
( cd "$OUTDIR_BASE"; sha256sum -c "$outsigdir"/noncodesigned.SHA256SUMS )
165-
166-
# Now produce all.SHA256SUMS manifest
167-
if [ -e "all.SHA256SUMS" ]; then
168-
echo "all.SHA256SUMS already there!"
169-
elif (( ${#codesigned_fragments[@]} )); then
170-
cat "${OUTDIRS[@]}" \
171-
| sort -u \
172-
| sort -k2 \
173-
> all.SHA256SUMS
174-
else
175-
echo "no codesigned outputs found"
176-
fi
195+
temp_codesigned="$(mktemp)"
196+
trap 'rm -rf -- "$temp_codesigned"' EXIT
177197

178-
if [ -e all.SHA256SUMS ]; then
179-
( cd "$OUTDIR_BASE"; sha256sum -c "$outsigdir"/all.SHA256SUMS )
198+
if (( ${#codesigned_fragments[@]} )); then
199+
# Note: all.SHA256SUMS attests to all of $sha256sum_fragments, but is
200+
# not needed if there are no $codesigned_fragments
201+
cat "${sha256sum_fragments[@]}" \
202+
| sort -u \
203+
| sort -k2 \
204+
> "$temp_codesigned"
205+
if [ -e codesigned.SHA256SUMS ]; then
206+
# The SHA256SUMS already exists, make sure it's exactly what we
207+
# expect, error out if not
208+
if diff -u all.SHA256SUMS "$temp_codesigned"; then
209+
echo "An all.SHA256SUMS file already exists for '${VERSION}' and is up-to-date."
210+
else
211+
shasum_already_exists all.SHA256SUMS
212+
exit 1
213+
fi
214+
else
215+
mv "$temp_codesigned" codesigned.SHA256SUMS
180216
fi
217+
else
218+
# It is fine to have the codesigned outputs be missing (perhaps the
219+
# detached codesigs have not been published yet), just print a log
220+
# message instead of erroring out
221+
echo "INFO: No codesigned outputs found for '${VERSION}', skipping..."
181222
fi
182223

183-
184224
if [ -z "$NO_SIGN" ]; then
185225
echo "Signing SHA256SUMS to produce SHA256SUMS.asc"
186226
for i in *.SHA256SUMS; do

0 commit comments

Comments
 (0)