Skip to content

Commit 93fa116

Browse files
committed
Add data security document
1 parent 1830df6 commit 93fa116

2 files changed

Lines changed: 664 additions & 0 deletions

File tree

Lines changed: 332 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,332 @@
1+
<!--
2+
3+
Licensed to the Apache Software Foundation (ASF) under one
4+
or more contributor license agreements. See the NOTICE file
5+
distributed with this work for additional information
6+
regarding copyright ownership. The ASF licenses this file
7+
to you under the Apache License, Version 2.0 (the
8+
"License"); you may not use this file except in compliance
9+
with the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing,
14+
software distributed under the License is distributed on an
15+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
KIND, either express or implied. See the License for the
17+
specific language governing permissions and limitations
18+
under the License.
19+
20+
-->
21+
22+
# 数据安全
23+
24+
## 1. 数据存储加密
25+
### 1.1 概述
26+
27+
IoTDB 通过内置的透明加密引擎支持对数据文件 TsFile 的加密防护。启用加密功能后,即可实现数据在写入时实时加密、读取时无缝解密的全流程自动化处理,保证落盘文件数据安全。该过程完全融入数据库内核,业务应用无感知且无需进行任何代码改造或查询逻辑调整。
28+
29+
### 1.2 参数配置
30+
31+
IoTDB 配置文件` ${IOTDB_HOME}/${IOTDB_CONF}/iotdb-system.properties`中数据加密相关的参数如下所示:
32+
33+
| 参数 | 注释 |
34+
| :----------------- | :----------------------------------------------------------- |
35+
| `encrypt flag` | `true`为开启加密 |
36+
| `encrypt_key_path` | 文件导入的主密钥文件路径 |
37+
| `encrypt type` | 加密方式,例如"`org.apache.tsfile.encrypt.UNENCRYPTED`"。目前包括`AES128``UNENCRYPTED(SM4128)`|
38+
39+
### 1.3 使用流程
40+
41+
1. 获取 加密算法 Jar 包
42+
2. 将 Jar 包放置于 IoTDB 部署路径下 lib 文件夹中,即 `${IOTDB_HOME}/lib`
43+
3. 修改配置文件 ` ${IOTDB_HOME}/${IOTDB_CONF}/iotdb-system.properties`
44+
45+
```Properties
46+
encrypt_flag=true
47+
encrypt_type=org.example.encrypt.AES128.AES128
48+
# encrypt_type=org.example.encrypt.SM4128.5M41283
49+
encrypt_key_path=......
50+
```
51+
52+
4. 启动 IoTDB 即可使用对应加密算法对 TsFile 文件进行加密。
53+
54+
### 1.4 注意事项
55+
56+
- 开启加密后,对合并功能无影响。但不支持系统内存在与配置不同的加密文件,以免触发合并时发生冲突。
57+
- 开启加密后禁用 Pipe。
58+
- 开启加密后禁用 Load TsFile。
59+
- 建议对主密钥文件路径进行一定的保护,例如存放在特定权限才能访问的磁盘上等。
60+
61+
## 2. 数据传输加密
62+
63+
### 2.1 概述
64+
65+
在某些客户的生产环境中,对于数据传输的安全性要求更高,希望有更安全的传输方式,因此扩展了 Datanode 的 Client RPC 以支持数据传输加密。以下是支持数据传输加密的方式:
66+
67+
1. Thrift 除了普通模式与压缩模式外,增加 SSL 模式
68+
2. SessionPool(java) 与 Session(java) 支持使用 SSL 模式连接
69+
3. JDBC 支持使用 SSL 模式连接
70+
4. CLI 支持使用 SSL 模式连接
71+
5. 支持压缩模式
72+
6. 支持 RSA ECDSA 证书
73+
1. RSA证书:算法为RSA,签名算法为SHA256withRSA
74+
2. ECDSA 证书:算法为EC,签名算法为SHA256withECDSA
75+
76+
### 2.2 Server端配置
77+
78+
iotdb-datanode.properties 的默认配置,默认 ssl 是关闭状态。
79+
80+
#### 2.2.1 默认配置示例:
81+
82+
```Properties
83+
#iotdb-datanode.properties的默认配置
84+
.......
85+
# is SSL enabled
86+
#enable_thrift_ssl=false
87+
88+
# SSL key store path
89+
#key_store_path=
90+
91+
# SSL key store password
92+
#key_store_pwd=
93+
.......
94+
```
95+
96+
#### 2.2.2 启用 ssl 配置步骤:
97+
98+
1. 在iotdb/conf/iotdb-datanode.properties 配置文件中找到enable_thrift_ssl 配置默认是false,修改成true
99+
2. 修改key_store_path,添加生成的keystore路径
100+
3. 修改key_store_pwd为生成keystore的密码(秘钥库口令)
101+
4. 启动iotdb服务
102+
103+
启用 ssl 配置后,需重启 iotdb 服务。同时,对应的使用到 dn_rpc_port 接口的客户端都需要使用 ssl 方式访问,否则不能访问。
104+
105+
```SQL
106+
# is SSL enabled
107+
enable_thrift_ssl=true
108+
109+
# SSL key store path
110+
key_store_path=/Users/keystore/.keystore
111+
112+
# SSL key store password
113+
key_store_pwd=123456
114+
```
115+
116+
### 2.3 Java SessionPool
117+
118+
SessionPool 客户端配置ssl 需要配置useSSL、trustStore、trustStorePwd。
119+
120+
- useSSL :是否开启ssl
121+
- trustStore :truststore的证书路径
122+
- trustStorePwd:truststore的证书密码(秘钥库口令)
123+
124+
#### 2.3.1 代码示例:
125+
126+
```SQL
127+
sessionPool =
128+
new SessionPool.Builder()
129+
.nodeUrls(nodeUrls)
130+
.user("root")
131+
.password("root")
132+
.maxSize(3)
133+
.useSSL(true)
134+
.trustStore("/Users/key/.truststore")
135+
.trustStorePwd("123456")
136+
.build();
137+
```
138+
139+
### 2.4 Java Session
140+
141+
Session 客户端配置ssl 需要配置useSSL、trustStore、trustStorePwd 代码示例
142+
143+
- useSSL :是否开启ssl
144+
- trustStore :truststore的证书路径
145+
- trustStorePwd:truststore的证书密码(秘钥库口令)
146+
147+
#### 2.4.1 代码示例:
148+
149+
```SQL
150+
session =
151+
new Session.Builder()
152+
.host(LOCAL_HOST)
153+
.port(6667)
154+
.username("root")
155+
.password("root")
156+
.version(Version.V_1_0)
157+
.useSSL(true)
158+
.trustStore("/Users/key/.truststore")
159+
.trustStorePwd("123456")
160+
.build();
161+
```
162+
163+
### 2.5 JDBC
164+
165+
jdbc 支持两种方式的ssl 配置,需要配置use_ssl、trust_store、trust_store_pwd。
166+
167+
- use_ssl:是否开启ssl 默认是false
168+
- trust_store:truststore的证书路径
169+
- trust_store_pwd:truststore证书的密码(秘钥库口令)
170+
171+
#### 2.5.1 代码示例:
172+
173+
**方式一、在jdbc 的url 中配置使用**
174+
175+
jdbc:iotdb://ip:port?use_ssl=true&trust_store_pwd=123456&trust_store=/Users/keystore/.truststore
176+
177+
```SQL
178+
try (Connection connection =
179+
DriverManager.getConnection(
180+
"jdbc:iotdb://127.0.0.1:6667?version=V_1_0&use_ssl=true&trust_store_pwd=123456&trust_store=/Users/keystore/.truststore",
181+
"root",
182+
"root");Statement statement = connection.createStatement(){
183+
// 代码逻辑
184+
185+
}catch (IoTDBSQLException e) {
186+
logger.error("IoTDB Jdbc example error", e);
187+
}
188+
```
189+
190+
**方式二、使用Properties 来配置使用**
191+
192+
```SQL
193+
java.util.Properties info = new Properties();
194+
info.setProperty("use_ssl", "true");
195+
info.setProperty("user", "root");
196+
info.setProperty("password", "root");
197+
info.setProperty("trust_store", "/Users/keystore/.truststore");
198+
info.setProperty("trust_store_pwd","123456");
199+
try (Connection connection =
200+
DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667?version=V_1_0", info);
201+
Statement statement = connection.createStatement()) {
202+
// 代码逻辑
203+
204+
}catch (IoTDBSQLException e) {
205+
logger.error("IoTDB Jdbc example error", e);
206+
}
207+
```
208+
209+
### 2.6 CLI
210+
211+
```SQL
212+
./start-cli.sh -h 127.0.0.1 -p 6667 -u root -pw root -usessl true -ts /Users/keystore/.truststore -tpw 123456
213+
```
214+
215+
### 2.7 SSL证书生成方式
216+
217+
需安装 JDK、测试jdk8和jdk11生成证书未发现问题
218+
219+
1. 创建私钥(.keystore)
220+
221+
```SQL
222+
keytool -genkeypair -alias mykey -keyalg RSA -validity 7 -keystore .keystore
223+
```
224+
225+
2. 导出对应的证书
226+
227+
```SQL
228+
keytool -export -alias mykey -keystore .keystore -rfc -file certificate.cer
229+
```
230+
231+
3. 将该证书导入到客户端信任库(公钥:.truststore)
232+
233+
```SQL
234+
keytool -import -alias mykey -file certificate.cer -keystore .truststore
235+
```
236+
237+
4. 创建 ECDSA 证书
238+
239+
提示:FIPS标准规定使用 RSA、ECDSA 或 EdDSA(23年刚支持)
240+
241+
```Bash
242+
keytool -genkeypair -alias mykey -keyalg EC -validity 7 -keystore aa.keystore
243+
244+
keytool -export -alias mykey -keystore aa.keystore -rfc -file certificate.cer
245+
246+
keytool -import -alias mykey -file certificate.cer -keystore aa.truststore
247+
```
248+
249+
### 2.8 性能测试
250+
251+
#### 2.8.1 插入测试
252+
253+
使用session 的inserttablet方法,使用100 设备*100测点,每批 100 行 loop 100 次,测试3遍,三次插入使用不同的设备名字,测试环境:1c1d,mac 16G,Intel Core i5。
254+
255+
**代码示例:**
256+
257+
```Properties
258+
for(int i=0;i<100;i++){
259+
insertTablet(i+"");
260+
}
261+
private static void insertTablet(String d) throws IoTDBConnectionException, StatementExecutionException {
262+
List<MeasurementSchema> schemaList = new ArrayList<>();
263+
for(int i=1;i<=100;i++){
264+
schemaList.add(new MeasurementSchema("s"+i, TSDataType.INT64));
265+
}
266+
267+
Tablet tablet = new Tablet(ROOT_SG1_D1+d, schemaList, 100);
268+
long timestamp = System.currentTimeMillis();
269+
270+
for (long row = 0; row < 100; row++) {
271+
int rowIndex = tablet.rowSize++;
272+
tablet.addTimestamp(rowIndex, timestamp);
273+
for (int s = 0; s < 100; s++) {
274+
long value = random.nextLong();
275+
tablet.addValue(schemaList.get(s).getMeasurementId(), rowIndex, value);
276+
}
277+
if (tablet.rowSize == tablet.getMaxRowNumber()) {
278+
session.insertTablet(tablet, true);
279+
tablet.reset();
280+
}
281+
timestamp++;
282+
}
283+
284+
if (tablet.rowSize != 0) {
285+
session.insertTablet(tablet);
286+
tablet.reset();
287+
}
288+
289+
}
290+
```
291+
292+
**插入测试结果:**
293+
294+
| | ssl(单位ms) | 非ssl(单位ms) |
295+
| ------ | ------------- | --------------- |
296+
| 第一次 | 13611 | 14099 |
297+
| 第二次 | 11938 | 10236 |
298+
| 第三次 | 13801 | 13663 |
299+
300+
**测试结论:**
301+
302+
根据ssl 三次取平均值为13117ms、非ssl取平均值12666ms,综合测试结果可以看出非ssl 插入速度略快ssl(`3.5%`)
303+
304+
#### 2.8.2 查询测试
305+
306+
使用session 的executeQueryStatement方法、循环100次查询不同设备的测点,测试3遍,查询完后修改ssl重启iotdb,三次查询使用不同的设备名字,测试环境:1c1d,mac 16G,Intel Core i5
307+
308+
**代码示例:**
309+
310+
```Properties
311+
private static void query(String d) throws IoTDBConnectionException, StatementExecutionException {
312+
try (SessionDataSet dataSet = session.executeQueryStatement("select * from root.sg1.d9"+d)) {
313+
System.out.println(dataSet.getColumnNames());
314+
dataSet.setFetchSize(1024); // default is 10000
315+
while (dataSet.hasNext()) {
316+
System.out.println(dataSet.next());
317+
}
318+
}
319+
}
320+
```
321+
322+
**查询测试结果:**
323+
324+
| | ssl(单位ms) | 非ssl(单位ms) |
325+
| ------ | ------------- | --------------- |
326+
| 第一次 | 7101 | 6111 |
327+
| 第二次 | 3109 | 2162 |
328+
| 第三次 | 3594 | 3032 |
329+
330+
**测试结论:**
331+
332+
根据ssl 三次取平均值为4601ms、非ssl取平均值3768ms,综合测试结果可以看出非ssl 插入速度略快ssl(`22%`

0 commit comments

Comments
 (0)