Skip to content

Commit f7f1b8f

Browse files
authored
Update Cloud ID default port, parsing of Cloud ID
1 parent cd34547 commit f7f1b8f

File tree

7 files changed

+175
-41
lines changed

7 files changed

+175
-41
lines changed

docs/index.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,27 @@ description of the options.
230230

231231
.. _certifi: http://certifiio.readthedocs.io/en/latest/
232232

233+
Connecting via Cloud ID
234+
~~~~~~~~~~~~~~~~~~~~~~~
235+
236+
Cloud ID is an easy way to configure your client to work
237+
with your Elastic Cloud deployment. Combine the ``cloud_id``
238+
with either ``http_auth`` or ``api_key`` to authenticate
239+
with your Elastic Cloud deployment.
240+
241+
Using ``cloud_id`` enables TLS verification and HTTP compression by default
242+
and sets the port to ``443`` unless otherwise overwritten via the ``port`` parameter
243+
or the port value encoded within ``cloud_id``. Using Cloud ID also disables sniffing.
244+
245+
.. code-block:: python
246+
247+
from elasticsearch import Elasticsearch
248+
249+
es = Elasticsearch(
250+
cloud_id="cluster-1:dXMa5Fx...",
251+
http_auth=("elastic", "<password>"),
252+
)
253+
233254
APIKey Authentication
234255
~~~~~~~~~~~~~~~~~~~~~~
235256

elasticsearch/connection/base.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Connection(object):
4444
def __init__(
4545
self,
4646
host="localhost",
47-
port=9200,
47+
port=None,
4848
use_ssl=False,
4949
url_prefix="",
5050
timeout=10,
@@ -59,20 +59,28 @@ def __init__(
5959
if cloud_id:
6060
try:
6161
_, cloud_id = cloud_id.split(":")
62-
parent_dn, es_uuid, _ = (
62+
parent_dn, es_uuid = (
6363
binascii.a2b_base64(cloud_id.encode("utf-8"))
6464
.decode("utf-8")
65-
.split("$")
65+
.split("$")[:2]
6666
)
67-
except ValueError:
67+
if ":" in parent_dn:
68+
parent_dn, _, parent_port = parent_dn.rpartition(":")
69+
if port is None and parent_port != "443":
70+
port = int(parent_port)
71+
except (ValueError, IndexError):
6872
raise ImproperlyConfigured("'cloud_id' is not properly formatted")
6973

7074
host = "%s.%s" % (es_uuid, parent_dn)
71-
port = 9243
7275
use_ssl = True
7376
if http_compress is None:
7477
http_compress = True
7578

79+
# If cloud_id isn't set and port is default then use 9200.
80+
# Cloud should use '443' by default via the 'https' scheme.
81+
elif port is None:
82+
port = 9200
83+
7684
# Work-around if the implementing class doesn't
7785
# define the headers property before calling super().__init__()
7886
if not hasattr(self, "headers"):
@@ -102,7 +110,9 @@ def __init__(
102110

103111
self.hostname = host
104112
self.port = port
105-
self.host = "%s://%s:%s" % (scheme, host, port)
113+
self.host = "%s://%s" % (scheme, host)
114+
if self.port is not None:
115+
self.host += ":%s" % self.port
106116
if url_prefix:
107117
url_prefix = "/" + url_prefix.strip("/")
108118
self.url_prefix = url_prefix

elasticsearch/connection/http_requests.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class RequestsHttpConnection(Connection):
4545
def __init__(
4646
self,
4747
host="localhost",
48-
port=9200,
48+
port=None,
4949
http_auth=None,
5050
use_ssl=False,
5151
verify_certs=True,
@@ -93,12 +93,7 @@ def __init__(
9393
http_auth = tuple(http_auth.split(":", 1))
9494
self.session.auth = http_auth
9595

96-
self.base_url = "http%s://%s:%d%s" % (
97-
"s" if self.use_ssl else "",
98-
self.hostname,
99-
self.port,
100-
self.url_prefix,
101-
)
96+
self.base_url = "%s%s" % (self.host, self.url_prefix,)
10297
self.session.verify = verify_certs
10398
if not client_key:
10499
self.session.cert = client_cert
@@ -118,7 +113,7 @@ def __init__(
118113
if self.use_ssl and not verify_certs and ssl_show_warn:
119114
warnings.warn(
120115
"Connecting to %s using SSL with verify_certs=False is insecure."
121-
% self.base_url
116+
% self.host
122117
)
123118

124119
def perform_request(

elasticsearch/connection/http_urllib3.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class Urllib3HttpConnection(Connection):
8282
def __init__(
8383
self,
8484
host="localhost",
85-
port=9200,
85+
port=None,
8686
http_auth=None,
8787
use_ssl=False,
8888
verify_certs=VERIFY_CERTS_DEFAULT,
@@ -186,7 +186,7 @@ def __init__(
186186
if ssl_show_warn:
187187
warnings.warn(
188188
"Connecting to %s using SSL with verify_certs=False is insecure."
189-
% host
189+
% self.host
190190
)
191191
if not ssl_show_warn:
192192
urllib3.disable_warnings()

elasticsearch/transport.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ def __init__(
128128
# retain the original connection instances for sniffing
129129
self.seed_connections = self.connection_pool.connections[:]
130130

131+
# Don't enable sniffing on Cloud instances.
132+
if kwargs.get("cloud_id", False):
133+
sniff_on_start = False
134+
sniff_on_connection_fail = False
135+
131136
# sniffing data
132137
self.sniffer_timeout = sniffer_timeout
133138
self.sniff_on_connection_fail = sniff_on_connection_fail

0 commit comments

Comments
 (0)