Skip to content

Commit 7d5f2c2

Browse files
authored
feat: skip of verifying SSL certificate could be configured via config file or environment properties (#143)
1 parent 45d1b0b commit 7d5f2c2

File tree

5 files changed

+72
-7
lines changed

5 files changed

+72
-7
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
## 1.10.0 [unreleased]
22

33
### Features
4-
1. [#136](https://github.com/influxdata/influxdb-client-python/pull/136): Allows users to skip of verifying SSL certificate
4+
1. [#136](https://github.com/influxdata/influxdb-client-python/pull/136): Allows users to skip of verifying SSL certificate
5+
1. [#143](https://github.com/influxdata/influxdb-client-python/pull/143): Skip of verifying SSL certificate could be configured via config file or environment properties
56

67
## 1.9.0 [2020-07-17]
78

README.rst

+3
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ The following options are supported:
169169
- ``org`` - default destination organization for writes and queries
170170
- ``token`` - the token to use for the authorization
171171
- ``timeout`` - socket timeout in ms (default value is 10000)
172+
- ``verify_ssl`` - set this to false to skip verifying SSL certificate when calling API from https server
172173

173174
.. code-block:: python
174175
@@ -181,6 +182,7 @@ The following options are supported:
181182
org=my-org
182183
token=my-token
183184
timeout=6000
185+
verify_ssl=False
184186
185187
Via Environment Properties
186188
^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -192,6 +194,7 @@ Supported properties are:
192194
- ``INFLUXDB_V2_ORG`` - default destination organization for writes and queries
193195
- ``INFLUXDB_V2_TOKEN`` - the token to use for the authorization
194196
- ``INFLUXDB_V2_TIMEOUT`` - socket timeout in ms (default value is 10000)
197+
- ``INFLUXDB_V2_VERIFY_SSL`` - set this to false to skip verifying SSL certificate when calling API from https server
195198

196199
.. code-block:: python
197200

influxdb_client/client/influxdb_client.py

+33-5
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,16 @@ def __init__(self, url, token, debug=None, timeout=10000, enable_gzip=False, org
6060

6161
@classmethod
6262
def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gzip=False):
63-
"""Configure client via '*.ini' file in segment 'influx2'."""
63+
"""
64+
Configure client via '*.ini' file in segment 'influx2'.
65+
66+
Supported options:
67+
- url
68+
- org
69+
- token
70+
- timeout,
71+
- verify_ssl
72+
"""
6473
config = configparser.ConfigParser()
6574
config.read(config_file)
6675

@@ -77,24 +86,39 @@ def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gz
7786
if config.has_option('influx2', 'org'):
7887
org = config['influx2']['org']
7988

89+
verify_ssl = True
90+
if config.has_option('influx2', 'verify_ssl'):
91+
verify_ssl = config['influx2']['verify_ssl']
92+
8093
default_tags = None
8194

8295
if config.has_section('tags'):
8396
default_tags = dict(config.items('tags'))
8497

8598
if timeout:
8699
return cls(url, token, debug=debug, timeout=int(timeout), org=org, default_tags=default_tags,
87-
enable_gzip=enable_gzip)
100+
enable_gzip=enable_gzip, verify_ssl=_to_bool(verify_ssl))
88101

89-
return cls(url, token, debug=debug, org=org, default_tags=default_tags, enable_gzip=enable_gzip)
102+
return cls(url, token, debug=debug, org=org, default_tags=default_tags, enable_gzip=enable_gzip,
103+
verify_ssl=_to_bool(verify_ssl))
90104

91105
@classmethod
92106
def from_env_properties(cls, debug=None, enable_gzip=False):
93-
"""Configure client via environment properties."""
107+
"""
108+
Configure client via environment properties.
109+
110+
Supported environment properties:
111+
- INFLUXDB_V2_URL
112+
- INFLUXDB_V2_ORG
113+
- INFLUXDB_V2_TOKEN
114+
- INFLUXDB_V2_TIMEOUT
115+
- INFLUXDB_V2_VERIFY_SSL
116+
"""
94117
url = os.getenv('INFLUXDB_V2_URL', "http://localhost:9999")
95118
token = os.getenv('INFLUXDB_V2_TOKEN', "my-token")
96119
timeout = os.getenv('INFLUXDB_V2_TIMEOUT', "10000")
97120
org = os.getenv('INFLUXDB_V2_ORG', "my-org")
121+
verify_ssl = os.getenv('INFLUXDB_V2_VERIFY_SSL', "True")
98122

99123
default_tags = dict()
100124

@@ -103,7 +127,7 @@ def from_env_properties(cls, debug=None, enable_gzip=False):
103127
default_tags[key[16:].lower()] = value
104128

105129
return cls(url, token, debug=debug, timeout=int(timeout), org=org, default_tags=default_tags,
106-
enable_gzip=enable_gzip)
130+
enable_gzip=enable_gzip, verify_ssl=_to_bool(verify_ssl))
107131

108132
def write_api(self, write_options=WriteOptions(), point_settings=PointSettings()) -> WriteApi:
109133
"""
@@ -247,3 +271,7 @@ def update_request_body(self, path: str, body):
247271
return gzip.compress(bytes(_body, "utf-8"))
248272

249273
return _body
274+
275+
276+
def _to_bool(verify_ssl):
277+
return str(verify_ssl).lower() in ("yes", "true")

tests/config-disabled-ssl.ini

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[influx2]
2+
url=http://localhost:9999
3+
org=my-org
4+
token=my-token
5+
timeout=6000
6+
verify_ssl=False
7+
8+
[tags]
9+
id = 132-987-655
10+
customer = California Miner
11+
data_center = ${env.data_center}

tests/test_InfluxDBClient.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import http.server
22
import json
3+
import os
34
import threading
45
import unittest
56

@@ -26,7 +27,6 @@ def test_TrailingSlashInUrl(self):
2627
def test_ConnectToSelfSignedServer(self):
2728
import http.server
2829
import ssl
29-
import os
3030

3131
# Disable unverified HTTPS requests
3232
import urllib3
@@ -49,6 +49,28 @@ def test_ConnectToSelfSignedServer(self):
4949
self.assertEqual(health.status, "pass")
5050
self.assertEqual(health.name, "influxdb")
5151

52+
def test_init_from_file_ssl_default(self):
53+
self.client = InfluxDBClient.from_config_file(f'{os.path.dirname(__file__)}/config.ini')
54+
55+
self.assertTrue(self.client.api_client.configuration.verify_ssl)
56+
57+
def test_init_from_file_ssl(self):
58+
self.client = InfluxDBClient.from_config_file(f'{os.path.dirname(__file__)}/config-disabled-ssl.ini')
59+
60+
self.assertFalse(self.client.api_client.configuration.verify_ssl)
61+
62+
def test_init_from_env_ssl_default(self):
63+
del os.environ["INFLUXDB_V2_VERIFY_SSL"]
64+
self.client = InfluxDBClient.from_env_properties()
65+
66+
self.assertTrue(self.client.api_client.configuration.verify_ssl)
67+
68+
def test_init_from_env_ssl(self):
69+
os.environ["INFLUXDB_V2_VERIFY_SSL"] = "False"
70+
self.client = InfluxDBClient.from_env_properties()
71+
72+
self.assertFalse(self.client.api_client.configuration.verify_ssl)
73+
5274

5375
class ServerWithSelfSingedSSL(http.server.SimpleHTTPRequestHandler):
5476
def _set_headers(self):

0 commit comments

Comments
 (0)