-
Notifications
You must be signed in to change notification settings - Fork 91
#1125 Allow non-credentialed users to run ETL pipelines #1150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
adamzev
merged 7 commits into
CodeForPhilly:staging
from
adamzev:1125-credential-handling
May 10, 2025
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4e150d0
chore(release): 1.0.0 [skip ci]
semantic-release-bot 3125fae
feat: allow non-credentialed users and cloud env credentials
adamzev cf08d08
chore: add VACANT_LOT_DB back to docker-compose env
adamzev 7167ebc
chore: modify data_diff's send_to_slack method
adamzev e23ceb7
Update CHANGELOG.md
adamzev d802e51
chore: made credentials optional for current/old pipeline
adamzev 0151adf
Merge branch 'staging' into 1125-credential-handling
adamzev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,34 @@ | ||
| import os | ||
|
|
||
| from config.config import is_docker | ||
| from sqlalchemy import create_engine | ||
|
|
||
| url: str = ( | ||
| os.environ["VACANT_LOTS_DB"].replace("localhost", "host.docker.internal") | ||
| if is_docker() | ||
| else os.environ["VACANT_LOTS_DB"] | ||
| ) | ||
| from config.config import is_docker | ||
|
|
||
| # Detect if running in Cloud Run: | ||
| is_cloud_run = "K_SERVICE" in os.environ or "CLOUD_RUN_JOB" in os.environ | ||
|
|
||
| # Use host.docker.internal when running locally in Docker | ||
| # except when running in Cloud Run | ||
| if is_docker() and not is_cloud_run: | ||
| host = "host.docker.internal" | ||
| else: | ||
| host = "localhost" | ||
|
|
||
|
|
||
| if os.getenv("VACANT_LOTS_DB"): | ||
| # Use the provided database URL | ||
| url = os.getenv("VACANT_LOTS_DB") | ||
| else: | ||
| # Use the specified port, pw, db and user to construct the URL | ||
| pw = os.environ["POSTGRES_PASSWORD"] | ||
| port = os.getenv("POSTGRES_PORT", "5432") | ||
| db = os.getenv("POSTGRES_DB", "vacantlotdb") | ||
| user = os.getenv("POSTGRES_USER", "postgres") | ||
| url: str = f"postgresql://{user}:{pw}@{host}:{port}/{db}" | ||
| print( | ||
| f"Connecting to database with URL: postgresql://{user}:****@{host}:{port}/{db}" | ||
| ) | ||
|
|
||
|
|
||
| local_engine = create_engine(url) | ||
| conn = local_engine.connect() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import logging as log | ||
| import os | ||
|
|
||
| from google.cloud import storage | ||
|
|
||
| from config.config import log_level | ||
|
|
||
| log.basicConfig(level=log_level) | ||
|
|
||
|
|
||
| class GCSBucketManager: | ||
| """ | ||
| A manager for interacting with a Google Cloud Storage bucket. | ||
|
|
||
| This class initializes a bucket client using Application Default Credentials, | ||
| an optional service account key, or falls back to an anonymous client for read-only access. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, bucket_name: str = None, credential_path: str = None, client=None | ||
| ): | ||
| """ | ||
| Initialize the GCSBucketManager. | ||
|
|
||
| Args: | ||
| bucket_name (str): Name of the bucket. Defaults to the environment variable | ||
| 'GOOGLE_CLOUD_BUCKET_NAME' or "cleanandgreenphl" if not set. | ||
| credential_path (str): Optional path to a service account credentials file. | ||
| client: Optional storage. Client instance for dependency injection in testing. | ||
| """ | ||
| self.bucket_name = bucket_name or os.getenv( | ||
| "GOOGLE_CLOUD_BUCKET_NAME", "cleanandgreenphl" | ||
| ) | ||
|
|
||
| self.read_only = False | ||
|
|
||
| if client is not None: | ||
| self._client = client | ||
| else: | ||
| self._client = self._init_client(credential_path) | ||
|
|
||
| self.bucket = self._client.bucket(self.bucket_name) | ||
|
|
||
| def _init_client(self, credential_path: str = None): | ||
| """ | ||
| Attempt to initialize the storage client using a credential file, application default | ||
| credentials or fall back to anonymous/read-only. | ||
| """ | ||
| project_name = os.getenv("GOOGLE_CLOUD_PROJECT", "clean-and-green-philly") | ||
| credentials_path = credential_path or "/app/service-account-key.json" | ||
| is_credentials_file = os.path.exists(credentials_path) | ||
|
|
||
| if is_credentials_file: | ||
| os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = credentials_path | ||
|
|
||
| try: | ||
| # This will use application default credentials if GOOGLE_APPLICATION_CREDENTIALS is not set | ||
| if is_credentials_file: | ||
| print(f"Using service account key at {credentials_path}") | ||
| else: | ||
| print("Using application default credentials") | ||
| return storage.Client(project=project_name) | ||
|
|
||
| except Exception as e: | ||
| log.warning(f"Failed to initialize client with service account key: {e}") | ||
| log.warning("Falling back to anonymous client (read-only mode)") | ||
| self.read_only = True | ||
| return storage.Client.create_anonymous_client() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I probably should switch this back to
in-case we're in docker and VACANT_LOTS_DB is defined.