ServiceOptions.getCredentials can return null and cause NPE in Logging and PubSub
In ServiceOptions.java is no credentials are explicitly specified, it would default to calling getApplicationDefault here: https://github.com/GoogleCloudPlatform/google-cloud-java/blob/master/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java#L312
But that method can fail by throwing an exception if for eg the credentials file is missing. In that case ServiceOptions just catches the exception, ignores it and returns null: https://github.com/GoogleCloudPlatform/google-cloud-java/blob/master/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java#L314
But reading the documentation on getCredentials, it is not clear that callers should be handling null return value. google-cloud-java seems to be not handling the null return value atleast in couple of places: https://github.com/GoogleCloudPlatform/google-cloud-java/blob/00add6e26fc2e1a533b715df28a5568222483f65/google-cloud-logging/src/main/java/com/google/cloud/logging/spi/DefaultLoggingRpc.java#L117 https://github.com/GoogleCloudPlatform/google-cloud-java/blob/00add6e26fc2e1a533b715df28a5568222483f65/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/DefaultPubSubRpc.java#L146
Re-opening because:
- #1955 was rolled back because integration tests failed
- #1955 only fixed Logging, not the rest of the APIs
WIth this fix (#1961) the NPE error for Logging should be fixed. New Pubsub shouldn't have this issue.