Skip to content

Commit e8377c3

Browse files
fix: Add scope for Credentials defined by a JSON file (#1907)
1 parent 3c14e01 commit e8377c3

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

core/src/main/java/com/google/cloud/sql/core/FileCredentialFactory.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
package com.google.cloud.sql.core;
1818

1919
import com.google.api.client.http.HttpRequestInitializer;
20+
import com.google.api.services.sqladmin.SQLAdminScopes;
2021
import com.google.auth.http.HttpCredentialsAdapter;
2122
import com.google.auth.oauth2.GoogleCredentials;
2223
import com.google.cloud.sql.CredentialFactory;
2324
import java.io.FileInputStream;
2425
import java.io.IOException;
26+
import java.util.Arrays;
2527

2628
class FileCredentialFactory implements CredentialFactory {
2729
private final String path;
@@ -37,10 +39,19 @@ public HttpRequestInitializer create() {
3739

3840
@Override
3941
public GoogleCredentials getCredentials() {
42+
GoogleCredentials credentials;
4043
try {
41-
return GoogleCredentials.fromStream(new FileInputStream(path));
44+
credentials = GoogleCredentials.fromStream(new FileInputStream(path));
4245
} catch (IOException e) {
4346
throw new IllegalStateException("Unable to load GoogleCredentials from file " + path, e);
4447
}
48+
49+
if (credentials.createScopedRequired()) {
50+
credentials =
51+
credentials.createScoped(
52+
Arrays.asList(SQLAdminScopes.SQLSERVICE_ADMIN, SQLAdminScopes.CLOUD_PLATFORM));
53+
}
54+
55+
return credentials;
4556
}
4657
}

0 commit comments

Comments
 (0)