[jbosstools-commits] JBoss Tools SVN: r41602 - in trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui: wizard and 1 other directory.

jbosstools-commits at lists.jboss.org jbosstools-commits at lists.jboss.org
Thu May 31 19:17:35 EDT 2012


Author: adietish
Date: 2012-05-31 19:17:33 -0400 (Thu, 31 May 2012)
New Revision: 41602

Added:
   trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/utils/SSHUserConfig.java
Modified:
   trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/utils/FileUtils.java
   trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/NewDomainWizardPage.java
   trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/NewDomainWizardPageModel.java
   trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/PassphraseDialog.java
Log:
[JBIDE-12031] now allowing keys id_rsa or libra_id_rsa (depending on the user to have a <home>/.ssh/config with libra-keys or not)

Modified: trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/utils/FileUtils.java
===================================================================
--- trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/utils/FileUtils.java	2012-05-31 23:16:19 UTC (rev 41601)
+++ trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/utils/FileUtils.java	2012-05-31 23:17:33 UTC (rev 41602)
@@ -155,6 +155,7 @@
 
 	public static final void writeTo(String content, File destination) throws FileNotFoundException {
 		PrintWriter writer = new PrintWriter(destination);
+		writer.write(content);
 		writer.flush();
 		writer.close();
 	}

Added: trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/utils/SSHUserConfig.java
===================================================================
--- trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/utils/SSHUserConfig.java	                        (rev 0)
+++ trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/utils/SSHUserConfig.java	2012-05-31 23:17:33 UTC (rev 41602)
@@ -0,0 +1,99 @@
+/*******************************************************************************
+ * Copyright (c) 2012 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.openshift.express.internal.ui.utils;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import com.openshift.client.OpenShiftException;
+
+/**
+ * A class that parses the user ssh configuration and retrieves the
+ * <tt>IdentityFile</tt> setting from a <tt>Host</tt> block that is configuring
+ * the OpenShift application (<tt>rhcloud.com</tt>) PaaS.
+ * 
+ * @author Andre Dietisheim
+ */
+public class SSHUserConfig {
+
+	private static final String VALUE_LIBRA_SSH_HOST = "rhcloud.com";
+	private static final String KEY_CONFIGBLOCK_HOST_START = "Host ";
+	private static final String KEY_CONFIGBLOCK_HOST_END = "\n";
+	private static final Pattern IDENTITYFILE_PATTERN = Pattern.compile(".+IdentityFile (.+)");
+	private static final String CONFIG_FILENAME = "config";
+	private File configFile;
+
+	public SSHUserConfig(String sshHome) {
+		this(new File(sshHome, CONFIG_FILENAME));
+	}
+
+	public SSHUserConfig(File configFile) {
+		this.configFile = configFile;
+	}
+
+	public boolean exists() {
+		return FileUtils.canRead(configFile);
+	}
+
+	public File getFile() {
+		return configFile;
+	}
+	
+	public boolean hasLibraIdentifyFile() throws OpenShiftException {
+		return getLibraIdentityFile() != null;
+	}
+
+	public String getLibraIdentityFile() throws OpenShiftException {
+		if (!exists()) {
+			return null;
+		}
+
+		BufferedReader reader = null;
+		try {
+			reader = new BufferedReader(new FileReader(configFile));
+			for (String data = reader.readLine(); data != null; data = reader.readLine()) {
+				if (!data.startsWith(KEY_CONFIGBLOCK_HOST_START)
+						|| !data.endsWith(VALUE_LIBRA_SSH_HOST)) {
+					continue;
+				}
+
+				for (data = reader.readLine(); data != null; reader.readLine()) {
+					if (data.equals(KEY_CONFIGBLOCK_HOST_END)) {
+						continue;
+					}
+
+					Matcher matcher = IDENTITYFILE_PATTERN.matcher(data);
+					if (!matcher.find()
+							|| matcher.groupCount() < 1) {
+						continue;
+					}
+
+					return matcher.group(1);
+				}
+			}
+			return null;
+		} catch (IOException e) {
+			throw new OpenShiftException("Could not read file {0}", configFile);
+		} finally {
+			try {
+				if (reader != null) {
+					reader.close();
+				}
+			} catch (IOException e) {
+				// ignore THIS IS INTENTIONAL
+			}
+		}
+	}
+}


Property changes on: trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/utils/SSHUserConfig.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain

Modified: trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/NewDomainWizardPage.java
===================================================================
--- trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/NewDomainWizardPage.java	2012-05-31 23:16:19 UTC (rev 41601)
+++ trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/NewDomainWizardPage.java	2012-05-31 23:17:33 UTC (rev 41602)
@@ -54,6 +54,7 @@
 import org.jboss.tools.common.ui.ssh.SshPrivateKeysPreferences;
 import org.jboss.tools.openshift.express.internal.ui.OpenShiftUIActivator;
 import org.jboss.tools.openshift.express.internal.ui.utils.FileUtils;
+import org.jboss.tools.openshift.express.internal.ui.utils.SSHUserConfig;
 import org.jboss.tools.openshift.express.internal.ui.utils.StringUtils;
 
 import com.openshift.client.OpenShiftException;
@@ -123,7 +124,7 @@
 		}
 
 		Button browseButton = new Button(container, SWT.PUSH);
-		browseButton.setText("Browse");
+		browseButton.setText("Browse...");
 		GridDataFactory.fillDefaults()
 				.align(SWT.LEFT, SWT.CENTER).hint(100, SWT.DEFAULT).applyTo(browseButton);
 		browseButton.addSelectionListener(onBrowse());
@@ -150,10 +151,10 @@
 			public void widgetSelected(SelectionEvent e) {
 
 				try {
-					if (pageModel.libraPublicKeyExists()) {
+					if (pageModel.publicKeyExists()) {
 						MessageDialog.openInformation(getShell(), 
 								"Libra Key already present", 
-								"You already have a key at \"" + pageModel.getLibraPublicKey() + "\". Please move it or use it.");
+								"You already have a key at \"" + pageModel.getPublicKey() + "\". Please move it or use it.");
 						return;
 					} 
 
@@ -220,7 +221,7 @@
 
 	@Override
 	protected void setupWizardPageSupport(DataBindingContext dbc) {
-		ParametrizableWizardPageSupport.create(IStatus.ERROR, this, dbc);
+		ParametrizableWizardPageSupport.create(IStatus.ERROR | IStatus.CANCEL, this, dbc);
 	}
 
 	private class SSHKeyValidator implements IValidator {
@@ -232,10 +233,14 @@
 					|| !FileUtils.canRead((String) value)) {
 				return ValidationStatus.error("You have to provide a valid ssh public key");
 			}
-			if (!isKeyKnownToSsh((String) value)) {
+			if (pageModel.hasConfiguredFixedPrivateKeys()) {
 				return ValidationStatus.warning(
-						NLS.bind("Could not find the private portion for your public key. "
-								+ "Make sure it is listed in the ssh2 preferences.", value));
+						NLS.bind("Your SSH config ({0}) contains fixed keys for OpenShift servers. " +
+								"This can override any Eclipse specific SSH key preferences.", new SSHUserConfig(pageModel.getSSH2Home()).getFile()));
+			} else if (!isKeyKnownToSsh((String) value)) {
+					return ValidationStatus.warning(
+							NLS.bind("Could not find the private portion for your public key in the preferences. "
+									+ "Make sure it is listed in the ssh2 preferences.", value));
 			}
 			return ValidationStatus.ok();
 		}
@@ -244,14 +249,15 @@
 			if (StringUtils.isEmpty(publicKeyPath)) {
 				return false;
 			}
-			for (String preferencesKey : SshPrivateKeysPreferences.getKeys()) {
+			for (String preferencesKey : pageModel.getPrivateKeysFromPreferences()) {
 				try {
 					File privateKey = SshPrivateKeysPreferences.getKeyFile(preferencesKey);
 					if (privateKey == null
 							|| !FileUtils.canRead(privateKey)) {
 						continue;
 					}
-					if (publicKeyPath.startsWith(privateKey.getAbsolutePath() + ".")) {
+					if (publicKeyPath.startsWith(privateKey.getAbsolutePath() + ".")
+							|| publicKeyPath.startsWith(privateKey.getPath() + ".")) {
 						return true;
 					}
 				} catch (FileNotFoundException e) {

Modified: trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/NewDomainWizardPageModel.java
===================================================================
--- trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/NewDomainWizardPageModel.java	2012-05-31 23:16:19 UTC (rev 41601)
+++ trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/NewDomainWizardPageModel.java	2012-05-31 23:17:33 UTC (rev 41602)
@@ -22,6 +22,7 @@
 import org.jboss.tools.common.ui.databinding.ObservableUIPojo;
 import org.jboss.tools.openshift.express.internal.core.console.UserDelegate;
 import org.jboss.tools.openshift.express.internal.ui.utils.FileUtils;
+import org.jboss.tools.openshift.express.internal.ui.utils.SSHUserConfig;
 
 import com.openshift.client.IDomain;
 import com.openshift.client.IOpenShiftSSHKey;
@@ -35,8 +36,10 @@
  */
 public class NewDomainWizardPageModel extends ObservableUIPojo {
 
-	public static final String LIBRA_KEY = "libra_id_rsa";
+	public static final String ID_RSA_FILENAME = "id_rsa";
+	public static final String LIBRA_ID_RSA_FILENAME = "libra_id_rsa";
 	private static final String PUBLIC_KEY_SUFFIX = ".pub";
+	private static final String KEYS_SEPARATOR = ",";
 
 	private static final String SSHKEY_DEFAULT_NAME = "jbosstools"; //$NON-NLS-1$
 
@@ -53,21 +56,22 @@
 	}
 
 	public void initSshKey() throws OpenShiftException {
-		if (!libraPublicKeyExists()) {
+		if (!publicKeyExists()) {
 			return;
 		}
-		File libraPublicKey = getLibraPublicKey();
+		File libraPublicKey = getPublicKey();
 		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();
+	public File getPublicKey() throws OpenShiftException {
+		File libraPrivateKey = getPrivateKey();
 		return new File(libraPrivateKey.getParent(), getPublicKeyPath(libraPrivateKey.getName()));
 	}
 
@@ -75,30 +79,39 @@
 		return privateKeyPath + PUBLIC_KEY_SUFFIX;
 	}
 
-	public File getLibraPrivateKey() throws OpenShiftException {
-		String ssh2Home = getSSH2Home();
-		File ssh2HomeFile = new File(ssh2Home);
-		if (!FileUtils.canRead(ssh2HomeFile)) {
-			try {
-				ssh2HomeFile.createNewFile();
-			} catch(IOException e) {
-				throw new OpenShiftException("Could not create ssh2 home directory at {0}", ssh2Home);
-			}
+	public File getPrivateKey() throws OpenShiftException {
+		String ssh2Home = checkedGetSSH2Home();
+		ensureSSHHomeExists(ssh2Home);
+		if (hasConfiguredFixedPrivateKeys()) {
+			return new File(ssh2Home, LIBRA_ID_RSA_FILENAME);
+		} else {
+			return new File(ssh2Home, ID_RSA_FILENAME);
 		}
-		
-		return new File(ssh2Home, LIBRA_KEY);
 	}
 
-	private String getSSH2Home() throws OpenShiftException {
-		Preferences preferences = JSchCorePlugin.getPlugin().getPluginPreferences();
-		String ssh2Home = preferences.getString(IConstants.KEY_SSH2HOME);
+	private String checkedGetSSH2Home() throws OpenShiftException {
+		String ssh2Home = getSSH2Home();
 		if (ssh2Home == null 
 				|| ssh2Home.trim().length() == 0) {
 			throw new OpenShiftException("Could not determine your ssh2 home directory");
 		}
 		return ssh2Home;
 	}
+	
+	public String getSSH2Home() {
+		return JSchCorePlugin.getPlugin().getPluginPreferences().getString(IConstants.KEY_SSH2HOME);
+	}
 
+	public boolean hasConfiguredFixedPrivateKeys() {
+		try {
+			SSHUserConfig sshUserConfig = new SSHUserConfig(checkedGetSSH2Home());
+			return sshUserConfig.hasLibraIdentifyFile();
+		} catch (OpenShiftException e) {
+			return false;
+		}
+	}
+
+
 	public String getDomainId() {
 		return this.domainId;
 	}
@@ -123,25 +136,25 @@
 		return sshKey;
 	}
 	
-	public boolean libraPublicKeyExists() throws OpenShiftException {
-		return FileUtils.canRead(getLibraPublicKey());
+	public boolean publicKeyExists() throws OpenShiftException {
+		return FileUtils.canRead(getPublicKey());
 	}
 
 	public void createLibraKeyPair(String passPhrase) throws FileNotFoundException, OpenShiftException {
-		File libraPublicKey = getLibraPublicKey();
+		File libraPublicKey = getPublicKey();
 		if (libraPublicKey.canRead()) {
 			// key already exists
 			return;
 		}
-		createSSHHome(getSSH2Home());
-		File libraPrivateKey = getLibraPrivateKey();
+		ensureSSHHomeExists(checkedGetSSH2Home());
+		File libraPrivateKey = getPrivateKey();
 		SSHKeyPair keyPair = SSHKeyPair.create(passPhrase, libraPrivateKey.getAbsolutePath(), libraPublicKey.getAbsolutePath());
 		setFilePermissions(libraPrivateKey);
 		addToPrivateKeysPreferences(keyPair);
 		setSshKey(keyPair.getPublicKeyPath());
 	}
 	
-	private void createSSHHome(String ssh2Home)
+	private void ensureSSHHomeExists(String ssh2Home)
 			throws OpenShiftException {
 		File ssh2HomeFile = new File(ssh2Home);
 		if (FileUtils.canRead(ssh2HomeFile)) {
@@ -171,6 +184,17 @@
 		file.setWritable(true, true);
 	}
 
+	public String[] getPrivateKeysFromPreferences() {
+		String privateKeys = 
+				JSchCorePlugin.getPlugin().getPluginPreferences().getString(IConstants.KEY_PRIVATEKEY);
+		if (privateKeys != null 
+				&& privateKeys.trim().length() > 0) {
+			return privateKeys.split(KEYS_SEPARATOR);
+		} else {
+			return new String[0];
+		}
+	}
+	
 	private void addToPrivateKeysPreferences(SSHKeyPair keyPair) {
 		Preferences preferences = JSchCorePlugin.getPlugin().getPluginPreferences();
 		String privateKeys = preferences.getString(IConstants.KEY_PRIVATEKEY);

Modified: trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/PassphraseDialog.java
===================================================================
--- trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/PassphraseDialog.java	2012-05-31 23:16:19 UTC (rev 41601)
+++ trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/PassphraseDialog.java	2012-05-31 23:17:33 UTC (rev 41602)
@@ -10,34 +10,21 @@
  ******************************************************************************/
 package org.jboss.tools.openshift.express.internal.ui.wizard;
 
-import org.eclipse.jface.dialogs.IInputValidator;
 import org.eclipse.jface.dialogs.InputDialog;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Shell;
-import org.jboss.tools.openshift.express.internal.ui.utils.StringUtils;
 
 public class PassphraseDialog extends InputDialog {
 
 	public PassphraseDialog(Shell shell) {
 		super(shell
 				, "New ssh key"
-				, "Please pick a passphrase for your new ssh key pair"
+				, "Please pick a passphrase for your new ssh key pair. A passphrase is recommended but not required."
 				, null
-				, new PassphraseValidator());
+				, null);
 	}
 
 	protected int getInputTextStyle() {
 		return SWT.SINGLE | SWT.BORDER | SWT.PASSWORD;
 	}
-
-	private static class PassphraseValidator implements IInputValidator {
-
-		@Override
-		public String isValid(String input) {
-			if (StringUtils.isEmpty(input)) {
-				return "You have to provide a pass phrase";
-			}
-			return null;
-		}
-	}
 }



More information about the jbosstools-commits mailing list