Skip to content

Commit 7e42c2f

Browse files
authored
fix: migrate away from GoogleCredentials.fromStream() usages (googleapis#3339)
1 parent 792ea71 commit 7e42c2f

File tree

1 file changed

+47
-13
lines changed

1 file changed

+47
-13
lines changed

google-cloud-storage/src/main/java/com/google/cloud/storage/testing/RemoteStorageHelper.java

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.cloud.storage.testing;
1818

19+
import com.google.api.core.ObsoleteApi;
1920
import com.google.api.gax.paging.Page;
2021
import com.google.api.gax.retrying.RetrySettings;
2122
import com.google.auth.oauth2.GoogleCredentials;
@@ -186,7 +187,27 @@ public static String generateBucketName() {
186187
}
187188

188189
/**
189-
* Creates a {@code RemoteStorageHelper} object for the given project id and JSON key input
190+
* This method is obsolete because of a potential security risk. Use the {@link #create(String,
191+
* GoogleCredentials)} method instead.
192+
*
193+
* <p>If you know that you will be loading credential configurations of a specific type, it is
194+
* recommended to use a credential-type-specific `fromStream()` method. This will ensure that an
195+
* unexpected credential type with potential for malicious intent is not loaded unintentionally.
196+
* You might still have to do validation for certain credential types. Please follow the
197+
* recommendation for that method.
198+
*
199+
* <p>If you are loading your credential configuration from an untrusted source and have not
200+
* mitigated the risks (e.g. by validating the configuration yourself), make these changes as soon
201+
* as possible to prevent security risks to your environment.
202+
*
203+
* <p>Regardless of the method used, it is always your responsibility to validate configurations
204+
* received from external sources.
205+
*
206+
* <p>See the {@see <a
207+
* href="https://cloud.google.com/docs/authentication/external/externally-sourced-credentials">documentation</a>}
208+
* for more details.
209+
*
210+
* <p>Creates a {@code RemoteStorageHelper} object for the given project id and JSON key input
190211
* stream.
191212
*
192213
* @param projectId id of the project to be used for running the tests
@@ -195,21 +216,12 @@ public static String generateBucketName() {
195216
* @throws com.google.cloud.storage.testing.RemoteStorageHelper.StorageHelperException if {@code
196217
* keyStream} is not a valid JSON key stream
197218
*/
219+
@ObsoleteApi(
220+
"This method is obsolete because of a potential security risk. Use the create() variant with Credential parameter instead")
198221
public static RemoteStorageHelper create(String projectId, InputStream keyStream)
199222
throws StorageHelperException {
200223
try {
201-
HttpTransportOptions transportOptions =
202-
HttpStorageOptions.defaults().getDefaultTransportOptions();
203-
transportOptions =
204-
transportOptions.toBuilder().setConnectTimeout(60000).setReadTimeout(60000).build();
205-
StorageOptions storageOptions =
206-
StorageOptions.http()
207-
.setCredentials(GoogleCredentials.fromStream(keyStream))
208-
.setProjectId(projectId)
209-
.setRetrySettings(retrySettings())
210-
.setTransportOptions(transportOptions)
211-
.build();
212-
return new RemoteStorageHelper(storageOptions);
224+
return create(projectId, GoogleCredentials.fromStream(keyStream));
213225
} catch (IOException ex) {
214226
if (log.isLoggable(Level.WARNING)) {
215227
log.log(Level.WARNING, ex.getMessage());
@@ -218,6 +230,28 @@ public static RemoteStorageHelper create(String projectId, InputStream keyStream
218230
}
219231
}
220232

233+
/**
234+
* Creates a {@code RemoteStorageHelper} object for the given project id and Credential.
235+
*
236+
* @param projectId id of the project to be used for running the tests
237+
* @param credentials GoogleCredential to set to StorageOptions
238+
* @return A {@code RemoteStorageHelper} object for the provided options
239+
*/
240+
public static RemoteStorageHelper create(String projectId, GoogleCredentials credentials) {
241+
HttpTransportOptions transportOptions =
242+
HttpStorageOptions.defaults().getDefaultTransportOptions();
243+
transportOptions =
244+
transportOptions.toBuilder().setConnectTimeout(60000).setReadTimeout(60000).build();
245+
StorageOptions storageOptions =
246+
StorageOptions.http()
247+
.setCredentials(credentials)
248+
.setProjectId(projectId)
249+
.setRetrySettings(retrySettings())
250+
.setTransportOptions(transportOptions)
251+
.build();
252+
return new RemoteStorageHelper(storageOptions);
253+
}
254+
221255
/**
222256
* Creates a {@code RemoteStorageHelper} object using default project id and authentication
223257
* credentials.

0 commit comments

Comments
 (0)