Spaces:
Running
Running
File size: 1,937 Bytes
50a5912 fa7ac61 50a5912 fa7ac61 50a5912 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
"""
This module handles the retrieval of credentials
required for authentication with GCP services.
"""
import json
import os
import stat
import tempfile
# Path to local credentials file, used in local development.
CREDENTIALS_PATH = os.environ.get("CREDENTIALS_PATH")
# Credentials passed as an environment variable, used in deployment.
CREDENTIALS = os.environ.get("CREDENTIALS")
def set_credentials(credentials: str = None, credentials_path: str = None):
if not credentials and not credentials_path:
raise ValueError(
"No credentials found. Ensure credentials or credentials path is set.")
if credentials_path:
if os.path.exists(credentials_path):
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = credentials_path
return
raise FileNotFoundError(f"Credentials file not found: {credentials_path}")
# Create a temporary file to store credentials for
# services that don't accept string format credentials.
with tempfile.NamedTemporaryFile(mode="w", suffix=".json",
delete=False) as cred_file:
cred_file.write(credentials)
os.chmod(cred_file.name, stat.S_IRUSR)
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = cred_file.name
# TODO(#69): Replace get_credentials_json with set_credentials.
def get_credentials_json():
if not CREDENTIALS and not CREDENTIALS_PATH:
raise ValueError(
"No credentials found. Ensure CREDENTIALS or CREDENTIALS_PATH is set.")
# Use the environment variable for credentials when a file cannot be used
# in the environment, as credentials should not be made public.
if CREDENTIALS:
return json.loads(CREDENTIALS)
if not os.path.exists(CREDENTIALS_PATH):
raise FileNotFoundError(f"Credentials file not found: {CREDENTIALS_PATH}")
# Set credentials using a file in a local environment.
with open(CREDENTIALS_PATH, "r", encoding="utf-8") as cred_file:
return json.load(cred_file)
|