Skip to content

Commit 5268ce3

Browse files
fix: Add scope for Credentials (#517)
1 parent 2c35502 commit 5268ce3

6 files changed

+35
-7
lines changed

alloydb-jdbc-connector/src/main/java/com/google/cloud/alloydb/ConstantCredentialFactory.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@
1818

1919
import com.google.api.gax.core.FixedCredentialsProvider;
2020
import com.google.auth.oauth2.GoogleCredentials;
21+
import java.util.Arrays;
2122

2223
class ConstantCredentialFactory implements CredentialFactory {
2324

2425
private final GoogleCredentials credentials;
2526

2627
public ConstantCredentialFactory(GoogleCredentials credentials) {
27-
this.credentials = credentials;
28+
if (credentials.createScopedRequired()) {
29+
this.credentials = credentials.createScoped(Arrays.asList(SCOPE_CLOUD_PLATFORM));
30+
} else {
31+
this.credentials = credentials;
32+
}
2833
}
2934

3035
@Override

alloydb-jdbc-connector/src/main/java/com/google/cloud/alloydb/CredentialFactory.java

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import com.google.auth.oauth2.GoogleCredentials;
2121

2222
interface CredentialFactory {
23+
static final String SCOPE_CLOUD_PLATFORM = "https://www.googleapis.com/auth/cloud-platform";
24+
2325
default FixedCredentialsProvider create() {
2426
return FixedCredentialsProvider.create(getCredentials());
2527
}

alloydb-jdbc-connector/src/main/java/com/google/cloud/alloydb/DefaultCredentialFactory.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,22 @@
1818

1919
import com.google.auth.oauth2.GoogleCredentials;
2020
import java.io.IOException;
21+
import java.util.Arrays;
2122

2223
class DefaultCredentialFactory implements CredentialFactory {
2324
@Override
2425
public GoogleCredentials getCredentials() {
26+
GoogleCredentials credentials;
2527
try {
26-
return GoogleCredentials.getApplicationDefault();
28+
credentials = GoogleCredentials.getApplicationDefault();
2729
} catch (IOException e) {
2830
throw new RuntimeException("failed to retrieve OAuth2 access token", e);
2931
}
32+
33+
if (credentials.createScopedRequired()) {
34+
credentials = credentials.createScoped(Arrays.asList(SCOPE_CLOUD_PLATFORM));
35+
}
36+
37+
return credentials;
3038
}
3139
}

alloydb-jdbc-connector/src/main/java/com/google/cloud/alloydb/FileCredentialFactory.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.auth.oauth2.GoogleCredentials;
2020
import java.io.FileInputStream;
2121
import java.io.IOException;
22+
import java.util.Arrays;
2223

2324
class FileCredentialFactory implements CredentialFactory {
2425
private final String path;
@@ -29,10 +30,17 @@ class FileCredentialFactory implements CredentialFactory {
2930

3031
@Override
3132
public GoogleCredentials getCredentials() {
33+
GoogleCredentials credentials;
3234
try {
33-
return GoogleCredentials.fromStream(new FileInputStream(path));
35+
credentials = GoogleCredentials.fromStream(new FileInputStream(path));
3436
} catch (IOException e) {
3537
throw new IllegalStateException("Unable to load GoogleCredentials from file " + path, e);
3638
}
39+
40+
if (credentials.createScopedRequired()) {
41+
credentials = credentials.createScoped(Arrays.asList(SCOPE_CLOUD_PLATFORM));
42+
}
43+
44+
return credentials;
3745
}
3846
}

alloydb-jdbc-connector/src/main/java/com/google/cloud/alloydb/ServiceAccountImpersonatingCredentialFactory.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@
3838
*/
3939
class ServiceAccountImpersonatingCredentialFactory implements CredentialFactory {
4040

41-
private static final String CLOUD_PLATFORM = "https://www.googleapis.com/auth/cloud-platform";
42-
private static final String ALLOYDB_LOGIN = "https://www.googleapis.com/auth/alloydb.login";
4341
private final CredentialFactory source;
4442
private final List<String> delegates;
4543
private final String targetPrincipal;
@@ -70,7 +68,7 @@ public GoogleCredentials getCredentials() {
7068
.setSourceCredentials(credentials)
7169
.setTargetPrincipal(targetPrincipal)
7270
.setDelegates(this.delegates)
73-
.setScopes(Arrays.asList(ALLOYDB_LOGIN, CLOUD_PLATFORM))
71+
.setScopes(Arrays.asList(SCOPE_CLOUD_PLATFORM))
7472
.build();
7573
return credentials;
7674
}

alloydb-jdbc-connector/src/main/java/com/google/cloud/alloydb/SupplierCredentialFactory.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.cloud.alloydb;
1818

1919
import com.google.auth.oauth2.GoogleCredentials;
20+
import java.util.Arrays;
2021
import java.util.function.Supplier;
2122

2223
class SupplierCredentialFactory implements CredentialFactory {
@@ -29,6 +30,12 @@ public SupplierCredentialFactory(Supplier<GoogleCredentials> supplier) {
2930

3031
@Override
3132
public GoogleCredentials getCredentials() {
32-
return supplier.get();
33+
GoogleCredentials credentials = supplier.get();
34+
35+
if (credentials.createScopedRequired()) {
36+
credentials = credentials.createScoped(Arrays.asList(SCOPE_CLOUD_PLATFORM));
37+
}
38+
39+
return credentials;
3340
}
3441
}

0 commit comments

Comments
 (0)