6161logger = logging .getLogger ("airbyte-cdk.cli.secrets" )
6262
6363try :
64+ import google .auth .exceptions
6465 from google .cloud import secretmanager_v1 as secretmanager
6566 from google .cloud .secretmanager_v1 import Secret
6667except ImportError :
6768 # If the package is not installed, we will raise an error in the CLI command.
69+ google = None # type: ignore
6870 secretmanager = None # type: ignore
6971 Secret = None # type: ignore
7072
@@ -414,7 +416,14 @@ def _get_secret_filepath(
414416
415417
416418def _get_gsm_secrets_client () -> "secretmanager.SecretManagerServiceClient" : # type: ignore
417- """Get the Google Secret Manager client."""
419+ """Get the Google Secret Manager client.
420+
421+ If the `GCP_GSM_CREDENTIALS` environment variable is set, the client will be
422+ created using service account credentials from that JSON string. Otherwise, the
423+ client will fall back to Application Default Credentials (ADC), which supports
424+ user credentials from `gcloud auth application-default login`, GCE metadata
425+ server credentials, and other standard GCP authentication methods.
426+ """
418427 if not secretmanager :
419428 raise ImportError (
420429 "google-cloud-secret-manager package is required for Secret Manager integration. "
@@ -423,18 +432,28 @@ def _get_gsm_secrets_client() -> "secretmanager.SecretManagerServiceClient": #
423432 )
424433
425434 credentials_json = os .environ .get ("GCP_GSM_CREDENTIALS" )
426- if not credentials_json :
427- raise ValueError (
428- "No Google Cloud credentials found. "
429- "Please set the `GCP_GSM_CREDENTIALS` environment variable."
435+ if credentials_json :
436+ click .echo (
437+ "Using GCP service account credentials from GCP_GSM_CREDENTIALS env var." , err = True
438+ )
439+ return cast (
440+ "secretmanager.SecretManagerServiceClient" ,
441+ secretmanager .SecretManagerServiceClient .from_service_account_info (
442+ json .loads (credentials_json )
443+ ),
430444 )
431445
432- return cast (
433- "secretmanager.SecretManagerServiceClient" ,
434- secretmanager .SecretManagerServiceClient .from_service_account_info (
435- json .loads (credentials_json )
436- ),
446+ click .echo (
447+ "GCP_GSM_CREDENTIALS not set. Using Application Default Credentials (ADC)." , err = True
437448 )
449+ try :
450+ return secretmanager .SecretManagerServiceClient ()
451+ except google .auth .exceptions .DefaultCredentialsError :
452+ raise ValueError (
453+ "No Google Cloud credentials found. "
454+ "Either set the `GCP_GSM_CREDENTIALS` environment variable with service account JSON, "
455+ "or run `gcloud auth application-default login` to authenticate with your user account."
456+ ) from None
438457
439458
440459def _print_ci_secrets_masks (
0 commit comments