Skip to content

Commit

Permalink
JENKINS-41824 Meaningful exception message if the EC2 KeyPair private…
Browse files Browse the repository at this point in the history
… key is missing/empty: `IOException("This private key cannot be empty")` instead of `NullPointerException() (#226)
  • Loading branch information
Cyrille Le Clerc authored and Francis Upton IV committed Aug 18, 2017
1 parent e90581d commit 0fdf522
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/main/java/hudson/plugins/ec2/EC2PrivateKey.java
Expand Up @@ -56,13 +56,17 @@ public String getPrivateKey() {

/**
* Obtains the fingerprint of the key in the "ab:cd:ef:...:12" format.
*/
/**
* Obtains the fingerprint of the key in the "ab:cd:ef:...:12" format.
*
* @throw IOException if the underlying private key is invalid: empty or password protected
* (password protected private keys are not yet supported)
*/
public String getFingerprint() throws IOException {
String pemData = privateKey.getPlainText();
if (pemData == null || pemData.isEmpty()) {
throw new IOException("This private key cannot be empty");
}
try {
return PEMEncodable.decode(privateKey.getPlainText()).getPrivateKeyFingerprint();
return PEMEncodable.decode(pemData).getPrivateKeyFingerprint();
} catch (UnrecoverableKeyException e) {
throw new IOException("This private key is password protected, which isn't supported yet");
}
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/hudson/plugins/ec2/EC2PrivateKeyTest.java
Expand Up @@ -25,6 +25,7 @@

import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;

import java.io.IOException;
Expand Down Expand Up @@ -94,4 +95,11 @@ public void testPublicFingerprint() throws IOException {
+ "-----END RSA PRIVATE KEY-----");
assertEquals("e3:cc:f6:5d:0b:bb:8b:ca:32:12:fd:70:98:57:c0:21", k.getPublicFingerprint());
}

@Issue("JENKINS-41824")
@Test(expected = IOException.class)
public void testEmptyPrivateKey() throws Exception {
EC2PrivateKey k = new EC2PrivateKey("");
k.getFingerprint();
}
}

0 comments on commit 0fdf522

Please sign in to comment.