Skip to content

Commit e03560f

Browse files
authored
Fix ratis TLS not working (apache#16510)
1 parent 2d44ef0 commit e03560f

File tree

6 files changed

+115
-7
lines changed

6 files changed

+115
-7
lines changed

iotdb-client/client-py/tests/integration/sqlalchemy/test_dialect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def test_dialect():
7777
# test get_schema_names
7878
schema_names = insp.get_schema_names()
7979
if not operator.ge(
80-
schema_names, ["root.__system", "root.cursor", "root.cursor_s1"]
80+
schema_names, ["root.__audit", "root.cursor", "root.cursor_s1"]
8181
):
8282
test_fail()
8383
print_message("Actual result " + str(schema_names))

iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/RatisClient.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.commons.pool2.impl.DefaultPooledObject;
2828
import org.apache.ratis.client.RaftClient;
2929
import org.apache.ratis.client.RaftClientRpc;
30+
import org.apache.ratis.conf.Parameters;
3031
import org.apache.ratis.conf.RaftProperties;
3132
import org.apache.ratis.protocol.RaftGroup;
3233
import org.apache.ratis.protocol.exceptions.LeaderSteppingDownException;
@@ -89,16 +90,19 @@ static class Factory extends BaseClientFactory<RaftGroup, RatisClient> {
8990
private final RaftProperties raftProperties;
9091
private final RaftClientRpc clientRpc;
9192
private final RatisConfig.Client config;
93+
private final Parameters parameters;
9294

9395
public Factory(
9496
ClientManager<RaftGroup, RatisClient> clientManager,
9597
RaftProperties raftProperties,
9698
RaftClientRpc clientRpc,
97-
RatisConfig.Client config) {
99+
RatisConfig.Client config,
100+
Parameters parameters) {
98101
super(clientManager);
99102
this.raftProperties = raftProperties;
100103
this.clientRpc = clientRpc;
101104
this.config = config;
105+
this.parameters = parameters;
102106
}
103107

104108
@Override
@@ -116,6 +120,7 @@ public PooledObject<RatisClient> makeObject(RaftGroup group) {
116120
.setRaftGroup(group)
117121
.setRetryPolicy(new RatisRetryPolicy(config))
118122
.setClientRpc(clientRpc)
123+
.setParameters(parameters)
119124
.build(),
120125
clientManager));
121126
}
@@ -131,16 +136,19 @@ static class EndlessRetryFactory extends BaseClientFactory<RaftGroup, RatisClien
131136
private final RaftProperties raftProperties;
132137
private final RaftClientRpc clientRpc;
133138
private final RatisConfig.Client config;
139+
private final Parameters parameters;
134140

135141
public EndlessRetryFactory(
136142
ClientManager<RaftGroup, RatisClient> clientManager,
137143
RaftProperties raftProperties,
138144
RaftClientRpc clientRpc,
139-
RatisConfig.Client config) {
145+
RatisConfig.Client config,
146+
Parameters parameters) {
140147
super(clientManager);
141148
this.raftProperties = raftProperties;
142149
this.clientRpc = clientRpc;
143150
this.config = config;
151+
this.parameters = parameters;
144152
}
145153

146154
@Override
@@ -157,6 +165,7 @@ public PooledObject<RatisClient> makeObject(RaftGroup group) {
157165
.setProperties(raftProperties)
158166
.setRaftGroup(group)
159167
.setRetryPolicy(new RatisEndlessRetryPolicy(config))
168+
.setParameters(parameters)
160169
.setClientRpc(clientRpc)
161170
.build(),
162171
clientManager));

iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/RatisConsensus.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ class RatisConsensus implements IConsensus {
122122

123123
private final RaftProperties properties = new RaftProperties();
124124
private final RaftClientRpc clientRpc;
125+
private final Parameters parameters;
125126

126127
private final IClientManager<RaftGroup, RatisClient> clientManager;
127128
private final IClientManager<RaftGroup, RatisClient> reconfigurationClientManager;
@@ -158,7 +159,7 @@ public RatisConsensus(ConsensusConfig config, IStateMachine.Registry registry) {
158159
RaftServerConfigKeys.setStorageDir(properties, Collections.singletonList(storageDir));
159160
GrpcConfigKeys.Server.setPort(properties, config.getThisNodeEndPoint().getPort());
160161

161-
Parameters parameters = Utils.initRatisConfig(properties, config.getRatisConfig());
162+
this.parameters = Utils.initRatisConfig(properties, config.getRatisConfig());
162163
this.config = config.getRatisConfig();
163164
this.readOption = this.config.getRead().getReadOption();
164165
this.canServeStaleRead =
@@ -223,6 +224,7 @@ public RatisConsensus(ConsensusConfig config, IStateMachine.Registry registry) {
223224
.setServerId(myself.getId())
224225
.setProperties(properties)
225226
.setOption(RaftStorage.StartupOption.RECOVER)
227+
.setParameters(parameters)
226228
.setStateMachineRegistry(
227229
raftGroupId ->
228230
new ApplicationStateMachineProxy(
@@ -1034,8 +1036,9 @@ public GenericKeyedObjectPool<RaftGroup, RatisClient> createClientPool(
10341036
new GenericKeyedObjectPool<>(
10351037
isReconfiguration
10361038
? new RatisClient.EndlessRetryFactory(
1037-
manager, properties, clientRpc, config.getClient())
1038-
: new RatisClient.Factory(manager, properties, clientRpc, config.getClient()),
1039+
manager, properties, clientRpc, config.getClient(), parameters)
1040+
: new RatisClient.Factory(
1041+
manager, properties, clientRpc, config.getClient(), parameters),
10391042
new ClientPoolProperty.Builder<RatisClient>()
10401043
.setMaxClientNumForEachNode(config.getClient().getMaxClientNumForEachNode())
10411044
.build()
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.iotdb.consensus.ratis.utils;
21+
22+
import javax.net.ssl.SSLEngine;
23+
import javax.net.ssl.X509ExtendedTrustManager;
24+
import javax.net.ssl.X509TrustManager;
25+
26+
import java.net.Socket;
27+
import java.security.cert.CertificateException;
28+
import java.security.cert.X509Certificate;
29+
30+
public class NoHostnameVerificationTrustManager extends X509ExtendedTrustManager {
31+
32+
private final X509TrustManager delegate;
33+
34+
public NoHostnameVerificationTrustManager(X509TrustManager delegate) {
35+
this.delegate = delegate;
36+
}
37+
38+
@Override
39+
public X509Certificate[] getAcceptedIssuers() {
40+
return delegate.getAcceptedIssuers();
41+
}
42+
43+
@Override
44+
public void checkClientTrusted(X509Certificate[] chain, String authType)
45+
throws CertificateException {
46+
delegate.checkClientTrusted(chain, authType);
47+
}
48+
49+
@Override
50+
public void checkServerTrusted(X509Certificate[] chain, String authType)
51+
throws CertificateException {
52+
delegate.checkServerTrusted(chain, authType);
53+
}
54+
55+
@Override
56+
public void checkClientTrusted(X509Certificate[] chain, String authType, Socket socket)
57+
throws CertificateException {
58+
if (delegate instanceof X509ExtendedTrustManager) {
59+
((X509ExtendedTrustManager) delegate).checkClientTrusted(chain, authType, socket);
60+
} else {
61+
delegate.checkClientTrusted(chain, authType);
62+
}
63+
}
64+
65+
@Override
66+
public void checkServerTrusted(X509Certificate[] chain, String authType, Socket socket)
67+
throws CertificateException {
68+
// Skip hostname check by calling base method
69+
delegate.checkServerTrusted(chain, authType);
70+
}
71+
72+
@Override
73+
public void checkClientTrusted(X509Certificate[] chain, String authType, SSLEngine engine)
74+
throws CertificateException {
75+
if (delegate instanceof X509ExtendedTrustManager) {
76+
((X509ExtendedTrustManager) delegate).checkClientTrusted(chain, authType, engine);
77+
} else {
78+
delegate.checkClientTrusted(chain, authType);
79+
}
80+
}
81+
82+
@Override
83+
public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngine engine)
84+
throws CertificateException {
85+
// Skip hostname check by calling base method
86+
delegate.checkServerTrusted(chain, authType);
87+
}
88+
}

iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/utils/Utils.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import javax.net.ssl.KeyManagerFactory;
5656
import javax.net.ssl.TrustManager;
5757
import javax.net.ssl.TrustManagerFactory;
58+
import javax.net.ssl.X509TrustManager;
5859

5960
import java.io.File;
6061
import java.io.InputStream;
@@ -385,7 +386,13 @@ public static Parameters initRatisConfig(RaftProperties properties, RatisConfig
385386
TrustManagerFactory tmf =
386387
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
387388
tmf.init(trustStore);
388-
TrustManager trustManager = tmf.getTrustManagers()[0];
389+
TrustManager originalTrustManager = tmf.getTrustManagers()[0];
390+
391+
// The self-signed certification may not set Subject Alternative Name (SAN)
392+
// Thrift with ssl didn't check it, but Grpc did.
393+
// Wrap to disable the verification
394+
TrustManager trustManager =
395+
new NoHostnameVerificationTrustManager((X509TrustManager) originalTrustManager);
389396
GrpcConfigKeys.TLS.setConf(parameters, new GrpcTlsConfig(keyManager, trustManager, true));
390397
} catch (Exception e) {
391398
LOGGER.error("Failed to read key store or trust store.", e);

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@
155155
<sonar.coverage.jacoco.xmlReportPaths>target/jacoco-merged-reports/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
156156
<!-- Exclude all generated code -->
157157
<sonar.exclusions>**/generated-sources</sonar.exclusions>
158+
<sonar.test.exclusions>**/test/**</sonar.test.exclusions>
158159
<!-- URL of the ASF SonarQube server -->
159160
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
160161
<sonar.java.checkstyle.reportPaths>target/checkstyle-report.xml</sonar.java.checkstyle.reportPaths>

0 commit comments

Comments
 (0)