Author: adietish
Date: 2011-10-26 16:27:19 -0400 (Wed, 26 Oct 2011)
New Revision: 36023
Modified:
branches/jbosstools-3.3.0.M4/as/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/NewDomainWizardPage.java
branches/jbosstools-3.3.0.M4/as/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/NewDomainWizardPageModel.java
Log:
[JNIDE-10049] now creating a single key file only (libra_id_rsa) and pre-initializing it
in the UI if the file exists
Modified:
branches/jbosstools-3.3.0.M4/as/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/NewDomainWizardPage.java
===================================================================
---
branches/jbosstools-3.3.0.M4/as/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/NewDomainWizardPage.java 2011-10-26
20:16:28 UTC (rev 36022)
+++
branches/jbosstools-3.3.0.M4/as/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/NewDomainWizardPage.java 2011-10-26
20:27:19 UTC (rev 36023)
@@ -26,6 +26,7 @@
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.InputDialog;
+import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.wizard.IWizard;
@@ -93,6 +94,16 @@
"You have to select a ssh public key")),
new UpdateValueStrategy().setAfterGetValidator(new SSHKeyValidator()));
ControlDecorationSupport.create(sshKeyTextBinding, SWT.TOP | SWT.LEFT);
+ try {
+ model.initSshKey();
+ } catch (OpenShiftException ex) {
+ IStatus status = new Status(IStatus.ERROR, OpenShiftUIActivator.PLUGIN_ID,
+ "Could check your ssh keys", ex);
+ OpenShiftUIActivator.log(status);
+ ErrorDialog.openError(getShell(),
+ "Error checking your ssh keys",
+ "Could not check your ssh keys", status);
+ }
Button browseButton = new Button(container, SWT.PUSH);
browseButton.setText("Browse");
@@ -113,7 +124,6 @@
GridDataFactory.fillDefaults()
.span(3, 1).align(SWT.FILL, SWT.CENTER).applyTo(sshPrefsLink);
sshPrefsLink.addSelectionListener(onSshPrefs());
-
}
private SelectionListener onNew() {
@@ -122,26 +132,35 @@
@Override
public void widgetSelected(SelectionEvent e) {
- InputDialog dialog = new PassphraseDialog(getShell());
- if (Dialog.OK == dialog.open()) {
- try {
- String passPhrase = dialog.getValue();
- model.createSShKeyPair(passPhrase);
- } catch (FileNotFoundException ex) {
- IStatus status = new Status(IStatus.ERROR, OpenShiftUIActivator.PLUGIN_ID,
- "Could not read the ssh key folder", ex);
- OpenShiftUIActivator.log(status);
- ErrorDialog.openError(getShell(),
- "Error creating a new ssh key pair",
- "Could not create a new ssh key pair", status);
- } catch (OpenShiftException ex) {
- IStatus status = new Status(IStatus.ERROR, OpenShiftUIActivator.PLUGIN_ID,
- "Could not create an ssh key pair", ex);
- OpenShiftUIActivator.log(status);
- ErrorDialog.openError(getShell(),
- "Error creating a new ssh key pair",
- "Could not create a new ssh key pair", status);
+ try {
+ if (model.libraPublicKeyExists()) {
+ MessageDialog.openInformation(getShell(),
+ "Libra Key already present",
+ "You already have a key at \"" + model.getLibraPublicKey() +
"\". Please move it or use it.");
+ return;
+ }
+
+ InputDialog dialog = new PassphraseDialog(getShell());
+ if (Dialog.OK == dialog.open()) {
+ try {
+ String passPhrase = dialog.getValue();
+ model.createLibraKeyPair(passPhrase);
+ } catch (FileNotFoundException ex) {
+ IStatus status = new Status(IStatus.ERROR, OpenShiftUIActivator.PLUGIN_ID,
+ "Could not read the ssh key folder", ex);
+ OpenShiftUIActivator.log(status);
+ ErrorDialog.openError(getShell(),
+ "Error creating a new ssh key pair",
+ "Could not create a new ssh key pair", status);
+ }
}
+ } catch (OpenShiftException ex) {
+ IStatus status = new Status(IStatus.ERROR, OpenShiftUIActivator.PLUGIN_ID,
+ "Could not create an ssh key pair", ex);
+ OpenShiftUIActivator.log(status);
+ ErrorDialog.openError(getShell(),
+ "Error creating a new ssh key pair",
+ "Could not create a new ssh key pair", status);
}
}
};
Modified:
branches/jbosstools-3.3.0.M4/as/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/NewDomainWizardPageModel.java
===================================================================
---
branches/jbosstools-3.3.0.M4/as/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/NewDomainWizardPageModel.java 2011-10-26
20:16:28 UTC (rev 36022)
+++
branches/jbosstools-3.3.0.M4/as/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/NewDomainWizardPageModel.java 2011-10-26
20:27:19 UTC (rev 36023)
@@ -18,7 +18,6 @@
import org.eclipse.jsch.internal.core.IConstants;
import org.eclipse.jsch.internal.core.JSchCorePlugin;
import org.jboss.tools.common.ui.databinding.ObservableUIPojo;
-import org.jboss.tools.common.ui.ssh.SshPrivateKeysPreferences;
import org.jboss.tools.openshift.express.client.IDomain;
import org.jboss.tools.openshift.express.client.ISSHPublicKey;
import org.jboss.tools.openshift.express.client.IUser;
@@ -32,7 +31,7 @@
*/
public class NewDomainWizardPageModel extends ObservableUIPojo {
- private static final String OPENSHIFT_KEY_PREFIX = "openshift_id_rsa_";
+ public static final String LIBRA_KEY = "libra_id_rsa";
private static final String PUBLIC_KEY_SUFFIX = ".pub";
public static final String PROPERTY_NAMESPACE = "namespace";
@@ -49,6 +48,40 @@
this.user = user;
}
+ public void initSshKey() throws OpenShiftException {
+ if (!libraPublicKeyExists()) {
+ return;
+ }
+ File libraPublicKey = getLibraPublicKey();
+ setSshKey(libraPublicKey.getAbsolutePath());
+ }
+
+ /**
+ * Returns the file of the libra public key. It is not checking if the file exists.
+ *
+ * @return the libra public key
+ * @throws OpenShiftException
+ */
+ public File getLibraPublicKey() throws OpenShiftException {
+ File libraPrivateKey = getLibraPrivateKey();
+ return new File(libraPrivateKey.getParent(),
getPublicKeyPath(libraPrivateKey.getName()));
+ }
+
+ private String getPublicKeyPath(String privateKeyPath) {
+ return privateKeyPath + PUBLIC_KEY_SUFFIX;
+ }
+
+ public File getLibraPrivateKey() throws OpenShiftException {
+ Preferences preferences = JSchCorePlugin.getPlugin().getPluginPreferences();
+ String ssh2Home = preferences.getString(IConstants.KEY_SSH2HOME);
+ if (ssh2Home == null
+ || ssh2Home.trim().length() == 0) {
+ throw new OpenShiftException("Could not determine your ssh2 home
directory");
+ }
+
+ return new File(ssh2Home, LIBRA_KEY);
+ }
+
public String getNamespace() {
return this.namespace;
}
@@ -61,10 +94,19 @@
public String getSshKey() {
return sshKey;
}
+
+ public boolean libraPublicKeyExists() throws OpenShiftException {
+ return FileUtils.canRead(getLibraPublicKey());
+ }
- public void createSShKeyPair(String passPhrase) throws FileNotFoundException,
OpenShiftException {
- String sshKeysDirectory = SshPrivateKeysPreferences.getSshKeyDirectory();
- SSHKeyPair keyPair = createSshKeyPair(passPhrase, sshKeysDirectory);
+ public void createLibraKeyPair(String passPhrase) throws FileNotFoundException,
OpenShiftException {
+ File libraPublicKey = getLibraPublicKey();
+ if (libraPublicKey.canRead()) {
+ // key already exists
+ return;
+ }
+ File libraPrivateKey = getLibraPrivateKey();
+ SSHKeyPair keyPair = SSHKeyPair.create(passPhrase, libraPrivateKey.getAbsolutePath(),
libraPublicKey.getAbsolutePath());
addToPrivateKeysPreferences(keyPair);
setSshKey(keyPair.getPublicKeyPath());
}
@@ -79,31 +121,10 @@
privateKeys = keyPair.getPrivateKeyPath();
}
preferences.setValue(IConstants.KEY_PRIVATEKEY, privateKeys);
- JSchCorePlugin.getPlugin().setNeedToLoadKnownHosts(true);
JSchCorePlugin.getPlugin().setNeedToLoadKeys(true);
JSchCorePlugin.getPlugin().savePluginPreferences();
}
-
- private SSHKeyPair createSshKeyPair(String passPhrase, String sshKeysDirectory) throws
OpenShiftException {
- String privateKeyPath = getKeyPairFileName(sshKeysDirectory);
- String publicKeyPath = getPublicKeyPath(privateKeyPath);
- return SSHKeyPair.create(passPhrase, privateKeyPath, publicKeyPath);
- }
-
- private String getKeyPairFileName(String sshKeysDirectory) {
- int i = 0;
- File privateKey = null;
- while (FileUtils.canRead(privateKey = new File(sshKeysDirectory, OPENSHIFT_KEY_PREFIX +
i))
- || FileUtils.canRead(new File(sshKeysDirectory,
getPublicKeyPath(privateKey.getName())))) {
- i++;
- }
- return privateKey.getAbsolutePath();
- }
-
- private String getPublicKeyPath(String privateKeyPath) {
- return privateKeyPath + PUBLIC_KEY_SUFFIX;
- }
-
+
public void setSshKey(String sshKey) {
firePropertyChange(PROPERTY_SSHKEY, this.sshKey, this.sshKey = sshKey);
}