You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2017/11/30 09:23:58 UTC

[camel] 01/02: CAMEL-12051 - Camel-Jsch: Allow to pass the privateKey as byte[] and not only via file

This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit e7eab52012fbe67715a7aeebbf02eac824ba5384
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Thu Nov 30 09:56:55 2017 +0100

    CAMEL-12051 - Camel-Jsch: Allow to pass the privateKey as byte[] and not only via file
---
 .../camel/component/scp/ScpConfiguration.java      | 14 ++++++++++++
 .../apache/camel/component/scp/ScpOperations.java  | 13 +++++++++++
 .../camel/component/scp/ScpSimpleProduceTest.java  | 25 ++++++++++++++++++++++
 3 files changed, 52 insertions(+)

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 2df5a3e..762b746 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
@@ -37,6 +37,8 @@ public class ScpConfiguration extends RemoteFileConfiguration {
     @UriParam(label = "security", secret = true)
     private String privateKeyFile;
     @UriParam(label = "security", secret = true)
+    private byte[] privateKeyBytes;
+    @UriParam(label = "security", secret = true)
     private String privateKeyFilePassphrase;
     @UriParam(enums = "no,yes", defaultValue = "no")
     private String strictHostKeyChecking;
@@ -95,6 +97,18 @@ public class ScpConfiguration extends RemoteFileConfiguration {
      */
     public void setPrivateKeyFile(String privateKeyFile) {
         this.privateKeyFile = privateKeyFile;
+    }    
+
+    public byte[] getPrivateKeyBytes() {
+        return privateKeyBytes;
+    }
+
+    /**
+     * Set the private key bytes to that the endpoint can do private key verification.
+     * This must be used only if privateKeyFile wasn't set. Otherwise the file will have the priority.
+     */
+    public void setPrivateKeyBytes(byte[] privateKeyBytes) {
+        this.privateKeyBytes = privateKeyBytes;
     }
 
     public String getPrivateKeyFilePassphrase() {
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 3b35167..efd1eca 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
@@ -238,8 +238,21 @@ public class ScpOperations implements RemoteFileOperations<ScpFile> {
                 } catch (Exception e) {
                     throw new GenericFileOperationFailedException("Cannot load private keyfile: " + config.getPrivateKeyFile(), e);
                 }
+            } else if (ObjectHelper.isNotEmpty(config.getPrivateKeyBytes())) {
+                LOG.trace("Using private key bytes: {}", config.getPrivateKeyBytes());
+
+                String pkfp = config.getPrivateKeyFilePassphrase();
+
+                byte[] data = config.getPrivateKeyBytes();
+                
+                try {
+                    jsch.addIdentity("camel-jsch", data, null, pkfp != null ? pkfp.getBytes() : null);
+                } catch (Exception e) {
+                    throw new GenericFileOperationFailedException("Cannot load private key bytes: " + config.getPrivateKeyBytes(), e);
+                }                
             }
 
+
             String knownHostsFile = config.getKnownHostsFile();
             if (knownHostsFile == null && config.isUseUserKnownHostsFile()) {
                 if (userKnownHostFile == null) {
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 634d252..0fe2f17 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
@@ -16,8 +16,12 @@
  */
 package org.apache.camel.component.scp;
 
+import java.io.File;
+import java.nio.file.Files;
+
 import org.apache.camel.Exchange;
 import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.JndiRegistry;
 import org.junit.Assume;
 import org.junit.Ignore;
 import org.junit.Test;
@@ -122,4 +126,25 @@ public class ScpSimpleProduceTest extends ScpServerTestSupport {
 
         assertMockEndpointsSatisfied();
     }
+  
+    @Test
+    @Ignore("Fails on CI servers")
+    public void testScpProducePrivateKeyByte() throws Exception {
+        Assume.assumeTrue(this.isSetupComplete());
+
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+
+        String uri = getScpUri() + "?username=admin&privateKeyBytes=#privKey&privateKeyFilePassphrase=password&knownHostsFile=" + getKnownHostsFile();
+        template.sendBodyAndHeader(uri, "Hallo Welt", Exchange.FILE_NAME, "welt.txt");
+
+        assertMockEndpointsSatisfied();
+    }
+    
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry registry = super.createRegistry();
+        byte[] privKey = Files.readAllBytes(new File("src/test/resources/camel-key.priv").toPath());
+        registry.bind("privKey", privKey);
+        return registry;
+    }
 }

-- 
To stop receiving notification emails like this one, please contact
"commits@camel.apache.org" <co...@camel.apache.org>.