From 5eb77b450a925d7c5846fe3966e246182380a4f3 Mon Sep 17 00:00:00 2001 From: Tatiane Tosta <91583351+ttosta-google@users.noreply.github.com> Date: Wed, 28 Feb 2024 13:24:20 -0500 Subject: [PATCH] fix: Use GoogleCredentials to fetch user credentials for Cloud SQL (#2644) Co-authored-by: Taras Zubrei --- .../autoconfigure/sql/SqlCredentialFactory.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/sql/SqlCredentialFactory.java b/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/sql/SqlCredentialFactory.java index 83df80069a..13a40f8f9f 100644 --- a/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/sql/SqlCredentialFactory.java +++ b/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/sql/SqlCredentialFactory.java @@ -16,8 +16,9 @@ package com.google.cloud.spring.autoconfigure.sql; -import com.google.api.client.auth.oauth2.Credential; -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +import com.google.api.client.http.HttpRequestInitializer; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import com.google.cloud.spring.core.GcpScope; import com.google.cloud.sql.CredentialFactory; import java.io.ByteArrayInputStream; @@ -46,7 +47,12 @@ public class SqlCredentialFactory implements CredentialFactory { private static final Log LOGGER = LogFactory.getLog(SqlCredentialFactory.class); @Override - public Credential create() { + public HttpRequestInitializer create() { + return new HttpCredentialsAdapter(getCredentials()); + } + + @Override + public GoogleCredentials getCredentials() { String credentialResourceLocation = System.getProperty(CREDENTIAL_LOCATION_PROPERTY_NAME); String encodedCredential = System.getProperty(CREDENTIAL_ENCODED_KEY_PROPERTY_NAME); @@ -72,7 +78,7 @@ public Credential create() { credentialsInputStream = new FileInputStream(credentialResourceLocation); } - return GoogleCredential.fromStream(credentialsInputStream) + return GoogleCredentials.fromStream(credentialsInputStream) .createScoped(Collections.singleton(GcpScope.SQLADMIN.getUrl())); } catch (IOException ioe) { LOGGER.warn("There was an error loading Cloud SQL credential.", ioe);