Repository: camel
Updated Branches:
  refs/heads/master 0c9b6a093 -> 14418a46f


CAMEL-11738: camel-jsch - Allow to load key file from classpath


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

Branch: refs/heads/master
Commit: 14418a46fe290f26fff10c67672876e6ca035bb9
Parents: 0c9b6a0
Author: Claus Ibsen <davscl...@apache.org>
Authored: Sat Sep 2 10:21:11 2017 +0200
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Sat Sep 2 10:21:11 2017 +0200

----------------------------------------------------------------------
 .../camel-jsch/src/main/docs/scp-component.adoc |  4 +--
 .../camel/component/scp/ScpConfiguration.java   |  2 ++
 .../camel/component/scp/ScpOperations.java      | 32 ++++++++++++++++++--
 .../component/scp/ScpSimpleProduceTest.java     | 13 ++++++++
 4 files changed, 47 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/14418a46/components/camel-jsch/src/main/docs/scp-component.adoc
----------------------------------------------------------------------
diff --git a/components/camel-jsch/src/main/docs/scp-component.adoc 
b/components/camel-jsch/src/main/docs/scp-component.adoc
index e3c92a2..669841d 100644
--- a/components/camel-jsch/src/main/docs/scp-component.adoc
+++ b/components/camel-jsch/src/main/docs/scp-component.adoc
@@ -94,10 +94,10 @@ with the following path and query parameters:
 | **soTimeout** (advanced) | Sets the so timeout Used only by FTPClient | 
300000 | int
 | **synchronous** (advanced) | Sets whether synchronous processing should be 
strictly used or Camel is allowed to use asynchronous processing (if 
supported). | false | boolean
 | **timeout** (advanced) | Sets the data timeout for waiting for reply Used 
only by FTPClient | 30000 | int
-| **knownHostsFile** (security) | Sets the known_hosts file so that the jsch 
endpoint can do host key verification. |  | String
+| **knownHostsFile** (security) | Sets the known_hosts file so that the jsch 
endpoint can do host key verification. You can prefix with classpath: to load 
the file from classpath instead of file system. |  | String
 | **password** (security) | Password to use for login |  | String
 | **preferredAuthentications** (security) | Set a comma separated list of 
authentications that will be used in order of preference. Possible 
authentication methods are defined by JCraft JSCH. Some examples include: 
gssapi-with-micpublickeykeyboard-interactivepassword If not specified the JSCH 
and/or system defaults will be used. |  | String
-| **privateKeyFile** (security) | Set the private key file to that the 
endpoint can do private key verification. |  | String
+| **privateKeyFile** (security) | Set the private key file to that the 
endpoint can do private key verification. You can prefix with classpath: to 
load the file from classpath instead of file system. |  | String
 | **privateKeyFilePassphrase** (security) | Set the private key file 
passphrase to that the endpoint can do private key verification. |  | String
 | **username** (security) | Username to use for login |  | String
 | **useUserKnownHostsFile** (security) | If knownHostFile has not been 
explicit configured then use the host file from System.getProperty(user.home) 
/.ssh/known_hosts | true | boolean

http://git-wip-us.apache.org/repos/asf/camel/blob/14418a46/components/camel-jsch/src/main/java/org/apache/camel/component/scp/ScpConfiguration.java
----------------------------------------------------------------------
diff --git 
a/components/camel-jsch/src/main/java/org/apache/camel/component/scp/ScpConfiguration.java
 
b/components/camel-jsch/src/main/java/org/apache/camel/component/scp/ScpConfiguration.java
index 5caea73..2df5a3e 100644
--- 
a/components/camel-jsch/src/main/java/org/apache/camel/component/scp/ScpConfiguration.java
+++ 
b/components/camel-jsch/src/main/java/org/apache/camel/component/scp/ScpConfiguration.java
@@ -68,6 +68,7 @@ public class ScpConfiguration extends RemoteFileConfiguration 
{
 
     /**
      * Sets the known_hosts file, so that the jsch endpoint can do host key 
verification.
+     * You can prefix with classpath: to load the file from classpath instead 
of file system.
      */
     public void setKnownHostsFile(String knownHostsFile) {
         this.knownHostsFile = knownHostsFile;
@@ -90,6 +91,7 @@ public class ScpConfiguration extends RemoteFileConfiguration 
{
 
     /**
      * Set the private key file to that the endpoint can do private key 
verification.
+     * You can prefix with classpath: to load the file from classpath instead 
of file system.
      */
     public void setPrivateKeyFile(String privateKeyFile) {
         this.privateKeyFile = privateKeyFile;

http://git-wip-us.apache.org/repos/asf/camel/blob/14418a46/components/camel-jsch/src/main/java/org/apache/camel/component/scp/ScpOperations.java
----------------------------------------------------------------------
diff --git 
a/components/camel-jsch/src/main/java/org/apache/camel/component/scp/ScpOperations.java
 
b/components/camel-jsch/src/main/java/org/apache/camel/component/scp/ScpOperations.java
index 1fd64cd..3b35167 100644
--- 
a/components/camel-jsch/src/main/java/org/apache/camel/component/scp/ScpOperations.java
+++ 
b/components/camel-jsch/src/main/java/org/apache/camel/component/scp/ScpOperations.java
@@ -39,6 +39,8 @@ import 
org.apache.camel.component.file.remote.RemoteFileConfiguration;
 import org.apache.camel.component.file.remote.RemoteFileOperations;
 import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.ResolverHelper;
+import org.apache.camel.util.ResourceHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -223,7 +225,19 @@ public class ScpOperations implements 
RemoteFileOperations<ScpFile> {
             if (ObjectHelper.isNotEmpty(config.getPrivateKeyFile())) {
                 LOG.trace("Using private keyfile: {}", 
config.getPrivateKeyFile());
                 String pkfp = config.getPrivateKeyFilePassphrase();
-                jsch.addIdentity(config.getPrivateKeyFile(), 
ObjectHelper.isNotEmpty(pkfp) ? pkfp : null);
+
+                String name = config.getPrivateKeyFile();
+                // load from file system by default
+                if (!name.startsWith("classpath:")) {
+                    name = "file:" + name;
+                }
+                try {
+                    InputStream is = 
ResourceHelper.resolveMandatoryResourceAsInputStream(endpoint.getCamelContext(),
 name);
+                    byte[] data = 
endpoint.getCamelContext().getTypeConverter().mandatoryConvertTo(byte[].class, 
is);
+                    jsch.addIdentity("camel-jsch", data, null, pkfp != null ? 
pkfp.getBytes() : null);
+                } catch (Exception e) {
+                    throw new GenericFileOperationFailedException("Cannot load 
private keyfile: " + config.getPrivateKeyFile(), e);
+                }
             }
 
             String knownHostsFile = config.getKnownHostsFile();
@@ -234,7 +248,21 @@ public class ScpOperations implements 
RemoteFileOperations<ScpFile> {
                 }
                 knownHostsFile = userKnownHostFile;
             }
-            jsch.setKnownHosts(ObjectHelper.isEmpty(knownHostsFile) ? null : 
knownHostsFile);
+            // load file as input stream which can then load from classpath etc
+            if (ObjectHelper.isNotEmpty(knownHostsFile)) {
+                // load from file system by default
+                if (!knownHostsFile.startsWith("classpath:")) {
+                    knownHostsFile = "file:" + knownHostsFile;
+                }
+                try {
+                    InputStream is = 
ResourceHelper.resolveMandatoryResourceAsInputStream(endpoint.getCamelContext(),
 knownHostsFile);
+                    jsch.setKnownHosts(is);
+                } catch (Exception e) {
+                    throw new GenericFileOperationFailedException("Cannot load 
known host file: " + knownHostsFile, e);
+                }
+            } else {
+                jsch.setKnownHosts((String) null);
+            }
             session = jsch.getSession(config.getUsername(), config.getHost(), 
config.getPort());
             session.setTimeout(config.getTimeout());
             session.setUserInfo(new SessionUserInfo(config));

http://git-wip-us.apache.org/repos/asf/camel/blob/14418a46/components/camel-jsch/src/test/java/org/apache/camel/component/scp/ScpSimpleProduceTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-jsch/src/test/java/org/apache/camel/component/scp/ScpSimpleProduceTest.java
 
b/components/camel-jsch/src/test/java/org/apache/camel/component/scp/ScpSimpleProduceTest.java
index ffc3958..634d252 100644
--- 
a/components/camel-jsch/src/test/java/org/apache/camel/component/scp/ScpSimpleProduceTest.java
+++ 
b/components/camel-jsch/src/test/java/org/apache/camel/component/scp/ScpSimpleProduceTest.java
@@ -109,4 +109,17 @@ public class ScpSimpleProduceTest extends 
ScpServerTestSupport {
 
         assertMockEndpointsSatisfied();
     }
+
+    @Test
+    @Ignore("Fails on CI servers")
+    public void testScpProducePrivateKeyFromClasspath() throws Exception {
+        Assume.assumeTrue(this.isSetupComplete());
+
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+
+        String uri = getScpUri() + 
"?username=admin&privateKeyFile=classpath:camel-key.priv&privateKeyFilePassphrase=password&knownHostsFile="
 + getKnownHostsFile();
+        template.sendBodyAndHeader(uri, "Hallo Welt", Exchange.FILE_NAME, 
"welt.txt");
+
+        assertMockEndpointsSatisfied();
+    }
 }

Reply via email to