1616
1717package com .google .cloud .storage .testing ;
1818
19+ import com .google .api .core .ObsoleteApi ;
1920import com .google .api .gax .paging .Page ;
2021import com .google .api .gax .retrying .RetrySettings ;
2122import 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