Skip to content

Commit d503026

Browse files
committed
Merge bitcoin/bitcoin#22182: guix: Overhaul how guix-{attest,verify} works and hierarchy
e2c40a4 guix-attest: Error out if SHA256SUMS is unexpected (Carl Dong) 4cc35da Rewrite guix-{attest,verify} for new hier (Carl Dong) 28a9c9b Make SHA256SUMS fragment right after build (Carl Dong) Pull request description: Based on: #22075 Code reviewers: I recommend reading the new `guix-{attest,verify}` files instead of trying to read the diff The following changes resolve many usability improvements which were pointed out to me: 1. Some maintainers like to extract their "uncodesigned tarball" inside the `output/` directory, resulting in the older `guix-attest` mistakenly attesting to the extracted contents 2. Maintainers whose GPG keys reside on an external smartcard often need to physically interact with the smartcard as a way to approve the signing operation, having one signature per platform means a lot of fidgeting 3. Maintainers wishing to sign on a separate machine now has the option of transferring only a subtree of `output/`, namely `output/*/SHA256SUMS.part`, in order to perform a signature (you may need to specify an `$OUTDIR_BASE` env var) 4. An `all.SHA256SUMS` file should be usable as the base `SHA256SUMS` in bitcoin core torrents and on the release server. For those who sign on an separate machine than the one you do builds on, the following steps will work: 1. `env GUIX_SIGS_REPO=/home/achow101/guix.sigs SIGNER=achow101 NO_SIGN=1 ./contrib/guix/guix-attest` 2. Copy `/home/achow101/guix.sigs/<tag>/achow101` (which does not yet have signatures) to signing machine 3. Sign the `SHA256SUMS` files: ```bash for i in "<path-to-achow101>/*.SHA256SUMS"; do gpg --detach-sign --local-user "<your-key-here>" --armor --output "$i"{.asc,} done ``` 5. Upload `<path-to-achow101>` (now with signatures) to `guix.sigs` ----- After this change, output directories will now include a `SHA256SUMS.part` fragment, created immediately after a successful build: ``` output └── x86_64-w64-mingw32 ├── bitcoin-4e069f7589da-win64-debug.zip ├── bitcoin-4e069f7589da-win64-setup-unsigned.exe ├── bitcoin-4e069f7589da-win64.zip ├── bitcoin-4e069f7589da-win-unsigned.tar.gz └── SHA256SUMS.part ``` These `SHA256SUMS.part` fragments look something like: ``` 3ebd7262b1a0a5bb757fef1f70e7e14033c70f98c059bc4dbfee5d1992b25825 dist-archive/bitcoin-4e069f7589da.tar.gz def2e7d3de5ab3e3f955344e75151df4f33713f9101f5295bd13c9375bdf633b x86_64-w64-mingw32/bitcoin-4e069f7589da-win64-debug.zip 643049fe3ee4a4e83a1739607e67b11b7c9b1a66208a6f35a9ff634ba795500e x86_64-w64-mingw32/bitcoin-4e069f7589da-win64-setup-unsigned.exe a247a1ccec0ccc2e138c648284bd01f6a761f2d8d6d07d91b5b4a6670ec3f288 x86_64-w64-mingw32/bitcoin-4e069f7589da-win-unsigned.tar.gz fab76a836dcc592e39c04fd2396696633fb6eb56e39ecbf6c909bd173ed4280c x86_64-w64-mingw32/bitcoin-4e069f7589da-win64.zip ``` Meaning that they are valid `SHA256SUMS` files when `sha256sum --check`'d at the `guix-build-*/output` directory level When `guix-attest` is invoked, these `SHA256SUMS.part` files are combined and sorted (by `-k2`, `LC_ALL=C`) to create: 1. `noncodesigned.SHA256SUMS` for a manifest of all non-codesigned outputs, and 3. `all.SHA256SUMS` for a manifest of all outputs including non-codesigned outputs Then both files are signed, resulting in the following `guix.sigs` hierarchy: ``` 4e069f7589da/ └── dongcarl ├── all.SHA256SUMS ├── all.SHA256SUMS.asc ├── noncodesigned.SHA256SUMS └── noncodesigned.SHA256SUMS.asc ``` ACKs for top commit: achow101: ACK e2c40a4 hebasto: ACK e2c40a4, tested on Linux Mint 20.1 (x86_64) with and w/o `NO_SIGN=1`. Changes in `contrib/guix/libexec/codesign.sh` and `contrib/guix/guix-verify` are reviewed only. Tree-SHA512: 618aacefb0eb6595735a9ab6a98ea6598fce65f9ccf33fa1e7ef93bf140c0f6cfc16e34870c6aa3e4777dd3f004b92a82a994141879870141742df948ec59c1f
2 parents 7c561be + e2c40a4 commit d503026

File tree

5 files changed

+193
-125
lines changed

5 files changed

+193
-125
lines changed

contrib/guix/guix-attest

Lines changed: 101 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -99,24 +99,34 @@ fi
9999
# We should be able to find at least one output
100100
################
101101

102-
echo "Looking for build output directories in ${OUTDIR_BASE}"
102+
echo "Looking for build output SHA256SUMS fragments in ${OUTDIR_BASE}"
103103

104104
shopt -s nullglob
105-
OUTDIRS=( "${OUTDIR_BASE}"/* ) # 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

108-
if (( ${#OUTDIRS[@]} )); then
109-
echo "Found build output directories:"
110-
for outdir in "${OUTDIRS[@]}"; do
108+
noncodesigned_fragments=()
109+
codesigned_fragments=()
110+
111+
if (( ${#sha256sum_fragments[@]} )); then
112+
echo "Found build output SHA256SUMS fragments:"
113+
for outdir in "${sha256sum_fragments[@]}"; do
111114
echo " '$outdir'"
115+
case "$outdir" in
116+
"$OUTDIR_BASE"/*-codesigned/SHA256SUMS.part)
117+
codesigned_fragments+=("$outdir")
118+
;;
119+
*)
120+
noncodesigned_fragments+=("$outdir")
121+
;;
122+
esac
112123
done
113124
echo
114125
else
115-
echo "ERR: Could not find any build output directories in ${OUTDIR_BASE}"
126+
echo "ERR: Could not find any build output SHA256SUMS fragments in ${OUTDIR_BASE}"
116127
exit 1
117128
fi
118129

119-
120130
##############
121131
## Attest ##
122132
##############
@@ -126,82 +136,105 @@ fi
126136
# HOST: The output directory being attested
127137
#
128138
out_name() {
129-
basename "$1"
139+
basename "$(dirname "$1")"
130140
}
131141

132-
# Usage: out_sig_dir $outdir
133-
#
134-
# outdir: The output directory being attested
135-
#
136-
out_sig_dir() {
137-
echo "$GUIX_SIGS_REPO/$VERSION/$(out_name "$1")/$signer_name"
138-
}
142+
shasum_already_exists() {
143+
cat <<EOF
144+
--
139145
140-
# Accumulate a list of signature directories that already exist...
141-
outdirs_already_attested_to=()
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).
142149
143-
echo "Attesting to build outputs for version: '${VERSION}'"
144-
echo ""
150+
See the diff above for more context.
145151
146-
# MAIN LOGIC: Loop through each output for VERSION and attest to output in
147-
# GUIX_SIGS_REPO as SIGNER, if attestation does not exist
148-
for outdir in "${OUTDIRS[@]}"; do
149-
if [ -e "${outdir}/SKIPATTEST.TAG" ]; then
150-
echo "${outname}: SKIPPING: Output directory marked with SKIPATTEST.TAG file"
151-
continue
152-
fi
153-
outname="$(out_name "$outdir")"
154-
outsigdir="$(out_sig_dir "$outdir")"
155-
if [ -e "$outsigdir" ]; then
156-
echo "${outname}: SKIPPING: Signature directory already exists in the specified guix.sigs repository"
157-
outdirs_already_attested_to+=("$outdir")
158-
else
159-
# Clean up incomplete sigdir if something fails (likely gpg)
160-
trap 'rm -rf "$outsigdir"' ERR
152+
Hint: You may wish to remove the existing attestations and their signatures by
153+
invoking:
161154
162-
mkdir -p "$outsigdir"
155+
rm '${PWD}/${1}'{,.asc}
163156
164-
(
165-
cd "$outdir"
157+
Then try running this script again.
166158
167-
if [ -e inputs.SHA256SUMS ]; then
168-
echo "${outname}: Including existent input SHA256SUMS"
169-
cat inputs.SHA256SUMS >> "$outsigdir"/SHA256SUMS
170-
fi
159+
EOF
160+
}
171161

172-
echo "${outname}: Hashing build outputs to produce SHA256SUMS"
173-
files="$(find -L . -type f ! -iname '*.SHA256SUMS')"
174-
if [ -n "$files" ]; then
175-
cut -c3- <<< "$files" | env LC_ALL=C sort | xargs sha256sum >> "$outsigdir"/SHA256SUMS
162+
echo "Attesting to build outputs for version: '${VERSION}'"
163+
echo ""
164+
165+
outsigdir="$GUIX_SIGS_REPO/$VERSION/$signer_name"
166+
mkdir -p "$outsigdir"
167+
(
168+
cd "$outsigdir"
169+
170+
temp_noncodesigned="$(mktemp)"
171+
trap 'rm -rf -- "$temp_noncodesigned"' EXIT
172+
173+
if (( ${#noncodesigned_fragments[@]} )); then
174+
cat "${noncodesigned_fragments[@]}" \
175+
| sort -u \
176+
| sort -k2 \
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."
176183
else
177-
echo "ERR: ${outname}: No outputs found in '${outdir}'"
184+
shasum_already_exists noncodesigned.SHA256SUMS
178185
exit 1
179186
fi
180-
)
181-
if [ -z "$NO_SIGN" ]; then
182-
echo "${outname}: Signing SHA256SUMS to produce SHA256SUMS.asc"
183-
gpg --detach-sign --local-user "$gpg_key_name" --armor --output "$outsigdir"/SHA256SUMS.asc "$outsigdir"/SHA256SUMS
184187
else
185-
echo "${outname}: Not signing SHA256SUMS as \$NO_SIGN is not empty"
188+
mv "$temp_noncodesigned" noncodesigned.SHA256SUMS
186189
fi
187-
echo ""
188-
189-
trap - ERR # Reset ERR trap
190+
else
191+
echo "ERR: No noncodesigned outputs found for '${VERSION}', exiting..."
192+
exit 1
190193
fi
191-
done
192-
193-
if (( ${#outdirs_already_attested_to[@]} )); then
194-
# ...so that we can print them out nicely in a warning message
195-
cat << EOF
196194

197-
WARN: Signature directories from '$signer_name' already exist in the specified
198-
guix.sigs repository for the following output directories and were
199-
skipped:
195+
temp_codesigned="$(mktemp)"
196+
trap 'rm -rf -- "$temp_codesigned"' EXIT
197+
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
216+
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..."
222+
fi
200223

201-
EOF
202-
for outdir in "${outdirs_already_attested_to[@]}"; do
203-
echo " '${outdir}'"
204-
echo " Corresponds to: '$(out_sig_dir "$outdir")'"
224+
if [ -z "$NO_SIGN" ]; then
225+
echo "Signing SHA256SUMS to produce SHA256SUMS.asc"
226+
for i in *.SHA256SUMS; do
227+
if [ ! -e "$i".asc ]; then
228+
gpg --detach-sign \
229+
--local-user "$gpg_key_name" \
230+
--armor \
231+
--output "$i".asc "$i"
232+
else
233+
echo "Signature already there"
234+
fi
235+
done
236+
else
237+
echo "Not signing SHA256SUMS as \$NO_SIGN is not empty"
238+
fi
205239
echo ""
206-
done
207-
fi
240+
)

contrib/guix/guix-verify

Lines changed: 69 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -56,58 +56,87 @@ cmd_usage
5656
exit 1
5757
fi
5858

59-
################
60-
# We should be able to find at least one output
61-
################
59+
##############
60+
## Verify ##
61+
##############
6262

6363
OUTSIGDIR_BASE="${GUIX_SIGS_REPO}/${VERSION}"
64-
echo "Looking for output signature directories in '${OUTSIGDIR_BASE}'"
64+
echo "Looking for signature directories in '${OUTSIGDIR_BASE}'"
65+
echo ""
66+
67+
# Usage: verify compare_manifest current_manifest
68+
verify() {
69+
local compare_manifest="$1"
70+
local current_manifest="$2"
71+
if ! gpg --quiet --batch --verify "$current_manifest".asc "$current_manifest" 1>&2; then
72+
echo "ERR: Failed to verify GPG signature in '${current_manifest}'"
73+
echo ""
74+
echo "Hint: Either the signature is invalid or the public key is missing"
75+
echo ""
76+
elif ! diff --report-identical "$compare_manifest" "$current_manifest" 1>&2; then
77+
echo "ERR: The SHA256SUMS attestation in these two directories differ:"
78+
echo " '${compare_manifest}'"
79+
echo " '${current_manifest}'"
80+
echo ""
81+
else
82+
echo "Verified: '${current_manifest}'"
83+
echo ""
84+
fi
85+
}
6586

6687
shopt -s nullglob
67-
OUTSIGDIRS=( "$OUTSIGDIR_BASE"/* ) # This expands to an array of directories...
88+
all_noncodesigned=( "$OUTSIGDIR_BASE"/*/noncodesigned.SHA256SUMS )
6889
shopt -u nullglob
6990

70-
if (( ${#OUTSIGDIRS[@]} )); then
71-
echo "Found output signature directories:"
72-
for outsigdir in "${OUTSIGDIRS[@]}"; do
73-
echo " '$outsigdir'"
91+
echo "--------------------"
92+
echo ""
93+
if (( ${#all_noncodesigned[@]} )); then
94+
compare_noncodesigned="${all_noncodesigned[0]}"
95+
96+
for current_manifest in "${all_noncodesigned[@]}"; do
97+
verify "$compare_noncodesigned" "$current_manifest"
7498
done
75-
echo
99+
100+
echo "DONE: Checking output signatures for noncodesigned.SHA256SUMS"
101+
echo ""
76102
else
77-
echo "ERR: Could not find any output signature directories in ${OUTSIGDIR_BASE}"
78-
exit 1
103+
echo "WARN: No signature directories with noncodesigned.SHA256SUMS found"
104+
echo ""
79105
fi
80106

107+
shopt -s nullglob
108+
all_all=( "$OUTSIGDIR_BASE"/*/all.SHA256SUMS )
109+
shopt -u nullglob
81110

82-
##############
83-
## Verify ##
84-
##############
111+
echo "--------------------"
112+
echo ""
113+
if (( ${#all_all[@]} )); then
114+
compare_all="${all_all[0]}"
85115

86-
# MAIN LOGIC: Loop through each output for VERSION and check that the SHA256SUMS
87-
# and SHA256SUMS.asc file match between signers, using the first
88-
# available signer as the arbitrary comparison base.
89-
for outsigdir in "${OUTSIGDIRS[@]}"; do
90-
echo "BEGIN: Checking output signatures for $(basename "$outsigdir")"
91-
echo ""
92-
signer_dirs=( "$outsigdir"/* ) # This expands to an array of directories...
93-
compare_signer_dir="${signer_dirs[0]}" # ...we just want the first one
94-
for current_signer_dir in "${signer_dirs[@]}"; do
95-
if ! gpg --quiet --batch --verify "$current_signer_dir"/SHA256SUMS.asc "$current_signer_dir"/SHA256SUMS; then
96-
echo "ERR: Failed to verify GPG signature in '${current_signer_dir}/SHA256SUMS.asc'"
97-
echo ""
98-
echo "Hint: Either the signature is invalid or the public key is missing"
99-
echo ""
100-
elif ! diff --report-identical "$compare_signer_dir"/SHA256SUMS "$current_signer_dir"/SHA256SUMS; then
101-
echo "ERR: The SHA256SUMS attestation in these two directories differ:"
102-
echo " '${compare_signer_dir}'"
103-
echo " '${current_signer_dir}'"
104-
echo ""
105-
else
106-
echo "Verified: '${current_signer_dir}'"
107-
echo ""
108-
fi
116+
for current_manifest in "${all_all[@]}"; do
117+
verify "$compare_all" "$current_manifest"
109118
done
110-
echo "DONE: Checking output signatures for $(basename "$outsigdir")"
119+
120+
# Sanity check: there should be no entries that exist in
121+
# noncodesigned.SHA256SUMS that doesn't exist in all.SHA256SUMS
122+
if [[ "$(comm -23 <(sort "$compare_noncodesigned") <(sort "$compare_all") | wc -c)" -ne 0 ]]; then
123+
echo "ERR: There are unique lines in noncodesigned.SHA256SUMS which"
124+
echo " do not exist in all.SHA256SUMS, something went very wrong."
125+
exit 1
126+
fi
127+
128+
echo "DONE: Checking output signatures for all.SHA256SUMS"
111129
echo ""
130+
else
131+
echo "WARN: No signature directories with all.SHA256SUMS found"
132+
echo ""
133+
fi
134+
135+
echo "===================="
136+
echo ""
137+
if (( ${#all_noncodesigned[@]} + ${#all_all[@]} == 0 )); then
138+
echo "ERR: Unable to perform any verifications as no signature directories"
139+
echo " were found"
112140
echo ""
113-
done
141+
exit 1
142+
fi

contrib/guix/libexec/build.sh

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -230,20 +230,7 @@ if [ ! -e "$GIT_ARCHIVE" ]; then
230230
git archive --prefix="${DISTNAME}/" --output="$GIT_ARCHIVE" HEAD
231231
fi
232232

233-
# tmpdir="$(mktemp -d)"
234-
# (
235-
# cd "$tmpdir"
236-
# mkdir -p inputs
237-
# ln -sf --target-directory=inputs "$GIT_ARCHIVE"
238-
239-
# mkdir -p "$OUTDIR"
240-
# find -L inputs -type f -print0 | xargs -0 sha256sum > "${OUTDIR}/inputs.SHA256SUMS"
241-
# )
242-
243233
mkdir -p "$OUTDIR"
244-
cat << EOF > "$OUTDIR"/inputs.SHA256SUMS
245-
$(sha256sum "$GIT_ARCHIVE" | cut -d' ' -f1) inputs/$(basename "$GIT_ARCHIVE")
246-
EOF
247234

248235
###########################
249236
# Binary Tarball Building #
@@ -450,3 +437,13 @@ mkdir -p "$DISTSRC"
450437
rm -rf "$ACTUAL_OUTDIR"
451438
mv --no-target-directory "$OUTDIR" "$ACTUAL_OUTDIR" \
452439
|| ( rm -rf "$ACTUAL_OUTDIR" && exit 1 )
440+
441+
(
442+
cd /outdir-base
443+
{
444+
echo "$GIT_ARCHIVE"
445+
find "$ACTUAL_OUTDIR" -type f
446+
} | xargs realpath --relative-base="$PWD" \
447+
| xargs sha256sum \
448+
| sponge "$ACTUAL_OUTDIR"/SHA256SUMS.part
449+
)

contrib/guix/libexec/codesign.sh

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ if [ ! -e "$CODESIGNATURE_GIT_ARCHIVE" ]; then
5555
fi
5656

5757
mkdir -p "$OUTDIR"
58-
cat << EOF > "$OUTDIR"/inputs.SHA256SUMS
59-
$(sha256sum "$UNSIGNED_TARBALL" | cut -d' ' -f1) inputs/$(basename "$UNSIGNED_TARBALL")
60-
$(sha256sum "$CODESIGNATURE_GIT_ARCHIVE" | cut -d' ' -f1) inputs/$(basename "$CODESIGNATURE_GIT_ARCHIVE")
61-
EOF
6258

6359
mkdir -p "$DISTSRC"
6460
(
@@ -103,3 +99,14 @@ mkdir -p "$DISTSRC"
10399
rm -rf "$ACTUAL_OUTDIR"
104100
mv --no-target-directory "$OUTDIR" "$ACTUAL_OUTDIR" \
105101
|| ( rm -rf "$ACTUAL_OUTDIR" && exit 1 )
102+
103+
(
104+
cd /outdir-base
105+
{
106+
echo "$UNSIGNED_TARBALL"
107+
echo "$CODESIGNATURE_GIT_ARCHIVE"
108+
find "$ACTUAL_OUTDIR" -type f
109+
} | xargs realpath --relative-base="$PWD" \
110+
| xargs sha256sum \
111+
| sponge "$ACTUAL_OUTDIR"/SHA256SUMS.part
112+
)

0 commit comments

Comments
 (0)