[jbosstools-commits] JBoss Tools SVN: r35325 - in trunk/as: tests/org.jboss.ide.eclipse.as.openshift.test/src/org/jboss/ide/eclipse/as/openshift/test/internal/core and 1 other directory.
jbosstools-commits at lists.jboss.org
jbosstools-commits at lists.jboss.org
Tue Oct 4 09:40:43 EDT 2011
Author: adietish
Date: 2011-10-04 09:40:42 -0400 (Tue, 04 Oct 2011)
New Revision: 35325
Modified:
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/core/ISSHPublicKey.java
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/core/SSHKeyPair.java
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/core/SSHPublicKey.java
trunk/as/tests/org.jboss.ide.eclipse.as.openshift.test/src/org/jboss/ide/eclipse/as/openshift/test/internal/core/SSHKeyTest.java
Log:
[JBIDE-9793] implemented capability to load public key files in SSHPublicKey
Modified: trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/core/ISSHPublicKey.java
===================================================================
--- trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/core/ISSHPublicKey.java 2011-10-04 13:04:52 UTC (rev 35324)
+++ trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/core/ISSHPublicKey.java 2011-10-04 13:40:42 UTC (rev 35325)
@@ -25,5 +25,4 @@
*/
public String getPublicKey() throws OpenshiftException ;
- public void update(ISSHPublicKey sshPublicKey) throws OpenshiftException;
}
Modified: trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/core/SSHKeyPair.java
===================================================================
--- trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/core/SSHKeyPair.java 2011-10-04 13:04:52 UTC (rev 35324)
+++ trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/core/SSHKeyPair.java 2011-10-04 13:40:42 UTC (rev 35325)
@@ -100,11 +100,4 @@
protected String getPublicKeyPath() {
return publicKeyPath;
}
-
- @Override
- public void update(ISSHPublicKey sshPublicKey) {
- // do nothing, there's no sense to update this key since the public key
- // misses the private key
- }
-
}
Modified: trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/core/SSHPublicKey.java
===================================================================
--- trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/core/SSHPublicKey.java 2011-10-04 13:04:52 UTC (rev 35324)
+++ trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/core/SSHPublicKey.java 2011-10-04 13:40:42 UTC (rev 35325)
@@ -10,26 +10,48 @@
******************************************************************************/
package org.jboss.ide.eclipse.as.openshift.core;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import org.jboss.ide.eclipse.as.openshift.core.internal.utils.StreamUtils;
+
/**
* @author André Dietisheim
*/
public class SSHPublicKey implements ISSHPublicKey {
+ private static final Pattern PUBLICKEY_PATTERN = Pattern.compile("[^ ]+ ([^ ]+)( .+)*");
+
private String publicKey;
+ public SSHPublicKey(File publicKeyFilePath) throws IOException, OpenshiftException {
+ this.publicKey = extractPublicKey(publicKeyFilePath);
+ }
+
public SSHPublicKey(String publicKey) {
this.publicKey = publicKey;
}
+ private String extractPublicKey(File file) throws OpenshiftException, FileNotFoundException, IOException {
+ String keyWithIdAndComment = StreamUtils.readToString(new FileReader(file));
+ Matcher matcher = PUBLICKEY_PATTERN.matcher(keyWithIdAndComment);
+ if (!matcher.find()
+ || matcher.groupCount() < 1) {
+ throw new OpenshiftException("Could not load public key from file \"{0}\"", file.getAbsolutePath());
+ }
+
+ return matcher.group(1);
+ }
+
public String getPublicKey() {
return publicKey;
}
-
+
void update(String publicKey) {
+ this.publicKey = publicKey;
}
-
- public void update(ISSHPublicKey sshPublicKey) throws OpenshiftException {
- this.publicKey = sshPublicKey.getPublicKey();
- }
}
Modified: trunk/as/tests/org.jboss.ide.eclipse.as.openshift.test/src/org/jboss/ide/eclipse/as/openshift/test/internal/core/SSHKeyTest.java
===================================================================
--- trunk/as/tests/org.jboss.ide.eclipse.as.openshift.test/src/org/jboss/ide/eclipse/as/openshift/test/internal/core/SSHKeyTest.java 2011-10-04 13:04:52 UTC (rev 35324)
+++ trunk/as/tests/org.jboss.ide.eclipse.as.openshift.test/src/org/jboss/ide/eclipse/as/openshift/test/internal/core/SSHKeyTest.java 2011-10-04 13:40:42 UTC (rev 35325)
@@ -10,13 +10,16 @@
******************************************************************************/
package org.jboss.ide.eclipse.as.openshift.test.internal.core;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
+import org.jboss.ide.eclipse.as.openshift.core.ISSHPublicKey;
import org.jboss.ide.eclipse.as.openshift.core.SSHKeyPair;
+import org.jboss.ide.eclipse.as.openshift.core.SSHPublicKey;
import org.junit.Test;
public class SSHKeyTest {
@@ -36,7 +39,7 @@
}
@Test
- public void canLoadPublicKey() throws Exception {
+ public void canLoadKeyPair() throws Exception {
String publicKeyPath = createTempFile().getAbsolutePath();
String privateKeyPath = createTempFile().getAbsolutePath();
SSHKeyPair.create(PASSPHRASE, privateKeyPath, publicKeyPath);
@@ -49,6 +52,23 @@
assertTrue(!publicKey.contains(" ")); // no comment
}
+ @Test
+ public void canLoadPublicKey() throws Exception {
+ String publicKeyPath = createTempFile().getAbsolutePath();
+ String privateKeyPath = createTempFile().getAbsolutePath();
+ SSHKeyPair.create(PASSPHRASE, privateKeyPath, publicKeyPath);
+
+ ISSHPublicKey sshKey = new SSHPublicKey(new File(publicKeyPath));
+ String publicKey = sshKey.getPublicKey();
+
+ assertNotNull(publicKey);
+ assertTrue(!publicKey.contains("ssh-rsa")); // no identifier
+ assertTrue(!publicKey.contains(" ")); // no comment
+
+ SSHKeyPair keyPair = SSHKeyPair.load(privateKeyPath, publicKeyPath);
+ assertEquals(publicKey, keyPair.getPublicKey());
+ }
+
private File createTempFile() throws IOException {
return File.createTempFile(String.valueOf(System.currentTimeMillis()), null);
}
More information about the jbosstools-commits
mailing list