-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for MySQL auto IAM AuthN (#466)
- Loading branch information
1 parent
212f3a4
commit 80644d7
Showing
10 changed files
with
113 additions
and
26 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
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 |
---|---|---|
|
@@ -242,10 +242,16 @@ connector.connect( | |
Note: If specifying Private IP, your application must already be in the same VPC network as your Cloud SQL Instance. | ||
|
||
### IAM Authentication | ||
Connections using [Automatic IAM database authentication](https://cloud.google.com/sql/docs/postgres/authentication#automatic) are supported when using the Postgres driver. This feature is unsupported for other drivers. If automatic IAM authentication is not supported for your driver, you can use [Manual IAM database authentication](https://cloud.google.com/sql/docs/postgres/authentication#manual) to connect. | ||
First, make sure to [configure your Cloud SQL Instance to allow IAM authentication](https://cloud.google.com/sql/docs/postgres/create-edit-iam-instances#configure-iam-db-instance) and [add an IAM database user](https://cloud.google.com/sql/docs/postgres/create-manage-iam-users#creating-a-database-user). | ||
Connections using [Automatic IAM database authentication](https://cloud.google.com/sql/docs/postgres/authentication#automatic) are supported when using Postgres or MySQL drivers. | ||
First, make sure to [configure your Cloud SQL Instance to allow IAM authentication](https://cloud.google.com/sql/docs/postgres/create-edit-iam-instances#configure-iam-db-instance) | ||
and [add an IAM database user](https://cloud.google.com/sql/docs/postgres/create-manage-iam-users#creating-a-database-user). | ||
|
||
Now, you can connect using user or service account credentials instead of a password. | ||
In the call to connect, set the `enable_iam_auth` keyword argument to true and `user` to the email address associated with your IAM user. | ||
In the call to connect, set the `enable_iam_auth` keyword argument to true and the `user` argument to the appropriately formatted IAM principal. | ||
> Postgres: For an IAM user account, this is the user's email address. For a service account, it is the service account's email without the `.gserviceaccount.com` domain suffix. | ||
> MySQL: For an IAM user account, this is the user's email address, without the @ or domain name. For example, for `[email protected]`, set the `user` argument to `test-user`. For a service account, this is the service account's email address without the `@project-id.iam.gserviceaccount.com` suffix. | ||
Example: | ||
```python | ||
connector.connect( | ||
|
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
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
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
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,86 @@ | ||
""" | ||
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 | ||
https://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 os | ||
import uuid | ||
from typing import Generator | ||
|
||
import pytest | ||
import pymysql | ||
import sqlalchemy | ||
from google.cloud.sql.connector import Connector | ||
|
||
table_name = f"books_{uuid.uuid4().hex}" | ||
|
||
|
||
# [START cloud_sql_connector_mysql_pymysql_iam_auth] | ||
# The Cloud SQL Python Connector can be used along with SQLAlchemy using the | ||
# 'creator' argument to 'create_engine' | ||
def init_connection_engine() -> sqlalchemy.engine.Engine: | ||
def getconn() -> pymysql.connections.Connection: | ||
# initialize Connector object for connections to Cloud SQL | ||
with Connector() as connector: | ||
conn: pymysql.connections.Connection = connector.connect( | ||
os.environ["MYSQL_IAM_CONNECTION_NAME"], | ||
"pymysql", | ||
user=os.environ["MYSQL_IAM_USER"], | ||
db=os.environ["MYSQL_DB"], | ||
enable_iam_auth=True, | ||
) | ||
return conn | ||
|
||
# create SQLAlchemy connection pool | ||
pool = sqlalchemy.create_engine( | ||
"mysql+pymysql://", | ||
creator=getconn, | ||
) | ||
return pool | ||
|
||
|
||
# [END cloud_sql_connector_mysql_pymysql_iam_auth] | ||
|
||
|
||
@pytest.fixture(name="pool") | ||
def setup() -> Generator: | ||
pool = init_connection_engine() | ||
|
||
with pool.connect() as conn: | ||
conn.execute( | ||
f"CREATE TABLE IF NOT EXISTS {table_name}" | ||
" ( id CHAR(20) NOT NULL, title TEXT NOT NULL );" | ||
) | ||
|
||
yield pool | ||
|
||
with pool.connect() as conn: | ||
conn.execute(f"DROP TABLE IF EXISTS {table_name}") | ||
|
||
|
||
def test_pooled_connection_with_pymysql_iam_auth( | ||
pool: sqlalchemy.engine.Engine, | ||
) -> None: | ||
insert_stmt = sqlalchemy.text( | ||
f"INSERT INTO {table_name} (id, title) VALUES (:id, :title)", | ||
) | ||
with pool.connect() as conn: | ||
conn.execute(insert_stmt, id="book1", title="Book One") | ||
conn.execute(insert_stmt, id="book2", title="Book Two") | ||
|
||
select_stmt = sqlalchemy.text(f"SELECT title FROM {table_name} ORDER BY ID;") | ||
with pool.connect() as conn: | ||
rows = conn.execute(select_stmt).fetchall() | ||
titles = [row[0] for row in rows] | ||
|
||
assert titles == ["Book One", "Book Two"] |
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