Skip to content

Commit 0411108

Browse files
committed
RATIS-2331. Reuse SslContext in gRPC.
1 parent 35615d9 commit 0411108

8 files changed

Lines changed: 103 additions & 242 deletions

File tree

ratis-grpc/src/main/java/org/apache/ratis/grpc/GrpcFactory.java

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.apache.ratis.server.leader.FollowerInfo;
3333
import org.apache.ratis.server.leader.LeaderState;
3434
import org.apache.ratis.thirdparty.io.netty.buffer.PooledByteBufAllocator;
35+
import org.apache.ratis.thirdparty.io.netty.handler.ssl.SslContext;
3536
import org.apache.ratis.util.JavaUtils;
3637
import org.slf4j.Logger;
3738
import org.slf4j.LoggerFactory;
@@ -72,12 +73,6 @@ static boolean checkPooledByteBufAllocatorUseCacheForAllThreads(Consumer<String>
7273
private final GrpcTlsConfig clientTlsConfig;
7374
private final GrpcTlsConfig serverTlsConfig;
7475

75-
public static Parameters newRaftParameters(GrpcTlsConfig conf) {
76-
final Parameters p = new Parameters();
77-
GrpcConfigKeys.TLS.setConf(p, conf);
78-
return p;
79-
}
80-
8176
public GrpcFactory(Parameters parameters) {
8277
this(GrpcConfigKeys.Server.servicesCustomizer(parameters),
8378
GrpcConfigKeys.TLS.conf(parameters),
@@ -87,10 +82,6 @@ public GrpcFactory(Parameters parameters) {
8782
);
8883
}
8984

90-
public GrpcFactory(GrpcTlsConfig tlsConfig) {
91-
this(null, tlsConfig, null, null, null);
92-
}
93-
9485
private GrpcFactory(GrpcServices.Customizer servicesCustomizer,
9586
GrpcTlsConfig tlsConfig, GrpcTlsConfig adminTlsConfig,
9687
GrpcTlsConfig clientTlsConfig, GrpcTlsConfig serverTlsConfig) {
@@ -102,22 +93,6 @@ private GrpcFactory(GrpcServices.Customizer servicesCustomizer,
10293
this.serverTlsConfig = serverTlsConfig;
10394
}
10495

105-
public GrpcTlsConfig getTlsConfig() {
106-
return tlsConfig;
107-
}
108-
109-
public GrpcTlsConfig getAdminTlsConfig() {
110-
return adminTlsConfig != null ? adminTlsConfig : tlsConfig;
111-
}
112-
113-
public GrpcTlsConfig getClientTlsConfig() {
114-
return clientTlsConfig != null ? clientTlsConfig : tlsConfig;
115-
}
116-
117-
public GrpcTlsConfig getServerTlsConfig() {
118-
return serverTlsConfig != null ? serverTlsConfig : tlsConfig;
119-
}
120-
12196
@Override
12297
public SupportedRpcType getRpcType() {
12398
return SupportedRpcType.GRPC;
@@ -128,22 +103,35 @@ public LogAppender newLogAppender(RaftServer.Division server, LeaderState state,
128103
return new GrpcLogAppender(server, state, f);
129104
}
130105

106+
static SslContext buildSslContextForServer(GrpcTlsConfig tlsConf, SslContext defaultSslContext) {
107+
return tlsConf == null ? defaultSslContext : GrpcUtil.buildSslContextForServer(tlsConf);
108+
}
109+
131110
@Override
132111
public GrpcServices newRaftServerRpc(RaftServer server) {
133112
checkPooledByteBufAllocatorUseCacheForAllThreads(LOG::info);
113+
114+
final SslContext defaultSslContext = GrpcUtil.buildSslContextForServer(tlsConfig);
134115
return GrpcServicesImpl.newBuilder()
135116
.setServer(server)
136117
.setCustomizer(servicesCustomizer)
137-
.setAdminTlsConfig(getAdminTlsConfig())
138-
.setServerTlsConfig(getServerTlsConfig())
139-
.setClientTlsConfig(getClientTlsConfig())
118+
.setAdminSslContext(buildSslContextForServer(adminTlsConfig, defaultSslContext))
119+
.setServerSslContext(buildSslContextForServer(serverTlsConfig, defaultSslContext))
120+
.setClientSslContext(buildSslContextForServer(clientTlsConfig, defaultSslContext))
140121
.build();
141122
}
142123

124+
static SslContext buildSslContextForClient(GrpcTlsConfig tlsConf, SslContext defaultSslContext) {
125+
return tlsConf == null ? defaultSslContext : GrpcUtil.buildSslContextForClient(tlsConf);
126+
}
127+
143128
@Override
144129
public GrpcClientRpc newRaftClientRpc(ClientId clientId, RaftProperties properties) {
145130
checkPooledByteBufAllocatorUseCacheForAllThreads(LOG::debug);
131+
132+
final SslContext defaultSslContext = GrpcUtil.buildSslContextForClient(tlsConfig);
146133
return new GrpcClientRpc(clientId, properties,
147-
getAdminTlsConfig(), getClientTlsConfig());
134+
buildSslContextForClient(adminTlsConfig, defaultSslContext),
135+
buildSslContextForClient(clientTlsConfig, defaultSslContext));
148136
}
149137
}

ratis-grpc/src/main/java/org/apache/ratis/grpc/GrpcUtil.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
import org.apache.ratis.thirdparty.io.grpc.Metadata;
2929
import org.apache.ratis.thirdparty.io.grpc.Status;
3030
import org.apache.ratis.thirdparty.io.grpc.StatusRuntimeException;
31+
import org.apache.ratis.thirdparty.io.grpc.netty.GrpcSslContexts;
3132
import org.apache.ratis.thirdparty.io.grpc.stub.StreamObserver;
33+
import org.apache.ratis.thirdparty.io.netty.handler.ssl.ClientAuth;
34+
import org.apache.ratis.thirdparty.io.netty.handler.ssl.SslContext;
3235
import org.apache.ratis.thirdparty.io.netty.handler.ssl.SslContextBuilder;
3336
import org.apache.ratis.util.IOUtils;
3437
import org.apache.ratis.util.JavaUtils;
@@ -39,13 +42,16 @@
3942
import org.slf4j.LoggerFactory;
4043

4144
import javax.net.ssl.KeyManager;
45+
import javax.net.ssl.SSLException;
4246
import javax.net.ssl.TrustManager;
4347
import java.io.IOException;
4448
import java.util.concurrent.CompletableFuture;
4549
import java.util.concurrent.TimeUnit;
4650
import java.util.function.Function;
4751
import java.util.function.Supplier;
4852

53+
import static org.apache.ratis.thirdparty.io.netty.handler.ssl.SslProvider.OPENSSL;
54+
4955
public interface GrpcUtil {
5056
Logger LOG = LoggerFactory.getLogger(GrpcUtil.class);
5157

@@ -297,4 +303,38 @@ static void setKeyManager(SslContextBuilder b, KeyManagerConf keyManagerConfig)
297303
b.keyManager(privateKey.get(), certificates.get());
298304
}
299305
}
306+
307+
static SslContext buildSslContextForServer(GrpcTlsConfig tlsConf) {
308+
if (tlsConf == null) {
309+
return null;
310+
}
311+
SslContextBuilder b = initSslContextBuilderForServer(tlsConf.getKeyManager());
312+
if (tlsConf.getMtlsEnabled()) {
313+
b.clientAuth(ClientAuth.REQUIRE);
314+
setTrustManager(b, tlsConf.getTrustManager());
315+
}
316+
b = GrpcSslContexts.configure(b, OPENSSL);
317+
try {
318+
return b.build();
319+
} catch (Exception e) {
320+
throw new IllegalArgumentException("Failed to buildSslContextForServer from tlsConfig " + tlsConf, e);
321+
}
322+
}
323+
324+
static SslContext buildSslContextForClient(GrpcTlsConfig tlsConf) {
325+
if (tlsConf == null) {
326+
return null;
327+
}
328+
329+
final SslContextBuilder b = GrpcSslContexts.forClient();
330+
setTrustManager(b, tlsConf.getTrustManager());
331+
if (tlsConf.getMtlsEnabled()) {
332+
setKeyManager(b, tlsConf.getKeyManager());
333+
}
334+
try {
335+
return b.build();
336+
} catch (SSLException e) {
337+
throw new IllegalArgumentException("Failed to buildSslContextForClient from tlsConfig " + tlsConf, e);
338+
}
339+
}
300340
}

ratis-grpc/src/main/java/org/apache/ratis/grpc/client/GrpcClientProtocolClient.java

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.apache.ratis.client.impl.ClientProtoUtils;
2222
import org.apache.ratis.conf.RaftProperties;
2323
import org.apache.ratis.grpc.GrpcConfigKeys;
24-
import org.apache.ratis.grpc.GrpcTlsConfig;
2524
import org.apache.ratis.grpc.GrpcUtil;
2625
import org.apache.ratis.grpc.metrics.intercept.client.MetricClientInterceptor;
2726
import org.apache.ratis.proto.RaftProtos.GroupInfoReplyProto;
@@ -49,11 +48,10 @@
4948
import org.apache.ratis.protocol.exceptions.TimeoutIOException;
5049
import org.apache.ratis.thirdparty.io.grpc.ManagedChannel;
5150
import org.apache.ratis.thirdparty.io.grpc.StatusRuntimeException;
52-
import org.apache.ratis.thirdparty.io.grpc.netty.GrpcSslContexts;
5351
import org.apache.ratis.thirdparty.io.grpc.netty.NegotiationType;
5452
import org.apache.ratis.thirdparty.io.grpc.netty.NettyChannelBuilder;
5553
import org.apache.ratis.thirdparty.io.grpc.stub.StreamObserver;
56-
import org.apache.ratis.thirdparty.io.netty.handler.ssl.SslContextBuilder;
54+
import org.apache.ratis.thirdparty.io.netty.handler.ssl.SslContext;
5755
import org.apache.ratis.util.CollectionUtils;
5856
import org.apache.ratis.util.JavaUtils;
5957
import org.apache.ratis.util.SizeInBytes;
@@ -97,7 +95,7 @@ public class GrpcClientProtocolClient implements Closeable {
9795
private final MetricClientInterceptor metricClientInterceptor;
9896

9997
GrpcClientProtocolClient(ClientId id, RaftPeer target, RaftProperties properties,
100-
GrpcTlsConfig adminTlsConfig, GrpcTlsConfig clientTlsConfig) {
98+
SslContext adminSslContext, SslContext clientSslContext) {
10199
this.name = JavaUtils.memoize(() -> id + "->" + target.getId());
102100
this.target = target;
103101
final SizeInBytes flowControlWindow = GrpcConfigKeys.flowControlWindow(properties, LOG::debug);
@@ -110,11 +108,9 @@ public class GrpcClientProtocolClient implements Closeable {
110108
.filter(x -> !x.isEmpty()).orElse(target.getAddress());
111109
final boolean separateAdminChannel = !Objects.equals(clientAddress, adminAddress);
112110

113-
clientChannel = buildChannel(clientAddress, clientTlsConfig,
114-
flowControlWindow, maxMessageSize);
111+
clientChannel = buildChannel(clientAddress, clientSslContext, flowControlWindow, maxMessageSize);
115112
adminChannel = separateAdminChannel
116-
? buildChannel(adminAddress, adminTlsConfig,
117-
flowControlWindow, maxMessageSize)
113+
? buildChannel(adminAddress, adminSslContext, flowControlWindow, maxMessageSize)
118114
: clientChannel;
119115

120116
asyncStub = RaftClientProtocolServiceGrpc.newStub(clientChannel);
@@ -124,26 +120,16 @@ public class GrpcClientProtocolClient implements Closeable {
124120
RaftClientConfigKeys.Rpc.watchRequestTimeout(properties);
125121
}
126122

127-
private ManagedChannel buildChannel(String address, GrpcTlsConfig tlsConf,
123+
private ManagedChannel buildChannel(String address, SslContext sslContext,
128124
SizeInBytes flowControlWindow, SizeInBytes maxMessageSize) {
129125
NettyChannelBuilder channelBuilder =
130126
NettyChannelBuilder.forTarget(address);
131127
// ignore any http proxy for grpc
132128
channelBuilder.proxyDetector(uri -> null);
133129

134-
if (tlsConf != null) {
130+
if (sslContext != null) {
135131
LOG.debug("Setting TLS for {}", address);
136-
SslContextBuilder sslContextBuilder = GrpcSslContexts.forClient();
137-
GrpcUtil.setTrustManager(sslContextBuilder, tlsConf.getTrustManager());
138-
if (tlsConf.getMtlsEnabled()) {
139-
GrpcUtil.setKeyManager(sslContextBuilder, tlsConf.getKeyManager());
140-
}
141-
try {
142-
channelBuilder.useTransportSecurity().sslContext(
143-
sslContextBuilder.build());
144-
} catch (Exception ex) {
145-
throw new RuntimeException(ex);
146-
}
132+
channelBuilder.useTransportSecurity().sslContext(sslContext);
147133
} else {
148134
channelBuilder.negotiationType(NegotiationType.PLAINTEXT);
149135
}

ratis-grpc/src/main/java/org/apache/ratis/grpc/client/GrpcClientProtocolProxy.java

Lines changed: 0 additions & 108 deletions
This file was deleted.

ratis-grpc/src/main/java/org/apache/ratis/grpc/client/GrpcClientRpc.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.apache.ratis.client.impl.RaftClientRpcWithProxy;
2222
import org.apache.ratis.conf.RaftProperties;
2323
import org.apache.ratis.grpc.GrpcConfigKeys;
24-
import org.apache.ratis.grpc.GrpcTlsConfig;
2524
import org.apache.ratis.grpc.GrpcUtil;
2625
import org.apache.ratis.protocol.*;
2726
import org.apache.ratis.protocol.exceptions.AlreadyClosedException;
@@ -36,6 +35,7 @@
3635
import org.apache.ratis.proto.RaftProtos.TransferLeadershipRequestProto;
3736
import org.apache.ratis.proto.RaftProtos.SnapshotManagementRequestProto;
3837
import org.apache.ratis.proto.RaftProtos.LeaderElectionManagementRequestProto;
38+
import org.apache.ratis.thirdparty.io.netty.handler.ssl.SslContext;
3939
import org.apache.ratis.util.IOUtils;
4040
import org.apache.ratis.util.JavaUtils;
4141
import org.apache.ratis.util.PeerProxyMap;
@@ -54,9 +54,9 @@ public class GrpcClientRpc extends RaftClientRpcWithProxy<GrpcClientProtocolClie
5454
private final int maxMessageSize;
5555

5656
public GrpcClientRpc(ClientId clientId, RaftProperties properties,
57-
GrpcTlsConfig adminTlsConfig, GrpcTlsConfig clientTlsConfig) {
57+
SslContext adminSslContext, SslContext clientSslContext) {
5858
super(new PeerProxyMap<>(clientId.toString(),
59-
p -> new GrpcClientProtocolClient(clientId, p, properties, adminTlsConfig, clientTlsConfig)));
59+
p -> new GrpcClientProtocolClient(clientId, p, properties, adminSslContext, clientSslContext)));
6060
this.clientId = clientId;
6161
this.maxMessageSize = GrpcConfigKeys.messageSizeMax(properties, LOG::debug).getSizeInt();
6262
}

0 commit comments

Comments
 (0)