From 2432ae6130fc9b5c4f93853761fb6b9716d5a24f Mon Sep 17 00:00:00 2001 From: "donghyuk.yang" Date: Sun, 11 May 2014 15:41:29 +0900 Subject: [PATCH] VIEW: ROOTSTRAP: Shows why you insert ssh private and public key in connection dialog. Change-Id: I2bf3f78d5fb91675eab8add75f37cd7b9f0fdd39 Signed-off-by: donghyuk.yang --- .../src/org/tizen/nativeplatform/util/SSHUtil.java | 37 ++++++-- .../views/ui/RemoteConnectionDialog.java | 101 +++++++++++++-------- .../views/ui/RootstrapUIMessages.properties | 2 + 3 files changed, 92 insertions(+), 48 deletions(-) diff --git a/org.tizen.nativeplatform/src/org/tizen/nativeplatform/util/SSHUtil.java b/org.tizen.nativeplatform/src/org/tizen/nativeplatform/util/SSHUtil.java index 9481550..d7631b2 100644 --- a/org.tizen.nativeplatform/src/org/tizen/nativeplatform/util/SSHUtil.java +++ b/org.tizen.nativeplatform/src/org/tizen/nativeplatform/util/SSHUtil.java @@ -61,7 +61,31 @@ public class SSHUtil { return new Path(DEVICE_SSH_DIR).append(filename).toString(); } - public static void registerAuthorizedKey(String publicKey) { + public static boolean registerAuthorizedKey(String publicKey) { + try { + FileReader fileReader = new FileReader(publicKey); + BufferedReader reader = new BufferedReader(fileReader); + String pubkey = reader.readLine(); + IRemoteExecutionTools exectool = RemoteConnectionManager.getRemoteTools().getExecTool(); + IRemoteFileTools filetool = RemoteConnectionManager.getRemoteTools().getFileTool(); + String userhome = RemoteConnectionManager.getRemoteTools().getUserhome(); + IPath remoteSshPath = new Path(userhome).append(".ssh"); + String remoteAuthfile = remoteSshPath.append("authorized_keys").toString(); + filetool.assureDirectory(remoteSshPath.toString(), new NullProgressMonitor()); + String cmd = String.format("echo \"%s\" >> %s", pubkey, remoteAuthfile); + exectool.executeBashCommand(cmd); + if (!isSetAuthorizedKey(publicKey)) { + return false; + } + } catch (Exception e) { + e.printStackTrace(); + logger.error("Failed to register authorized key", e); + } + return true; + } + + public static boolean isSetAuthorizedKey(String publicKey) { + boolean isSet = false; try { FileReader fileReader = new FileReader(publicKey); BufferedReader reader = new BufferedReader(fileReader); @@ -71,22 +95,17 @@ public class SSHUtil { String userhome = RemoteConnectionManager.getRemoteTools().getUserhome(); IPath remoteSshPath = new Path(userhome).append(".ssh"); String remoteAuthfile = remoteSshPath.append("authorized_keys").toString(); - boolean needSet = true; if (filetool.hasFile(remoteAuthfile, new NullProgressMonitor())) { String cmd = String.format("grep -rn \"%s\" %s", pubkey, remoteAuthfile); String output = exectool.executeWithOutput(cmd); if (!output.isEmpty()) { - needSet = false; + isSet = true; } } - if (needSet) { - filetool.assureDirectory(remoteSshPath.toString(), new NullProgressMonitor()); - String cmd = String.format("echo \"%s\" >> %s", pubkey, remoteAuthfile); - exectool.executeBashCommand(cmd); - } } catch (Exception e) { e.printStackTrace(); - logger.error("Failed to register authorized key%s]", e); + logger.error("Failed to check authorized key", e); } + return isSet; } } diff --git a/org.tizen.nativeplatform/src/org/tizen/nativeplatform/views/ui/RemoteConnectionDialog.java b/org.tizen.nativeplatform/src/org/tizen/nativeplatform/views/ui/RemoteConnectionDialog.java index ad502bf..d5a9ffa 100644 --- a/org.tizen.nativeplatform/src/org/tizen/nativeplatform/views/ui/RemoteConnectionDialog.java +++ b/org.tizen.nativeplatform/src/org/tizen/nativeplatform/views/ui/RemoteConnectionDialog.java @@ -41,6 +41,7 @@ import org.eclipse.jface.operation.IRunnableWithProgress; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; @@ -53,7 +54,6 @@ import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; import org.tizen.common.util.DialogUtil; import org.tizen.common.util.log.UserLogger; -import org.tizen.nativeplatform.preferences.PreferencesManager; import org.tizen.nativeplatform.preferences.RemoteServerPreferencesManager; import org.tizen.nativeplatform.remote.connection.RemoteConnectionManager; import org.tizen.nativeplatform.util.PlatformUserInteraction; @@ -72,8 +72,8 @@ public class RemoteConnectionDialog extends Dialog { private int x = 0; private int y = 0; private int width = 400; - private int height = 250; - + private int height = 300; + private boolean connected = false; private final String BUNDLE_NAME = RemoteConnectionDialog.class.getPackage().getName() @@ -100,7 +100,7 @@ public class RemoteConnectionDialog extends Dialog { @Override protected void createButtonsForButtonBar(Composite parent) { super.createButtonsForButtonBar(parent); - + Button ok = getButton(IDialogConstants.OK_ID); ok.setText("Connect"); setButtonLayoutData(ok); @@ -114,11 +114,12 @@ public class RemoteConnectionDialog extends Dialog { | GridData.VERTICAL_ALIGN_BEGINNING)); composite.setLayout(new GridLayout(1, false)); + createGuideComposite(composite); createConnectionComposite(composite); createSshComposite(composite); return null; } - + private void createSshComposite(Composite parent) { Composite composite = new Composite(parent, SWT.NONE); composite.setLayout(new GridLayout(3, false)); @@ -127,7 +128,7 @@ public class RemoteConnectionDialog extends Dialog { gridData = new GridData(GridData.FILL_HORIZONTAL); gridData.minimumHeight = 0; - + Label sshPrivate = new Label(composite, SWT.NONE); sshPrivate.setText("SSH Private key:"); sshPrivateText = new Text(composite, SWT.SINGLE | SWT.BORDER | SWT.READ_ONLY); @@ -175,7 +176,20 @@ public class RemoteConnectionDialog extends Dialog { } }); } - + + private void createGuideComposite(Composite parent) { + Composite comp_descrip = new Composite(parent, SWT.NONE | SWT.BORDER); + Color whiteColor = new Color(parent.getDisplay(), 255, 255, 255); + comp_descrip.setLayout(new GridLayout(1, false)); + GridData comp_descrip_gd = new GridData(GridData.FILL_HORIZONTAL); + comp_descrip_gd.heightHint = 40; + comp_descrip.setLayoutData(comp_descrip_gd); + comp_descrip.setBackground(whiteColor); + Label label = new Label(comp_descrip, SWT.WRAP); + label.setText(resources.getString("RemoteConDlg.HeadlineGuide")); + label.setBackground(whiteColor); + } + private String getDefaultSshPrivateKey() { String home = System.getProperty("user.home"); IPath path = new Path(home).append(".ssh").append("id_rsa"); @@ -185,8 +199,7 @@ public class RemoteConnectionDialog extends Dialog { return ""; } } - - + private String getDefaultSshPublicKey() { String home = System.getProperty("user.home"); IPath path = new Path(home).append(".ssh").append("id_rsa.pub"); @@ -196,15 +209,16 @@ public class RemoteConnectionDialog extends Dialog { return ""; } } + private void createConnectionComposite(Composite parent) { Composite composite = new Composite(parent, SWT.NONE); composite.setLayout(new GridLayout(2, false)); GridData gridData = new GridData(GridData.FILL_BOTH); composite.setLayoutData(gridData); - + gridData = new GridData(GridData.FILL_HORIZONTAL); gridData.minimumHeight = 0; - + Label hostLabel = new Label(composite, SWT.NONE); hostLabel.setText("Host:"); hostText = new Text(composite, SWT.SINGLE | SWT.BORDER); @@ -219,11 +233,11 @@ public class RemoteConnectionDialog extends Dialog { passwordLabel.setText("Password:"); passText = new Text(composite, SWT.SINGLE | SWT.BORDER | SWT.PASSWORD); passText.setLayoutData(gridData); - //passText.setText(PreferencesManager.getRemoteServerPasswd()); + // passText.setText(PreferencesManager.getRemoteServerPasswd()); } public boolean connected() { - return connected; + return connected; } @Override @@ -233,41 +247,50 @@ public class RemoteConnectionDialog extends Dialog { final String pass = passText.getText(); final String privateKey = sshPrivateText.getText(); final String publicKey = sshPublicText.getText(); - - boolean isNotOk = host.isEmpty() || user.isEmpty() || pass.isEmpty() - || privateKey.isEmpty() || publicKey.isEmpty(); - + + boolean isNotOk = host.isEmpty() || user.isEmpty() || pass.isEmpty() + || privateKey.isEmpty() || publicKey.isEmpty(); + if (isNotOk) { DialogUtil.openErrorDialog("Please input all connection information."); return; } - + ProgressMonitorDialog dialog = new ProgressMonitorDialog(shell); try { dialog.run(true, true, new IRunnableWithProgress() { @Override public void run(IProgressMonitor monitor) throws InvocationTargetException, - InterruptedException { - monitor.beginTask(resources.getString("RemoteConnDlg.Progress.Creating"), 1); - try { - UserLogger.start(PlatformUserInteraction.CONNECT_SSH); - connected = RemoteConnectionManager.createConnection(host, user, pass, publicKey, privateKey, - new SubProgressMonitor(monitor, 1)); - UserLogger.end(PlatformUserInteraction.CONNECT_SSH); - if (!connected) { - DialogUtil.openMessageDialog(getShell(), "Failed to connect"); - } else { - SSHUtil.registerAuthorizedKey(publicKey); - } - } finally { - monitor.done(); - } + InterruptedException { + monitor.beginTask(resources.getString("RemoteConnDlg.Progress.Creating"), 1); + try { + UserLogger.start(PlatformUserInteraction.CONNECT_SSH); + connected = RemoteConnectionManager.createConnection(host, user, pass, + publicKey, privateKey, new SubProgressMonitor(monitor, 1)); + UserLogger.end(PlatformUserInteraction.CONNECT_SSH); + if (!connected) { + DialogUtil.openMessageDialog(getShell(), "Failed to connect"); + } else { + if (!SSHUtil.isSetAuthorizedKey(publicKey)) { + if (SSHUtil.registerAuthorizedKey(publicKey)) { + DialogUtil + .openMessageDialog(getShell(), + "Your public key registration has been sucessfully sent"); + } else { + DialogUtil.openErrorDialog(getShell(), "Error", + "Failed to register public key"); + } + } + } + } finally { + monitor.done(); + } } - }); - } catch (Exception e) { - return; - } - - super.okPressed(); + }); + } catch (Exception e) { + return; + } + + super.okPressed(); } } diff --git a/org.tizen.nativeplatform/src/org/tizen/nativeplatform/views/ui/RootstrapUIMessages.properties b/org.tizen.nativeplatform/src/org/tizen/nativeplatform/views/ui/RootstrapUIMessages.properties index b856837..7f06a6d 100644 --- a/org.tizen.nativeplatform/src/org/tizen/nativeplatform/views/ui/RootstrapUIMessages.properties +++ b/org.tizen.nativeplatform/src/org/tizen/nativeplatform/views/ui/RootstrapUIMessages.properties @@ -59,6 +59,8 @@ GenRootDlg.Error.Empty.Arch = Architecture is empty. GenRootDlg.Error.Miss.Prerequisite = "%s" package is not installed in your machine.\nTry to command \"sudo apt-get install %s\". GenRootDlg.Error.Exists.Rootstrap = "%s" rootstrap exists. +RemoteConDlg.HeadlineGuide = Private key and public key are used for connecting between\nIDE and build server, device and build server. + ChangeConfFileDlg.Title = Change (build) configuration file MofifyRootDlg.Button.ConfFile.Check = Specify project (build) configuration file MofifyRootDlg.Label.Name = Name : -- 2.7.4