Skip to content

Commit 68708fe

Browse files
authored
The Python SDK supports SSL encryption (#684)
1 parent 2657d0e commit 68708fe

4 files changed

Lines changed: 374 additions & 0 deletions

File tree

src/UserGuide/Master/Table/API/Programming-Python-Native-API.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,100 @@ class TableSessionPoolConfig(object):
277277
- Ensure that `TableSession` instances retrieved from the `TableSessionPool` are properly closed after use.
278278
- After closing the `TableSessionPool`, it will no longer be possible to retrieve new sessions.
279279

280+
### 3.3 SSL Connection
281+
282+
#### 3.3.1 Server Certificate Configuration
283+
284+
In the `conf/iotdb-system.properties` configuration file, locate or add the following configuration items:
285+
286+
```Java
287+
enable_thrift_ssl=true
288+
key_store_path=/path/to/your/server_keystore.jks
289+
key_store_pwd=your_keystore_password
290+
```
291+
292+
#### 3.3.2 Configure Python Client Certificate
293+
294+
- Set `use_ssl` to True to enable SSL.
295+
- Specify the client certificate path using the `ca_certs` parameter.
296+
297+
```Java
298+
use_ssl = True
299+
ca_certs = "/path/to/your/server.crt" # 或 ca_certs = "/path/to/your//ca_cert.pem"
300+
```
301+
**Example Code: Using SSL to Connect to IoTDB**
302+
303+
```Java
304+
# Licensed to the Apache Software Foundation (ASF) under one
305+
# or more contributor license agreements. See the NOTICE file
306+
# distributed with this work for additional information
307+
# regarding copyright ownership. The ASF licenses this file
308+
# to you under the Apache License, Version 2.0 (the
309+
# "License"); you may not use this file except in compliance
310+
# with the License. You may obtain a copy of the License at
311+
#
312+
# http://www.apache.org/licenses/LICENSE-2.0
313+
#
314+
# Unless required by applicable law or agreed to in writing,
315+
# software distributed under the License is distributed on an
316+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
317+
# KIND, either express or implied. See the License for the
318+
# specific language governing permissions and limitations
319+
# under the License.
320+
#
321+
322+
from iotdb.SessionPool import PoolConfig, SessionPool
323+
from iotdb.Session import Session
324+
325+
ip = "127.0.0.1"
326+
port_ = "6667"
327+
username_ = "root"
328+
password_ = "root"
329+
# Configure SSL enabled
330+
use_ssl = True
331+
# Configure certificate path
332+
ca_certs = "/path/server.crt"
333+
334+
335+
def get_data():
336+
session = Session(
337+
ip, port_, username_, password_, use_ssl=use_ssl, ca_certs=ca_certs
338+
)
339+
session.open(False)
340+
result = session.execute_query_statement("select * from root.eg.etth")
341+
df = result.todf()
342+
df.rename(columns={"Time": "date"}, inplace=True)
343+
session.close()
344+
return df
345+
346+
347+
def get_data2():
348+
pool_config = PoolConfig(
349+
host=ip,
350+
port=port_,
351+
user_name=username_,
352+
password=password_,
353+
fetch_size=1024,
354+
time_zone="UTC+8",
355+
max_retry=3,
356+
use_ssl=use_ssl,
357+
ca_certs=ca_certs,
358+
)
359+
max_pool_size = 5
360+
wait_timeout_in_ms = 3000
361+
session_pool = SessionPool(pool_config, max_pool_size, wait_timeout_in_ms)
362+
session = session_pool.get_session()
363+
result = session.execute_query_statement("select * from root.eg.etth")
364+
df = result.todf()
365+
df.rename(columns={"Time": "date"}, inplace=True)
366+
session_pool.put_back(session)
367+
session_pool.close()
368+
369+
370+
if __name__ == "__main__":
371+
df = get_data()
372+
```
373+
280374
## 4. Sample Code
281375

282376
**Session** Example: You can find the full example code at [Session Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/table_model_session_example.py).

src/UserGuide/latest-Table/API/Programming-Python-Native-API.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,100 @@ class TableSessionPoolConfig(object):
277277
- Ensure that `TableSession` instances retrieved from the `TableSessionPool` are properly closed after use.
278278
- After closing the `TableSessionPool`, it will no longer be possible to retrieve new sessions.
279279

280+
### 3.3 SSL Connection
281+
282+
#### 3.3.1 Server Certificate Configuration
283+
284+
In the `conf/iotdb-system.properties` configuration file, locate or add the following configuration items:
285+
286+
```Java
287+
enable_thrift_ssl=true
288+
key_store_path=/path/to/your/server_keystore.jks
289+
key_store_pwd=your_keystore_password
290+
```
291+
292+
#### 3.3.2 Configure Python Client Certificate
293+
294+
- Set `use_ssl` to True to enable SSL.
295+
- Specify the client certificate path using the `ca_certs` parameter.
296+
297+
```Java
298+
use_ssl = True
299+
ca_certs = "/path/to/your/server.crt" # 或 ca_certs = "/path/to/your//ca_cert.pem"
300+
```
301+
**Example Code: Using SSL to Connect to IoTDB**
302+
303+
```Java
304+
# Licensed to the Apache Software Foundation (ASF) under one
305+
# or more contributor license agreements. See the NOTICE file
306+
# distributed with this work for additional information
307+
# regarding copyright ownership. The ASF licenses this file
308+
# to you under the Apache License, Version 2.0 (the
309+
# "License"); you may not use this file except in compliance
310+
# with the License. You may obtain a copy of the License at
311+
#
312+
# http://www.apache.org/licenses/LICENSE-2.0
313+
#
314+
# Unless required by applicable law or agreed to in writing,
315+
# software distributed under the License is distributed on an
316+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
317+
# KIND, either express or implied. See the License for the
318+
# specific language governing permissions and limitations
319+
# under the License.
320+
#
321+
322+
from iotdb.SessionPool import PoolConfig, SessionPool
323+
from iotdb.Session import Session
324+
325+
ip = "127.0.0.1"
326+
port_ = "6667"
327+
username_ = "root"
328+
password_ = "root"
329+
# Configure SSL enabled
330+
use_ssl = True
331+
# Configure certificate path
332+
ca_certs = "/path/server.crt"
333+
334+
335+
def get_data():
336+
session = Session(
337+
ip, port_, username_, password_, use_ssl=use_ssl, ca_certs=ca_certs
338+
)
339+
session.open(False)
340+
result = session.execute_query_statement("select * from root.eg.etth")
341+
df = result.todf()
342+
df.rename(columns={"Time": "date"}, inplace=True)
343+
session.close()
344+
return df
345+
346+
347+
def get_data2():
348+
pool_config = PoolConfig(
349+
host=ip,
350+
port=port_,
351+
user_name=username_,
352+
password=password_,
353+
fetch_size=1024,
354+
time_zone="UTC+8",
355+
max_retry=3,
356+
use_ssl=use_ssl,
357+
ca_certs=ca_certs,
358+
)
359+
max_pool_size = 5
360+
wait_timeout_in_ms = 3000
361+
session_pool = SessionPool(pool_config, max_pool_size, wait_timeout_in_ms)
362+
session = session_pool.get_session()
363+
result = session.execute_query_statement("select * from root.eg.etth")
364+
df = result.todf()
365+
df.rename(columns={"Time": "date"}, inplace=True)
366+
session_pool.put_back(session)
367+
session_pool.close()
368+
369+
370+
if __name__ == "__main__":
371+
df = get_data()
372+
```
373+
280374
## 4. Sample Code
281375

282376
**Session** Example: You can find the full example code at [Session Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/table_model_session_example.py).

src/zh/UserGuide/Master/Table/API/Programming-Python-Native-API.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,99 @@ class TableSessionPoolConfig(object):
286286

287287
"""
288288
```
289+
### 3.3 SSL 连接
290+
291+
#### 3.3.1 服务器端配置证书
292+
293+
`conf/iotdb-system.properties` 配置文件中查找或添加以下配置项:
294+
295+
```Java
296+
enable_thrift_ssl=true
297+
key_store_path=/path/to/your/server_keystore.jks
298+
key_store_pwd=your_keystore_password
299+
```
300+
301+
#### 3.3.2 配置 python 客户端证书
302+
303+
- 设置 use_ssl 为 True 以启用 SSL。
304+
- 指定客户端证书路径,使用 ca_certs 参数。
305+
306+
```Java
307+
use_ssl = True
308+
ca_certs = "/path/to/your/server.crt" # 或 ca_certs = "/path/to/your//ca_cert.pem"
309+
```
310+
**示例代码:使用 SSL 连接 IoTDB**
311+
312+
```Java
313+
# Licensed to the Apache Software Foundation (ASF) under one
314+
# or more contributor license agreements. See the NOTICE file
315+
# distributed with this work for additional information
316+
# regarding copyright ownership. The ASF licenses this file
317+
# to you under the Apache License, Version 2.0 (the
318+
# "License"); you may not use this file except in compliance
319+
# with the License. You may obtain a copy of the License at
320+
#
321+
# http://www.apache.org/licenses/LICENSE-2.0
322+
#
323+
# Unless required by applicable law or agreed to in writing,
324+
# software distributed under the License is distributed on an
325+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
326+
# KIND, either express or implied. See the License for the
327+
# specific language governing permissions and limitations
328+
# under the License.
329+
#
330+
331+
from iotdb.SessionPool import PoolConfig, SessionPool
332+
from iotdb.Session import Session
333+
334+
ip = "127.0.0.1"
335+
port_ = "6667"
336+
username_ = "root"
337+
password_ = "root"
338+
# Configure SSL enabled
339+
use_ssl = True
340+
# Configure certificate path
341+
ca_certs = "/path/server.crt"
342+
343+
344+
def get_data():
345+
session = Session(
346+
ip, port_, username_, password_, use_ssl=use_ssl, ca_certs=ca_certs
347+
)
348+
session.open(False)
349+
result = session.execute_query_statement("select * from root.eg.etth")
350+
df = result.todf()
351+
df.rename(columns={"Time": "date"}, inplace=True)
352+
session.close()
353+
return df
354+
355+
356+
def get_data2():
357+
pool_config = PoolConfig(
358+
host=ip,
359+
port=port_,
360+
user_name=username_,
361+
password=password_,
362+
fetch_size=1024,
363+
time_zone="UTC+8",
364+
max_retry=3,
365+
use_ssl=use_ssl,
366+
ca_certs=ca_certs,
367+
)
368+
max_pool_size = 5
369+
wait_timeout_in_ms = 3000
370+
session_pool = SessionPool(pool_config, max_pool_size, wait_timeout_in_ms)
371+
session = session_pool.get_session()
372+
result = session.execute_query_statement("select * from root.eg.etth")
373+
df = result.todf()
374+
df.rename(columns={"Time": "date"}, inplace=True)
375+
session_pool.put_back(session)
376+
session_pool.close()
377+
378+
379+
if __name__ == "__main__":
380+
df = get_data()
381+
```
289382

290383
## 4. 示例代码
291384

0 commit comments

Comments
 (0)