Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for SNI #755

Merged
merged 7 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions kazoo/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def __init__(
ca=None,
use_ssl=False,
verify_certs=True,
check_hostname=False,
jessesightler-redhat marked this conversation as resolved.
Show resolved Hide resolved
**kwargs,
):
"""Create a :class:`KazooClient` instance. All time arguments
Expand Down Expand Up @@ -237,6 +238,7 @@ def __init__(

self.use_ssl = use_ssl
self.verify_certs = verify_certs
self.check_hostname = check_hostname
self.certfile = certfile
self.keyfile = keyfile
self.keyfile_password = keyfile_password
Expand Down Expand Up @@ -758,15 +760,18 @@ def command(self, cmd=b"ruok"):
raise ConnectionLoss("No connection to server")

peer = self._connection._socket.getpeername()[:2]
peer_host = self._connection._socket.getpeername()[1]
sock = self.handler.create_connection(
peer,
hostname=peer_host,
timeout=self._session_timeout / 1000.0,
use_ssl=self.use_ssl,
ca=self.ca,
certfile=self.certfile,
keyfile=self.keyfile,
keyfile_password=self.keyfile_password,
verify_certs=self.verify_certs,
check_hostname=self.check_hostname
)
sock.sendall(cmd)
result = sock.recv(8192)
Expand Down
6 changes: 4 additions & 2 deletions kazoo/handlers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,15 @@ def create_tcp_socket(module):
def create_tcp_connection(
module,
address,
hostname=None,
timeout=None,
use_ssl=False,
ca=None,
certfile=None,
keyfile=None,
keyfile_password=None,
verify_certs=True,
check_hostname=False,
options=None,
ciphers=None,
):
Expand Down Expand Up @@ -241,7 +243,7 @@ def create_tcp_connection(
# verify_mode to CERT_NONE.
# TODO: Make hostname verification configurable as some users may
jessesightler-redhat marked this conversation as resolved.
Show resolved Hide resolved
# elect to use it.
context.check_hostname = False
context.check_hostname = check_hostname
context.verify_mode = (
ssl.CERT_REQUIRED if verify_certs else ssl.CERT_NONE
jessesightler-redhat marked this conversation as resolved.
Show resolved Hide resolved
)
Expand All @@ -258,7 +260,7 @@ def create_tcp_connection(
addrs = socket.getaddrinfo(
address[0], address[1], 0, socket.SOCK_STREAM
)
conn = context.wrap_socket(module.socket(addrs[0][0]))
conn = context.wrap_socket(module.socket(addrs[0][0]), server_hostname=hostname)
jessesightler-redhat marked this conversation as resolved.
Show resolved Hide resolved
conn.settimeout(timeout_at)
conn.connect(address)
sock = conn
Expand Down
2 changes: 2 additions & 0 deletions kazoo/protocol/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,13 +703,15 @@ def _connect(self, host, hostip, port):
with self._socket_error_handling():
self._socket = self.handler.create_connection(
address=(hostip, port),
hostname=host,
timeout=client._session_timeout / 1000.0,
use_ssl=self.client.use_ssl,
keyfile=self.client.keyfile,
certfile=self.client.certfile,
ca=self.client.ca,
keyfile_password=self.client.keyfile_password,
verify_certs=self.client.verify_certs,
check_hostname=self.client.check_hostname
)

self._socket.setblocking(0)
Expand Down
16 changes: 15 additions & 1 deletion kazoo/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,26 @@ def test_timeout_arg(self):
timeout = call_args[0][1]
assert timeout >= 0, "socket timeout must be nonnegative"

def test_ssl_server_hostname(self):
jessesightler-redhat marked this conversation as resolved.
Show resolved Hide resolved
from kazoo.handlers import utils
from kazoo.handlers.utils import create_tcp_connection, socket, ssl
jessesightler-redhat marked this conversation as resolved.
Show resolved Hide resolved

with patch.object(utils, "_set_default_tcpsock_options"):
with patch.object(ssl.SSLContext, "wrap_socket") as wrap_socket:
create_tcp_connection(
socket, ("127.0.0.1", 2181), timeout=1.5, hostname="fakehostname", use_ssl=True
jessesightler-redhat marked this conversation as resolved.
Show resolved Hide resolved
)

for call_args in wrap_socket.call_args_list:
server_hostname = call_args[1]['server_hostname']
assert server_hostname == "fakehostname"

def test_timeout_arg_eventlet(self):
if not EVENTLET_HANDLER_AVAILABLE:
pytest.skip("eventlet handler not available.")

from kazoo.handlers import utils
from kazoo.handlers.utils import create_tcp_connection, time
from kazoo.handlers.utils import create_tcp_connection, socket, time
jessesightler-redhat marked this conversation as resolved.
Show resolved Hide resolved

with patch.object(socket, "create_connection") as create_connection:
with patch.object(utils, "_set_default_tcpsock_options"):
Expand Down
Loading