Skip to content

Commit

Permalink
Reduce overloading of ArmoredAsciiSigner#get
Browse files Browse the repository at this point in the history
Closes gh-4
  • Loading branch information
wilkinsona committed Nov 19, 2024
1 parent b106e28 commit 9052d84
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 254 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -54,8 +53,6 @@ class ArmoredAsciiSignerTests {

private File signingKeyFile;

private Resource signingKeyResource;

private String signingKeyContent;

private String passphrase = "password";
Expand All @@ -72,194 +69,66 @@ 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);
this.expectedSignature = copyToString(ArmoredAsciiSignerTests.class.getResourceAsStream("expected.asc"));
}

@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");
}

Expand Down

0 comments on commit 9052d84

Please sign in to comment.