[WIP] remove scheme checking

This commit is contained in:
Honza Král
2017-05-15 22:01:54 +02:00
parent 1aee1bb030
commit d336c571b9
6 changed files with 13 additions and 26 deletions
-3
View File
@@ -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),
+6 -5
View File
@@ -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('/')
+3 -3
View File
@@ -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)
+2 -8
View File
@@ -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.")
@@ -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'
}
],
+2 -5
View File
@@ -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)