Skip to content

Commit 2f5ba5d

Browse files
committed
Save changes.
1 parent 92f3182 commit 2f5ba5d

File tree

11 files changed

+188
-161
lines changed

11 files changed

+188
-161
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,16 @@ private InternalProtocolNegotiators() {}
3838
* Returns a {@link ProtocolNegotiator} that ensures the pipeline is set up so that TLS will
3939
* be negotiated, the {@code handler} is added and writes to the {@link io.netty.channel.Channel}
4040
* may happen immediately, even before the TLS Handshake is complete.
41+
*
4142
* @param executorPool a dedicated {@link Executor} pool for time-consuming TLS tasks
43+
* @param isXdsTarget
4244
*/
4345
public static InternalProtocolNegotiator.ProtocolNegotiator tls(SslContext sslContext,
44-
ObjectPool<? extends Executor> executorPool,
45-
Optional<Runnable> handshakeCompleteRunnable,
46-
String sni) {
46+
ObjectPool<? extends Executor> executorPool,
47+
Optional<Runnable> handshakeCompleteRunnable,
48+
String sni, boolean isXdsTarget) {
4749
final io.grpc.netty.ProtocolNegotiator negotiator = ProtocolNegotiators.tls(sslContext,
48-
executorPool, handshakeCompleteRunnable, null, sni);
50+
executorPool, handshakeCompleteRunnable, null, sni, isXdsTarget);
4951
final class TlsNegotiator implements InternalProtocolNegotiator.ProtocolNegotiator {
5052

5153
@Override
@@ -73,8 +75,8 @@ public void close() {
7375
* may happen immediately, even before the TLS Handshake is complete.
7476
*/
7577
public static InternalProtocolNegotiator.ProtocolNegotiator tls(
76-
SslContext sslContext, String sni) {
77-
return tls(sslContext, null, Optional.absent(), sni);
78+
SslContext sslContext, String sni, boolean isXdsTarget) {
79+
return tls(sslContext, null, Optional.absent(), sni, isXdsTarget);
7880
}
7981

8082
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ static ProtocolNegotiator createProtocolNegotiatorByType(
652652
case PLAINTEXT_UPGRADE:
653653
return ProtocolNegotiators.plaintextUpgrade();
654654
case TLS:
655-
return ProtocolNegotiators.tls(sslContext, executorPool, Optional.absent(), null, null);
655+
return ProtocolNegotiators.tls(sslContext, executorPool, Optional.absent(), null, null, false);
656656
default:
657657
throw new IllegalArgumentException("Unsupported negotiationType: " + negotiationType);
658658
}

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
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;
2524
import com.google.errorprone.annotations.ForOverride;
2625
import io.grpc.Attributes;
2726
import io.grpc.CallCredentials;
@@ -583,7 +582,7 @@ public ClientTlsProtocolNegotiator(SslContext sslContext,
583582
ObjectPool<? extends Executor> executorPool,
584583
Optional<Runnable> handshakeCompleteRunnable,
585584
X509TrustManager x509ExtendedTrustManager,
586-
String sni) {
585+
String sni, boolean isXdsTarget) {
587586
this.sslContext = Preconditions.checkNotNull(sslContext, "sslContext");
588587
this.executorPool = executorPool;
589588
if (this.executorPool != null) {
@@ -592,13 +591,15 @@ public ClientTlsProtocolNegotiator(SslContext sslContext,
592591
this.handshakeCompleteRunnable = handshakeCompleteRunnable;
593592
this.x509ExtendedTrustManager = x509ExtendedTrustManager;
594593
this.sni = sni;
594+
this.isXdsTarget = isXdsTarget;
595595
}
596596

597597
private final SslContext sslContext;
598598
private final ObjectPool<? extends Executor> executorPool;
599599
private final Optional<Runnable> handshakeCompleteRunnable;
600600
private final X509TrustManager x509ExtendedTrustManager;
601601
private final String sni;
602+
private final boolean isXdsTarget;
602603
private Executor executor;
603604

604605
@Override
@@ -611,7 +612,7 @@ public ChannelHandler newHandler(GrpcHttp2ConnectionHandler grpcHandler) {
611612
ChannelHandler gnh = new GrpcNegotiationHandler(grpcHandler);
612613
ChannelLogger negotiationLogger = grpcHandler.getNegotiationLogger();
613614
ChannelHandler cth = new ClientTlsHandler(gnh, sslContext,
614-
!Strings.isNullOrEmpty(sni) ? sni : grpcHandler.getAuthority(),
615+
isXdsTarget ? sni : grpcHandler.getAuthority(),
615616
this.executor, negotiationLogger, handshakeCompleteRunnable, null,
616617
x509ExtendedTrustManager);
617618
return new WaitUntilActiveHandler(cth, negotiationLogger);
@@ -753,13 +754,14 @@ static HostPort parseAuthority(String authority) {
753754
* Returns a {@link ProtocolNegotiator} that ensures the pipeline is set up so that TLS will
754755
* be negotiated, the {@code handler} is added and writes to the {@link io.netty.channel.Channel}
755756
* may happen immediately, even before the TLS Handshake is complete.
757+
*
756758
* @param executorPool a dedicated {@link Executor} pool for time-consuming TLS tasks
757759
*/
758760
public static ProtocolNegotiator tls(SslContext sslContext,
759-
ObjectPool<? extends Executor> executorPool, Optional<Runnable> handshakeCompleteRunnable,
760-
X509TrustManager x509ExtendedTrustManager, String sni) {
761+
ObjectPool<? extends Executor> executorPool, Optional<Runnable> handshakeCompleteRunnable,
762+
X509TrustManager x509ExtendedTrustManager, String sni, boolean isXdsTarget) {
761763
return new ClientTlsProtocolNegotiator(sslContext, executorPool, handshakeCompleteRunnable,
762-
x509ExtendedTrustManager, sni);
764+
x509ExtendedTrustManager, sni, isXdsTarget);
763765
}
764766

765767
/**
@@ -769,7 +771,7 @@ public static ProtocolNegotiator tls(SslContext sslContext,
769771
*/
770772
public static ProtocolNegotiator tls(SslContext sslContext,
771773
X509TrustManager x509ExtendedTrustManager) {
772-
return tls(sslContext, null, Optional.absent(), x509ExtendedTrustManager, null);
774+
return tls(sslContext, null, Optional.absent(), x509ExtendedTrustManager, null, false);
773775
}
774776

775777
public static ProtocolNegotiator.ClientFactory tlsClientFactory(SslContext sslContext,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ public void tlsNegotiationServerExecutorShouldSucceed() throws Exception {
836836
.keyManager(clientCert, clientKey)
837837
.build();
838838
ProtocolNegotiator negotiator = ProtocolNegotiators.tls(clientContext, clientExecutorPool,
839-
Optional.absent(), null, null);
839+
Optional.absent(), null, null, false);
840840
// after starting the client, the Executor in the client pool should be used
841841
assertEquals(true, clientExecutorPool.isInUse());
842842
final NettyClientTransport transport = newTransport(negotiator);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1271,7 +1271,7 @@ public void clientTlsHandler_firesNegotiation() throws Exception {
12711271
}
12721272
FakeGrpcHttp2ConnectionHandler gh = FakeGrpcHttp2ConnectionHandler.newHandler();
12731273
ClientTlsProtocolNegotiator pn = new ClientTlsProtocolNegotiator(clientSslContext,
1274-
null, Optional.absent(), null, null);
1274+
null, Optional.absent(), null, null, false);
12751275
WriteBufferingAndExceptionHandler clientWbaeh =
12761276
new WriteBufferingAndExceptionHandler(pn.newHandler(gh));
12771277

s2a/src/main/java/io/grpc/s2a/internal/handshaker/S2AProtocolNegotiatorFactory.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import io.grpc.netty.InternalProtocolNegotiator.ProtocolNegotiator;
3939
import io.grpc.netty.InternalProtocolNegotiators;
4040
import io.grpc.netty.InternalProtocolNegotiators.ProtocolNegotiationHandler;
41-
import io.grpc.s2a.internal.handshaker.S2AIdentity;
4241
import io.netty.channel.ChannelHandler;
4342
import io.netty.channel.ChannelHandlerAdapter;
4443
import io.netty.channel.ChannelHandlerContext;
@@ -260,7 +259,7 @@ public void run() {
260259
s2aStub.close();
261260
}
262261
}),
263-
null)
262+
null, false)
264263
.newHandler(grpcHandler);
265264

266265
// Delegate the rest of the handshake to the TLS handler. and remove the

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@
5454
@VisibleForTesting
5555
public final class SecurityProtocolNegotiators {
5656

57-
static boolean useChannelAuthorityIfNoSniApplicable =
58-
GrpcUtil.getFlag("GRPC_USE_CHANNEL_AUTHORITY_IF_NO_SNI_APPLICABLE", false);
59-
6057
/** Name associated with individual address, if available (e.g., DNS name). */
6158
@EquivalentAddressGroup.Attr
6259
public static final Attributes.Key<String> ATTR_ADDRESS_NAME =
@@ -196,6 +193,8 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
196193
@VisibleForTesting
197194
static final class ClientSecurityHandler
198195
extends InternalProtocolNegotiators.ProtocolNegotiationHandler {
196+
static boolean isXdsSniEnabled = GrpcUtil.getFlag("GRPC_EXPERIMENTAL_XDS_SNI", false);
197+
199198
private final GrpcHttp2ConnectionHandler grpcHandler;
200199
private final SslContextProviderSupplier sslContextProviderSupplier;
201200
private final String sni;
@@ -219,12 +218,12 @@ public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
219218
this.sslContextProviderSupplier = sslContextProviderSupplier;
220219
EnvoyServerProtoData.BaseTlsContext tlsContext = sslContextProviderSupplier.getTlsContext();
221220
UpstreamTlsContext upstreamTlsContext = ((UpstreamTlsContext) tlsContext);
222-
String sniVal = upstreamTlsContext.getAutoHostSni() && !Strings.isNullOrEmpty(endpointHostname)
223-
? endpointHostname : upstreamTlsContext.getSni();
224-
if (Strings.isNullOrEmpty(sniVal) && useChannelAuthorityIfNoSniApplicable) {
225-
sniVal = grpcHandler.getAuthority();
221+
if (isXdsSniEnabled) {
222+
sni = upstreamTlsContext.getAutoHostSni() && !Strings.isNullOrEmpty(endpointHostname)
223+
? endpointHostname : upstreamTlsContext.getSni();
224+
} else {
225+
sni = grpcHandler.getAuthority();
226226
}
227-
sni = sniVal;
228227
}
229228

230229
@VisibleForTesting
@@ -250,7 +249,7 @@ public void updateSslContext(SslContext sslContext) {
250249
"ClientSecurityHandler.updateSslContext authority={0}, ctx.name={1}",
251250
new Object[]{grpcHandler.getAuthority(), ctx.name()});
252251
ChannelHandler handler =
253-
InternalProtocolNegotiators.tls(sslContext, sni).newHandler(grpcHandler);
252+
InternalProtocolNegotiators.tls(sslContext, sni, true).newHandler(grpcHandler);
254253

255254
// Delegate rest of handshake to TLS handler
256255
ctx.pipeline().addAfter(ctx.name(), null, handler);

xds/src/main/java/io/grpc/xds/internal/security/trust/XdsX509TrustManager.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import io.envoyproxy.envoy.extensions.transport_sockets.tls.v3.CertificateValidationContext;
2828
import io.envoyproxy.envoy.type.matcher.v3.RegexMatcher;
2929
import io.envoyproxy.envoy.type.matcher.v3.StringMatcher;
30+
import io.grpc.internal.GrpcUtil;
3031
import io.grpc.internal.SpiffeUtil;
3132
import java.net.Socket;
3233
import java.security.cert.CertificateException;
@@ -52,6 +53,8 @@
5253
*/
5354
final class XdsX509TrustManager extends X509ExtendedTrustManager implements X509TrustManager {
5455

56+
static boolean isXdsSniEnabled = GrpcUtil.getFlag("GRPC_EXPERIMENTAL_XDS_SNI", false);
57+
5558
// ref: io.grpc.okhttp.internal.OkHostnameVerifier and
5659
// sun.security.x509.GeneralNameInterface
5760
private static final int ALT_DNS_NAME = 2;
@@ -217,7 +220,7 @@ void verifySubjectAltNameInChain(X509Certificate[] peerCertChain) throws Certifi
217220
return;
218221
}
219222
@SuppressWarnings("deprecation") // gRFC A29 predates match_typed_subject_alt_names
220-
List<StringMatcher> verifyList = !Strings.isNullOrEmpty(sniForSanMatching)
223+
List<StringMatcher> verifyList = isXdsSniEnabled && !Strings.isNullOrEmpty(sniForSanMatching)
221224
? ImmutableList.of(StringMatcher.newBuilder().setExact(sniForSanMatching).build())
222225
: certContext.getMatchSubjectAltNamesList();
223226
if (verifyList.isEmpty()) {

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,6 @@ public void tlsClientServer_useSystemRootCerts_mtls() throws Exception {
282282
}
283283
}
284284

285-
/**
286-
* Use system root ca cert for TLS channel - no mTLS.
287-
* Subj Alt Names to match are specified in the validation context.
288-
*/
289285
@Test
290286
public void tlsClientServer_noAutoSniValidation_failureToMatchSubjAltNames()
291287
throws Exception {
@@ -346,7 +342,7 @@ public void tlsClientServer_autoSniValidation_sniInUTC()
346342
}
347343

348344
@Test
349-
public void tlsClientServer_sni_san_validation_from_hostname()
345+
public void tlsClientServer_autoSniValidation_sniFromHostname()
350346
throws Exception {
351347
Path trustStoreFilePath = getCacertFilePathForTestCa();
352348
try {

0 commit comments

Comments
 (0)