googleapis/java-storage-nio

NPE in requesterPays check if credentials don't match provided user project

lbergelson opened this issue · 0 comments

The last release introduced a new NullPointerException when trying to use an invalid service account to access a requester pays bucket. The null pointer hides the actual cause of the error which makes it hard to debug. (Although the cause is not particularly clear from the original error message anyway)

Expect to see something like this:

Error getting access token for service account: 400 Bad Request
POST https://oauth2.googleapis.com/token
{"error":"invalid_grant","error_description":"Invalid JWT Signature."}, iss: 806222273987-uilktks3j6i7962rp0v7nusveer58497@developer.gserviceaccount.com
com.google.cloud.storage.StorageException: Error getting access token for service account: 400 Bad Request
....

But instead you get


java.lang.NullPointerException
	at com.google.cloud.storage.contrib.nio.CloudStorageFileSystemProvider.requesterPays(CloudStorageFileSystemProvider.java:970)
	at com.google.cloud.storage.contrib.nio.CloudStorageFileSystem.<init>(CloudStorageFileSystem.java:209)
	at com.google.cloud.storage.contrib.nio.CloudStorageFileSystem.forBucket(CloudStorageFileSystem.java:196)
	at com.google.cloud.storage.contrib.nio.CloudStorageFileSystem.forBucket(CloudStorageFileSystem.java:164)
	at com.google.cloud.storage.contrib.nio.CloudStorageFileSystem.forBucket(CloudStorageFileSystem.java:154)
	at org.broadinstitute.hellbender.utils.gcs.BucketUtils.getPathOnGcs(BucketUtils.java:410)
	at org.broadinstitute.hellbender.utils.io.IOUtils.getPath(IOUtils.java:773)
	at org.broadinstitute.hellbender.utils.gcs.BucketUtilsUnitTest.throwNPE(BucketUtilsUnitTest.java:421)

Environment details

  1. Cloud NIO
  2. OS type and version: all
  3. Java version: all
  4. version(s): 0.123.23

Steps to reproduce

  1. Attempt to check requester pays status of a bucket under these conditions:
    • correctly formatted but revoked service account credentials
    • autoDetectRequesterPays = true
  2. NPE

Code example

    @Test
    public void throwNPE() throws IOException {
            CloudStorageFileSystemProvider.setDefaultCloudStorageConfiguration(
                    CloudStorageConfiguration.builder()
                    .autoDetectRequesterPays(true)
                    .userProject("ANY_PROJECT_NAME")
                    .build());
            CloudStorageFileSystemProvider.setStorageOptions(
                    StorageOptions.newBuilder()
                            .setCredentials(ServiceAccountCredentials.fromStream(new FileInputStream(INVALID_KEY)))
                            .build()
            );
            final Path path = IOUtils.getPath(FILE_IN_REQUESTER_PAYS_BUCKET);
    }

Stack trace

java.lang.NullPointerException
	at com.google.cloud.storage.contrib.nio.CloudStorageFileSystemProvider.requesterPays(CloudStorageFileSystemProvider.java:970)
	at com.google.cloud.storage.contrib.nio.CloudStorageFileSystem.<init>(CloudStorageFileSystem.java:209)
	at com.google.cloud.storage.contrib.nio.CloudStorageFileSystem.forBucket(CloudStorageFileSystem.java:196)
	at com.google.cloud.storage.contrib.nio.CloudStorageFileSystem.forBucket(CloudStorageFileSystem.java:164)
	at com.google.cloud.storage.contrib.nio.CloudStorageFileSystem.forBucket(CloudStorageFileSystem.java:154)
	at org.broadinstitute.hellbender.utils.gcs.BucketUtils.getPathOnGcs(BucketUtils.java:410)
	at org.broadinstitute.hellbender.utils.io.IOUtils.getPath(IOUtils.java:773)
	at org.broadinstitute.hellbender.utils.gcs.BucketUtilsUnitTest.throwNPE(BucketUtilsUnitTest.java:421)
	...