Skip to content

Commit

Permalink
Remove Netty dependency
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 dependency for TLS connection, 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 committed May 5, 2023
1 parent 4245d80 commit 66df60c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 24 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>
```

If using TLS then it may be desirable to include an appropriate security provider (as described in the [gRPC Ssecurity 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'
```

If using TLS then it may be desirable to include an appropriate security provider (as described in the [gRPC Ssecurity 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
16 changes: 6 additions & 10 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 @@ -127,10 +127,6 @@
<version>6.0.53</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
Expand Down Expand Up @@ -372,7 +368,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
15 changes: 8 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,12 @@ 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 grpcChannel = Grpc.newChannelBuilder("gateway.example.org:1337", TlsChannelCredentials.create())
.build();
ManagedChannel channel = Grpc.newChannelBuilder(info.url, credentials)
.overrideAuthority(info.serverNameOverride)
.build();
currentGateway.connect(channel);
Expand Down

0 comments on commit 66df60c

Please sign in to comment.