From d336c571b913720cb42e3c69229f6fa7a6371dfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Honza=20Kr=C3=A1l?= Date: Mon, 15 May 2017 21:56:49 +0200 Subject: [PATCH] [WIP] remove scheme checking --- elasticsearch/client/__init__.py | 3 --- elasticsearch/connection/base.py | 11 ++++++----- elasticsearch/connection/http_requests.py | 6 +++--- elasticsearch/transport.py | 10 ++-------- test_elasticsearch/test_client/__init__.py | 2 -- test_elasticsearch/test_transport.py | 7 ++----- 6 files changed, 13 insertions(+), 26 deletions(-) diff --git a/elasticsearch/client/__init__.py b/elasticsearch/client/__init__.py index 488cf8be..0ffcc70e 100644 --- a/elasticsearch/client/__init__.py +++ b/elasticsearch/client/__init__.py @@ -44,9 +44,6 @@ def _normalize_hosts(hosts): if parsed_url.scheme == "https": h['port'] = parsed_url.port or 443 h['use_ssl'] = True - h['scheme'] = 'http' - elif parsed_url.scheme: - h['scheme'] = parsed_url.scheme if parsed_url.username or parsed_url.password: h['http_auth'] = '%s:%s' % (unquote(parsed_url.username), diff --git a/elasticsearch/connection/base.py b/elasticsearch/connection/base.py index 21486679..62d62f65 100644 --- a/elasticsearch/connection/base.py +++ b/elasticsearch/connection/base.py @@ -24,8 +24,6 @@ class Connection(object): Also responsible for logging. """ - transport_schema = 'http' - def __init__(self, host='localhost', port=9200, use_ssl=False, url_prefix='', timeout=10, **kwargs): """ :arg host: hostname of the node (default: localhost) @@ -33,9 +31,12 @@ class Connection(object): :arg url_prefix: optional url prefix for elasticsearch :arg timeout: default timeout in seconds (float, default: 10) """ - scheme = self.transport_schema - if use_ssl: - scheme += 's' + scheme = kwargs.get('scheme', 'http') + if use_ssl or scheme == 'https': + scheme = 'https' + use_ssl = True + self.use_ssl = use_ssl + self.host = '%s://%s:%s' % (scheme, host, port) if url_prefix: url_prefix = '/' + url_prefix.strip('/') diff --git a/elasticsearch/connection/http_requests.py b/elasticsearch/connection/http_requests.py index f2a9d3f1..59dd381c 100644 --- a/elasticsearch/connection/http_requests.py +++ b/elasticsearch/connection/http_requests.py @@ -32,7 +32,7 @@ class RequestsHttpConnection(Connection): if not REQUESTS_AVAILABLE: raise ImproperlyConfigured("Please install requests to use RequestsHttpConnection.") - super(RequestsHttpConnection, self).__init__(host=host, port=port, **kwargs) + super(RequestsHttpConnection, self).__init__(host=host, port=port, use_ssl=use_ssl, **kwargs) self.session = requests.Session() self.session.headers = headers or {} self.session.headers.setdefault('content-type', 'application/json') @@ -43,7 +43,7 @@ class RequestsHttpConnection(Connection): http_auth = tuple(http_auth.split(':', 1)) self.session.auth = http_auth self.base_url = 'http%s://%s:%d%s' % ( - 's' if use_ssl else '', + 's' if self.use_ssl else '', host, port, self.url_prefix ) self.session.verify = verify_certs @@ -57,7 +57,7 @@ class RequestsHttpConnection(Connection): raise ImproperlyConfigured("You cannot pass CA certificates when verify SSL is off.") self.session.verify = ca_certs - if use_ssl and not verify_certs: + if self.use_ssl and not verify_certs: warnings.warn( 'Connecting to %s using SSL with verify_certs=False is insecure.' % self.base_url) diff --git a/elasticsearch/transport.py b/elasticsearch/transport.py index 70715f27..9408ae42 100644 --- a/elasticsearch/transport.py +++ b/elasticsearch/transport.py @@ -151,12 +151,6 @@ class Transport(object): # previously unseen params, create new connection kwargs = self.kwargs.copy() kwargs.update(host) - - if 'scheme' in host and host['scheme'] != self.connection_class.transport_schema: - raise ImproperlyConfigured( - 'Scheme specified in connection (%s) is not the same as the connection class (%s) specifies (%s).' % ( - host['scheme'], self.connection_class.__name__, self.connection_class.transport_schema - )) return self.connection_class(**kwargs) connections = map(_create_connection, hosts) @@ -242,8 +236,8 @@ class Transport(object): hosts = list(filter(None, (self._get_host_info(n) for n in node_info))) - # we weren't able to get any nodes, maybe using an incompatible - # transport_schema or host_info_callback blocked all - raise error. + # we weren't able to get any nodes or host_info_callback blocked all - + # raise error. if not hosts: raise TransportError("N/A", "Unable to sniff hosts - no viable hosts found.") diff --git a/test_elasticsearch/test_client/__init__.py b/test_elasticsearch/test_client/__init__.py index ec01145e..1bcf913b 100644 --- a/test_elasticsearch/test_client/__init__.py +++ b/test_elasticsearch/test_client/__init__.py @@ -24,14 +24,12 @@ class TestNormalizeHosts(TestCase): "host": "elastic.co", "port": 42, "use_ssl": True, - 'scheme': 'http' }, { "host": "elastic.co", "http_auth": "user:secret", "use_ssl": True, "port": 443, - 'scheme': 'http', 'url_prefix': '/prefix' } ], diff --git a/test_elasticsearch/test_transport.py b/test_elasticsearch/test_transport.py index 1db30358..74f52fd4 100644 --- a/test_elasticsearch/test_transport.py +++ b/test_elasticsearch/test_transport.py @@ -68,9 +68,6 @@ class TestTransport(TestCase): t = Transport([{'host': 'localhost'}]) self.assertIsInstance(t.connection_pool, DummyConnectionPool) - def test_host_with_scheme_different_from_connection_fails(self): - self.assertRaises(ImproperlyConfigured, Transport, [{'host': 'localhost', 'scheme': 'thrift'}]) - def test_request_timeout_extracted_from_params_and_passed(self): t = Transport([{}], connection_class=DummyConnection) @@ -111,7 +108,7 @@ class TestTransport(TestCase): def test_kwargs_passed_on_to_connections(self): t = Transport([{'host': 'google.com'}], port=123) self.assertEquals(1, len(t.connection_pool.connections)) - self.assertEquals('%s://google.com:123' % t.connection_pool.connections[0].transport_schema, t.connection_pool.connections[0].host) + self.assertEquals('http://google.com:123', t.connection_pool.connections[0].host) def test_kwargs_passed_on_to_connection_pool(self): dt = object() @@ -131,7 +128,7 @@ class TestTransport(TestCase): t.add_connection({"host": "google.com", "port": 1234}) self.assertEquals(2, len(t.connection_pool.connections)) - self.assertEquals('%s://google.com:1234' % t.connection_pool.connections[1].transport_schema, t.connection_pool.connections[1].host) + self.assertEquals('http://google.com:1234', t.connection_pool.connections[1].host) def test_request_will_fail_after_X_retries(self): t = Transport([{'exception': ConnectionError('abandon ship')}], connection_class=DummyConnection)