Skip to content

Commit

Permalink
Merge pull request #201 from xjdr/ck/TACO-109-handle-plaintext-clients
Browse files Browse the repository at this point in the history
handle plaintext clients
  • Loading branch information
pdex authored Apr 9, 2018
2 parents fde21cc + 9d49a57 commit 0d36da5
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.handler.codec.http.HttpServerCodec;
import java.util.function.Supplier;
import lombok.val;

Expand All @@ -30,26 +31,36 @@ private ChannelHandler buildHttp2Handler() {
@Override
protected void initChannel(Channel channel) throws Exception {
if (state.sslContext != null) {
// This client has SSL configured, this allows us to do several things dynamically
channel
.pipeline()
.addLast(
"ssl handler",
state.sslContext.newHandler(
channel.alloc(), state.remote.getHostString(), state.remote.getPort()));
channel.alloc(), state.remote.getHostString(), state.remote.getPort()))
// SSL allows us to use ALPN to negotiate for either http1 or http2
.addLast(
"negotiation handler",
new HttpClientNegotiationHandler(ClientChannelInitializer.this::buildHttp2Handler))
// ALPN will allow us to swap this out for the appropriate netty codec
.addLast("codec", CodecPlaceholderHandler.INSTANCE)
// ALPN will allow us to swap this out for the appropriate xio codec
.addLast("application codec", ApplicationCodecPlaceholderHandler.INSTANCE);
} else {
// This client does not have SSL configured so we can make a few assumptions
// No need for a negotiation handler as we have no ALPN
// No need for an http2 handler as we don't allow that over cleartext
channel
.pipeline()
.addLast("codec", new HttpServerCodec())
.addLast("application codec", new Http1ServerCodec());
}
channel
.pipeline()
.addLast(
"negotiation handler",
new HttpClientNegotiationHandler(ClientChannelInitializer.this::buildHttp2Handler))
.addLast("codec", CodecPlaceholderHandler.INSTANCE);
if (tracing != null) {
val traceHandler = tracing.newClientHandler(state.config.isTlsEnabled());
Pipelines.addHandler(channel.pipeline(), "distributed tracing", traceHandler);
}
channel
.pipeline()
.addLast("application codec", ApplicationCodecPlaceholderHandler.INSTANCE)
.addLast("idle handler", new XioIdleDisconnectHandler(60, 60, 60))
.addLast("message logging", new XioMessageLogger(Client.class, "objects"))
.addLast("request buffer", new RequestBuffer())
Expand Down
5 changes: 5 additions & 0 deletions xio-core/src/main/java/com/xjeffrose/xio/http/Headers.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ default String get(String name) {
}
}

default Headers setLowerCase(CharSequence name, CharSequence value) {
set(name.toString().toLowerCase(), value);
return this;
}

/**
* @param isTrailer this Headers object will be used for trailers.
* @param isRequest this Headers object will be used in a request header.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public HttpClientTracingDispatch(HttpTracing httpTracing, boolean ssl) {
tracing = httpTracing.tracing();
tracer = tracing.tracer();
handler = HttpClientHandler.create(httpTracing, new XioHttpClientAdapter(ssl));
injector = httpTracing.tracing().propagation().injector(Headers::set);
injector = httpTracing.tracing().propagation().injector(Headers::setLowerCase);
}

private Headers addRemoteIp(ChannelHandlerContext ctx, Headers headers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.ssl.SslHandler;
import lombok.val;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.SocketPolicy;
Expand Down Expand Up @@ -78,4 +79,36 @@ public void testEnabledTracing() throws Exception {
val result = testChannel.pipeline().get(HttpClientTracingHandler.class);
assertEquals(result, tracingHandler);
}

@Test
public void testEnableSsl() {
val channelConfig = ChannelConfiguration.clientConfig(1, "worker");
val clientConfig = new ClientConfig(ConfigFactory.load().getConfig("xio.sslClient"));
val clientState = new ClientState(channelConfig, clientConfig);
// when we have enabled Tracing the tracing returns a non-null newClientHandler
when(tracing.newClientHandler(clientConfig.getTls().isUseSsl())).thenReturn(tracingHandler);

subject = new ClientChannelInitializer(clientState, () -> appHandler, tracing);

// Assert that we did not add a HttpClientTracingHandler to the pipeline
val testChannel = new EmbeddedChannel(subject);
val result = testChannel.pipeline().get(SslHandler.class);
assertNotNull(result);
}

@Test
public void testDisableSsl() {
val channelConfig = ChannelConfiguration.clientConfig(1, "worker");
val clientConfig = new ClientConfig(ConfigFactory.load().getConfig("xio.basicClient"));
val clientState = new ClientState(channelConfig, clientConfig);
// when we have enabled Tracing the tracing returns a non-null newClientHandler
when(tracing.newClientHandler(clientConfig.getTls().isUseSsl())).thenReturn(tracingHandler);

subject = new ClientChannelInitializer(clientState, () -> appHandler, tracing);

// Assert that we did not add a HttpClientTracingHandler to the pipeline
val testChannel = new EmbeddedChannel(subject);
val result = testChannel.pipeline().get(SslHandler.class);
assertNull(result);
}
}
8 changes: 8 additions & 0 deletions xio-core/src/test/resources/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,14 @@ xio {
}
}
}
sslClient = ${xio.clientTemplate} {
name = sslClient
settings {
tls {
useSsl = true
}
}
}
invalidZipkinParameters = ${xio.clientTemplate} {
name = invalidZipkinParameters
settings {
Expand Down

0 comments on commit 0d36da5

Please sign in to comment.