1
1
import time
2
2
import urllib3
3
- from urllib3 .exceptions import ReadTimeoutError
3
+ from urllib3 .exceptions import ReadTimeoutError , SSLError as UrllibSSLError
4
4
5
5
from .base import Connection
6
- from ..exceptions import ConnectionError , ConnectionTimeout
6
+ from ..exceptions import ConnectionError , ImproperlyConfigured , ConnectionTimeout , SSLError
7
7
from ..compat import urlencode
8
8
9
9
class Urllib3HttpConnection (Connection ):
@@ -13,10 +13,17 @@ class Urllib3HttpConnection(Connection):
13
13
:arg http_auth: optional http auth information as either ':' separated
14
14
string or a tuple
15
15
: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
16
20
:arg maxsize: the maximum number of connections which will be kept open to
17
21
this host.
18
22
"""
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
+
20
27
super (Urllib3HttpConnection , self ).__init__ (host = host , port = port , ** kwargs )
21
28
self .headers = {}
22
29
if http_auth is not None :
@@ -25,10 +32,17 @@ def __init__(self, host='localhost', port=9200, http_auth=None, use_ssl=False, m
25
32
self .headers = urllib3 .make_headers (basic_auth = http_auth )
26
33
27
34
pool_class = urllib3 .HTTPConnectionPool
35
+ kw = {}
28
36
if use_ssl :
29
37
pool_class = urllib3 .HTTPSConnectionPool
30
38
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 )
32
46
33
47
def perform_request (self , method , url , params = None , body = None , timeout = None , ignore = ()):
34
48
url = self .url_prefix + url
@@ -50,6 +64,9 @@ def perform_request(self, method, url, params=None, body=None, timeout=None, ign
50
64
response = self .pool .urlopen (method , url , body , retries = False , headers = self .headers , ** kw )
51
65
duration = time .time () - start
52
66
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 )
53
70
except ReadTimeoutError as e :
54
71
self .log_request_fail (method , full_url , body , time .time () - start , exception = e )
55
72
raise ConnectionTimeout ('TIMEOUT' , str (e ), e )
0 commit comments