Skip to content

Commit a2747da

Browse files
authored
[ISSUE #9769] Add tls.ciphers and tls.protocols in system properties (#9770)
1 parent ccdf9a6 commit a2747da

File tree

4 files changed

+64
-12
lines changed

4 files changed

+64
-12
lines changed

docs/cn/Configuration_TLS.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ tls.server.certPath=/opt/certFiles/server.pem
5252
tls.server.authClient=false
5353
# The store path of trusted certificates for verifying the client endpoint's certificate
5454
tls.server.trustCertPath=/opt/certFiles/ca.pem
55+
# The ciphers in TLS
56+
# tls.ciphers=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
57+
# The protocols in TLS
58+
# tls.protocols=TLSv1.2,TLSv1.3
5559
```
5660

5761
如果需要客户端连接时也进行认证,则还需要在该文件中增加以下内容
@@ -66,6 +70,10 @@ tls.client.certPath=/opt/certFiles/client.pem
6670
tls.client.authServer=false
6771
# The store path of trusted certificates for verifying the server endpoint's certificate
6872
tls.client.trustCertPath=/opt/certFiles/ca.pem
73+
# The ciphers in TLS
74+
# tls.ciphers=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
75+
# The protocols in TLS
76+
# tls.protocols=TLSv1.2,TLSv1.3
6977
```
7078

7179

@@ -96,6 +104,10 @@ tls.client.keyPassword=123456
96104
tls.client.certPath=/opt/certFiles/client.pem
97105
# The store path of trusted certificates for verifying the server endpoint's certificate
98106
tls.client.trustCertPath=/opt/certFiles/ca.pem
107+
# The ciphers in TLS
108+
# tls.ciphers=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
109+
# The protocols in TLS
110+
# tls.protocols=TLSv1.2,TLSv1.3
99111
```
100112

101113
JVM中需要加以下参数.tls.config.file的值需要使用之前创建的文件:

proxy/src/main/java/org/apache/rocketmq/proxy/remoting/MultiProtocolTlsHelper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public static SslContext buildSslContext() throws IOException, CertificateExcept
9494
ApplicationProtocolConfig.SelectedListenerFailureBehavior.ACCEPT,
9595
ApplicationProtocolNames.HTTP_2));
9696

97+
moreTlsConfig(sslContextBuilder);
9798
return sslContextBuilder.build();
9899
}
99100

remoting/src/main/java/org/apache/rocketmq/remoting/netty/TlsHelper.java

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,33 @@
2929
import java.io.IOException;
3030
import java.io.InputStream;
3131
import java.security.cert.CertificateException;
32+
import java.util.Arrays;
3233
import java.util.Properties;
3334
import org.apache.rocketmq.common.constant.LoggerName;
3435
import org.apache.rocketmq.logging.org.slf4j.Logger;
3536
import org.apache.rocketmq.logging.org.slf4j.LoggerFactory;
3637

38+
import static org.apache.rocketmq.remoting.netty.TlsSystemConfig.TLS_CIPHERS;
3739
import static org.apache.rocketmq.remoting.netty.TlsSystemConfig.TLS_CLIENT_AUTHSERVER;
3840
import static org.apache.rocketmq.remoting.netty.TlsSystemConfig.TLS_CLIENT_CERTPATH;
3941
import static org.apache.rocketmq.remoting.netty.TlsSystemConfig.TLS_CLIENT_KEYPASSWORD;
4042
import static org.apache.rocketmq.remoting.netty.TlsSystemConfig.TLS_CLIENT_KEYPATH;
4143
import static org.apache.rocketmq.remoting.netty.TlsSystemConfig.TLS_CLIENT_TRUSTCERTPATH;
44+
import static org.apache.rocketmq.remoting.netty.TlsSystemConfig.TLS_PROTOCOLS;
4245
import static org.apache.rocketmq.remoting.netty.TlsSystemConfig.TLS_SERVER_AUTHCLIENT;
4346
import static org.apache.rocketmq.remoting.netty.TlsSystemConfig.TLS_SERVER_CERTPATH;
4447
import static org.apache.rocketmq.remoting.netty.TlsSystemConfig.TLS_SERVER_KEYPASSWORD;
4548
import static org.apache.rocketmq.remoting.netty.TlsSystemConfig.TLS_SERVER_KEYPATH;
4649
import static org.apache.rocketmq.remoting.netty.TlsSystemConfig.TLS_SERVER_NEED_CLIENT_AUTH;
4750
import static org.apache.rocketmq.remoting.netty.TlsSystemConfig.TLS_SERVER_TRUSTCERTPATH;
4851
import static org.apache.rocketmq.remoting.netty.TlsSystemConfig.TLS_TEST_MODE_ENABLE;
52+
import static org.apache.rocketmq.remoting.netty.TlsSystemConfig.tlsCiphers;
4953
import static org.apache.rocketmq.remoting.netty.TlsSystemConfig.tlsClientAuthServer;
5054
import static org.apache.rocketmq.remoting.netty.TlsSystemConfig.tlsClientCertPath;
5155
import static org.apache.rocketmq.remoting.netty.TlsSystemConfig.tlsClientKeyPassword;
5256
import static org.apache.rocketmq.remoting.netty.TlsSystemConfig.tlsClientKeyPath;
5357
import static org.apache.rocketmq.remoting.netty.TlsSystemConfig.tlsClientTrustCertPath;
58+
import static org.apache.rocketmq.remoting.netty.TlsSystemConfig.tlsProtocols;
5459
import static org.apache.rocketmq.remoting.netty.TlsSystemConfig.tlsServerAuthClient;
5560
import static org.apache.rocketmq.remoting.netty.TlsSystemConfig.tlsServerCertPath;
5661
import static org.apache.rocketmq.remoting.netty.TlsSystemConfig.tlsServerKeyPassword;
@@ -102,15 +107,15 @@ public static SslContext buildSslContext(boolean forClient) throws IOException,
102107
LOGGER.info("Using JDK SSL provider");
103108
}
104109

110+
SslContextBuilder sslContextBuilder = null;
105111
if (forClient) {
106112
if (tlsTestModeEnable) {
107-
return SslContextBuilder
113+
sslContextBuilder = SslContextBuilder
108114
.forClient()
109115
.sslProvider(SslProvider.JDK)
110-
.trustManager(InsecureTrustManagerFactory.INSTANCE)
111-
.build();
116+
.trustManager(InsecureTrustManagerFactory.INSTANCE);
112117
} else {
113-
SslContextBuilder sslContextBuilder = SslContextBuilder.forClient().sslProvider(SslProvider.JDK);
118+
sslContextBuilder = SslContextBuilder.forClient().sslProvider(SslProvider.JDK);
114119

115120

116121
if (!tlsClientAuthServer) {
@@ -121,23 +126,21 @@ public static SslContext buildSslContext(boolean forClient) throws IOException,
121126
}
122127
}
123128

124-
return sslContextBuilder.keyManager(
129+
sslContextBuilder = sslContextBuilder.keyManager(
125130
!isNullOrEmpty(tlsClientCertPath) ? new FileInputStream(tlsClientCertPath) : null,
126131
!isNullOrEmpty(tlsClientKeyPath) ? decryptionStrategy.decryptPrivateKey(tlsClientKeyPath, true) : null,
127-
!isNullOrEmpty(tlsClientKeyPassword) ? tlsClientKeyPassword : null)
128-
.build();
132+
!isNullOrEmpty(tlsClientKeyPassword) ? tlsClientKeyPassword : null);
129133
}
130134
} else {
131135

132136
if (tlsTestModeEnable) {
133137
SelfSignedCertificate selfSignedCertificate = new SelfSignedCertificate();
134-
return SslContextBuilder
138+
sslContextBuilder = SslContextBuilder
135139
.forServer(selfSignedCertificate.certificate(), selfSignedCertificate.privateKey())
136140
.sslProvider(provider)
137-
.clientAuth(ClientAuth.OPTIONAL)
138-
.build();
141+
.clientAuth(ClientAuth.OPTIONAL);
139142
} else {
140-
SslContextBuilder sslContextBuilder = SslContextBuilder.forServer(
143+
sslContextBuilder = SslContextBuilder.forServer(
141144
!isNullOrEmpty(tlsServerCertPath) ? new FileInputStream(tlsServerCertPath) : null,
142145
!isNullOrEmpty(tlsServerKeyPath) ? decryptionStrategy.decryptPrivateKey(tlsServerKeyPath, false) : null,
143146
!isNullOrEmpty(tlsServerKeyPassword) ? tlsServerKeyPassword : null)
@@ -152,11 +155,20 @@ public static SslContext buildSslContext(boolean forClient) throws IOException,
152155
}
153156

154157
sslContextBuilder.clientAuth(parseClientAuthMode(tlsServerNeedClientAuth));
155-
return sslContextBuilder.build();
156158
}
157159
}
160+
moreTlsConfig(sslContextBuilder);
161+
return sslContextBuilder.build();
158162
}
159163

164+
protected static void moreTlsConfig(SslContextBuilder sslContextBuilder) {
165+
if (tlsCiphers != null) {
166+
sslContextBuilder.ciphers(Arrays.asList(tlsCiphers.split(",")));
167+
}
168+
if (tlsProtocols != null) {
169+
sslContextBuilder.protocols(tlsProtocols.split(","));
170+
}
171+
}
160172
private static void extractTlsConfigFromFile(final File configFile) {
161173
if (!(configFile.exists() && configFile.isFile() && configFile.canRead())) {
162174
LOGGER.info("Tls config file doesn't exist, skip it");
@@ -192,6 +204,9 @@ private static void extractTlsConfigFromFile(final File configFile) {
192204
tlsClientCertPath = properties.getProperty(TLS_CLIENT_CERTPATH, tlsClientCertPath);
193205
tlsClientAuthServer = Boolean.parseBoolean(properties.getProperty(TLS_CLIENT_AUTHSERVER, String.valueOf(tlsClientAuthServer)));
194206
tlsClientTrustCertPath = properties.getProperty(TLS_CLIENT_TRUSTCERTPATH, tlsClientTrustCertPath);
207+
208+
tlsCiphers = properties.getProperty(TLS_CIPHERS, tlsCiphers);
209+
tlsProtocols = properties.getProperty(TLS_PROTOCOLS, tlsProtocols);
195210
}
196211

197212
private static void logTheFinalUsedTlsConfig() {
@@ -207,6 +222,9 @@ private static void logTheFinalUsedTlsConfig() {
207222
LOGGER.debug("{} = {}", TLS_CLIENT_CERTPATH, tlsClientCertPath);
208223
LOGGER.debug("{} = {}", TLS_CLIENT_AUTHSERVER, tlsClientAuthServer);
209224
LOGGER.debug("{} = {}", TLS_CLIENT_TRUSTCERTPATH, tlsClientTrustCertPath);
225+
226+
LOGGER.debug("{} = {}", TLS_CIPHERS, tlsCiphers);
227+
LOGGER.debug("{} = {}", TLS_PROTOCOLS, tlsProtocols);
210228
}
211229

212230
private static ClientAuth parseClientAuthMode(String authMode) {

remoting/src/main/java/org/apache/rocketmq/remoting/netty/TlsSystemConfig.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public class TlsSystemConfig {
3939
public static final String TLS_CLIENT_AUTHSERVER = "tls.client.authServer";
4040
public static final String TLS_CLIENT_TRUSTCERTPATH = "tls.client.trustCertPath";
4141

42+
public static final String TLS_CIPHERS = "tls.ciphers";
43+
public static final String TLS_PROTOCOLS = "tls.protocols";
44+
4245

4346
/**
4447
* To determine whether use SSL in client-side, include SDK client and BrokerOuterAPI
@@ -122,4 +125,22 @@ public class TlsSystemConfig {
122125
* except {@link TlsSystemConfig#tlsMode} and {@link TlsSystemConfig#tlsEnable}
123126
*/
124127
public static String tlsConfigFile = System.getProperty(TLS_CONFIG_FILE, "/etc/rocketmq/tls.properties");
128+
129+
/**
130+
* The ciphers to be used in TLS
131+
* <ol>
132+
* <li>If null, use the default ciphers</li>
133+
* <li>Otherwise, use the ciphers specified in this string, eg: -Dtls.ciphers=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256</li>
134+
* </ol>
135+
*/
136+
public static String tlsCiphers = System.getProperty(TLS_CIPHERS, null);
137+
138+
/**
139+
* The protocols to be used in TLS
140+
* <ol>
141+
* <li>If null, use the default protocols</li>
142+
* <li>Otherwise, use the protocols specified in this string, eg: -Dtls.protocols=TLSv1.2,TLSv1.3</li>
143+
* </ol>
144+
*/
145+
public static String tlsProtocols = System.getProperty(TLS_PROTOCOLS, null);
125146
}

0 commit comments

Comments
 (0)