From e2724dc3b5df0248c25d292835cd0b36f419f9d1 Mon Sep 17 00:00:00 2001 From: Devin Smith Date: Thu, 3 Mar 2022 15:20:57 -0800 Subject: [PATCH] Change the default Deephaven java client TLS port to 443 --- .../client/examples/ConnectOptions.java | 15 +-- java-client/session/build.gradle | 3 +- .../deephaven/client/impl/ChannelHelper.java | 42 ++++---- .../DeephavenTargetNameResolverProviders.java | 95 +++++++++++++++++++ .../io/deephaven/uri/DeephavenTarget.java | 5 + .../server/uri/BarrageTableResolver.java | 2 +- 6 files changed, 125 insertions(+), 37 deletions(-) create mode 100644 java-client/session/src/main/java/io/deephaven/client/impl/DeephavenTargetNameResolverProviders.java diff --git a/java-client/example-utilities/src/main/java/io/deephaven/client/examples/ConnectOptions.java b/java-client/example-utilities/src/main/java/io/deephaven/client/examples/ConnectOptions.java index effbb35f6fd..76849813ad9 100644 --- a/java-client/example-utilities/src/main/java/io/deephaven/client/examples/ConnectOptions.java +++ b/java-client/example-utilities/src/main/java/io/deephaven/client/examples/ConnectOptions.java @@ -1,5 +1,6 @@ package io.deephaven.client.examples; +import io.deephaven.client.impl.ChannelHelper; import io.deephaven.uri.DeephavenTarget; import io.deephaven.uri.DeephavenUri; import io.grpc.ManagedChannel; @@ -10,16 +11,12 @@ public class ConnectOptions { public static final String DEFAULT_HOST = "localhost"; - public static final int DEFAULT_PORT = 10000; - public static final DeephavenTarget DEFAULT_TARGET = DeephavenTarget.builder() .host(DEFAULT_HOST) - .port(DEFAULT_PORT) .isSecure(false) .build(); - public static final String DEFAULT_TARGET_VALUE = - DeephavenUri.PLAINTEXT_SCHEME + "://" + DEFAULT_HOST + ":" + DEFAULT_PORT; + public static final String DEFAULT_TARGET_VALUE = DeephavenUri.PLAINTEXT_SCHEME + "://" + DEFAULT_HOST; public static ManagedChannel open(ConnectOptions options) { if (options == null) { @@ -38,13 +35,7 @@ public static ManagedChannel open(ConnectOptions options) { String userAgent; public ManagedChannel open() { - final ManagedChannelBuilder builder = - ManagedChannelBuilder.forAddress(target.host(), target.port().orElse(DEFAULT_PORT)); - if (target.isSecure()) { - builder.useTransportSecurity(); - } else { - builder.usePlaintext(); - } + final ManagedChannelBuilder builder = ChannelHelper.channelBuilder(target); if (userAgent != null) { builder.userAgent(userAgent); } diff --git a/java-client/session/build.gradle b/java-client/session/build.gradle index 137e42047ad..0a35fae4f40 100644 --- a/java-client/session/build.gradle +++ b/java-client/session/build.gradle @@ -11,12 +11,13 @@ dependencies { Classpaths.inheritGrpcPlatform(project, 'api') api 'io.grpc:grpc-api' + implementation 'io.grpc:grpc-core' // TODO(deephaven-core#1783): Make proto-backplane-grpc implementation dependency api project(':proto:proto-backplane-grpc') Classpaths.inheritImmutables(project) - + Classpaths.inheritAutoService(project) compileOnly 'javax.inject:javax.inject:1' Classpaths.inheritJUnitPlatform(project) diff --git a/java-client/session/src/main/java/io/deephaven/client/impl/ChannelHelper.java b/java-client/session/src/main/java/io/deephaven/client/impl/ChannelHelper.java index df9df064bff..1534da0a74b 100644 --- a/java-client/session/src/main/java/io/deephaven/client/impl/ChannelHelper.java +++ b/java-client/session/src/main/java/io/deephaven/client/impl/ChannelHelper.java @@ -1,37 +1,33 @@ package io.deephaven.client.impl; import io.deephaven.uri.DeephavenTarget; -import io.grpc.ManagedChannel; +import io.grpc.ChannelCredentials; +import io.grpc.Grpc; +import io.grpc.InsecureChannelCredentials; import io.grpc.ManagedChannelBuilder; +import io.grpc.TlsChannelCredentials; -public class ChannelHelper { +public final class ChannelHelper { - public static final int DEFAULT_TLS_PORT = 8080; + public static final int DEFAULT_TLS_PORT = 443; - public static final int DEFAULT_PLAINTEXT_PORT = 8080; - - public static ManagedChannel channel(DeephavenTarget target) { - return channelBuilder(target).build(); - } + public static final int DEFAULT_PLAINTEXT_PORT = 10000; + /** + * Creates a basic {@link ManagedChannelBuilder} by invoking + * {@link Grpc#newChannelBuilder(String, ChannelCredentials)} with {@link DeephavenTarget#toString()} and the + * appropriate {@link io.grpc.CallCredentials}. + * + * @param target the Deephaven target + * @return the channel builder + */ public static ManagedChannelBuilder channelBuilder(DeephavenTarget target) { - final ManagedChannelBuilder builder = ManagedChannelBuilder.forAddress(target.host(), port(target)); + final ChannelCredentials credentials; if (target.isSecure()) { - builder.useTransportSecurity(); + credentials = TlsChannelCredentials.newBuilder().build(); } else { - builder.usePlaintext(); - } - return builder; - } - - public static int port(DeephavenTarget target) { - if (target.port().isPresent()) { - return target.port().getAsInt(); - } - // TODO(deephaven-core#1489): Support service discovery for DeephavenTarget - if (target.isSecure()) { - return Integer.getInteger("deephaven.target.port", DEFAULT_TLS_PORT); + credentials = InsecureChannelCredentials.create(); } - return Integer.getInteger("deephaven.target.plaintext_port", DEFAULT_PLAINTEXT_PORT); + return Grpc.newChannelBuilder(target.toString(), credentials); } } diff --git a/java-client/session/src/main/java/io/deephaven/client/impl/DeephavenTargetNameResolverProviders.java b/java-client/session/src/main/java/io/deephaven/client/impl/DeephavenTargetNameResolverProviders.java new file mode 100644 index 00000000000..fd2074e7f8c --- /dev/null +++ b/java-client/session/src/main/java/io/deephaven/client/impl/DeephavenTargetNameResolverProviders.java @@ -0,0 +1,95 @@ +package io.deephaven.client.impl; + +import com.google.auto.service.AutoService; +import io.deephaven.uri.DeephavenTarget; +import io.deephaven.uri.DeephavenUri; +import io.grpc.NameResolver; +import io.grpc.NameResolver.Args; +import io.grpc.NameResolverProvider; +import io.grpc.internal.DnsNameResolverProvider; + +import java.net.URI; + +public final class DeephavenTargetNameResolverProviders { + + // TODO(deephaven-core#1489): Support service discovery for DeephavenTarget + + private static final DnsNameResolverProvider DNS_NAME_RESOLVER_PROVIDER = new DnsNameResolverProvider(); + + private static URI deephavenToDnsTarget(URI targetUri) { + final DeephavenTarget deephavenTarget = DeephavenTarget.of(targetUri); + // https://grpc.github.io/grpc/core/md_doc_naming.html + return deephavenTarget.port().isPresent() + ? URI.create(String.format("dns:///%s:%d", deephavenTarget.host(), deephavenTarget.port().getAsInt())) + : URI.create(String.format("dns:///%s", deephavenTarget.host())); + } + + /** + * Provides {@link NameResolver} support for plaintext {@link DeephavenTarget}. + */ + @AutoService(NameResolverProvider.class) + public static final class Plaintext extends NameResolverProvider { + + private static Args setDefaultPort(Args args) { + return args.toBuilder().setDefaultPort(ChannelHelper.DEFAULT_PLAINTEXT_PORT).build(); + } + + @Override + protected boolean isAvailable() { + return true; + } + + @Override + protected int priority() { + return 5; + } + + @Override + public NameResolver newNameResolver(URI targetUri, Args args) { + if (!DeephavenUri.PLAINTEXT_SCHEME.equals(targetUri.getScheme())) { + return null; + } + return DNS_NAME_RESOLVER_PROVIDER.newNameResolver(deephavenToDnsTarget(targetUri), setDefaultPort(args)); + } + + @Override + public String getDefaultScheme() { + return DeephavenUri.PLAINTEXT_SCHEME; + } + } + + + /** + * Provides {@link NameResolver} support for secure {@link DeephavenTarget}. + */ + @AutoService(NameResolverProvider.class) + public static final class Secure extends NameResolverProvider { + + private static Args setDefaultPort(Args args) { + return args.toBuilder().setDefaultPort(ChannelHelper.DEFAULT_TLS_PORT).build(); + } + + @Override + protected boolean isAvailable() { + return true; + } + + @Override + protected int priority() { + return 5; + } + + @Override + public NameResolver newNameResolver(URI targetUri, Args args) { + if (!DeephavenUri.SECURE_SCHEME.equals(targetUri.getScheme())) { + return null; + } + return DNS_NAME_RESOLVER_PROVIDER.newNameResolver(deephavenToDnsTarget(targetUri), setDefaultPort(args)); + } + + @Override + public String getDefaultScheme() { + return DeephavenUri.SECURE_SCHEME; + } + } +} diff --git a/java-client/uri/src/main/java/io/deephaven/uri/DeephavenTarget.java b/java-client/uri/src/main/java/io/deephaven/uri/DeephavenTarget.java index fb8b4cdf6a7..81e11b04fe8 100644 --- a/java-client/uri/src/main/java/io/deephaven/uri/DeephavenTarget.java +++ b/java-client/uri/src/main/java/io/deephaven/uri/DeephavenTarget.java @@ -125,6 +125,11 @@ final void checkHostPort() { } } + /** + * The target as a URI string. + * + * @return the URI string + */ @Override public final String toString() { final String scheme = isSecure() ? DeephavenUri.SECURE_SCHEME : DeephavenUri.PLAINTEXT_SCHEME; diff --git a/server/src/main/java/io/deephaven/server/uri/BarrageTableResolver.java b/server/src/main/java/io/deephaven/server/uri/BarrageTableResolver.java index 8b59ab94dd2..e7bae41733a 100644 --- a/server/src/main/java/io/deephaven/server/uri/BarrageTableResolver.java +++ b/server/src/main/java/io/deephaven/server/uri/BarrageTableResolver.java @@ -275,7 +275,7 @@ private BarrageSession session(DeephavenTarget target) { } private BarrageSession newSession(DeephavenTarget target) { - return newSession(ChannelHelper.channel(target)); + return newSession(ChannelHelper.channelBuilder(target).build()); } private BarrageSession newSession(ManagedChannel channel) {