-
Notifications
You must be signed in to change notification settings - Fork 870
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Trace SSL handshakes in netty 4.0 #4635
Merged
mateuszrzeszutek
merged 1 commit into
open-telemetry:main
from
mateuszrzeszutek:netty-4.0-ssl
Nov 16, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
215 changes: 215 additions & 0 deletions
215
instrumentation/netty/netty-4.0/javaagent/src/test/groovy/Netty40ClientSslTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import io.netty.bootstrap.Bootstrap | ||
import io.netty.buffer.Unpooled | ||
import io.netty.channel.Channel | ||
import io.netty.channel.ChannelInitializer | ||
import io.netty.channel.ChannelPipeline | ||
import io.netty.channel.EventLoopGroup | ||
import io.netty.channel.nio.NioEventLoopGroup | ||
import io.netty.channel.socket.SocketChannel | ||
import io.netty.channel.socket.nio.NioSocketChannel | ||
import io.netty.handler.codec.http.DefaultFullHttpRequest | ||
import io.netty.handler.codec.http.HttpClientCodec | ||
import io.netty.handler.codec.http.HttpHeaders | ||
import io.netty.handler.codec.http.HttpMethod | ||
import io.netty.handler.codec.http.HttpVersion | ||
import io.netty.handler.ssl.SslHandler | ||
import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification | ||
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestServer | ||
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes | ||
import spock.lang.Shared | ||
|
||
import javax.net.ssl.SSLContext | ||
import javax.net.ssl.SSLHandshakeException | ||
import java.util.concurrent.CompletableFuture | ||
import java.util.concurrent.ExecutionException | ||
import java.util.concurrent.TimeUnit | ||
|
||
import static io.opentelemetry.api.trace.SpanKind.CLIENT | ||
import static io.opentelemetry.api.trace.SpanKind.INTERNAL | ||
import static io.opentelemetry.api.trace.SpanKind.SERVER | ||
import static io.opentelemetry.api.trace.StatusCode.ERROR | ||
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NetTransportValues.IP_TCP | ||
|
||
class Netty40ClientSslTest extends AgentInstrumentationSpecification { | ||
|
||
@Shared | ||
HttpClientTestServer server | ||
@Shared | ||
EventLoopGroup eventLoopGroup | ||
|
||
def setupSpec() { | ||
server = new HttpClientTestServer(openTelemetry) | ||
server.start() | ||
eventLoopGroup = new NioEventLoopGroup() | ||
} | ||
|
||
def cleanupSpec() { | ||
server.stop().get(10, TimeUnit.SECONDS) | ||
eventLoopGroup.shutdownGracefully().sync() | ||
} | ||
|
||
def "should fail SSL handshake"() { | ||
given: | ||
def bootstrap = createBootstrap(eventLoopGroup, ["SSLv3"]) | ||
|
||
def uri = server.resolveHttpsAddress("/success") | ||
def request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri.toString(), Unpooled.EMPTY_BUFFER) | ||
HttpHeaders.setHost(request, uri.host) | ||
|
||
when: | ||
Channel channel = null | ||
runWithSpan("parent") { | ||
channel = bootstrap.connect(uri.host, uri.port).sync().channel() | ||
def result = new CompletableFuture<Integer>() | ||
channel.pipeline().addLast(new ClientHandler(result)) | ||
channel.writeAndFlush(request).get(10, TimeUnit.SECONDS) | ||
result.get(10, TimeUnit.SECONDS) | ||
} | ||
|
||
then: | ||
Throwable thrownException = thrown() | ||
if (thrownException instanceof ExecutionException) { | ||
thrownException = thrownException.cause | ||
} | ||
|
||
assertTraces(1) { | ||
trace(0, 3) { | ||
span(0) { | ||
name "parent" | ||
status ERROR | ||
errorEvent(thrownException.class, thrownException.message) | ||
} | ||
span(1) { | ||
name "CONNECT" | ||
kind INTERNAL | ||
childOf span(0) | ||
attributes { | ||
"${SemanticAttributes.NET_TRANSPORT.key}" IP_TCP | ||
"${SemanticAttributes.NET_PEER_NAME.key}" uri.host | ||
"${SemanticAttributes.NET_PEER_PORT.key}" uri.port | ||
"${SemanticAttributes.NET_PEER_IP.key}" { it == null || it == "127.0.0.1" } | ||
} | ||
} | ||
span(2) { | ||
name "SSL handshake" | ||
kind INTERNAL | ||
childOf span(0) | ||
status ERROR | ||
// netty swallows the exception, it doesn't make any sense to hard-code the message | ||
errorEventWithAnyMessage(SSLHandshakeException) | ||
attributes { | ||
"${SemanticAttributes.NET_TRANSPORT.key}" IP_TCP | ||
"${SemanticAttributes.NET_PEER_NAME.key}" uri.host | ||
"${SemanticAttributes.NET_PEER_PORT.key}" uri.port | ||
"${SemanticAttributes.NET_PEER_IP.key}" { it == null || it == "127.0.0.1" } | ||
} | ||
} | ||
} | ||
} | ||
|
||
cleanup: | ||
channel?.close()?.sync() | ||
} | ||
|
||
def "should successfully establish SSL handshake"() { | ||
given: | ||
def bootstrap = createBootstrap(eventLoopGroup, ["TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3"]) | ||
|
||
def uri = server.resolveHttpsAddress("/success") | ||
def request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri.toString(), Unpooled.EMPTY_BUFFER) | ||
HttpHeaders.setHost(request, uri.host) | ||
|
||
when: | ||
Channel channel = null | ||
runWithSpan("parent") { | ||
channel = bootstrap.connect(uri.host, uri.port).sync().channel() | ||
def result = new CompletableFuture<Integer>() | ||
channel.pipeline().addLast(new ClientHandler(result)) | ||
channel.writeAndFlush(request).get(10, TimeUnit.SECONDS) | ||
result.get(10, TimeUnit.SECONDS) | ||
} | ||
|
||
then: | ||
assertTraces(1) { | ||
trace(0, 5) { | ||
span(0) { | ||
name "parent" | ||
} | ||
span(1) { | ||
name "CONNECT" | ||
kind INTERNAL | ||
childOf span(0) | ||
attributes { | ||
"${SemanticAttributes.NET_TRANSPORT.key}" IP_TCP | ||
"${SemanticAttributes.NET_PEER_NAME.key}" uri.host | ||
"${SemanticAttributes.NET_PEER_PORT.key}" uri.port | ||
"${SemanticAttributes.NET_PEER_IP.key}" { it == null || it == "127.0.0.1" } | ||
} | ||
} | ||
span(2) { | ||
name "SSL handshake" | ||
kind INTERNAL | ||
childOf span(0) | ||
attributes { | ||
"${SemanticAttributes.NET_TRANSPORT.key}" IP_TCP | ||
"${SemanticAttributes.NET_PEER_NAME.key}" uri.host | ||
"${SemanticAttributes.NET_PEER_PORT.key}" uri.port | ||
"${SemanticAttributes.NET_PEER_IP.key}" { it == null || it == "127.0.0.1" } | ||
} | ||
} | ||
span(3) { | ||
name "HTTP GET" | ||
kind CLIENT | ||
childOf(span(0)) | ||
} | ||
span(4) { | ||
name "test-http-server" | ||
kind SERVER | ||
childOf(span(3)) | ||
} | ||
} | ||
} | ||
|
||
cleanup: | ||
channel?.close()?.sync() | ||
} | ||
|
||
// list of default ciphers copied from netty's JdkSslContext | ||
private static final String[] SUPPORTED_CIPHERS = [ | ||
"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", | ||
"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", | ||
"TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", | ||
"TLS_RSA_WITH_AES_128_GCM_SHA256", | ||
"TLS_RSA_WITH_AES_128_CBC_SHA", | ||
"TLS_RSA_WITH_AES_256_CBC_SHA", | ||
"SSL_RSA_WITH_3DES_EDE_CBC_SHA" | ||
] | ||
|
||
private static Bootstrap createBootstrap(EventLoopGroup eventLoopGroup, List<String> enabledProtocols) { | ||
def bootstrap = new Bootstrap() | ||
bootstrap.group(eventLoopGroup) | ||
.channel(NioSocketChannel) | ||
.handler(new ChannelInitializer<SocketChannel>() { | ||
@Override | ||
protected void initChannel(SocketChannel socketChannel) throws Exception { | ||
ChannelPipeline pipeline = socketChannel.pipeline() | ||
|
||
def sslContext = SSLContext.getInstance("TLS") | ||
sslContext.init(null, null, null) | ||
def sslEngine = sslContext.createSSLEngine() | ||
sslEngine.setUseClientMode(true) | ||
sslEngine.setEnabledProtocols(enabledProtocols as String[]) | ||
sslEngine.setEnabledCipherSuites(SUPPORTED_CIPHERS) | ||
pipeline.addLast(new SslHandler(sslEngine)) | ||
|
||
pipeline.addLast(new HttpClientCodec()) | ||
} | ||
}) | ||
bootstrap | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just out of curiosity - why this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
doConnect
actually traces more than just connection - it starts with tracing channel registration (which happens before) and ends after handlerchannelActive()
has returned (which is way after SSL handshake is finished). ThedoConnect0
method seems to be doing just the connection, which is more accurate.