Skip to content

Commit 51ad5d3

Browse files
alvarolmedofxdgear
authored andcommitted
ssl_show_warn option added (#913)
* ssl_show_warn option added * Typo fixed
1 parent 2c6e603 commit 51ad5d3

File tree

5 files changed

+42
-5
lines changed

5 files changed

+42
-5
lines changed

Changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Changelog
66
6.4.0 (dev)
77
-----------
88
* Removed deprecated option ``update_all_types``.
9+
* Using insecure SSL configuration (``verify_cert=False``) raises a warning, this can
10+
be not showed with ``ssl_show_warn=False``
911

1012
6.3.0 (2018-06-20)
1113
-----------

elasticsearch/client/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,20 @@ class Elasticsearch(object):
123123
ca_certs='/path/to/CA_certs'
124124
)
125125
126+
If using SSL, but don't verify the certs, a warning message is showed
127+
optionally (see :class:`~elasticsearch.Urllib3HttpConnection` for
128+
detailed description of the options)::
129+
130+
es = Elasticsearch(
131+
['localhost:443', 'other_host:443'],
132+
# turn on SSL
133+
use_ssl=True,
134+
# no verify SSL certificates
135+
verify_certs=False,
136+
# don't show warnings about ssl certs verification
137+
ssl_show_warn=False
138+
)
139+
126140
SSL client authentication is supported
127141
(see :class:`~elasticsearch.Urllib3HttpConnection` for
128142
detailed description of the options)::

elasticsearch/connection/http_requests.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class RequestsHttpConnection(Connection):
1818
string or a tuple. Any value will be passed into requests as `auth`.
1919
:arg use_ssl: use ssl for the connection if `True`
2020
:arg verify_certs: whether to verify SSL certificates
21+
:arg ssl_show_warn: show warning when verify certs is disabled
2122
:arg ca_certs: optional path to CA bundle. By default standard requests'
2223
bundle will be used.
2324
:arg client_cert: path to the file containing the private key and the
@@ -27,7 +28,7 @@ class RequestsHttpConnection(Connection):
2728
:arg headers: any custom http headers to be add to requests
2829
"""
2930
def __init__(self, host='localhost', port=9200, http_auth=None,
30-
use_ssl=False, verify_certs=True, ca_certs=None, client_cert=None,
31+
use_ssl=False, verify_certs=True, ssl_show_warn=True, ca_certs=None, client_cert=None,
3132
client_key=None, headers=None, **kwargs):
3233
if not REQUESTS_AVAILABLE:
3334
raise ImproperlyConfigured("Please install requests to use RequestsHttpConnection.")
@@ -57,7 +58,7 @@ def __init__(self, host='localhost', port=9200, http_auth=None,
5758
raise ImproperlyConfigured("You cannot pass CA certificates when verify SSL is off.")
5859
self.session.verify = ca_certs
5960

60-
if self.use_ssl and not verify_certs:
61+
if self.use_ssl and not verify_certs and ssl_show_warn:
6162
warnings.warn(
6263
'Connecting to %s using SSL with verify_certs=False is insecure.' % self.base_url)
6364

elasticsearch/connection/http_urllib3.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class Urllib3HttpConnection(Connection):
4848
string or a tuple
4949
:arg use_ssl: use ssl for the connection if `True`
5050
:arg verify_certs: whether to verify SSL certificates
51+
:arg ssl_show_warn: show warning when verify certs is disabled
5152
:arg ca_certs: optional path to CA bundle.
5253
See https://urllib3.readthedocs.io/en/latest/security.html#using-certifi-with-urllib3
5354
for instructions how to get default set
@@ -67,7 +68,7 @@ class Urllib3HttpConnection(Connection):
6768
:arg http_compress: Use gzip compression
6869
"""
6970
def __init__(self, host='localhost', port=9200, http_auth=None,
70-
use_ssl=False, verify_certs=VERIFY_CERTS_DEFAULT, ca_certs=None, client_cert=None,
71+
use_ssl=False, verify_certs=VERIFY_CERTS_DEFAULT, ssl_show_warn=True, ca_certs=None, client_cert=None,
7172
client_key=None, ssl_version=None, ssl_assert_hostname=None,
7273
ssl_assert_fingerprint=None, maxsize=10, headers=None, ssl_context=None, http_compress=False, **kwargs):
7374

@@ -131,8 +132,9 @@ def __init__(self, host='localhost', port=9200, http_auth=None,
131132
'key_file': client_key,
132133
})
133134
else:
134-
warnings.warn(
135-
'Connecting to %s using SSL with verify_certs=False is insecure.' % host)
135+
if ssl_show_warn:
136+
warnings.warn(
137+
'Connecting to %s using SSL with verify_certs=False is insecure.' % host)
136138

137139
self.pool = pool_class(host, port=port, timeout=self.timeout, maxsize=maxsize, **kw)
138140

test_elasticsearch/test_connection.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ def test_uses_https_if_verify_certs_is_off(self):
7474

7575
self.assertIsInstance(con.pool, urllib3.HTTPSConnectionPool)
7676

77+
def nowarn_when_test_uses_https_if_verify_certs_is_off(self):
78+
with warnings.catch_warnings(record=True) as w:
79+
con = Urllib3HttpConnection(use_ssl=True, verify_certs=False, ssl_show_warn=False)
80+
self.assertEquals(0, len(w))
81+
82+
self.assertIsInstance(con.pool, urllib3.HTTPSConnectionPool)
83+
7784
def test_doesnt_use_https_if_not_specified(self):
7885
con = Urllib3HttpConnection()
7986
self.assertIsInstance(con.pool, urllib3.HTTPConnectionPool)
@@ -129,6 +136,17 @@ def test_uses_https_if_verify_certs_is_off(self):
129136
self.assertEquals('GET', request.method)
130137
self.assertEquals(None, request.body)
131138

139+
def nowarn_when_test_uses_https_if_verify_certs_is_off(self):
140+
with warnings.catch_warnings(record=True) as w:
141+
con = self._get_mock_connection({'use_ssl': True, 'url_prefix': 'url', 'verify_certs': False, 'ssl_show_warn': False})
142+
self.assertEquals(0, len(w))
143+
144+
request = self._get_request(con, 'GET', '/')
145+
146+
self.assertEquals('https://localhost:9200/url/', request.url)
147+
self.assertEquals('GET', request.method)
148+
self.assertEquals(None, request.body)
149+
132150
def test_merge_headers(self):
133151
con = self._get_mock_connection(connection_params={'headers': {'h1': 'v1', 'h2': 'v2'}})
134152
req = self._get_request(con, 'GET', '/', headers={'h2': 'v2p', 'h3': 'v3'})

0 commit comments

Comments
 (0)