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

implement a shortcut for determining secure connections, now supporting unix sockets #695

Merged
merged 1 commit into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ To be included in 1.0.0 (unreleased)
* Fix timed out MySQL 8.0 connections being returned from Pool #660
* Ensure connections are properly closed before raising an OperationalError when the server connection is lost #660
* Ensure connections are properly closed before raising an InternalError when packet sequence numbers are out of sync #660
* Unix sockets are now internally considered secure, allowing sha256_password and caching_sha2_password auth methods to be used #695


0.0.22 (2021-11-14)
Expand Down
12 changes: 8 additions & 4 deletions aiomysql/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ def __init__(self, host="localhost", user=None, password="",
self._client_auth_plugin = auth_plugin
self._server_auth_plugin = ""
self._auth_plugin_used = ""
self._secure = False
self.server_public_key = server_public_key
self.salt = None

Expand Down Expand Up @@ -526,14 +527,15 @@ async def _connect(self):
# raise OperationalError(CR.CR_SERVER_GONE_ERROR,
# "MySQL server has gone away (%r)" % (e,))
try:
if self._unix_socket and self._host in ('localhost', '127.0.0.1'):
if self._unix_socket:
self._reader, self._writer = await \
asyncio.wait_for(
_open_unix_connection(
self._unix_socket),
timeout=self.connect_timeout)
self.host_info = "Localhost via UNIX socket: " + \
self._unix_socket
self._secure = True
else:
self._reader, self._writer = await \
asyncio.wait_for(
Expand Down Expand Up @@ -743,7 +745,7 @@ async def _request_authentication(self):
if self.user is None:
raise ValueError("Did not specify a username")

if self._ssl_context:
if self._ssl_context and self.server_capabilities & CLIENT.SSL:
# capablities, max packet, charset
data = struct.pack('<IIB', self.client_flag, 16777216, 33)
data += b'\x00' * (32 - len(data))
Expand All @@ -770,6 +772,8 @@ async def _request_authentication(self):
server_hostname=self._host
)

self._secure = True

charset_id = charset_by_name(self.charset).id
if isinstance(self.user, str):
_user = self.user.encode(self.encoding)
Expand Down Expand Up @@ -960,7 +964,7 @@ async def caching_sha2_password_auth(self, pkt):

logger.debug("caching sha2: Trying full auth...")

if self._ssl_context:
if self._secure:
logger.debug("caching sha2: Sending plain "
"password via secure connection")
self.write_packet(self._password.encode('latin1') + b'\0')
Expand Down Expand Up @@ -991,7 +995,7 @@ async def caching_sha2_password_auth(self, pkt):
pkt.check_error()

async def sha256_password_auth(self, pkt):
if self._ssl_context:
if self._secure:
logger.debug("sha256: Sending plain password")
data = self._password.encode('latin1') + b'\0'
self.write_packet(data)
Expand Down