You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by gn...@apache.org on 2017/06/07 11:36:11 UTC
karaf git commit: Fix for OpenSSHGeneratorKeyFileProvider writeSshKey
method: JUnit test to assert the class can read what was previously written
Repository: karaf
Updated Branches:
refs/heads/karaf-4.1.x a35af8c38 -> 9d1372e9a
Fix for OpenSSHGeneratorKeyFileProvider writeSshKey method: JUnit test to assert the class can read what was previously written
Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/9d1372e9
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/9d1372e9
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/9d1372e9
Branch: refs/heads/karaf-4.1.x
Commit: 9d1372e9aa5cf58b10594977b1c51392712b7cbb
Parents: a35af8c
Author: Lukasz Lech <l....@ringler.ch>
Authored: Thu Mar 30 10:29:05 2017 +0200
Committer: Guillaume Nodet <gn...@apache.org>
Committed: Wed Jun 7 12:17:28 2017 +0200
----------------------------------------------------------------------
.../ssh/OpenSSHGeneratorFileKeyProvider.java | 6 ++--
.../OpenSSHGeneratorKeyFileProviderTest.java | 32 ++++++++++++++++++--
2 files changed, 34 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/karaf/blob/9d1372e9/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/OpenSSHGeneratorFileKeyProvider.java
----------------------------------------------------------------------
diff --git a/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/OpenSSHGeneratorFileKeyProvider.java b/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/OpenSSHGeneratorFileKeyProvider.java
index 0285053..4050644 100644
--- a/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/OpenSSHGeneratorFileKeyProvider.java
+++ b/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/OpenSSHGeneratorFileKeyProvider.java
@@ -19,6 +19,7 @@
package org.apache.karaf.shell.ssh;
+import org.apache.commons.ssl.PEMItem;
import org.apache.commons.ssl.PEMUtil;
import org.apache.commons.ssl.PKCS8Key;
import org.apache.sshd.server.keyprovider.AbstractGeneratorHostKeyProvider;
@@ -29,6 +30,7 @@ import java.io.OutputStream;
import java.nio.file.Paths;
import java.security.GeneralSecurityException;
import java.security.KeyPair;
+import java.security.interfaces.RSAPrivateCrtKey;
import java.util.ArrayList;
import java.util.Collection;
@@ -72,10 +74,10 @@ public class OpenSSHGeneratorFileKeyProvider extends AbstractGeneratorHostKeyPro
@Override
protected void doWriteKeyPair(String resourceKey, KeyPair kp, OutputStream os) throws IOException, GeneralSecurityException {
Collection<Object> items = new ArrayList<>();
- items.add(kp.getPrivate());
- items.add(kp.getPublic());
+ items.add(new PEMItem(kp.getPrivate().getEncoded(), "PRIVATE KEY"));
byte[] bytes = PEMUtil.encode(items);
os.write(bytes);
+ os.close();
}
}
http://git-wip-us.apache.org/repos/asf/karaf/blob/9d1372e9/shell/ssh/src/test/java/org/apache/karaf/shell/ssh/OpenSSHGeneratorKeyFileProviderTest.java
----------------------------------------------------------------------
diff --git a/shell/ssh/src/test/java/org/apache/karaf/shell/ssh/OpenSSHGeneratorKeyFileProviderTest.java b/shell/ssh/src/test/java/org/apache/karaf/shell/ssh/OpenSSHGeneratorKeyFileProviderTest.java
index 2ba97cf..04066c4 100644
--- a/shell/ssh/src/test/java/org/apache/karaf/shell/ssh/OpenSSHGeneratorKeyFileProviderTest.java
+++ b/shell/ssh/src/test/java/org/apache/karaf/shell/ssh/OpenSSHGeneratorKeyFileProviderTest.java
@@ -18,9 +18,14 @@
*/
package org.apache.karaf.shell.ssh;
-import org.junit.Test;
-
+import java.io.File;
+import java.math.BigInteger;
import java.security.KeyPair;
+import java.security.interfaces.RSAPrivateCrtKey;
+import java.security.interfaces.RSAPublicKey;
+
+import org.junit.Assert;
+import org.junit.Test;
public class OpenSSHGeneratorKeyFileProviderTest {
@@ -30,5 +35,28 @@ public class OpenSSHGeneratorKeyFileProviderTest {
prov.setOverwriteAllowed(false);
KeyPair keys = prov.loadKeys().iterator().next();
// how would we tell if they read 'correctly'? Well, the base class will throw if the key isn't reasonable.
+ Assert.assertNotNull(keys);
+ Assert.assertTrue("Loaded key is not RSA Key", keys.getPublic() instanceof RSAPublicKey);
+ Assert.assertEquals(65537, ((RSAPublicKey) keys.getPublic()).getPublicExponent().intValue());
+ }
+
+ @Test
+ public void writeSshKey() throws Exception {
+ // create a temporary file
+ File temp = File.createTempFile(this.getClass().getCanonicalName(), ".pem");
+ temp.deleteOnExit();
+ OpenSSHGeneratorFileKeyProvider prov = new OpenSSHGeneratorFileKeyProvider(temp.getPath(), "RSA", 4096);
+ KeyPair keys = prov.loadKeys().iterator().next();
+ Assert.assertNotNull(keys);
+ Assert.assertTrue(temp.exists());
+ Assert.assertFalse(temp.length() == 0);
+ BigInteger privateExponent = ((RSAPrivateCrtKey) keys.getPrivate()).getPrivateExponent();
+ // read and check if correctly read
+ prov = new OpenSSHGeneratorFileKeyProvider(temp.getPath());
+ keys = prov.loadKeys().iterator().next();
+ Assert.assertNotNull(keys);
+ Assert.assertTrue("Loaded key is not RSA Key", keys.getPrivate() instanceof RSAPrivateCrtKey);
+ BigInteger privateExponent2 = ((RSAPrivateCrtKey) keys.getPrivate()).getPrivateExponent();
+ Assert.assertEquals(privateExponent, privateExponent2);
}
}