Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: skip of verifying SSL certificate could be configured via config file or environment properties #143

Merged
merged 1 commit into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
## 1.10.0 [unreleased]

### Features
1. [#136](https://github.com/influxdata/influxdb-client-python/pull/136): Allows users to skip of verifying SSL certificate
1. [#136](https://github.com/influxdata/influxdb-client-python/pull/136): Allows users to skip of verifying SSL certificate
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

## 1.9.0 [2020-07-17]

Expand Down
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ The following options are supported:
- ``org`` - default destination organization for writes and queries
- ``token`` - the token to use for the authorization
- ``timeout`` - socket timeout in ms (default value is 10000)
- ``verify_ssl`` - set this to false to skip verifying SSL certificate when calling API from https server

.. code-block:: python

Expand All @@ -181,6 +182,7 @@ The following options are supported:
org=my-org
token=my-token
timeout=6000
verify_ssl=False

Via Environment Properties
^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -192,6 +194,7 @@ Supported properties are:
- ``INFLUXDB_V2_ORG`` - default destination organization for writes and queries
- ``INFLUXDB_V2_TOKEN`` - the token to use for the authorization
- ``INFLUXDB_V2_TIMEOUT`` - socket timeout in ms (default value is 10000)
- ``INFLUXDB_V2_VERIFY_SSL`` - set this to false to skip verifying SSL certificate when calling API from https server

.. code-block:: python

Expand Down
38 changes: 33 additions & 5 deletions influxdb_client/client/influxdb_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,16 @@ def __init__(self, url, token, debug=None, timeout=10000, enable_gzip=False, org

@classmethod
def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gzip=False):
"""Configure client via '*.ini' file in segment 'influx2'."""
"""
Configure client via '*.ini' file in segment 'influx2'.

Supported options:
- url
- org
- token
- timeout,
- verify_ssl
"""
config = configparser.ConfigParser()
config.read(config_file)

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

verify_ssl = True
if config.has_option('influx2', 'verify_ssl'):
verify_ssl = config['influx2']['verify_ssl']

default_tags = None

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

if timeout:
return cls(url, token, debug=debug, timeout=int(timeout), org=org, default_tags=default_tags,
enable_gzip=enable_gzip)
enable_gzip=enable_gzip, verify_ssl=_to_bool(verify_ssl))

return cls(url, token, debug=debug, org=org, default_tags=default_tags, enable_gzip=enable_gzip)
return cls(url, token, debug=debug, org=org, default_tags=default_tags, enable_gzip=enable_gzip,
verify_ssl=_to_bool(verify_ssl))

@classmethod
def from_env_properties(cls, debug=None, enable_gzip=False):
"""Configure client via environment properties."""
"""
Configure client via environment properties.

Supported environment properties:
- INFLUXDB_V2_URL
- INFLUXDB_V2_ORG
- INFLUXDB_V2_TOKEN
- INFLUXDB_V2_TIMEOUT
- INFLUXDB_V2_VERIFY_SSL
"""
url = os.getenv('INFLUXDB_V2_URL', "http://localhost:9999")
token = os.getenv('INFLUXDB_V2_TOKEN', "my-token")
timeout = os.getenv('INFLUXDB_V2_TIMEOUT', "10000")
org = os.getenv('INFLUXDB_V2_ORG', "my-org")
verify_ssl = os.getenv('INFLUXDB_V2_VERIFY_SSL', "True")

default_tags = dict()

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

return cls(url, token, debug=debug, timeout=int(timeout), org=org, default_tags=default_tags,
enable_gzip=enable_gzip)
enable_gzip=enable_gzip, verify_ssl=_to_bool(verify_ssl))

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

return _body


def _to_bool(verify_ssl):
return str(verify_ssl).lower() in ("yes", "true")
11 changes: 11 additions & 0 deletions tests/config-disabled-ssl.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[influx2]
url=http://localhost:9999
org=my-org
token=my-token
timeout=6000
verify_ssl=False

[tags]
id = 132-987-655
customer = California Miner
data_center = ${env.data_center}
24 changes: 23 additions & 1 deletion tests/test_InfluxDBClient.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import http.server
import json
import os
import threading
import unittest

Expand All @@ -26,7 +27,6 @@ def test_TrailingSlashInUrl(self):
def test_ConnectToSelfSignedServer(self):
import http.server
import ssl
import os

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

def test_init_from_file_ssl_default(self):
self.client = InfluxDBClient.from_config_file(f'{os.path.dirname(__file__)}/config.ini')

self.assertTrue(self.client.api_client.configuration.verify_ssl)

def test_init_from_file_ssl(self):
self.client = InfluxDBClient.from_config_file(f'{os.path.dirname(__file__)}/config-disabled-ssl.ini')

self.assertFalse(self.client.api_client.configuration.verify_ssl)

def test_init_from_env_ssl_default(self):
del os.environ["INFLUXDB_V2_VERIFY_SSL"]
self.client = InfluxDBClient.from_env_properties()

self.assertTrue(self.client.api_client.configuration.verify_ssl)

def test_init_from_env_ssl(self):
os.environ["INFLUXDB_V2_VERIFY_SSL"] = "False"
self.client = InfluxDBClient.from_env_properties()

self.assertFalse(self.client.api_client.configuration.verify_ssl)


class ServerWithSelfSingedSSL(http.server.SimpleHTTPRequestHandler):
def _set_headers(self):
Expand Down