Repository navigation
Support Workload identity using service account for GET Signed URL in GCS - #6980
thainguyensunya wants to merge 3 commits into
Conversation
Greptile SummaryThis PR adds Workload Identity support to the GCS signed URL generation path in Confidence Score: 4/5Safe 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 backend/utils/other/storage.py — specifically the credential refresh block in Important Files Changed
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]
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 |
There was a problem hiding this comment.
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.
| if not creds.valid: | ||
| creds.refresh(google.auth.transport.requests.Request()) |
There was a problem hiding this comment.
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.
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. |
|
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:
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! 💜 |
Change:
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.