diff --git a/build-logic/src/main/java/dev/caskeleton/buildlogic/publishing/LibraryPublishingPlugin.java b/build-logic/src/main/java/dev/caskeleton/buildlogic/publishing/LibraryPublishingPlugin.java index 5aac5a4..92a9e19 100644 --- a/build-logic/src/main/java/dev/caskeleton/buildlogic/publishing/LibraryPublishingPlugin.java +++ b/build-logic/src/main/java/dev/caskeleton/buildlogic/publishing/LibraryPublishingPlugin.java @@ -37,7 +37,8 @@ import org.gradle.plugins.signing.SigningExtension; * the versions its own resolution chose ({@code versionMapping}), and the plugin's POM * customization, which would add the BOM imports a consumer never inherits, is off. * - *

Signing: with {@code SIGNING_KEY} (an ASCII-armoured PGP secret key) and {@code + *

Signing: with {@code SIGNING_KEY} (an ASCII-armoured PGP secret key, or the base64 of one; + * {@link SigningKeys}) and {@code * SIGNING_PASSWORD} in the environment, every published file gets an {@code .asc}. Without them * nothing is signed, and a task that uploads to {@code gitea} fails before it starts: an unsigned * release is not a release. @@ -138,7 +139,8 @@ public final class LibraryPublishingPlugin implements Plugin { .dir("staging-repository")); }); - String signingKey = project.getProviders().environmentVariable("SIGNING_KEY").getOrElse(""); + String signingKey = + SigningKeys.armoured(project.getProviders().environmentVariable("SIGNING_KEY").getOrElse("")); String signingPassword = project.getProviders().environmentVariable("SIGNING_PASSWORD").getOrElse(""); boolean signed = !signingKey.isBlank(); diff --git a/build-logic/src/main/java/dev/caskeleton/buildlogic/publishing/SigningKeys.java b/build-logic/src/main/java/dev/caskeleton/buildlogic/publishing/SigningKeys.java new file mode 100644 index 0000000..2996b87 --- /dev/null +++ b/build-logic/src/main/java/dev/caskeleton/buildlogic/publishing/SigningKeys.java @@ -0,0 +1,42 @@ +package dev.caskeleton.buildlogic.publishing; + +import java.nio.charset.StandardCharsets; +import java.util.Base64; + +/** + * The ASCII-armoured key Gradle's in-memory signatory reads, from what a CI secret holds. + * + *

A secret store keeps a multi-line value badly or not at all, so the common practice is to put + * the armoured key file in base64 on one line ({@code base64 -w0 signing-key.asc}). Gradle's {@code + * useInMemoryPgpKeys} reads only the armoured text and reports the base64 form as "Could not read + * PGP secret key" (v1.0.0 release run 9 and the secret put in after it). Both forms are accepted: + * a value that decodes to an armoured private key block is unwrapped, anything else is passed on + * unchanged, so a wrong value still fails, and {@code verifySigningKey} says why. + */ +public final class SigningKeys { + private static final String BEGIN = "-----BEGIN PGP PRIVATE KEY BLOCK-----"; + + private SigningKeys() {} + + /** + * The armoured key: {@code value} itself, or the armoured block it is the base64 of. + * + * @param value the secret as given (armoured, base64 of the armoured file, or anything else) + * @return the armoured key, or {@code value} unchanged when it is neither form + */ + public static String armoured(String value) { + if (value == null || value.isBlank() || value.contains(BEGIN)) { + return value; + } + String compact = value.replaceAll("\\s", ""); + if (!compact.matches("[A-Za-z0-9+/]+={0,2}")) { + return value; + } + try { + String decoded = new String(Base64.getDecoder().decode(compact), StandardCharsets.UTF_8); + return decoded.strip().startsWith(BEGIN) ? decoded : value; + } catch (IllegalArgumentException notBase64) { + return value; + } + } +} diff --git a/build-logic/src/test/java/dev/caskeleton/buildlogic/publishing/SigningKeysTest.java b/build-logic/src/test/java/dev/caskeleton/buildlogic/publishing/SigningKeysTest.java new file mode 100644 index 0000000..9be7ffd --- /dev/null +++ b/build-logic/src/test/java/dev/caskeleton/buildlogic/publishing/SigningKeysTest.java @@ -0,0 +1,41 @@ +package dev.caskeleton.buildlogic.publishing; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.nio.charset.StandardCharsets; +import java.util.Base64; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class SigningKeysTest { + private static final String ARMOURED = + "-----BEGIN PGP PRIVATE KEY BLOCK-----\n\nlQdGBGrv1SYBEADGgq8C\n=abcd\n" + + "-----END PGP PRIVATE KEY BLOCK-----\n"; + + @Test + @DisplayName("armour 그대로인 키는 바꾸지 않는다") + void anArmouredKeyIsUsedAsItIs() { + assertThat(SigningKeys.armoured(ARMOURED)).isEqualTo(ARMOURED); + } + + @Test + @DisplayName("armour 파일을 base64로 감싼 값은 풀어서 armour로 쓴다") + void aBase64WrappedArmouredKeyIsUnwrapped() { + String wrapped = Base64.getEncoder().encodeToString(ARMOURED.getBytes(StandardCharsets.UTF_8)); + + assertThat(SigningKeys.armoured(wrapped)).isEqualTo(ARMOURED); + assertThat(SigningKeys.armoured(wrapped.substring(0, 40) + "\n" + wrapped.substring(40))) + .isEqualTo(ARMOURED); + } + + @Test + @DisplayName("지문이나 armour가 아닌 base64는 그대로 두어 서명이 원래 오류로 실패하게 한다") + void anythingElseIsLeftAlone() { + String fingerprint = "92646CEAFE816CF8B9C249F0A1C936D7F791F8AB"; + String otherBase64 = Base64.getEncoder().encodeToString("not a key".getBytes(StandardCharsets.UTF_8)); + + assertThat(SigningKeys.armoured(fingerprint)).isEqualTo(fingerprint); + assertThat(SigningKeys.armoured(otherBase64)).isEqualTo(otherBase64); + assertThat(SigningKeys.armoured("")).isEmpty(); + } +} diff --git a/build.gradle b/build.gradle index 70020c3..021e0eb 100644 --- a/build.gradle +++ b/build.gradle @@ -1,4 +1,5 @@ import dev.caskeleton.buildlogic.java.JavaConventionsPlugin +import dev.caskeleton.buildlogic.publishing.SigningKeys plugins { id 'ca.architecture' @@ -190,7 +191,8 @@ publishing { } } -def signingKey = providers.environmentVariable('SIGNING_KEY').getOrElse('') +// The armoured key, or the base64 of the armoured file (the usual way to keep it in a CI secret). +def signingKey = SigningKeys.armoured(providers.environmentVariable('SIGNING_KEY').getOrElse('')) // Before anything is signed: the secret's shape, that gpg imports it as the release key and that // SIGNING_PASSWORD signs with it, printing no part of either. Gradle's signing reports every one of diff --git a/docs/work/release.md b/docs/work/release.md index ab041cc..2f33f64 100644 --- a/docs/work/release.md +++ b/docs/work/release.md @@ -27,7 +27,7 @@ main 병합 ──> 소유자가 태그 vX.Y.Z push ──> .gitea/workflows/rel - 서명 키: RSA 4096, 지문 `92646CEAFE816CF8B9C249F0A1C936D7F791F8AB`, 2028-11-07 만료. 공개 키는 README 「서명」이다. - 첫 태그는 `v1.0.0`이다(3절). -**`SIGNING_KEY`에 넣는 것**은 키 지문이나 키 ID(16진수 40·16·8글자)가 아니라 비밀 키 파일 전체다. +**`SIGNING_KEY`에 넣는 것**은 키 지문이나 키 ID(16진수 40·16·8글자)가 아니라 비밀 키 파일 전체다. 파일 그대로(여러 줄)나 그 파일을 base64로 한 줄로 만든 값(`base64 -w0 signing-key.asc`) 둘 다 된다. 빌드가 base64를 풀어 쓴다(`SigningKeys`). ```bash gpg --armor --export-secret-keys 92646CEAFE816CF8B9C249F0A1C936D7F791F8AB > signing-key.asc diff --git a/scripts/release/check-signing-key.sh b/scripts/release/check-signing-key.sh index 6fef35d..d1cb1c6 100755 --- a/scripts/release/check-signing-key.sh +++ b/scripts/release/check-signing-key.sh @@ -24,7 +24,10 @@ if [ -z "${SIGNING_KEY:-}" ]; then fi [ -n "${SIGNING_PASSWORD:-}" ] || echo "check-signing-key: SIGNING_PASSWORD is empty (only right for a key without a passphrase)" -EXPECTED_FINGERPRINT=$expected python3 - <<'PY' || fail=1 +home=$(mktemp -d) +trap 'rm -rf "$home"' EXIT +chmod 700 "$home" +KEY_OUT=$home/key.asc EXPECTED_FINGERPRINT=$expected python3 - <<'PY' || fail=1 import base64, os, re, sys key = os.environ["SIGNING_KEY"] @@ -40,6 +43,18 @@ print(f"shape: {len(key)} characters, {key.count(chr(10)) + 1} lines," f" trailing whitespace {'yes' if key[-1:].isspace() else 'no'}") stripped = key.strip() +compact = re.sub(r"\s", "", key) +if "-----BEGIN" not in key and re.fullmatch(r"[A-Za-z0-9+/]+={0,2}", compact or "x"): + try: + unwrapped = base64.b64decode(compact).decode("utf-8") + except Exception: + unwrapped = "" + if unwrapped.strip().startswith("-----BEGIN PGP PRIVATE KEY BLOCK-----"): + print("form: base64 of an armoured key file on one value; the build unwraps it (SigningKeys), so the checks below read the unwrapped text") + key = unwrapped + stripped = key.strip() + print(f"unwrapped shape: {len(key)} characters, {key.count(chr(10)) + 1} lines") + if re.fullmatch(r"(0x)?[0-9A-Fa-f]{8}|(0x)?[0-9A-Fa-f]{16}|[0-9A-Fa-f]{40}", stripped): expected = os.environ.get("EXPECTED_FINGERPRINT", "") same = " (the release key's fingerprint)" if expected and stripped.upper() == expected.upper() else "" @@ -115,13 +130,12 @@ if raw: for problem in problems: print(f"PROBLEM: {problem}") +with open(os.environ["KEY_OUT"], "w", encoding="utf-8") as out: + out.write(key) sys.exit(1 if problems else 0) PY -home=$(mktemp -d) -trap 'rm -rf "$home"' EXIT -chmod 700 "$home" -if printf '%s\n' "$SIGNING_KEY" | GNUPGHOME="$home" gpg --batch --quiet --import >/dev/null 2>&1; then +if [ -s "$home/key.asc" ] && GNUPGHOME="$home" gpg --batch --quiet --import "$home/key.asc" >/dev/null 2>&1; then fingerprint=$(GNUPGHOME="$home" gpg --batch --with-colons --list-secret-keys 2>/dev/null | awk -F: '/^fpr/{print $10; exit}') echo "gpg import: ok, secret key fingerprint ${fingerprint:-none}" if [ -n "$expected" ] && [ "${fingerprint:-}" != "$expected" ]; then