VIEW: ROOTSTRAP: Shows why you insert ssh private and public key in 46/20646/1
authordonghyuk.yang <donghyuk.yang@samsung.com>
Sun, 11 May 2014 06:41:29 +0000 (15:41 +0900)
committerdonghyuk.yang <donghyuk.yang@samsung.com>
Sun, 11 May 2014 06:41:29 +0000 (15:41 +0900)
connection dialog.

Change-Id: I2bf3f78d5fb91675eab8add75f37cd7b9f0fdd39
Signed-off-by: donghyuk.yang <donghyuk.yang@samsung.com>
org.tizen.nativeplatform/src/org/tizen/nativeplatform/util/SSHUtil.java
org.tizen.nativeplatform/src/org/tizen/nativeplatform/views/ui/RemoteConnectionDialog.java
org.tizen.nativeplatform/src/org/tizen/nativeplatform/views/ui/RootstrapUIMessages.properties

index 9481550..d7631b2 100644 (file)
@@ -61,7 +61,31 @@ public class SSHUtil {
         return new Path(DEVICE_SSH_DIR).append(filename).toString();\r
     }\r
     \r
-    public static void registerAuthorizedKey(String publicKey) {\r
+    public static boolean registerAuthorizedKey(String publicKey) {\r
+        try {\r
+            FileReader fileReader = new FileReader(publicKey);\r
+            BufferedReader reader = new BufferedReader(fileReader);\r
+            String pubkey = reader.readLine();\r
+            IRemoteExecutionTools exectool = RemoteConnectionManager.getRemoteTools().getExecTool();\r
+            IRemoteFileTools filetool = RemoteConnectionManager.getRemoteTools().getFileTool();\r
+            String userhome = RemoteConnectionManager.getRemoteTools().getUserhome();\r
+            IPath remoteSshPath = new Path(userhome).append(".ssh");\r
+            String remoteAuthfile = remoteSshPath.append("authorized_keys").toString();\r
+            filetool.assureDirectory(remoteSshPath.toString(), new NullProgressMonitor());\r
+            String cmd = String.format("echo \"%s\" >> %s", pubkey, remoteAuthfile);\r
+            exectool.executeBashCommand(cmd);\r
+            if (!isSetAuthorizedKey(publicKey)) {\r
+                return false;\r
+            }\r
+        } catch (Exception e) {\r
+            e.printStackTrace();\r
+            logger.error("Failed to register authorized key", e);\r
+        }\r
+        return true;\r
+    }\r
+    \r
+    public static boolean isSetAuthorizedKey(String publicKey) {\r
+        boolean isSet = false;\r
         try {\r
             FileReader fileReader = new FileReader(publicKey);\r
             BufferedReader reader = new BufferedReader(fileReader);\r
@@ -71,22 +95,17 @@ public class SSHUtil {
             String userhome = RemoteConnectionManager.getRemoteTools().getUserhome();\r
             IPath remoteSshPath = new Path(userhome).append(".ssh");\r
             String remoteAuthfile = remoteSshPath.append("authorized_keys").toString();\r
-            boolean needSet = true;\r
             if (filetool.hasFile(remoteAuthfile, new NullProgressMonitor())) {\r
                 String cmd = String.format("grep -rn \"%s\" %s", pubkey, remoteAuthfile);\r
                 String output = exectool.executeWithOutput(cmd);\r
                 if (!output.isEmpty()) {\r
-                    needSet = false;\r
+                    isSet = true;\r
                 }\r
             }\r
-            if (needSet) {\r
-               filetool.assureDirectory(remoteSshPath.toString(), new NullProgressMonitor());\r
-                String cmd = String.format("echo \"%s\" >> %s", pubkey, remoteAuthfile);\r
-                exectool.executeBashCommand(cmd);\r
-            }\r
         } catch (Exception e) {\r
             e.printStackTrace();\r
-            logger.error("Failed to register authorized key%s]", e);\r
+            logger.error("Failed to check authorized key", e);\r
         }\r
+        return isSet;\r
     }\r
 }\r
index ad502bf..d5a9ffa 100644 (file)
@@ -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();
     }
 }
index b856837..7f06a6d 100644 (file)
@@ -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 :