@@ -448,6 +448,65 @@ def _setup_service_account_credentials_from_dict(self, creds_data: dict) -> None
448448 raise ValueError (f"Invalid service account credentials: { e } " ) from e
449449
450450
451+ def prepare_credentials_for_adc (self ) -> None :
452+ """
453+ Prepare Google credentials for Application Default Credentials (ADC).
454+
455+ Supports both inline JSON and file paths:
456+ - Inline JSON: Writes temp file and updates env var
457+ - File path: Validates existence, logs warning if not found
458+
459+ This enables google.auth.default() to work with both credential sources while
460+ maintaining official API usage.
461+
462+ Raises:
463+ ValueError: If inline JSON is invalid or incomplete
464+ """
465+ creds_env = os .environ .get ("GOOGLE_APPLICATION_CREDENTIALS" )
466+
467+ # If not set, log warning and return
468+ if not creds_env :
469+ logger .warning ("GOOGLE_APPLICATION_CREDENTIALS not set. Will fall back to ADC." )
470+ return
471+
472+ # Inline JSON pattern
473+ if creds_env .strip ().startswith ("{" ):
474+ creds_data = None
475+ try :
476+ # Validate JSON structure
477+ creds_data = self ._parse_and_validate_credentials_json (creds_env )
478+
479+ # Write to temp file
480+ temp_path = Path ("/tmp/gcp-credentials.json" )
481+ with open (temp_path , "w" , encoding = "utf-8" ) as f :
482+ json .dump (creds_data , f )
483+
484+ # Set restrictive permissions
485+ temp_path .chmod (0o600 )
486+
487+ # Update env var
488+ os .environ ["GOOGLE_APPLICATION_CREDENTIALS" ] = str (temp_path )
489+
490+ logger .info ("Prepared inline JSON credentials for ADC" )
491+
492+ except ValueError :
493+ # Re-raise validation errors
494+ raise
495+
496+ except Exception as e :
497+ raise ValueError (f"Failed to prepare inline credentials: { e } " ) from e
498+
499+ finally :
500+ # Clear data from memory
501+ if creds_data :
502+ creds_data .clear ()
503+
504+ # File path pattern
505+ elif not Path (creds_env ).exists ():
506+ logger .warning (f"Credentials file not found: { creds_env } " )
507+ logger .warning ("Will attempt to use gcloud CLI or other ADC sources" )
508+
509+
451510 def setup_google_credentials (self ) -> None :
452511 """
453512 Set up Google credentials directly in memory from environment variable.
0 commit comments