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: config option to allow basic auth #263

Merged
merged 7 commits into from
May 28, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/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
Expand Down
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
25 changes: 19 additions & 6 deletions influxdb_client/client/influxdb_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import configparser
import os
import base64

from influxdb_client import Configuration, ApiClient, HealthCheck, HealthService, Ready, ReadyService
from influxdb_client.client.authorizations_api import AuthorizationsApi
Expand Down Expand Up @@ -41,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
Expand All @@ -66,6 +69,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

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,
Expand Down Expand Up @@ -103,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::

Expand All @@ -112,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
Expand All @@ -126,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"
Expand All @@ -143,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')

Expand All @@ -164,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):
Expand All @@ -187,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")
Expand All @@ -195,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()

Expand All @@ -204,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:
"""
Expand Down
3 changes: 3 additions & 0 deletions influxdb_client/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ org=my-org
token=my-token
timeout=6000
connection_pool_maxsize=55
auth_basic=false

[tags]
id = 132-987-655
Expand Down
1 change: 1 addition & 0 deletions tests/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
active = true
timeout = 6000
connection_pool_maxsize = 55
auth_basic = False

[tags]
id = "132-987-655"
Expand Down
1 change: 1 addition & 0 deletions tests/test_InfluxDBClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down