Skip to content

Commit

Permalink
Remove Netty dependency (#587)
Browse files Browse the repository at this point in the history
Use Java gRPC instead of Netty API to establish TLS connections. Requires client application to add an appropriate gRPC channel service provider dependency, but also gives the client control of the library to be used rather than mandating Netty.

Signed-off-by: Mark S. Lewis <Mark.S.Lewis@outlook.com>
  • Loading branch information
bestbeforetoday authored May 9, 2023
1 parent 4245d80 commit 0ddb630
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
25 changes: 21 additions & 4 deletions java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,20 @@ Add the following dependency to your project's `pom.xml` file:

```xml
<dependency>
<groupId>org.hyperledger.fabric</groupId>
<artifactId>fabric-gateway</artifactId>
<version>1.1.0</version>
<groupId>org.hyperledger.fabric</groupId>
<artifactId>fabric-gateway</artifactId>
<version>1.2.2</version>
</dependency>
```

A suitable gRPC channel service provider must also be specified (as described in the [gRPC security documentation](https://github.com/grpc/grpc-java/blob/master/SECURITY.md#transport-security-tls)), such as:

```xml
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>1.54.1</version>
<scope>runtime</scope>
</dependency>
```

Expand All @@ -37,7 +48,13 @@ Add the following dependency to your project's `pom.xml` file:
Add the following dependency to your project's `build.gradle` file:

```groovy
implementation 'org.hyperledger.fabric:fabric-gateway:1.1.0'
implementation 'org.hyperledger.fabric:fabric-gateway:1.2.2'
```

A suitable gRPC channel service provider must also be specified (as described in the [gRPC security documentation](https://github.com/grpc/grpc-java/blob/master/SECURITY.md#transport-security-tls)), such as:

```groovy
runtimeOnly 'io.grpc:grpc-netty-shaded:1.54.1'
```

## Compatibility
Expand Down
13 changes: 7 additions & 6 deletions java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<javaVersion>8</javaVersion>
<grpcVersion>1.54.0</grpcVersion>
<protobufVersion>3.21.12</protobufVersion> <!-- keep in step with the version used by io.grpc:grpc-protobuf -->
<grpcVersion>1.54.1</grpcVersion>
<protobufVersion>3.21.12</protobufVersion> <!-- keep major/minor version in step with one used by io.grpc:grpc-protobuf -->
<enforceJavaVersion>1.8</enforceJavaVersion> <!-- this is overridden in the release profile -->
<bouncyCastleVersion>1.72</bouncyCastleVersion>
<bouncyCastleVersion>1.73</bouncyCastleVersion>
<skipUnitTests>${skipTests}</skipUnitTests>
</properties>

Expand All @@ -50,14 +50,14 @@
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-bom</artifactId>
<version>7.11.2</version>
<version>7.12.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.9.2</version>
<version>5.9.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down Expand Up @@ -130,6 +130,7 @@
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
Expand Down Expand Up @@ -372,7 +373,7 @@
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>10.9.3</version>
<version>10.10.0</version>
</dependency>
</dependencies>
</plugin>
Expand Down
6 changes: 3 additions & 3 deletions java/src/main/javadoc/overview.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
the ledger state using an instantiated smart contract.</p>

<pre><code>
import io.grpc.Grpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.TlsChannelCredentials;
import org.hyperledger.fabric.client.*;
import org.hyperledger.fabric.client.identity.*;

Expand All @@ -38,8 +39,7 @@
PrivateKey privateKey = Identities.readPrivateKey(keyReader);
Signer signer = Signers.newPrivateKeySigner(privateKey);

ManagedChannel grpcChannel = ManagedChannelBuilder.forAddress("gateway.example.org", 1337)
.usePlaintext()
ManagedChannel grpcChannel = Grpc.newChannelBuilder("gateway.example.org:1337", TlsChannelCredentials.create())
.build();

Gateway.Builder builder = Gateway.newInstance()
Expand Down
13 changes: 6 additions & 7 deletions java/src/test/java/scenario/ScenarioSteps.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import io.grpc.ChannelCredentials;
import io.grpc.Grpc;
import io.grpc.ManagedChannel;
import io.grpc.Status;
import io.grpc.netty.shaded.io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder;
import io.grpc.netty.shaded.io.netty.handler.ssl.SslContext;
import io.grpc.TlsChannelCredentials;
import org.hyperledger.fabric.client.ChaincodeEvent;
import org.hyperledger.fabric.client.GatewayException;
import org.hyperledger.fabric.client.identity.Identities;
Expand Down Expand Up @@ -330,11 +330,10 @@ public void createGatewayWithoutSigner(String name, String user, String mspId) t
@Given("I connect the gateway to {word}")
public void connectGateway(String name) throws Exception {
ConnectionInfo info = peerConnectionInfo.get(name);
SslContext sslContext = GrpcSslContexts.forClient()
.trustManager(Files.newInputStream(Paths.get(info.tlsRootCertPath)))
ChannelCredentials credentials = TlsChannelCredentials.newBuilder()
.trustManager(Paths.get(info.tlsRootCertPath).toFile())
.build();
ManagedChannel channel = NettyChannelBuilder.forTarget(info.url)
.sslContext(sslContext)
ManagedChannel channel = Grpc.newChannelBuilder(info.url, credentials)
.overrideAuthority(info.serverNameOverride)
.build();
currentGateway.connect(channel);
Expand Down

0 comments on commit 0ddb630

Please sign in to comment.