-
Notifications
You must be signed in to change notification settings - Fork 253
/
connection.py
1338 lines (1134 loc) · 48.6 KB
/
connection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Python implementation of the MySQL client-server protocol
# http://dev.mysql.com/doc/internals/en/client-server-protocol.html
import asyncio
import os
import socket
import struct
import sys
import warnings
import configparser
import getpass
from functools import partial
from pymysql.charset import charset_by_name, charset_by_id
from pymysql.constants import SERVER_STATUS
from pymysql.constants import CLIENT
from pymysql.constants import COMMAND
from pymysql.constants import FIELD_TYPE
from pymysql.util import byte2int, int2byte
from pymysql.converters import (escape_item, encoders, decoders,
escape_string, escape_bytes_prefixed, through)
from pymysql.err import (Warning, Error,
InterfaceError, DataError, DatabaseError,
OperationalError,
IntegrityError, InternalError, NotSupportedError,
ProgrammingError)
from pymysql.connections import TEXT_TYPES, MAX_PACKET_LEN, DEFAULT_CHARSET
from pymysql.connections import _auth
from pymysql.connections import pack_int24
from pymysql.connections import MysqlPacket
from pymysql.connections import FieldDescriptorPacket
from pymysql.connections import EOFPacketWrapper
from pymysql.connections import OKPacketWrapper
from pymysql.connections import LoadLocalPacketWrapper
from pymysql.connections import lenenc_int
# from aiomysql.utils import _convert_to_str
from .cursors import Cursor
from .utils import _ConnectionContextManager, _ContextManager
from .log import logger
DEFAULT_USER = getpass.getuser()
def connect(host="localhost", user=None, password="",
db=None, port=3306, unix_socket=None,
charset='', sql_mode=None,
read_default_file=None, conv=decoders, use_unicode=None,
client_flag=0, cursorclass=Cursor, init_command=None,
connect_timeout=None, read_default_group=None,
no_delay=None, autocommit=False, echo=False,
local_infile=False, loop=None, ssl=None, auth_plugin='',
program_name='', server_public_key=None):
"""See connections.Connection.__init__() for information about
defaults."""
coro = _connect(host=host, user=user, password=password, db=db,
port=port, unix_socket=unix_socket, charset=charset,
sql_mode=sql_mode, read_default_file=read_default_file,
conv=conv, use_unicode=use_unicode,
client_flag=client_flag, cursorclass=cursorclass,
init_command=init_command,
connect_timeout=connect_timeout,
read_default_group=read_default_group,
no_delay=no_delay, autocommit=autocommit, echo=echo,
local_infile=local_infile, loop=loop, ssl=ssl,
auth_plugin=auth_plugin, program_name=program_name)
return _ConnectionContextManager(coro)
async def _connect(*args, **kwargs):
conn = Connection(*args, **kwargs)
await conn._connect()
return conn
class Connection:
"""Representation of a socket with a mysql server.
The proper way to get an instance of this class is to call
connect().
"""
def __init__(self, host="localhost", user=None, password="",
db=None, port=3306, unix_socket=None,
charset='', sql_mode=None,
read_default_file=None, conv=decoders, use_unicode=None,
client_flag=0, cursorclass=Cursor, init_command=None,
connect_timeout=None, read_default_group=None,
no_delay=None, autocommit=False, echo=False,
local_infile=False, loop=None, ssl=None, auth_plugin='',
program_name='', server_public_key=None):
"""
Establish a connection to the MySQL database. Accepts several
arguments:
:param host: Host where the database server is located
:param user: Username to log in as
:param password: Password to use.
:param db: Database to use, None to not use a particular one.
:param port: MySQL port to use, default is usually OK.
:param unix_socket: Optionally, you can use a unix socket rather
than TCP/IP.
:param charset: Charset you want to use.
:param sql_mode: Default SQL_MODE to use.
:param read_default_file: Specifies my.cnf file to read these
parameters from under the [client] section.
:param conv: Decoders dictionary to use instead of the default one.
This is used to provide custom marshalling of types.
See converters.
:param use_unicode: Whether or not to default to unicode strings.
:param client_flag: Custom flags to send to MySQL. Find
potential values in constants.CLIENT.
:param cursorclass: Custom cursor class to use.
:param init_command: Initial SQL statement to run when connection is
established.
:param connect_timeout: Timeout before throwing an exception
when connecting.
:param read_default_group: Group to read from in the configuration
file.
:param no_delay: Disable Nagle's algorithm on the socket
:param autocommit: Autocommit mode. None means use server default.
(default: False)
:param local_infile: boolean to enable the use of LOAD DATA LOCAL
command. (default: False)
:param ssl: Optional SSL Context to force SSL
:param auth_plugin: String to manually specify the authentication
plugin to use, i.e you will want to use mysql_clear_password
when using IAM authentication with Amazon RDS.
(default: Server Default)
:param program_name: Program name string to provide when
handshaking with MySQL. (default: sys.argv[0])
:param server_public_key: SHA256 authentication plugin public
key value.
:param loop: asyncio loop
"""
self._loop = loop or asyncio.get_event_loop()
if use_unicode is None and sys.version_info[0] > 2:
use_unicode = True
if read_default_file:
if not read_default_group:
read_default_group = "client"
cfg = configparser.RawConfigParser()
cfg.read(os.path.expanduser(read_default_file))
_config = partial(cfg.get, read_default_group)
user = _config("user", fallback=user)
password = _config("password", fallback=password)
host = _config("host", fallback=host)
db = _config("database", fallback=db)
unix_socket = _config("socket", fallback=unix_socket)
port = int(_config("port", fallback=port))
charset = _config("default-character-set", fallback=charset)
# pymysql port
if no_delay is not None:
warnings.warn("no_delay option is deprecated", DeprecationWarning)
no_delay = bool(no_delay)
else:
no_delay = True
self._host = host
self._port = port
self._user = user or DEFAULT_USER
self._password = password or ""
self._db = db
self._no_delay = no_delay
self._echo = echo
self._last_usage = self._loop.time()
self._client_auth_plugin = auth_plugin
self._server_auth_plugin = ""
self._auth_plugin_used = ""
self.server_public_key = server_public_key
self.salt = None
from . import __version__
self._connect_attrs = {
'_client_name': 'aiomysql',
'_pid': str(os.getpid()),
'_client_version': __version__,
}
if program_name:
self._connect_attrs["program_name"] = program_name
elif sys.argv:
self._connect_attrs["program_name"] = sys.argv[0]
self._unix_socket = unix_socket
if charset:
self._charset = charset
self.use_unicode = True
else:
self._charset = DEFAULT_CHARSET
self.use_unicode = False
if use_unicode is not None:
self.use_unicode = use_unicode
self._ssl_context = ssl
if ssl:
client_flag |= CLIENT.SSL
self._encoding = charset_by_name(self._charset).encoding
if local_infile:
client_flag |= CLIENT.LOCAL_FILES
client_flag |= CLIENT.CAPABILITIES
client_flag |= CLIENT.MULTI_STATEMENTS
if self._db:
client_flag |= CLIENT.CONNECT_WITH_DB
self.client_flag = client_flag
self.cursorclass = cursorclass
self.connect_timeout = connect_timeout
self._result = None
self._affected_rows = 0
self.host_info = "Not connected"
#: specified autocommit mode. None means use server default.
self.autocommit_mode = autocommit
self.encoders = encoders # Need for MySQLdb compatibility.
self.decoders = conv
self.sql_mode = sql_mode
self.init_command = init_command
# asyncio StreamReader, StreamWriter
self._reader = None
self._writer = None
# If connection was closed for specific reason, we should show that to
# user
self._close_reason = None
@property
def host(self):
"""MySQL server IP address or name"""
return self._host
@property
def port(self):
"""MySQL server TCP/IP port"""
return self._port
@property
def unix_socket(self):
"""MySQL Unix socket file location"""
return self._unix_socket
@property
def db(self):
"""Current database name."""
return self._db
@property
def user(self):
"""User used while connecting to MySQL"""
return self._user
@property
def echo(self):
"""Return echo mode status."""
return self._echo
@property
def last_usage(self):
"""Return time() when connection was used."""
return self._last_usage
@property
def loop(self):
return self._loop
@property
def closed(self):
"""The readonly property that returns ``True`` if connections is
closed.
"""
return self._writer is None
@property
def encoding(self):
"""Encoding employed for this connection."""
return self._encoding
@property
def charset(self):
"""Returns the character set for current connection."""
return self._charset
def close(self):
"""Close socket connection"""
if self._writer:
self._writer.transport.close()
self._writer = None
self._reader = None
async def ensure_closed(self):
"""Send quit command and then close socket connection"""
if self._writer is None:
# connection has been closed
return
send_data = struct.pack('<i', 1) + int2byte(COMMAND.COM_QUIT)
self._writer.write(send_data)
await self._writer.drain()
self.close()
async def autocommit(self, value):
"""Enable/disable autocommit mode for current MySQL session.
:param value: ``bool``, toggle autocommit
"""
self.autocommit_mode = bool(value)
current = self.get_autocommit()
if value != current:
await self._send_autocommit_mode()
def get_autocommit(self):
"""Returns autocommit status for current MySQL session.
:returns bool: current autocommit status."""
status = self.server_status & SERVER_STATUS.SERVER_STATUS_AUTOCOMMIT
return bool(status)
async def _read_ok_packet(self):
pkt = await self._read_packet()
if not pkt.is_ok_packet():
raise OperationalError(2014, "Command Out of Sync")
ok = OKPacketWrapper(pkt)
self.server_status = ok.server_status
return True
async def _send_autocommit_mode(self):
"""Set whether or not to commit after every execute() """
await self._execute_command(
COMMAND.COM_QUERY,
"SET AUTOCOMMIT = %s" % self.escape(self.autocommit_mode))
await self._read_ok_packet()
async def begin(self):
"""Begin transaction."""
await self._execute_command(COMMAND.COM_QUERY, "BEGIN")
await self._read_ok_packet()
async def commit(self):
"""Commit changes to stable storage."""
await self._execute_command(COMMAND.COM_QUERY, "COMMIT")
await self._read_ok_packet()
async def rollback(self):
"""Roll back the current transaction."""
await self._execute_command(COMMAND.COM_QUERY, "ROLLBACK")
await self._read_ok_packet()
async def select_db(self, db):
"""Set current db"""
await self._execute_command(COMMAND.COM_INIT_DB, db)
await self._read_ok_packet()
async def show_warnings(self):
"""SHOW WARNINGS"""
await self._execute_command(COMMAND.COM_QUERY, "SHOW WARNINGS")
result = MySQLResult(self)
await result.read()
return result.rows
def escape(self, obj):
""" Escape whatever value you pass to it"""
if isinstance(obj, str):
return "'" + self.escape_string(obj) + "'"
if isinstance(obj, bytes):
return escape_bytes_prefixed(obj)
return escape_item(obj, self._charset)
def literal(self, obj):
"""Alias for escape()"""
return self.escape(obj)
def escape_string(self, s):
if (self.server_status &
SERVER_STATUS.SERVER_STATUS_NO_BACKSLASH_ESCAPES):
return s.replace("'", "''")
return escape_string(s)
def cursor(self, *cursors):
"""Instantiates and returns a cursor
By default, :class:`Cursor` is returned. It is possible to also give a
custom cursor through the cursor_class parameter, but it needs to
be a subclass of :class:`Cursor`
:param cursor: custom cursor class.
:returns: instance of cursor, by default :class:`Cursor`
:raises TypeError: cursor_class is not a subclass of Cursor.
"""
self._ensure_alive()
self._last_usage = self._loop.time()
try:
if cursors and \
any(not issubclass(cursor, Cursor) for cursor in cursors):
raise TypeError('Custom cursor must be subclass of Cursor')
except TypeError:
raise TypeError('Custom cursor must be subclass of Cursor')
if cursors and len(cursors) == 1:
cur = cursors[0](self, self._echo)
elif cursors:
cursor_name = ''.join(map(lambda x: x.__name__, cursors)) \
.replace('Cursor', '') + 'Cursor'
cursor_class = type(cursor_name, cursors, {})
cur = cursor_class(self, self._echo)
else:
cur = self.cursorclass(self, self._echo)
fut = self._loop.create_future()
fut.set_result(cur)
return _ContextManager(fut)
# The following methods are INTERNAL USE ONLY (called from Cursor)
async def query(self, sql, unbuffered=False):
# logger.debug("DEBUG: sending query: %s", _convert_to_str(sql))
if isinstance(sql, str):
sql = sql.encode(self.encoding, 'surrogateescape')
await self._execute_command(COMMAND.COM_QUERY, sql)
await self._read_query_result(unbuffered=unbuffered)
return self._affected_rows
async def next_result(self):
await self._read_query_result()
return self._affected_rows
def affected_rows(self):
return self._affected_rows
async def kill(self, thread_id):
arg = struct.pack('<I', thread_id)
await self._execute_command(COMMAND.COM_PROCESS_KILL, arg)
await self._read_ok_packet()
async def ping(self, reconnect=True):
"""Check if the server is alive"""
if self._writer is None and self._reader is None:
if reconnect:
await self._connect()
reconnect = False
else:
raise Error("Already closed")
try:
await self._execute_command(COMMAND.COM_PING, "")
await self._read_ok_packet()
except Exception:
if reconnect:
await self._connect()
await self.ping(False)
else:
raise
async def set_charset(self, charset):
"""Sets the character set for the current connection"""
# Make sure charset is supported.
encoding = charset_by_name(charset).encoding
await self._execute_command(COMMAND.COM_QUERY, "SET NAMES %s"
% self.escape(charset))
await self._read_packet()
self._charset = charset
self._encoding = encoding
async def _connect(self):
# TODO: Set close callback
# raise OperationalError(2006,
# "MySQL server has gone away (%r)" % (e,))
try:
if self._unix_socket and self._host in ('localhost', '127.0.0.1'):
self._reader, self._writer = await \
asyncio.wait_for(
asyncio.open_unix_connection(
self._unix_socket),
timeout=self.connect_timeout)
self.host_info = "Localhost via UNIX socket: " + \
self._unix_socket
else:
self._reader, self._writer = await \
asyncio.wait_for(
asyncio.open_connection(
self._host,
self._port),
timeout=self.connect_timeout)
self._set_keep_alive()
self.host_info = "socket %s:%d" % (self._host, self._port)
# do not set no delay in case of unix_socket
if self._no_delay and not self._unix_socket:
self._set_nodelay(True)
self._next_seq_id = 0
await self._get_server_information()
await self._request_authentication()
self.connected_time = self._loop.time()
if self.sql_mode is not None:
await self.query("SET sql_mode=%s" % (self.sql_mode,))
if self.init_command is not None:
await self.query(self.init_command)
await self.commit()
if self.autocommit_mode is not None:
await self.autocommit(self.autocommit_mode)
except Exception as e:
if self._writer:
self._writer.transport.close()
self._reader = None
self._writer = None
raise OperationalError(2003,
"Can't connect to MySQL server on %r" %
self._host) from e
def _set_keep_alive(self):
transport = self._writer.transport
transport.pause_reading()
raw_sock = transport.get_extra_info('socket', default=None)
if raw_sock is None:
raise RuntimeError("Transport does not expose socket instance")
raw_sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
transport.resume_reading()
def _set_nodelay(self, value):
flag = int(bool(value))
transport = self._writer.transport
transport.pause_reading()
raw_sock = transport.get_extra_info('socket', default=None)
if raw_sock is None:
raise RuntimeError("Transport does not expose socket instance")
raw_sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, flag)
transport.resume_reading()
def write_packet(self, payload):
"""Writes an entire "mysql packet" in its entirety to the network
addings its length and sequence number.
"""
# Internal note: when you build packet manually and calls
# _write_bytes() directly, you should set self._next_seq_id properly.
data = pack_int24(len(payload)) + int2byte(self._next_seq_id) + payload
self._write_bytes(data)
self._next_seq_id = (self._next_seq_id + 1) % 256
async def _read_packet(self, packet_type=MysqlPacket):
"""Read an entire "mysql packet" in its entirety from the network
and return a MysqlPacket type that represents the results.
"""
buff = b''
while True:
try:
packet_header = await self._read_bytes(4)
except asyncio.CancelledError:
self._close_on_cancel()
raise
btrl, btrh, packet_number = struct.unpack(
'<HBB', packet_header)
bytes_to_read = btrl + (btrh << 16)
# Outbound and inbound packets are numbered sequentialy, so
# we increment in both write_packet and read_packet. The count
# is reset at new COMMAND PHASE.
if packet_number != self._next_seq_id:
raise InternalError(
"Packet sequence number wrong - got %d expected %d" %
(packet_number, self._next_seq_id))
self._next_seq_id = (self._next_seq_id + 1) % 256
try:
recv_data = await self._read_bytes(bytes_to_read)
except asyncio.CancelledError:
self._close_on_cancel()
raise
buff += recv_data
# https://dev.mysql.com/doc/internals/en/sending-more-than-16mbyte.html
if bytes_to_read == 0xffffff:
continue
if bytes_to_read < MAX_PACKET_LEN:
break
packet = packet_type(buff, self._encoding)
packet.check_error()
return packet
async def _read_bytes(self, num_bytes):
try:
data = await self._reader.readexactly(num_bytes)
except asyncio.IncompleteReadError as e:
msg = "Lost connection to MySQL server during query"
raise OperationalError(2013, msg) from e
except (IOError, OSError) as e:
msg = "Lost connection to MySQL server during query (%s)" % (e,)
raise OperationalError(2013, msg) from e
return data
def _write_bytes(self, data):
return self._writer.write(data)
async def _read_query_result(self, unbuffered=False):
self._result = None
if unbuffered:
try:
result = MySQLResult(self)
await result.init_unbuffered_query()
except BaseException:
result.unbuffered_active = False
result.connection = None
raise
else:
result = MySQLResult(self)
await result.read()
self._result = result
self._affected_rows = result.affected_rows
if result.server_status is not None:
self.server_status = result.server_status
def insert_id(self):
if self._result:
return self._result.insert_id
else:
return 0
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
if exc_type:
self.close()
else:
await self.ensure_closed()
return
async def _execute_command(self, command, sql):
self._ensure_alive()
# If the last query was unbuffered, make sure it finishes before
# sending new commands
if self._result is not None:
if self._result.unbuffered_active:
warnings.warn("Previous unbuffered result was left incomplete")
self._result._finish_unbuffered_query()
while self._result.has_next:
await self.next_result()
self._result = None
if isinstance(sql, str):
sql = sql.encode(self._encoding)
chunk_size = min(MAX_PACKET_LEN, len(sql) + 1) # +1 is for command
prelude = struct.pack('<iB', chunk_size, command)
self._write_bytes(prelude + sql[:chunk_size - 1])
# logger.debug(dump_packet(prelude + sql))
self._next_seq_id = 1
if chunk_size < MAX_PACKET_LEN:
return
sql = sql[chunk_size - 1:]
while True:
chunk_size = min(MAX_PACKET_LEN, len(sql))
self.write_packet(sql[:chunk_size])
sql = sql[chunk_size:]
if not sql and chunk_size < MAX_PACKET_LEN:
break
async def _request_authentication(self):
# https://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::HandshakeResponse
if int(self.server_version.split('.', 1)[0]) >= 5:
self.client_flag |= CLIENT.MULTI_RESULTS
if self.user is None:
raise ValueError("Did not specify a username")
if self._ssl_context:
# capablities, max packet, charset
data = struct.pack('<IIB', self.client_flag, 16777216, 33)
data += b'\x00' * (32 - len(data))
self.write_packet(data)
# Stop sending events to data_received
self._writer.transport.pause_reading()
# Get the raw socket from the transport
raw_sock = self._writer.transport.get_extra_info('socket',
default=None)
if raw_sock is None:
raise RuntimeError("Transport does not expose socket instance")
raw_sock = raw_sock.dup()
self._writer.transport.close()
# MySQL expects TLS negotiation to happen in the middle of a
# TCP connection not at start. Passing in a socket to
# open_connection will cause it to negotiate TLS on an existing
# connection not initiate a new one.
self._reader, self._writer = await asyncio.open_connection(
sock=raw_sock, ssl=self._ssl_context,
server_hostname=self._host
)
charset_id = charset_by_name(self.charset).id
if isinstance(self.user, str):
_user = self.user.encode(self.encoding)
else:
_user = self.user
data_init = struct.pack('<iIB23s', self.client_flag, MAX_PACKET_LEN,
charset_id, b'')
data = data_init + _user + b'\0'
authresp = b''
auth_plugin = self._client_auth_plugin
if not self._client_auth_plugin:
# Contains the auth plugin from handshake
auth_plugin = self._server_auth_plugin
if auth_plugin in ('', 'mysql_native_password'):
authresp = _auth.scramble_native_password(
self._password.encode('latin1'), self.salt)
elif auth_plugin == 'caching_sha2_password':
if self._password:
authresp = _auth.scramble_caching_sha2(
self._password.encode('latin1'), self.salt
)
# Else: empty password
elif auth_plugin == 'sha256_password':
if self._ssl_context and self.server_capabilities & CLIENT.SSL:
authresp = self._password.encode('latin1') + b'\0'
elif self._password:
authresp = b'\1' # request public key
else:
authresp = b'\0' # empty password
elif auth_plugin in ('', 'mysql_clear_password'):
authresp = self._password.encode('latin1') + b'\0'
if self.server_capabilities & CLIENT.PLUGIN_AUTH_LENENC_CLIENT_DATA:
data += lenenc_int(len(authresp)) + authresp
elif self.server_capabilities & CLIENT.SECURE_CONNECTION:
data += struct.pack('B', len(authresp)) + authresp
else: # pragma: no cover
# not testing against servers without secure auth (>=5.0)
data += authresp + b'\0'
if self._db and self.server_capabilities & CLIENT.CONNECT_WITH_DB:
if isinstance(self._db, str):
db = self._db.encode(self.encoding)
else:
db = self._db
data += db + b'\0'
if self.server_capabilities & CLIENT.PLUGIN_AUTH:
name = auth_plugin
if isinstance(name, str):
name = name.encode('ascii')
data += name + b'\0'
self._auth_plugin_used = auth_plugin
# Sends the server a few pieces of client info
if self.server_capabilities & CLIENT.CONNECT_ATTRS:
connect_attrs = b''
for k, v in self._connect_attrs.items():
k, v = k.encode('utf8'), v.encode('utf8')
connect_attrs += struct.pack('B', len(k)) + k
connect_attrs += struct.pack('B', len(v)) + v
data += struct.pack('B', len(connect_attrs)) + connect_attrs
self.write_packet(data)
auth_packet = await self._read_packet()
# if authentication method isn't accepted the first byte
# will have the octet 254
if auth_packet.is_auth_switch_request():
# https://dev.mysql.com/doc/internals/en/
# connection-phase-packets.html#packet-Protocol::AuthSwitchRequest
auth_packet.read_uint8() # 0xfe packet identifier
plugin_name = auth_packet.read_string()
if (self.server_capabilities & CLIENT.PLUGIN_AUTH and
plugin_name is not None):
await self._process_auth(plugin_name, auth_packet)
else:
# send legacy handshake
data = _auth.scramble_old_password(
self._password.encode('latin1'),
auth_packet.read_all()) + b'\0'
self.write_packet(data)
await self._read_packet()
elif auth_packet.is_extra_auth_data():
if auth_plugin == "caching_sha2_password":
await self.caching_sha2_password_auth(auth_packet)
elif auth_plugin == "sha256_password":
await self.sha256_password_auth(auth_packet)
else:
raise OperationalError("Received extra packet "
"for auth method %r", auth_plugin)
async def _process_auth(self, plugin_name, auth_packet):
# These auth plugins do their own packet handling
if plugin_name == b"caching_sha2_password":
await self.caching_sha2_password_auth(auth_packet)
self._auth_plugin_used = plugin_name.decode()
elif plugin_name == b"sha256_password":
await self.sha256_password_auth(auth_packet)
self._auth_plugin_used = plugin_name.decode()
else:
if plugin_name == b"mysql_native_password":
# https://dev.mysql.com/doc/internals/en/
# secure-password-authentication.html#packet-Authentication::
# Native41
data = _auth.scramble_native_password(
self._password.encode('latin1'),
auth_packet.read_all())
elif plugin_name == b"mysql_old_password":
# https://dev.mysql.com/doc/internals/en/
# old-password-authentication.html
data = _auth.scramble_old_password(
self._password.encode('latin1'),
auth_packet.read_all()
) + b'\0'
elif plugin_name == b"mysql_clear_password":
# https://dev.mysql.com/doc/internals/en/
# clear-text-authentication.html
data = self._password.encode('latin1') + b'\0'
else:
raise OperationalError(
2059, "Authentication plugin '{0}'"
" not configured".format(plugin_name)
)
self.write_packet(data)
pkt = await self._read_packet()
pkt.check_error()
self._auth_plugin_used = plugin_name.decode()
return pkt
async def caching_sha2_password_auth(self, pkt):
# No password fast path
if not self._password:
self.write_packet(b'')
pkt = await self._read_packet()
pkt.check_error()
return pkt
if pkt.is_auth_switch_request():
# Try from fast auth
logger.debug("caching sha2: Trying fast path")
self.salt = pkt.read_all()
scrambled = _auth.scramble_caching_sha2(
self._password.encode('latin1'), self.salt
)
self.write_packet(scrambled)
pkt = await self._read_packet()
pkt.check_error()
# else: fast auth is tried in initial handshake
if not pkt.is_extra_auth_data():
raise OperationalError(
"caching sha2: Unknown packet "
"for fast auth: {0}".format(pkt._data[:1])
)
# magic numbers:
# 2 - request public key
# 3 - fast auth succeeded
# 4 - need full auth
pkt.advance(1)
n = pkt.read_uint8()
if n == 3:
logger.debug("caching sha2: succeeded by fast path.")
pkt = await self._read_packet()
pkt.check_error() # pkt must be OK packet
return pkt
if n != 4:
raise OperationalError("caching sha2: Unknown "
"result for fast auth: {0}".format(n))
logger.debug("caching sha2: Trying full auth...")
if self._ssl_context:
logger.debug("caching sha2: Sending plain "
"password via secure connection")
self.write_packet(self._password.encode('latin1') + b'\0')
pkt = await self._read_packet()
pkt.check_error()
return pkt
if not self.server_public_key:
self.write_packet(b'\x02')
pkt = await self._read_packet() # Request public key
pkt.check_error()
if not pkt.is_extra_auth_data():
raise OperationalError(
"caching sha2: Unknown packet "
"for public key: {0}".format(pkt._data[:1])
)
self.server_public_key = pkt._data[1:]
logger.debug(self.server_public_key.decode('ascii'))
data = _auth.sha2_rsa_encrypt(
self._password.encode('latin1'), self.salt,
self.server_public_key
)
self.write_packet(data)
pkt = await self._read_packet()
pkt.check_error()
async def sha256_password_auth(self, pkt):
if self._ssl_context:
logger.debug("sha256: Sending plain password")
data = self._password.encode('latin1') + b'\0'
self.write_packet(data)
pkt = await self._read_packet()
pkt.check_error()
return pkt
if pkt.is_auth_switch_request():
self.salt = pkt.read_all()
if not self.server_public_key and self._password:
# Request server public key
logger.debug("sha256: Requesting server public key")
self.write_packet(b'\1')
pkt = await self._read_packet()
pkt.check_error()
if pkt.is_extra_auth_data():
self.server_public_key = pkt._data[1:]
logger.debug(
"Received public key:\n",
self.server_public_key.decode('ascii')
)
if self._password:
if not self.server_public_key:
raise OperationalError("Couldn't receive server's public key")
data = _auth.sha2_rsa_encrypt(
self._password.encode('latin1'), self.salt,
self.server_public_key
)
else:
data = b''
self.write_packet(data)
pkt = await self._read_packet()
pkt.check_error()
return pkt
# _mysql support
def thread_id(self):
return self.server_thread_id[0]
def character_set_name(self):
return self._charset
def get_host_info(self):
return self.host_info
def get_proto_info(self):
return self.protocol_version
async def _get_server_information(self):
i = 0
packet = await self._read_packet()
data = packet.get_all_data()
# logger.debug(dump_packet(data))
self.protocol_version = byte2int(data[i:i + 1])
i += 1
server_end = data.find(b'\0', i)
self.server_version = data[i:server_end].decode('latin1')
i = server_end + 1
self.server_thread_id = struct.unpack('<I', data[i:i + 4])
i += 4
self.salt = data[i:i + 8]