|
| 1 | +# Copyright (c) 2025 Airbyte, Inc., all rights reserved. |
| 2 | +"""Secret management commands. |
| 3 | +
|
| 4 | +This module provides commands for managing secrets for Airbyte connectors. |
| 5 | +
|
| 6 | +Usage: |
| 7 | + airbyte-cdk secrets fetch --connector-name source-github |
| 8 | + airbyte-cdk secrets fetch --connector-directory /path/to/connector |
| 9 | + airbyte-cdk secrets fetch # Run from within a connector directory |
| 10 | +
|
| 11 | +Usage without pre-installing (stateless): |
| 12 | + pipx run airbyte-cdk secrets fetch ... |
| 13 | + uvx airbyte-cdk secrets fetch ... |
| 14 | +
|
| 15 | +The 'fetch' command retrieves secrets from Google Secret Manager based on connector |
| 16 | +labels and writes them to the connector's `secrets` directory. |
| 17 | +""" |
| 18 | + |
| 19 | +import json |
| 20 | +import os |
| 21 | +from pathlib import Path |
| 22 | + |
| 23 | +import rich_click as click |
| 24 | + |
| 25 | +from airbyte_cdk.cli.airbyte_cdk._util import resolve_connector_name_and_directory |
| 26 | + |
| 27 | +AIRBYTE_INTERNAL_GCP_PROJECT = "dataline-integration-testing" |
| 28 | +CONNECTOR_LABEL = "connector" |
| 29 | + |
| 30 | + |
| 31 | +@click.group( |
| 32 | + name="secrets", |
| 33 | + help=__doc__.replace("\n", "\n\n"), |
| 34 | +) |
| 35 | +def secrets_cli_group() -> None: |
| 36 | + """Secret management commands.""" |
| 37 | + pass |
| 38 | + |
| 39 | + |
| 40 | +@secrets_cli_group.command() |
| 41 | +@click.option( |
| 42 | + "--connector-name", |
| 43 | + type=str, |
| 44 | + help="Name of the connector to fetch secrets for. Ignored if --connector-directory is provided.", |
| 45 | +) |
| 46 | +@click.option( |
| 47 | + "--connector-directory", |
| 48 | + type=click.Path(exists=True, file_okay=False, path_type=Path), |
| 49 | + help="Path to the connector directory.", |
| 50 | +) |
| 51 | +@click.option( |
| 52 | + "--gcp-project-id", |
| 53 | + type=str, |
| 54 | + default=AIRBYTE_INTERNAL_GCP_PROJECT, |
| 55 | + help=f"GCP project ID. Defaults to '{AIRBYTE_INTERNAL_GCP_PROJECT}'.", |
| 56 | +) |
| 57 | +def fetch( |
| 58 | + connector_name: str | None = None, |
| 59 | + connector_directory: Path | None = None, |
| 60 | + gcp_project_id: str = AIRBYTE_INTERNAL_GCP_PROJECT, |
| 61 | +) -> None: |
| 62 | + """Fetch secrets for a connector from Google Secret Manager. |
| 63 | +
|
| 64 | + This command fetches secrets for a connector from Google Secret Manager and writes them |
| 65 | + to the connector's secrets directory. |
| 66 | +
|
| 67 | + If no connector name or directory is provided, we will look within the current working |
| 68 | + directory. If the current working directory is not a connector directory (e.g. starting |
| 69 | + with 'source-') and no connector name or path is provided, the process will fail. |
| 70 | + """ |
| 71 | + try: |
| 72 | + from google.cloud import secretmanager_v1 as secretmanager |
| 73 | + except ImportError: |
| 74 | + raise ImportError( |
| 75 | + "google-cloud-secret-manager package is required for Secret Manager integration. " |
| 76 | + "Install it with 'pip install airbyte-cdk[dev]' " |
| 77 | + "or 'pip install google-cloud-secret-manager'." |
| 78 | + ) |
| 79 | + |
| 80 | + click.echo("Fetching secrets...") |
| 81 | + |
| 82 | + # Resolve connector name/directory |
| 83 | + try: |
| 84 | + connector_name, connector_directory = resolve_connector_name_and_directory( |
| 85 | + connector_name=connector_name, |
| 86 | + connector_directory=connector_directory, |
| 87 | + ) |
| 88 | + except FileNotFoundError as e: |
| 89 | + raise FileNotFoundError( |
| 90 | + f"Could not find connector directory for '{connector_name}'. " |
| 91 | + "Please provide the --connector-directory option with the path to the connector. " |
| 92 | + "Note: This command requires either running from within a connector directory, " |
| 93 | + "being in the airbyte monorepo, or explicitly providing the connector directory path." |
| 94 | + ) from e |
| 95 | + except ValueError as e: |
| 96 | + raise ValueError(str(e)) |
| 97 | + |
| 98 | + # Create secrets directory if it doesn't exist |
| 99 | + secrets_dir = connector_directory / "secrets" |
| 100 | + secrets_dir.mkdir(parents=True, exist_ok=True) |
| 101 | + |
| 102 | + gitignore_path = secrets_dir / ".gitignore" |
| 103 | + gitignore_path.write_text("*") |
| 104 | + |
| 105 | + # Get GSM client |
| 106 | + credentials_json = os.environ.get("GCP_GSM_CREDENTIALS") |
| 107 | + if not credentials_json: |
| 108 | + raise ValueError( |
| 109 | + "No Google Cloud credentials found. Please set the GCP_GSM_CREDENTIALS environment variable." |
| 110 | + ) |
| 111 | + |
| 112 | + client = secretmanager.SecretManagerServiceClient.from_service_account_info( |
| 113 | + json.loads(credentials_json) |
| 114 | + ) |
| 115 | + |
| 116 | + # List all secrets with the connector label |
| 117 | + parent = f"projects/{gcp_project_id}" |
| 118 | + filter_string = f"labels.{CONNECTOR_LABEL}={connector_name}" |
| 119 | + secrets = client.list_secrets( |
| 120 | + request=secretmanager.ListSecretsRequest( |
| 121 | + parent=parent, |
| 122 | + filter=filter_string, |
| 123 | + ) |
| 124 | + ) |
| 125 | + |
| 126 | + # Fetch and write secrets |
| 127 | + secret_count = 0 |
| 128 | + for secret in secrets: |
| 129 | + secret_name = secret.name |
| 130 | + version_name = f"{secret_name}/versions/latest" |
| 131 | + response = client.access_secret_version(name=version_name) |
| 132 | + payload = response.payload.data.decode("UTF-8") |
| 133 | + |
| 134 | + filename_base = "config" # Default filename |
| 135 | + if secret.labels and "filename" in secret.labels: |
| 136 | + filename_base = secret.labels["filename"] |
| 137 | + |
| 138 | + secret_file_path = secrets_dir / f"{filename_base}.json" |
| 139 | + secret_file_path.write_text(payload) |
| 140 | + click.echo(f"Secret written to: {secret_file_path}") |
| 141 | + secret_count += 1 |
| 142 | + |
| 143 | + if secret_count == 0: |
| 144 | + click.echo(f"No secrets found for connector: {connector_name}") |
| 145 | + |
| 146 | + |
| 147 | +__all__ = [ |
| 148 | + "secrets_cli_group", |
| 149 | +] |
0 commit comments