Repository: camel Updated Branches: refs/heads/master b3f0c7407 -> 4f01b7861
CAMEL-11152 - naming fix with the impl Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/4f01b786 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/4f01b786 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/4f01b786 Branch: refs/heads/master Commit: 4f01b7861e3cced634926dc4baa3377c9fa32a47 Parents: b3f0c74 Author: onders86 <ondersez...@gmail.com> Authored: Tue Apr 18 09:06:39 2017 +0300 Committer: Andrea Cosentino <anco...@gmail.com> Committed: Tue Apr 18 13:21:20 2017 +0200 ---------------------------------------------------------------------- .../camel-ssh/src/main/docs/ssh-component.adoc | 6 ++++- .../camel/component/ssh/SshConstants.java | 24 ++++++++++++++++++ .../apache/camel/component/ssh/SshConsumer.java | 5 ++-- .../apache/camel/component/ssh/SshHelper.java | 20 ++++++++++++--- .../apache/camel/component/ssh/SshProducer.java | 6 ++++- .../component/ssh/SshComponentProducerTest.java | 26 ++++++++++++++++++++ 6 files changed, 80 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/4f01b786/components/camel-ssh/src/main/docs/ssh-component.adoc ---------------------------------------------------------------------- diff --git a/components/camel-ssh/src/main/docs/ssh-component.adoc b/components/camel-ssh/src/main/docs/ssh-component.adoc index 3db3d0e..d38b469 100644 --- a/components/camel-ssh/src/main/docs/ssh-component.adoc +++ b/components/camel-ssh/src/main/docs/ssh-component.adoc @@ -141,7 +141,11 @@ and use that for authentication. `keyPairProvider` has been set, and if so, it will use that to for certificate based authentication. 3. If neither `certResource` nor `keyPairProvider` are set, it will use -the `username` and `password` options for authentication. +the `username` and `password` options for authentication. Even though the `username` +and `password` are provided in the endpoint configuration and headers set with +`SshConstants.USERNAME_HEADER` (`CamelSshUsername`) and +`SshConstants.PASSWORD_HEADER` (`CamelSshPassword`), the endpoint +configuration is surpassed and credentials set in the headers are used. The following route fragment shows an SSH polling consumer using a certificate from the classpath. http://git-wip-us.apache.org/repos/asf/camel/blob/4f01b786/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshConstants.java ---------------------------------------------------------------------- diff --git a/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshConstants.java b/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshConstants.java new file mode 100644 index 0000000..f3f2527 --- /dev/null +++ b/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshConstants.java @@ -0,0 +1,24 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.ssh; + +public interface SshConstants { + + String USERNAME_HEADER = "CamelSshUsername"; + String PASSWORD_HEADER = "CamelSshPassword"; + +} http://git-wip-us.apache.org/repos/asf/camel/blob/4f01b786/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshConsumer.java ---------------------------------------------------------------------- diff --git a/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshConsumer.java b/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshConsumer.java index da12bd2..3e07bb9 100644 --- a/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshConsumer.java +++ b/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshConsumer.java @@ -56,9 +56,10 @@ public class SshConsumer extends ScheduledPollConsumer { } String command = endpoint.getPollCommand(); - SshResult result = SshHelper.sendExecCommand(command, endpoint, client); - Exchange exchange = endpoint.createExchange(); + + SshResult result = SshHelper.sendExecCommand(exchange.getIn().getHeaders(), command, endpoint, client); + exchange.getIn().setBody(result.getStdout()); exchange.getIn().setHeader(SshResult.EXIT_VALUE, result.getExitValue()); exchange.getIn().setHeader(SshResult.STDERR, result.getStderr()); http://git-wip-us.apache.org/repos/asf/camel/blob/4f01b786/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshHelper.java ---------------------------------------------------------------------- diff --git a/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshHelper.java b/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshHelper.java index 6c9ea02..972bb8a 100644 --- a/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshHelper.java +++ b/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshHelper.java @@ -19,6 +19,7 @@ package org.apache.camel.component.ssh; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.security.KeyPair; +import java.util.Map; import org.apache.camel.RuntimeCamelException; import org.apache.sshd.ClientChannel; @@ -38,7 +39,7 @@ public final class SshHelper { private SshHelper() { } - public static SshResult sendExecCommand(String command, SshEndpoint endpoint, SshClient client) throws Exception { + public static SshResult sendExecCommand(Map<String, Object> headers, String command, SshEndpoint endpoint, SshClient client) throws Exception { SshResult result = null; SshConfiguration configuration = endpoint.getConfiguration(); @@ -81,8 +82,21 @@ public final class SshHelper { KeyPair pair = keyPairProvider.loadKey(configuration.getKeyType()); authResult = session.authPublicKey(configuration.getUsername(), pair); } else { - LOG.debug("Attempting to authenticate username '{}' using Password...", configuration.getUsername()); - authResult = session.authPassword(configuration.getUsername(), configuration.getPassword()); + String userName = configuration.getUsername(); + String password = configuration.getPassword(); + + Object userNameHeaderObj = headers.get(SshConstants.USERNAME_HEADER); + if (userNameHeaderObj != null && userNameHeaderObj instanceof String) { + userName = (String) headers.get(SshConstants.USERNAME_HEADER); + } + + Object passwordHeaderObj = headers.get(SshConstants.PASSWORD_HEADER); + if (passwordHeaderObj != null && passwordHeaderObj instanceof String) { + password = (String) headers.get(SshConstants.PASSWORD_HEADER); + } + + LOG.debug("Attempting to authenticate username '{}' using Password...", userName); + authResult = session.authPassword(userName, password); } authResult.await(configuration.getTimeout()); http://git-wip-us.apache.org/repos/asf/camel/blob/4f01b786/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshProducer.java b/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshProducer.java index 7c4b663..9a731cf 100644 --- a/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshProducer.java +++ b/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshProducer.java @@ -16,6 +16,8 @@ */ package org.apache.camel.component.ssh; +import java.util.Map; + import org.apache.camel.CamelExchangeException; import org.apache.camel.Exchange; import org.apache.camel.Message; @@ -54,9 +56,11 @@ public class SshProducer extends DefaultProducer { public void process(Exchange exchange) throws Exception { final Message in = exchange.getIn(); String command = in.getMandatoryBody(String.class); + + final Map<String, Object> headers = exchange.getIn().getHeaders(); try { - SshResult result = SshHelper.sendExecCommand(command, endpoint, client); + SshResult result = SshHelper.sendExecCommand(headers, command, endpoint, client); exchange.getOut().setBody(result.getStdout()); exchange.getOut().setHeader(SshResult.EXIT_VALUE, result.getExitValue()); exchange.getOut().setHeader(SshResult.STDERR, result.getStderr()); http://git-wip-us.apache.org/repos/asf/camel/blob/4f01b786/components/camel-ssh/src/test/java/org/apache/camel/component/ssh/SshComponentProducerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-ssh/src/test/java/org/apache/camel/component/ssh/SshComponentProducerTest.java b/components/camel-ssh/src/test/java/org/apache/camel/component/ssh/SshComponentProducerTest.java index c34f565..b781e33 100644 --- a/components/camel-ssh/src/test/java/org/apache/camel/component/ssh/SshComponentProducerTest.java +++ b/components/camel-ssh/src/test/java/org/apache/camel/component/ssh/SshComponentProducerTest.java @@ -16,6 +16,9 @@ */ package org.apache.camel.component.ssh; +import java.util.HashMap; +import java.util.Map; + import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; @@ -79,6 +82,25 @@ public class SshComponentProducerTest extends SshComponentTestSupport { assertMockEndpointsSatisfied(); } + + @Test + public void testCredentialsAsHeaders() throws Exception { + final String msg = "test\n"; + + MockEndpoint mock = getMockEndpoint("mock:password"); + mock.expectedMinimumMessageCount(1); + mock.expectedBodiesReceived(msg); + mock.expectedHeaderReceived(SshResult.EXIT_VALUE, 0); + mock.expectedHeaderReceived(SshResult.STDERR, "Error:test\n"); + + Map<String, Object> headers = new HashMap<String, Object>(); + headers.put(SshConstants.USERNAME_HEADER, "smx"); + headers.put(SshConstants.PASSWORD_HEADER, "smx"); + + template.sendBodyAndHeaders("direct:sshCredentialsWithHeaders", msg, headers); + + assertMockEndpointsSatisfied(); + } @Override protected RouteBuilder createRouteBuilder() throws Exception { @@ -92,6 +114,10 @@ public class SshComponentProducerTest extends SshComponentTestSupport { from("direct:ssh") .to("ssh://smx:smx@localhost:" + port + "?timeout=3000") .to("mock:password"); + + from("direct:sshCredentialsWithHeaders") + .to("ssh://localhost:" + port + "?timeout=3000") + .to("mock:password"); } }; }