|
62 | 62 |
|
63 | 63 | try: |
64 | 64 | 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 | + ) |
66 | 69 | except ImportError: |
67 | 70 | # If the package is not installed, we will raise an error in the CLI command. |
68 | 71 | secretmanager = None # type: ignore |
69 | 72 | Secret = None # type: ignore |
| 73 | + SecretManagerServiceClient = None # type: ignore |
70 | 74 |
|
71 | 75 |
|
72 | 76 | @click.group( |
@@ -408,29 +412,31 @@ def _get_secret_filepath( |
408 | 412 |
|
409 | 413 | return secrets_dir / "config.json" # Default filename |
410 | 414 |
|
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: |
415 | 418 | 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'" |
419 | 420 | ) |
420 | 421 |
|
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: |
423 | 435 | 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 |
427 | 439 |
|
428 | | - return cast( |
429 | | - "secretmanager.SecretManagerServiceClient", |
430 | | - secretmanager.SecretManagerServiceClient.from_service_account_info( |
431 | | - json.loads(credentials_json) |
432 | | - ), |
433 | | - ) |
434 | 440 |
|
435 | 441 |
|
436 | 442 | def _print_ci_secrets_masks( |
|
0 commit comments