Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@
import com.google.api.core.InternalApi;
import com.google.cloud.NoCredentials;
import com.google.common.base.Strings;
import com.google.common.net.InetAddresses;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;

@InternalApi
public class DatastoreUtils {

private static final String DEFAULT_DATASTORE_HOSTNAME = "datastore.googleapis.com";

public static boolean isEmulator(DatastoreOptions datastoreOptions) {
return isLocalHost(datastoreOptions.getHost())
|| NoCredentials.getInstance().equals(datastoreOptions.getCredentials());
Expand All @@ -34,12 +39,33 @@ public static boolean isLocalHost(String host) {
if (Strings.isNullOrEmpty(host)) {
return false;
}
String hostWithoutScheme = removeScheme(host);
if (Strings.isNullOrEmpty(hostWithoutScheme)) {
return false;
}
try {
String normalizedHost = "http://" + removeScheme(host);
InetAddress hostAddr = InetAddress.getByName(new URL(normalizedHost).getHost());
String hostname = new URL("http://" + hostWithoutScheme).getHost();
// Short-circuit the default Datastore host before DNS resolution so default clients in
// air-gapped or non-default-universe networks never look up datastore.googleapis.com.
if (Strings.isNullOrEmpty(hostname) || DEFAULT_DATASTORE_HOSTNAME.equals(hostname)) {
return false;
}
// Avoid DNS lookups for literal "localhost" and numeric IPv4/IPv6 addresses (e.g., 127.0.0.1,
// 0.0.0.0, [::1]).
if ("localhost".equals(hostname)) {
return true;
}
if (InetAddresses.isUriInetAddress(hostname)) {
InetAddress hostAddr = InetAddresses.forUriString(hostname);
return hostAddr.isAnyLocalAddress() || hostAddr.isLoopbackAddress();
}
// Fall back to DNS for custom local aliases (e.g., /etc/hosts entries mapped to loopback).
InetAddress hostAddr = InetAddress.getByName(hostname);
return hostAddr.isAnyLocalAddress() || hostAddr.isLoopbackAddress();
} catch (Exception e) {
throw new RuntimeException(e);
} catch (UnknownHostException | MalformedURLException e) {
// Unresolvable or malformed hosts are not local emulators; fail open to non-emulator mode
// instead of crashing client initialization in air-gapped networks.
return false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package com.google.cloud.datastore.spi.v1;

import static com.google.cloud.datastore.DatastoreUtils.isLocalHost;
import static com.google.cloud.datastore.DatastoreUtils.removeScheme;

import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
Expand Down Expand Up @@ -47,8 +50,6 @@
import com.google.rpc.Code;
import io.opencensus.trace.Tracing;
import java.io.IOException;
import java.net.InetAddress;
import java.net.URL;
import javax.annotation.Nullable;

@InternalApi
Expand Down Expand Up @@ -184,30 +185,6 @@ public void initialize(HttpRequest httpRequest) throws IOException {
};
}

private static boolean isLocalHost(String host) {
if (!host.isEmpty()) {
try {
String normalizedHost = "http://" + removeScheme(host);
InetAddress hostAddr = InetAddress.getByName(new URL(normalizedHost).getHost());
return hostAddr.isAnyLocalAddress() || hostAddr.isLoopbackAddress();
} catch (Exception e) {
// ignore
}
}
return false;
}

private static String removeScheme(String url) {
if (url != null) {
if (url.startsWith("https://")) {
return url.substring("https://".length());
} else if (url.startsWith("http://")) {
return url.substring("http://".length());
}
}
return url;
}

private static DatastoreException translate(
com.google.datastore.v1.client.DatastoreException exception) {
return translate(exception, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import static com.google.cloud.datastore.DatastoreUtils.removeScheme;
import static com.google.common.truth.Truth.assertThat;

import com.google.cloud.datastore.v1.DatastoreSettings;
import com.google.datastore.v1.client.DatastoreFactory;
import org.junit.Test;

public class DatastoreUtilsTest {
Expand All @@ -30,7 +32,14 @@ public void testIsLocalHost() {
assertThat(isLocalHost("")).isFalse();
assertThat(isLocalHost("http://localhost:9090")).isTrue();
assertThat(isLocalHost("https://localhost:9090")).isTrue();
assertThat(isLocalHost("127.0.0.1:8081")).isTrue();
assertThat(isLocalHost("0.0.0.0:8081")).isTrue();
assertThat(isLocalHost("http://[::1]:8081")).isTrue();
assertThat(isLocalHost("10.10.10.10:9090")).isFalse();
assertThat(isLocalHost(DatastoreSettings.getDefaultEndpoint())).isFalse();
assertThat(isLocalHost(DatastoreFactory.DEFAULT_HOST)).isFalse();
assertThat(isLocalHost("unresolvable.invalid:443")).isFalse();
assertThat(isLocalHost(":::invalid")).isFalse();
}

@Test
Expand Down
Loading