diff --git a/subprojects/signing/src/main/groovy/org/gradle/plugins/signing/signatory/pgp/PgpSignatoryFactory.groovy b/subprojects/signing/src/main/groovy/org/gradle/plugins/signing/signatory/pgp/PgpSignatoryFactory.groovy index e77f20c5e4df..3b60fed20f5a 100644 --- a/subprojects/signing/src/main/groovy/org/gradle/plugins/signing/signatory/pgp/PgpSignatoryFactory.groovy +++ b/subprojects/signing/src/main/groovy/org/gradle/plugins/signing/signatory/pgp/PgpSignatoryFactory.groovy @@ -72,11 +72,22 @@ class PgpSignatoryFactory { } PGPSecretKey readSecretKey(String keyId, File file) { + if (!file.exists()) { + throw new InvalidUserDataException("Unable to retrieve secret key from key ring file '$file' as it does not exist") + } + file.withInputStream { readSecretKey(it, keyId, "file: $file.absolutePath") } } protected PGPSecretKey readSecretKey(InputStream input, String keyId, String sourceDescription) { - readSecretKey(new PGPSecretKeyRingCollection(input), normalizeKeyId(keyId), sourceDescription) + def keyRingCollection + try { + keyRingCollection = new PGPSecretKeyRingCollection(input) + } catch (Exception e) { + throw new InvalidUserDataException("Unable to read secret key from $sourceDescription (it may not be a PGP secret key ring)", e) + } + + readSecretKey(keyRingCollection, normalizeKeyId(keyId), sourceDescription) } protected PGPSecretKey readSecretKey(PGPSecretKeyRingCollection keyRings, PgpKeyId keyId, String sourceDescription) { diff --git a/subprojects/signing/src/test/groovy/org/gradle/plugins/signing/SignatoriesConfigurationSpec.groovy b/subprojects/signing/src/test/groovy/org/gradle/plugins/signing/SignatoriesConfigurationSpec.groovy index c672784912d5..0bde32f4f1b1 100644 --- a/subprojects/signing/src/test/groovy/org/gradle/plugins/signing/SignatoriesConfigurationSpec.groovy +++ b/subprojects/signing/src/test/groovy/org/gradle/plugins/signing/SignatoriesConfigurationSpec.groovy @@ -15,6 +15,8 @@ */ package org.gradle.plugins.signing +import org.gradle.api.InvalidUserDataException + class SignatoriesConfigurationSpec extends SigningProjectSpec { def setup() { @@ -65,5 +67,31 @@ class SignatoriesConfigurationSpec extends SigningProjectSpec { signing.signatories.custom != null signing.signatories.custom.keyId.asHex == properties.keyId } + + def "trying to read non existent file produces reasonable error message"() { + when: + setProperty("signing.keyId", "aaaaaaaa") + setProperty("signing.secretKeyRingFile", "i/dont/exist") + setProperty("signing.password", "anything") + + and: + signing.signatory + + then: + def e = thrown(InvalidUserDataException) + e.message.contains "it does not exist" + } + + def "trying to use an invalid key ring file produces a reasonable error message"() { + given: + addSigningProperties(set: "invalid-key-ring") + + when: + signing.signatory + + then: + def e = thrown(InvalidUserDataException) + e.message.contains "Unable to read secret key from" + } } \ No newline at end of file diff --git a/subprojects/signing/src/test/resources/keys/invalid-key-ring/keyId.txt b/subprojects/signing/src/test/resources/keys/invalid-key-ring/keyId.txt new file mode 100644 index 000000000000..ee028cc4cadb --- /dev/null +++ b/subprojects/signing/src/test/resources/keys/invalid-key-ring/keyId.txt @@ -0,0 +1 @@ +24875D73 diff --git a/subprojects/signing/src/test/resources/keys/invalid-key-ring/password.txt b/subprojects/signing/src/test/resources/keys/invalid-key-ring/password.txt new file mode 100644 index 000000000000..82be7ae0c7cb --- /dev/null +++ b/subprojects/signing/src/test/resources/keys/invalid-key-ring/password.txt @@ -0,0 +1 @@ +gradle \ No newline at end of file diff --git a/subprojects/signing/src/test/resources/keys/invalid-key-ring/secring.gpg b/subprojects/signing/src/test/resources/keys/invalid-key-ring/secring.gpg new file mode 100644 index 000000000000..c74084ef4c51 --- /dev/null +++ b/subprojects/signing/src/test/resources/keys/invalid-key-ring/secring.gpg @@ -0,0 +1 @@ +not a valid key ring file \ No newline at end of file