File tree 6 files changed +35
-7
lines changed
alloydb-jdbc-connector/src/main/java/com/google/cloud/alloydb
6 files changed +35
-7
lines changed Original file line number Diff line number Diff line change 18
18
19
19
import com .google .api .gax .core .FixedCredentialsProvider ;
20
20
import com .google .auth .oauth2 .GoogleCredentials ;
21
+ import java .util .Arrays ;
21
22
22
23
class ConstantCredentialFactory implements CredentialFactory {
23
24
24
25
private final GoogleCredentials credentials ;
25
26
26
27
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
+ }
28
33
}
29
34
30
35
@ Override
Original file line number Diff line number Diff line change 20
20
import com .google .auth .oauth2 .GoogleCredentials ;
21
21
22
22
interface CredentialFactory {
23
+ static final String SCOPE_CLOUD_PLATFORM = "https://www.googleapis.com/auth/cloud-platform" ;
24
+
23
25
default FixedCredentialsProvider create () {
24
26
return FixedCredentialsProvider .create (getCredentials ());
25
27
}
Original file line number Diff line number Diff line change 18
18
19
19
import com .google .auth .oauth2 .GoogleCredentials ;
20
20
import java .io .IOException ;
21
+ import java .util .Arrays ;
21
22
22
23
class DefaultCredentialFactory implements CredentialFactory {
23
24
@ Override
24
25
public GoogleCredentials getCredentials () {
26
+ GoogleCredentials credentials ;
25
27
try {
26
- return GoogleCredentials .getApplicationDefault ();
28
+ credentials = GoogleCredentials .getApplicationDefault ();
27
29
} catch (IOException e ) {
28
30
throw new RuntimeException ("failed to retrieve OAuth2 access token" , e );
29
31
}
32
+
33
+ if (credentials .createScopedRequired ()) {
34
+ credentials = credentials .createScoped (Arrays .asList (SCOPE_CLOUD_PLATFORM ));
35
+ }
36
+
37
+ return credentials ;
30
38
}
31
39
}
Original file line number Diff line number Diff line change 19
19
import com .google .auth .oauth2 .GoogleCredentials ;
20
20
import java .io .FileInputStream ;
21
21
import java .io .IOException ;
22
+ import java .util .Arrays ;
22
23
23
24
class FileCredentialFactory implements CredentialFactory {
24
25
private final String path ;
@@ -29,10 +30,17 @@ class FileCredentialFactory implements CredentialFactory {
29
30
30
31
@ Override
31
32
public GoogleCredentials getCredentials () {
33
+ GoogleCredentials credentials ;
32
34
try {
33
- return GoogleCredentials .fromStream (new FileInputStream (path ));
35
+ credentials = GoogleCredentials .fromStream (new FileInputStream (path ));
34
36
} catch (IOException e ) {
35
37
throw new IllegalStateException ("Unable to load GoogleCredentials from file " + path , e );
36
38
}
39
+
40
+ if (credentials .createScopedRequired ()) {
41
+ credentials = credentials .createScoped (Arrays .asList (SCOPE_CLOUD_PLATFORM ));
42
+ }
43
+
44
+ return credentials ;
37
45
}
38
46
}
Original file line number Diff line number Diff line change 38
38
*/
39
39
class ServiceAccountImpersonatingCredentialFactory implements CredentialFactory {
40
40
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" ;
43
41
private final CredentialFactory source ;
44
42
private final List <String > delegates ;
45
43
private final String targetPrincipal ;
@@ -70,7 +68,7 @@ public GoogleCredentials getCredentials() {
70
68
.setSourceCredentials (credentials )
71
69
.setTargetPrincipal (targetPrincipal )
72
70
.setDelegates (this .delegates )
73
- .setScopes (Arrays .asList (ALLOYDB_LOGIN , CLOUD_PLATFORM ))
71
+ .setScopes (Arrays .asList (SCOPE_CLOUD_PLATFORM ))
74
72
.build ();
75
73
return credentials ;
76
74
}
Original file line number Diff line number Diff line change 17
17
package com .google .cloud .alloydb ;
18
18
19
19
import com .google .auth .oauth2 .GoogleCredentials ;
20
+ import java .util .Arrays ;
20
21
import java .util .function .Supplier ;
21
22
22
23
class SupplierCredentialFactory implements CredentialFactory {
@@ -29,6 +30,12 @@ public SupplierCredentialFactory(Supplier<GoogleCredentials> supplier) {
29
30
30
31
@ Override
31
32
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 ;
33
40
}
34
41
}
You can’t perform that action at this time.
0 commit comments