Skip to content

Commit a576df0

Browse files
committed
Fallback flag when no sni is available to send to specify to use xds channel authority itself.
1 parent a6f1bc9 commit a576df0

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
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+
5760
/** Name associated with individual address, if available (e.g., DNS name). */
5861
@EquivalentAddressGroup.Attr
5962
public static final Attributes.Key<String> ATTR_ADDRESS_NAME =
@@ -216,8 +219,12 @@ public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
216219
this.sslContextProviderSupplier = sslContextProviderSupplier;
217220
EnvoyServerProtoData.BaseTlsContext tlsContext = sslContextProviderSupplier.getTlsContext();
218221
UpstreamTlsContext upstreamTlsContext = ((UpstreamTlsContext) tlsContext);
219-
sni = upstreamTlsContext.getAutoHostSni() && !Strings.isNullOrEmpty(endpointHostname)
222+
String sniVal = upstreamTlsContext.getAutoHostSni() && !Strings.isNullOrEmpty(endpointHostname)
220223
? endpointHostname : upstreamTlsContext.getSni();
224+
if (Strings.isNullOrEmpty(sniVal) && useChannelAuthorityIfNoSniApplicable) {
225+
sniVal = grpcHandler.getAuthority();
226+
}
227+
sni = sniVal;
221228
}
222229

223230
@VisibleForTesting

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public class SecurityProtocolNegotiatorsTest {
8888

8989
private static final String HOSTNAME = "hostname";
9090
private static final String SNI_IN_UTC = "sni-in-upstream-tls-context";
91+
private static final String FAKE_AUTHORITY = "authority";
9192

9293
private final GrpcHttp2ConnectionHandler grpcHandler =
9394
FakeGrpcHttp2ConnectionHandler.newHandler();
@@ -269,6 +270,29 @@ public void sniInClientSecurityHandler_autoHostSniIsFalse_usesSniFromUpstreamTls
269270
assertThat(clientSecurityHandler.getSni()).isEqualTo(SNI_IN_UTC);
270271
}
271272

273+
@Test
274+
public void emptySni_useChannelAuthorityIfNoSniApplicableIsTrue_usesChannelAuthority() {
275+
SecurityProtocolNegotiators.useChannelAuthorityIfNoSniApplicable = true;
276+
try {
277+
Bootstrapper.BootstrapInfo bootstrapInfoForClient = CommonBootstrapperTestUtils
278+
.buildBootstrapInfo("google_cloud_private_spiffe-client", CLIENT_KEY_FILE, CLIENT_PEM_FILE,
279+
CA_PEM_FILE, null, null, null, null, null);
280+
UpstreamTlsContext upstreamTlsContext =
281+
CommonTlsContextTestsUtil
282+
.buildUpstreamTlsContext("google_cloud_private_spiffe-client", true, "", false);
283+
SslContextProviderSupplier sslContextProviderSupplier =
284+
new SslContextProviderSupplier(upstreamTlsContext,
285+
new TlsContextManagerImpl(bootstrapInfoForClient));
286+
287+
ClientSecurityHandler clientSecurityHandler =
288+
new ClientSecurityHandler(grpcHandler, sslContextProviderSupplier, HOSTNAME);
289+
290+
assertThat(clientSecurityHandler.getSni()).isEqualTo(FAKE_AUTHORITY);
291+
} finally {
292+
SecurityProtocolNegotiators.useChannelAuthorityIfNoSniApplicable = false;
293+
}
294+
}
295+
272296
@Test
273297
public void serverSecurityHandler_addLast()
274298
throws InterruptedException, TimeoutException, ExecutionException {
@@ -533,7 +557,7 @@ static FakeGrpcHttp2ConnectionHandler newHandler() {
533557

534558
@Override
535559
public String getAuthority() {
536-
return "authority";
560+
return FAKE_AUTHORITY;
537561
}
538562
}
539563
}

0 commit comments

Comments
 (0)