Skip to content

Commit

Permalink
Enable OpenSSH key handling (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
tilln committed May 27, 2021
1 parent 452207f commit 0117559
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>nz.co.breakpoint.jmeter</groupId>
<artifactId>jmeter-sshmon</artifactId>
<packaging>jar</packaging>
<version>1.3</version>
<version>1.4-SNAPSHOT</version>
<name>jmeter-sshmon</name>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import org.apache.log.Logger;
import org.apache.sshd.common.NamedResource;
import org.apache.sshd.common.config.keys.FilePasswordProvider;
import org.apache.sshd.common.config.keys.loader.pem.PEMResourceParserUtils;
import org.apache.sshd.common.config.keys.loader.KeyPairResourceParser;
import org.apache.sshd.common.util.security.SecurityUtils;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.KeyPair;

import java.util.Arrays;
import java.util.List;

// TODO unit tests
public class KeyHelper {

private static final Logger log = LoggingManager.getLoggerForClass();
Expand All @@ -21,9 +21,10 @@ public static KeyPair toKeyPair(String privateKey, String password) throws IOExc
final NamedResource dummy = NamedResource.ofName("");
final List<String> lines = Arrays.asList(privateKey.split("\n"));
log.debug("Extracting key pair from ["+lines+"]");
KeyPairResourceParser parser = SecurityUtils.getKeyPairResourceParser();
try {
if (PEMResourceParserUtils.PROXY.canExtractKeyPairs(dummy, lines)) {
return PEMResourceParserUtils.PROXY.loadKeyPairs(null, dummy, FilePasswordProvider.of(password), lines).iterator().next();
if (parser.canExtractKeyPairs(dummy, lines)) {
return parser.loadKeyPairs(null, dummy, FilePasswordProvider.of(password), lines).iterator().next();
}
log.error("Unsupported private key format '"+privateKey+"'");
} catch (GeneralSecurityException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package nz.co.breakpoint.jmeter.vizualizers.sshmon;

import org.junit.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import static org.junit.Assert.assertNotNull;

public class KeyHelperTest {

String getFileContent(String filename) throws IOException {
return new String(Files.readAllBytes(Paths.get(filename)));
}

@Test
public void testRSAFormat() throws IOException {
assertNotNull(KeyHelper.toKeyPair(getFileContent("src/test/resources/hostkey.key"), ""));
}

@Test
public void testOpenSSHFormat() throws IOException {
assertNotNull(KeyHelper.toKeyPair(getFileContent("src/test/resources/openssh.key"), "changeit"));
}
}
30 changes: 30 additions & 0 deletions src/test/resources/openssh.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAACmFlczI1Ni1jdHIAAAAGYmNyeXB0AAAAGAAAABAF
8MNkEz3tA1ftREnfVbmIAAAAEAAAAAEAAAEVAAAAB3NzaC1yc2EAAAABJQAAAQEA
7/W6vTosOxnm7K5hriPosl4JlSx1fwcMCg+0FaBuWroTzdPEcgDwDVlzKNldd2V9
CNt9yxVR4wOqJAJsP3cPMH75ndz3Nli35vvWYzi/9Ub98tuA532TOozmtkHE24dP
fD0sJ2v2GkXNeORPAhYxhMYHCDDYcIWGguYH324whbF0m5X6j1u2IaJumwRBLkYm
9zSjOfv85lYnMDyzsW2FN3W4g00tgYVXw2OtUy9MC7dOOrWikkDA3yHq1244nmfv
/aoH7F5syGqI924WnWbPWk15+6vAH5sGU+ybL/EULvbTaa2VvU3JPMlYe7x5EJNk
NBPucNBOv0aQdbO1mMjqgQAAA9DZ92Ehoe1cwqFnCiZ2Eu0LB5aIZ3mHkHOWU1iK
mizv0qkC37jq5AH0qvy038qM92mxvaKP7DVHHbJTvb1HoJbzJI1pwpWBFHLBBOm9
uSex5Da/a6o46+ij+Rx+4iv8IFGfvPXMHIAzd5Z9LiB95Z6F0O+jZm0zgTjacXl4
Q+iG2bHJn/soIYmIiie3O83gk2JJpdau7QOmcskxxsvRv26zlw4Fb6o0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