cloud-sql-python-connector 1.9.1__tar.gz → 1.10.0__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.
Files changed (29) hide show
  1. {cloud-sql-python-connector-1.9.1 → cloud-sql-python-connector-1.10.0}/PKG-INFO +75 -13
  2. {cloud-sql-python-connector-1.9.1 → cloud-sql-python-connector-1.10.0}/README.md +74 -12
  3. {cloud-sql-python-connector-1.9.1 → cloud-sql-python-connector-1.10.0}/cloud_sql_python_connector.egg-info/PKG-INFO +75 -13
  4. {cloud-sql-python-connector-1.9.1 → cloud-sql-python-connector-1.10.0}/cloud_sql_python_connector.egg-info/SOURCES.txt +3 -0
  5. {cloud-sql-python-connector-1.9.1 → cloud-sql-python-connector-1.10.0}/google/cloud/sql/connector/__init__.py +9 -2
  6. {cloud-sql-python-connector-1.9.1 → cloud-sql-python-connector-1.10.0}/google/cloud/sql/connector/client.py +83 -13
  7. cloud-sql-python-connector-1.10.0/google/cloud/sql/connector/connection_info.py +105 -0
  8. {cloud-sql-python-connector-1.9.1 → cloud-sql-python-connector-1.10.0}/google/cloud/sql/connector/connector.py +81 -29
  9. cloud-sql-python-connector-1.10.0/google/cloud/sql/connector/enums.py +85 -0
  10. {cloud-sql-python-connector-1.9.1 → cloud-sql-python-connector-1.10.0}/google/cloud/sql/connector/exceptions.py +15 -0
  11. cloud-sql-python-connector-1.10.0/google/cloud/sql/connector/instance.py +251 -0
  12. cloud-sql-python-connector-1.10.0/google/cloud/sql/connector/lazy.py +132 -0
  13. {cloud-sql-python-connector-1.9.1 → cloud-sql-python-connector-1.10.0}/google/cloud/sql/connector/version.py +1 -1
  14. cloud-sql-python-connector-1.9.1/google/cloud/sql/connector/instance.py +0 -390
  15. {cloud-sql-python-connector-1.9.1 → cloud-sql-python-connector-1.10.0}/LICENSE +0 -0
  16. {cloud-sql-python-connector-1.9.1 → cloud-sql-python-connector-1.10.0}/cloud_sql_python_connector.egg-info/dependency_links.txt +0 -0
  17. {cloud-sql-python-connector-1.9.1 → cloud-sql-python-connector-1.10.0}/cloud_sql_python_connector.egg-info/not-zip-safe +0 -0
  18. {cloud-sql-python-connector-1.9.1 → cloud-sql-python-connector-1.10.0}/cloud_sql_python_connector.egg-info/requires.txt +0 -0
  19. {cloud-sql-python-connector-1.9.1 → cloud-sql-python-connector-1.10.0}/cloud_sql_python_connector.egg-info/top_level.txt +0 -0
  20. {cloud-sql-python-connector-1.9.1 → cloud-sql-python-connector-1.10.0}/google/cloud/sql/connector/asyncpg.py +0 -0
  21. {cloud-sql-python-connector-1.9.1 → cloud-sql-python-connector-1.10.0}/google/cloud/sql/connector/pg8000.py +0 -0
  22. {cloud-sql-python-connector-1.9.1 → cloud-sql-python-connector-1.10.0}/google/cloud/sql/connector/py.typed +0 -0
  23. {cloud-sql-python-connector-1.9.1 → cloud-sql-python-connector-1.10.0}/google/cloud/sql/connector/pymysql.py +0 -0
  24. {cloud-sql-python-connector-1.9.1 → cloud-sql-python-connector-1.10.0}/google/cloud/sql/connector/pytds.py +0 -0
  25. {cloud-sql-python-connector-1.9.1 → cloud-sql-python-connector-1.10.0}/google/cloud/sql/connector/rate_limiter.py +0 -0
  26. {cloud-sql-python-connector-1.9.1 → cloud-sql-python-connector-1.10.0}/google/cloud/sql/connector/refresh_utils.py +0 -0
  27. {cloud-sql-python-connector-1.9.1 → cloud-sql-python-connector-1.10.0}/google/cloud/sql/connector/utils.py +0 -0
  28. {cloud-sql-python-connector-1.9.1 → cloud-sql-python-connector-1.10.0}/setup.cfg +0 -0
  29. {cloud-sql-python-connector-1.9.1 → cloud-sql-python-connector-1.10.0}/setup.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: cloud-sql-python-connector
3
- Version: 1.9.1
3
+ Version: 1.10.0
4
4
  Summary: The Cloud SQL Python Connector is a library that can be used alongside a database driver to allow users with sufficient permissions to connect to a Cloud SQL database without having to manually allowlist IPs or manage SSL certificates.
5
5
  Home-page: https://github.com/GoogleCloudPlatform/cloud-sql-python-connector
6
6
  Author: Google LLC
@@ -199,9 +199,10 @@ To close the `Connector` object's background resources, call its `close()` metho
199
199
  connector.close()
200
200
  ```
201
201
 
202
- **Note**: For more examples of using SQLAlchemy to manage connection pooling with the connector, please see [Cloud SQL SQLAlchemy Samples](https://cloud.google.com/sql/docs/postgres/connect-connectors#python_1).
203
-
204
- **Note for SQL Server users**: If your SQL Server instance requires SSL, you need to download the CA certificate for your instance and include `cafile={path to downloaded certificate}` and `validate_host=False`. This is a workaround for a [known issue](https://issuetracker.google.com/184867147).
202
+ > [!NOTE]
203
+ >
204
+ > For more examples of using SQLAlchemy to manage connection pooling with the connector,
205
+ > please see [Cloud SQL SQLAlchemy Samples](https://cloud.google.com/sql/docs/postgres/connect-connectors#python_1).
205
206
 
206
207
  ### Configuring the Connector
207
208
 
@@ -210,14 +211,15 @@ defaults for each connection to make, you can initialize a
210
211
  `Connector` object as follows:
211
212
 
212
213
  ```python
213
- from google.cloud.sql.connector import Connector, IPTypes
214
+ from google.cloud.sql.connector import Connector
214
215
 
215
216
  # Note: all parameters below are optional
216
217
  connector = Connector(
217
218
  ip_type="public", # can also be "private" or "psc"
218
219
  enable_iam_auth=False,
219
220
  timeout=30,
220
- credentials=custom_creds # google.auth.credentials.Credentials
221
+ credentials=custom_creds, # google.auth.credentials.Credentials
222
+ refresh_strategy="lazy", # can be "lazy" or "background"
221
223
  )
222
224
  ```
223
225
 
@@ -279,6 +281,21 @@ with Connector() as connector:
279
281
  print(row)
280
282
  ```
281
283
 
284
+ ### Configuring a Lazy Refresh (Cloud Run, Cloud Functions etc.)
285
+
286
+ The Connector's `refresh_strategy` argument can be set to `"lazy"` to configure
287
+ the Python Connector to retrieve connection info lazily and as-needed.
288
+ Otherwise, a background refresh cycle runs to retrive the connection info
289
+ periodically. This setting is useful in environments where the CPU may be
290
+ throttled outside of a request context, e.g., Cloud Run, Cloud Functions, etc.
291
+
292
+ To set the refresh strategy, set the `refresh_strategy` keyword argument when
293
+ initializing a `Connector`:
294
+
295
+ ```python
296
+ connector = Connector(refresh_strategy="lazy")
297
+ ```
298
+
282
299
  ### Specifying IP Address Type
283
300
 
284
301
  The Cloud SQL Python Connector can be used to connect to Cloud SQL instances
@@ -301,9 +318,11 @@ conn = connector.connect(
301
318
  )
302
319
  ```
303
320
 
304
- Note: If specifying Private IP or Private Service Connect, your application must be
305
- attached to the proper VPC network to connect to your Cloud SQL instance. For most
306
- applications this will require the use of a [VPC Connector][vpc-connector].
321
+ > [!IMPORTANT]
322
+ >
323
+ > If specifying Private IP or Private Service Connect (PSC), your application must be
324
+ > attached to the proper VPC network to connect to your Cloud SQL instance. For most
325
+ > applications this will require the use of a [VPC Connector][vpc-connector].
307
326
 
308
327
  [psc]: https://cloud.google.com/vpc/docs/private-service-connect
309
328
  [vpc-connector]: https://cloud.google.com/vpc/docs/configure-serverless-vpc-access#create-connector
@@ -332,9 +351,20 @@ conn = connector.connect(
332
351
  )
333
352
  ```
334
353
 
335
- ### SQL Server Active Directory Authentication
354
+ ### SQL Server (MSSQL)
355
+
356
+ > [!IMPORTANT]
357
+ >
358
+ > If your SQL Server instance is set to [enforce SSL connections](https://cloud.google.com/sql/docs/sqlserver/configure-ssl-instance#enforcing-ssl),
359
+ > you need to download the CA certificate for your instance and include `cafile={path to downloaded certificate}`
360
+ > and `validate_host=False`. This is a workaround for a [known issue](https://issuetracker.google.com/184867147).
361
+
362
+ #### Active Directory Authentication
336
363
 
337
- Active Directory authentication for SQL Server instances is currently only supported on Windows. First, make sure to follow [these steps](https://cloud.google.com/blog/topics/developers-practitioners/creating-sql-server-instance-integrated-active-directory-using-google-cloud-sql) to set up a Managed AD domain and join your Cloud SQL instance to the domain. [See here for more info on Cloud SQL Active Directory integration](https://cloud.google.com/sql/docs/sqlserver/ad).
364
+ Active Directory authentication for SQL Server instances is currently only supported on Windows.
365
+ First, make sure to follow [these steps](https://cloud.google.com/blog/topics/developers-practitioners/creating-sql-server-instance-integrated-active-directory-using-google-cloud-sql)
366
+ to set up a Managed AD domain and join your Cloud SQL instance to the domain.
367
+ [See here for more info on Cloud SQL Active Directory integration](https://cloud.google.com/sql/docs/sqlserver/ad).
338
368
 
339
369
  Once you have followed the steps linked above, you can run the following code to return a connection object:
340
370
 
@@ -367,6 +397,14 @@ The Python Connector can be used alongside popular Python web frameworks such
367
397
  as Flask, FastAPI, etc, to integrate Cloud SQL databases within your
368
398
  web applications.
369
399
 
400
+ > [!NOTE]
401
+ >
402
+ > For serverless environments such as Cloud Functions, Cloud Run, etc, it may be
403
+ > beneficial to initialize the `Connector` with the lazy refresh strategy.
404
+ > i.e. `Connector(refresh_strategy="lazy")`
405
+ >
406
+ > See [Configuring a Lazy Refresh](#configuring-a-lazy-refresh-cloud-run-cloud-functions-etc)
407
+
370
408
  #### Flask-SQLAlchemy
371
409
 
372
410
  [Flask-SQLAlchemy](https://flask-sqlalchemy.palletsprojects.com/en/2.x/)
@@ -547,8 +585,10 @@ An alternative to using the `create_async_connector` function is initializing
547
585
  a `Connector` as an async context manager, removing the need for explicit
548
586
  calls to `connector.close_async()` to cleanup resources.
549
587
 
550
- **Note:** This alternative requires that the running event loop be
551
- passed in as the `loop` argument to `Connector()`.
588
+ > [!NOTE]
589
+ >
590
+ > This alternative requires that the running event loop be
591
+ > passed in as the `loop` argument to `Connector()`.
552
592
 
553
593
  ```python
554
594
  import asyncio
@@ -595,6 +635,28 @@ async def main():
595
635
  await pool.dispose()
596
636
  ```
597
637
 
638
+ ### Debug Logging
639
+
640
+ The Cloud SQL Python Connector uses the standard [Python logging module][python-logging]
641
+ for debug logging support.
642
+
643
+ Add the below code to your application to enable debug logging with the Cloud SQL
644
+ Python Connector:
645
+
646
+ ```python
647
+ import logging
648
+
649
+ logging.basicConfig(format="%(asctime)s [%(levelname)s]: %(message)s")
650
+ logger = logging.getLogger(name="google.cloud.sql.connector")
651
+ logger.setLevel(logging.DEBUG)
652
+ ```
653
+
654
+ For more details on configuring logging, please refer to the
655
+ [Python logging docs][configure-logging].
656
+
657
+ [python-logging]: https://docs.python.org/3/library/logging.html
658
+ [configure-logging]: https://docs.python.org/3/howto/logging.html#configuring-logging
659
+
598
660
  ## Support policy
599
661
 
600
662
  ### Major version lifecycle
@@ -173,9 +173,10 @@ To close the `Connector` object's background resources, call its `close()` metho
173
173
  connector.close()
174
174
  ```
175
175
 
176
- **Note**: For more examples of using SQLAlchemy to manage connection pooling with the connector, please see [Cloud SQL SQLAlchemy Samples](https://cloud.google.com/sql/docs/postgres/connect-connectors#python_1).
177
-
178
- **Note for SQL Server users**: If your SQL Server instance requires SSL, you need to download the CA certificate for your instance and include `cafile={path to downloaded certificate}` and `validate_host=False`. This is a workaround for a [known issue](https://issuetracker.google.com/184867147).
176
+ > [!NOTE]
177
+ >
178
+ > For more examples of using SQLAlchemy to manage connection pooling with the connector,
179
+ > please see [Cloud SQL SQLAlchemy Samples](https://cloud.google.com/sql/docs/postgres/connect-connectors#python_1).
179
180
 
180
181
  ### Configuring the Connector
181
182
 
@@ -184,14 +185,15 @@ defaults for each connection to make, you can initialize a
184
185
  `Connector` object as follows:
185
186
 
186
187
  ```python
187
- from google.cloud.sql.connector import Connector, IPTypes
188
+ from google.cloud.sql.connector import Connector
188
189
 
189
190
  # Note: all parameters below are optional
190
191
  connector = Connector(
191
192
  ip_type="public", # can also be "private" or "psc"
192
193
  enable_iam_auth=False,
193
194
  timeout=30,
194
- credentials=custom_creds # google.auth.credentials.Credentials
195
+ credentials=custom_creds, # google.auth.credentials.Credentials
196
+ refresh_strategy="lazy", # can be "lazy" or "background"
195
197
  )
196
198
  ```
197
199
 
@@ -253,6 +255,21 @@ with Connector() as connector:
253
255
  print(row)
254
256
  ```
255
257
 
258
+ ### Configuring a Lazy Refresh (Cloud Run, Cloud Functions etc.)
259
+
260
+ The Connector's `refresh_strategy` argument can be set to `"lazy"` to configure
261
+ the Python Connector to retrieve connection info lazily and as-needed.
262
+ Otherwise, a background refresh cycle runs to retrive the connection info
263
+ periodically. This setting is useful in environments where the CPU may be
264
+ throttled outside of a request context, e.g., Cloud Run, Cloud Functions, etc.
265
+
266
+ To set the refresh strategy, set the `refresh_strategy` keyword argument when
267
+ initializing a `Connector`:
268
+
269
+ ```python
270
+ connector = Connector(refresh_strategy="lazy")
271
+ ```
272
+
256
273
  ### Specifying IP Address Type
257
274
 
258
275
  The Cloud SQL Python Connector can be used to connect to Cloud SQL instances
@@ -275,9 +292,11 @@ conn = connector.connect(
275
292
  )
276
293
  ```
277
294
 
278
- Note: If specifying Private IP or Private Service Connect, your application must be
279
- attached to the proper VPC network to connect to your Cloud SQL instance. For most
280
- applications this will require the use of a [VPC Connector][vpc-connector].
295
+ > [!IMPORTANT]
296
+ >
297
+ > If specifying Private IP or Private Service Connect (PSC), your application must be
298
+ > attached to the proper VPC network to connect to your Cloud SQL instance. For most
299
+ > applications this will require the use of a [VPC Connector][vpc-connector].
281
300
 
282
301
  [psc]: https://cloud.google.com/vpc/docs/private-service-connect
283
302
  [vpc-connector]: https://cloud.google.com/vpc/docs/configure-serverless-vpc-access#create-connector
@@ -306,9 +325,20 @@ conn = connector.connect(
306
325
  )
307
326
  ```
308
327
 
309
- ### SQL Server Active Directory Authentication
328
+ ### SQL Server (MSSQL)
329
+
330
+ > [!IMPORTANT]
331
+ >
332
+ > If your SQL Server instance is set to [enforce SSL connections](https://cloud.google.com/sql/docs/sqlserver/configure-ssl-instance#enforcing-ssl),
333
+ > you need to download the CA certificate for your instance and include `cafile={path to downloaded certificate}`
334
+ > and `validate_host=False`. This is a workaround for a [known issue](https://issuetracker.google.com/184867147).
335
+
336
+ #### Active Directory Authentication
310
337
 
311
- Active Directory authentication for SQL Server instances is currently only supported on Windows. First, make sure to follow [these steps](https://cloud.google.com/blog/topics/developers-practitioners/creating-sql-server-instance-integrated-active-directory-using-google-cloud-sql) to set up a Managed AD domain and join your Cloud SQL instance to the domain. [See here for more info on Cloud SQL Active Directory integration](https://cloud.google.com/sql/docs/sqlserver/ad).
338
+ Active Directory authentication for SQL Server instances is currently only supported on Windows.
339
+ First, make sure to follow [these steps](https://cloud.google.com/blog/topics/developers-practitioners/creating-sql-server-instance-integrated-active-directory-using-google-cloud-sql)
340
+ to set up a Managed AD domain and join your Cloud SQL instance to the domain.
341
+ [See here for more info on Cloud SQL Active Directory integration](https://cloud.google.com/sql/docs/sqlserver/ad).
312
342
 
313
343
  Once you have followed the steps linked above, you can run the following code to return a connection object:
314
344
 
@@ -341,6 +371,14 @@ The Python Connector can be used alongside popular Python web frameworks such
341
371
  as Flask, FastAPI, etc, to integrate Cloud SQL databases within your
342
372
  web applications.
343
373
 
374
+ > [!NOTE]
375
+ >
376
+ > For serverless environments such as Cloud Functions, Cloud Run, etc, it may be
377
+ > beneficial to initialize the `Connector` with the lazy refresh strategy.
378
+ > i.e. `Connector(refresh_strategy="lazy")`
379
+ >
380
+ > See [Configuring a Lazy Refresh](#configuring-a-lazy-refresh-cloud-run-cloud-functions-etc)
381
+
344
382
  #### Flask-SQLAlchemy
345
383
 
346
384
  [Flask-SQLAlchemy](https://flask-sqlalchemy.palletsprojects.com/en/2.x/)
@@ -521,8 +559,10 @@ An alternative to using the `create_async_connector` function is initializing
521
559
  a `Connector` as an async context manager, removing the need for explicit
522
560
  calls to `connector.close_async()` to cleanup resources.
523
561
 
524
- **Note:** This alternative requires that the running event loop be
525
- passed in as the `loop` argument to `Connector()`.
562
+ > [!NOTE]
563
+ >
564
+ > This alternative requires that the running event loop be
565
+ > passed in as the `loop` argument to `Connector()`.
526
566
 
527
567
  ```python
528
568
  import asyncio
@@ -569,6 +609,28 @@ async def main():
569
609
  await pool.dispose()
570
610
  ```
571
611
 
612
+ ### Debug Logging
613
+
614
+ The Cloud SQL Python Connector uses the standard [Python logging module][python-logging]
615
+ for debug logging support.
616
+
617
+ Add the below code to your application to enable debug logging with the Cloud SQL
618
+ Python Connector:
619
+
620
+ ```python
621
+ import logging
622
+
623
+ logging.basicConfig(format="%(asctime)s [%(levelname)s]: %(message)s")
624
+ logger = logging.getLogger(name="google.cloud.sql.connector")
625
+ logger.setLevel(logging.DEBUG)
626
+ ```
627
+
628
+ For more details on configuring logging, please refer to the
629
+ [Python logging docs][configure-logging].
630
+
631
+ [python-logging]: https://docs.python.org/3/library/logging.html
632
+ [configure-logging]: https://docs.python.org/3/howto/logging.html#configuring-logging
633
+
572
634
  ## Support policy
573
635
 
574
636
  ### Major version lifecycle
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: cloud-sql-python-connector
3
- Version: 1.9.1
3
+ Version: 1.10.0
4
4
  Summary: The Cloud SQL Python Connector is a library that can be used alongside a database driver to allow users with sufficient permissions to connect to a Cloud SQL database without having to manually allowlist IPs or manage SSL certificates.
5
5
  Home-page: https://github.com/GoogleCloudPlatform/cloud-sql-python-connector
6
6
  Author: Google LLC
@@ -199,9 +199,10 @@ To close the `Connector` object's background resources, call its `close()` metho
199
199
  connector.close()
200
200
  ```
201
201
 
202
- **Note**: For more examples of using SQLAlchemy to manage connection pooling with the connector, please see [Cloud SQL SQLAlchemy Samples](https://cloud.google.com/sql/docs/postgres/connect-connectors#python_1).
203
-
204
- **Note for SQL Server users**: If your SQL Server instance requires SSL, you need to download the CA certificate for your instance and include `cafile={path to downloaded certificate}` and `validate_host=False`. This is a workaround for a [known issue](https://issuetracker.google.com/184867147).
202
+ > [!NOTE]
203
+ >
204
+ > For more examples of using SQLAlchemy to manage connection pooling with the connector,
205
+ > please see [Cloud SQL SQLAlchemy Samples](https://cloud.google.com/sql/docs/postgres/connect-connectors#python_1).
205
206
 
206
207
  ### Configuring the Connector
207
208
 
@@ -210,14 +211,15 @@ defaults for each connection to make, you can initialize a
210
211
  `Connector` object as follows:
211
212
 
212
213
  ```python
213
- from google.cloud.sql.connector import Connector, IPTypes
214
+ from google.cloud.sql.connector import Connector
214
215
 
215
216
  # Note: all parameters below are optional
216
217
  connector = Connector(
217
218
  ip_type="public", # can also be "private" or "psc"
218
219
  enable_iam_auth=False,
219
220
  timeout=30,
220
- credentials=custom_creds # google.auth.credentials.Credentials
221
+ credentials=custom_creds, # google.auth.credentials.Credentials
222
+ refresh_strategy="lazy", # can be "lazy" or "background"
221
223
  )
222
224
  ```
223
225
 
@@ -279,6 +281,21 @@ with Connector() as connector:
279
281
  print(row)
280
282
  ```
281
283
 
284
+ ### Configuring a Lazy Refresh (Cloud Run, Cloud Functions etc.)
285
+
286
+ The Connector's `refresh_strategy` argument can be set to `"lazy"` to configure
287
+ the Python Connector to retrieve connection info lazily and as-needed.
288
+ Otherwise, a background refresh cycle runs to retrive the connection info
289
+ periodically. This setting is useful in environments where the CPU may be
290
+ throttled outside of a request context, e.g., Cloud Run, Cloud Functions, etc.
291
+
292
+ To set the refresh strategy, set the `refresh_strategy` keyword argument when
293
+ initializing a `Connector`:
294
+
295
+ ```python
296
+ connector = Connector(refresh_strategy="lazy")
297
+ ```
298
+
282
299
  ### Specifying IP Address Type
283
300
 
284
301
  The Cloud SQL Python Connector can be used to connect to Cloud SQL instances
@@ -301,9 +318,11 @@ conn = connector.connect(
301
318
  )
302
319
  ```
303
320
 
304
- Note: If specifying Private IP or Private Service Connect, your application must be
305
- attached to the proper VPC network to connect to your Cloud SQL instance. For most
306
- applications this will require the use of a [VPC Connector][vpc-connector].
321
+ > [!IMPORTANT]
322
+ >
323
+ > If specifying Private IP or Private Service Connect (PSC), your application must be
324
+ > attached to the proper VPC network to connect to your Cloud SQL instance. For most
325
+ > applications this will require the use of a [VPC Connector][vpc-connector].
307
326
 
308
327
  [psc]: https://cloud.google.com/vpc/docs/private-service-connect
309
328
  [vpc-connector]: https://cloud.google.com/vpc/docs/configure-serverless-vpc-access#create-connector
@@ -332,9 +351,20 @@ conn = connector.connect(
332
351
  )
333
352
  ```
334
353
 
335
- ### SQL Server Active Directory Authentication
354
+ ### SQL Server (MSSQL)
355
+
356
+ > [!IMPORTANT]
357
+ >
358
+ > If your SQL Server instance is set to [enforce SSL connections](https://cloud.google.com/sql/docs/sqlserver/configure-ssl-instance#enforcing-ssl),
359
+ > you need to download the CA certificate for your instance and include `cafile={path to downloaded certificate}`
360
+ > and `validate_host=False`. This is a workaround for a [known issue](https://issuetracker.google.com/184867147).
361
+
362
+ #### Active Directory Authentication
336
363
 
337
- Active Directory authentication for SQL Server instances is currently only supported on Windows. First, make sure to follow [these steps](https://cloud.google.com/blog/topics/developers-practitioners/creating-sql-server-instance-integrated-active-directory-using-google-cloud-sql) to set up a Managed AD domain and join your Cloud SQL instance to the domain. [See here for more info on Cloud SQL Active Directory integration](https://cloud.google.com/sql/docs/sqlserver/ad).
364
+ Active Directory authentication for SQL Server instances is currently only supported on Windows.
365
+ First, make sure to follow [these steps](https://cloud.google.com/blog/topics/developers-practitioners/creating-sql-server-instance-integrated-active-directory-using-google-cloud-sql)
366
+ to set up a Managed AD domain and join your Cloud SQL instance to the domain.
367
+ [See here for more info on Cloud SQL Active Directory integration](https://cloud.google.com/sql/docs/sqlserver/ad).
338
368
 
339
369
  Once you have followed the steps linked above, you can run the following code to return a connection object:
340
370
 
@@ -367,6 +397,14 @@ The Python Connector can be used alongside popular Python web frameworks such
367
397
  as Flask, FastAPI, etc, to integrate Cloud SQL databases within your
368
398
  web applications.
369
399
 
400
+ > [!NOTE]
401
+ >
402
+ > For serverless environments such as Cloud Functions, Cloud Run, etc, it may be
403
+ > beneficial to initialize the `Connector` with the lazy refresh strategy.
404
+ > i.e. `Connector(refresh_strategy="lazy")`
405
+ >
406
+ > See [Configuring a Lazy Refresh](#configuring-a-lazy-refresh-cloud-run-cloud-functions-etc)
407
+
370
408
  #### Flask-SQLAlchemy
371
409
 
372
410
  [Flask-SQLAlchemy](https://flask-sqlalchemy.palletsprojects.com/en/2.x/)
@@ -547,8 +585,10 @@ An alternative to using the `create_async_connector` function is initializing
547
585
  a `Connector` as an async context manager, removing the need for explicit
548
586
  calls to `connector.close_async()` to cleanup resources.
549
587
 
550
- **Note:** This alternative requires that the running event loop be
551
- passed in as the `loop` argument to `Connector()`.
588
+ > [!NOTE]
589
+ >
590
+ > This alternative requires that the running event loop be
591
+ > passed in as the `loop` argument to `Connector()`.
552
592
 
553
593
  ```python
554
594
  import asyncio
@@ -595,6 +635,28 @@ async def main():
595
635
  await pool.dispose()
596
636
  ```
597
637
 
638
+ ### Debug Logging
639
+
640
+ The Cloud SQL Python Connector uses the standard [Python logging module][python-logging]
641
+ for debug logging support.
642
+
643
+ Add the below code to your application to enable debug logging with the Cloud SQL
644
+ Python Connector:
645
+
646
+ ```python
647
+ import logging
648
+
649
+ logging.basicConfig(format="%(asctime)s [%(levelname)s]: %(message)s")
650
+ logger = logging.getLogger(name="google.cloud.sql.connector")
651
+ logger.setLevel(logging.DEBUG)
652
+ ```
653
+
654
+ For more details on configuring logging, please refer to the
655
+ [Python logging docs][configure-logging].
656
+
657
+ [python-logging]: https://docs.python.org/3/library/logging.html
658
+ [configure-logging]: https://docs.python.org/3/howto/logging.html#configuring-logging
659
+
598
660
  ## Support policy
599
661
 
600
662
  ### Major version lifecycle
@@ -11,9 +11,12 @@ cloud_sql_python_connector.egg-info/top_level.txt
11
11
  google/cloud/sql/connector/__init__.py
12
12
  google/cloud/sql/connector/asyncpg.py
13
13
  google/cloud/sql/connector/client.py
14
+ google/cloud/sql/connector/connection_info.py
14
15
  google/cloud/sql/connector/connector.py
16
+ google/cloud/sql/connector/enums.py
15
17
  google/cloud/sql/connector/exceptions.py
16
18
  google/cloud/sql/connector/instance.py
19
+ google/cloud/sql/connector/lazy.py
17
20
  google/cloud/sql/connector/pg8000.py
18
21
  google/cloud/sql/connector/py.typed
19
22
  google/cloud/sql/connector/pymysql.py
@@ -16,7 +16,14 @@ limitations under the License.
16
16
 
17
17
  from google.cloud.sql.connector.connector import Connector
18
18
  from google.cloud.sql.connector.connector import create_async_connector
19
- from google.cloud.sql.connector.instance import IPTypes
19
+ from google.cloud.sql.connector.enums import IPTypes
20
+ from google.cloud.sql.connector.enums import RefreshStrategy
20
21
  from google.cloud.sql.connector.version import __version__
21
22
 
22
- __all__ = ["__version__", "create_async_connector", "Connector", "IPTypes"]
23
+ __all__ = [
24
+ "__version__",
25
+ "create_async_connector",
26
+ "Connector",
27
+ "IPTypes",
28
+ "RefreshStrategy",
29
+ ]
@@ -14,6 +14,7 @@
14
14
 
15
15
  from __future__ import annotations
16
16
 
17
+ import asyncio
17
18
  import datetime
18
19
  import logging
19
20
  from typing import Any, Dict, Optional, Tuple, TYPE_CHECKING
@@ -21,8 +22,11 @@ from typing import Any, Dict, Optional, Tuple, TYPE_CHECKING
21
22
  import aiohttp
22
23
  from cryptography.hazmat.backends import default_backend
23
24
  from cryptography.x509 import load_pem_x509_certificate
24
- import google.auth.transport.requests
25
+ from google.auth.credentials import TokenState
26
+ from google.auth.transport import requests
25
27
 
28
+ from google.cloud.sql.connector.connection_info import ConnectionInfo
29
+ from google.cloud.sql.connector.exceptions import AutoIAMAuthNotSupported
26
30
  from google.cloud.sql.connector.refresh_utils import _downscope_credentials
27
31
  from google.cloud.sql.connector.version import __version__ as version
28
32
 
@@ -113,9 +117,6 @@ class CloudSQLClient:
113
117
  addresses and their type and a string representing the
114
118
  certificate authority.
115
119
  """
116
- if not self._credentials.valid:
117
- request = google.auth.transport.requests.Request()
118
- self._credentials.refresh(request)
119
120
 
120
121
  headers = {
121
122
  "Authorization": f"Bearer {self._credentials.token}",
@@ -123,8 +124,6 @@ class CloudSQLClient:
123
124
 
124
125
  url = f"{self._sqladmin_api_endpoint}/sql/{API_VERSION}/projects/{project}/instances/{instance}/connectSettings"
125
126
 
126
- logger.debug(f"['{instance}']: Requesting metadata")
127
-
128
127
  resp = await self._client.get(url, headers=headers, raise_for_status=True)
129
128
  ret_dict = await resp.json()
130
129
 
@@ -174,13 +173,6 @@ class CloudSQLClient:
174
173
  :returns: An ephemeral certificate from the Cloud SQL instance that allows
175
174
  authorized connections to the instance.
176
175
  """
177
-
178
- logger.debug(f"['{instance}']: Requesting ephemeral certificate")
179
-
180
- if not self._credentials.valid:
181
- request = google.auth.transport.requests.Request()
182
- self._credentials.refresh(request)
183
-
184
176
  headers = {
185
177
  "Authorization": f"Bearer {self._credentials.token}",
186
178
  }
@@ -220,6 +212,84 @@ class CloudSQLClient:
220
212
  expiration = token_expiration
221
213
  return ephemeral_cert, expiration
222
214
 
215
+ async def get_connection_info(
216
+ self,
217
+ project: str,
218
+ region: str,
219
+ instance: str,
220
+ keys: asyncio.Future,
221
+ enable_iam_auth: bool,
222
+ ) -> ConnectionInfo:
223
+ """Immediately performs a full refresh operation using the Cloud SQL
224
+ Admin API.
225
+
226
+ Args:
227
+ project (str): The name of the project the Cloud SQL instance is
228
+ located in.
229
+ region (str): The region the Cloud SQL instance is located in.
230
+ instance (str): Name of the Cloud SQL instance.
231
+ keys (asyncio.Future): A future to the client's public-private key
232
+ pair.
233
+ enable_iam_auth (bool): Whether an automatic IAM database
234
+ authentication connection is being requested (Postgres and MySQL).
235
+
236
+ Returns:
237
+ ConnectionInfo: All the information required to connect securely to
238
+ the Cloud SQL instance.
239
+ Raises:
240
+ AutoIAMAuthNotSupported: Database engine does not support automatic
241
+ IAM authentication.
242
+ """
243
+ priv_key, pub_key = await keys
244
+ # before making Cloud SQL Admin API calls, refresh creds if required
245
+ if not self._credentials.token_state == TokenState.FRESH:
246
+ self._credentials.refresh(requests.Request())
247
+
248
+ metadata_task = asyncio.create_task(
249
+ self._get_metadata(
250
+ project,
251
+ region,
252
+ instance,
253
+ )
254
+ )
255
+
256
+ ephemeral_task = asyncio.create_task(
257
+ self._get_ephemeral(
258
+ project,
259
+ instance,
260
+ pub_key,
261
+ enable_iam_auth,
262
+ )
263
+ )
264
+ try:
265
+ metadata = await metadata_task
266
+ # check if automatic IAM database authn is supported for database engine
267
+ if enable_iam_auth and not metadata["database_version"].startswith(
268
+ ("POSTGRES", "MYSQL")
269
+ ):
270
+ raise AutoIAMAuthNotSupported(
271
+ f"'{metadata['database_version']}' does not support "
272
+ "automatic IAM authentication. It is only supported with "
273
+ "Cloud SQL Postgres or MySQL instances."
274
+ )
275
+ except Exception:
276
+ # cancel ephemeral cert task if exception occurs before it is awaited
277
+ ephemeral_task.cancel()
278
+ raise
279
+
280
+ ephemeral_cert, expiration = await ephemeral_task
281
+
282
+ return ConnectionInfo(
283
+ ephemeral_cert,
284
+ metadata["server_ca_cert"],
285
+ priv_key,
286
+ metadata["ip_addresses"],
287
+ metadata["database_version"],
288
+ expiration,
289
+ )
290
+
223
291
  async def close(self) -> None:
224
292
  """Close CloudSQLClient gracefully."""
293
+ logger.debug("Waiting for Connector's http client to close")
225
294
  await self._client.close()
295
+ logger.debug("Closed Connector's http client")