-
Notifications
You must be signed in to change notification settings - Fork 2
/
MySQLInternalConnectionPoolPasswordWarning.py
64 lines (55 loc) · 2.93 KB
/
MySQLInternalConnectionPoolPasswordWarning.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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import mysql.connector
from aws_advanced_python_wrapper import AwsWrapperConnection
from aws_advanced_python_wrapper.connection_provider import \
ConnectionProviderManager
from aws_advanced_python_wrapper.sql_alchemy_connection_provider import \
SqlAlchemyPooledConnectionProvider
if __name__ == "__main__":
params = {
# In general, you should not use instance URLs to connect. However, we will use one here to simplify this
# example, because internal connection pools are only opened when connecting to an instance URL. Normally the
# internal connection pool would be opened when read_only is set instead of when you are initially connecting.
"host": "database-instance.xyz.us-east-1.rds.amazonaws.com",
"database": "mysql",
"user": "admin",
"plugins": "read_write_splitting,failover",
"autocommit": True
}
correct_password = "correct_password"
incorrect_password = "incorrect_password"
provider = SqlAlchemyPooledConnectionProvider()
ConnectionProviderManager.set_connection_provider(provider)
# Create an internal connection pool with the correct password
conn = AwsWrapperConnection.connect(mysql.connector.Connect, **params, password=correct_password)
# Finished with connection. The connection is not actually closed here, instead it will be returned to the pool but
# will remain open.
conn.close()
# Even though we use an incorrect password, the original connection 'conn' will be returned by the pool, and we can
# still use it.
with AwsWrapperConnection.connect(
mysql.connector.Connect, **params, password=incorrect_password) as incorrect_password_conn:
incorrect_password_conn.cursor().execute("SELECT 1")
# Closes all pools and removes all cached pool connections
ConnectionProviderManager.release_resources()
try:
# Correctly throws an exception - creates a fresh connection pool which will check the password because there
# are no longer any cached pool connections.
with AwsWrapperConnection.connect(
mysql.connector.Connect, **params, password=incorrect_password) as incorrect_password_conn:
# Will not reach - exception will be thrown
pass
except Exception:
print("Failed to connect - password was incorrect")