aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/java/io
diff options
context:
space:
mode:
authorCarl Mastrangelo <notcarl@google.com>2018-10-19 15:35:40 -0700
committerGitHub <noreply@github.com>2018-10-19 15:35:40 -0700
commit092bf2e39aaf960df2c6335667f4048452ec1fcc (patch)
treeca85aa69c45adedd356f21086d70c39565754385 /core/src/main/java/io
parenta61ac5ac6e42c2974eef399d4e10674108489295 (diff)
downloadgrpc-grpc-java-092bf2e39aaf960df2c6335667f4048452ec1fcc.tar.gz
core: ignore localhost and IP addresses for JNDI (1.16.x backport)
This change is mainly to fix a test, but it also is an implementation of the proposal here: https://github.com/grpc/proposal/pull/79 In short: * Do not do SRV or TXT lookups when the target name is `localhost`. This can be overriden by a system property * Do not do SRV or TXT lookups when the target name is an IPv6 or IPv4 address. This _cannot_ be overriden. The constructed domains for these queries would themselves not be valid. (e.g. _grpclb._tcp.192.168.0.1) * Speeds up initial connection when communicating over local host, since it is extremely uncommon that such a connection would need gRPCLB or SRV records I expect to remove the system property after a release if no one asks about it.
Diffstat (limited to 'core/src/main/java/io')
-rw-r--r--core/src/main/java/io/grpc/internal/DnsNameResolver.java30
1 files changed, 29 insertions, 1 deletions
diff --git a/core/src/main/java/io/grpc/internal/DnsNameResolver.java b/core/src/main/java/io/grpc/internal/DnsNameResolver.java
index ccbce27db..dfa815525 100644
--- a/core/src/main/java/io/grpc/internal/DnsNameResolver.java
+++ b/core/src/main/java/io/grpc/internal/DnsNameResolver.java
@@ -86,6 +86,8 @@ final class DnsNameResolver extends NameResolver {
private static final String JNDI_PROPERTY =
System.getProperty("io.grpc.internal.DnsNameResolverProvider.enable_jndi", "true");
+ private static final String JNDI_LOCALHOST_PROPERTY =
+ System.getProperty("io.grpc.internal.DnsNameResolverProvider.enable_jndi_localhost", "false");
private static final String JNDI_SRV_PROPERTY =
System.getProperty("io.grpc.internal.DnsNameResolverProvider.enable_grpclb", "false");
private static final String JNDI_TXT_PROPERTY =
@@ -107,6 +109,8 @@ final class DnsNameResolver extends NameResolver {
@VisibleForTesting
static boolean enableJndi = Boolean.parseBoolean(JNDI_PROPERTY);
@VisibleForTesting
+ static boolean enableJndiLocalhost = Boolean.parseBoolean(JNDI_LOCALHOST_PROPERTY);
+ @VisibleForTesting
static boolean enableSrv = Boolean.parseBoolean(JNDI_SRV_PROPERTY);
@VisibleForTesting
static boolean enableTxt = Boolean.parseBoolean(JNDI_TXT_PROPERTY);
@@ -230,7 +234,7 @@ final class DnsNameResolver extends NameResolver {
ResolutionResults resolutionResults;
try {
ResourceResolver resourceResolver = null;
- if (enableJndi) {
+ if (shouldUseJndi(enableJndi, enableJndiLocalhost, host)) {
resourceResolver = getResourceResolver();
}
resolutionResults =
@@ -629,4 +633,28 @@ final class DnsNameResolver extends NameResolver {
}
return localHostname;
}
+
+ @VisibleForTesting
+ static boolean shouldUseJndi(boolean jndiEnabled, boolean jndiLocalhostEnabled, String target) {
+ if (!jndiEnabled) {
+ return false;
+ }
+ if ("localhost".equalsIgnoreCase(target)) {
+ return jndiLocalhostEnabled;
+ }
+ // Check if this name looks like IPv6
+ if (target.contains(":")) {
+ return false;
+ }
+ // Check if this might be IPv4. Such addresses have no alphabetic characters. This also
+ // checks the target is empty.
+ boolean alldigits = true;
+ for (int i = 0; i < target.length(); i++) {
+ char c = target.charAt(i);
+ if (c != '.') {
+ alldigits &= (c >= '0' && c <= '9');
+ }
+ }
+ return !alldigits;
+ }
}