From df847be32fa4717abf3a17927e7bda98980fc3df Mon Sep 17 00:00:00 2001 From: Reinhard Weber Date: Wed, 26 May 2021 14:32:42 +0200 Subject: [PATCH 1/7] Optionally allow basic auth This can be useful for setups of InfluxDB 1.8 without authentication when authentication is handled by a proxy instead. --- influxdb_client/client/influxdb_client.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/influxdb_client/client/influxdb_client.py b/influxdb_client/client/influxdb_client.py index d38d38ec..a2b60f3e 100644 --- a/influxdb_client/client/influxdb_client.py +++ b/influxdb_client/client/influxdb_client.py @@ -3,7 +3,7 @@ from __future__ import absolute_import import configparser -import os +import os, base64 from influxdb_client import Configuration, ApiClient, HealthCheck, HealthService, Ready, ReadyService from influxdb_client.client.authorizations_api import AuthorizationsApi @@ -66,6 +66,12 @@ def __init__(self, url, token, debug=None, timeout=10_000, enable_gzip=False, or auth_header_name = "Authorization" auth_header_value = "Token " + auth_token + # allow basic authentication for cases where InfluxDB (backwards 1.8 doesn't use auth but proxies perform basic auth) + auth_basic = kwargs.get('auth_basic', False) + if auth_basic: + auth_header_value = "Basic " + base64.b64encode(token.encode()).decode() + + retries = kwargs.get('retries', False) self.api_client = ApiClient(configuration=conf, header_name=auth_header_name, From 8c239580102bbc89aba00fc966a914b432f9f94b Mon Sep 17 00:00:00 2001 From: Reinhard Weber Date: Wed, 26 May 2021 16:08:52 +0200 Subject: [PATCH 2/7] feat: optional parameter to allow basic auth --- influxdb_client/client/influxdb_client.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/influxdb_client/client/influxdb_client.py b/influxdb_client/client/influxdb_client.py index a2b60f3e..7f7fb20f 100644 --- a/influxdb_client/client/influxdb_client.py +++ b/influxdb_client/client/influxdb_client.py @@ -3,7 +3,8 @@ from __future__ import absolute_import import configparser -import os, base64 +import os +import base64 from influxdb_client import Configuration, ApiClient, HealthCheck, HealthService, Ready, ReadyService from influxdb_client.client.authorizations_api import AuthorizationsApi @@ -66,12 +67,10 @@ def __init__(self, url, token, debug=None, timeout=10_000, enable_gzip=False, or auth_header_name = "Authorization" auth_header_value = "Token " + auth_token - # allow basic authentication for cases where InfluxDB (backwards 1.8 doesn't use auth but proxies perform basic auth) auth_basic = kwargs.get('auth_basic', False) if auth_basic: auth_header_value = "Basic " + base64.b64encode(token.encode()).decode() - retries = kwargs.get('retries', False) self.api_client = ApiClient(configuration=conf, header_name=auth_header_name, From 3af8c88fbac29e60ac37303377a0e2a6f12f6845 Mon Sep 17 00:00:00 2001 From: Reinhard Weber Date: Thu, 27 May 2021 10:20:22 +0200 Subject: [PATCH 3/7] feat: optionally allow basic auth for 1.8.x --- README.rst | 2 ++ influxdb_client/client/influxdb_client.py | 20 ++++++++++++++------ tests/config.ini | 1 + tests/config.toml | 1 + tests/test_InfluxDBClient.py | 1 + 5 files changed, 19 insertions(+), 6 deletions(-) diff --git a/README.rst b/README.rst index ee7b13c6..4044adec 100644 --- a/README.rst +++ b/README.rst @@ -175,6 +175,7 @@ The following options are supported: - ``verify_ssl`` - set this to false to skip verifying SSL certificate when calling API from https server - ``ssl_ca_cert`` - set this to customize the certificate file to verify the peer - ``connection_pool_maxsize`` - set the number of connections to save that can be reused by urllib3 +- ``auth_basic`` - enable http basic authentication when talking to a InfluxDB 1.8.x without authentication but is accessed via reverse proxy with basic authentication (defaults to false) .. code-block:: python @@ -202,6 +203,7 @@ Supported properties are: - ``INFLUXDB_V2_VERIFY_SSL`` - set this to false to skip verifying SSL certificate when calling API from https server - ``INFLUXDB_V2_SSL_CA_CERT`` - set this to customize the certificate file to verify the peer - ``INFLUXDB_V2_CONNECTION_POOL_MAXSIZE`` - set the number of connections to save that can be reused by urllib3 +- ``INFLUXDB_V2_AUTH_BASIC`` - enable http basic authentication when talking to a InfluxDB 1.8.x without authentication but is accessed via reverse proxy with basic authentication (defaults to false) .. code-block:: python diff --git a/influxdb_client/client/influxdb_client.py b/influxdb_client/client/influxdb_client.py index 7f7fb20f..cad755dd 100644 --- a/influxdb_client/client/influxdb_client.py +++ b/influxdb_client/client/influxdb_client.py @@ -42,7 +42,9 @@ def __init__(self, url, token, debug=None, timeout=10_000, enable_gzip=False, or Defaults to "multiprocessing.cpu_count() * 5". :key urllib3.util.retry.Retry retries: Set the default retry strategy that is used for all HTTP requests except batching writes. As a default there is no one retry strategy. - + :key bool auth_basic: Set this to true to enable basic authentication when talking to a InfluxDB 1.8.x that does not + use auth-enabled but is protected by a reverse proxy with basic authentication. + (defaults to false, don't set to true when talking to InfluxDB 2) """ self.url = url self.token = token @@ -108,6 +110,7 @@ def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gz - verify_ssl - ssl_ca_cert - connection_pool_maxsize + - auth_basic config.ini example:: @@ -117,6 +120,7 @@ def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gz token=my-token timeout=6000 connection_pool_maxsize=25 + auth_basic=false [tags] id = 132-987-655 @@ -131,6 +135,7 @@ def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gz org = "my-org" timeout = 6000 connection_pool_maxsize = 25 + auth_basic = false [tags] id = "132-987-655" @@ -148,12 +153,10 @@ def config_value(key: str): token = config_value('token') timeout = None - if config.has_option('influx2', 'timeout'): timeout = config_value('timeout') org = None - if config.has_option('influx2', 'org'): org = config_value('org') @@ -169,15 +172,18 @@ def config_value(key: str): if config.has_option('influx2', 'connection_pool_maxsize'): connection_pool_maxsize = config_value('connection_pool_maxsize') - default_tags = None + auth_basic = False + if config.has_option('influx2', 'auth_basic'): + auth_basic = config_value('auth_basic') + default_tags = None if config.has_section('tags'): tags = {k: v.strip('"') for k, v in config.items('tags')} default_tags = dict(tags) return cls(url, token, debug=debug, timeout=_to_int(timeout), org=org, default_tags=default_tags, enable_gzip=enable_gzip, verify_ssl=_to_bool(verify_ssl), ssl_ca_cert=ssl_ca_cert, - connection_pool_maxsize=_to_int(connection_pool_maxsize)) + connection_pool_maxsize=_to_int(connection_pool_maxsize), auth_basic=_to_bool(auth_basic)) @classmethod def from_env_properties(cls, debug=None, enable_gzip=False): @@ -192,6 +198,7 @@ def from_env_properties(cls, debug=None, enable_gzip=False): - INFLUXDB_V2_VERIFY_SSL - INFLUXDB_V2_SSL_CA_CERT - INFLUXDB_V2_CONNECTION_POOL_MAXSIZE + - INFLUXDB_V2_AUTH_BASIC """ url = os.getenv('INFLUXDB_V2_URL', "http://localhost:8086") token = os.getenv('INFLUXDB_V2_TOKEN', "my-token") @@ -200,6 +207,7 @@ def from_env_properties(cls, debug=None, enable_gzip=False): verify_ssl = os.getenv('INFLUXDB_V2_VERIFY_SSL', "True") ssl_ca_cert = os.getenv('INFLUXDB_V2_SSL_CA_CERT', None) connection_pool_maxsize = os.getenv('INFLUXDB_V2_CONNECTION_POOL_MAXSIZE', None) + auth_basic = os.getenv('INFLUXDB_V2_AUTH_BASIC', "False") default_tags = dict() @@ -209,7 +217,7 @@ def from_env_properties(cls, debug=None, enable_gzip=False): return cls(url, token, debug=debug, timeout=_to_int(timeout), org=org, default_tags=default_tags, enable_gzip=enable_gzip, verify_ssl=_to_bool(verify_ssl), ssl_ca_cert=ssl_ca_cert, - connection_pool_maxsize=_to_int(connection_pool_maxsize)) + connection_pool_maxsize=_to_int(connection_pool_maxsize), auth_basic=_to_bool(auth_basic)) def write_api(self, write_options=WriteOptions(), point_settings=PointSettings()) -> WriteApi: """ diff --git a/tests/config.ini b/tests/config.ini index a5897fc2..e3a58cd0 100644 --- a/tests/config.ini +++ b/tests/config.ini @@ -4,6 +4,7 @@ org=my-org token=my-token timeout=6000 connection_pool_maxsize=55 +auth_basic=false [tags] id = 132-987-655 diff --git a/tests/config.toml b/tests/config.toml index ae4126e6..af68b325 100644 --- a/tests/config.toml +++ b/tests/config.toml @@ -5,6 +5,7 @@ active = true timeout = 6000 connection_pool_maxsize = 55 + auth_basic = False [tags] id = "132-987-655" diff --git a/tests/test_InfluxDBClient.py b/tests/test_InfluxDBClient.py index a006cb0c..dc798fdc 100644 --- a/tests/test_InfluxDBClient.py +++ b/tests/test_InfluxDBClient.py @@ -72,6 +72,7 @@ def assertConfig(self): self.assertEqual("California Miner", self.client.default_tags["customer"]) self.assertEqual("${env.data_center}", self.client.default_tags["data_center"]) self.assertEqual(55, self.client.api_client.configuration.connection_pool_maxsize) + self.assertEqual(False, self.client.api_client.configuration.auth_basic) def test_init_from_file_ssl_default(self): self.client = InfluxDBClient.from_config_file(f'{os.path.dirname(__file__)}/config.ini') From 6a62fdde99b49a6ed425ab70d3d91105090c6d8b Mon Sep 17 00:00:00 2001 From: Reinhard Weber Date: Thu, 27 May 2021 12:04:36 +0200 Subject: [PATCH 4/7] feat: optionally allow basic auth for 1.8.x --- influxdb_client/configuration.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/influxdb_client/configuration.py b/influxdb_client/configuration.py index c0f4f0f1..5eeb90a3 100644 --- a/influxdb_client/configuration.py +++ b/influxdb_client/configuration.py @@ -107,6 +107,9 @@ def __init__(self): # It can also be a pair (tuple) of (connection, read) timeouts. self.timeout = None + # Set to True/False to enable basic authentication when using proxied InfluxDB 1.8.x with no auth-enabled + self.auth_basic = False + # Proxy URL self.proxy = None # Safe chars for path_param From 174ec3edd0af8817e3a0861a2390916f134f70a6 Mon Sep 17 00:00:00 2001 From: Reinhard Weber Date: Thu, 27 May 2021 12:22:21 +0200 Subject: [PATCH 5/7] style: documentation code style --- influxdb_client/client/influxdb_client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/influxdb_client/client/influxdb_client.py b/influxdb_client/client/influxdb_client.py index cad755dd..1fbd1d41 100644 --- a/influxdb_client/client/influxdb_client.py +++ b/influxdb_client/client/influxdb_client.py @@ -42,8 +42,8 @@ def __init__(self, url, token, debug=None, timeout=10_000, enable_gzip=False, or Defaults to "multiprocessing.cpu_count() * 5". :key urllib3.util.retry.Retry retries: Set the default retry strategy that is used for all HTTP requests except batching writes. As a default there is no one retry strategy. - :key bool auth_basic: Set this to true to enable basic authentication when talking to a InfluxDB 1.8.x that does not - use auth-enabled but is protected by a reverse proxy with basic authentication. + :key bool auth_basic: Set this to true to enable basic authentication when talking to a InfluxDB 1.8.x that + does not use auth-enabled but is protected by a reverse proxy with basic authentication. (defaults to false, don't set to true when talking to InfluxDB 2) """ self.url = url From d900c8d09291c0e49bf010fda33810b1e1fd61f1 Mon Sep 17 00:00:00 2001 From: Reinhard Weber Date: Thu, 27 May 2021 16:27:49 +0200 Subject: [PATCH 6/7] feat: changelog update for auth_basic --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7695d7af..a3d7ddff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ 1. [#237](https://github.com/influxdata/influxdb-client-python/pull/237): Use kwargs to pass query parameters into API list call - useful for the ability to use pagination. 1. [#241](https://github.com/influxdata/influxdb-client-python/pull/241): Add detail error message for not supported type of `Point.field` 1. [#238](https://github.com/influxdata/influxdb-client-python/pull/238): Add possibility to specify default `timezone` for datetimes without `tzinfo` +1. [#262](https://github.com/influxdata/influxdb-client-python/issues/262): Add option `auth_basic` to allow proxied access to InfluxDB 1.8.x compatibility API ### Bug Fixes 1. [#254](https://github.com/influxdata/influxdb-client-python/pull/254): Serialize `numpy` floats into LineProtocol From cd3278c90d751a25c8693367a6495a7d07e5c0a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Bedn=C3=A1=C5=99?= Date: Fri, 28 May 2021 07:03:38 +0200 Subject: [PATCH 7/7] docs: update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a3d7ddff..dadb8163 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ 1. [#237](https://github.com/influxdata/influxdb-client-python/pull/237): Use kwargs to pass query parameters into API list call - useful for the ability to use pagination. 1. [#241](https://github.com/influxdata/influxdb-client-python/pull/241): Add detail error message for not supported type of `Point.field` 1. [#238](https://github.com/influxdata/influxdb-client-python/pull/238): Add possibility to specify default `timezone` for datetimes without `tzinfo` -1. [#262](https://github.com/influxdata/influxdb-client-python/issues/262): Add option `auth_basic` to allow proxied access to InfluxDB 1.8.x compatibility API +1. [#262](https://github.com/influxdata/influxdb-client-python/pull/263): Add option `auth_basic` to allow proxied access to InfluxDB 1.8.x compatibility API ### Bug Fixes 1. [#254](https://github.com/influxdata/influxdb-client-python/pull/254): Serialize `numpy` floats into LineProtocol