Skip to content

Commit 57c70ba

Browse files
feat(cli): fall back to Application Default Credentials for secrets commands (#898)
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent ef8df7b commit 57c70ba

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

airbyte_cdk/cli/airbyte_cdk/_secrets.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,12 @@
6161
logger = logging.getLogger("airbyte-cdk.cli.secrets")
6262

6363
try:
64+
import google.auth.exceptions
6465
from google.cloud import secretmanager_v1 as secretmanager
6566
from google.cloud.secretmanager_v1 import Secret
6667
except 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

416418
def _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

440459
def _print_ci_secrets_masks(

0 commit comments

Comments
 (0)