diff --git a/alts/src/main/java/io/grpc/alts/ComputeEngineChannelCredentials.java b/alts/src/main/java/io/grpc/alts/ComputeEngineChannelCredentials.java index ff5bab6ec7..6bc034c349 100644 --- a/alts/src/main/java/io/grpc/alts/ComputeEngineChannelCredentials.java +++ b/alts/src/main/java/io/grpc/alts/ComputeEngineChannelCredentials.java @@ -69,7 +69,6 @@ public final class ComputeEngineChannelCredentials { return new GoogleDefaultProtocolNegotiatorFactory( /* targetServiceAccounts= */ ImmutableList.of(), SharedResourcePool.forResource(HandshakerServiceChannel.SHARED_HANDSHAKER_CHANNEL), - sslContext, - null); + sslContext); } } diff --git a/alts/src/main/java/io/grpc/alts/GoogleDefaultChannelCredentials.java b/alts/src/main/java/io/grpc/alts/GoogleDefaultChannelCredentials.java index 41c6b99827..9c8b39cfc1 100644 --- a/alts/src/main/java/io/grpc/alts/GoogleDefaultChannelCredentials.java +++ b/alts/src/main/java/io/grpc/alts/GoogleDefaultChannelCredentials.java @@ -18,7 +18,6 @@ package io.grpc.alts; import com.google.auth.oauth2.GoogleCredentials; import com.google.common.collect.ImmutableList; -import io.grpc.Attributes; import io.grpc.CallCredentials; import io.grpc.ChannelCredentials; import io.grpc.CompositeChannelCredentials; @@ -32,8 +31,6 @@ import io.grpc.netty.InternalNettyChannelCredentials; import io.grpc.netty.InternalProtocolNegotiator; import io.netty.handler.ssl.SslContext; import java.io.IOException; -import java.util.logging.Level; -import java.util.logging.Logger; import javax.net.ssl.SSLException; /** @@ -42,8 +39,6 @@ import javax.net.ssl.SSLException; */ @ExperimentalApi("https://github.com/grpc/grpc-java/issues/7479") public final class GoogleDefaultChannelCredentials { - private static Logger logger = Logger.getLogger(GoogleDefaultChannelCredentials.class.getName()); - private GoogleDefaultChannelCredentials() {} /** @@ -66,7 +61,6 @@ public final class GoogleDefaultChannelCredentials { return CompositeChannelCredentials.create(nettyCredentials, callCredentials); } - @SuppressWarnings("unchecked") private static InternalProtocolNegotiator.ClientFactory createClientFactory() { SslContext sslContext; try { @@ -74,25 +68,9 @@ public final class GoogleDefaultChannelCredentials { } catch (SSLException e) { throw new RuntimeException(e); } - Attributes.Key clusterNameAttrKey = null; - try { - Class klass = Class.forName("io.grpc.xds.InternalXdsAttributes"); - clusterNameAttrKey = - (Attributes.Key) klass.getField("ATTR_CLUSTER_NAME").get(null); - } catch (ClassNotFoundException e) { - logger.log(Level.FINE, - "Unable to load xDS endpoint cluster name key, this may be expected", e); - } catch (NoSuchFieldException e) { - logger.log(Level.FINE, - "Unable to load xDS endpoint cluster name key, this may be expected", e); - } catch (IllegalAccessException e) { - logger.log(Level.FINE, - "Unable to load xDS endpoint cluster name key, this may be expected", e); - } return new GoogleDefaultProtocolNegotiatorFactory( /* targetServiceAccounts= */ ImmutableList.of(), SharedResourcePool.forResource(HandshakerServiceChannel.SHARED_HANDSHAKER_CHANNEL), - sslContext, - clusterNameAttrKey); + sslContext); } } diff --git a/alts/src/main/java/io/grpc/alts/internal/AltsProtocolNegotiator.java b/alts/src/main/java/io/grpc/alts/internal/AltsProtocolNegotiator.java index e3cf87e620..8be903f247 100644 --- a/alts/src/main/java/io/grpc/alts/internal/AltsProtocolNegotiator.java +++ b/alts/src/main/java/io/grpc/alts/internal/AltsProtocolNegotiator.java @@ -41,6 +41,7 @@ import io.netty.handler.ssl.SslContext; import io.netty.util.AsciiString; import java.security.GeneralSecurityException; import java.util.List; +import java.util.logging.Level; import java.util.logging.Logger; import javax.annotation.Nullable; @@ -194,11 +195,12 @@ public final class AltsProtocolNegotiator { */ public static final class GoogleDefaultProtocolNegotiatorFactory implements InternalProtocolNegotiator.ClientFactory { + @VisibleForTesting + @Nullable + static Attributes.Key clusterNameAttrKey = loadClusterNameAttrKey(); private final ImmutableList targetServiceAccounts; private final ObjectPool handshakerChannelPool; private final SslContext sslContext; - @Nullable - private final Attributes.Key clusterNameAttrKey; /** * Creates Negotiator Factory, which will either use the targetServiceAccounts and @@ -207,12 +209,10 @@ public final class AltsProtocolNegotiator { public GoogleDefaultProtocolNegotiatorFactory( List targetServiceAccounts, ObjectPool handshakerChannelPool, - SslContext sslContext, - @Nullable Attributes.Key clusterNameAttrKey) { + SslContext sslContext) { this.targetServiceAccounts = ImmutableList.copyOf(targetServiceAccounts); this.handshakerChannelPool = checkNotNull(handshakerChannelPool, "handshakerChannelPool"); this.sslContext = checkNotNull(sslContext, "sslContext"); - this.clusterNameAttrKey = clusterNameAttrKey; } @Override @@ -228,6 +228,26 @@ public final class AltsProtocolNegotiator { public int getDefaultPort() { return 443; } + + @SuppressWarnings("unchecked") + @Nullable + private static Attributes.Key loadClusterNameAttrKey() { + Attributes.Key key = null; + try { + Class klass = Class.forName("io.grpc.xds.InternalXdsAttributes"); + key = (Attributes.Key) klass.getField("ATTR_CLUSTER_NAME").get(null); + } catch (ClassNotFoundException e) { + logger.log(Level.FINE, + "Unable to load xDS endpoint cluster name key, this may be expected", e); + } catch (NoSuchFieldException e) { + logger.log(Level.FINE, + "Unable to load xDS endpoint cluster name key, this may be expected", e); + } catch (IllegalAccessException e) { + logger.log(Level.FINE, + "Unable to load xDS endpoint cluster name key, this may be expected", e); + } + return key; + } } private static final class GoogleDefaultProtocolNegotiator implements ProtocolNegotiator { diff --git a/alts/src/test/java/io/grpc/alts/internal/GoogleDefaultProtocolNegotiatorTest.java b/alts/src/test/java/io/grpc/alts/internal/GoogleDefaultProtocolNegotiatorTest.java index bdb1c215ca..740462be62 100644 --- a/alts/src/test/java/io/grpc/alts/internal/GoogleDefaultProtocolNegotiatorTest.java +++ b/alts/src/test/java/io/grpc/alts/internal/GoogleDefaultProtocolNegotiatorTest.java @@ -56,6 +56,7 @@ public final class GoogleDefaultProtocolNegotiatorTest { @RunWith(JUnit4.class) public abstract static class HandlerSelectionTest { private ProtocolNegotiator googleProtocolNegotiator; + private Attributes.Key originalClusterNameAttrKey; private final ObjectPool handshakerChannelPool = new ObjectPool() { @Override @@ -73,18 +74,22 @@ public final class GoogleDefaultProtocolNegotiatorTest { @Before public void setUp() throws Exception { SslContext sslContext = GrpcSslContexts.forClient().build(); - + originalClusterNameAttrKey = + AltsProtocolNegotiator.GoogleDefaultProtocolNegotiatorFactory.clusterNameAttrKey; + AltsProtocolNegotiator.GoogleDefaultProtocolNegotiatorFactory.clusterNameAttrKey = + getClusterNameAttrKey(); googleProtocolNegotiator = new AltsProtocolNegotiator.GoogleDefaultProtocolNegotiatorFactory( ImmutableList.of(), handshakerChannelPool, - sslContext, - getClusterNameAttrKey()) + sslContext) .newNegotiator(); } @After public void tearDown() { googleProtocolNegotiator.close(); + AltsProtocolNegotiator.GoogleDefaultProtocolNegotiatorFactory.clusterNameAttrKey = + originalClusterNameAttrKey; } @Nullable