Skip to content

Fix SslHandler in Netty client #1184

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

Merged
merged 1 commit into from
Apr 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"category": "Netty NIO HTTP Client",
"type": "bugfix",
"description": "Fix a bug where SNI was not enabled in Netty NIO Async Client for TLS and caused the requests to fail of handshake_failure in some services. See [#1171](https://github.com/aws/aws-sdk-java-v2/issues/1171)"
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ protected ChannelPool newPool(URI key) {

AtomicReference<ChannelPool> channelPoolRef = new AtomicReference<>();
ChannelPipelineInitializer handler =
new ChannelPipelineInitializer(protocol, sslContext, maxStreams, channelPoolRef, configuration);
new ChannelPipelineInitializer(protocol, sslContext, maxStreams, channelPoolRef, configuration, key);
channelPoolRef.set(createChannelPool(bootstrap, handler));
return channelPoolRef.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslHandler;
import java.net.URI;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLParameters;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.http.Protocol;
import software.amazon.awssdk.http.nio.netty.internal.http2.Http2SettingsFrameHandler;
Expand All @@ -46,25 +50,34 @@ public final class ChannelPipelineInitializer extends AbstractChannelPoolHandler
private final long clientMaxStreams;
private final AtomicReference<ChannelPool> channelPoolRef;
private final NettyConfiguration configuration;
private final URI poolKey;

public ChannelPipelineInitializer(Protocol protocol,
SslContext sslCtx,
long clientMaxStreams,
AtomicReference<ChannelPool> channelPoolRef,
NettyConfiguration configuration) {
NettyConfiguration configuration,
URI poolKey) {
this.protocol = protocol;
this.sslCtx = sslCtx;
this.clientMaxStreams = clientMaxStreams;
this.channelPoolRef = channelPoolRef;
this.configuration = configuration;
this.poolKey = poolKey;
}

@Override
public void channelCreated(Channel ch) {
ch.attr(PROTOCOL_FUTURE).set(new CompletableFuture<>());
ChannelPipeline pipeline = ch.pipeline();
if (sslCtx != null) {
pipeline.addLast(sslCtx.newHandler(ch.alloc()));

// Need to provide host and port to enable SNI
// https://github.com/netty/netty/issues/3801#issuecomment-104274440
SslHandler sslHandler = sslCtx.newHandler(ch.alloc(), poolKey.getHost(), poolKey.getPort());
configureSslEngine(sslHandler.engine());

pipeline.addLast(sslHandler);
pipeline.addLast(SslCloseCompletionEventHandler.getInstance());
}

Expand All @@ -87,6 +100,20 @@ public void channelCreated(Channel ch) {
pipeline.addLast(new LoggingHandler(LogLevel.DEBUG));
}

/**
* Enable HostName verification.
*
* See https://netty.io/4.0/api/io/netty/handler/ssl/SslContext.html#newHandler-io.netty.buffer.ByteBufAllocator-java.lang
* .String-int-
*
* @param sslEngine the sslEngine to configure
*/
private void configureSslEngine(SSLEngine sslEngine) {
SSLParameters sslParameters = sslEngine.getSSLParameters();
sslParameters.setEndpointIdentificationAlgorithm("HTTPS");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How in the world is this not the default...

sslEngine.setSSLParameters(sslParameters);
}

private void configureHttp2(Channel ch, ChannelPipeline pipeline) {
ForkedHttp2MultiplexCodecBuilder codecBuilder = ForkedHttp2MultiplexCodecBuilder
.forClient(new NoOpChannelInitializer())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.services.pinpoint;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.BeforeClass;
import org.junit.Test;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.testutils.service.AwsTestBase;
import software.amazon.awssdk.utils.builder.SdkBuilder;

public class PinpointIntegTest extends AwsTestBase {

protected static PinpointAsyncClient pinpointAsyncClient;

@BeforeClass
public static void setup() {
pinpointAsyncClient = PinpointAsyncClient.builder()
.region(Region.US_WEST_2)
.credentialsProvider(CREDENTIALS_PROVIDER_CHAIN)
.build();
}

@Test
public void getApps() {
assertThat(pinpointAsyncClient.getApps(SdkBuilder::build).join()).isNotNull();
}
}