CAMEL-10403: Camel-Nats: Add TLS Support
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a6006218 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a6006218 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a6006218 Branch: refs/heads/master Commit: a60062181ca70843c17cdd5cf6b1a1dee3f2b465 Parents: 36c9839 Author: Andrea Cosentino <anco...@gmail.com> Authored: Thu Oct 20 14:19:49 2016 +0200 Committer: Andrea Cosentino <anco...@gmail.com> Committed: Thu Oct 20 14:23:16 2016 +0200 ---------------------------------------------------------------------- .../src/main/docs/nats-component.adoc | 5 ++- .../camel/component/nats/NatsConfiguration.java | 42 +++++++++++++++++++- .../camel/component/nats/NatsConsumer.java | 12 +++++- .../camel/component/nats/NatsProducer.java | 12 +++++- 4 files changed, 67 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/a6006218/components/camel-nats/src/main/docs/nats-component.adoc ---------------------------------------------------------------------- diff --git a/components/camel-nats/src/main/docs/nats-component.adoc b/components/camel-nats/src/main/docs/nats-component.adoc index 6dc30da..daad731 100644 --- a/components/camel-nats/src/main/docs/nats-component.adoc +++ b/components/camel-nats/src/main/docs/nats-component.adoc @@ -44,7 +44,7 @@ The Nats component has no options. // endpoint options: START -The Nats component supports 18 endpoint options which are listed below: +The Nats component supports 21 endpoint options which are listed below: {% raw %} [width="100%",cols="2,1,1m,1m,5",options="header"] @@ -68,6 +68,9 @@ The Nats component supports 18 endpoint options which are listed below: | exchangePattern | consumer (advanced) | | ExchangePattern | Sets the exchange pattern when the consumer creates an exchange. | replySubject | producer | | String | the subject to which subscribers should send response | synchronous | advanced | false | boolean | Sets whether synchronous processing should be strictly used or Camel is allowed to use asynchronous processing (if supported). +| secure | security | false | boolean | set set secure option indicating TLS is required +| sslContextParameters | security | | SSLContextParameters | To configure security using SSLContextParameters +| tlsDebug | security | false | boolean | TLS Debug it will add additional console output |======================================================================= {% endraw %} // endpoint options: END http://git-wip-us.apache.org/repos/asf/camel/blob/a6006218/components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsConfiguration.java ---------------------------------------------------------------------- diff --git a/components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsConfiguration.java b/components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsConfiguration.java index f80e307..26e0921 100644 --- a/components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsConfiguration.java +++ b/components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsConfiguration.java @@ -22,6 +22,7 @@ import org.apache.camel.spi.Metadata; import org.apache.camel.spi.UriParam; import org.apache.camel.spi.UriParams; import org.apache.camel.spi.UriPath; +import org.apache.camel.util.jsse.SSLContextParameters; @UriParams public class NatsConfiguration { @@ -56,6 +57,12 @@ public class NatsConfiguration { private String maxMessages; @UriParam(label = "consumer", defaultValue = "10") private int poolSize = 10; + @UriParam(label = "security") + private boolean secure; + @UriParam(label = "security") + private boolean tlsDebug; + @UriParam(label = "security", description = "SSL configuration") + private SSLContextParameters sslContextParameters; /** * URLs to one or more NAT servers. Use comma to separate URLs when specifying multiple servers. @@ -212,7 +219,40 @@ public class NatsConfiguration { this.poolSize = poolSize; } - private static <T> void addPropertyIfNotNull(Properties props, String key, T value) { + /** + * set set secure option indicating TLS is required + */ + public boolean isSecure() { + return secure; + } + + public void setSecure(boolean secure) { + this.secure = secure; + } + + /** + * TLS Debug, it will add additional console output + */ + public boolean isTlsDebug() { + return tlsDebug; + } + + public void setTlsDebug(boolean tlsDebug) { + this.tlsDebug = tlsDebug; + } + + /** + * To configure security using SSLContextParameters + */ + public SSLContextParameters getSslContextParameters() { + return sslContextParameters; + } + + public void setSslContextParameters(SSLContextParameters sslContextParameters) { + this.sslContextParameters = sslContextParameters; + } + + private static <T> void addPropertyIfNotNull(Properties props, String key, T value) { if (value != null) { props.put(key, value); } http://git-wip-us.apache.org/repos/asf/camel/blob/a6006218/components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsConsumer.java ---------------------------------------------------------------------- diff --git a/components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsConsumer.java b/components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsConsumer.java index 8fc2eff..e1c46df 100644 --- a/components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsConsumer.java +++ b/components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsConsumer.java @@ -17,10 +17,13 @@ package org.apache.camel.component.nats; import java.io.IOException; +import java.security.GeneralSecurityException; import java.util.Properties; import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeoutException; +import javax.net.ssl.SSLContext; + import io.nats.client.Connection; import io.nats.client.ConnectionFactory; import io.nats.client.Message; @@ -95,9 +98,16 @@ public class NatsConsumer extends DefaultConsumer { } } - private Connection getConnection() throws IOException, InterruptedException, TimeoutException { + private Connection getConnection() throws IOException, InterruptedException, TimeoutException, GeneralSecurityException { Properties prop = getEndpoint().getNatsConfiguration().createProperties(); ConnectionFactory factory = new ConnectionFactory(prop); + if (getEndpoint().getNatsConfiguration().getSslContextParameters() != null && getEndpoint().getNatsConfiguration().isSecure()) { + SSLContext sslCtx = getEndpoint().getNatsConfiguration().getSslContextParameters().createSSLContext(getEndpoint().getCamelContext()); + factory.setSSLContext(sslCtx); + if (getEndpoint().getNatsConfiguration().isTlsDebug()) { + factory.setTlsDebug(getEndpoint().getNatsConfiguration().isTlsDebug()); + } + } connection = factory.createConnection(); return connection; } http://git-wip-us.apache.org/repos/asf/camel/blob/a6006218/components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsProducer.java b/components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsProducer.java index f34e8c0..0ee1236 100644 --- a/components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsProducer.java +++ b/components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsProducer.java @@ -17,9 +17,12 @@ package org.apache.camel.component.nats; import java.io.IOException; +import java.security.GeneralSecurityException; import java.util.Properties; import java.util.concurrent.TimeoutException; +import javax.net.ssl.SSLContext; + import io.nats.client.Connection; import io.nats.client.ConnectionFactory; @@ -79,9 +82,16 @@ public class NatsProducer extends DefaultProducer { } } - private Connection getConnection() throws TimeoutException, IOException { + private Connection getConnection() throws TimeoutException, IOException, GeneralSecurityException { Properties prop = getEndpoint().getNatsConfiguration().createProperties(); ConnectionFactory factory = new ConnectionFactory(prop); + if (getEndpoint().getNatsConfiguration().getSslContextParameters() != null && getEndpoint().getNatsConfiguration().isSecure()) { + SSLContext sslCtx = getEndpoint().getNatsConfiguration().getSslContextParameters().createSSLContext(getEndpoint().getCamelContext()); + factory.setSSLContext(sslCtx); + if (getEndpoint().getNatsConfiguration().isTlsDebug()) { + factory.setTlsDebug(getEndpoint().getNatsConfiguration().isTlsDebug()); + } + } connection = factory.createConnection(); return connection; }