Skip to content

Commit ff98b40

Browse files
chore: update MySQL notebook to use auto IAM AuthN (GoogleCloudPlatform#576)
1 parent bb4ab5d commit ff98b40

File tree

1 file changed

+5
-13
lines changed

1 file changed

+5
-13
lines changed

samples/notebooks/mysql_python_connector.ipynb

+5-13
Original file line numberDiff line numberDiff line change
@@ -584,9 +584,9 @@
584584
"source": [
585585
"### 🪪 IAM Database Authentication \n",
586586
"\n",
587-
"[Manual IAM database authentication](https://cloud.google.com/sql/docs/mysql/authentication#manual) is supported for **MySQL** Cloud SQL instances. \n",
587+
"[Automatic IAM database authentication](https://cloud.google.com/sql/docs/mysql/authentication#automatic) is supported for **MySQL** Cloud SQL instances. \n",
588588
"\n",
589-
"> 💡 This allows an IAM user to establish an authenticated connection to a MySQL database using an **OAuth2** token as their password in the connector's `connect` method.\n",
589+
"> 💡 This allows an IAM user to establish an authenticated connection to a MySQL database without having to set a password and enabling the `enable_iam_auth` parameter in the connector's `connect` method.\n",
590590
"\n",
591591
"> 🚨 If you are using a pre-existing Cloud SQL instance within this notebook you may need to [configure Cloud SQL instance to allow IAM authentication](https://cloud.google.com/sql/docs/mysql/create-edit-iam-instances#configuring_existing_instances_for) by setting the `cloudsql_iam_authentication` database flag to `On`. \n",
592592
"(Cloud SQL instances created within this notebook already have it enabled)\n"
@@ -663,28 +663,20 @@
663663
"import sqlalchemy\n",
664664
"\n",
665665
"# IAM database user parameter (IAM user's email before the \"@\" sign, mysql truncates usernames)\n",
666-
"# ex. IAM user with email \"[email protected]\" would have database user name \"demo-user\"\n",
666+
"# ex. IAM user with email \"[email protected]\" would have database username \"demo-user\"\n",
667667
"IAM_USER = current_user[0].split(\"@\")[0]\n",
668668
"\n",
669-
"# get application default credentials of IAM user (current logged in user)\n",
670-
"credentials, project = google.auth.default()\n",
671-
"\n",
672-
"# refresh credentials if expired\n",
673-
"if not credentials.valid:\n",
674-
" request = Request()\n",
675-
" credentials.refresh(request)\n",
676-
"\n",
677669
"# initialize connector\n",
678670
"connector = Connector()\n",
679671
"\n",
680-
"# getconn now using IAM user and OAuth2 token as password\n",
672+
"# getconn now using IAM user and requiring no password with IAM Auth enabled\n",
681673
"def getconn():\n",
682674
" conn = connector.connect(\n",
683675
" INSTANCE_CONNECTION_NAME,\n",
684676
" \"pymysql\",\n",
685677
" user=IAM_USER,\n",
686-
" password=credentials.token,\n",
687678
" db=\"\", # log in to instance but don't connect to specific database\n",
679+
" enable_iam_auth=True\n",
688680
" )\n",
689681
" return conn\n",
690682
"\n",

0 commit comments

Comments
 (0)