Use Java gRPC instead of Netty API to establish TLS connections

Include specific gRPC TLS implementation in dependencies instead of relying on an appropriate one to be present as a transitive dependency.

Signed-off-by: Mark S. Lewis <Mark.S.Lewis@outlook.com>
This commit is contained in:
Mark S. Lewis 2023-05-05 16:27:22 +01:00 committed by Dave Enyeart
parent 84f9ba1dc4
commit e93cfacf1f
6 changed files with 37 additions and 34 deletions

View file

@ -19,9 +19,10 @@ repositories {
}
dependencies {
implementation 'org.hyperledger.fabric:fabric-gateway:1.1.1'
implementation 'io.grpc:grpc-netty-shaded:1.50.1'
implementation 'com.google.code.gson:gson:2.9.1'
implementation 'org.hyperledger.fabric:fabric-gateway:1.2.2'
compileOnly 'io.grpc:grpc-api:1.54.1'
runtimeOnly 'io.grpc:grpc-netty-shaded:1.54.1'
implementation 'com.google.code.gson:gson:2.10.1'
}
java {

View file

@ -7,9 +7,9 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParser;
import io.grpc.Grpc;
import io.grpc.ManagedChannel;
import io.grpc.netty.shaded.io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder;
import io.grpc.TlsChannelCredentials;
import org.hyperledger.fabric.client.CommitException;
import org.hyperledger.fabric.client.CommitStatusException;
import org.hyperledger.fabric.client.Contract;
@ -74,12 +74,12 @@ public final class App {
}
}
private static ManagedChannel newGrpcConnection() throws IOException, CertificateException {
var tlsCertReader = Files.newBufferedReader(TLS_CERT_PATH);
var tlsCert = Identities.readX509Certificate(tlsCertReader);
return NettyChannelBuilder.forTarget(PEER_ENDPOINT)
.sslContext(GrpcSslContexts.forClient().trustManager(tlsCert).build()).overrideAuthority(OVERRIDE_AUTH)
private static ManagedChannel newGrpcConnection() throws IOException {
var credentials = TlsChannelCredentials.newBuilder()
.trustManager(TLS_CERT_PATH.toFile())
.build();
return Grpc.newChannelBuilder(PEER_ENDPOINT, credentials)
.overrideAuthority(OVERRIDE_AUTH)
.build();
}

View file

@ -8,9 +8,10 @@ repositories {
}
dependencies {
implementation 'org.hyperledger.fabric:fabric-gateway:1.1.1'
implementation 'io.grpc:grpc-netty-shaded:1.50.1'
implementation 'com.google.code.gson:gson:2.9.1'
implementation 'org.hyperledger.fabric:fabric-gateway:1.2.2'
compileOnly 'io.grpc:grpc-api:1.54.1'
runtimeOnly 'io.grpc:grpc-netty-shaded:1.54.1'
implementation 'com.google.code.gson:gson:2.10.1'
}
java {

View file

@ -4,9 +4,9 @@
* SPDX-License-Identifier: Apache-2.0
*/
import io.grpc.Grpc;
import io.grpc.ManagedChannel;
import io.grpc.netty.shaded.io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder;
import io.grpc.TlsChannelCredentials;
import org.hyperledger.fabric.client.identity.Identities;
import org.hyperledger.fabric.client.identity.Identity;
import org.hyperledger.fabric.client.identity.Signer;
@ -40,12 +40,12 @@ public final class Connections {
// Private constructor to prevent instantiation
}
public static ManagedChannel newGrpcConnection() throws IOException, CertificateException {
var tlsCertReader = Files.newBufferedReader(tlsCertPath);
var tlsCert = Identities.readX509Certificate(tlsCertReader);
return NettyChannelBuilder.forTarget(peerEndpoint)
.sslContext(GrpcSslContexts.forClient().trustManager(tlsCert).build()).overrideAuthority(overrideAuth)
public static ManagedChannel newGrpcConnection() throws IOException {
var credentials = TlsChannelCredentials.newBuilder()
.trustManager(tlsCertPath.toFile())
.build();
return Grpc.newChannelBuilder(peerEndpoint, credentials)
.overrideAuthority(overrideAuth)
.build();
}

View file

@ -14,10 +14,11 @@ repositories {
}
dependencies {
implementation 'org.hyperledger.fabric:fabric-gateway:1.1.1'
implementation 'org.hyperledger.fabric:fabric-protos:0.1.5'
implementation 'io.grpc:grpc-netty-shaded:1.50.1'
implementation 'com.google.code.gson:gson:2.9.1'
implementation 'org.hyperledger.fabric:fabric-gateway:1.2.2'
implementation 'org.hyperledger.fabric:fabric-protos:0.2.0'
compileOnly 'io.grpc:grpc-api:1.54.1'
runtimeOnly 'io.grpc:grpc-netty-shaded:1.54.1'
implementation 'com.google.code.gson:gson:2.10.1'
}
java {

View file

@ -5,9 +5,9 @@
*/
import io.grpc.Channel;
import io.grpc.Grpc;
import io.grpc.ManagedChannel;
import io.grpc.netty.shaded.io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder;
import io.grpc.TlsChannelCredentials;
import org.hyperledger.fabric.client.Gateway;
import org.hyperledger.fabric.client.identity.Identities;
import org.hyperledger.fabric.client.identity.Identity;
@ -73,12 +73,12 @@ public final class Connections {
// Private constructor to prevent instantiation
}
public static ManagedChannel newGrpcConnection() throws IOException, CertificateException {
var tlsCertReader = Files.newBufferedReader(TLS_CERT_PATH);
var tlsCert = Identities.readX509Certificate(tlsCertReader);
return NettyChannelBuilder.forTarget(PEER_ENDPOINT)
.sslContext(GrpcSslContexts.forClient().trustManager(tlsCert).build()).overrideAuthority(PEER_HOST_ALIAS)
public static ManagedChannel newGrpcConnection() throws IOException {
var credentials = TlsChannelCredentials.newBuilder()
.trustManager(TLS_CERT_PATH.toFile())
.build();
return Grpc.newChannelBuilder(PEER_ENDPOINT, credentials)
.overrideAuthority(PEER_HOST_ALIAS)
.build();
}