Skip to content

Commit 8fd0b71

Browse files
committed
gh-155896: Allow overriding TLS roles in StreamWriter.start_tls
1 parent 0fba211 commit 8fd0b71

4 files changed

Lines changed: 56 additions & 2 deletions

File tree

‎Doc/library/asyncio-stream.rst‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ StreamWriter
399399
to the event loop explicitly with ``await asyncio.sleep(0)``
400400
(see :func:`asyncio.sleep`).
401401

402-
.. method:: start_tls(sslcontext, *, server_hostname=None, \
402+
.. method:: start_tls(sslcontext, *, server_side=None, server_hostname=None, \
403403
ssl_handshake_timeout=None, ssl_shutdown_timeout=None)
404404
:async:
405405

@@ -409,6 +409,10 @@ StreamWriter
409409

410410
* *sslcontext*: a configured instance of :class:`~ssl.SSLContext`.
411411

412+
* *server_side*: set to ``True`` to perform the server side of the TLS
413+
handshake, or ``False`` to perform the client side. By default
414+
(``None``), the role is inferred from how the stream was created.
415+
412416
* *server_hostname*: sets or overrides the host name that the target
413417
server's certificate will be matched against.
414418

@@ -426,6 +430,8 @@ StreamWriter
426430
Added the *ssl_shutdown_timeout* parameter.
427431

428432
.. versionchanged:: 3.16
433+
Added the *server_side* parameter.
434+
429435
Raises a ``ValueError`` if ``sslcontext.check_hostname`` is ``True``
430436
and ``server_hostname`` is not supplied.
431437

‎Lib/asyncio/streams.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,11 +395,13 @@ async def drain(self):
395395
await self._protocol._drain_helper()
396396

397397
async def start_tls(self, sslcontext, *,
398+
server_side=None,
398399
server_hostname=None,
399400
ssl_handshake_timeout=None,
400401
ssl_shutdown_timeout=None):
401402
"""Upgrade an existing stream-based connection to TLS."""
402-
server_side = self._protocol._client_connected_cb is not None
403+
if server_side is None:
404+
server_side = self._protocol._client_connected_cb is not None
403405
protocol = self._protocol
404406
await self.drain()
405407
new_transport = await self._loop.start_tls( # type: ignore

‎Lib/test/test_asyncio/test_streams.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import asyncio
1616
from test.test_asyncio import utils as test_utils
17+
from test import support
1718
from test.support import socket_helper
1819

1920

@@ -861,6 +862,49 @@ async def run_test():
861862
self.loop.run_until_complete(run_test())
862863
self.assertEqual(messages, [])
863864

865+
@unittest.skipIf(ssl is None, 'No ssl module')
866+
def test_start_tls_reversed_roles(self):
867+
async def run_test():
868+
accepted = self.loop.create_future()
869+
async with await asyncio.start_server(
870+
lambda r, w: accepted.set_result((r, w)),
871+
socket_helper.HOSTv4, 0,
872+
) as server:
873+
address = server.sockets[0].getsockname()
874+
client_reader, client_writer = await asyncio.open_connection(
875+
*address
876+
)
877+
server_reader, server_writer = await accepted
878+
try:
879+
await asyncio.gather(
880+
client_writer.start_tls(
881+
test_utils.simple_server_sslcontext(),
882+
server_side=True,
883+
),
884+
server_writer.start_tls(
885+
test_utils.simple_client_sslcontext(),
886+
server_side=False,
887+
),
888+
)
889+
client_writer.write(b"hello")
890+
self.assertEqual(
891+
await server_reader.readexactly(5), b"hello"
892+
)
893+
server_writer.write(b"world")
894+
self.assertEqual(
895+
await client_reader.readexactly(5), b"world"
896+
)
897+
finally:
898+
client_writer.close()
899+
server_writer.close()
900+
await asyncio.gather(
901+
client_writer.wait_closed(), server_writer.wait_closed()
902+
)
903+
904+
self.loop.run_until_complete(
905+
asyncio.wait_for(run_test(), support.SHORT_TIMEOUT)
906+
)
907+
864908
def test_streamwriter_start_tls_updates_reader_transport(self):
865909
reader = asyncio.StreamReader(loop=self.loop)
866910
protocol = asyncio.StreamReaderProtocol(reader, loop=self.loop)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Allow overriding the TLS client or server role in
2+
:meth:`asyncio.StreamWriter.start_tls` with the *server_side* keyword argument.

0 commit comments

Comments
 (0)