11import time
22import urllib3
3- from urllib3 .exceptions import ReadTimeoutError
3+ from urllib3 .exceptions import ReadTimeoutError , SSLError as UrllibSSLError
44
55from .base import Connection
6- from ..exceptions import ConnectionError , ConnectionTimeout
6+ from ..exceptions import ConnectionError , ImproperlyConfigured , ConnectionTimeout , SSLError
77from ..compat import urlencode
88
99class Urllib3HttpConnection (Connection ):
@@ -13,10 +13,17 @@ class Urllib3HttpConnection(Connection):
1313 :arg http_auth: optional http auth information as either ':' separated
1414 string or a tuple
1515 :arg use_ssl: use ssl for the connection if `True`
16+ :arg verify_certs: whether to verify SSL certificates
17+ :arg ca_certs: optional path to CA bundle. See
18+ http://urllib3.readthedocs.org/en/latest/security.html#using-certifi-with-urllib3
19+ for instructions how to get default set
1620 :arg maxsize: the maximum number of connections which will be kept open to
1721 this host.
1822 """
19- def __init__ (self , host = 'localhost' , port = 9200 , http_auth = None , use_ssl = False , maxsize = 10 , ** kwargs ):
23+ def __init__ (self , host = 'localhost' , port = 9200 , http_auth = None ,
24+ use_ssl = False , verify_certs = False , ca_certs = None , maxsize = 10 ,
25+ ** kwargs ):
26+
2027 super (Urllib3HttpConnection , self ).__init__ (host = host , port = port , ** kwargs )
2128 self .headers = {}
2229 if http_auth is not None :
@@ -25,10 +32,17 @@ def __init__(self, host='localhost', port=9200, http_auth=None, use_ssl=False, m
2532 self .headers = urllib3 .make_headers (basic_auth = http_auth )
2633
2734 pool_class = urllib3 .HTTPConnectionPool
35+ kw = {}
2836 if use_ssl :
2937 pool_class = urllib3 .HTTPSConnectionPool
3038
31- self .pool = pool_class (host , port = port , timeout = self .timeout , maxsize = maxsize )
39+ if verify_certs :
40+ kw ['cert_reqs' ] = 'CERT_REQUIRED'
41+ kw ['ca_certs' ] = ca_certs
42+ elif ca_certs :
43+ raise ImproperlyConfigured ("You cannot pass CA certificates when verify SSL is off." )
44+
45+ self .pool = pool_class (host , port = port , timeout = self .timeout , maxsize = maxsize , ** kw )
3246
3347 def perform_request (self , method , url , params = None , body = None , timeout = None , ignore = ()):
3448 url = self .url_prefix + url
@@ -50,6 +64,9 @@ def perform_request(self, method, url, params=None, body=None, timeout=None, ign
5064 response = self .pool .urlopen (method , url , body , retries = False , headers = self .headers , ** kw )
5165 duration = time .time () - start
5266 raw_data = response .data .decode ('utf-8' )
67+ except UrllibSSLError as e :
68+ self .log_request_fail (method , full_url , body , time .time () - start , exception = e )
69+ raise SSLError ('N/A' , str (e ), e )
5370 except ReadTimeoutError as e :
5471 self .log_request_fail (method , full_url , body , time .time () - start , exception = e )
5572 raise ConnectionTimeout ('TIMEOUT' , str (e ), e )
0 commit comments