You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by ff...@apache.org on 2014/06/23 11:55:26 UTC

git commit: [KARAF-3095]add a flag to bin/client so that it can read private key from a specified location

Repository: karaf
Updated Branches:
  refs/heads/karaf-2.x 994d3cce3 -> d728cc98c


[KARAF-3095]add a flag to bin/client so that it can read private key from a specified location


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/d728cc98
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/d728cc98
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/d728cc98

Branch: refs/heads/karaf-2.x
Commit: d728cc98ca06edaca677bd3f02da2e6f6bef453b
Parents: 994d3cc
Author: Freeman Fang <fr...@gmail.com>
Authored: Mon Jun 23 17:55:13 2014 +0800
Committer: Freeman Fang <fr...@gmail.com>
Committed: Mon Jun 23 17:55:13 2014 +0800

----------------------------------------------------------------------
 .../main/java/org/apache/karaf/client/Main.java | 22 ++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/d728cc98/client/src/main/java/org/apache/karaf/client/Main.java
----------------------------------------------------------------------
diff --git a/client/src/main/java/org/apache/karaf/client/Main.java b/client/src/main/java/org/apache/karaf/client/Main.java
index 9fbf85c..c3ee985 100644
--- a/client/src/main/java/org/apache/karaf/client/Main.java
+++ b/client/src/main/java/org/apache/karaf/client/Main.java
@@ -38,6 +38,7 @@ import org.apache.sshd.client.UserInteraction;
 import org.apache.sshd.client.channel.ChannelShell;
 import org.apache.sshd.client.future.ConnectFuture;
 import org.apache.sshd.common.RuntimeSshException;
+import org.apache.sshd.common.keyprovider.FileKeyPairProvider;
 import org.fusesource.jansi.AnsiConsole;
 import org.slf4j.impl.SimpleLogger;
 
@@ -61,6 +62,7 @@ public class Main {
         String user = "karaf";
         String password = null;
         StringBuilder command = new StringBuilder();
+        String keyFile = null;
 
         Properties usersCfg = loadProps(new File(System.getProperty("karaf.etc"), "users.properties"));
         if (!usersCfg.isEmpty()) {
@@ -91,6 +93,8 @@ public class Main {
                     batch = true;
                 } else if (args[i].equals("-f")) {
                     file = args[++i];
+                } else if (args[i].equals("-k")) {
+                    keyFile = args[++i];
                 } else if (args[i].equals("--help")) {
                     System.out.println("Apache Karaf client");
                     System.out.println("  -a [port]     specify the port to connect to");
@@ -104,6 +108,7 @@ public class Main {
                     System.out.println("  -d [delay]    intra-retry delay (defaults to 2 seconds)");
                     System.out.println("  -b            batch mode, specify multiple commands via standard input");
                     System.out.println("  -f [file]    read commands from the specified file");
+                    System.out.println("  -k [keyFile]    specify the private keyFile location when using key login, need have BouncyCastle registered as security provider using this flag");
                     System.out.println("  [commands]    commands to run");
                     System.out.println("If no commands are specified, the client will be put in an interactive mode");
                     System.exit(0);
@@ -146,7 +151,7 @@ public class Main {
 
             final Console console = System.console();
             client = SshClient.setUpDefaultClient();
-            setupAgent(user, client);
+            setupAgent(user, client, keyFile);
             client.setUserInteraction(new UserInteraction() {
                 public void welcome(String banner) {
                     System.out.println(banner);
@@ -267,15 +272,15 @@ public class Main {
         return props;
     }
 
-    private static void setupAgent(String user, SshClient client) {
+    private static void setupAgent(String user, SshClient client, String keyFile) {
         SshAgent agent;
         URL builtInPrivateKey = Main.class.getClassLoader().getResource("karaf.key");
-        agent = startAgent(user, builtInPrivateKey);
+        agent = startAgent(user, builtInPrivateKey, keyFile);
         client.setAgentFactory(new LocalAgentFactory(agent));
         client.getProperties().put(SshAgent.SSH_AUTHSOCKET_ENV_NAME, "local");
     }
 
-    private static SshAgent startAgent(String user, URL privateKeyUrl) {
+    private static SshAgent startAgent(String user, URL privateKeyUrl, String keyFile) {
         InputStream is = null;
         try {
             SshAgent agent = new AgentImpl();
@@ -284,6 +289,15 @@ public class Main {
             KeyPair keyPair = (KeyPair) r.readObject();
             is.close();
             agent.addIdentity(keyPair, user);
+            
+            if (keyFile != null) {
+                String[] keyFiles = new String[]{keyFile};
+                FileKeyPairProvider fileKeyPairProvider = new FileKeyPairProvider(keyFiles);
+                for (KeyPair key : fileKeyPairProvider.loadKeys()) {
+                    agent.addIdentity(key, user);                
+                }
+            }
+            
             return agent;
         } catch (Throwable e) {
             close(is);