From 9052d8469c8445f99cbfc94ac896310caba2fc0a Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 19 Nov 2024 10:37:17 +0000 Subject: [PATCH] Reduce overloading of ArmoredAsciiSigner#get Closes gh-4 --- .../openpgp/ArmoredAsciiSigner.java | 111 +----------- .../DeployableArtifactsSignerTests.java | 5 +- .../openpgp/ArmoredAsciiSignerTests.java | 163 ++---------------- 3 files changed, 25 insertions(+), 254 deletions(-) diff --git a/src/main/java/io/spring/github/actions/artifactorydeploy/openpgp/ArmoredAsciiSigner.java b/src/main/java/io/spring/github/actions/artifactorydeploy/openpgp/ArmoredAsciiSigner.java index acb6d9a..9e56468 100644 --- a/src/main/java/io/spring/github/actions/artifactorydeploy/openpgp/ArmoredAsciiSigner.java +++ b/src/main/java/io/spring/github/actions/artifactorydeploy/openpgp/ArmoredAsciiSigner.java @@ -18,7 +18,6 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; -import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; @@ -214,7 +213,7 @@ private void updateSignatureGenerator(InputStream source, PGPSignatureGenerator /** * Get an {@link ArmoredAsciiSigner} for the given {@code signingKey} and - * {@code passphrase}. The signing key may either contain a PHP private key block or + * {@code passphrase}. The signing key may either contain a PGP private key block or * reference a file. * @param signingKey the signing key (either the key itself or a reference to a file) * @param passphrase the passphrase to use @@ -225,116 +224,18 @@ public static ArmoredAsciiSigner get(String signingKey, String passphrase) throw return get(Clock.systemDefaultZone(), signingKey, passphrase); } - /** - * Get an {@link ArmoredAsciiSigner} for the given {@code signingKey} and - * {@code passphrase}. The signing key may either contain a PHP private key block or - * reference a file. - * @param clock the clock to use when generating signatures - * @param signingKey the signing key (either the key itself or a reference to a file) - * @param passphrase the passphrase to use - * @return an {@link ArmoredAsciiSigner} insance - * @throws IOException on IO error - */ - public static ArmoredAsciiSigner get(Clock clock, String signingKey, String passphrase) throws IOException { + static ArmoredAsciiSigner get(Clock clock, String signingKey, String passphrase) throws IOException { Assert.notNull(clock, "Clock must not be null"); Assert.notNull(signingKey, "SigningKey must not be null"); Assert.hasText(signingKey, "SigningKey must not be empty"); + Assert.notNull(passphrase, "Passphrase must not be null"); if (isArmoredAscii(signingKey)) { byte[] bytes = signingKey.getBytes(StandardCharsets.UTF_8); - return get(clock, new ByteArrayInputStream(bytes), passphrase); + return new ArmoredAsciiSigner(clock, new ByteArrayInputStream(bytes), passphrase); } Assert.isTrue(!signingKey.contains("\n"), - "Signing key is not does not contain a PGP private key block and does not reference a file"); - return get(clock, new File(signingKey), passphrase); - } - - /** - * Get an {@link ArmoredAsciiSigner} for the given {@code signingKey} file and - * {@code passphrase}. - * @param signingKey the signing key file - * @param passphrase the passphrase to use - * @return an {@link ArmoredAsciiSigner} insance - * @throws IOException on IO error - */ - public static ArmoredAsciiSigner get(File signingKey, String passphrase) throws IOException { - return get(Clock.systemDefaultZone(), signingKey, passphrase); - } - - /** - * Get an {@link ArmoredAsciiSigner} for the given {@code signingKey} and - * {@code passphrase}. - * @param clock the clock to use when generating signatures - * @param signingKey the signing key file - * @param passphrase the passphrase to use - * @return an {@link ArmoredAsciiSigner} insance - * @throws IOException on IO error - */ - public static ArmoredAsciiSigner get(Clock clock, File signingKey, String passphrase) throws IOException { - Assert.notNull(clock, "Clock must not be null"); - Assert.notNull(signingKey, "SigningKey must not be null"); - Assert.notNull(passphrase, "Passphrase must not be null"); - Assert.isTrue(signingKey.exists(), "Signing key file does not exist"); - Assert.isTrue(signingKey.isFile(), "Signing key file does not reference a file"); - return get(clock, new FileInputStream(signingKey), passphrase); - } - - /** - * Get an {@link ArmoredAsciiSigner} for the given {@code signingKey} and - * {@code passphrase}. - * @param signingKey an {@link InputStreamSource} providing the signing key - * @param passphrase the passphrase to use - * @return an {@link ArmoredAsciiSigner} insance - * @throws IOException on IO error - */ - public static ArmoredAsciiSigner get(InputStreamSource signingKey, String passphrase) throws IOException { - return get(Clock.systemDefaultZone(), signingKey, passphrase); - } - - /** - * Get an {@link ArmoredAsciiSigner} for the given {@code signingKey} and - * {@code passphrase}. - * @param clock the clock to use when generating signatures - * @param signingKey an {@link InputStreamSource} providing the signing key - * @param passphrase the passphrase to use - * @return an {@link ArmoredAsciiSigner} insance - * @throws IOException on IO error - */ - public static ArmoredAsciiSigner get(Clock clock, InputStreamSource signingKey, String passphrase) - throws IOException { - Assert.notNull(clock, "Clock must not be null"); - Assert.notNull(signingKey, "SigningKey must not be null"); - Assert.notNull(passphrase, "Passphrase must not be null"); - return get(clock, signingKey.getInputStream(), passphrase); - } - - /** - * Get an {@link ArmoredAsciiSigner} for the given {@code signingKey} and - * {@code passphrase}. - * @param signingKey an {@link InputStream} providing the signing key (will be closed - * after use) - * @param passphrase the passphrase to use - * @return an {@link ArmoredAsciiSigner} insance - * @throws IOException on IO error - */ - public static ArmoredAsciiSigner get(InputStream signingKey, String passphrase) throws IOException { - return get(Clock.systemDefaultZone(), signingKey, passphrase); - } - - /** - * Get an {@link ArmoredAsciiSigner} for the given {@code signingKey} and - * {@code passphrase}. - * @param clock the clock to use when generating signatures - * @param signingKey an {@link InputStream} providing the signing key (will be closed - * after use) - * @param passphrase the passphrase to use - * @return an {@link ArmoredAsciiSigner} insance - * @throws IOException on IO error - */ - public static ArmoredAsciiSigner get(Clock clock, InputStream signingKey, String passphrase) throws IOException { - Assert.notNull(clock, "Clock must not be null"); - Assert.notNull(signingKey, "SigningKey must not be null"); - Assert.notNull(passphrase, "Passphrase must not be null"); - return new ArmoredAsciiSigner(clock, signingKey, passphrase); + "Signing key does not contain a PGP private key block and does not reference a file"); + return new ArmoredAsciiSigner(clock, new FileInputStream(signingKey), passphrase); } private static boolean isArmoredAscii(String signingKey) { diff --git a/src/test/java/io/spring/github/actions/artifactorydeploy/DeployableArtifactsSignerTests.java b/src/test/java/io/spring/github/actions/artifactorydeploy/DeployableArtifactsSignerTests.java index aab00c9..3e98994 100644 --- a/src/test/java/io/spring/github/actions/artifactorydeploy/DeployableArtifactsSignerTests.java +++ b/src/test/java/io/spring/github/actions/artifactorydeploy/DeployableArtifactsSignerTests.java @@ -54,8 +54,9 @@ class DeployableArtifactsSignerTests { @BeforeEach void setup() throws IOException { - ArmoredAsciiSigner signer = ArmoredAsciiSigner - .get(ArmoredAsciiSigner.class.getResourceAsStream("test-private.txt"), "password"); + String signingKey = new String(ArmoredAsciiSigner.class.getResourceAsStream("test-private.txt").readAllBytes(), + StandardCharsets.UTF_8); + ArmoredAsciiSigner signer = ArmoredAsciiSigner.get(signingKey, "password"); this.signer = new DeployableArtifactsSigner(signer, this.properties); } diff --git a/src/test/java/io/spring/github/actions/artifactorydeploy/openpgp/ArmoredAsciiSignerTests.java b/src/test/java/io/spring/github/actions/artifactorydeploy/openpgp/ArmoredAsciiSignerTests.java index c66acf2..4e525e5 100644 --- a/src/test/java/io/spring/github/actions/artifactorydeploy/openpgp/ArmoredAsciiSignerTests.java +++ b/src/test/java/io/spring/github/actions/artifactorydeploy/openpgp/ArmoredAsciiSignerTests.java @@ -34,7 +34,6 @@ import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.InputStreamSource; -import org.springframework.core.io.Resource; import org.springframework.util.FileCopyUtils; import static org.assertj.core.api.Assertions.assertThat; @@ -54,8 +53,6 @@ class ArmoredAsciiSignerTests { private File signingKeyFile; - private Resource signingKeyResource; - private String signingKeyContent; private String passphrase = "password"; @@ -72,7 +69,6 @@ class ArmoredAsciiSignerTests { void setup(@TempDir File temp) throws Exception { this.temp = temp; this.signingKeyFile = copyClasspathFile("test-private.txt"); - this.signingKeyResource = new FileSystemResource(this.signingKeyFile); this.signingKeyContent = copyToString(this.signingKeyFile); this.sourceFile = copyClasspathFile("source.txt"); this.sourceContent = copyToString(this.sourceFile); @@ -80,186 +76,59 @@ void setup(@TempDir File temp) throws Exception { } @Test - void getWithStringSigningKeyWhenSigningKeyIsKeyReturnsSigner() throws Exception { + void getWhenSigningKeyIsKeyReturnsSigner() throws Exception { ArmoredAsciiSigner signer = ArmoredAsciiSigner.get(FIXED, this.signingKeyContent, this.passphrase); assertThat(signer.sign(this.sourceContent)).isEqualTo(this.expectedSignature); } @Test - void getWithStringSigningKeyWhenSigningKeyIsFileReturnsSigner() throws Exception { + void getWhenSigningKeyIsFileReturnsSigner() throws Exception { ArmoredAsciiSigner signer = ArmoredAsciiSigner.get(FIXED, this.signingKeyFile.getAbsolutePath(), this.passphrase); assertThat(signer.sign(this.sourceContent)).isEqualTo(this.expectedSignature); } @Test - void getWithStringSigningKeyWhenClockIsNullThrowsException() { + void getWhenClockIsNullThrowsException() { assertThatIllegalArgumentException() - .isThrownBy(() -> ArmoredAsciiSigner.get(null, this.signingKeyContent, this.passphrase)) + .isThrownBy(() -> ArmoredAsciiSigner.get((Clock) null, this.signingKeyContent, this.passphrase)) .withMessage("Clock must not be null"); } @Test - void getWithStringSigningKeyWhenSigningKeyIsNullThrowsException() { - assertThatIllegalArgumentException() - .isThrownBy(() -> ArmoredAsciiSigner.get(FIXED, (String) null, this.passphrase)) + void getWhenSigningKeyIsNullThrowsException() { + assertThatIllegalArgumentException().isThrownBy(() -> ArmoredAsciiSigner.get((String) null, this.passphrase)) .withMessage("SigningKey must not be null"); } @Test - void getWithStringSigningKeyWhenSigningKeyIsEmptyThrowsException() { - assertThatIllegalArgumentException().isThrownBy(() -> ArmoredAsciiSigner.get(FIXED, "", this.passphrase)) + void getWhenSigningKeyIsEmptyThrowsException() { + assertThatIllegalArgumentException().isThrownBy(() -> ArmoredAsciiSigner.get("", this.passphrase)) .withMessage("SigningKey must not be empty"); } @Test - void getWithStringSigningKeyWhenSigningKeyIsMultiLineWithoutHeaderThrowsException() { - assertThatIllegalArgumentException().isThrownBy(() -> ArmoredAsciiSigner.get(FIXED, "ab\ncd", this.passphrase)) - .withMessage( - "Signing key is not does not contain a PGP private key block " + "and does not reference a file"); + void getWhenSigningKeyIsMultiLineWithoutHeaderThrowsException() { + assertThatIllegalArgumentException().isThrownBy(() -> ArmoredAsciiSigner.get("ab\ncd", this.passphrase)) + .withMessage("Signing key does not contain a PGP private key block and does not reference a file"); } @Test - void getWithStringSigningKeyWhenSigningKeyIsMalformedThrowsException() throws Exception { + void getWhenSigningKeyIsMalformedThrowsException() throws Exception { String signingKey = copyToString(getClass().getResourceAsStream("test-bad-private.txt")); assertThatIllegalStateException().isThrownBy(() -> ArmoredAsciiSigner.get(signingKey, this.passphrase)) .withMessage("Unable to read signing key"); } @Test - void getWithStringSigningKeyWhenPassphraseIsNullThrowsException() { - assertThatIllegalArgumentException() - .isThrownBy(() -> ArmoredAsciiSigner.get(FIXED, this.signingKeyContent, null)) - .withMessage("Passphrase must not be null"); - } - - @Test - void getWithStringSigningKeyWhenPassphraseIsWrongThrowsException() { - assertThatIllegalStateException().isThrownBy(() -> ArmoredAsciiSigner.get(FIXED, this.signingKeyFile, "bad")) - .withMessage("Unable to extract private key"); - } - - @Test - void getWithFileSigningKeyKeyReturnsSigner() throws IOException { - ArmoredAsciiSigner signer = ArmoredAsciiSigner.get(FIXED, this.signingKeyFile, this.passphrase); - assertThat(signer.sign(this.sourceContent)).isEqualTo(this.expectedSignature); - } - - @Test - void getWithFileSigningKeyWhenClockIsNullThrowsException() { - assertThatIllegalArgumentException() - .isThrownBy(() -> ArmoredAsciiSigner.get(null, this.signingKeyFile, this.passphrase)) - .withMessage("Clock must not be null"); - } - - @Test - void getWithFileSigningKeyWhenSigningKeyIsNullThrowsException() { - assertThatIllegalArgumentException() - .isThrownBy(() -> ArmoredAsciiSigner.get(FIXED, (File) null, this.passphrase)) - .withMessage("SigningKey must not be null"); - } - - @Test - void getWithFileSigningKeyWhenSigningKeyIsMalformedThrowsException() throws Exception { - File signingKey = copyClasspathFile("test-bad-private.txt"); - assertThatIllegalStateException().isThrownBy(() -> ArmoredAsciiSigner.get(FIXED, signingKey, this.passphrase)) - .withMessage("Unable to read signing key"); - } - - @Test - void getWithFileSigningKeyWhenPassphraseIsNullThrowsException() { - assertThatIllegalArgumentException().isThrownBy(() -> ArmoredAsciiSigner.get(FIXED, this.signingKeyFile, null)) - .withMessage("Passphrase must not be null"); - } - - @Test - void getWithFileSigningKeyWhenPassphraseIsWrongThrowsException() { - assertThatIllegalStateException().isThrownBy(() -> ArmoredAsciiSigner.get(FIXED, this.signingKeyFile, "bad")) - .withMessage("Unable to extract private key"); - } - - @Test - void getWithInputStreamSourceSigningKeyKeyReturnsSigner() throws Exception { - ArmoredAsciiSigner signer = ArmoredAsciiSigner.get(FIXED, this.signingKeyResource, this.passphrase); - assertThat(signer.sign(this.sourceContent)).isEqualTo(this.expectedSignature); - } - - @Test - void getWithInputStreamSourceSigningKeyWhenClockIsNullThrowsException() { - assertThatIllegalArgumentException() - .isThrownBy(() -> ArmoredAsciiSigner.get(null, this.signingKeyResource, this.passphrase)) - .withMessage("Clock must not be null"); - } - - @Test - void getWithInputStreamSourceSigningKeyWhenSigningKeyIsNullThrowsException() { - assertThatIllegalArgumentException() - .isThrownBy(() -> ArmoredAsciiSigner.get(FIXED, (InputStreamSource) null, this.passphrase)) - .withMessage("SigningKey must not be null"); - } - - @Test - void getWithInputStreamSourceSigningKeyWhenSigningKeyIsMalformedThrowsException() throws Exception { - File signingKeyFile = copyClasspathFile("test-bad-private.txt"); - Resource signingKeyResource = new FileSystemResource(signingKeyFile); - assertThatIllegalStateException().isThrownBy(() -> ArmoredAsciiSigner.get(signingKeyResource, this.passphrase)) - .withMessage("Unable to read signing key"); - } - - @Test - void getWithInputStreamSourceSigningKeyWhenPassphraseIsNullThrowsException() { - assertThatIllegalArgumentException() - .isThrownBy(() -> ArmoredAsciiSigner.get(FIXED, this.signingKeyResource, null)) - .withMessage("Passphrase must not be null"); - } - - @Test - void getWithInputStreamSourceSigningKeyWhenPassphraseIsWrongThrowsException() { - assertThatIllegalStateException() - .isThrownBy(() -> ArmoredAsciiSigner.get(FIXED, this.signingKeyResource, "bad")) - .withMessage("Unable to extract private key"); - } - - @Test - void getWithInputStreamSigningKeyKeyReturnsSigner() { - assertThatIllegalStateException() - .isThrownBy(() -> ArmoredAsciiSigner.get(FIXED, new FileInputStream(this.signingKeyFile), "bad")) - .withMessage("Unable to extract private key"); - } - - @Test - void getWithInputStreamSigningKeyWhenClockIsNullThrowsException() { - assertThatIllegalArgumentException() - .isThrownBy(() -> ArmoredAsciiSigner.get(null, new FileInputStream(this.signingKeyFile), this.passphrase)) - .withMessage("Clock must not be null"); - } - - @Test - void getWithInputStreamSigningKeyWhenSigningKeyIsNullThrowsException() { - assertThatIllegalArgumentException() - .isThrownBy(() -> ArmoredAsciiSigner.get(FIXED, (InputStream) null, this.passphrase)) - .withMessage("SigningKey must not be null"); - } - - @Test - void getWithInputStreamSigningKeyWhenSigningKeyIsMalformedThrowsException() throws Exception { - File signingKey = copyClasspathFile("test-bad-private.txt"); - assertThatIllegalStateException() - .isThrownBy(() -> ArmoredAsciiSigner.get(FIXED, new FileInputStream(signingKey), this.passphrase)) - .withMessage("Unable to read signing key"); - } - - @Test - void getWithInputStreamSigningKeyWhenPassphraseIsNullThrowsException() { - assertThatIllegalArgumentException() - .isThrownBy(() -> ArmoredAsciiSigner.get(FIXED, new FileInputStream(this.signingKeyFile), null)) + void getWhenPassphraseIsNullThrowsException() { + assertThatIllegalArgumentException().isThrownBy(() -> ArmoredAsciiSigner.get(this.signingKeyContent, null)) .withMessage("Passphrase must not be null"); } @Test - void getWithInputStreamSigningKeyWhenPassphraseIsWrongThrowsException() { - assertThatIllegalStateException() - .isThrownBy(() -> ArmoredAsciiSigner.get(FIXED, new FileInputStream(this.signingKeyFile), "bad")) + void getWhenPassphraseIsWrongThrowsException() { + assertThatIllegalStateException().isThrownBy(() -> ArmoredAsciiSigner.get(this.signingKeyContent, "bad")) .withMessage("Unable to extract private key"); }