Skip to content

Commit a8e081c

Browse files
authored
chore: Migrate away from GoogleCredential.fromStream() (#3925)
See b/437991832 for more information
1 parent d40e8d7 commit a8e081c

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientSettingsTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import com.google.api.gax.rpc.testing.FakeClientSettings;
4545
import com.google.auth.Credentials;
4646
import com.google.auth.oauth2.GoogleCredentials;
47+
import com.google.auth.oauth2.ServiceAccountCredentials;
4748
import com.google.common.collect.ImmutableMap;
4849
import com.google.common.truth.Truth;
4950
import java.io.ByteArrayInputStream;
@@ -69,7 +70,7 @@ class ClientSettingsTest {
6970
"quota_project_id_from_credentials";
7071
private static final String QUOTA_PROJECT_ID_FROM_CONTEXT =
7172
"quota_project_id_from_client_context";
72-
private static final String JSON_KEY_QUOTA_PROJECT_ID =
73+
private static final String SA_JSON_KEY_QUOTA_PROJECT_ID =
7374
"{\n"
7475
+ " \"private_key_id\": \"somekeyid\",\n"
7576
+ " \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggS"
@@ -102,7 +103,7 @@ class ClientSettingsTest {
102103
+ "}";
103104

104105
private static final GoogleCredentials credentialsWithQuotaProject =
105-
loadCredentials(JSON_KEY_QUOTA_PROJECT_ID);
106+
loadServiceAccountCredentials(SA_JSON_KEY_QUOTA_PROJECT_ID);
106107

107108
@Test
108109
void testEmptyBuilder() throws Exception {
@@ -321,10 +322,10 @@ void testApplyToAllUnaryMethods() throws Exception {
321322
.containsExactly(StatusCode.Code.DEADLINE_EXCEEDED);
322323
}
323324

324-
static GoogleCredentials loadCredentials(String credentialFile) {
325+
static GoogleCredentials loadServiceAccountCredentials(String serviceAccountCredentialFile) {
325326
try {
326-
InputStream keyStream = new ByteArrayInputStream(credentialFile.getBytes());
327-
return GoogleCredentials.fromStream(keyStream);
327+
InputStream keyStream = new ByteArrayInputStream(serviceAccountCredentialFile.getBytes());
328+
return ServiceAccountCredentials.fromStream(keyStream);
328329
} catch (IOException e) {
329330
fail("Couldn't create fake JSON credentials.");
330331
}

java-core/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.google.api.core.ApiClock;
3939
import com.google.api.core.CurrentMillisClock;
4040
import com.google.auth.oauth2.GoogleCredentials;
41+
import com.google.auth.oauth2.ServiceAccountCredentials;
4142
import com.google.cloud.spi.ServiceRpcFactory;
4243
import com.google.common.collect.ArrayListMultimap;
4344
import com.google.common.collect.Multimap;
@@ -58,7 +59,7 @@ class ServiceOptionsTest {
5859
private static GoogleCredentials credentialsWithQuotaProject;
5960
private static GoogleCredentials credentialsNotInGDU;
6061

61-
private static final String JSON_KEY =
62+
private static final String SA_JSON_KEY =
6263
"{\n"
6364
+ " \"private_key_id\": \"somekeyid\",\n"
6465
+ " \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggS"
@@ -87,7 +88,7 @@ class ServiceOptionsTest {
8788
+ " \"universe_domain\": \"googleapis.com\"\n"
8889
+ "}";
8990

90-
private static final String JSON_KEY_PROJECT_ID =
91+
private static final String SA_JSON_KEY_PROJECT_ID =
9192
"{\n"
9293
+ " \"private_key_id\": \"somekeyid\",\n"
9394
+ " \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggS"
@@ -117,7 +118,7 @@ class ServiceOptionsTest {
117118
+ " \"universe_domain\": \"googleapis.com\"\n"
118119
+ "}";
119120

120-
private static final String JSON_KEY_QUOTA_PROJECT_ID =
121+
private static final String SA_JSON_KEY_QUOTA_PROJECT_ID =
121122
"{\n"
122123
+ " \"private_key_id\": \"somekeyid\",\n"
123124
+ " \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggS"
@@ -149,7 +150,7 @@ class ServiceOptionsTest {
149150
+ "}";
150151

151152
// Key added by copying the keys above and adding in the universe domain field
152-
private static final String JSON_KEY_NON_GDU =
153+
private static final String SA_JSON_KEY_NON_GDU =
153154
"{\n"
154155
+ " \"private_key_id\": \"somekeyid\",\n"
155156
+ " \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggS"
@@ -179,16 +180,16 @@ class ServiceOptionsTest {
179180
+ "}";
180181

181182
static {
182-
credentials = loadCredentials(JSON_KEY);
183-
credentialsWithProjectId = loadCredentials(JSON_KEY_PROJECT_ID);
184-
credentialsWithQuotaProject = loadCredentials(JSON_KEY_QUOTA_PROJECT_ID);
185-
credentialsNotInGDU = loadCredentials(JSON_KEY_NON_GDU);
183+
credentials = loadServiceAccountCredentials(SA_JSON_KEY);
184+
credentialsWithProjectId = loadServiceAccountCredentials(SA_JSON_KEY_PROJECT_ID);
185+
credentialsWithQuotaProject = loadServiceAccountCredentials(SA_JSON_KEY_QUOTA_PROJECT_ID);
186+
credentialsNotInGDU = loadServiceAccountCredentials(SA_JSON_KEY_NON_GDU);
186187
}
187188

188-
static GoogleCredentials loadCredentials(String credentialFile) {
189+
static GoogleCredentials loadServiceAccountCredentials(String credentialFile) {
189190
try {
190191
InputStream keyStream = new ByteArrayInputStream(credentialFile.getBytes());
191-
return GoogleCredentials.fromStream(keyStream);
192+
return ServiceAccountCredentials.fromStream(keyStream);
192193
} catch (IOException e) {
193194
fail("Couldn't create fake JSON credentials.");
194195
}

0 commit comments

Comments
 (0)