Skip to content

Commit c06d4fd

Browse files
author
Lloyd Wallis
committed
Add support for providing a client certificate for mutual TLS authentication.
1 parent d370895 commit c06d4fd

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

influxdb/client.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ class InfluxDBClient(object):
6161
:type proxies: dict
6262
:param path: path of InfluxDB on the server to connect, defaults to ''
6363
:type path: str
64+
:param cert: Path to client certificate to use for mutual TLS
65+
authentication, defaults to None
66+
:type cert: str
67+
68+
:raises ValueError: if cert is provided but ssl is disabled (set to False)
6469
"""
6570

6671
def __init__(self,
@@ -78,6 +83,7 @@ def __init__(self,
7883
proxies=None,
7984
pool_size=10,
8085
path='',
86+
cert=None,
8187
):
8288
"""Construct a new InfluxDBClient object."""
8389
self.__host = host
@@ -120,6 +126,14 @@ def __init__(self,
120126
else:
121127
self._proxies = proxies
122128

129+
if cert:
130+
if not ssl:
131+
raise ValueError(
132+
"Client certificate provided but ssl is disabled."
133+
)
134+
else:
135+
self._session.cert = cert
136+
123137
self.__baseurl = "{0}://{1}:{2}{3}".format(
124138
self._scheme,
125139
self._host,

influxdb/tests/client_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ def test_dsn(self):
149149
**{'ssl': False})
150150
self.assertEqual('http://my.host.fr:1886', cli._baseurl)
151151

152+
def test_cert(self):
153+
"""Test mutual TLS authentication for TestInfluxDBClient object."""
154+
cli = InfluxDBClient(ssl=True, cert='/etc/pki/tls/private/dummy.crt')
155+
self.assertEqual(cli._session.cert, '/etc/pki/tls/private/dummy.crt')
156+
157+
with self.assertRaises(ValueError):
158+
cli = InfluxDBClient(cert='/etc/pki/tls/private/dummy.crt')
159+
152160
def test_switch_database(self):
153161
"""Test switch database in TestInfluxDBClient object."""
154162
cli = InfluxDBClient('host', 8086, 'username', 'password', 'database')

0 commit comments

Comments
 (0)