8000 Dependencies: Permit installation with urllib3 2.0 · crate/crate-python@c276159 · GitHub
[go: up one dir, main page]

Skip to content

Commit c276159

Browse files
committed
Dependencies: Permit installation with urllib3 2.0
1 parent 524936e commit c276159

File tree

6 files changed

+61
-2
lines changed

6 files changed

+61
-2
lines changed

CHANGES.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ Unreleased
77

88
- Properly handle Python-native UUID types in SQL parameters
99
- SQLAlchemy: Fix handling URL parameters ``timeout`` and ``pool_size``
10+
- Permit installation with urllib3 v2, see also `urllib3 v2.0 roadmap`_
11+
and `urllib3 v2.0 migration guide`_. You can optionally retain support
12+
for TLS 1.0 and TLS 1.1, but a few other outdated use-cases of X.509
13+
certificate details are immanent, like no longer accepting the long
14+
deprecated ``commonName`` attribute. Instead, going forward, only the
15+
``subjectAltName`` attribute will be used.
16+
17+
.. _urllib3 v2.0 migration guide: https://urllib3.readthedocs.io/en/latest/v2-migration-guide.html
18+
.. _urllib3 v2.0 roadmap: https://urllib3.readthedocs.io/en/stable/v2-roadmap.html
1019

1120

1221
2023/07/17 0.33.0

docs/by-example/https.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,18 @@ The connection will also fail when providing an invalid CA certificate:
110110
Traceback (most recent call last):
111111
...
112112
crate.client.exceptions.ConnectionError: Server not available, exception: HTTPSConnectionPool...
113+
114+
115+
Relaxing minimum SSL version
116+
============================
117+
118+
urrlib3 v2 dropped support for TLS 1.0 and TLS 1.1 by default, see `Modern security by default -
119+
HTTPS requires TLS 1.2+`_. If you need to re-enable it, use the ``ssl_relax_minimum_version`` flag,
120+
which will configure ``kwargs["ssl_minimum_version"] = ssl.TLSVersion.MINIMUM_SUPPORTED``.
121+
122+
>>> client = HttpClient([crate_host], ssl_relax_minimum_version=True, verify_ssl_cert=False)
123+
>>> client.server_infos(crate_host)
124+
('https://localhost:65534', 'test', '0.0.0')
125+
126+
127+
.. _Modern security by default - HTTPS requires TLS 1.2+: https://urllib3.readthedocs.io/en/latest/v2-migration-guide.html#https-requires-tls-1-2

docs/connect.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,16 @@ Here, replace ``<CERT_FILE>`` with the path to the client certificate file, and
139139
verification. In such circumstances, you can combine the two methods above
140140
to do both at once.
141141

142+
Relaxing minimum SSL version
143+
............................
144+
145+
urrlib3 v2 dropped support for TLS 1.0 and TLS 1.1 by default, see `Modern security by default -
146+
HTTPS requires TLS 1.2+`_. If you need to re-enable it, use the ``ssl_relax_minimum_version`` flag,
147+
which will configure ``kwargs["ssl_minimum_version"] = ssl.TLSVersion.MINIMUM_SUPPORTED``.
148+
149+
>>> connection = client.connect(..., ssl_relax_minimum_version=True)
150+
151+
142152
Timeout
143153
-------
144154

@@ -268,6 +278,7 @@ Once you're connected, you can :ref:`query CrateDB <query>`.
268278

269279

270280
.. _client-side random load balancing: https://en.wikipedia.org/wiki/Load_balancing_(computing)#Client-side_random_load_balancing
281+
.. _Modern security by default - HTTPS requires TLS 1.2+: https://urllib3.readthedocs.io/en/latest/v2-migration-guide.html#https-requires-tls-1-2
271282
.. _Python Database API Specification v2.0: https://www.python.org/dev/peps/pep-0249/
272283
.. _round-robin DNS: https://en.wikipedia.org/wiki/Round-robin_DNS
273284
.. _sample application: https://github.com/crate/crate-sample-apps/tree/main/python-flask

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def read(path):
5858
'crate = crate.client.sqlalchemy:CrateDialect'
5959
]
6060
},
61-
install_requires=['urllib3>=1.9,<2'],
61+
install_requires=['urllib3<2.1'],
6262
extras_require=dict(
6363
sqlalchemy=['sqlalchemy>=1.0,<2.1',
6464
'geojson>=2.5.0,<4',

src/crate/client/connection.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def __init__(self,
3838
error_trace=False,
3939
cert_file=None,
4040
key_file=None,
41+
ssl_relax_minimum_version=False,
4142
username=None,
4243
password=None,
4344
schema=None,
@@ -138,6 +139,7 @@ def __init__(self,
138139
error_trace=error_trace,
139140
cert_file=cert_file,
140141
key_file=key_file,
142+
ssl_relax_minimum_version=ssl_relax_minimum_version,
141143
username=username,
142144
password=password,
143145
schema=schema,

src/crate/client/http.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from decimal import Decimal
3838
from uuid import UUID
3939

40+
import urllib3
4041
from urllib3 import connection_from_url
4142
from urllib3.connection import HTTPConnection
4243
from urllib3.exceptions import (
@@ -48,6 +49,8 @@
4849
SSLError,
4950
)
5051
from urllib3.util.retry import Retry
52+
53+
from crate.client._pep440 import Version
5154
from crate.client.exceptions import (
5255
ConnectionError,
5356
BlobLocationNotFoundException,
@@ -274,6 +277,19 @@ def _remove_certs_for_non_https(server, kwargs):
274277
return kwargs
275278

276279

280+
def _update_pool_kwargs_for_ssl_minimum_version(server, kwargs):
281+
"""
282+
On urllib3 v2, re-add support for TLS 1.0 and TLS 1.1.
283+
284+
https://urllib3.readthedocs.io/en/latest/v2-migration-guide.html#https-requires-tls-1-2
285+
"""
286+
if Version(urllib3.__version__) >= Version("2"):
287+
from urllib3.util import parse_url
288+
scheme, _, host, port, *_ = parse_url(server)
289+
if scheme == "https":
290+
kwargs["ssl_minimum_version"] = ssl.TLSVersion.MINIMUM_SUPPORTED
291+
292+
277293
def _create_sql_payload(stmt, args, bulk_args):
278294
if not isinstance(stmt, str):
279295
raise ValueError('stmt is not a string')
@@ -304,7 +320,7 @@ def _get_socket_opts(keepalive=True,
304320
# always use TCP keepalive
305321
opts = [(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)]
306322

307-
# hasattr check because some of the options depend on system capabilities
323+
# hasattr check because some options depend on system capabilities
308324
# see https://docs.python.org/3/library/socket.html#socket.SOMAXCONN
309325
if hasattr(socket, 'TCP_KEEPIDLE') and tcp_keepidle is not None:
310326
opts.append((socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, tcp_keepidle))
@@ -340,6 +356,7 @@ def __init__(self,
340356
error_trace=False,
341357
cert_file=None,
342358
key_file=None,
359+
ssl_relax_minimum_version=False,
343360
username=None,
344361
password=None,
345362
schema=None,
@@ -380,6 +397,7 @@ def __init__(self,
380397
'socket_tcp_keepintvl': socket_tcp_keepintvl,
381398
'socket_tcp_keepcnt': socket_tcp_keepcnt,
382399
})
400+
self.ssl_relax_minimum_version = ssl_relax_minimum_version
383401
self.backoff_factor = backoff_factor
384402
self.server_pool = {}
385403
self._update_server_pool(servers, **pool_kw)
@@ -400,6 +418,10 @@ def close(self):
400418

401419
def _create_server(self, server, **pool_kw):
402420
kwargs = _remove_certs_for_non_https(server, pool_kw)
421+
# After updating to urllib3 v2, optionally retain support for TLS 1.0 and TLS 1.1,
422+
# in order to support connectivity to older versions of CrateDB.
423+
if self.ssl_relax_minimum_version:
424+
_update_pool_kwargs_for_ssl_minimum_version(server, kwargs)
403425
self.server_pool[server] = Server(server, **kwargs)
404426

405427
def _update_server_pool(self, servers, **pool_kw):

0 commit comments

Comments
 (0)
0