Skip to content

Commit 8314f7b

Browse files
committed
Set verify_certs default to True
If certifi is installed use it as default for ca_certs value Closes elastic#403
1 parent aa3a743 commit 8314f7b

File tree

5 files changed

+28
-16
lines changed

5 files changed

+28
-16
lines changed

Changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Changelog
88

99
Version compatible with elasticsearch 5.0
1010

11+
* when using SSL certificate validation is now on by default. Install
12+
``certifi`` or supply root certificate bundle.
1113
* added ``headers`` arg to connections to support custom http headers
1214
* passing in a keyword parameter with ``None`` as value will cause that param
1315
to be ignored

docs/index.rst

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,7 @@ elasticsearch cluster, including certificate verification and http auth::
189189
['localhost', 'otherhost'],
190190
http_auth=('user', 'secret'),
191191
port=443,
192-
use_ssl=True,
193-
verify_certs=True,
194-
ca_certs=certifi.where(),
192+
use_ssl=True
195193
)
196194

197195
# SSL client authentication using client_cert and client_key
@@ -201,18 +199,18 @@ elasticsearch cluster, including certificate verification and http auth::
201199
http_auth=('user', 'secret'),
202200
port=443,
203201
use_ssl=True,
204-
verify_certs=True,
205202
ca_certs='/path/to/cacert.pem',
206203
client_cert='/path/to/client_cert.pem',
207204
client_key='/path/to/client_key.pem',
208205
)
209206

210207
.. warning::
211208

212-
By default SSL certificates won't be verified, pass in
213-
``verify_certs=True`` to make sure your certificates will get verified. The
214-
client doesn't ship with any CA certificates; easiest way to obtain the
215-
common set is by using the `certifi`_ package (as shown above).
209+
``elasticsearch-py`` doesn't ship with default set of root certificates. To
210+
have working SSL certificate validation you need to either specify your own
211+
as ``ca_certs`` or install `certifi`_ which will be picked up
212+
automatically.
213+
216214

217215
See class :class:`~elasticsearch.Urllib3HttpConnection` for detailed
218216
description of the options.

elasticsearch/connection/http_requests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class RequestsHttpConnection(Connection):
2727
:arg headers: any custom http headers to be add to requests
2828
"""
2929
def __init__(self, host='localhost', port=9200, http_auth=None,
30-
use_ssl=False, verify_certs=False, ca_certs=None, client_cert=None,
30+
use_ssl=False, verify_certs=True, ca_certs=None, client_cert=None,
3131
client_key=None, headers=None, **kwargs):
3232
if not REQUESTS_AVAILABLE:
3333
raise ImproperlyConfigured("Please install requests to use RequestsHttpConnection.")

elasticsearch/connection/http_urllib3.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
from urllib3.exceptions import ReadTimeoutError, SSLError as UrllibSSLError
44
import warnings
55

6+
CA_CERTS = None
7+
8+
try:
9+
import certifi
10+
CA_CERTS = certifi.where()
11+
except ImportError:
12+
pass
13+
614
from .base import Connection
715
from ..exceptions import ConnectionError, ImproperlyConfigured, ConnectionTimeout, SSLError
816
from ..compat import urlencode
@@ -36,7 +44,7 @@ class Urllib3HttpConnection(Connection):
3644
:arg headers: any custom http headers to be add to requests
3745
"""
3846
def __init__(self, host='localhost', port=9200, http_auth=None,
39-
use_ssl=False, verify_certs=False, ca_certs=None, client_cert=None,
47+
use_ssl=False, verify_certs=True, ca_certs=None, client_cert=None,
4048
client_key=None, ssl_version=None, ssl_assert_hostname=None,
4149
ssl_assert_fingerprint=None, maxsize=10, headers=None, **kwargs):
4250

@@ -48,6 +56,7 @@ def __init__(self, host='localhost', port=9200, http_auth=None,
4856
http_auth = ':'.join(http_auth)
4957
self.headers.update(urllib3.make_headers(basic_auth=http_auth))
5058

59+
ca_certs = CA_CERTS if ca_certs is None else ca_certs
5160
pool_class = urllib3.HTTPConnectionPool
5261
kw = {}
5362
if use_ssl:
@@ -59,14 +68,17 @@ def __init__(self, host='localhost', port=9200, http_auth=None,
5968
})
6069

6170
if verify_certs:
71+
if not ca_certs:
72+
raise ImproperlyConfigured("Root certificates are missing for certificate "
73+
"validation. Either pass them in using the ca_certs parameter or "
74+
"install certifi to use it automatically.")
75+
6276
kw.update({
6377
'cert_reqs': 'CERT_REQUIRED',
6478
'ca_certs': ca_certs,
6579
'cert_file': client_cert,
6680
'key_file': client_key,
6781
})
68-
elif ca_certs:
69-
raise ImproperlyConfigured("You cannot pass CA certificates when verify SSL is off.")
7082
else:
7183
warnings.warn(
7284
'Connecting to %s using SSL with verify_certs=False is insecure.' % host)

test_elasticsearch/test_connection.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ def test_http_auth_list(self):
3535
self.assertEquals({'authorization': 'Basic dXNlcm5hbWU6c2VjcmV0',
3636
'connection': 'keep-alive'}, con.headers)
3737

38-
def test_uses_https_if_specified(self):
38+
def test_uses_https_if_verify_certs_is_off(self):
3939
with warnings.catch_warnings(record=True) as w:
40-
con = Urllib3HttpConnection(use_ssl=True)
40+
con = Urllib3HttpConnection(use_ssl=True, verify_certs=False)
4141
self.assertEquals(1, len(w))
4242
self.assertEquals('Connecting to localhost using SSL with verify_certs=False is insecure.', str(w[0].message))
4343

@@ -86,9 +86,9 @@ def test_timeout_set(self):
8686
con = RequestsHttpConnection(timeout=42)
8787
self.assertEquals(42, con.timeout)
8888

89-
def test_use_https_if_specified(self):
89+
def test_uses_https_if_verify_certs_is_off(self):
9090
with warnings.catch_warnings(record=True) as w:
91-
con = self._get_mock_connection({'use_ssl': True, 'url_prefix': 'url'})
91+
con = self._get_mock_connection({'use_ssl': True, 'url_prefix': 'url', 'verify_certs': False})
9292
self.assertEquals(1, len(w))
9393
self.assertEquals('Connecting to https://localhost:9200/url using SSL with verify_certs=False is insecure.', str(w[0].message))
9494

0 commit comments

Comments
 (0)