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

feat: support tls:// schema to auto enable tls #192

Merged
merged 3 commits into from
Nov 16, 2024
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
Expand Up @@ -19,19 +19,16 @@

import com.google.common.base.Throwables;
import io.grpc.CallCredentials;
import io.grpc.ChannelCredentials;
import io.grpc.Grpc;
import io.grpc.InsecureChannelCredentials;
import io.grpc.ManagedChannel;
import io.grpc.Metadata;
import io.grpc.TlsChannelCredentials;
import io.grpc.internal.BackoffPolicy;
import io.grpc.stub.MetadataUtils;
import io.streamnative.oxia.client.api.Authentication;
import io.streamnative.oxia.proto.OxiaClientGrpc;

import java.lang.reflect.Field;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import javax.annotation.Nullable;

Expand All @@ -40,19 +37,30 @@

@Slf4j
public class OxiaStub implements AutoCloseable {
public static String TLS_SCHEMA = "tls://";
private final ManagedChannel channel;
private final @NonNull OxiaClientGrpc.OxiaClientStub asyncStub;

static String getAddress(String address) {
if (address.startsWith(TLS_SCHEMA)) {
return address.substring(TLS_SCHEMA.length());
}
return address;
}

static ChannelCredentials getChannelCredential(String address, boolean tlsEnabled) {
return tlsEnabled || address.startsWith(TLS_SCHEMA)
? TlsChannelCredentials.newBuilder().build()
: InsecureChannelCredentials.create();
}

public OxiaStub(
String address,
@Nullable Authentication authentication,
boolean enableTls,
@Nullable BackoffPolicy.Provider backoffProvider) {
this(Grpc.newChannelBuilder(
address,
enableTls
? TlsChannelCredentials.newBuilder().build()
: InsecureChannelCredentials.create())

this(Grpc.newChannelBuilder(getAddress(address), getChannelCredential(address, enableTls))
.directExecutor()
.build(),
authentication, backoffProvider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package io.streamnative.oxia.client.grpc;

import io.grpc.InsecureChannelCredentials;
import io.grpc.TlsChannelCredentials;
import io.grpc.stub.StreamObserver;
import io.streamnative.oxia.proto.GetRequest;
import io.streamnative.oxia.proto.ReadRequest;
Expand Down Expand Up @@ -137,4 +139,30 @@ public void testMaxConnectionPerNode() {
}
Assertions.assertEquals(maxConnectionPerNode, stubManager.stubs.size());
}

@Test
public void testAddressTrim() {
final var tlsAddress = "tls://localhost:6648";
Assertions.assertEquals("localhost:6648", OxiaStub.getAddress(tlsAddress));

final var planTxtAddress = "localhost:6648";
Assertions.assertEquals("localhost:6648", OxiaStub.getAddress(planTxtAddress));
}

@Test
public void testTlsCredential() {
final var tlsAddress = "tls://localhost:6648";
var channelCredential = OxiaStub.getChannelCredential(tlsAddress, false);
Assertions.assertInstanceOf(TlsChannelCredentials.class, channelCredential);

channelCredential = OxiaStub.getChannelCredential(tlsAddress, true);
Assertions.assertInstanceOf(TlsChannelCredentials.class, channelCredential);

final var planTxtAddress = "localhost:6648";
channelCredential = OxiaStub.getChannelCredential(planTxtAddress, false);
Assertions.assertInstanceOf(InsecureChannelCredentials.class, channelCredential);

channelCredential = OxiaStub.getChannelCredential(planTxtAddress, true);
Assertions.assertInstanceOf(TlsChannelCredentials.class, channelCredential);
}
}
Loading