Skip to content

Commit c8fd53f

Browse files
committed
feat: python client supports configuring TLS
Signed-off-by: yehao <[email protected]>
1 parent 9450fe7 commit c8fd53f

File tree

5 files changed

+92
-4
lines changed

5 files changed

+92
-4
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,40 @@ if __name__ == "__main__":
115115
print(f"query failed, {error}")
116116

117117
```
118+
119+
Enable TLS (skip certificate authentication):
120+
121+
```python
122+
from opengemini_client import Client, Config, Address, TlsConfig
123+
124+
if __name__ == "__main__":
125+
config = Config(address=[Address(host='127.0.0.1', port=8443)], tls_enabled=True)
126+
cli = Client(config)
127+
try:
128+
cli.ping(0)
129+
print("ping success")
130+
except Exception as error:
131+
print(f"ping failed, {error}")
132+
133+
```
134+
135+
Enable TLS (Certificate Authentication):
136+
137+
```python
138+
import ssl
139+
from opengemini_client import Client, Config, Address, TlsConfig
140+
141+
if __name__ == "__main__":
142+
context = ssl.SSLContext()
143+
context.verify_mode = ssl.CERT_REQUIRED
144+
context.load_verify_locations("ca.crt")
145+
config = Config(address=[Address(host='127.0.0.1', port=8443)], tls_enabled=True,
146+
tls_config=TlsConfig(ca_file="ca.crt"))
147+
cli = Client(config)
148+
try:
149+
cli.ping(0)
150+
print("ping success")
151+
except Exception as error:
152+
print(f"ping failed, {error}")
153+
154+
```

README_CN.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,40 @@ if __name__ == "__main__":
116116
print(f"query failed, {error}")
117117

118118
```
119+
120+
开启TLS(跳过证书认证):
121+
122+
```python
123+
from opengemini_client import Client, Config, Address, TlsConfig
124+
125+
if __name__ == "__main__":
126+
config = Config(address=[Address(host='127.0.0.1', port=8443)], tls_enabled=True)
127+
cli = Client(config)
128+
try:
129+
cli.ping(0)
130+
print("ping success")
131+
except Exception as error:
132+
print(f"ping failed, {error}")
133+
134+
```
135+
136+
开启TLS(证书认证):
137+
138+
```python
139+
import ssl
140+
from opengemini_client import Client, Config, Address, TlsConfig
141+
142+
if __name__ == "__main__":
143+
context = ssl.SSLContext()
144+
context.verify_mode = ssl.CERT_REQUIRED
145+
context.load_verify_locations("ca.crt")
146+
config = Config(address=[Address(host='127.0.0.1', port=8443)], tls_enabled=True,
147+
tls_config=TlsConfig(ca_file="ca.crt"))
148+
cli = Client(config)
149+
try:
150+
cli.ping(0)
151+
print("ping success")
152+
except Exception as error:
153+
print(f"ping failed, {error}")
154+
155+
```

opengemini_client/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
RpConfig,
3131
Series,
3232
SeriesResult,
33+
TlsConfig,
3334
ValuesResult
3435
)
3536

opengemini_client/client_impl.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from opengemini_client.models import Config, BatchPoints, Query, QueryResult, Series, SeriesResult, RpConfig, \
3030
ValuesResult, KeyValue
3131
from opengemini_client.url_const import UrlConst
32-
from opengemini_client.models import AuthType
32+
from opengemini_client.models import AuthType, TlsConfig
3333

3434

3535
def check_config(config: Config):
@@ -45,6 +45,9 @@ def check_config(config: Config):
4545
if config.auth_config.auth_type == AuthType.TOKEN and len(config.auth_config.token) == 0:
4646
raise ValueError("invalid auth config due to empty token")
4747

48+
if config.tls_enabled and config.tls_config is None:
49+
config.tls_config = TlsConfig()
50+
4851
if config.batch_config is not None:
4952
if config.batch_config.batch_interval <= 0:
5053
raise ValueError("batch enabled,batch interval must be greater than 0")
@@ -85,7 +88,11 @@ class OpenGeminiDBClient(Client, ABC):
8588
def __init__(self, config: Config):
8689
self.config = check_config(config)
8790
self.session = requests.Session()
88-
protocol = "https://" if config.tls_enabled else "http://"
91+
protocol = "http://"
92+
if config.tls_enabled:
93+
protocol = "https://"
94+
self.session.cert = (config.tls_config.cert_file, config.tls_config.key_file)
95+
self.session.verify = config.tls_config.ca_file
8996
self.endpoints = [f"{protocol}{addr.host}:{addr.port}" for addr in config.address]
9097
self.endpoints_iter = itertools.cycle(self.endpoints)
9198

opengemini_client/models.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# limitations under the License.
1414

1515
import io
16-
import ssl
1716
from dataclasses import field, dataclass
1817
from datetime import datetime, timedelta
1918
from enum import Enum
@@ -39,6 +38,13 @@ class AuthConfig:
3938
token: str = ''
4039

4140

41+
@dataclass
42+
class TlsConfig:
43+
cert_file: str = ''
44+
key_file: str = ''
45+
ca_file: str = ''
46+
47+
4248
@dataclass
4349
class BatchConfig:
4450
batch_interval: int
@@ -54,7 +60,7 @@ class Config:
5460
gzip_enabled: bool = False
5561
tls_enabled: bool = False
5662
auth_config: AuthConfig = None
57-
tls_config: ssl.SSLContext = None
63+
tls_config: TlsConfig = None
5864

5965

6066
@dataclass

0 commit comments

Comments
 (0)