Skip to content

Commit 7c3bfbf

Browse files
committed
Narrowed LIKE scope as was picking up unrelated rows
Fixed unawaited authentication plugin coros
1 parent 19ab680 commit 7c3bfbf

File tree

4 files changed

+80
-36
lines changed

4 files changed

+80
-36
lines changed

aiomysql/connection.py

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -795,38 +795,44 @@ async def _request_authentication(self):
795795
"for auth method %r", auth_plugin)
796796

797797
async def _process_auth(self, plugin_name, auth_packet):
798+
# These auth plugins do their own packet handling
798799
if plugin_name == b"caching_sha2_password":
799-
return self.caching_sha2_password_auth(auth_packet)
800+
await self.caching_sha2_password_auth(auth_packet)
800801
elif plugin_name == b"sha256_password":
801-
return self.sha256_password_auth(auth_packet)
802-
elif plugin_name == b"mysql_native_password":
803-
# https://dev.mysql.com/doc/internals/en/
804-
# secure-password-authentication.html#packet-Authentication::
805-
# Native41
806-
data = _auth.scramble_native_password(
807-
self._password.encode('latin1'),
808-
auth_packet.read_all())
809-
elif plugin_name == b"mysql_old_password":
810-
# https://dev.mysql.com/doc/internals/en/
811-
# old-password-authentication.html
812-
data = _auth.scramble_old_password(self._password.encode('latin1'),
813-
auth_packet.read_all()) + b'\0'
814-
elif plugin_name == b"mysql_clear_password":
815-
# https://dev.mysql.com/doc/internals/en/
816-
# clear-text-authentication.html
817-
data = self._password.encode('latin1') + b'\0'
802+
await self.sha256_password_auth(auth_packet)
818803
else:
819-
raise OperationalError(
820-
2059, "Authentication plugin '%s' not configured" % plugin_name
821-
)
822804

823-
self.write_packet(data)
824-
pkt = await self._read_packet()
825-
pkt.check_error()
805+
if plugin_name == b"mysql_native_password":
806+
# https://dev.mysql.com/doc/internals/en/
807+
# secure-password-authentication.html#packet-Authentication::
808+
# Native41
809+
data = _auth.scramble_native_password(
810+
self._password.encode('latin1'),
811+
auth_packet.read_all())
812+
elif plugin_name == b"mysql_old_password":
813+
# https://dev.mysql.com/doc/internals/en/
814+
# old-password-authentication.html
815+
data = _auth.scramble_old_password(
816+
self._password.encode('latin1'),
817+
auth_packet.read_all()
818+
) + b'\0'
819+
elif plugin_name == b"mysql_clear_password":
820+
# https://dev.mysql.com/doc/internals/en/
821+
# clear-text-authentication.html
822+
data = self._password.encode('latin1') + b'\0'
823+
else:
824+
raise OperationalError(
825+
2059, "Authentication plugin '{0}'"
826+
" not configured".format(plugin_name)
827+
)
828+
829+
self.write_packet(data)
830+
pkt = await self._read_packet()
831+
pkt.check_error()
826832

827-
self._auth_plugin_used = plugin_name
833+
self._auth_plugin_used = plugin_name
828834

829-
return pkt
835+
return pkt
830836

831837
async def caching_sha2_password_auth(self, pkt):
832838
# No password fast path

examples/example_ssl.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import asyncio
2+
import ssl
3+
import aiomysql
4+
5+
ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
6+
ctx.check_hostname = False
7+
ctx.load_verify_locations(cafile='../tests/ssl_resources/ssl/ca.pem')
8+
9+
10+
async def main():
11+
async with aiomysql.create_pool(
12+
host='localhost', port=3306, user='root',
13+
password='rootpw', ssl=ctx,
14+
auth_plugin='mysql_clear_password') as pool:
15+
16+
async with pool.get() as conn:
17+
async with conn.cursor() as cur:
18+
# Run simple command
19+
await cur.execute("SHOW DATABASES;")
20+
value = await cur.fetchall()
21+
22+
values = [item[0] for item in value]
23+
# Spot check the answers, we should at least have mysql
24+
# and information_schema
25+
assert 'mysql' in values, \
26+
'Could not find the "mysql" table'
27+
assert 'information_schema' in values, \
28+
'Could not find the "mysql" table'
29+
30+
# Check TLS variables
31+
await cur.execute("SHOW STATUS LIKE 'Ssl_version%';")
32+
value = await cur.fetchone()
33+
34+
# The context has TLS
35+
assert value[1].startswith('TLS'), \
36+
'Not connected to the database with TLS'
37+
38+
asyncio.get_event_loop().run_until_complete(main())

tests/conftest.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ def pytest_generate_tests(metafunc):
3636
metafunc.parametrize("loop_type", loop_type)
3737

3838
if 'mysql_tag' in metafunc.fixturenames:
39-
# tags = set(metafunc.config.option.mysql_tag)
40-
# if not tags:
41-
# tags = ['5.7']
42-
# elif 'all' in tags:
43-
# tags = ['5.6', '5.7', '8.0']
44-
# else:
45-
# tags = list(tags)
46-
metafunc.parametrize("mysql_tag", ['5.6', '8.0'], scope='session')
39+
tags = set(metafunc.config.option.mysql_tag)
40+
if not tags:
41+
tags = ['5.6', '8.0']
42+
elif 'all' in tags:
43+
tags = ['5.6', '5.7', '8.0']
44+
else:
45+
tags = list(tags)
46+
metafunc.parametrize("mysql_tag", tags, scope='session')
4747

4848

4949
# This is here unless someone fixes the generate_tests bit
@@ -288,7 +288,7 @@ def mysql_server(unused_port, docker, session_id, mysql_tag, request):
288288
assert result['have_ssl'] == "YES", \
289289
"SSL Not Enabled on docker'd MySQL"
290290

291-
cursor.execute("SHOW STATUS LIKE '%Ssl_version%'")
291+
cursor.execute("SHOW STATUS LIKE 'Ssl_version%'")
292292

293293
result = cursor.fetchone()
294294
# As we connected with TLS, it should start with that :D

tests/test_ssl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async def test_tls_connect(mysql_server, loop):
2222
'Could not find the "mysql" table'
2323

2424
# Check TLS variables
25-
await cur.execute("SHOW STATUS LIKE '%Ssl_version%';")
25+
await cur.execute("SHOW STATUS LIKE 'Ssl_version%';")
2626
value = await cur.fetchone()
2727

2828
# The context has TLS

0 commit comments

Comments
 (0)