Skip to content

Commit f36bcb8

Browse files
mbwhitejt-nti
authored andcommitted
Port of ALPN fix for TLS
Signed-off-by: Matthew B White <whitemat@uk.ibm.com>
1 parent 50d52fa commit f36bcb8

File tree

8 files changed

+48
-22
lines changed

8 files changed

+48
-22
lines changed

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/NettyGrpcServer.java

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import io.grpc.Server;
1111
import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder;
1212
import io.grpc.netty.shaded.io.netty.handler.ssl.SslContextBuilder;
13+
import io.grpc.netty.shaded.io.netty.handler.ssl.ApplicationProtocolConfig;
14+
import io.grpc.netty.shaded.io.netty.handler.ssl.ApplicationProtocolNames;
1315
import org.apache.commons.logging.Log;
1416
import org.apache.commons.logging.LogFactory;
1517

@@ -23,7 +25,7 @@
2325
*/
2426
public final class NettyGrpcServer implements GrpcServer {
2527

26-
private static Log logger = LogFactory.getLog(NettyGrpcServer.class);
28+
private static final Log LOGGER = LogFactory.getLog(NettyGrpcServer.class);
2729

2830
private final Server server;
2931
/**
@@ -53,30 +55,40 @@ public NettyGrpcServer(final ChaincodeBase chaincodeBase, final ChaincodeServerP
5355
.maxInboundMessageSize(chaincodeServerProperties.getMaxInboundMessageSize());
5456

5557
if (chaincodeServerProperties.isTlsEnabled()) {
58+
SslContextBuilder sslContextBuilder;
5659
final File keyCertChainFile = Paths.get(chaincodeServerProperties.getKeyCertChainFile()).toFile();
5760
final File keyFile = Paths.get(chaincodeServerProperties.getKeyFile()).toFile();
5861

5962
if (chaincodeServerProperties.getKeyPassword() == null || chaincodeServerProperties.getKeyPassword().isEmpty()) {
60-
serverBuilder.sslContext(SslContextBuilder.forServer(keyCertChainFile, keyFile).build());
63+
sslContextBuilder = SslContextBuilder.forServer(keyCertChainFile, keyFile);
6164
} else {
62-
serverBuilder.sslContext(SslContextBuilder.forServer(keyCertChainFile, keyFile, chaincodeServerProperties.getKeyPassword()).build());
65+
sslContextBuilder = SslContextBuilder.forServer(keyCertChainFile, keyFile, chaincodeServerProperties.getKeyPassword());
6366
}
67+
68+
ApplicationProtocolConfig apn = new ApplicationProtocolConfig(
69+
ApplicationProtocolConfig.Protocol.ALPN,
70+
ApplicationProtocolConfig.SelectorFailureBehavior.NO_ADVERTISE,
71+
ApplicationProtocolConfig.SelectedListenerFailureBehavior.ACCEPT,
72+
ApplicationProtocolNames.HTTP_2);
73+
sslContextBuilder.applicationProtocolConfig(apn);
74+
75+
serverBuilder.sslContext(sslContextBuilder.build());
6476
}
6577

66-
logger.info("<<<<<<<<<<<<<chaincodeServerProperties>>>>>>>>>>>>:\n");
67-
logger.info("PortChaincodeServer:" + chaincodeServerProperties.getPortChaincodeServer());
68-
logger.info("MaxInboundMetadataSize:" + chaincodeServerProperties.getMaxInboundMetadataSize());
69-
logger.info("MaxInboundMessageSize:" + chaincodeServerProperties.getMaxInboundMessageSize());
70-
logger.info("MaxConnectionAgeSeconds:" + chaincodeServerProperties.getMaxConnectionAgeSeconds());
71-
logger.info("KeepAliveTimeoutSeconds:" + chaincodeServerProperties.getKeepAliveTimeoutSeconds());
72-
logger.info("PermitKeepAliveTimeMinutes:" + chaincodeServerProperties.getPermitKeepAliveTimeMinutes());
73-
logger.info("KeepAliveTimeMinutes:" + chaincodeServerProperties.getKeepAliveTimeMinutes());
74-
logger.info("PermitKeepAliveWithoutCalls:" + chaincodeServerProperties.getPermitKeepAliveWithoutCalls());
75-
logger.info("KeyPassword:" + chaincodeServerProperties.getKeyPassword());
76-
logger.info("KeyCertChainFile:" + chaincodeServerProperties.getKeyCertChainFile());
77-
logger.info("KeyFile:" + chaincodeServerProperties.getKeyFile());
78-
logger.info("isTlsEnabled:" + chaincodeServerProperties.isTlsEnabled());
79-
logger.info("\n");
78+
LOGGER.info("<<<<<<<<<<<<<chaincodeServerProperties>>>>>>>>>>>>:\n");
79+
LOGGER.info("PortChaincodeServer:" + chaincodeServerProperties.getPortChaincodeServer());
80+
LOGGER.info("MaxInboundMetadataSize:" + chaincodeServerProperties.getMaxInboundMetadataSize());
81+
LOGGER.info("MaxInboundMessageSize:" + chaincodeServerProperties.getMaxInboundMessageSize());
82+
LOGGER.info("MaxConnectionAgeSeconds:" + chaincodeServerProperties.getMaxConnectionAgeSeconds());
83+
LOGGER.info("KeepAliveTimeoutSeconds:" + chaincodeServerProperties.getKeepAliveTimeoutSeconds());
84+
LOGGER.info("PermitKeepAliveTimeMinutes:" + chaincodeServerProperties.getPermitKeepAliveTimeMinutes());
85+
LOGGER.info("KeepAliveTimeMinutes:" + chaincodeServerProperties.getKeepAliveTimeMinutes());
86+
LOGGER.info("PermitKeepAliveWithoutCalls:" + chaincodeServerProperties.getPermitKeepAliveWithoutCalls());
87+
LOGGER.info("KeyPassword:" + chaincodeServerProperties.getKeyPassword());
88+
LOGGER.info("KeyCertChainFile:" + chaincodeServerProperties.getKeyCertChainFile());
89+
LOGGER.info("KeyFile:" + chaincodeServerProperties.getKeyFile());
90+
LOGGER.info("isTlsEnabled:" + chaincodeServerProperties.isTlsEnabled());
91+
LOGGER.info("\n");
8092

8193
this.server = serverBuilder.build();
8294
}
@@ -87,11 +99,11 @@ public NettyGrpcServer(final ChaincodeBase chaincodeBase, final ChaincodeServerP
8799
* @throws IOException
88100
*/
89101
public void start() throws IOException {
90-
logger.info("start grpc server");
102+
LOGGER.info("start grpc server");
91103
Runtime.getRuntime()
92104
.addShutdownHook(
93105
new Thread(() -> {
94-
// Use stderr here since the logger may have been reset by its JVM shutdown hook.
106+
// Use stderr here since the LOGGER may have been reset by its JVM shutdown hook.
95107
System.err.println("*** shutting down gRPC server since JVM is shutting down");
96108
NettyGrpcServer.this.stop();
97109
System.err.println("*** server shut down");
@@ -105,15 +117,15 @@ public void start() throws IOException {
105117
* @throws InterruptedException
106118
*/
107119
public void blockUntilShutdown() throws InterruptedException {
108-
logger.info("Waits for the server to become terminated.");
120+
LOGGER.info("Waits for the server to become terminated.");
109121
server.awaitTermination();
110122
}
111123

112124
/**
113125
* shutdown now grpc server.
114126
*/
115127
public void stop() {
116-
logger.info("shutdown now grpc server.");
128+
LOGGER.info("shutdown now grpc server.");
117129
server.shutdownNow();
118130
}
119131
}

fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/NettyGrpcServerTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,12 @@ void startAndStopTlsPassword() {
288288
try {
289289
final ChaincodeBase chaincodeBase = new EmptyChaincode();
290290
chaincodeBase.processEnvironmentOptions();
291-
ChaincodeServer chaincodeServer = new NettyChaincodeServer(chaincodeBase, new ChaincodeServerProperties());
291+
final ChaincodeServerProperties chaincodeServerProperties = new ChaincodeServerProperties();
292+
chaincodeServerProperties.setTlsEnabled(true);
293+
chaincodeServerProperties.setKeyFile("src/test/resources/client.key.password-protected");
294+
chaincodeServerProperties.setKeyCertChainFile("src/test/resources/client.crt");
295+
chaincodeServerProperties.setKeyPassword("test");
296+
ChaincodeServer chaincodeServer = new NettyChaincodeServer(chaincodeBase, chaincodeServerProperties);
292297
new Thread(() -> {
293298
try {
294299
chaincodeServer.start();
@@ -315,6 +320,9 @@ void startAndStopTlsWithoutPassword() {
315320
final ChaincodeBase chaincodeBase = new EmptyChaincode();
316321
chaincodeBase.processEnvironmentOptions();
317322
final ChaincodeServerProperties chaincodeServerProperties = new ChaincodeServerProperties();
323+
chaincodeServerProperties.setTlsEnabled(true);
324+
chaincodeServerProperties.setKeyFile("src/test/resources/client.key");
325+
chaincodeServerProperties.setKeyCertChainFile("src/test/resources/client.crt");
318326
ChaincodeServer chaincodeServer = new NettyChaincodeServer(chaincodeBase, chaincodeServerProperties);
319327
new Thread(() -> {
320328
try {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-----BEGIN ENCRYPTED PRIVATE KEY-----
2+
MIGxMBwGCiqGSIb3DQEMAQMwDgQIfzm0IqTm+rACAggABIGQDY1vpaSD+KDuVRyT
3+
Gi35536iOYUuVoz01ktV3YCDv03Pm5+8xZ1JXXW8lDM3JP/TcKbocRRk63y/R7O2
4+
dB9kcyV7/gYtYH0B3TMk1/x1WtfHL8JnYRFHQ/OuhYjJ6O04B4aY2waeYByzsIsI
5+
YhNVZq5fZ7/bjsy8b54o57WD4DDHH3uRysbv8I5TaDVyJMJq
6+
-----END ENCRYPTED PRIVATE KEY-----

0 commit comments

Comments
 (0)