Skip to content

Commit 2dce4c0

Browse files
authored
Merge pull request elastic#690 from fxdgear/nick/update_docs
Updating Docs to reference SSL Context
2 parents 190e4fd + 50f89e4 commit 2dce4c0

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

README

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,22 @@ Simple use-case::
8888

8989
.. _Full documentation: https://elasticsearch-py.readthedocs.io/
9090

91+
Elastic Cloud (and SSL) use-case::
92+
93+
>>> from elasticsearch import Elasticsearch
94+
>>> es = Elasticsearch("https://elasticsearch.url:port", http_auth=('elastic','yourpassword'))
95+
>>> es.info()
96+
97+
Using SSL Context with a self-signed cert use-case::
98+
99+
>>> from elasticsearch import Elasticsearch
100+
>>> from elasticsearch.connection import create_ssl_context
101+
102+
>>> context = create_ssl_context(cafile="path/to/cafile.pem")
103+
>>> es = Elasticsearch("https://elasticsearch.url:port", ssl_context=context, http_auth=('elastic','yourpassword'))
104+
>>> es.info()
105+
106+
91107

92108
Features
93109
--------

docs/connection.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ If you want to create your own `SSLContext` object you can create one natively u
5454
python SSL library with the `create_default_context` (https://docs.python.org/3/library/ssl.html#ssl.create_default_context) method
5555
or you can use the wrapper function :function:`~elasticsearch.connection.http_urllib3.create_ssl_context`.
5656

57+
To create an `SSLContext` object you only need to use one of cafile, capath or cadata::
58+
59+
>>> from elasticsearch.connection import create_ssl_context
60+
>>> context = create_ssl_context(cafile=None, capath=None, cadata=None)
61+
62+
* `cafile` is the path to your CA File
63+
* `capath` is the directory of a collection of CA's
64+
* `cadata` is either an ASCII string of one or more PEM-encoded certificates or a bytes-like object of DER-encoded certificates.
5765

5866
.. autoclass:: Urllib3HttpConnection
5967
:members:

docs/index.rst

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ Example Usage
5959
es = Elasticsearch()
6060

6161
doc = {
62-
'author': 'kimchy',
63-
'text': 'Elasticsearch: cool. bonsai cool.',
62+
'author': 'kimchy',
63+
'text': 'Elasticsearch: cool. bonsai cool.',
6464
'timestamp': datetime.now(),
6565
}
6666
res = es.index(index="test-index", doc_type='tweet', id=1, body=doc)
@@ -191,34 +191,32 @@ elasticsearch cluster, including certificate verification and http auth::
191191

192192
# ... or specify common parameters as kwargs
193193

194-
# use certifi for CA certificates
195-
import certifi
196-
197194
es = Elasticsearch(
198195
['localhost', 'otherhost'],
199196
http_auth=('user', 'secret'),
197+
scheme="https",
200198
port=443,
201-
use_ssl=True
202199
)
203200

204201
# SSL client authentication using client_cert and client_key
205202

203+
from elasticsearch.connection import create_ssl_context
204+
205+
context = create_ssl_context(cafile="path/to/cert.pem")
206206
es = Elasticsearch(
207207
['localhost', 'otherhost'],
208208
http_auth=('user', 'secret'),
209+
scheme="https",
209210
port=443,
210-
use_ssl=True,
211-
ca_certs='/path/to/cacert.pem',
212-
client_cert='/path/to/client_cert.pem',
213-
client_key='/path/to/client_key.pem',
211+
ssl_context=context,
214212
)
215213

216214
.. warning::
217215

218216
``elasticsearch-py`` doesn't ship with default set of root certificates. To
219217
have working SSL certificate validation you need to either specify your own
220-
as ``ca_certs`` or install `certifi`_ which will be picked up
221-
automatically.
218+
as ``cafile`` or ``capath`` or ``cadata`` or install `certifi`_ which will
219+
be picked up automatically.
222220

223221

224222
See class :class:`~elasticsearch.Urllib3HttpConnection` for detailed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
from .base import Connection
22
from .http_requests import RequestsHttpConnection
3-
from .http_urllib3 import Urllib3HttpConnection
3+
from .http_urllib3 import Urllib3HttpConnection, create_ssl_context
4+

0 commit comments

Comments
 (0)