2727import datetime
2828from enum import Enum
2929import google .auth
30- from google .auth .credentials import Credentials
30+ from google .auth .credentials import Credentials , with_scopes_if_required
3131import google .auth .transport .requests
3232import OpenSSL
3333import platform
@@ -117,6 +117,15 @@ def __init__(self, *args: Any) -> None:
117117 super (PlatformNotSupportedError , self ).__init__ (self , * args )
118118
119119
120+ class CredentialsTypeError (Exception ):
121+ """
122+ Raised when credentials parameter is not proper type.
123+ """
124+
125+ def __init__ (self , * args : Any ) -> None :
126+ super (CredentialsTypeError , self ).__init__ (self , * args )
127+
128+
120129class InstanceMetadata :
121130 ip_addrs : Dict [str , Any ]
122131 context : ssl .SSLContext
@@ -177,6 +186,11 @@ class InstanceConnectionManager:
177186 The user agent string to append to SQLAdmin API requests
178187 :type user_agent_string: str
179188
189+ :type credentials: google.auth.credentials.Credentials
190+ :param credentials
191+ Credentials object used to authenticate connections to Cloud SQL server.
192+ If not specified, Application Default Credentials are used.
193+
180194 :param enable_iam_auth
181195 Enables IAM based authentication for Postgres instances.
182196 :type enable_iam_auth: bool
@@ -229,6 +243,7 @@ def __init__(
229243 driver_name : str ,
230244 keys : concurrent .futures .Future ,
231245 loop : asyncio .AbstractEventLoop ,
246+ credentials : Optional [Credentials ] = None ,
232247 enable_iam_auth : bool = False ,
233248 ) -> None :
234249 # Validate connection string
@@ -250,7 +265,14 @@ def __init__(
250265 self ._user_agent_string = f"{ APPLICATION_NAME } /{ version } +{ driver_name } "
251266 self ._loop = loop
252267 self ._keys = asyncio .wrap_future (keys , loop = self ._loop )
253- self ._auth_init ()
268+ # validate credentials type
269+ if not isinstance (credentials , Credentials ) and credentials is not None :
270+ raise CredentialsTypeError (
271+ "Arg credentials must be type 'google.auth.credentials.Credentials' "
272+ "or None (to use Application Default Credentials)"
273+ )
274+
275+ self ._auth_init (credentials )
254276
255277 self ._refresh_rate_limiter = AsyncRateLimiter (
256278 max_capacity = 2 , rate = 1 / 30 , loop = self ._loop
@@ -343,17 +365,25 @@ async def _get_instance_data(self) -> InstanceMetadata:
343365 self ._enable_iam_auth ,
344366 )
345367
346- def _auth_init (self ) -> None :
368+ def _auth_init (self , credentials : Optional [ Credentials ] ) -> None :
347369 """Creates and assigns a Google Python API service object for
348370 Google Cloud SQL Admin API.
349- """
350371
351- credentials , project = google .auth .default (
352- scopes = [
353- "https://www.googleapis.com/auth/sqlservice.admin" ,
354- "https://www.googleapis.com/auth/cloud-platform" ,
355- ]
356- )
372+ :type credentials: google.auth.credentials.Credentials
373+ :param credentials
374+ Credentials object used to authenticate connections to Cloud SQL server.
375+ If not specified, Application Default Credentials are used.
376+ """
377+ scopes = [
378+ "https://www.googleapis.com/auth/sqlservice.admin" ,
379+ "https://www.googleapis.com/auth/cloud-platform" ,
380+ ]
381+ # if Credentials object is passed in, use for authentication
382+ if isinstance (credentials , Credentials ):
383+ credentials = with_scopes_if_required (credentials , scopes = scopes )
384+ # otherwise use application default credentials
385+ else :
386+ credentials , project = google .auth .default (scopes = scopes )
357387
358388 self ._credentials = credentials
359389
0 commit comments