pyoceanbase 1.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyoceanbase-1.0.1/CHANGELOG.md +16 -0
- pyoceanbase-1.0.1/LICENSE +19 -0
- pyoceanbase-1.0.1/MANIFEST.in +1 -0
- pyoceanbase-1.0.1/PKG-INFO +73 -0
- pyoceanbase-1.0.1/README.md +50 -0
- pyoceanbase-1.0.1/pyoceanbase/__init__.py +183 -0
- pyoceanbase-1.0.1/pyoceanbase/_auth.py +272 -0
- pyoceanbase-1.0.1/pyoceanbase/charset.py +217 -0
- pyoceanbase-1.0.1/pyoceanbase/connections.py +1477 -0
- pyoceanbase-1.0.1/pyoceanbase/constants/CLIENT.py +47 -0
- pyoceanbase-1.0.1/pyoceanbase/constants/COMMAND.py +32 -0
- pyoceanbase-1.0.1/pyoceanbase/constants/CR.py +79 -0
- pyoceanbase-1.0.1/pyoceanbase/constants/ER.py +477 -0
- pyoceanbase-1.0.1/pyoceanbase/constants/FIELD_TYPE.py +31 -0
- pyoceanbase-1.0.1/pyoceanbase/constants/FLAG.py +15 -0
- pyoceanbase-1.0.1/pyoceanbase/constants/SERVER_STATUS.py +10 -0
- pyoceanbase-1.0.1/pyoceanbase/constants/__init__.py +0 -0
- pyoceanbase-1.0.1/pyoceanbase/converters.py +365 -0
- pyoceanbase-1.0.1/pyoceanbase/cursors.py +515 -0
- pyoceanbase-1.0.1/pyoceanbase/err.py +154 -0
- pyoceanbase-1.0.1/pyoceanbase/optionfile.py +21 -0
- pyoceanbase-1.0.1/pyoceanbase/protocol.py +356 -0
- pyoceanbase-1.0.1/pyoceanbase/times.py +20 -0
- pyoceanbase-1.0.1/pyoceanbase.egg-info/PKG-INFO +73 -0
- pyoceanbase-1.0.1/pyoceanbase.egg-info/SOURCES.txt +30 -0
- pyoceanbase-1.0.1/pyoceanbase.egg-info/dependency_links.txt +1 -0
- pyoceanbase-1.0.1/pyoceanbase.egg-info/requires.txt +6 -0
- pyoceanbase-1.0.1/pyoceanbase.egg-info/top_level.txt +1 -0
- pyoceanbase-1.0.1/pyproject.toml +58 -0
- pyoceanbase-1.0.1/setup.cfg +4 -0
- pyoceanbase-1.0.1/tests/test_auth.py +120 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## v1.0.1
|
|
4
|
+
|
|
5
|
+
* 分发包改名 `pyoceanbase`,顶层 import 包同步改为 `pyoceanbase`,
|
|
6
|
+
可与上游 PyMySQL 共存。
|
|
7
|
+
* 客户端标识改为 `pyoceanbase`。
|
|
8
|
+
|
|
9
|
+
## v1.0.0
|
|
10
|
+
|
|
11
|
+
* 首版。基线:上游 `PyMySQL 1.2.0`。
|
|
12
|
+
* 默认置上握手包 capability bit27(`QUERY_ATTRIBUTES`),
|
|
13
|
+
OceanBase Oracle 租户可直接用 MySQL 协议登录;否则服务端报
|
|
14
|
+
`Error 1235: Oracle tenant for current client driver is not supported`。
|
|
15
|
+
* 已在 OceanBase 4.2.1.11 Oracle 租户验证:连接、查询、绑定变量、
|
|
16
|
+
建表写入、事务提交回滚。
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2010, 2013 PyMySQL contributors
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
THE SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
include README.md LICENSE CHANGELOG.md
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pyoceanbase
|
|
3
|
+
Version: 1.0.1
|
|
4
|
+
Summary: Pure Python OceanBase Driver (PyMySQL fork with Oracle tenant support)
|
|
5
|
+
Author-email: Inada Naoki <songofacandy@gmail.com>, Yutaka Matsubara <yutaka.matsubara@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Project, https://github.com/oceanbase-driver/python-oceanbase-driver
|
|
8
|
+
Project-URL: Changelog, https://github.com/oceanbase-driver/python-oceanbase-driver/blob/master/CHANGELOG.md
|
|
9
|
+
Keywords: MySQL
|
|
10
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
11
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
12
|
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Topic :: Database
|
|
15
|
+
Requires-Python: >=3.9
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Provides-Extra: rsa
|
|
19
|
+
Requires-Dist: cryptography>=46.0.7; extra == "rsa"
|
|
20
|
+
Provides-Extra: ed25519
|
|
21
|
+
Requires-Dist: PyNaCl>=1.6.2; extra == "ed25519"
|
|
22
|
+
Dynamic: license-file
|
|
23
|
+
|
|
24
|
+
# python-oceanbase-driver
|
|
25
|
+
|
|
26
|
+
Python 的 OceanBase 驱动,基于 `PyMySQL 1.2.0`,纯 Python,用法与 PyMySQL 完全一致,
|
|
27
|
+
区别是默认置上握手包 capability bit27,可直连 OceanBase **Oracle 租户**。
|
|
28
|
+
未打补丁的 MySQL 协议驱动连 Oracle 租户会被服务端拒绝:
|
|
29
|
+
|
|
30
|
+
```text
|
|
31
|
+
pyoceanbase.err.NotSupportedError: (1235, 'Oracle tenant for current client driver is not supported')
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
原理:OceanBase 登录时检查该位(`OB_CLIENT_SUPPORT_ORACLE_MODE`,
|
|
35
|
+
与 MySQL 8.0 的 `CLIENT_QUERY_ATTRIBUTES` 同一位),mysql 8.0 客户端默认置位,
|
|
36
|
+
PyMySQL 默认没置。本驱动默认置上,对 MySQL / MariaDB / OB MySQL 租户无副作用。
|
|
37
|
+
|
|
38
|
+
## 安装
|
|
39
|
+
|
|
40
|
+
```shell
|
|
41
|
+
pip install pyoceanbase
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
注意:顶层包名为 `pyoceanbase`,可与上游 PyMySQL 共存于同一环境,按需选用。
|
|
45
|
+
|
|
46
|
+
## 用法
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
import pyoceanbase
|
|
50
|
+
|
|
51
|
+
conn = pyoceanbase.connect(
|
|
52
|
+
host="HOST",
|
|
53
|
+
port=9090,
|
|
54
|
+
user="USER@TENANT#CLUSTER",
|
|
55
|
+
password="PASSWORD",
|
|
56
|
+
database="DBNAME",
|
|
57
|
+
)
|
|
58
|
+
with conn.cursor() as cur:
|
|
59
|
+
cur.execute("SELECT USER FROM DUAL")
|
|
60
|
+
print(cur.fetchone())
|
|
61
|
+
conn.close()
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## 注意事项
|
|
65
|
+
|
|
66
|
+
1. 业务 SQL 须用 Oracle 方言(如 `FROM DUAL`、`SYSDATE`)。
|
|
67
|
+
2. 默认 `autocommit=False`,写入后记得 `conn.commit()`(标准 PyMySQL 行为)。
|
|
68
|
+
3. 数字类型默认返回 `Decimal`(标准 PyMySQL 行为),可用 `conv` 覆盖。
|
|
69
|
+
4. DSN 参数等细节见上游文档:https://github.com/PyMySQL/PyMySQL
|
|
70
|
+
|
|
71
|
+
## 协议
|
|
72
|
+
|
|
73
|
+
MIT,与上游一致。
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# python-oceanbase-driver
|
|
2
|
+
|
|
3
|
+
Python 的 OceanBase 驱动,基于 `PyMySQL 1.2.0`,纯 Python,用法与 PyMySQL 完全一致,
|
|
4
|
+
区别是默认置上握手包 capability bit27,可直连 OceanBase **Oracle 租户**。
|
|
5
|
+
未打补丁的 MySQL 协议驱动连 Oracle 租户会被服务端拒绝:
|
|
6
|
+
|
|
7
|
+
```text
|
|
8
|
+
pyoceanbase.err.NotSupportedError: (1235, 'Oracle tenant for current client driver is not supported')
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
原理:OceanBase 登录时检查该位(`OB_CLIENT_SUPPORT_ORACLE_MODE`,
|
|
12
|
+
与 MySQL 8.0 的 `CLIENT_QUERY_ATTRIBUTES` 同一位),mysql 8.0 客户端默认置位,
|
|
13
|
+
PyMySQL 默认没置。本驱动默认置上,对 MySQL / MariaDB / OB MySQL 租户无副作用。
|
|
14
|
+
|
|
15
|
+
## 安装
|
|
16
|
+
|
|
17
|
+
```shell
|
|
18
|
+
pip install pyoceanbase
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
注意:顶层包名为 `pyoceanbase`,可与上游 PyMySQL 共存于同一环境,按需选用。
|
|
22
|
+
|
|
23
|
+
## 用法
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
import pyoceanbase
|
|
27
|
+
|
|
28
|
+
conn = pyoceanbase.connect(
|
|
29
|
+
host="HOST",
|
|
30
|
+
port=9090,
|
|
31
|
+
user="USER@TENANT#CLUSTER",
|
|
32
|
+
password="PASSWORD",
|
|
33
|
+
database="DBNAME",
|
|
34
|
+
)
|
|
35
|
+
with conn.cursor() as cur:
|
|
36
|
+
cur.execute("SELECT USER FROM DUAL")
|
|
37
|
+
print(cur.fetchone())
|
|
38
|
+
conn.close()
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## 注意事项
|
|
42
|
+
|
|
43
|
+
1. 业务 SQL 须用 Oracle 方言(如 `FROM DUAL`、`SYSDATE`)。
|
|
44
|
+
2. 默认 `autocommit=False`,写入后记得 `conn.commit()`(标准 PyMySQL 行为)。
|
|
45
|
+
3. 数字类型默认返回 `Decimal`(标准 PyMySQL 行为),可用 `conv` 覆盖。
|
|
46
|
+
4. DSN 参数等细节见上游文档:https://github.com/PyMySQL/PyMySQL
|
|
47
|
+
|
|
48
|
+
## 协议
|
|
49
|
+
|
|
50
|
+
MIT,与上游一致。
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
"""
|
|
2
|
+
PyMySQL: A pure-Python MySQL client library.
|
|
3
|
+
|
|
4
|
+
Copyright (c) 2010-2016 PyMySQL contributors
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in
|
|
14
|
+
all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
22
|
+
THE SOFTWARE.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
import sys
|
|
26
|
+
|
|
27
|
+
from .constants import FIELD_TYPE
|
|
28
|
+
from .err import (
|
|
29
|
+
Warning,
|
|
30
|
+
Error,
|
|
31
|
+
InterfaceError,
|
|
32
|
+
DataError,
|
|
33
|
+
DatabaseError,
|
|
34
|
+
OperationalError,
|
|
35
|
+
IntegrityError,
|
|
36
|
+
InternalError,
|
|
37
|
+
NotSupportedError,
|
|
38
|
+
ProgrammingError,
|
|
39
|
+
MySQLError,
|
|
40
|
+
)
|
|
41
|
+
from .times import (
|
|
42
|
+
Date,
|
|
43
|
+
Time,
|
|
44
|
+
Timestamp,
|
|
45
|
+
DateFromTicks,
|
|
46
|
+
TimeFromTicks,
|
|
47
|
+
TimestampFromTicks,
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
# Fork version (upstream PyMySQL base: 1.2.0).
|
|
51
|
+
# Used by setuptools and connection_attrs
|
|
52
|
+
VERSION = (1, 0, 1, "final")
|
|
53
|
+
VERSION_STRING = "1.0.1"
|
|
54
|
+
|
|
55
|
+
### for mysqlclient compatibility
|
|
56
|
+
### Django checks mysqlclient version.
|
|
57
|
+
version_info = (2, 2, 8, "final", 1)
|
|
58
|
+
__version__ = "2.2.8"
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def get_client_info(): # for MySQLdb compatibility
|
|
62
|
+
return __version__
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def install_as_MySQLdb():
|
|
66
|
+
"""
|
|
67
|
+
After this function is called, any application that imports MySQLdb
|
|
68
|
+
will unwittingly actually use pyoceanbase.
|
|
69
|
+
"""
|
|
70
|
+
sys.modules["MySQLdb"] = sys.modules["pyoceanbase"]
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
# end of mysqlclient compatibility code
|
|
74
|
+
|
|
75
|
+
threadsafety = 1
|
|
76
|
+
apilevel = "2.0"
|
|
77
|
+
paramstyle = "pyformat"
|
|
78
|
+
|
|
79
|
+
from . import connections # noqa: E402
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
class DBAPISet(frozenset):
|
|
83
|
+
def __ne__(self, other):
|
|
84
|
+
if isinstance(other, set):
|
|
85
|
+
return frozenset.__ne__(self, other)
|
|
86
|
+
else:
|
|
87
|
+
return other not in self
|
|
88
|
+
|
|
89
|
+
def __eq__(self, other):
|
|
90
|
+
if isinstance(other, frozenset):
|
|
91
|
+
return frozenset.__eq__(self, other)
|
|
92
|
+
else:
|
|
93
|
+
return other in self
|
|
94
|
+
|
|
95
|
+
def __hash__(self):
|
|
96
|
+
return frozenset.__hash__(self)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
STRING = DBAPISet([FIELD_TYPE.ENUM, FIELD_TYPE.STRING, FIELD_TYPE.VAR_STRING])
|
|
100
|
+
BINARY = DBAPISet(
|
|
101
|
+
[
|
|
102
|
+
FIELD_TYPE.BLOB,
|
|
103
|
+
FIELD_TYPE.LONG_BLOB,
|
|
104
|
+
FIELD_TYPE.MEDIUM_BLOB,
|
|
105
|
+
FIELD_TYPE.TINY_BLOB,
|
|
106
|
+
]
|
|
107
|
+
)
|
|
108
|
+
NUMBER = DBAPISet(
|
|
109
|
+
[
|
|
110
|
+
FIELD_TYPE.DECIMAL,
|
|
111
|
+
FIELD_TYPE.DOUBLE,
|
|
112
|
+
FIELD_TYPE.FLOAT,
|
|
113
|
+
FIELD_TYPE.INT24,
|
|
114
|
+
FIELD_TYPE.LONG,
|
|
115
|
+
FIELD_TYPE.LONGLONG,
|
|
116
|
+
FIELD_TYPE.TINY,
|
|
117
|
+
FIELD_TYPE.YEAR,
|
|
118
|
+
]
|
|
119
|
+
)
|
|
120
|
+
DATE = DBAPISet([FIELD_TYPE.DATE, FIELD_TYPE.NEWDATE])
|
|
121
|
+
TIME = DBAPISet([FIELD_TYPE.TIME])
|
|
122
|
+
TIMESTAMP = DBAPISet([FIELD_TYPE.TIMESTAMP, FIELD_TYPE.DATETIME])
|
|
123
|
+
DATETIME = TIMESTAMP
|
|
124
|
+
ROWID = DBAPISet()
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def Binary(x):
|
|
128
|
+
"""Return x as a binary type."""
|
|
129
|
+
return bytes(x)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def thread_safe():
|
|
133
|
+
return True # match MySQLdb.thread_safe()
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
Connect = connect = Connection = connections.Connection
|
|
137
|
+
NULL = "NULL"
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
__all__ = [
|
|
141
|
+
"BINARY",
|
|
142
|
+
"Binary",
|
|
143
|
+
"Connect",
|
|
144
|
+
"Connection",
|
|
145
|
+
"DATE",
|
|
146
|
+
"Date",
|
|
147
|
+
"Time",
|
|
148
|
+
"Timestamp",
|
|
149
|
+
"DateFromTicks",
|
|
150
|
+
"TimeFromTicks",
|
|
151
|
+
"TimestampFromTicks",
|
|
152
|
+
"DataError",
|
|
153
|
+
"DatabaseError",
|
|
154
|
+
"Error",
|
|
155
|
+
"FIELD_TYPE",
|
|
156
|
+
"IntegrityError",
|
|
157
|
+
"InterfaceError",
|
|
158
|
+
"InternalError",
|
|
159
|
+
"MySQLError",
|
|
160
|
+
"NULL",
|
|
161
|
+
"NUMBER",
|
|
162
|
+
"NotSupportedError",
|
|
163
|
+
"DBAPISet",
|
|
164
|
+
"OperationalError",
|
|
165
|
+
"ProgrammingError",
|
|
166
|
+
"ROWID",
|
|
167
|
+
"STRING",
|
|
168
|
+
"TIME",
|
|
169
|
+
"TIMESTAMP",
|
|
170
|
+
"Warning",
|
|
171
|
+
"apilevel",
|
|
172
|
+
"connect",
|
|
173
|
+
"connections",
|
|
174
|
+
"constants",
|
|
175
|
+
"converters",
|
|
176
|
+
"cursors",
|
|
177
|
+
"get_client_info",
|
|
178
|
+
"paramstyle",
|
|
179
|
+
"threadsafety",
|
|
180
|
+
"version_info",
|
|
181
|
+
"install_as_MySQLdb",
|
|
182
|
+
"__version__",
|
|
183
|
+
]
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Implements auth methods
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from .err import OperationalError
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
try:
|
|
9
|
+
from cryptography.hazmat.backends import default_backend
|
|
10
|
+
from cryptography.hazmat.primitives import serialization, hashes
|
|
11
|
+
from cryptography.hazmat.primitives.asymmetric import padding
|
|
12
|
+
|
|
13
|
+
_have_cryptography = True
|
|
14
|
+
except ImportError:
|
|
15
|
+
_have_cryptography = False
|
|
16
|
+
|
|
17
|
+
from functools import partial
|
|
18
|
+
import hashlib
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
DEBUG = False
|
|
22
|
+
SCRAMBLE_LENGTH = 20
|
|
23
|
+
sha1_new = partial(hashlib.new, "sha1")
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
# mysql_native_password
|
|
27
|
+
# https://dev.mysql.com/doc/internals/en/secure-password-authentication.html#packet-Authentication::Native41
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def scramble_native_password(password, message):
|
|
31
|
+
"""Scramble used for mysql_native_password"""
|
|
32
|
+
if not password:
|
|
33
|
+
return b""
|
|
34
|
+
|
|
35
|
+
stage1 = sha1_new(password).digest()
|
|
36
|
+
stage2 = sha1_new(stage1).digest()
|
|
37
|
+
s = sha1_new()
|
|
38
|
+
s.update(message[:SCRAMBLE_LENGTH])
|
|
39
|
+
s.update(stage2)
|
|
40
|
+
result = s.digest()
|
|
41
|
+
return _my_crypt(result, stage1)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _my_crypt(message1, message2):
|
|
45
|
+
result = bytearray(message1)
|
|
46
|
+
|
|
47
|
+
for i in range(len(result)):
|
|
48
|
+
result[i] ^= message2[i]
|
|
49
|
+
|
|
50
|
+
return bytes(result)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
# MariaDB's client_ed25519-plugin
|
|
54
|
+
# https://mariadb.com/kb/en/library/connection/#client_ed25519-plugin
|
|
55
|
+
|
|
56
|
+
_nacl_bindings = False
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _init_nacl():
|
|
60
|
+
global _nacl_bindings
|
|
61
|
+
try:
|
|
62
|
+
from nacl import bindings
|
|
63
|
+
|
|
64
|
+
_nacl_bindings = bindings
|
|
65
|
+
except ImportError:
|
|
66
|
+
raise RuntimeError(
|
|
67
|
+
"'pynacl' package is required for ed25519_password auth method"
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _scalar_clamp(s32):
|
|
72
|
+
ba = bytearray(s32)
|
|
73
|
+
ba0 = bytes(bytearray([ba[0] & 248]))
|
|
74
|
+
ba31 = bytes(bytearray([(ba[31] & 127) | 64]))
|
|
75
|
+
return ba0 + bytes(s32[1:31]) + ba31
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def ed25519_password(password, scramble):
|
|
79
|
+
"""Sign a random scramble with elliptic curve Ed25519.
|
|
80
|
+
|
|
81
|
+
Secret and public key are derived from password.
|
|
82
|
+
"""
|
|
83
|
+
# variable names based on rfc8032 section-5.1.6
|
|
84
|
+
#
|
|
85
|
+
if not _nacl_bindings:
|
|
86
|
+
_init_nacl()
|
|
87
|
+
|
|
88
|
+
# h = SHA512(password)
|
|
89
|
+
h = hashlib.sha512(password).digest()
|
|
90
|
+
|
|
91
|
+
# s = prune(first_half(h))
|
|
92
|
+
s = _scalar_clamp(h[:32])
|
|
93
|
+
|
|
94
|
+
# r = SHA512(second_half(h) || M)
|
|
95
|
+
r = hashlib.sha512(h[32:] + scramble).digest()
|
|
96
|
+
|
|
97
|
+
# R = encoded point [r]B
|
|
98
|
+
r = _nacl_bindings.crypto_core_ed25519_scalar_reduce(r)
|
|
99
|
+
R = _nacl_bindings.crypto_scalarmult_ed25519_base_noclamp(r)
|
|
100
|
+
|
|
101
|
+
# A = encoded point [s]B
|
|
102
|
+
A = _nacl_bindings.crypto_scalarmult_ed25519_base_noclamp(s)
|
|
103
|
+
|
|
104
|
+
# k = SHA512(R || A || M)
|
|
105
|
+
k = hashlib.sha512(R + A + scramble).digest()
|
|
106
|
+
|
|
107
|
+
# S = (k * s + r) mod L
|
|
108
|
+
k = _nacl_bindings.crypto_core_ed25519_scalar_reduce(k)
|
|
109
|
+
ks = _nacl_bindings.crypto_core_ed25519_scalar_mul(k, s)
|
|
110
|
+
S = _nacl_bindings.crypto_core_ed25519_scalar_add(ks, r)
|
|
111
|
+
|
|
112
|
+
# signature = R || S
|
|
113
|
+
return R + S
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
# sha256_password
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def _roundtrip(conn, send_data):
|
|
120
|
+
conn.write_packet(send_data)
|
|
121
|
+
pkt = conn._read_packet()
|
|
122
|
+
pkt.check_error()
|
|
123
|
+
return pkt
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def _xor_password(password, salt):
|
|
127
|
+
# Trailing NUL character will be added in Auth Switch Request.
|
|
128
|
+
# See https://github.com/mysql/mysql-server/blob/7d10c82196c8e45554f27c00681474a9fb86d137/sql/auth/sha2_password.cc#L939-L945
|
|
129
|
+
salt = salt[:SCRAMBLE_LENGTH]
|
|
130
|
+
password_bytes = bytearray(password)
|
|
131
|
+
# salt = bytearray(salt) # for PY2 compat.
|
|
132
|
+
salt_len = len(salt)
|
|
133
|
+
for i in range(len(password_bytes)):
|
|
134
|
+
password_bytes[i] ^= salt[i % salt_len]
|
|
135
|
+
return bytes(password_bytes)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def sha2_rsa_encrypt(password, salt, public_key):
|
|
139
|
+
"""Encrypt password with salt and public_key.
|
|
140
|
+
|
|
141
|
+
Used for sha256_password and caching_sha2_password.
|
|
142
|
+
"""
|
|
143
|
+
if not _have_cryptography:
|
|
144
|
+
raise RuntimeError(
|
|
145
|
+
"'cryptography' package is required for sha256_password or"
|
|
146
|
+
+ " caching_sha2_password auth methods"
|
|
147
|
+
)
|
|
148
|
+
message = _xor_password(password + b"\0", salt)
|
|
149
|
+
rsa_key = serialization.load_pem_public_key(public_key, default_backend())
|
|
150
|
+
return rsa_key.encrypt(
|
|
151
|
+
message,
|
|
152
|
+
padding.OAEP(
|
|
153
|
+
mgf=padding.MGF1(algorithm=hashes.SHA1()),
|
|
154
|
+
algorithm=hashes.SHA1(),
|
|
155
|
+
label=None,
|
|
156
|
+
),
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def sha256_password_auth(conn, pkt):
|
|
161
|
+
if conn._secure:
|
|
162
|
+
if DEBUG:
|
|
163
|
+
print("sha256: Sending plain password")
|
|
164
|
+
data = conn.password + b"\0"
|
|
165
|
+
return _roundtrip(conn, data)
|
|
166
|
+
|
|
167
|
+
if pkt.is_auth_switch_request():
|
|
168
|
+
conn.salt = pkt.read_all()
|
|
169
|
+
if conn.salt.endswith(b"\0"):
|
|
170
|
+
conn.salt = conn.salt[:-1]
|
|
171
|
+
if not conn.server_public_key and conn.password:
|
|
172
|
+
# Request server public key
|
|
173
|
+
if DEBUG:
|
|
174
|
+
print("sha256: Requesting server public key")
|
|
175
|
+
pkt = _roundtrip(conn, b"\1")
|
|
176
|
+
|
|
177
|
+
if pkt.is_extra_auth_data():
|
|
178
|
+
conn.server_public_key = pkt._data[1:]
|
|
179
|
+
if DEBUG:
|
|
180
|
+
print("Received public key:\n", conn.server_public_key.decode("ascii"))
|
|
181
|
+
|
|
182
|
+
if conn.password:
|
|
183
|
+
if not conn.server_public_key:
|
|
184
|
+
raise OperationalError("Couldn't receive server's public key")
|
|
185
|
+
|
|
186
|
+
data = sha2_rsa_encrypt(conn.password, conn.salt, conn.server_public_key)
|
|
187
|
+
else:
|
|
188
|
+
data = b""
|
|
189
|
+
|
|
190
|
+
return _roundtrip(conn, data)
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def scramble_caching_sha2(password, nonce):
|
|
194
|
+
# (bytes, bytes) -> bytes
|
|
195
|
+
"""Scramble algorithm used in cached_sha2_password fast path.
|
|
196
|
+
|
|
197
|
+
XOR(SHA256(password), SHA256(SHA256(SHA256(password)), nonce))
|
|
198
|
+
"""
|
|
199
|
+
if not password:
|
|
200
|
+
return b""
|
|
201
|
+
|
|
202
|
+
p1 = hashlib.sha256(password).digest()
|
|
203
|
+
p2 = hashlib.sha256(p1).digest()
|
|
204
|
+
p3 = hashlib.sha256(p2 + nonce).digest()
|
|
205
|
+
|
|
206
|
+
res = bytearray(p1)
|
|
207
|
+
for i in range(len(p3)):
|
|
208
|
+
res[i] ^= p3[i]
|
|
209
|
+
|
|
210
|
+
return bytes(res)
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def caching_sha2_password_auth(conn, pkt):
|
|
214
|
+
# No password fast path
|
|
215
|
+
if not conn.password:
|
|
216
|
+
return _roundtrip(conn, b"")
|
|
217
|
+
|
|
218
|
+
if pkt.is_auth_switch_request():
|
|
219
|
+
# Try from fast auth
|
|
220
|
+
conn.salt = pkt.read_all()
|
|
221
|
+
if conn.salt.endswith(b"\0"): # str.removesuffix is available in 3.9
|
|
222
|
+
conn.salt = conn.salt[:-1]
|
|
223
|
+
if DEBUG:
|
|
224
|
+
print(f"caching sha2: Trying fast path. salt={conn.salt.hex()!r}")
|
|
225
|
+
scrambled = scramble_caching_sha2(conn.password, conn.salt)
|
|
226
|
+
pkt = _roundtrip(conn, scrambled)
|
|
227
|
+
# else: fast auth is tried in initial handshake
|
|
228
|
+
|
|
229
|
+
if not pkt.is_extra_auth_data():
|
|
230
|
+
raise OperationalError(
|
|
231
|
+
"caching sha2: Unknown packet for fast auth: %s" % pkt._data[:1]
|
|
232
|
+
)
|
|
233
|
+
|
|
234
|
+
# magic numbers:
|
|
235
|
+
# 2 - request public key
|
|
236
|
+
# 3 - fast auth succeeded
|
|
237
|
+
# 4 - need full auth
|
|
238
|
+
|
|
239
|
+
pkt.advance(1)
|
|
240
|
+
n = pkt.read_uint8()
|
|
241
|
+
|
|
242
|
+
if n == 3:
|
|
243
|
+
if DEBUG:
|
|
244
|
+
print("caching sha2: succeeded by fast path.")
|
|
245
|
+
pkt = conn._read_packet()
|
|
246
|
+
pkt.check_error() # pkt must be OK packet
|
|
247
|
+
return pkt
|
|
248
|
+
|
|
249
|
+
if n != 4:
|
|
250
|
+
raise OperationalError("caching sha2: Unknown result for fast auth: %s" % n)
|
|
251
|
+
|
|
252
|
+
if DEBUG:
|
|
253
|
+
print("caching sha2: Trying full auth...")
|
|
254
|
+
|
|
255
|
+
if conn._secure:
|
|
256
|
+
if DEBUG:
|
|
257
|
+
print("caching sha2: Sending plain password via secure connection")
|
|
258
|
+
return _roundtrip(conn, conn.password + b"\0")
|
|
259
|
+
|
|
260
|
+
if not conn.server_public_key:
|
|
261
|
+
pkt = _roundtrip(conn, b"\x02") # Request public key
|
|
262
|
+
if not pkt.is_extra_auth_data():
|
|
263
|
+
raise OperationalError(
|
|
264
|
+
"caching sha2: Unknown packet for public key: %s" % pkt._data[:1]
|
|
265
|
+
)
|
|
266
|
+
|
|
267
|
+
conn.server_public_key = pkt._data[1:]
|
|
268
|
+
if DEBUG:
|
|
269
|
+
print(conn.server_public_key.decode("ascii"))
|
|
270
|
+
|
|
271
|
+
data = sha2_rsa_encrypt(conn.password, conn.salt, conn.server_public_key)
|
|
272
|
+
pkt = _roundtrip(conn, data)
|