Skip to content

Commit 5470cd3

Browse files
dicejpetyaslavova
andcommitted
skip ssl import if not available (redis#3078)
* skip `ssl` import if not available Signed-off-by: Joel Dice <joel.dice@fermyon.com> * address review feedback and fix lint errors Signed-off-by: Joel Dice <joel.dice@fermyon.com> * remove TYPE_CHECKING clause from ssl conditional Signed-off-by: Joel Dice <joel.dice@fermyon.com> --------- Signed-off-by: Joel Dice <joel.dice@fermyon.com> Co-authored-by: petyaslavova <petya.slavova@redis.com>
1 parent bdac9e6 commit 5470cd3

File tree

4 files changed

+48
-15
lines changed

4 files changed

+48
-15
lines changed

redis/asyncio/client.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import copy
33
import inspect
44
import re
5-
import ssl
65
import warnings
76
from typing import (
87
TYPE_CHECKING,
@@ -72,6 +71,7 @@
7271
from redis.typing import ChannelT, EncodableT, KeyT
7372
from redis.utils import (
7473
HIREDIS_AVAILABLE,
74+
SSL_AVAILABLE,
7575
_set_info_logger,
7676
deprecated_function,
7777
get_lib_version,
@@ -80,6 +80,11 @@
8080
truncate_command_for_exception,
8181
)
8282

83+
if TYPE_CHECKING and SSL_AVAILABLE:
84+
from ssl import TLSVersion
85+
else:
86+
TLSVersion = None
87+
8388
PubSubHandler = Callable[[Dict[str, str]], Awaitable[None]]
8489
_KeyT = TypeVar("_KeyT", bound=KeyT)
8590
_ArgT = TypeVar("_ArgT", KeyT, EncodableT)
@@ -227,7 +232,7 @@ def __init__(
227232
ssl_ca_certs: Optional[str] = None,
228233
ssl_ca_data: Optional[str] = None,
229234
ssl_check_hostname: bool = False,
230-
ssl_min_version: Optional[ssl.TLSVersion] = None,
235+
ssl_min_version: Optional[TLSVersion] = None,
231236
ssl_ciphers: Optional[str] = None,
232237
max_connections: Optional[int] = None,
233238
single_connection_client: bool = False,

redis/asyncio/cluster.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import collections
33
import random
44
import socket
5-
import ssl
65
import warnings
76
from typing import (
87
Any,
@@ -64,7 +63,19 @@
6463
TryAgainError,
6564
)
6665
from redis.typing import AnyKeyT, EncodableT, KeyT
67-
from redis.utils import deprecated_function, get_lib_version, safe_str, str_if_bytes, truncate_command_for_exception
66+
from redis.utils import (
67+
SSL_AVAILABLE,
68+
deprecated_function,
69+
get_lib_version,
70+
safe_str,
71+
str_if_bytes,
72+
truncate_command_for_exception,
73+
)
74+
75+
if SSL_AVAILABLE:
76+
from ssl import TLSVersion
77+
else:
78+
TLSVersion = None
6879

6980
TargetNodesT = TypeVar(
7081
"TargetNodesT", str, "ClusterNode", List["ClusterNode"], Dict[Any, "ClusterNode"]
@@ -247,7 +258,7 @@ def __init__(
247258
ssl_certfile: Optional[str] = None,
248259
ssl_check_hostname: bool = False,
249260
ssl_keyfile: Optional[str] = None,
250-
ssl_min_version: Optional[ssl.TLSVersion] = None,
261+
ssl_min_version: Optional[TLSVersion] = None,
251262
ssl_ciphers: Optional[str] = None,
252263
protocol: Optional[int] = 2,
253264
address_remap: Optional[Callable[[Tuple[str, int]], Tuple[str, int]]] = None,
@@ -1161,9 +1172,7 @@ def get_node(
11611172
return self.nodes_cache.get(node_name)
11621173
else:
11631174
raise DataError(
1164-
"get_node requires one of the following: "
1165-
"1. node name "
1166-
"2. host and port"
1175+
"get_node requires one of the following: 1. node name 2. host and port"
11671176
)
11681177

11691178
def set_nodes(
@@ -1345,7 +1354,7 @@ async def initialize(self) -> None:
13451354
if len(disagreements) > 5:
13461355
raise RedisClusterException(
13471356
f"startup_nodes could not agree on a valid "
1348-
f'slots cache: {", ".join(disagreements)}'
1357+
f"slots cache: {', '.join(disagreements)}"
13491358
)
13501359

13511360
# Validate if all slots are covered or if we should try next startup node

redis/asyncio/connection.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import enum
44
import inspect
55
import socket
6-
import ssl
76
import sys
87
import warnings
98
import weakref
@@ -27,6 +26,16 @@
2726
)
2827
from urllib.parse import ParseResult, parse_qs, unquote, urlparse
2928

29+
from ..utils import SSL_AVAILABLE
30+
31+
if SSL_AVAILABLE:
32+
import ssl
33+
from ssl import SSLContext, TLSVersion
34+
else:
35+
ssl = None
36+
TLSVersion = None
37+
SSLContext = None
38+
3039
from ..auth.token import TokenInterface
3140
from ..event import AsyncAfterConnectionReleasedEvent, EventDispatcher
3241
from ..utils import deprecated_args, format_error_message
@@ -763,10 +772,13 @@ def __init__(
763772
ssl_ca_certs: Optional[str] = None,
764773
ssl_ca_data: Optional[str] = None,
765774
ssl_check_hostname: bool = False,
766-
ssl_min_version: Optional[ssl.TLSVersion] = None,
775+
ssl_min_version: Optional[TLSVersion] = None,
767776
ssl_ciphers: Optional[str] = None,
768777
**kwargs,
769778
):
779+
if not SSL_AVAILABLE:
780+
raise RedisError("Python wasn't built with SSL support")
781+
770782
self.ssl_context: RedisSSLContext = RedisSSLContext(
771783
keyfile=ssl_keyfile,
772784
certfile=ssl_certfile,
@@ -834,9 +846,12 @@ def __init__(
834846
ca_certs: Optional[str] = None,
835847
ca_data: Optional[str] = None,
836848
check_hostname: bool = False,
837-
min_version: Optional[ssl.TLSVersion] = None,
849+
min_version: Optional[TLSVersion] = None,
838850
ciphers: Optional[str] = None,
839851
):
852+
if not SSL_AVAILABLE:
853+
raise RedisError("Python wasn't built with SSL support")
854+
840855
self.keyfile = keyfile
841856
self.certfile = certfile
842857
if cert_reqs is None:
@@ -857,9 +872,9 @@ def __init__(
857872
self.check_hostname = check_hostname
858873
self.min_version = min_version
859874
self.ciphers = ciphers
860-
self.context: Optional[ssl.SSLContext] = None
875+
self.context: Optional[SSLContext] = None
861876

862-
def get(self) -> ssl.SSLContext:
877+
def get(self) -> SSLContext:
863878
if not self.context:
864879
context = ssl.create_default_context()
865880
context.check_hostname = self.check_hostname

redis/connection.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import copy
22
import os
33
import socket
4-
import ssl
54
import sys
65
import threading
76
import time
@@ -49,6 +48,11 @@
4948
str_if_bytes,
5049
)
5150

51+
if SSL_AVAILABLE:
52+
import ssl
53+
else:
54+
ssl = None
55+
5256
if HIREDIS_AVAILABLE:
5357
import hiredis
5458

0 commit comments

Comments
 (0)