Skip to content

Commit

Permalink
problem: no credentials for grpc connection
Browse files Browse the repository at this point in the history
solution: allow to build with an existing stub, or provide interceptors
  • Loading branch information
splix committed Jun 19, 2024
1 parent d91de78 commit 17686b7
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.emeraldpay.api.proto.BlockchainOuterClass;
import io.emeraldpay.api.proto.Common;
import io.emeraldpay.api.Chain;
import io.emeraldpay.api.proto.ReactorBlockchainGrpc;
import io.grpc.*;
import io.grpc.netty.NettyChannelBuilder;
import io.emeraldpay.etherjar.rpc.*;
Expand Down Expand Up @@ -55,7 +56,6 @@
*/
public class EmeraldTransport implements RpcTransport<DefaultBatch.FutureBatchItem> {

private final Channel channel;
private final BlockchainGrpc.BlockchainBlockingStub blockingStub;

private final ResponseJsonConverter responseJsonConverter = new ResponseJsonConverter();
Expand All @@ -67,17 +67,16 @@ public class EmeraldTransport implements RpcTransport<DefaultBatch.FutureBatchIt
private final Common.ChainRef chainRef;
private BlockchainOuterClass.Selector selector;

public EmeraldTransport(Channel channel,
public EmeraldTransport(BlockchainGrpc.BlockchainBlockingStub stub,
ObjectMapper objectMapper,
JacksonRpcConverter rpcConverter,
ExecutorService executorService,
Common.ChainRef chainRef) {
this.channel = channel;
this.objectMapper = objectMapper;
this.rpcConverter = rpcConverter;
this.executorService = executorService;
this.chainRef = chainRef;
blockingStub = BlockchainGrpc.newBlockingStub(channel);
blockingStub = stub;
}

/**
Expand All @@ -97,7 +96,7 @@ public static EmeraldTransport.Builder newBuilder() {
* @return new instance of EmeraldGrpcTransport configured for new chain
*/
public EmeraldTransport copyForChain(Chain chain) {
return new EmeraldTransport(channel, objectMapper, rpcConverter, executorService, Common.ChainRef.forNumber(chain.getId()));
return new EmeraldTransport(blockingStub, objectMapper, rpcConverter, executorService, Common.ChainRef.forNumber(chain.getId()));
}

/**
Expand Down Expand Up @@ -135,7 +134,7 @@ public EmeraldTransport copyForChain(Chain chain) {
* @return new instance of EmeraldGrpcTransport configured with new selector
*/
public EmeraldTransport copyWithSelector(BlockchainOuterClass.Selector selector) {
EmeraldTransport copy = new EmeraldTransport(channel, objectMapper, rpcConverter, executorService, chainRef);
EmeraldTransport copy = new EmeraldTransport(blockingStub, objectMapper, rpcConverter, executorService, chainRef);
copy.selector = selector;
return copy;
}
Expand Down Expand Up @@ -237,7 +236,7 @@ public <JS, RES> JS read(ByteString bytes, DefaultBatch.FutureBatchItem<JS, RES>

@Override
public void close() throws IOException {
Channel channel = this.channel;
Channel channel = this.blockingStub.getChannel();
if (channel instanceof ManagedChannel) {
((ManagedChannel) channel).shutdownNow();
try {
Expand All @@ -258,6 +257,8 @@ public static class Builder {

private SslContextBuilder sslContextBuilder;
private Channel channel;
private BlockchainGrpc.BlockchainBlockingStub stub;
private ClientInterceptor[] interceptors;

private ObjectMapper objectMapper;
private JacksonRpcConverter rpcConverter;
Expand All @@ -278,6 +279,20 @@ public Builder connectUsing(Channel channel) {
return this;
}

/**
* Setup with an existing stub. All other settings related to connection will be ignored
*
* @param stub existing stub
* @return builder
*/
public Builder connectUsing(BlockchainGrpc.BlockchainBlockingStub stub) {
this.stub = stub;
this.channel = null;
this.channelBuilder = null;
this.sslContextBuilder = null;
return this;
}

/**
* Apply a custom modification for the default NettyChannelBuilder
*
Expand Down Expand Up @@ -455,32 +470,49 @@ public Builder chain(Chain chain) {
return this;
}

/**
* Add interceptors to the client calls
*
* @param interceptors interceptors
* @return builder
*/
public Builder interceptors(ClientInterceptor... interceptors) {
this.interceptors = interceptors;
return this;
}

/**
* Validates configuration and builds GrpcTransport
*
* @return configured grpc transport
* @throws SSLException if problem with TLS certificates
*/
public EmeraldTransport build() throws SSLException {
if (channel == null) {
NettyChannelBuilder nettyBuilder = channelBuilder;
if (sslContextBuilder != null) {
nettyBuilder = nettyBuilder.useTransportSecurity()
.sslContext(sslContextBuilder.build());
}
if (useLoadBalancing) {
String policy = "round_robin";
if (LoadBalancerRegistry.getDefaultRegistry().getProvider(policy) != null) {
nettyBuilder = nettyBuilder.defaultLoadBalancingPolicy(policy);
if (stub == null) {
if (channel == null) {
NettyChannelBuilder nettyBuilder = channelBuilder;
if (sslContextBuilder != null) {
nettyBuilder = nettyBuilder.useTransportSecurity()
.sslContext(sslContextBuilder.build());
}
if (useLoadBalancing) {
String policy = "round_robin";
if (LoadBalancerRegistry.getDefaultRegistry().getProvider(policy) != null) {
nettyBuilder = nettyBuilder.defaultLoadBalancingPolicy(policy);
}
}
ManagedChannelBuilder<?> finalBuilder;
if (this.channelUpdate != null) {
finalBuilder = this.channelUpdate.apply(nettyBuilder);
} else {
finalBuilder = nettyBuilder;
}
channel = finalBuilder.build();
}
ManagedChannelBuilder<?> finalBuilder;
if (this.channelUpdate != null) {
finalBuilder = this.channelUpdate.apply(nettyBuilder);
} else {
finalBuilder = nettyBuilder;
stub = BlockchainGrpc.newBlockingStub(channel);
if (interceptors != null) {
stub = stub.withInterceptors(interceptors);
}
channel = finalBuilder.build();
}
if (executorService == null) {
threadsCount(2);
Expand All @@ -499,7 +531,7 @@ public EmeraldTransport build() throws SSLException {
chain = Chain.UNSPECIFIED;
}
Common.ChainRef chainRef = Common.ChainRef.forNumber(chain.getId());
return new EmeraldTransport(channel, objectMapper, rpcConverter, executorService, chainRef);
return new EmeraldTransport(stub, objectMapper, rpcConverter, executorService, chainRef);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.emeraldpay.api.proto.ReactorBlockchainGrpc;
import io.emeraldpay.api.Chain;
import io.grpc.Channel;
import io.grpc.ClientInterceptor;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import io.grpc.netty.GrpcSslContexts;
Expand All @@ -46,7 +47,6 @@

public class ReactorEmeraldClient extends AbstractReactorRpcClient implements ReactorRpcClient {

private final Channel channel;
private final ReactorBlockchainGrpc.ReactorBlockchainStub stub;

private final ObjectMapper objectMapper;
Expand All @@ -56,9 +56,8 @@ public class ReactorEmeraldClient extends AbstractReactorRpcClient implements Re

ResponseJsonConverter responseJsonConverter = new ResponseJsonConverter();

public ReactorEmeraldClient(Channel channel, ObjectMapper objectMapper, JacksonRpcConverter rpcConverter, Common.ChainRef chainRef) {
this.channel = channel;
this.stub = ReactorBlockchainGrpc.newReactorStub(channel);
public ReactorEmeraldClient(ReactorBlockchainGrpc.ReactorBlockchainStub stub, ObjectMapper objectMapper, JacksonRpcConverter rpcConverter, Common.ChainRef chainRef) {
this.stub = stub;
this.objectMapper = objectMapper;
this.rpcConverter = rpcConverter;
this.chainRef = chainRef;
Expand All @@ -76,7 +75,7 @@ public static Builder newBuilder() {
* @return new instance of ReactorEmeraldClient configured for new chain
*/
public ReactorEmeraldClient copyForChain(Chain chain) {
return new ReactorEmeraldClient(channel, objectMapper, rpcConverter, Common.ChainRef.forNumber(chain.getId()));
return new ReactorEmeraldClient(stub, objectMapper, rpcConverter, Common.ChainRef.forNumber(chain.getId()));
}

/**
Expand Down Expand Up @@ -114,7 +113,7 @@ public ReactorEmeraldClient copyForChain(Chain chain) {
* @return new instance of ReactorEmeraldClient configured with new selector
*/
public ReactorEmeraldClient copyWithSelector(BlockchainOuterClass.Selector selector) {
ReactorEmeraldClient copy = new ReactorEmeraldClient(channel, objectMapper, rpcConverter, chainRef);
ReactorEmeraldClient copy = new ReactorEmeraldClient(stub, objectMapper, rpcConverter, chainRef);
copy.selector = selector;
return copy;
}
Expand Down Expand Up @@ -212,6 +211,9 @@ public static class Builder {
private SslContextBuilder sslContextBuilder;
private Channel channel;

private ReactorBlockchainGrpc.ReactorBlockchainStub stub;
private ClientInterceptor[] interceptors;

private ObjectMapper objectMapper;
private JacksonRpcConverter rpcConverter;

Expand All @@ -230,6 +232,20 @@ public Builder connectUsing(Channel channel) {
return this;
}

/**
* Setup with an existing stub. All other settings related to connection will be ignored
*
* @param stub existing stub
* @return builder
*/
public Builder connectUsing(ReactorBlockchainGrpc.ReactorBlockchainStub stub) {
this.stub = stub;
this.channel = null;
this.channelBuilder = null;
this.sslContextBuilder = null;
return this;
}

/**
* Setup for address formatted as host:port
*
Expand Down Expand Up @@ -368,6 +384,17 @@ public Builder executor(Executor executor) {
return this;
}

/**
* Add interceptors to the client calls
*
* @param interceptors interceptors
* @return builder
*/
public Builder interceptors(ClientInterceptor... interceptors) {
this.interceptors = interceptors;
return this;
}

/**
*
* @param chain chain
Expand All @@ -385,12 +412,18 @@ public Builder chain(Chain chain) {
* @throws SSLException if problem with TLS certificates
*/
public ReactorEmeraldClient build() throws SSLException {
if (channel == null) {
if (sslContextBuilder != null) {
channelBuilder.useTransportSecurity()
.sslContext(sslContextBuilder.build());
if (stub == null) {
if (channel == null) {
if (sslContextBuilder != null) {
channelBuilder.useTransportSecurity()
.sslContext(sslContextBuilder.build());
}
channel = channelBuilder.build();
}
stub = ReactorBlockchainGrpc.newReactorStub(channel);
if (interceptors != null) {
stub = stub.withInterceptors(interceptors);
}
channel = channelBuilder.build();
}
if (objectMapper == null) {
objectMapper = new ObjectMapper();
Expand All @@ -402,7 +435,7 @@ public ReactorEmeraldClient build() throws SSLException {
chain = Chain.UNSPECIFIED;
}
Common.ChainRef chainRef = Common.ChainRef.forNumber(chain.getId());
return new ReactorEmeraldClient(channel, objectMapper, rpcConverter, chainRef);
return new ReactorEmeraldClient(stub, objectMapper, rpcConverter, chainRef);
}
}

Expand Down

0 comments on commit 17686b7

Please sign in to comment.