Files
coder/scripts/sign_with_gpg.sh
T
Dean Sheather d974fefc08 chore: sign with github.com/coder/gpgkms instead of scripts/gpgkms (#32308)
Replaces `scripts/gpgkms` with
[`github.com/coder/gpgkms`](https://github.com/coder/gpgkms) v0.2.0, the
same code extracted into a shared module (terraform-provider-coder
already signs its releases with it). v0.2.0 also rejects keys that
aren't HSM-backed (`gpg-release` is `HSM`, and the check reads
`protectionLevel` from the existing `GetPublicKey` call, so no new IAM
is needed), limits each KMS request to a minute, and writes `-out`
atomically.

- `go.mod`: gpgkms is a `tool` dependency, so the version is pinned in
`go.mod`/`go.sum` and Dependabot keeps it current. This bumps go-crypto
1.4.1 → 1.5.2, google.golang.org/api 0.308.0 → 0.311.0 and circl 1.6.3 →
1.6.5, plus a few indirect dependencies. go-crypto is now only indirect:
nothing else in coder/coder imports it.
- `scripts/gpgkms.sh` (new): runs it with `go tool` (like
`scripts/format_go_file.sh` does for gofumpt and gci), passing the
release key's OpenPGP identity (name, email, creation time, 4-year
lifetime). The library takes these as flags instead of hardcoding them,
so they now live in one place.
- It also pins `-fingerprint 0F685EFE0AE5A8182A473DD86E3A8E5613707520`,
so signing and `public-key` fail if the KMS key or creation time ever
change. That's the fingerprint every current main build prints (e.g. run
44031030839), and I recomputed it from the KMS public key and the
creation time alone, without signing anything. The fingerprint doesn't
cover the user ID or lifetime; the equivalence check below does.
- `scripts/sign_with_gpg.sh` and the "Print GPG KMS public key" step in
`build.yaml` call the wrapper instead of `./scripts/gpgkms`. Signatures
stay armored `.v2.asc` files, as before.
- `scripts/gpgkms/` is deleted.

## Same key, same signatures

The OpenPGP key's fingerprint depends on these exact inputs, so I
checked that nothing changed. With a local RSA-4096 key standing in for
KMS, the deleted `newEntity`/`writePublicKey`/`sign` produce the same
output as the gpgkms CLI with the flags read out of `scripts/gpgkms.sh`:

- The public key is **byte for byte identical**.
- A signature with `-prepend` is armored the same way and has the same
length (only the timestamp differs). Both the prepended signature and
the KMS signature verify in both outputs.
- Changing the lifetime in the wrapper by one hour makes the comparison
fail, so the check is sensitive.

```
$ git -C gpgkms describe --tags
v0.2.0
$ WRAPPER_ARGS_FILE=wrapper-args.txt go test ./cmd/gpgkms -run TestCoderWrapperMatchesOldCoderCode -count=3 -v
=== RUN   TestCoderWrapperMatchesOldCoderCode
    equiv_coder_test.go:40: wrapper args: ["-name" "Coder Release Signing Key" "-email" "security@coder.com" "-created" "2026-11-16T00:00:00Z" "-lifetime" "35040h"]
--- PASS: TestCoderWrapperMatchesOldCoderCode (0.33s)
--- PASS: TestCoderWrapperMatchesOldCoderCode (0.11s)
--- PASS: TestCoderWrapperMatchesOldCoderCode (0.20s)
ok  	github.com/coder/gpgkms/cmd/gpgkms	0.634s

# with -lifetime 35041h
    equiv_coder_test.go:56: public keys differ:
FAIL

# with the wrapper's -fingerprint pin and a random test key
    derived key has fingerprint 9794B34B5E098DE61DD6CE247E0DFD4BF203C917, want 0F685EFE0AE5A8182A473DD86E3A8E5613707520 (wrong -key, -created or KMS key?)
```

(The byte-for-byte comparison runs without `-fingerprint`, since the
test key is random.)

gpgkms also has its own compatibility test against this identity
(`compat_test.go`).

Other checks:

```
$ ./scripts/gpgkms.sh public-key                 # from the repo root (build.yaml)
gpgkms: -key is required
$ cd scripts && ./gpgkms.sh public-key           # from scripts/ (build_go.sh -> execrelative)
gpgkms: -key is required
$ ./gpgkms.sh sign -key x -prepend ../no-such-file -out o.asc ../go.mod   # relative paths resolve from the caller
gpgkms: read prepend signature: open ../no-such-file: no such file or directory
$ shellcheck -x scripts/gpgkms.sh scripts/sign_with_gpg.sh   # clean
$ shfmt -d scripts/gpgkms.sh scripts/sign_with_gpg.sh        # clean
$ actionlint .github/workflows/build.yaml                    # clean
$ go build ./... && go mod tidy -diff                        # clean
```

Not run here: real signing through Cloud KMS. The first main build after
merging prints the public key in the job summary; it should be identical
to the one printed by current main builds.

---

AI disclosure: this PR was written by an AI coding agent (Xum) at my
direction, and the agent ran the verification above. It stays a draft
until I've reviewed it.
2026-11-18 15:36:57 +11:00

100 lines
3.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# This script signs a given binary using GPG.
# It expects the binary to be signed as the first argument.
#
# Usage: ./sign_with_gpg.sh path/to/binary
#
# On success, the input file will be signed using the GPG key and the signature output file will moved to /site/out/bin/ (happens in the Makefile)
#
# Depends on the GPG utility. Requires the following environment variables to be set:
# - $CODER_GPG_RELEASE_KEY_BASE64: The base64 encoded private key to use.
#
# Optionally, the file is also signed with a release key held in Google Cloud
# KMS (see scripts/gpgkms.sh). That signature goes in a separate .v2.asc file, so
# the .asc file and every client that verifies it stay unchanged. Set both of
# these to enable it:
# - $CODER_GPG_KMS_KEY: The Cloud KMS CryptoKeyVersion resource name.
# - $CODER_GPG_KMS_CREDENTIALS_FILE: Google credentials for the KMS signer.
# Only gpgkms sees them, so they don't replace the job's default credentials.
set -euo pipefail
# shellcheck source=scripts/lib.sh
source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
requiredenvs CODER_GPG_RELEASE_KEY_BASE64
FILE_TO_SIGN="$1"
if [[ -z "$FILE_TO_SIGN" ]]; then
error "Usage: $0 <file_to_sign>"
fi
if [[ ! -f "$FILE_TO_SIGN" ]]; then
error "File not found: $FILE_TO_SIGN"
fi
# Import the GPG key.
old_gnupg_home="${GNUPGHOME:-}"
gnupg_home_temp="$(mktemp -d)"
export GNUPGHOME="$gnupg_home_temp"
# Remove the temporary keyring (which holds the private key) even if a step
# below fails.
trap 'rm -rf "$gnupg_home_temp"' EXIT
# Ensure GPG uses the temporary directory
echo "$CODER_GPG_RELEASE_KEY_BASE64" | base64 -d | gpg --homedir "$gnupg_home_temp" --import 1>&2
# Mark the imported key as ultimately trusted so GPG does not emit an
# "untrusted key" warning during signature verification. We derive the
# fingerprint from the keyring rather than hard-coding it so this works
# regardless of which key is supplied.
fingerprint="$(gpg --homedir "$gnupg_home_temp" --with-colons --fingerprint | awk -F: '/^fpr/ { print $10; exit }')"
echo "${fingerprint}:6:" | gpg --homedir "$gnupg_home_temp" --import-ownertrust 1>&2
# Sign the binary. This generates a file in the same directory and
# with the same name as the binary but ending in ".asc".
#
# We pipe `true` into `gpg` so that it never tries to be interactive (i.e.
# ask for a passphrase). The key we import above is not password protected.
true | gpg --homedir "$gnupg_home_temp" --detach-sign --armor "$FILE_TO_SIGN" 1>&2
# Verify the signature and capture the exit status
gpg --homedir "$gnupg_home_temp" --verify "${FILE_TO_SIGN}.asc" "$FILE_TO_SIGN" 1>&2
verification_result=$?
# Never leave a .v2.asc from an earlier build next to a newly built file.
rm -f "${FILE_TO_SIGN}.v2.asc"
if [[ "${CODER_GPG_KMS_KEY:-}" != "" ]]; then
requiredenvs CODER_GPG_KMS_CREDENTIALS_FILE
gpgkms() {
GOOGLE_APPLICATION_CREDENTIALS="$CODER_GPG_KMS_CREDENTIALS_FILE" "$PROJECT_ROOT/scripts/gpgkms.sh" "$@"
}
kms_signature="$gnupg_home_temp/kms.asc"
gpgkms sign -key "$CODER_GPG_KMS_KEY" -out "$kms_signature" "$FILE_TO_SIGN" 1>&2
# Verify against a keyring holding only the KMS key, so the file must
# contain exactly one signature, made by that key.
kms_gnupg_home="$gnupg_home_temp/kms"
mkdir -m 700 "$kms_gnupg_home"
gpgkms public-key -key "$CODER_GPG_KMS_KEY" | gpg --homedir "$kms_gnupg_home" --import 1>&2
gpg --homedir "$kms_gnupg_home" --verify "$kms_signature" "$FILE_TO_SIGN" 1>&2 || verification_result=$?
if [[ $verification_result -eq 0 ]]; then
mv "$kms_signature" "${FILE_TO_SIGN}.v2.asc"
fi
fi
# Clean up the temporary GPG home
rm -rf "$gnupg_home_temp"
unset GNUPGHOME
if [[ "$old_gnupg_home" != "" ]]; then
export GNUPGHOME="$old_gnupg_home"
fi
if [[ $verification_result -eq 0 ]]; then
echo "${FILE_TO_SIGN}.asc"
else
error "Signature verification failed!"
fi