Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add scope for Credentials defined by a JSON file #1907

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}