Fix: load Google credentials from env var for HuggingFace Spaces deployment
Browse files- Dockerfile +0 -3
- src/google_auth.py +17 -6
Dockerfile
CHANGED
|
@@ -12,10 +12,7 @@ RUN uv sync --no-dev --no-install-project
|
|
| 12 |
# Copy source
|
| 13 |
COPY . .
|
| 14 |
|
| 15 |
-
COPY credentials/creds.json /app/credentials/creds.json
|
| 16 |
-
|
| 17 |
ENV PATH="/app/.venv/bin:$PATH"
|
| 18 |
-
ENV GOOGLE_APPLICATION_CREDENTIALS=/app/credentials/creds.json
|
| 19 |
|
| 20 |
EXPOSE 7860
|
| 21 |
|
|
|
|
| 12 |
# Copy source
|
| 13 |
COPY . .
|
| 14 |
|
|
|
|
|
|
|
| 15 |
ENV PATH="/app/.venv/bin:$PATH"
|
|
|
|
| 16 |
|
| 17 |
EXPOSE 7860
|
| 18 |
|
src/google_auth.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import logging
|
| 3 |
from typing import Optional
|
|
@@ -14,20 +15,30 @@ _SCOPES = ["https://www.googleapis.com/auth/cloud-platform"]
|
|
| 14 |
def get_google_credentials() -> google.auth.credentials.Credentials:
|
| 15 |
"""
|
| 16 |
Resolution order:
|
| 17 |
-
1.
|
| 18 |
-
2.
|
|
|
|
| 19 |
"""
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
|
|
|
| 22 |
if creds_path:
|
| 23 |
if not os.path.isfile(creds_path):
|
| 24 |
raise FileNotFoundError(
|
| 25 |
f"GOOGLE_APPLICATION_CREDENTIALS='{creds_path}' tapi file tidak ditemukan."
|
| 26 |
)
|
| 27 |
logger.info("Google auth: service account dari %s", creds_path)
|
| 28 |
-
return service_account.Credentials.from_service_account_file(
|
| 29 |
-
creds_path, scopes=_SCOPES
|
| 30 |
-
)
|
| 31 |
|
| 32 |
logger.info("Google auth: menggunakan ADC (gcloud login)")
|
| 33 |
credentials, _ = google.auth.default(scopes=_SCOPES)
|
|
|
|
| 1 |
+
import json
|
| 2 |
import os
|
| 3 |
import logging
|
| 4 |
from typing import Optional
|
|
|
|
| 15 |
def get_google_credentials() -> google.auth.credentials.Credentials:
|
| 16 |
"""
|
| 17 |
Resolution order:
|
| 18 |
+
1. GOOGLE_CREDENTIALS_JSON env var (raw JSON string) → service account info
|
| 19 |
+
2. GOOGLE_APPLICATION_CREDENTIALS env var set & file exists → service account file
|
| 20 |
+
3. Fallback → ADC (gcloud auth application-default login)
|
| 21 |
"""
|
| 22 |
+
creds_json: Optional[str] = os.environ.get("GOOGLE_CREDENTIALS_JSON")
|
| 23 |
+
if creds_json:
|
| 24 |
+
logger.info("Google auth: service account dari GOOGLE_CREDENTIALS_JSON")
|
| 25 |
+
# Value may be raw JSON or base64-encoded JSON
|
| 26 |
+
try:
|
| 27 |
+
import base64
|
| 28 |
+
decoded = base64.b64decode(creds_json).decode("utf-8")
|
| 29 |
+
info = json.loads(decoded)
|
| 30 |
+
except Exception:
|
| 31 |
+
info = json.loads(creds_json)
|
| 32 |
+
return service_account.Credentials.from_service_account_info(info, scopes=_SCOPES)
|
| 33 |
|
| 34 |
+
creds_path: Optional[str] = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS")
|
| 35 |
if creds_path:
|
| 36 |
if not os.path.isfile(creds_path):
|
| 37 |
raise FileNotFoundError(
|
| 38 |
f"GOOGLE_APPLICATION_CREDENTIALS='{creds_path}' tapi file tidak ditemukan."
|
| 39 |
)
|
| 40 |
logger.info("Google auth: service account dari %s", creds_path)
|
| 41 |
+
return service_account.Credentials.from_service_account_file(creds_path, scopes=_SCOPES)
|
|
|
|
|
|
|
| 42 |
|
| 43 |
logger.info("Google auth: menggunakan ADC (gcloud login)")
|
| 44 |
credentials, _ = google.auth.default(scopes=_SCOPES)
|