Skip to content

Commit

Permalink
better error handling for secret manager
Browse files Browse the repository at this point in the history
  • Loading branch information
robedwards committed Jun 1, 2024
1 parent 65bd005 commit 24f3828
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions devai-cli/src/devai/commands/review.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from google.cloud.aiplatform import telemetry
import os
from google.cloud import secretmanager
from google.api_core.exceptions import NotFound
from google.api_core.exceptions import NotFound, PermissionDenied
from google.api_core.gapic_v1.client_info import ClientInfo
import logging

Expand All @@ -37,6 +37,14 @@ def ensure_env_variable(var_name):
return value

def get_prompt( secret_id: str) -> str:
"""Retrieves a secret value from Google Secret Manager.
Args:
secret_id: The ID of the secret to retrieve.
Returns:
The secret value as a string, or None if the secret is not found or the user lacks permission.
"""
try:
project_id = ensure_env_variable('PROJECT_ID')
logging.info("PROJECT_ID:", project_id)
Expand All @@ -48,11 +56,21 @@ def get_prompt( secret_id: str) -> str:
try:
response = client.access_secret_version(name=name)
payload = response.payload.data.decode("utf-8")
logging.info(f"ID: {secret_id} in project {project_id}")
logging.info(f"Successfully retrieved secret ID: {secret_id} in project {project_id}")
return payload

except PermissionDenied:
logging.warning(f"Insufficient permissions to access secret {secret_id} in project {project_id}")
return None

except NotFound:
logging.info(f"ID not found: {secret_id} in project {project_id}")
logging.info(f"Secret ID not found: {secret_id} in project {project_id}")
return None

except Exception as e: # Catching a broader range of potential errors
logging.error(f"An unexpected error occurred while retrieving secret '{secret_id}': {e}")
return None

except EnvironmentError as e:
logging.error(e)

Expand Down

0 comments on commit 24f3828

Please sign in to comment.