Skip to content

Commit

Permalink
fix: Add scope for Credentials defined by a JSON file (#1907)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttosta-google authored Mar 18, 2024
1 parent 3c14e01 commit e8377c3
Showing 1 changed file with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
package com.google.cloud.sql.core;

import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.services.sqladmin.SQLAdminScopes;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.sql.CredentialFactory;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;

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

@Override
public GoogleCredentials getCredentials() {
GoogleCredentials credentials;
try {
return GoogleCredentials.fromStream(new FileInputStream(path));
credentials = GoogleCredentials.fromStream(new FileInputStream(path));
} catch (IOException e) {
throw new IllegalStateException("Unable to load GoogleCredentials from file " + path, e);
}

if (credentials.createScopedRequired()) {
credentials =
credentials.createScoped(
Arrays.asList(SQLAdminScopes.SQLSERVICE_ADMIN, SQLAdminScopes.CLOUD_PLATFORM));
}

return credentials;
}
}

0 comments on commit e8377c3

Please sign in to comment.