Skip to content

Commit 94d0474

Browse files
lqiu96cloud-java-botsakthivelmaniirahul2393
authored
fix: Migrate away from GoogleCredentials.fromStream() usages (#4151)
* fix: Migrate away from GoogleCredentials.fromStream() usages * chore: generate libraries at Wed Oct 8 17:59:33 UTC 2025 * chore: generate libraries at Fri Oct 10 04:46:45 UTC 2025 * chore: Resolve issue with Paths.get() * chore: Add comment for fixed test --------- Co-authored-by: cloud-java-bot <cloud-java-bot@google.com> Co-authored-by: Sakthivel Subramanian <179120858+sakthivelmanii@users.noreply.github.com> Co-authored-by: rahul2393 <irahul@google.com>
1 parent b9d7daf commit 94d0474

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/CredentialsServiceTest.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@
2525
import com.google.auth.oauth2.ServiceAccountCredentials;
2626
import com.google.cloud.spanner.ErrorCode;
2727
import com.google.cloud.spanner.SpannerException;
28-
import java.io.FileInputStream;
28+
import java.io.File;
2929
import java.io.IOException;
30+
import java.nio.file.Files;
3031
import org.junit.Test;
3132
import org.junit.runner.RunWith;
3233
import org.junit.runners.JUnit4;
@@ -35,9 +36,9 @@
3536
@RunWith(JUnit4.class)
3637
public class CredentialsServiceTest {
3738
private static final String FILE_TEST_PATH =
38-
CredentialsServiceTest.class.getResource("test-key.json").getFile();
39-
private static final String APP_DEFAULT_FILE_TEST_PATH =
40-
CredentialsServiceTest.class.getResource("test-key-app-default.json").getFile();
39+
CredentialsServiceTest.class.getResource("test-key.json").getPath();
40+
private static final String SA_APP_DEFAULT_FILE_TEST_PATH =
41+
CredentialsServiceTest.class.getResource("test-key-app-default.json").getPath();
4142

4243
private static final String TEST_PROJECT_ID = "test-project";
4344
private static final String APP_DEFAULT_PROJECT_ID = "app-default-test-project";
@@ -49,7 +50,11 @@ public class CredentialsServiceTest {
4950
GoogleCredentials internalGetApplicationDefault() throws IOException {
5051
// Read application default credentials directly from a specific file instead of actually
5152
// fetching the default from the environment.
52-
return GoogleCredentials.fromStream(new FileInputStream(APP_DEFAULT_FILE_TEST_PATH));
53+
return ServiceAccountCredentials.fromStream(
54+
// Calling `getResource().getPath()` on Windows returns a string that might start with
55+
// something like `/C:/...`. Paths.get() interprets the leading / as part of the path
56+
// and would be invalid. Use `new File().toPath()` to read from these files.
57+
Files.newInputStream(new File(SA_APP_DEFAULT_FILE_TEST_PATH).toPath()));
5358
}
5459
};
5560

google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITVPCNegativeTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
import com.google.common.base.Strings;
4949
import com.google.longrunning.OperationsClient;
5050
import com.google.longrunning.OperationsSettings;
51-
import java.io.FileInputStream;
5251
import java.io.IOException;
5352
import java.util.ArrayList;
5453
import java.util.Collections;
@@ -352,9 +351,7 @@ public void deniedListBackupOperations() throws IOException {
352351
.setTransportChannelProvider(InstantiatingGrpcChannelProvider.newBuilder().build())
353352
.setEndpoint("spanner.googleapis.com:443")
354353
.setCredentialsProvider(
355-
FixedCredentialsProvider.create(
356-
GoogleCredentials.fromStream(
357-
new FileInputStream(System.getenv("GOOGLE_APPLICATION_CREDENTIALS")))))
354+
FixedCredentialsProvider.create(GoogleCredentials.getApplicationDefault()))
358355
.build())) {
359356
client.listOperations(backupId.getName() + "/operations", "");
360357
fail("Expected PermissionDeniedException");

0 commit comments

Comments
 (0)