Skip to content

Support Workload identity using service account for GET Signed URL in GCS - #6980

Closed
thainguyensunya wants to merge 3 commits into
mainfrom
task/migrate-backend-to-workload-identity
Closed

thainguyensunya wants to merge 3 commits into
mainfrom
task/migrate-backend-to-workload-identity

Conversation

@thainguyensunya

Copy link
Copy Markdown
Collaborator

Change:

  • Support Workload identity using service account for GET Signed URL in GCS

We plan to migrate google-credentials.json to Workload identity using Service account to authenticate to GCP services. This is the only code changes required for backend services for the migration.

@greptile-apps

greptile-apps Bot commented Apr 24, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds Workload Identity support to the GCS signed URL generation path in backend/utils/other/storage.py, replacing the single generate_signed_url call with a branch that detects whether the active credentials are a local service-account key or a keyless credential (e.g. Workload Identity), and uses IAM-based remote signing in the latter case.

Confidence Score: 4/5

Safe to merge with minor hardening — the credential refresh race and private attribute access are worth addressing but do not block the primary functionality.

The logic for detecting credential type and branching to IAM-based signing is correct. Two P2 findings remain: a thread-safety gap in the check-then-refresh pattern on the shared module-level credential object, and reliance on the private _credentials attribute. Neither is a definitive production breakage under normal single-threaded or low-concurrency conditions, but the race could manifest under load.

backend/utils/other/storage.py — specifically the credential refresh block in _get_signed_url

Important Files Changed

Filename Overview
backend/utils/other/storage.py Adds Workload Identity support for GCS signed URL generation by detecting credential type and refreshing/passing an access token when local private-key signing is unavailable; two minor concerns: concurrent credential refresh on the shared module-level client and use of a private _credentials attribute.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[_get_signed_url called] --> B{Cache hit?}
    B -- Yes --> C[Return cached URL]
    B -- No --> D[Read storage_client._credentials]
    D --> E{isinstance service_account.Credentials?}
    E -- Yes local key --> F[generate_signed_url local signing]
    E -- No Workload Identity --> G{GCS_SIGNER_SERVICE_ACCOUNT_EMAIL set?}
    G -- No --> H[Raise EnvironmentError]
    G -- Yes --> I{creds.valid?}
    I -- No --> J[creds.refresh via metadata server]
    J --> K[generate_signed_url IAM remote signing]
    I -- Yes --> K
    F --> L[cache_signed_url]
    K --> L
    L --> M[Return signed URL]
Loading

Reviews (1): Last reviewed commit: "Support Workload identity using service ..." | Re-trigger Greptile

return cached

signed_url = blob.generate_signed_url(version="v4", expiration=datetime.timedelta(minutes=minutes), method="GET")
creds = storage_client._credentials

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Accessing private _credentials attribute

storage_client._credentials is a private, implementation-detail attribute of the google-cloud-storage Client class. The leading underscore signals it is not part of the public API and may be renamed, moved, or removed in future library versions. Unfortunately there is no official public getter, but the recommended pattern when you need the underlying credentials is to pass them explicitly at client construction time and keep a reference yourself. Since the module already has a credentials variable in the SERVICE_ACCOUNT_JSON branch, you can restructure slightly to retain a reference to the resolved credentials object without touching the private attribute.

Comment on lines +1097 to +1098
if not creds.valid:
creds.refresh(google.auth.transport.requests.Request())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Credential refresh is not thread-safe under concurrent load

storage_client._credentials is a module-level singleton shared across all request handlers. Under concurrent load (FastAPI/uvicorn with multiple async tasks or threads), several callers can simultaneously evaluate not creds.valid as True and all proceed to call creds.refresh() concurrently. The google-auth library's Credentials.refresh() has no built-in mutex, so concurrent metadata-server calls can race, producing redundant token fetches and transient errors if the GCE metadata server throttles the requests. Wrapping the check-and-refresh block with a threading.Lock (or an asyncio.Lock if the call site is always inside an async context) eliminates the race.

@beastoin

Copy link
Copy Markdown
Collaborator

We plan to migrate google-credentials.json

my concern is it might affect to how our community build the omi backend.

so let's first have a complete plan then we move the needles one step one.

@beastoin beastoin closed this Apr 28, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Hey @thainguyensunya 👋

Thank you so much for taking the time to contribute to Omi! We truly appreciate you putting in the effort to submit this pull request.

After careful review, we've decided not to merge this particular PR. Please don't take this personally — we genuinely try to merge as many contributions as possible, but sometimes we have to make tough calls based on:

  • Project standards — Ensuring consistency across the codebase
  • User needs — Making sure changes align with what our users need
  • Code best practices — Maintaining code quality and maintainability
  • Project direction — Keeping aligned with our roadmap and vision

Your contribution is still valuable to us, and we'd love to see you contribute again in the future! If you'd like feedback on how to improve this PR or want to discuss alternative approaches, please don't hesitate to reach out.

Thank you for being part of the Omi community! 💜

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants