44from urllib3 .exceptions import ReadTimeoutError , SSLError as UrllibSSLError
55import warnings
66
7+ # sentinal value for `verify_certs`.
8+ # This is used to detect if a user is passing in a value for `verify_certs`
9+ # so we can raise a warning if using SSL kwargs AND SSLContext.
10+ VERIFY_CERTS_DEFAULT = None
11+
712CA_CERTS = None
813
914try :
@@ -41,8 +46,8 @@ class Urllib3HttpConnection(Connection):
4146 string or a tuple
4247 :arg use_ssl: use ssl for the connection if `True`
4348 :arg verify_certs: whether to verify SSL certificates
44- :arg ca_certs: optional path to CA bundle. See
45- https://urllib3.readthedocs.io/en/latest/security.html#using-certifi-with-urllib3
49+ :arg ca_certs: optional path to CA bundle.
50+ See https://urllib3.readthedocs.io/en/latest/security.html#using-certifi-with-urllib3
4651 for instructions how to get default set
4752 :arg client_cert: path to the file containing the private key and the
4853 certificate, or cert only if using client_key
@@ -59,7 +64,7 @@ class Urllib3HttpConnection(Connection):
5964 :arg headers: any custom http headers to be add to requests
6065 """
6166 def __init__ (self , host = 'localhost' , port = 9200 , http_auth = None ,
62- use_ssl = False , verify_certs = True , ca_certs = None , client_cert = None ,
67+ use_ssl = False , verify_certs = VERIFY_CERTS_DEFAULT , ca_certs = None , client_cert = None ,
6368 client_key = None , ssl_version = None , ssl_assert_hostname = None ,
6469 ssl_assert_fingerprint = None , maxsize = 10 , headers = None , ssl_context = None , ** kwargs ):
6570
@@ -80,48 +85,51 @@ def __init__(self, host='localhost', port=9200, http_auth=None,
8085 kw = {}
8186
8287 # if providing an SSL context, raise error if any other SSL related flag is used
83- if ssl_context and (ca_certs or ssl_version ):
84- raise ImproperlyConfigured ("When using `ssl_context`, `use_ssl`, `verify_certs`, `ca_certs` and `ssl_version` are not permitted" )
88+ if ssl_context and ( (verify_certs is not VERIFY_CERTS_DEFAULT ) or ca_certs
89+ or client_cert or client_key or ssl_version ):
90+ warnings .warn ("When using `ssl_context`, all other SSL related kwargs are ignored" )
8591
8692 # if ssl_context provided use SSL by default
87- if self .use_ssl or ssl_context :
88- ca_certs = CA_CERTS if ca_certs is None else ca_certs
89-
90- if not ca_certs and not ssl_context and verify_certs :
91- # If no ca_certs and no sslcontext passed and asking to verify certs
92- # raise error
93- raise ImproperlyConfigured ("Root certificates are missing for certificate "
94- "validation. Either pass them in using the ca_certs parameter or "
95- "install certifi to use it automatically." )
96- if verify_certs or ca_certs or ssl_version :
97- warnings .warn ('Use of `verify_certs`, `ca_certs`, `ssl_version` have been deprecated in favor of using SSLContext`' , DeprecationWarning )
93+ if ssl_context and self .use_ssl :
9894 pool_class = urllib3 .HTTPSConnectionPool
95+ kw .update ({
96+ 'assert_fingerprint' : ssl_assert_fingerprint ,
97+ 'ssl_context' : ssl_context ,
98+ })
99+ self .pool = pool_class (host , port = port , timeout = self .timeout , maxsize = maxsize , ** kw )
99100
100- if not ssl_context :
101- # if SSLContext hasn't been passed in, create one.
102- # need to skip if sslContext isn't avail
103- try :
104- ssl_context = create_ssl_context (cafile = ca_certs )
105- except AttributeError :
106- ssl_context = None
107-
108- if not verify_certs and ssl_context is not None :
109- ssl_context .check_hostname = False
110- ssl_context .verify_mode = ssl .CERT_NONE
111- warnings .warn (
112- 'Connecting to %s using SSL with verify_certs=False is insecure.' % host )
113-
101+ elif self .use_ssl :
102+ pool_class = urllib3 .HTTPSConnectionPool
114103 kw .update ({
115104 'ssl_version' : ssl_version ,
116105 'assert_hostname' : ssl_assert_hostname ,
117106 'assert_fingerprint' : ssl_assert_fingerprint ,
118- 'ssl_context' : ssl_context ,
119- 'cert_file' : client_cert ,
120- 'ca_certs' : ca_certs ,
121- 'key_file' : client_key ,
122107 })
108+
109+ # If `verify_certs` is sentinal value, default `verify_certs` to `True`
110+ if verify_certs is VERIFY_CERTS_DEFAULT :
111+ verify_certs = True
112+
113+ ca_certs = CA_CERTS if ca_certs is None else ca_certs
114+ if verify_certs :
115+ if not ca_certs :
116+ raise ImproperlyConfigured ("Root certificates are missing for certificate "
117+ "validation. Either pass them in using the ca_certs parameter or "
118+ "install certifi to use it automatically." )
119+
120+ kw .update ({
121+ 'cert_reqs' : 'CERT_REQUIRED' ,
122+ 'ca_certs' : ca_certs ,
123+ 'cert_file' : client_cert ,
124+ 'key_file' : client_key ,
125+ })
126+ else :
127+ warnings .warn (
128+ 'Connecting to %s using SSL with verify_certs=False is insecure.' % host )
129+
123130 self .pool = pool_class (host , port = port , timeout = self .timeout , maxsize = maxsize , ** kw )
124131
132+
125133 def perform_request (self , method , url , params = None , body = None , timeout = None , ignore = (), headers = None ):
126134 url = self .url_prefix + url
127135 if params :
0 commit comments