Skip to content

Commit 42c9df0

Browse files
committed
Save changes.
1 parent 30ffa7b commit 42c9df0

16 files changed

+216
-171
lines changed

netty/src/main/java/io/grpc/netty/InternalProtocolNegotiators.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ private InternalProtocolNegotiators() {}
4141
* @param executorPool a dedicated {@link Executor} pool for time-consuming TLS tasks
4242
*/
4343
public static InternalProtocolNegotiator.ProtocolNegotiator tls(SslContext sslContext,
44-
ObjectPool<? extends Executor> executorPool,
45-
Optional<Runnable> handshakeCompleteRunnable,
46-
String sni) {
44+
ObjectPool<? extends Executor> executorPool,
45+
Optional<Runnable> handshakeCompleteRunnable,
46+
String sni) {
4747
final io.grpc.netty.ProtocolNegotiator negotiator = ProtocolNegotiators.tls(sslContext,
4848
executorPool, handshakeCompleteRunnable, null, sni);
4949
final class TlsNegotiator implements InternalProtocolNegotiator.ProtocolNegotiator {
@@ -63,17 +63,17 @@ public void close() {
6363
negotiator.close();
6464
}
6565
}
66-
66+
6767
return new TlsNegotiator();
6868
}
69-
69+
7070
/**
7171
* Returns a {@link ProtocolNegotiator} that ensures the pipeline is set up so that TLS will
7272
* be negotiated, the {@code handler} is added and writes to the {@link io.netty.channel.Channel}
7373
* may happen immediately, even before the TLS Handshake is complete.
7474
*/
75-
public static InternalProtocolNegotiator.ProtocolNegotiator tls(SslContext sslContext, String sni) {
76-
return tls(sslContext, null, Optional.absent(), sni);
75+
public static InternalProtocolNegotiator.ProtocolNegotiator tls(SslContext sslContext) {
76+
return tls(sslContext, null, Optional.absent(), null);
7777
}
7878

7979
/**
@@ -156,7 +156,7 @@ public void close() {
156156
* Internal version of {@link WaitUntilActiveHandler}.
157157
*/
158158
public static ChannelHandler waitUntilActiveHandler(ChannelHandler next,
159-
ChannelLogger negotiationLogger) {
159+
ChannelLogger negotiationLogger) {
160160
return new WaitUntilActiveHandler(next, negotiationLogger);
161161
}
162162

@@ -171,19 +171,19 @@ public static ChannelHandler clientTlsHandler(
171171
ChannelHandler next, SslContext sslContext, String authority,
172172
ChannelLogger negotiationLogger) {
173173
return new ClientTlsHandler(next, sslContext, authority, null, negotiationLogger,
174-
Optional.absent(), null, null);
174+
Optional.absent(), null);
175175
}
176176

177177
public static class ProtocolNegotiationHandler
178178
extends ProtocolNegotiators.ProtocolNegotiationHandler {
179179

180180
protected ProtocolNegotiationHandler(ChannelHandler next, String negotiatorName,
181-
ChannelLogger negotiationLogger) {
181+
ChannelLogger negotiationLogger) {
182182
super(next, negotiatorName, negotiationLogger);
183183
}
184184

185185
protected ProtocolNegotiationHandler(ChannelHandler next, ChannelLogger negotiationLogger) {
186186
super(next, negotiationLogger);
187187
}
188188
}
189-
}
189+
}

netty/src/main/java/io/grpc/netty/ProtocolNegotiators.java

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import io.grpc.internal.GrpcUtil;
4747
import io.grpc.internal.NoopSslSession;
4848
import io.grpc.internal.ObjectPool;
49-
import io.netty.channel.Channel;
5049
import io.netty.channel.ChannelDuplexHandler;
5150
import io.netty.channel.ChannelFutureListener;
5251
import io.netty.channel.ChannelHandler;
@@ -61,7 +60,6 @@
6160
import io.netty.handler.codec.http2.Http2ClientUpgradeCodec;
6261
import io.netty.handler.proxy.HttpProxyHandler;
6362
import io.netty.handler.proxy.ProxyConnectionEvent;
64-
import io.netty.handler.ssl.ClientAuth;
6563
import io.netty.handler.ssl.OpenSsl;
6664
import io.netty.handler.ssl.OpenSslEngine;
6765
import io.netty.handler.ssl.SslContext;
@@ -141,7 +139,7 @@ public static FromChannelCredentialsResult from(ChannelCredentials creds) {
141139
trustManagers = tlsCreds.getTrustManagers();
142140
} else if (tlsCreds.getRootCertificates() != null) {
143141
trustManagers = Arrays.asList(CertificateUtils.createTrustManager(
144-
new ByteArrayInputStream(tlsCreds.getRootCertificates())));
142+
new ByteArrayInputStream(tlsCreds.getRootCertificates())));
145143
} else { // else use system default
146144
TrustManagerFactory tmf = TrustManagerFactory.getInstance(
147145
TrustManagerFactory.getDefaultAlgorithm());
@@ -159,7 +157,7 @@ public static FromChannelCredentialsResult from(ChannelCredentials creds) {
159157
}
160158
}
161159
return FromChannelCredentialsResult.negotiator(tlsClientFactory(builder.build(),
162-
(X509TrustManager) x509ExtendedTrustManager));
160+
(X509TrustManager) x509ExtendedTrustManager));
163161
} catch (SSLException | GeneralSecurityException ex) {
164162
log.log(Level.FINE, "Exception building SslContext", ex);
165163
return FromChannelCredentialsResult.error(
@@ -225,15 +223,15 @@ public static FromServerCredentialsResult from(ServerCredentials creds) {
225223
} // else use system default
226224
switch (tlsCreds.getClientAuth()) {
227225
case OPTIONAL:
228-
builder.clientAuth(ClientAuth.OPTIONAL);
226+
builder.clientAuth(io.netty.handler.ssl.ClientAuth.OPTIONAL);
229227
break;
230228

231229
case REQUIRE:
232-
builder.clientAuth(ClientAuth.REQUIRE);
230+
builder.clientAuth(io.netty.handler.ssl.ClientAuth.REQUIRE);
233231
break;
234232

235233
case NONE:
236-
builder.clientAuth(ClientAuth.NONE);
234+
builder.clientAuth(io.netty.handler.ssl.ClientAuth.NONE);
237235
break;
238236

239237
default:
@@ -281,7 +279,7 @@ public static final class FromChannelCredentialsResult {
281279
public final String error;
282280

283281
private FromChannelCredentialsResult(ProtocolNegotiator.ClientFactory negotiator,
284-
CallCredentials creds, String error) {
282+
CallCredentials creds, String error) {
285283
this.negotiator = negotiator;
286284
this.callCredentials = creds;
287285
this.error = error;
@@ -395,7 +393,7 @@ public ProtocolNegotiator newNegotiator(ObjectPool<? extends Executor> offloadEx
395393
* @param executorPool a dedicated {@link Executor} pool for time-consuming TLS tasks
396394
*/
397395
public static ProtocolNegotiator serverTls(final SslContext sslContext,
398-
final ObjectPool<? extends Executor> executorPool) {
396+
final ObjectPool<? extends Executor> executorPool) {
399397
Preconditions.checkNotNull(sslContext, "sslContext");
400398
final Executor executor;
401399
if (executorPool != null) {
@@ -444,8 +442,8 @@ static final class ServerTlsHandler extends ChannelInboundHandlerAdapter {
444442
private ProtocolNegotiationEvent pne = ProtocolNegotiationEvent.DEFAULT;
445443

446444
ServerTlsHandler(ChannelHandler next,
447-
SslContext sslContext,
448-
final ObjectPool<? extends Executor> executorPool) {
445+
SslContext sslContext,
446+
final ObjectPool<? extends Executor> executorPool) {
449447
this.sslContext = Preconditions.checkNotNull(sslContext, "sslContext");
450448
this.next = Preconditions.checkNotNull(next, "next");
451449
if (executorPool != null) {
@@ -475,7 +473,7 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc
475473
}
476474
SslHandler sslHandler = ctx.pipeline().get(SslHandler.class);
477475
if (!sslContext.applicationProtocolNegotiator().protocols().contains(
478-
sslHandler.applicationProtocol())) {
476+
sslHandler.applicationProtocol())) {
479477
logSslEngineDetails(Level.FINE, ctx, "TLS negotiation failed for new client.", null);
480478
ctx.fireExceptionCaught(unavailableException(
481479
"Failed protocol negotiation: Unable to find compatible protocol"));
@@ -502,8 +500,8 @@ private void fireProtocolNegotiationEvent(ChannelHandlerContext ctx, SSLSession
502500
* Returns a {@link ProtocolNegotiator} that does HTTP CONNECT proxy negotiation.
503501
*/
504502
public static ProtocolNegotiator httpProxy(final SocketAddress proxyAddress,
505-
final @Nullable String proxyUsername, final @Nullable String proxyPassword,
506-
final ProtocolNegotiator negotiator) {
503+
final @Nullable String proxyUsername, final @Nullable String proxyPassword,
504+
final ProtocolNegotiator negotiator) {
507505
Preconditions.checkNotNull(negotiator, "negotiator");
508506
Preconditions.checkNotNull(proxyAddress, "proxyAddress");
509507
final AsciiString scheme = negotiator.scheme();
@@ -580,8 +578,10 @@ protected void userEventTriggered0(ChannelHandlerContext ctx, Object evt) throws
580578
static final class ClientTlsProtocolNegotiator implements ProtocolNegotiator {
581579

582580
public ClientTlsProtocolNegotiator(SslContext sslContext,
583-
ObjectPool<? extends Executor> executorPool, Optional<Runnable> handshakeCompleteRunnable,
584-
X509TrustManager x509ExtendedTrustManager, String sni) {
581+
ObjectPool<? extends Executor> executorPool,
582+
Optional<Runnable> handshakeCompleteRunnable,
583+
X509TrustManager x509ExtendedTrustManager,
584+
String sni) {
585585
this.sslContext = Preconditions.checkNotNull(sslContext, "sslContext");
586586
this.executorPool = executorPool;
587587
if (this.executorPool != null) {
@@ -608,9 +608,9 @@ public AsciiString scheme() {
608608
public ChannelHandler newHandler(GrpcHttp2ConnectionHandler grpcHandler) {
609609
ChannelHandler gnh = new GrpcNegotiationHandler(grpcHandler);
610610
ChannelLogger negotiationLogger = grpcHandler.getNegotiationLogger();
611-
ChannelHandler cth = new ClientTlsHandler(gnh, sslContext, grpcHandler.getAuthority(),
612-
this.executor, negotiationLogger, handshakeCompleteRunnable,
613-
x509ExtendedTrustManager, sni);
611+
ChannelHandler cth = new ClientTlsHandler(gnh, sslContext,
612+
sni != null? sni : grpcHandler.getAuthority(),
613+
this.executor, negotiationLogger, handshakeCompleteRunnable, x509ExtendedTrustManager);
614614
return new WaitUntilActiveHandler(cth, negotiationLogger);
615615
}
616616

@@ -635,17 +635,15 @@ static final class ClientTlsHandler extends ProtocolNegotiationHandler {
635635
private Executor executor;
636636
private final Optional<Runnable> handshakeCompleteRunnable;
637637
private final X509TrustManager x509ExtendedTrustManager;
638-
private final String sni;
639638
private SSLEngine sslEngine;
640639

641-
ClientTlsHandler(ChannelHandler next, SslContext sslContext, String authority,
640+
ClientTlsHandler(ChannelHandler next, SslContext sslContext, String sni,
642641
Executor executor, ChannelLogger negotiationLogger,
643642
Optional<Runnable> handshakeCompleteRunnable,
644-
X509TrustManager x509ExtendedTrustManager, String sni) {
643+
X509TrustManager x509ExtendedTrustManager) {
645644
super(next, negotiationLogger);
646645
this.sslContext = Preconditions.checkNotNull(sslContext, "sslContext");
647-
this.sni = sni;
648-
HostPort hostPort = parseAuthority(authority);
646+
HostPort hostPort = parseAuthority(sni);
649647
this.host = hostPort.host;
650648
this.port = hostPort.port;
651649
this.executor = executor;
@@ -656,7 +654,7 @@ static final class ClientTlsHandler extends ProtocolNegotiationHandler {
656654
@Override
657655
@IgnoreJRERequirement
658656
protected void handlerAdded0(ChannelHandlerContext ctx) {
659-
sslEngine = sslContext.newEngine(ctx.alloc(), sni != null? sni : host, port);
657+
sslEngine = sslContext.newEngine(ctx.alloc(), host, port);
660658
SSLParameters sslParams = sslEngine.getSSLParameters();
661659
sslParams.setEndpointIdentificationAlgorithm("HTTPS");
662660
sslEngine.setSSLParameters(sslParams);
@@ -749,11 +747,9 @@ static HostPort parseAuthority(String authority) {
749747

750748
/**
751749
* Returns a {@link ProtocolNegotiator} that ensures the pipeline is set up so that TLS will
752-
* be negotiated, the {@code handler} is added and writes to the {@link Channel}
750+
* be negotiated, the {@code handler} is added and writes to the {@link io.netty.channel.Channel}
753751
* may happen immediately, even before the TLS Handshake is complete.
754-
*
755752
* @param executorPool a dedicated {@link Executor} pool for time-consuming TLS tasks
756-
* @param sni the SNI value to use in the Tls handshake
757753
*/
758754
public static ProtocolNegotiator tls(SslContext sslContext,
759755
ObjectPool<? extends Executor> executorPool, Optional<Runnable> handshakeCompleteRunnable,
@@ -764,16 +760,16 @@ public static ProtocolNegotiator tls(SslContext sslContext,
764760

765761
/**
766762
* Returns a {@link ProtocolNegotiator} that ensures the pipeline is set up so that TLS will
767-
* be negotiated, the {@code handler} is added and writes to the {@link Channel}
763+
* be negotiated, the {@code handler} is added and writes to the {@link io.netty.channel.Channel}
768764
* may happen immediately, even before the TLS Handshake is complete.
769765
*/
770766
public static ProtocolNegotiator tls(SslContext sslContext,
771-
X509TrustManager x509ExtendedTrustManager) {
767+
X509TrustManager x509ExtendedTrustManager) {
772768
return tls(sslContext, null, Optional.absent(), x509ExtendedTrustManager, null);
773769
}
774770

775771
public static ProtocolNegotiator.ClientFactory tlsClientFactory(SslContext sslContext,
776-
X509TrustManager x509ExtendedTrustManager) {
772+
X509TrustManager x509ExtendedTrustManager) {
777773
return new TlsProtocolNegotiatorClientFactory(sslContext, x509ExtendedTrustManager);
778774
}
779775

@@ -911,8 +907,8 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc
911907
}
912908

913909
/**
914-
* Returns a {@link ChannelHandler} that ensures that the {@code handler} is
915-
* added to the pipeline writes to the {@link Channel} may happen immediately,
910+
* Returns a {@link io.netty.channel.ChannelHandler} that ensures that the {@code handler} is
911+
* added to the pipeline writes to the {@link io.netty.channel.Channel} may happen immediately,
916912
* even before it is active.
917913
*/
918914
public static ProtocolNegotiator plaintext() {
@@ -941,7 +937,7 @@ private static RuntimeException unavailableException(String msg) {
941937

942938
@VisibleForTesting
943939
static void logSslEngineDetails(Level level, ChannelHandlerContext ctx, String msg,
944-
@Nullable Throwable t) {
940+
@Nullable Throwable t) {
945941
if (!log.isLoggable(level)) {
946942
return;
947943
}
@@ -1067,8 +1063,8 @@ static final class PlaintextHandler extends ProtocolNegotiationHandler {
10671063
protected void protocolNegotiationEventTriggered(ChannelHandlerContext ctx) {
10681064
ProtocolNegotiationEvent existingPne = getProtocolNegotiationEvent();
10691065
Attributes attrs = existingPne.getAttributes().toBuilder()
1070-
.set(GrpcAttributes.ATTR_AUTHORITY_VERIFIER, (authority) -> Status.OK)
1071-
.build();
1066+
.set(GrpcAttributes.ATTR_AUTHORITY_VERIFIER, (authority) -> Status.OK)
1067+
.build();
10721068
replaceProtocolNegotiationEvent(existingPne.withAttributes(attrs));
10731069
fireProtocolNegotiationEvent(ctx);
10741070
}
@@ -1130,7 +1126,7 @@ static class ProtocolNegotiationHandler extends ChannelDuplexHandler {
11301126
private final ChannelLogger negotiationLogger;
11311127

11321128
protected ProtocolNegotiationHandler(ChannelHandler next, String negotiatorName,
1133-
ChannelLogger negotiationLogger) {
1129+
ChannelLogger negotiationLogger) {
11341130
this.next = Preconditions.checkNotNull(next, "next");
11351131
this.negotiatorName = negotiatorName;
11361132
this.negotiationLogger = Preconditions.checkNotNull(negotiationLogger, "negotiationLogger");
@@ -1228,4 +1224,4 @@ public String getPeerHost() {
12281224
return peerHost;
12291225
}
12301226
}
1231-
}
1227+
}

netty/src/test/java/io/grpc/netty/NettyClientTransportTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
public class NettyClientTransportTest {
150150
@Rule public final MockitoRule mocks = MockitoJUnit.rule();
151151

152+
private static final String SNI = "sni";
152153
private static final SslContext SSL_CONTEXT = createSslContext();
153154

154155
@Mock
@@ -836,7 +837,7 @@ public void tlsNegotiationServerExecutorShouldSucceed() throws Exception {
836837
.keyManager(clientCert, clientKey)
837838
.build();
838839
ProtocolNegotiator negotiator = ProtocolNegotiators.tls(clientContext, clientExecutorPool,
839-
Optional.absent(), null, sni);
840+
Optional.absent(), null, SNI);
840841
// after starting the client, the Executor in the client pool should be used
841842
assertEquals(true, clientExecutorPool.isInUse());
842843
final NettyClientTransport transport = newTransport(negotiator);

0 commit comments

Comments
 (0)