Skip to content

Commit 74b40ae

Browse files
committed
feat: create custom trustmanager for gRPC connection
1 parent 25d1dc1 commit 74b40ae

5 files changed

Lines changed: 30 additions & 6 deletions

File tree

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
<grpc-netty-shaded.version>1.68.2</grpc-netty-shaded.version>
7373
<grpc-protobuf.version>1.70.0</grpc-protobuf.version>
7474
<grpc-stub.version>1.68.2</grpc-stub.version>
75+
<grpc-netty.version>1.73.0</grpc-netty.version>
7576
<annotations-api.version>6.0.53</annotations-api.version>
7677
</properties>
7778

@@ -97,6 +98,11 @@
9798
<artifactId>grpc-protobuf</artifactId>
9899
<version>${grpc-protobuf.version}</version>
99100
</dependency>
101+
<dependency>
102+
<groupId>io.grpc</groupId>
103+
<artifactId>grpc-netty</artifactId>
104+
<version>${grpc-netty.version}</version>
105+
</dependency>
100106
<dependency>
101107
<groupId>io.grpc</groupId>
102108
<artifactId>grpc-stub</artifactId>

src/it/java/io/weaviate/integration/DefaultRestTransportITest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class DefaultRestTransportITest {
2525
private TrustManagerFactory tmf;
2626

2727
@Before
28-
public void startMockServer() throws IOException {
28+
public void setUp() throws IOException {
2929
// MockServer does not verify exclusive ownership of the port
3030
// and using any well-known port like 8080 will produce flaky
3131
// test results with fairly confusing errors, like:
@@ -86,7 +86,7 @@ public void testCustomTrustStore_async() throws IOException, ExecutionException,
8686
}
8787

8888
@After
89-
public void stopMockServer() throws IOException {
89+
public void tearDown() throws IOException {
9090
mockServer.stop();
9191
transport.close();
9292
}

src/main/java/io/weaviate/client6/v1/api/Config.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public Custom httpHost(String httpHost) {
184184
}
185185

186186
public Custom httpPort(int port) {
187-
this.grpcPort = port;
187+
this.httpPort = port;
188188
return this;
189189
}
190190

src/main/java/io/weaviate/client6/v1/internal/TransportOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ protected TransportOptions(String scheme, String host, int port, H headers, Toke
2222
}
2323

2424
public boolean isSecure() {
25-
return scheme == "https";
25+
return scheme.equals("https");
2626
}
2727

2828
public String scheme() {

src/main/java/io/weaviate/client6/v1/internal/grpc/DefaultGrpcTransport.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
import java.io.IOException;
44
import java.util.concurrent.CompletableFuture;
55

6+
import javax.net.ssl.SSLException;
7+
68
import com.google.common.util.concurrent.FutureCallback;
79
import com.google.common.util.concurrent.Futures;
810
import com.google.common.util.concurrent.ListenableFuture;
911

1012
import io.grpc.ManagedChannel;
11-
import io.grpc.ManagedChannelBuilder;
13+
import io.grpc.netty.GrpcSslContexts;
14+
import io.grpc.netty.NettyChannelBuilder;
1215
import io.grpc.stub.MetadataUtils;
16+
import io.netty.handler.ssl.SslContext;
1317
import io.weaviate.client6.v1.internal.grpc.protocol.WeaviateGrpc;
1418
import io.weaviate.client6.v1.internal.grpc.protocol.WeaviateGrpc.WeaviateBlockingStub;
1519
import io.weaviate.client6.v1.internal.grpc.protocol.WeaviateGrpc.WeaviateFutureStub;
@@ -80,15 +84,29 @@ public void onFailure(Throwable t) {
8084
}
8185

8286
private static ManagedChannel buildChannel(GrpcChannelOptions transportOptions) {
83-
var channel = ManagedChannelBuilder.forAddress(transportOptions.host(), transportOptions.port());
87+
var channel = NettyChannelBuilder.forAddress(transportOptions.host(), transportOptions.port());
8488

8589
if (transportOptions.isSecure()) {
8690
channel.useTransportSecurity();
8791
} else {
8892
channel.usePlaintext();
8993
}
9094

95+
if (transportOptions.trustManagerFactory() != null) {
96+
SslContext sslCtx;
97+
try {
98+
sslCtx = GrpcSslContexts.forClient()
99+
.trustManager(transportOptions.trustManagerFactory())
100+
.build();
101+
} catch (SSLException e) {
102+
// todo: rethrow as WeaviateConnectionException
103+
throw new RuntimeException("create grpc transport", e);
104+
}
105+
channel.sslContext(sslCtx);
106+
}
107+
91108
channel.intercept(MetadataUtils.newAttachHeadersInterceptor(transportOptions.headers()));
109+
92110
return channel.build();
93111
}
94112

0 commit comments

Comments
 (0)