Repository: camel Updated Branches: refs/heads/master 9edcfbc1b -> eb75e9636
CAMEL-8202: camel-jsch - dont use known host file from META-INF but from user home by default. Option to turn that on/off. Also be less verbose logging by default, which can be set on the component. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/eb75e963 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/eb75e963 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/eb75e963 Branch: refs/heads/master Commit: eb75e9636e0695b64490cc037a4596c8f0d83d92 Parents: 9edcfbc Author: Claus Ibsen <davscl...@apache.org> Authored: Thu Feb 12 09:56:10 2015 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Thu Feb 12 09:56:10 2015 +0100 ---------------------------------------------------------------------- .../camel/component/scp/ScpComponent.java | 76 ++++++++++++++------ .../camel/component/scp/ScpConfiguration.java | 12 ++++ .../camel/component/scp/ScpOperations.java | 20 +++--- .../component/scp/ScpSimpleProduceTest.java | 13 ++++ 4 files changed, 92 insertions(+), 29 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/eb75e963/components/camel-jsch/src/main/java/org/apache/camel/component/scp/ScpComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-jsch/src/main/java/org/apache/camel/component/scp/ScpComponent.java b/components/camel-jsch/src/main/java/org/apache/camel/component/scp/ScpComponent.java index ff3283b..29c801e 100644 --- a/components/camel-jsch/src/main/java/org/apache/camel/component/scp/ScpComponent.java +++ b/components/camel-jsch/src/main/java/org/apache/camel/component/scp/ScpComponent.java @@ -33,15 +33,61 @@ public class ScpComponent extends RemoteFileComponent<ScpFile> { private static final Logger LOG = LoggerFactory.getLogger(ScpComponent.class); - static { + private boolean verboseLogging; + + public ScpComponent() { + } + + public ScpComponent(CamelContext context) { + super(context); + } + + @Override + protected GenericFileEndpoint<ScpFile> buildFileEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { + int query = uri.indexOf("?"); + return new ScpEndpoint(uri, this, new ScpConfiguration(new URI(query >= 0 ? uri.substring(0, query) : uri))); + } + + protected void afterPropertiesSet(GenericFileEndpoint<ScpFile> endpoint) throws Exception { + // noop + } + + @Override + protected void doStart() throws Exception { + super.doStart(); + + initJsch(); + } + + @Override + protected void doStop() throws Exception { + super.doStop(); + + // noop + } + + public boolean isVerboseLogging() { + return verboseLogging; + } + + /** + * JSCH is verbose logging out of the box. Therefore we turn the logging down to DEBUG logging by default. + * But setting this option to <tt>true</tt> turns on the verbose logging again. + */ + public void setVerboseLogging(boolean verboseLogging) { + this.verboseLogging = verboseLogging; + } + + protected void initJsch() { JSch.setConfig("StrictHostKeyChecking", "yes"); JSch.setLogger(new com.jcraft.jsch.Logger() { @Override public boolean isEnabled(int level) { return level == FATAL || level == ERROR ? LOG.isErrorEnabled() - : level == WARN ? LOG.isWarnEnabled() - : level == INFO ? LOG.isInfoEnabled() : LOG.isDebugEnabled(); + : level == WARN ? LOG.isWarnEnabled() + : level == INFO ? LOG.isInfoEnabled() : LOG.isDebugEnabled(); } + @Override public void log(int level, String message) { if (level == FATAL || level == ERROR) { @@ -49,7 +95,12 @@ public class ScpComponent extends RemoteFileComponent<ScpFile> { } else if (level == WARN) { LOG.warn("[JSCH] {}", message); } else if (level == INFO) { - LOG.info("[JSCH] {}", message); + // JSCH is verbose at INFO logging so allow to turn the noise down and log at DEBUG by default + if (isVerboseLogging()) { + LOG.info("[JSCH] {}", message); + } else { + LOG.debug("[JSCH] {}", message); + } } else { LOG.debug("[JSCH] {}", message); } @@ -57,22 +108,5 @@ public class ScpComponent extends RemoteFileComponent<ScpFile> { }); } - public ScpComponent() { - } - - public ScpComponent(CamelContext context) { - super(context); - } - - @Override - protected GenericFileEndpoint<ScpFile> buildFileEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { - int query = uri.indexOf("?"); - return new ScpEndpoint(uri, this, new ScpConfiguration(new URI(query >= 0 ? uri.substring(0, query) : uri))); - } - - protected void afterPropertiesSet(GenericFileEndpoint<ScpFile> endpoint) throws Exception { - // noop - } - } http://git-wip-us.apache.org/repos/asf/camel/blob/eb75e963/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 11ba2ac..daf452a 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 @@ -30,6 +30,10 @@ public class ScpConfiguration extends RemoteFileConfiguration { public static final int DEFAULT_SFTP_PORT = 22; public static final String DEFAULT_MOD = "664"; + @UriParam(defaultValue = "true") + private boolean useUserKnownHostsFile = true; + @UriParam(defaultValue = "true") + private boolean verboseLogging = true; @UriParam private String knownHostsFile; @UriParam @@ -72,6 +76,14 @@ public class ScpConfiguration extends RemoteFileConfiguration { this.knownHostsFile = knownHostsFile; } + public boolean isUseUserKnownHostsFile() { + return useUserKnownHostsFile; + } + + public void setUseUserKnownHostsFile(boolean useUserKnownHostsFile) { + this.useUserKnownHostsFile = useUserKnownHostsFile; + } + public String getPrivateKeyFile() { return privateKeyFile; } http://git-wip-us.apache.org/repos/asf/camel/blob/eb75e963/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 ce69c39..32afcbb 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 @@ -32,7 +32,6 @@ import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; import com.jcraft.jsch.UIKeyboardInteractive; import com.jcraft.jsch.UserInfo; - import org.apache.camel.Exchange; import org.apache.camel.InvalidPayloadException; import org.apache.camel.component.file.GenericFileEndpoint; @@ -41,7 +40,6 @@ 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.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -49,12 +47,12 @@ import org.slf4j.LoggerFactory; * SCP remote file operations */ public class ScpOperations implements RemoteFileOperations<ScpFile> { - private static final String DEFAULT_KNOWN_HOSTS = "META-INF/.ssh/known_hosts"; private static final Logger LOG = LoggerFactory.getLogger(ScpOperations.class); private ScpEndpoint endpoint; private Session session; private ChannelExec channel; + private String userKnownHostFile; @Override public void setEndpoint(GenericFileEndpoint<ScpFile> endpoint) { @@ -209,7 +207,6 @@ public class ScpOperations implements RemoteFileOperations<ScpFile> { @Override public boolean sendSiteCommand(String command) throws GenericFileOperationFailedException { - // TODO: not really used, maybe implement at a later time return true; } @@ -219,26 +216,33 @@ public class ScpOperations implements RemoteFileOperations<ScpFile> { final JSch jsch = new JSch(); // get from configuration if (ObjectHelper.isNotEmpty(config.getCiphers())) { - LOG.debug("Using ciphers: {}", config.getCiphers()); + LOG.trace("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()); + LOG.trace("Using private keyfile: {}", config.getPrivateKeyFile()); String pkfp = config.getPrivateKeyFilePassphrase(); jsch.addIdentity(config.getPrivateKeyFile(), ObjectHelper.isNotEmpty(pkfp) ? pkfp : null); } String knownHostsFile = config.getKnownHostsFile(); - jsch.setKnownHosts(ObjectHelper.isEmpty(knownHostsFile) ? DEFAULT_KNOWN_HOSTS : knownHostsFile); + if (knownHostsFile == null && config.isUseUserKnownHostsFile()) { + if (userKnownHostFile == null) { + userKnownHostFile = System.getProperty("user.home") + "/.ssh/known_hosts"; + LOG.info("Known host file not configured, using user known host file: " + userKnownHostFile); + } + knownHostsFile = userKnownHostFile; + } + jsch.setKnownHosts(ObjectHelper.isEmpty(knownHostsFile) ? null : knownHostsFile); session = jsch.getSession(config.getUsername(), config.getHost(), config.getPort()); session.setTimeout(config.getTimeout()); session.setUserInfo(new SessionUserInfo(config)); if (ObjectHelper.isNotEmpty(config.getStrictHostKeyChecking())) { - LOG.debug("Using StrickHostKeyChecking: {}", config.getStrictHostKeyChecking()); + LOG.trace("Using StrickHostKeyChecking: {}", config.getStrictHostKeyChecking()); session.setConfig("StrictHostKeyChecking", config.getStrictHostKeyChecking()); } http://git-wip-us.apache.org/repos/asf/camel/blob/eb75e963/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 bb034af..ffc3958 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 @@ -49,6 +49,19 @@ public class ScpSimpleProduceTest extends ScpServerTestSupport { } @Test + public void testScpSimpleProduceTwoTimes() throws Exception { + Assume.assumeTrue(this.isSetupComplete()); + + getMockEndpoint("mock:result").expectedBodiesReceivedInAnyOrder("Hello World", "Bye World"); + + String uri = getScpUri() + "?username=admin&password=admin&knownHostsFile=" + getKnownHostsFile(); + template.sendBodyAndHeader(uri, "Hello World", Exchange.FILE_NAME, "hello.txt"); + template.sendBodyAndHeader(uri, "Bye World", Exchange.FILE_NAME, "bye.txt"); + + assertMockEndpointsSatisfied(); + } + + @Test public void testScpSimpleSubPathProduce() throws Exception { Assume.assumeTrue(this.isSetupComplete());