From 0383af9a70ef00165a5815b1744f7ad9de3b764d Mon Sep 17 00:00:00 2001 From: Neo Wu Date: Thu, 20 Apr 2017 14:33:55 -0700 Subject: [PATCH 1/3] fix credentials NPE bug --- .../src/main/java/com/google/cloud/ServiceOptions.java | 7 +++++++ .../com/google/cloud/logging/spi/v2/GrpcLoggingRpc.java | 3 +-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java b/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java index 0db8f08c4d76..10403b054c26 100644 --- a/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java +++ b/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java @@ -461,6 +461,13 @@ public Credentials getCredentials() { return credentials; } + public boolean credentialIsNoCredentialInstance() { + if (credentials != null) { + return credentials.equals(NoCredentials.getInstance()); + } + return false; + } + /** * Returns the authentication credentials. If required, credentials are scoped. */ diff --git a/google-cloud-logging/src/main/java/com/google/cloud/logging/spi/v2/GrpcLoggingRpc.java b/google-cloud-logging/src/main/java/com/google/cloud/logging/spi/v2/GrpcLoggingRpc.java index d32b6d385e90..6888e667693a 100644 --- a/google-cloud-logging/src/main/java/com/google/cloud/logging/spi/v2/GrpcLoggingRpc.java +++ b/google-cloud-logging/src/main/java/com/google/cloud/logging/spi/v2/GrpcLoggingRpc.java @@ -28,7 +28,6 @@ import com.google.api.gax.grpc.UnaryCallSettings; import com.google.cloud.GrpcTransportOptions; import com.google.cloud.GrpcTransportOptions.ExecutorFactory; -import com.google.cloud.NoCredentials; import com.google.cloud.logging.LoggingException; import com.google.cloud.logging.LoggingOptions; import com.google.logging.v2.CreateLogMetricRequest; @@ -82,7 +81,7 @@ public GrpcLoggingRpc(LoggingOptions options) throws IOException { ChannelProvider channelProvider; // todo(mziccard): ChannelProvider should support null/absent credentials for testing if (options.getHost().contains("localhost") - || options.getCredentials().equals(NoCredentials.getInstance())) { + || options.credentialIsNoCredentialInstance()) { ManagedChannel managedChannel = ManagedChannelBuilder.forTarget(options.getHost()) .usePlaintext(true) .executor(executor) From de224fbf3a9734b0fefdff47a8110d5f06d7dff0 Mon Sep 17 00:00:00 2001 From: Neo Wu Date: Thu, 20 Apr 2017 15:03:36 -0700 Subject: [PATCH 2/3] add comments --- .../src/main/java/com/google/cloud/ServiceOptions.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java b/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java index 10403b054c26..01446a14efb4 100644 --- a/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java +++ b/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java @@ -19,7 +19,6 @@ import static com.google.common.base.MoreObjects.firstNonNull; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; -import static java.nio.charset.StandardCharsets.UTF_8; import com.google.api.core.ApiClock; import com.google.api.core.CurrentMillisClock; @@ -37,12 +36,9 @@ import java.io.FileReader; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.io.ObjectInputStream; import java.io.Serializable; import java.lang.reflect.Method; -import java.net.HttpURLConnection; -import java.net.URL; import java.nio.charset.Charset; import java.util.Locale; import java.util.Objects; @@ -461,6 +457,10 @@ public Credentials getCredentials() { return credentials; } + /** + * Returns whether the credential is {@code NoCredentials} instance + * @return true if credential is a {@code NoCredentials} instance, false if not + */ public boolean credentialIsNoCredentialInstance() { if (credentials != null) { return credentials.equals(NoCredentials.getInstance()); From 3f2d3a26d858eb5ec3dc35c01a0ac686a91a1e78 Mon Sep 17 00:00:00 2001 From: Neo Wu Date: Thu, 20 Apr 2017 17:01:43 -0700 Subject: [PATCH 3/3] fix options.getCredentials() NPE --- .../java/com/google/cloud/ServiceOptions.java | 15 ++++----------- .../cloud/logging/spi/v2/GrpcLoggingRpc.java | 12 +++++++----- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java b/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java index 01446a14efb4..0db8f08c4d76 100644 --- a/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java +++ b/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java @@ -19,6 +19,7 @@ import static com.google.common.base.MoreObjects.firstNonNull; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; +import static java.nio.charset.StandardCharsets.UTF_8; import com.google.api.core.ApiClock; import com.google.api.core.CurrentMillisClock; @@ -36,9 +37,12 @@ import java.io.FileReader; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.io.ObjectInputStream; import java.io.Serializable; import java.lang.reflect.Method; +import java.net.HttpURLConnection; +import java.net.URL; import java.nio.charset.Charset; import java.util.Locale; import java.util.Objects; @@ -457,17 +461,6 @@ public Credentials getCredentials() { return credentials; } - /** - * Returns whether the credential is {@code NoCredentials} instance - * @return true if credential is a {@code NoCredentials} instance, false if not - */ - public boolean credentialIsNoCredentialInstance() { - if (credentials != null) { - return credentials.equals(NoCredentials.getInstance()); - } - return false; - } - /** * Returns the authentication credentials. If required, credentials are scoped. */ diff --git a/google-cloud-logging/src/main/java/com/google/cloud/logging/spi/v2/GrpcLoggingRpc.java b/google-cloud-logging/src/main/java/com/google/cloud/logging/spi/v2/GrpcLoggingRpc.java index 6888e667693a..6346773d5d2c 100644 --- a/google-cloud-logging/src/main/java/com/google/cloud/logging/spi/v2/GrpcLoggingRpc.java +++ b/google-cloud-logging/src/main/java/com/google/cloud/logging/spi/v2/GrpcLoggingRpc.java @@ -28,6 +28,7 @@ import com.google.api.gax.grpc.UnaryCallSettings; import com.google.cloud.GrpcTransportOptions; import com.google.cloud.GrpcTransportOptions.ExecutorFactory; +import com.google.cloud.NoCredentials; import com.google.cloud.logging.LoggingException; import com.google.cloud.logging.LoggingOptions; import com.google.logging.v2.CreateLogMetricRequest; @@ -81,11 +82,12 @@ public GrpcLoggingRpc(LoggingOptions options) throws IOException { ChannelProvider channelProvider; // todo(mziccard): ChannelProvider should support null/absent credentials for testing if (options.getHost().contains("localhost") - || options.credentialIsNoCredentialInstance()) { - ManagedChannel managedChannel = ManagedChannelBuilder.forTarget(options.getHost()) - .usePlaintext(true) - .executor(executor) - .build(); + || NoCredentials.getInstance().equals(options.getCredentials())) { + ManagedChannel managedChannel = + ManagedChannelBuilder.forTarget(options.getHost()) + .usePlaintext(true) + .executor(executor) + .build(); channelProvider = FixedChannelProvider.create(managedChannel); } else { channelProvider = GrpcTransportOptions.setUpChannelProvider(