Skip to content

Commit f6cba7e

Browse files
refactor: create helper function for secret URL generation
Co-Authored-By: Aaron <AJ> Steers <[email protected]>
1 parent bb59d97 commit f6cba7e

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

airbyte_cdk/cli/airbyte_cdk/_secrets.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ def fetch(
132132
)
133133
# Fetch and write secrets
134134
secret_count = 0
135-
failed_secrets = []
136-
failed_secret_urls = []
135+
failed_secrets: List[str] = []
137136

138137
for secret in secrets:
139138
secret_file_path = _get_secret_filepath(
@@ -149,8 +148,6 @@ def fetch(
149148
if error:
150149
secret_name = secret.name.split("/secrets/")[-1] # Removes project prefix
151150
failed_secrets.append(secret_name)
152-
secret_url = f"https://console.cloud.google.com/security/secret-manager/secret/{secret_name}/versions?hl=en&project={gcp_project_id}"
153-
failed_secret_urls.append(secret_url)
154151
click.echo(f"Failed to retrieve secret '{secret_name}': {error}", err=True)
155152
else:
156153
click.echo(f"Secret written to: {secret_file_path.absolute()!s}", err=True)
@@ -175,7 +172,7 @@ def fetch(
175172
raise ConnectorSecretWithNoValidVersionsError(
176173
connector_name=connector_name,
177174
secret_names=failed_secrets,
178-
connector_secret_urls=failed_secret_urls,
175+
gcp_project_id=gcp_project_id,
179176
)
180177

181178
if not print_ci_secrets_masks:
@@ -259,8 +256,7 @@ def list_(
259256
for secret in secrets:
260257
full_secret_name = secret.name
261258
secret_name = full_secret_name.split("/secrets/")[-1] # Removes project prefix
262-
# E.g. https://console.cloud.google.com/security/secret-manager/secret/SECRET_SOURCE-SHOPIFY__CREDS/versions?hl=en&project=<gcp_project_id>
263-
secret_url = f"https://console.cloud.google.com/security/secret-manager/secret/{secret_name}/versions?hl=en&project={gcp_project_id}"
259+
secret_url = _get_secret_url(secret_name, gcp_project_id)
264260
table.add_row(
265261
f"[link={secret_url}]{secret_name}[/link]",
266262
"\n".join([f"{k}={v}" for k, v in secret.labels.items()]),
@@ -270,6 +266,19 @@ def list_(
270266
console.print(table)
271267

272268

269+
def _get_secret_url(secret_name: str, gcp_project_id: str) -> str:
270+
"""Generate a URL for a secret in the GCP Secret Manager console.
271+
272+
Args:
273+
secret_name: The name of the secret
274+
gcp_project_id: The GCP project ID
275+
276+
Returns:
277+
str: URL to the secret in the GCP console
278+
"""
279+
return f"https://console.cloud.google.com/security/secret-manager/secret/{secret_name}/versions?hl=en&project={gcp_project_id}"
280+
281+
273282
def _fetch_secret_handles(
274283
connector_name: str,
275284
gcp_project_id: str = AIRBYTE_INTERNAL_GCP_PROJECT,

airbyte_cdk/cli/airbyte_cdk/exceptions.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ class ConnectorSecretWithNoValidVersionsError(AirbyteConnectorError):
1313

1414
connector_name: str
1515
secret_names: List[str]
16-
connector_secret_urls: List[str]
16+
gcp_project_id: str
1717

1818
def __str__(self) -> str:
1919
"""Return a string representation of the exception."""
20-
urls_str = "\n".join([f"- {url}" for url in self.connector_secret_urls])
20+
from airbyte_cdk.cli.airbyte_cdk._secrets import _get_secret_url
21+
22+
urls = [_get_secret_url(secret_name, self.gcp_project_id) for secret_name in self.secret_names]
23+
urls_str = "\n".join([f"- {url}" for url in urls])
2124
secrets_str = ", ".join(self.secret_names)
2225
return (
2326
f"No valid versions found for the following secrets in connector '{self.connector_name}': {secrets_str}. "

0 commit comments

Comments
 (0)