Skip to content

Commit dd8fa02

Browse files
committed
Save changes.
1 parent e9c4e3c commit dd8fa02

File tree

9 files changed

+252
-79
lines changed

9 files changed

+252
-79
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public void close() {
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) {
76-
return tls(sslContext, null, Optional.absent(), null);
75+
public static InternalProtocolNegotiator.ProtocolNegotiator tls(SslContext sslContext, String sni) {
76+
return tls(sslContext, null, Optional.absent(), sni);
7777
}
7878

7979
/**
@@ -171,7 +171,7 @@ 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);
174+
Optional.absent(), null, null);
175175
}
176176

177177
public static class ProtocolNegotiationHandler

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.common.annotations.VisibleForTesting;
2222
import com.google.common.base.Optional;
2323
import com.google.common.base.Preconditions;
24+
import com.google.common.base.Strings;
2425
import com.google.errorprone.annotations.ForOverride;
2526
import io.grpc.Attributes;
2627
import io.grpc.CallCredentials;
@@ -89,6 +90,7 @@
8990
import javax.net.ssl.TrustManager;
9091
import javax.net.ssl.TrustManagerFactory;
9192
import javax.net.ssl.X509TrustManager;
93+
9294
import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement;
9395

9496
/**
@@ -609,8 +611,8 @@ public ChannelHandler newHandler(GrpcHttp2ConnectionHandler grpcHandler) {
609611
ChannelHandler gnh = new GrpcNegotiationHandler(grpcHandler);
610612
ChannelLogger negotiationLogger = grpcHandler.getNegotiationLogger();
611613
ChannelHandler cth = new ClientTlsHandler(gnh, sslContext,
612-
sni != null? sni : grpcHandler.getAuthority(),
613-
this.executor, negotiationLogger, handshakeCompleteRunnable, x509ExtendedTrustManager);
614+
!Strings.isNullOrEmpty(sni)? sni : grpcHandler.getAuthority(),
615+
this.executor, negotiationLogger, handshakeCompleteRunnable, null, x509ExtendedTrustManager);
614616
return new WaitUntilActiveHandler(cth, negotiationLogger);
615617
}
616618

@@ -637,13 +639,13 @@ static final class ClientTlsHandler extends ProtocolNegotiationHandler {
637639
private final X509TrustManager x509ExtendedTrustManager;
638640
private SSLEngine sslEngine;
639641

640-
ClientTlsHandler(ChannelHandler next, SslContext sslContext, String sni,
642+
ClientTlsHandler(ChannelHandler next, SslContext sslContext, String authority,
641643
Executor executor, ChannelLogger negotiationLogger,
642644
Optional<Runnable> handshakeCompleteRunnable,
643-
X509TrustManager x509ExtendedTrustManager) {
645+
ClientTlsProtocolNegotiator clientTlsProtocolNegotiator, X509TrustManager x509ExtendedTrustManager) {
644646
super(next, negotiationLogger);
645647
this.sslContext = Preconditions.checkNotNull(sslContext, "sslContext");
646-
HostPort hostPort = parseAuthority(sni);
648+
HostPort hostPort = parseAuthority(authority);
647649
this.host = hostPort.host;
648650
this.port = hostPort.port;
649651
this.executor = executor;

xds/src/main/java/io/grpc/xds/internal/security/SecurityProtocolNegotiators.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
import static com.google.common.base.Preconditions.checkNotNull;
2020

2121
import com.google.common.annotations.VisibleForTesting;
22+
import com.google.common.base.Strings;
2223
import io.grpc.Attributes;
2324
import io.grpc.EquivalentAddressGroup;
25+
import io.grpc.xds.EnvoyServerProtoData.UpstreamTlsContext;
2426
import io.grpc.Grpc;
2527
import io.grpc.internal.GrpcUtil;
2628
import io.grpc.internal.ObjectPool;
@@ -30,6 +32,7 @@
3032
import io.grpc.netty.InternalProtocolNegotiator.ProtocolNegotiator;
3133
import io.grpc.netty.InternalProtocolNegotiators;
3234
import io.grpc.netty.ProtocolNegotiationEvent;
35+
import io.grpc.xds.EnvoyServerProtoData;
3336
import io.netty.channel.ChannelHandler;
3437
import io.netty.channel.ChannelHandlerAdapter;
3538
import io.netty.channel.ChannelHandlerContext;
@@ -192,12 +195,12 @@ static final class ClientSecurityHandler
192195
extends InternalProtocolNegotiators.ProtocolNegotiationHandler {
193196
private final GrpcHttp2ConnectionHandler grpcHandler;
194197
private final SslContextProviderSupplier sslContextProviderSupplier;
195-
private final String hostname;
198+
private final String sni;
196199

197200
ClientSecurityHandler(
198201
GrpcHttp2ConnectionHandler grpcHandler,
199202
SslContextProviderSupplier sslContextProviderSupplier,
200-
String hostname) {
203+
String endpointHostname) {
201204
super(
202205
// superclass (InternalProtocolNegotiators.ProtocolNegotiationHandler) expects 'next'
203206
// handler but we don't have a next handler _yet_. So we "disable" superclass's behavior
@@ -211,7 +214,15 @@ public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
211214
checkNotNull(grpcHandler, "grpcHandler");
212215
this.grpcHandler = grpcHandler;
213216
this.sslContextProviderSupplier = sslContextProviderSupplier;
214-
this.hostname = hostname;
217+
EnvoyServerProtoData.BaseTlsContext tlsContext = sslContextProviderSupplier.getTlsContext();
218+
UpstreamTlsContext upstreamTlsContext = ((UpstreamTlsContext) tlsContext);
219+
sni = upstreamTlsContext.getAutoHostSni() && !Strings.isNullOrEmpty(endpointHostname)
220+
? endpointHostname : upstreamTlsContext.getSni();
221+
}
222+
223+
@VisibleForTesting
224+
String getSni() {
225+
return sni;
215226
}
216227

217228
@Override
@@ -220,10 +231,10 @@ protected void handlerAdded0(final ChannelHandlerContext ctx) {
220231
ctx.pipeline().addBefore(ctx.name(), null, bufferReads);
221232

222233
sslContextProviderSupplier.updateSslContext(
223-
new SslContextProvider.Callback(ctx.executor(), hostname) {
234+
new SslContextProvider.Callback(ctx.executor()) {
224235

225236
@Override
226-
public void updateSslContext(SslContext sslContext, String sni) {
237+
public void updateSslContext(SslContext sslContext) {
227238
if (ctx.isRemoved()) {
228239
return;
229240
}
@@ -232,7 +243,7 @@ public void updateSslContext(SslContext sslContext, String sni) {
232243
"ClientSecurityHandler.updateSslContext authority={0}, ctx.name={1}",
233244
new Object[]{grpcHandler.getAuthority(), ctx.name()});
234245
ChannelHandler handler =
235-
InternalProtocolNegotiators.tls(sslContext).newHandler(grpcHandler);
246+
InternalProtocolNegotiators.tls(sslContext, sni).newHandler(grpcHandler);
236247

237248
// Delegate rest of handshake to TLS handler
238249
ctx.pipeline().addAfter(ctx.name(), null, handler);
@@ -244,8 +255,8 @@ public void updateSslContext(SslContext sslContext, String sni) {
244255
public void onException(Throwable throwable) {
245256
ctx.fireExceptionCaught(throwable);
246257
}
247-
}
248-
);
258+
},
259+
sni);
249260
}
250261

251262
@Override
@@ -366,7 +377,7 @@ protected void handlerAdded0(final ChannelHandlerContext ctx) {
366377
new SslContextProvider.Callback(ctx.executor()) {
367378

368379
@Override
369-
public void updateSslContext(SslContext sslContext, String sni) {
380+
public void updateSslContext(SslContext sslContext) {
370381
ChannelHandler handler =
371382
InternalProtocolNegotiators.serverTls(sslContext).newHandler(grpcHandler);
372383

@@ -382,8 +393,8 @@ public void updateSslContext(SslContext sslContext, String sni) {
382393
public void onException(Throwable throwable) {
383394
ctx.fireExceptionCaught(throwable);
384395
}
385-
}
386-
);
396+
},
397+
null);
387398
}
388399
}
389400
}

xds/src/main/java/io/grpc/xds/internal/security/SslContextProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ protected String getHostname() {
6969
}
7070

7171
/** Informs callee of new/updated SslContext. */
72-
@VisibleForTesting public abstract void updateSslContext(SslContext sslContext, String sni);
72+
@VisibleForTesting public abstract void updateSslContext(SslContext sslContext);
7373

7474
/** Informs callee of an exception that was generated. */
7575
@VisibleForTesting protected abstract void onException(Throwable throwable);
@@ -132,7 +132,7 @@ protected final void performCallback(
132132
public void run() {
133133
try {
134134
SslContext sslContext = sslContextGetter.get();
135-
callback.updateSslContext(sslContext, callback.getHostname());
135+
callback.updateSslContext(sslContext);
136136
} catch (Throwable e) {
137137
callback.onException(e);
138138
}

xds/src/main/java/io/grpc/xds/internal/security/SslContextProviderSupplier.java

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,16 @@
2020

2121
import com.google.common.annotations.VisibleForTesting;
2222
import com.google.common.base.MoreObjects;
23-
import io.grpc.netty.GrpcSslContexts;
23+
import com.google.common.base.Strings;
2424
import io.grpc.xds.EnvoyServerProtoData.BaseTlsContext;
2525
import io.grpc.xds.EnvoyServerProtoData.DownstreamTlsContext;
2626
import io.grpc.xds.EnvoyServerProtoData.UpstreamTlsContext;
2727
import io.grpc.xds.TlsContextManager;
28-
import io.grpc.xds.internal.security.certprovider.CertProviderClientSslContextProvider;
2928
import io.netty.handler.ssl.SslContext;
29+
3030
import java.util.HashSet;
3131
import java.util.Objects;
3232
import java.util.Set;
33-
import javax.net.ssl.SSLException;
3433

3534
/**
3635
* Enables Client or server side to initialize this object with the received {@link BaseTlsContext}
@@ -58,29 +57,17 @@ public BaseTlsContext getTlsContext() {
5857
}
5958

6059
/** Updates SslContext via the passed callback. */
61-
public synchronized void updateSslContext(final SslContextProvider.Callback callback) {
60+
public synchronized void updateSslContext(final SslContextProvider.Callback callback, String sni) {
6261
checkNotNull(callback, "callback");
6362
try {
64-
String sni;
65-
if (tlsContext instanceof UpstreamTlsContext) {
66-
UpstreamTlsContext upstreamTlsContext = ((UpstreamTlsContext) tlsContext);
67-
sni = upstreamTlsContext.getAutoHostSni() ? callback.getHostname() : upstreamTlsContext.getSni();
68-
} else {
69-
sni = null;
70-
}
71-
if (!shutdown) {
72-
if (sslContextProvider == null) {
73-
sslContextProvider = getSslContextProvider(sni);
74-
}
75-
}
7663
// we want to increment the ref-count so call findOrCreate again...
7764
final SslContextProvider toRelease = getSslContextProvider(sni);
7865
toRelease.addCallback(
7966
new SslContextProvider.Callback(callback.getExecutor()) {
8067

8168
@Override
82-
public void updateSslContext(SslContext sslContext, String sni) {
83-
callback.updateSslContext(sslContext, sni);
69+
public void updateSslContext(SslContext sslContext) {
70+
callback.updateSslContext(sslContext);
8471
releaseSslContextProvider(toRelease, sni);
8572
}
8673

xds/src/test/java/io/grpc/xds/XdsClientWrapperForServerSdsTestMisc.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ public void releaseOldSupplierOnTemporaryError_noClose() throws Exception {
301301
private void callUpdateSslContext(SslContextProviderSupplier sslContextProviderSupplier) {
302302
assertThat(sslContextProviderSupplier).isNotNull();
303303
SslContextProvider.Callback callback = mock(SslContextProvider.Callback.class);
304-
sslContextProviderSupplier.updateSslContext(callback);
304+
sslContextProviderSupplier.updateSslContext(callback, null);
305305
}
306306

307307
private void sendListenerUpdate(

xds/src/test/java/io/grpc/xds/internal/security/CommonTlsContextTestsUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ public TestCallback(Executor executor) {
394394
}
395395

396396
@Override
397-
public void updateSslContext(SslContext sslContext, String sni) {
397+
public void updateSslContext(SslContext sslContext) {
398398
updatedSslContext = sslContext;
399399
}
400400

0 commit comments

Comments
 (0)