From 03300cb2debb6b31f23d88f0f99eabcbe9ce4069 Mon Sep 17 00:00:00 2001 From: Eric Anderson Date: Fri, 7 Dec 2018 14:34:11 -0800 Subject: [PATCH] alts: Eagerly add interceptor in GoogleDefaultChannelBuilder This allows calling build() multiple times as well as prevents other interceptors from being able to detect that we've implemented CallCredential attachment via an interceptor. (Previously they could have set their own CallCredentials which would have overridden the default creds.) --- .../alts/GoogleDefaultChannelBuilder.java | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/alts/src/main/java/io/grpc/alts/GoogleDefaultChannelBuilder.java b/alts/src/main/java/io/grpc/alts/GoogleDefaultChannelBuilder.java index 8a5ece69a2..0c66b99af1 100644 --- a/alts/src/main/java/io/grpc/alts/GoogleDefaultChannelBuilder.java +++ b/alts/src/main/java/io/grpc/alts/GoogleDefaultChannelBuilder.java @@ -60,6 +60,17 @@ public final class GoogleDefaultChannelBuilder delegate = NettyChannelBuilder.forTarget(target); InternalNettyChannelBuilder.setProtocolNegotiatorFactory( delegate(), new ProtocolNegotiatorFactory()); + @Nullable CallCredentials credentials = null; + Status status = Status.OK; + try { + credentials = MoreCallCredentials.from(GoogleCredentials.getApplicationDefault()); + } catch (IOException e) { + status = + Status.UNAUTHENTICATED + .withDescription("Failed to get Google default credentials") + .withCause(e); + } + delegate().intercept(new GoogleDefaultInterceptor(credentials, status)); } /** "Overrides" the static method in {@link ManagedChannelBuilder}. */ @@ -77,21 +88,6 @@ public final class GoogleDefaultChannelBuilder return delegate; } - @Override - public ManagedChannel build() { - @Nullable CallCredentials credentials = null; - Status status = Status.OK; - try { - credentials = MoreCallCredentials.from(GoogleCredentials.getApplicationDefault()); - } catch (IOException e) { - status = - Status.UNAUTHENTICATED - .withDescription("Failed to get Google default credentials") - .withCause(e); - } - return delegate().intercept(new GoogleDefaultInterceptor(credentials, status)).build(); - } - @VisibleForTesting GoogleDefaultProtocolNegotiator getProtocolNegotiatorForTest() { return negotiatorForTest;