Author: hadrian
Date: Fri Jun 15 02:29:59 2012
New Revision: 1350451

URL: http://svn.apache.org/viewvc?rev=1350451&view=rev
Log:
CAMEL-5242. Add support for pki based auth in camel-jsch

Added:
    
camel/branches/camel-2.9.x/components/camel-jsch/src/test/resources/camel-key.priv
    
camel/branches/camel-2.9.x/components/camel-jsch/src/test/resources/camel-key.pub
Modified:
    
camel/branches/camel-2.9.x/components/camel-jsch/src/main/java/org/apache/camel/component/jsch/ScpOperations.java
    
camel/branches/camel-2.9.x/components/camel-jsch/src/test/java/org/apache/camel/component/jsch/ScpServerTestSupport.java
    
camel/branches/camel-2.9.x/components/camel-jsch/src/test/java/org/apache/camel/component/jsch/ScpSimpleProduceTest.java

Modified: 
camel/branches/camel-2.9.x/components/camel-jsch/src/main/java/org/apache/camel/component/jsch/ScpOperations.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/components/camel-jsch/src/main/java/org/apache/camel/component/jsch/ScpOperations.java?rev=1350451&r1=1350450&r2=1350451&view=diff
==============================================================================
--- 
camel/branches/camel-2.9.x/components/camel-jsch/src/main/java/org/apache/camel/component/jsch/ScpOperations.java
 (original)
+++ 
camel/branches/camel-2.9.x/components/camel-jsch/src/main/java/org/apache/camel/component/jsch/ScpOperations.java
 Fri Jun 15 02:29:59 2012
@@ -32,8 +32,9 @@ import com.jcraft.jsch.JSchException;
 import com.jcraft.jsch.Session;
 import com.jcraft.jsch.UIKeyboardInteractive;
 import com.jcraft.jsch.UserInfo;
+
+import org.apache.camel.CamelExchangeException;
 import org.apache.camel.Exchange;
-import org.apache.camel.InvalidPayloadException;
 import org.apache.camel.component.file.GenericFileEndpoint;
 import org.apache.camel.component.file.GenericFileOperationFailedException;
 import org.apache.camel.component.file.remote.RemoteFileConfiguration;
@@ -44,8 +45,6 @@ import org.apache.camel.util.ObjectHelpe
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import static org.apache.camel.util.ObjectHelper.isNotEmpty;
-
 /**
  * SCP remote file operations
  */
@@ -107,7 +106,7 @@ public class ScpOperations implements Re
 
             try {
                 write(channel, file, 
ExchangeHelper.getMandatoryInBody(exchange, InputStream.class), cfg);
-            } catch (InvalidPayloadException e) {
+            } catch (CamelExchangeException e) {
                 throw new GenericFileOperationFailedException("Failed extract 
message body as InputStream", e);
             } catch (IOException e) {
                 throw new GenericFileOperationFailedException("Failed to write 
file " + file, e);
@@ -194,13 +193,21 @@ public class ScpOperations implements Re
         try {
             final JSch jsch = new JSch();
             // get from configuration
-            if (isNotEmpty(config.getCiphers())) {
+            if (ObjectHelper.isNotEmpty(config.getCiphers())) {
                 LOG.debug("Using ciphers: {}", config.getCiphers());
                 Hashtable<String, String> ciphers = new Hashtable<String, 
String>();
                 ciphers.put("cipher.s2c", config.getCiphers());
                 ciphers.put("cipher.c2s", config.getCiphers());
                 JSch.setConfig(ciphers);
             }
+            if (ObjectHelper.isNotEmpty(config.getPrivateKeyFile())) {
+                LOG.debug("Using private keyfile: {}", 
config.getPrivateKeyFile());
+                if 
(ObjectHelper.isNotEmpty(config.getPrivateKeyFilePassphrase())) {
+                    jsch.addIdentity(config.getPrivateKeyFile(), 
config.getPrivateKeyFilePassphrase());
+                } else {
+                    jsch.addIdentity(config.getPrivateKeyFile());
+                }
+            }
 
             String knownHostsFile = config.getKnownHostsFile();
             jsch.setKnownHosts(ObjectHelper.isEmpty(knownHostsFile) ? 
DEFAULT_KNOWN_HOSTS : knownHostsFile);

Modified: 
camel/branches/camel-2.9.x/components/camel-jsch/src/test/java/org/apache/camel/component/jsch/ScpServerTestSupport.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/components/camel-jsch/src/test/java/org/apache/camel/component/jsch/ScpServerTestSupport.java?rev=1350451&r1=1350450&r2=1350451&view=diff
==============================================================================
--- 
camel/branches/camel-2.9.x/components/camel-jsch/src/test/java/org/apache/camel/component/jsch/ScpServerTestSupport.java
 (original)
+++ 
camel/branches/camel-2.9.x/components/camel-jsch/src/test/java/org/apache/camel/component/jsch/ScpServerTestSupport.java
 Fri Jun 15 02:29:59 2012
@@ -20,7 +20,9 @@ import java.io.File;
 import java.io.IOException;
 import java.security.Provider;
 import java.security.Provider.Service;
+import java.security.PublicKey;
 import java.security.Security;
+import java.util.Arrays;
 
 import com.jcraft.jsch.JSch;
 import com.jcraft.jsch.JSchException;
@@ -31,10 +33,14 @@ import org.apache.camel.test.AvailablePo
 import org.apache.camel.test.junit4.CamelTestSupport;
 import org.apache.camel.util.FileUtil;
 import org.apache.sshd.SshServer;
+import org.apache.sshd.common.NamedFactory;
 import org.apache.sshd.common.keyprovider.FileKeyPairProvider;
+import org.apache.sshd.server.Command;
 import org.apache.sshd.server.PasswordAuthenticator;
+import org.apache.sshd.server.PublickeyAuthenticator;
 import org.apache.sshd.server.command.ScpCommandFactory;
 import org.apache.sshd.server.session.ServerSession;
+import org.apache.sshd.server.sftp.SftpSubsystem;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.BeforeClass;
@@ -116,10 +122,12 @@ public abstract class ScpServerTestSuppo
     }
 
 
+    @SuppressWarnings("unchecked")
     protected boolean startSshd() {
         sshd = SshServer.setUpDefaultServer();
         sshd.setPort(getPort());
         sshd.setKeyPairProvider(new FileKeyPairProvider(new 
String[]{"src/test/resources/hostkey.pem"}));
+        sshd.setSubsystemFactories(Arrays.<NamedFactory<Command>>asList(new 
SftpSubsystem.Factory()));
         sshd.setCommandFactory(new ScpCommandFactory());
         sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
             @Override
@@ -128,7 +136,12 @@ public abstract class ScpServerTestSuppo
                 return username != null && username.equals(password);
             }
         });
-        
+        sshd.setPublickeyAuthenticator(new PublickeyAuthenticator() {
+            @Override
+            public boolean authenticate(String username, PublicKey key, 
ServerSession session) {
+                return true;
+            }
+        });
         try {
             sshd.start();
             return true;

Modified: 
camel/branches/camel-2.9.x/components/camel-jsch/src/test/java/org/apache/camel/component/jsch/ScpSimpleProduceTest.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/components/camel-jsch/src/test/java/org/apache/camel/component/jsch/ScpSimpleProduceTest.java?rev=1350451&r1=1350450&r2=1350451&view=diff
==============================================================================
--- 
camel/branches/camel-2.9.x/components/camel-jsch/src/test/java/org/apache/camel/component/jsch/ScpSimpleProduceTest.java
 (original)
+++ 
camel/branches/camel-2.9.x/components/camel-jsch/src/test/java/org/apache/camel/component/jsch/ScpSimpleProduceTest.java
 Fri Jun 15 02:29:59 2012
@@ -81,4 +81,18 @@ public class ScpSimpleProduceTest extend
         // assertFalse("File should not have execute rights: " + file, 
file.canExecute());
         assertEquals("Bonjour Monde", 
context.getTypeConverter().convertTo(String.class, file));
     }
+
+    @Test
+    public void testScpProducePrivateKey() throws Exception {
+        Assume.assumeTrue(this.isSetupComplete());
+
+        String uri = getScpUri() + 
"?username=admin&privateKeyFile=src/test/resources/camel-key.priv&privateKeyFilePassphrase=password&knownHostsFile="
 + getKnownHostsFile();
+        template.sendBodyAndHeader(uri, "Hallo Welt", Exchange.FILE_NAME, 
"welt.txt");
+
+        File file = new File(getScpPath() + "/welt.txt").getAbsoluteFile();
+        assertTrue("File should exist: " + file, file.exists());
+        // Mina sshd we use for testing ignores file perms;
+        // assertFalse("File should not have execute rights: " + file, 
file.canExecute());
+        assertEquals("Hallo Welt", 
context.getTypeConverter().convertTo(String.class, file));
+    }
 }

Added: 
camel/branches/camel-2.9.x/components/camel-jsch/src/test/resources/camel-key.priv
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/components/camel-jsch/src/test/resources/camel-key.priv?rev=1350451&view=auto
==============================================================================
--- 
camel/branches/camel-2.9.x/components/camel-jsch/src/test/resources/camel-key.priv
 (added)
+++ 
camel/branches/camel-2.9.x/components/camel-jsch/src/test/resources/camel-key.priv
 Fri Jun 15 02:29:59 2012
@@ -0,0 +1,18 @@
+-----BEGIN RSA PRIVATE KEY-----
+Proc-Type: 4,ENCRYPTED
+DEK-Info: AES-128-CBC,FE79F4F193EC9E78B2839E067ACAC37A
+
++ruECi0mWj5y45zqJl3ombI2cLax6nhXKdYvVXgnlbAP7/l4q97870taCFun50n2
+XseA08mJohChSJ/+pcwABhpZspnsCMoorSg+MdQh1dUzz1kuLpWTz93rrobLU+V7
+mIkAsdBh0Blxv/O2oQ8F/2OVOrhsMUI3srMzQiBOFg5Cx4bPqdorvz8x8WT7LjOX
+bRq3qwghbMDPyylE4uhp4WBkRSCDifUYtJ8aLTTSE4BJWbc+o+d9W6f0rL3hRkXt
+adPP8cGaqKTRdvYsI63JpVu9ibzN3aAXA+hNWtVCk6enWyOBEgaovFQSbNhxhNJV
+PNZ1C6CIEKocs6X8jpZWtYoULDU6B7d8USzUh2wX+iUdxhi3Bt7wiOQKoLXdwd8B
+F0XCwyBMF7AruAStRl5nYP11QYGIMFyxAvAPK1oBB846QZumVjTjuM8CyL0BCptk
+WAqT5iobU2JkXHu/Oa6HU1bKJmTkJEs7lh2uBjgOlht2OwjD4kQJ4sPo8cP+6QmH
+foE424o6NmGUXPsZ4JOBKKWvZBu9WyAphvBNtb+yh5+gc+C7mgZHSRwdt2frV5pX
+kwJg3VoojH/MEVeRWHo9wPxj0Hd1n6Cg/p1/pHkhut4vFpUKYlXNPN1UeKF1EZpR
+cQnBT4NJjWDdpE9Yr+7nu+3f5N0pfUrNSGWFU6XzhQMw4778rR9TBgO0Og6Hbftz
+ZUDV5XJJ3PDAgCY7s/PI1chjxUKF15KxBYlQOqXaY9JgYzlkH9aC43NywO66wtC9
+bv6HP48zX554+NStWHXuY4dJ02miynAjZ2zceXCyjEOTVbjAzI3rpdHW+9X7bcKf
+-----END RSA PRIVATE KEY-----

Added: 
camel/branches/camel-2.9.x/components/camel-jsch/src/test/resources/camel-key.pub
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/components/camel-jsch/src/test/resources/camel-key.pub?rev=1350451&view=auto
==============================================================================
--- 
camel/branches/camel-2.9.x/components/camel-jsch/src/test/resources/camel-key.pub
 (added)
+++ 
camel/branches/camel-2.9.x/components/camel-jsch/src/test/resources/camel-key.pub
 Fri Jun 15 02:29:59 2012
@@ -0,0 +1 @@
+ssh-rsa 
AAAAB3NzaC1yc2EAAAADAQABAAAAgQDA62XKzYxxiNEsD3C2IaL89FLbL5/BUqni1VGK6OvuNWgc1jPYTuzhhqhZHMXkcWawi1odBV4gjZZo0cK3cLni8aBs2yqO9hD8ic1O6KAju7l3JHW6B/IIlx9UcwPXkSXfi9IXsW3Nznl/k2Q8F9OrTOqGNTkNTgwxzFr5DC4RWw==
 camel-t...@camel.apache.org


Reply via email to