Skip to content

Commit 89a2fc1

Browse files
committed
---
yaml --- r: 6221 b: refs/heads/tswast-patch-1 c: 2171251 h: refs/heads/master i: 6219: 49ce0bc
1 parent 47f7bed commit 89a2fc1

3 files changed

Lines changed: 80 additions & 26 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ refs/tags/v0.18.0: 9d193c4c4b9d1c6f21515dd8e50836b9194ec9bb
5757
refs/tags/v0.19.0: e67b56e4d8dad5f9a7b38c9b2107c23c828f2ed5
5858
refs/tags/v0.20.0: 839f7fb7156535146aa1cb2c5aadd8d375d854e8
5959
refs/tags/v0.20.1: 370471f437f1f4f68a11e068df5cd6bf39edb1fa
60-
refs/heads/tswast-patch-1: c23b9a230a34af229d2bde99036a2bcd64505b04
60+
refs/heads/tswast-patch-1: 21712519d78240f7ef906b5fa4b7c4b076f91230
6161
refs/heads/pubsub-streaming-pull: 19262b752ee874eb2ca3b950eb2aef44d5a5267b

branches/tswast-patch-1/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.google.api.services.datastore.client.DatastoreException;
3333
import com.google.api.services.datastore.client.DatastoreFactory;
3434
import com.google.api.services.datastore.client.DatastoreOptions.Builder;
35+
import com.google.common.base.Preconditions;
3536
import com.google.common.collect.ImmutableMap;
3637
import com.google.gcloud.datastore.DatastoreOptions;
3738
import com.google.gcloud.spi.DatastoreRpc.DatastoreRpcException.Reason;
@@ -40,6 +41,10 @@
4041
import org.json.JSONObject;
4142
import org.json.JSONTokener;
4243

44+
import java.net.InetAddress;
45+
import java.net.MalformedURLException;
46+
import java.net.URL;
47+
import java.net.UnknownHostException;
4348
import java.util.HashMap;
4449
import java.util.Map;
4550

@@ -62,14 +67,45 @@ public class DefaultDatastoreRpc implements DatastoreRpc {
6267
}
6368

6469
public DefaultDatastoreRpc(DatastoreOptions options) {
70+
String normalizedHost = normalizeHost(options.host());
6571
client = DatastoreFactory.get().create(
6672
new Builder()
6773
.dataset(options.projectId())
68-
.host(options.host())
74+
.host(normalizedHost)
6975
.initializer(options.httpRequestInitializer())
7076
.build());
7177
}
7278

79+
private static String normalizeHost(String host) {
80+
host = host.toLowerCase();
81+
if (includesScheme(host)) {
82+
Preconditions.checkArgument(!(host.startsWith("https://") && isLocalHost(host)),
83+
"\"https\" is not supported for localhost. Use \"http\" instead.");
84+
return host;
85+
}
86+
return "http://" + host;
87+
}
88+
89+
private static boolean isLocalHost(String host) {
90+
if (host != null) {
91+
try {
92+
String normalizedHost = host;
93+
if (!includesScheme(normalizedHost)) {
94+
normalizedHost = "http://" + normalizedHost;
95+
}
96+
InetAddress hostAddr = InetAddress.getByName(new URL(normalizedHost).getHost());
97+
return hostAddr.isAnyLocalAddress() || hostAddr.isLoopbackAddress();
98+
} catch (UnknownHostException | MalformedURLException e) {
99+
// ignore
100+
}
101+
}
102+
return false;
103+
}
104+
105+
private static boolean includesScheme(String url) {
106+
return url.startsWith("http://") || url.startsWith("https://");
107+
}
108+
73109
private static DatastoreRpcException translate(DatastoreException exception) {
74110
String message = exception.getMessage();
75111
String reasonStr = "";

branches/tswast-patch-1/gcloud-java-storage/src/test/java/com/google/gcloud/storage/RemoteGcsHelper.java

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.common.collect.ImmutableMap;
2020
import com.google.gcloud.AuthCredentials;
21+
import com.google.gcloud.RetryParams;
2122
import com.google.gcloud.storage.RemoteGcsHelper.Option.KeyFromClasspath;
2223

2324
import java.io.FileInputStream;
@@ -92,50 +93,37 @@ public static String generateBucketName() {
9293
}
9394

9495
/**
95-
* Creates a {@code RemoteGcsHelper} object.
96+
* Creates a {@code RemoteGcsHelper} object for the given project id and JSON key path.
9697
*
98+
* @param projectId id of the project to be used for running the tests
99+
* @param keyPath path to the JSON key to be used for running the tests
97100
* @param options creation options
98101
* @return A {@code RemoteGcsHelper} object for the provided options.
99-
* @throws com.google.gcloud.storage.RemoteGcsHelper.GcsHelperException if environment variables
100-
* {@code GCLOUD_TESTS_PROJECT_ID} and {@code GCLOUD_TESTS_KEY} are not set or if the file
101-
* pointed by {@code GCLOUD_TESTS_KEY} does not exist
102+
* @throws com.google.gcloud.storage.RemoteGcsHelper.GcsHelperException if the file pointed by
103+
* {@code keyPath} does not exist
102104
*/
103-
public static RemoteGcsHelper create(Option... options) throws GcsHelperException {
105+
public static RemoteGcsHelper create(String projectId, String keyPath, Option... options)
106+
throws GcsHelperException {
104107
boolean keyFromClassPath = false;
105108
Map<Class<? extends Option>, Option> optionsMap = Option.asImmutableMap(options);
106109
if (optionsMap.containsKey(KeyFromClasspath.class)) {
107110
keyFromClassPath =
108111
((KeyFromClasspath) optionsMap.get(KeyFromClasspath.class)).keyFromClasspath();
109112
}
110-
String projectId = System.getenv(PROJECT_ID_ENV_VAR);
111-
String stringKeyPath = System.getenv(PRIVATE_KEY_ENV_VAR);
112-
if (projectId == null) {
113-
String message = "Environment variable " + PROJECT_ID_ENV_VAR + " not set";
114-
if (log.isLoggable(Level.WARNING)) {
115-
log.log(Level.WARNING, message);
116-
}
117-
throw new GcsHelperException(message);
118-
}
119-
if (stringKeyPath == null) {
120-
String message = "Environment variable " + PRIVATE_KEY_ENV_VAR + " not set";
121-
if (log.isLoggable(Level.WARNING)) {
122-
log.log(Level.WARNING, message);
123-
}
124-
throw new GcsHelperException(message);
125-
}
126113
try {
127114
InputStream keyFileStream;
128115
if (keyFromClassPath) {
129-
keyFileStream = RemoteGcsHelper.class.getResourceAsStream(stringKeyPath);
116+
keyFileStream = RemoteGcsHelper.class.getResourceAsStream(keyPath);
130117
if (keyFileStream == null) {
131-
throw new FileNotFoundException(stringKeyPath + " not found in classpath");
118+
throw new FileNotFoundException(keyPath + " not found in classpath");
132119
}
133120
} else {
134-
keyFileStream = new FileInputStream(stringKeyPath);
121+
keyFileStream = new FileInputStream(keyPath);
135122
}
136123
StorageOptions storageOptions = StorageOptions.builder()
137124
.authCredentials(AuthCredentials.createForJson(keyFileStream))
138125
.projectId(projectId)
126+
.retryParams(RetryParams.getDefaultInstance())
139127
.build();
140128
return new RemoteGcsHelper(storageOptions);
141129
} catch (FileNotFoundException ex) {
@@ -151,6 +139,36 @@ public static RemoteGcsHelper create(Option... options) throws GcsHelperExceptio
151139
}
152140
}
153141

142+
/**
143+
* Creates a {@code RemoteGcsHelper} object. Project id and path to JSON key are read from two
144+
* environment variables: {@code GCLOUD_TESTS_PROJECT_ID} and {@code GCLOUD_TESTS_KEY}.
145+
*
146+
* @param options creation options
147+
* @return A {@code RemoteGcsHelper} object for the provided options.
148+
* @throws com.google.gcloud.storage.RemoteGcsHelper.GcsHelperException if environment variables
149+
* {@code GCLOUD_TESTS_PROJECT_ID} and {@code GCLOUD_TESTS_KEY} are not set or if the file
150+
* pointed by {@code GCLOUD_TESTS_KEY} does not exist
151+
*/
152+
public static RemoteGcsHelper create(Option... options) throws GcsHelperException {
153+
String projectId = System.getenv(PROJECT_ID_ENV_VAR);
154+
String keyPath = System.getenv(PRIVATE_KEY_ENV_VAR);
155+
if (projectId == null) {
156+
String message = "Environment variable " + PROJECT_ID_ENV_VAR + " not set";
157+
if (log.isLoggable(Level.WARNING)) {
158+
log.log(Level.WARNING, message);
159+
}
160+
throw new GcsHelperException(message);
161+
}
162+
if (keyPath == null) {
163+
String message = "Environment variable " + PRIVATE_KEY_ENV_VAR + " not set";
164+
if (log.isLoggable(Level.WARNING)) {
165+
log.log(Level.WARNING, message);
166+
}
167+
throw new GcsHelperException(message);
168+
}
169+
return create(projectId, keyPath, options);
170+
}
171+
154172
private static class DeleteBucketTask implements Callable<Boolean> {
155173

156174
private Storage storage;

0 commit comments

Comments
 (0)