Skip to content

Commit db50049

Browse files
committed
feat(cli): add support for GSM default creds and OIDC
1 parent fa8d54d commit db50049

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

airbyte_cdk/cli/airbyte_cdk/_secrets.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,15 @@
6262

6363
try:
6464
from google.cloud import secretmanager_v1 as secretmanager
65-
from google.cloud.secretmanager_v1 import Secret
65+
from google.cloud.secretmanager_v1 import (
66+
Secret,
67+
SecretManagerServiceClient,
68+
)
6669
except ImportError:
6770
# If the package is not installed, we will raise an error in the CLI command.
6871
secretmanager = None # type: ignore
6972
Secret = None # type: ignore
73+
SecretManagerServiceClient = None # type: ignore
7074

7175

7276
@click.group(
@@ -408,29 +412,31 @@ def _get_secret_filepath(
408412

409413
return secrets_dir / "config.json" # Default filename
410414

411-
412-
def _get_gsm_secrets_client() -> "secretmanager.SecretManagerServiceClient": # type: ignore
413-
"""Get the Google Secret Manager client."""
414-
if not secretmanager:
415+
def _get_gsm_secrets_client() -> "SecretManagerServiceClient":
416+
"""Initialize GSM client via env var or default credentials (including OIDC/WIF)."""
417+
if not secretmanager or not SecretManagerServiceClient:
415418
raise ImportError(
416-
"google-cloud-secret-manager package is required for Secret Manager integration. "
417-
"Install it with 'pip install airbyte-cdk[dev]' "
418-
"or 'pip install google-cloud-secret-manager'."
419+
"Missing google-cloud-secret-manager. Install 'pip install google-cloud-secret-manager'"
419420
)
420421

421-
credentials_json = os.environ.get("GCP_GSM_CREDENTIALS")
422-
if not credentials_json:
422+
creds_json: str | None = os.environ.get("GCP_GSM_CREDENTIALS", None)
423+
if creds_json:
424+
creds_dict = json.loads(creds_json)
425+
try:
426+
return SecretManagerServiceClient.from_service_account_info(creds_dict)
427+
except Exception as e:
428+
raise ValueError(
429+
"Invalid GCP_GSM_CREDENTIALS. Ensure it is a valid JSON string."
430+
) from e
431+
# Fallback: use Application Default Credentials (supports WIF/OIDC)
432+
try:
433+
return SecretManagerServiceClient()
434+
except Exception as e:
423435
raise ValueError(
424-
"No Google Cloud credentials found. "
425-
"Please set the `GCP_GSM_CREDENTIALS` environment variable."
426-
)
436+
"Unable to obtain GCP credentials. Set the `GCP_GSM_CREDENTIALS` env var or configure "
437+
"default credentials."
438+
) from e
427439

428-
return cast(
429-
"secretmanager.SecretManagerServiceClient",
430-
secretmanager.SecretManagerServiceClient.from_service_account_info(
431-
json.loads(credentials_json)
432-
),
433-
)
434440

435441

436442
def _print_ci_secrets_masks(

0 commit comments

Comments
 (0)