Skip to content

Commit 30b75c9

Browse files
feat: config option to allow basic auth (#263)
1 parent a421a6f commit 30b75c9

File tree

7 files changed

+28
-6
lines changed

7 files changed

+28
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
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.
55
1. [#241](https://github.com/influxdata/influxdb-client-python/pull/241): Add detail error message for not supported type of `Point.field`
66
1. [#238](https://github.com/influxdata/influxdb-client-python/pull/238): Add possibility to specify default `timezone` for datetimes without `tzinfo`
7+
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
78

89
### Bug Fixes
910
1. [#254](https://github.com/influxdata/influxdb-client-python/pull/254): Serialize `numpy` floats into LineProtocol

README.rst

+2
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ The following options are supported:
175175
- ``verify_ssl`` - set this to false to skip verifying SSL certificate when calling API from https server
176176
- ``ssl_ca_cert`` - set this to customize the certificate file to verify the peer
177177
- ``connection_pool_maxsize`` - set the number of connections to save that can be reused by urllib3
178+
- ``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)
178179

179180
.. code-block:: python
180181
@@ -202,6 +203,7 @@ Supported properties are:
202203
- ``INFLUXDB_V2_VERIFY_SSL`` - set this to false to skip verifying SSL certificate when calling API from https server
203204
- ``INFLUXDB_V2_SSL_CA_CERT`` - set this to customize the certificate file to verify the peer
204205
- ``INFLUXDB_V2_CONNECTION_POOL_MAXSIZE`` - set the number of connections to save that can be reused by urllib3
206+
- ``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)
205207

206208
.. code-block:: python
207209

influxdb_client/client/influxdb_client.py

+19-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import configparser
66
import os
7+
import base64
78

89
from influxdb_client import Configuration, ApiClient, HealthCheck, HealthService, Ready, ReadyService
910
from influxdb_client.client.authorizations_api import AuthorizationsApi
@@ -41,7 +42,9 @@ def __init__(self, url, token, debug=None, timeout=10_000, enable_gzip=False, or
4142
Defaults to "multiprocessing.cpu_count() * 5".
4243
:key urllib3.util.retry.Retry retries: Set the default retry strategy that is used for all HTTP requests
4344
except batching writes. As a default there is no one retry strategy.
44-
45+
:key bool auth_basic: Set this to true to enable basic authentication when talking to a InfluxDB 1.8.x that
46+
does not use auth-enabled but is protected by a reverse proxy with basic authentication.
47+
(defaults to false, don't set to true when talking to InfluxDB 2)
4548
"""
4649
self.url = url
4750
self.token = token
@@ -66,6 +69,10 @@ def __init__(self, url, token, debug=None, timeout=10_000, enable_gzip=False, or
6669
auth_header_name = "Authorization"
6770
auth_header_value = "Token " + auth_token
6871

72+
auth_basic = kwargs.get('auth_basic', False)
73+
if auth_basic:
74+
auth_header_value = "Basic " + base64.b64encode(token.encode()).decode()
75+
6976
retries = kwargs.get('retries', False)
7077

7178
self.api_client = ApiClient(configuration=conf, header_name=auth_header_name,
@@ -103,6 +110,7 @@ def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gz
103110
- verify_ssl
104111
- ssl_ca_cert
105112
- connection_pool_maxsize
113+
- auth_basic
106114
107115
config.ini example::
108116
@@ -112,6 +120,7 @@ def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gz
112120
token=my-token
113121
timeout=6000
114122
connection_pool_maxsize=25
123+
auth_basic=false
115124
116125
[tags]
117126
id = 132-987-655
@@ -126,6 +135,7 @@ def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gz
126135
org = "my-org"
127136
timeout = 6000
128137
connection_pool_maxsize = 25
138+
auth_basic = false
129139
130140
[tags]
131141
id = "132-987-655"
@@ -143,12 +153,10 @@ def config_value(key: str):
143153
token = config_value('token')
144154

145155
timeout = None
146-
147156
if config.has_option('influx2', 'timeout'):
148157
timeout = config_value('timeout')
149158

150159
org = None
151-
152160
if config.has_option('influx2', 'org'):
153161
org = config_value('org')
154162

@@ -164,15 +172,18 @@ def config_value(key: str):
164172
if config.has_option('influx2', 'connection_pool_maxsize'):
165173
connection_pool_maxsize = config_value('connection_pool_maxsize')
166174

167-
default_tags = None
175+
auth_basic = False
176+
if config.has_option('influx2', 'auth_basic'):
177+
auth_basic = config_value('auth_basic')
168178

179+
default_tags = None
169180
if config.has_section('tags'):
170181
tags = {k: v.strip('"') for k, v in config.items('tags')}
171182
default_tags = dict(tags)
172183

173184
return cls(url, token, debug=debug, timeout=_to_int(timeout), org=org, default_tags=default_tags,
174185
enable_gzip=enable_gzip, verify_ssl=_to_bool(verify_ssl), ssl_ca_cert=ssl_ca_cert,
175-
connection_pool_maxsize=_to_int(connection_pool_maxsize))
186+
connection_pool_maxsize=_to_int(connection_pool_maxsize), auth_basic=_to_bool(auth_basic))
176187

177188
@classmethod
178189
def from_env_properties(cls, debug=None, enable_gzip=False):
@@ -187,6 +198,7 @@ def from_env_properties(cls, debug=None, enable_gzip=False):
187198
- INFLUXDB_V2_VERIFY_SSL
188199
- INFLUXDB_V2_SSL_CA_CERT
189200
- INFLUXDB_V2_CONNECTION_POOL_MAXSIZE
201+
- INFLUXDB_V2_AUTH_BASIC
190202
"""
191203
url = os.getenv('INFLUXDB_V2_URL', "http://localhost:8086")
192204
token = os.getenv('INFLUXDB_V2_TOKEN', "my-token")
@@ -195,6 +207,7 @@ def from_env_properties(cls, debug=None, enable_gzip=False):
195207
verify_ssl = os.getenv('INFLUXDB_V2_VERIFY_SSL', "True")
196208
ssl_ca_cert = os.getenv('INFLUXDB_V2_SSL_CA_CERT', None)
197209
connection_pool_maxsize = os.getenv('INFLUXDB_V2_CONNECTION_POOL_MAXSIZE', None)
210+
auth_basic = os.getenv('INFLUXDB_V2_AUTH_BASIC', "False")
198211

199212
default_tags = dict()
200213

@@ -204,7 +217,7 @@ def from_env_properties(cls, debug=None, enable_gzip=False):
204217

205218
return cls(url, token, debug=debug, timeout=_to_int(timeout), org=org, default_tags=default_tags,
206219
enable_gzip=enable_gzip, verify_ssl=_to_bool(verify_ssl), ssl_ca_cert=ssl_ca_cert,
207-
connection_pool_maxsize=_to_int(connection_pool_maxsize))
220+
connection_pool_maxsize=_to_int(connection_pool_maxsize), auth_basic=_to_bool(auth_basic))
208221

209222
def write_api(self, write_options=WriteOptions(), point_settings=PointSettings()) -> WriteApi:
210223
"""

influxdb_client/configuration.py

+3
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ def __init__(self):
107107
# It can also be a pair (tuple) of (connection, read) timeouts.
108108
self.timeout = None
109109

110+
# Set to True/False to enable basic authentication when using proxied InfluxDB 1.8.x with no auth-enabled
111+
self.auth_basic = False
112+
110113
# Proxy URL
111114
self.proxy = None
112115
# Safe chars for path_param

tests/config.ini

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ org=my-org
44
token=my-token
55
timeout=6000
66
connection_pool_maxsize=55
7+
auth_basic=false
78

89
[tags]
910
id = 132-987-655

tests/config.toml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
active = true
66
timeout = 6000
77
connection_pool_maxsize = 55
8+
auth_basic = False
89

910
[tags]
1011
id = "132-987-655"

tests/test_InfluxDBClient.py

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def assertConfig(self):
7272
self.assertEqual("California Miner", self.client.default_tags["customer"])
7373
self.assertEqual("${env.data_center}", self.client.default_tags["data_center"])
7474
self.assertEqual(55, self.client.api_client.configuration.connection_pool_maxsize)
75+
self.assertEqual(False, self.client.api_client.configuration.auth_basic)
7576

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

0 commit comments

Comments
 (0)