Skip to content

Commit 6498808

Browse files
authored
fix: Migrate away from GoogleCredentials.fromStream() usages (#11765)
See b/437991832 for more information
1 parent 537e2a0 commit 6498808

File tree

3 files changed

+51
-22
lines changed

3 files changed

+51
-22
lines changed

java-translate/google-cloud-translate/src/main/java/com/google/cloud/translate/testing/RemoteTranslateHelper.java

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

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

19+
import com.google.api.core.ObsoleteApi;
1920
import com.google.api.gax.retrying.RetrySettings;
2021
import com.google.auth.oauth2.GoogleCredentials;
2122
import com.google.cloud.http.HttpTransportOptions;
@@ -51,7 +52,27 @@ public TranslateOptions getOptions() {
5152
}
5253

5354
/**
54-
* Creates a {@code RemoteTranslateHelper} object for the given project id and JSON key input
55+
* This method is obsolete because of a potential security risk. Use the {@link #create(String,
56+
* GoogleCredentials)} method instead.
57+
*
58+
* <p>If you know that you will be loading credential configurations of a specific type, it is
59+
* recommended to use a credential-type-specific `fromStream()` method. This will ensure that an
60+
* unexpected credential type with potential for malicious intent is not loaded unintentionally.
61+
* You might still have to do validation for certain credential types. Please follow the
62+
* recommendation for that method.
63+
*
64+
* <p>If you are loading your credential configuration from an untrusted source and have not
65+
* mitigated the risks (e.g. by validating the configuration yourself), make these changes as soon
66+
* as possible to prevent security risks to your environment.
67+
*
68+
* <p>Regardless of the method used, it is always your responsibility to validate configurations
69+
* received from external sources.
70+
*
71+
* <p>See the {@link <a
72+
* href="https://cloud.google.com/docs/authentication/external/externally-sourced-credentials">documentation</a>}
73+
* for more details.
74+
*
75+
* <p>Creates a {@code RemoteTranslateHelper} object for the given project id and JSON key input
5576
* stream.
5677
*
5778
* @param projectId id of the project to be used for running the tests
@@ -60,20 +81,12 @@ public TranslateOptions getOptions() {
6081
* @throws com.google.cloud.translate.testing.RemoteTranslateHelper.TranslateHelperException if
6182
* {@code keyStream} is not a valid JSON key stream
6283
*/
84+
@ObsoleteApi(
85+
"This method is obsolete because of a potential security risk. Use the create() variant with Credential parameter instead")
6386
public static RemoteTranslateHelper create(String projectId, InputStream keyStream)
6487
throws TranslateHelperException {
6588
try {
66-
HttpTransportOptions transportOptions = TranslateOptions.getDefaultHttpTransportOptions();
67-
transportOptions =
68-
transportOptions.toBuilder().setConnectTimeout(60000).setReadTimeout(60000).build();
69-
TranslateOptions translateOptions =
70-
TranslateOptions.newBuilder()
71-
.setCredentials(GoogleCredentials.fromStream(keyStream))
72-
.setProjectId(projectId)
73-
.setRetrySettings(retryParams())
74-
.setTransportOptions(transportOptions)
75-
.build();
76-
return new RemoteTranslateHelper(translateOptions);
89+
return create(projectId, GoogleCredentials.fromStream(keyStream));
7790
} catch (IOException ex) {
7891
if (logger.isLoggable(Level.WARNING)) {
7992
logger.log(Level.WARNING, ex.getMessage());
@@ -82,6 +95,28 @@ public static RemoteTranslateHelper create(String projectId, InputStream keyStre
8295
}
8396
}
8497

98+
/**
99+
* Creates a {@code RemoteTranslateHelper} object for the given project id and JSON key input
100+
* stream.
101+
*
102+
* @param projectId id of the project to be used for running the tests
103+
* @param credentials GoogleCredential to set to TranslateOptions
104+
* @return A {@code RemoteTranslateHelper} object for the provided options
105+
*/
106+
public static RemoteTranslateHelper create(String projectId, GoogleCredentials credentials) {
107+
HttpTransportOptions transportOptions = TranslateOptions.getDefaultHttpTransportOptions();
108+
transportOptions =
109+
transportOptions.toBuilder().setConnectTimeout(60000).setReadTimeout(60000).build();
110+
TranslateOptions translateOptions =
111+
TranslateOptions.newBuilder()
112+
.setCredentials(credentials)
113+
.setProjectId(projectId)
114+
.setRetrySettings(retryParams())
115+
.setTransportOptions(transportOptions)
116+
.build();
117+
return new RemoteTranslateHelper(translateOptions);
118+
}
119+
85120
/**
86121
* Creates a {@code RemoteTranslateHelper} object for the given API key.
87122
*

java-websecurityscanner/google-cloud-websecurityscanner/src/test/java/com/google/cloud/websecurityscanner/it/v1beta/VPCServiceControlNegativeTest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import com.google.cloud.websecurityscanner.v1beta.WebSecurityScannerClient.ListScanConfigsPagedResponse;
3232
import com.google.cloud.websecurityscanner.v1beta.WebSecurityScannerSettings;
3333
import com.google.common.collect.Lists;
34-
import java.io.FileInputStream;
3534
import java.io.IOException;
3635
import org.joda.time.DateTime;
3736
import org.joda.time.DateTimeZone;
@@ -59,8 +58,6 @@ public class VPCServiceControlNegativeTest {
5958
private static final String IN_VPCSC_TEST = System.getenv(IN_VPCSC_GOOGLE_CLOUD_TEST_ENV);
6059
private static final String OUT_VPCSC_PROJECT = System.getenv(OUT_VPCSC_PROJECT_ENV);
6160
private static final String OUT_VPCSC_HOSTNAME = System.getenv(OUT_VPCSC_HOSTNAME_ENV);
62-
private static final String OUT_VPCSC_GOOGLE_CREDENTIAL =
63-
System.getenv(GOOGLE_CREDENTIAL_DEFAULT_ENV);
6461

6562
private String testScanConfigCreationDisplayName;
6663

@@ -88,7 +85,7 @@ public static void setUpClass() {
8885
GOOGLE_CREDENTIAL_DEFAULT_ENV
8986
+ " must be set to google application credentials "
9087
+ "that is outside VPCSC perimeter",
91-
isNotEmpty(OUT_VPCSC_GOOGLE_CREDENTIAL));
88+
isNotEmpty(System.getenv(GOOGLE_CREDENTIAL_DEFAULT_ENV)));
9289
}
9390

9491
@Before
@@ -101,7 +98,7 @@ public void setup() {
10198

10299
private WebSecurityScannerSettings getWssSettingWithCredentials() throws IOException {
103100
GoogleCredentials credentials =
104-
GoogleCredentials.fromStream(new FileInputStream(OUT_VPCSC_GOOGLE_CREDENTIAL))
101+
GoogleCredentials.getApplicationDefault()
105102
.createScoped(Lists.newArrayList(GOOGLE_API_CLOUD_PLATFORM_LINK));
106103
return WebSecurityScannerSettings.newBuilder()
107104
.setCredentialsProvider(FixedCredentialsProvider.create(credentials))

java-websecurityscanner/google-cloud-websecurityscanner/src/test/java/com/google/cloud/websecurityscanner/it/v1beta/VPCServiceControlPositiveTest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import com.google.cloud.websecurityscanner.v1beta.WebSecurityScannerClient;
3939
import com.google.cloud.websecurityscanner.v1beta.WebSecurityScannerSettings;
4040
import com.google.common.collect.Lists;
41-
import java.io.FileInputStream;
4241
import java.io.IOException;
4342
import org.joda.time.DateTime;
4443
import org.joda.time.DateTimeZone;
@@ -64,8 +63,6 @@ public class VPCServiceControlPositiveTest {
6463
private static final String IN_VPCSC_TEST = System.getenv(IN_VPCSC_GOOGLE_CLOUD_TEST_ENV);
6564
private static final String IN_VPCSC_PROJECT = System.getenv(IN_VPCSC_PROJECT_ENV);
6665
private static final String IN_VPCSC_HOSTNAME = System.getenv(IN_VPCSC_HOSTNAME_ENV);
67-
private static final String IN_VPCSC_GOOGLE_CREDENTIAL =
68-
System.getenv(GOOGLE_CREDENTIAL_DEFAULT_ENV);
6966

7067
private String test0DisplayName;
7168
private String test1DisplayName;
@@ -97,7 +94,7 @@ public static void setUpClass() {
9794
GOOGLE_CREDENTIAL_DEFAULT_ENV
9895
+ " environment variable needs to be set to "
9996
+ "google application credentials that resides inside VPCSC",
100-
isNotEmpty(IN_VPCSC_GOOGLE_CREDENTIAL));
97+
isNotEmpty(System.getenv(GOOGLE_CREDENTIAL_DEFAULT_ENV)));
10198
}
10299

103100
@Before
@@ -111,7 +108,7 @@ public void setUp() {
111108

112109
private WebSecurityScannerSettings getWssSettingWithCredentials() throws IOException {
113110
GoogleCredentials credentials =
114-
GoogleCredentials.fromStream(new FileInputStream(IN_VPCSC_GOOGLE_CREDENTIAL))
111+
GoogleCredentials.getApplicationDefault()
115112
.createScoped(Lists.newArrayList(GOOGLE_API_CLOUD_PLATFORM_LINK));
116113
return WebSecurityScannerSettings.newBuilder()
117114
.setCredentialsProvider(FixedCredentialsProvider.create(credentials))

0 commit comments

Comments
 (0)