Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the default Deephaven java client TLS port to 443 #2066

Merged
merged 1 commit into from
Mar 10, 2022
Merged
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
@@ -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;
Expand All @@ -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) {
Expand All @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion java-client/session/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this was functionally the default before from the context of the examples (ConnectOptions.DEFAULT_PORT).


/**
* 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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down