-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cloudsql): add MySQL Auto IAM AuthN sample (#8526)
- Loading branch information
1 parent
2be381f
commit bcb2b27
Showing
5 changed files
with
87 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
cloud-sql/mysql/sqlalchemy/connect_connector_auto_iam_authn.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# Copyright 2022 Google LLC | ||
# | ||
# 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. | ||
|
||
# [START cloud_sql_mysql_sqlalchemy_auto_iam_authn] | ||
import os | ||
|
||
from google.cloud.sql.connector import Connector, IPTypes | ||
import pymysql | ||
|
||
import sqlalchemy | ||
|
||
|
||
# connect_with_connector_auto_iam_authn initializes a connection pool for | ||
# a Cloud SQL instance of MySQL using the Cloud SQL Python Connector | ||
# with Automatic IAM Database Authentication. | ||
def connect_with_connector_auto_iam_authn() -> sqlalchemy.engine.base.Engine: | ||
# Note: Saving credentials in environment variables is convenient, but not | ||
# secure - consider a more secure solution such as | ||
# Cloud Secret Manager (https://cloud.google.com/secret-manager) to help | ||
# keep secrets safe. | ||
instance_connection_name = os.environ["INSTANCE_CONNECTION_NAME"] # e.g. 'project:region:instance' | ||
db_iam_user = os.environ["DB_IAM_USER"] # e.g. 'sa-name@project-id.iam' | ||
db_name = os.environ["DB_NAME"] # e.g. 'my-database' | ||
|
||
ip_type = IPTypes.PRIVATE if os.environ.get("PRIVATE_IP") else IPTypes.PUBLIC | ||
|
||
# initialize Cloud SQL Python Connector object | ||
connector = Connector() | ||
|
||
def getconn() -> pymysql.connections.Connection: | ||
conn: pymysql.connections.Connection = connector.connect( | ||
instance_connection_name, | ||
"pymysql", | ||
user=db_iam_user, | ||
db=db_name, | ||
enable_iam_auth=True, | ||
ip_type=ip_type, | ||
) | ||
return conn | ||
|
||
# The Cloud SQL Python Connector can be used with SQLAlchemy | ||
# using the 'creator' argument to 'create_engine' | ||
pool = sqlalchemy.create_engine( | ||
"mysql+pymysql://", | ||
creator=getconn, | ||
# [START_EXCLUDE] | ||
# Pool size is the maximum number of permanent connections to keep. | ||
pool_size=5, | ||
|
||
# Temporarily exceeds the set pool_size if no connections are available. | ||
max_overflow=2, | ||
|
||
# The total number of concurrent connections for your application will be | ||
# a total of pool_size and max_overflow. | ||
|
||
# 'pool_timeout' is the maximum number of seconds to wait when retrieving a | ||
# new connection from the pool. After the specified amount of time, an | ||
# exception will be thrown. | ||
pool_timeout=30, # 30 seconds | ||
|
||
# 'pool_recycle' is the maximum number of seconds a connection can persist. | ||
# Connections that live longer than the specified amount of time will be | ||
# re-established | ||
pool_recycle=1800, # 30 minutes | ||
# [END_EXCLUDE] | ||
) | ||
return pool | ||
|
||
# [END cloud_sql_mysql_sqlalchemy_auto_iam_authn] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
Flask==2.1.0 | ||
pg8000==1.24.2 | ||
SQLAlchemy==1.4.38 | ||
cloud-sql-python-connector==0.6.1 | ||
cloud-sql-python-connector==1.0.0 | ||
gunicorn==20.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters