Spaces:
Sleeping
Sleeping
manage json secret
Browse files
config.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import os
|
2 |
import json
|
|
|
3 |
from dotenv import load_dotenv
|
4 |
|
5 |
# Load environment variables from .env file if it exists
|
@@ -23,24 +24,64 @@ if missing_vars:
|
|
23 |
""")
|
24 |
|
25 |
# Handle BigQuery credentials
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
with open(credentials_path, "w") as f:
|
33 |
-
f.write(credentials_content)
|
34 |
|
35 |
-
#
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
else:
|
41 |
-
print("
|
42 |
|
43 |
# BigQuery configuration
|
44 |
-
BIGQUERY_KEY_PATH = os.environ.get("BIGQUERY_KEY_PATH"
|
45 |
-
PROJECT_ID = os.environ.get("PROJECT_ID"
|
46 |
-
DATASET_ID = os.environ.get("DATASET_ID"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import json
|
3 |
+
import sys
|
4 |
from dotenv import load_dotenv
|
5 |
|
6 |
# Load environment variables from .env file if it exists
|
|
|
24 |
""")
|
25 |
|
26 |
# Handle BigQuery credentials
|
27 |
+
print("DEBUG: Handling BigQuery credentials...")
|
28 |
+
|
29 |
+
if os.environ.get("BIGQUERY_KEY_PATH"):
|
30 |
+
bigquery_key = os.environ.get("BIGQUERY_KEY_PATH")
|
31 |
+
print(f"DEBUG: BIGQUERY_KEY_PATH is set, length: {len(bigquery_key)} characters")
|
32 |
+
print(f"DEBUG: First 20 chars: {bigquery_key[:20]}...")
|
|
|
|
|
33 |
|
34 |
+
# If BIGQUERY_KEY_PATH contains the actual JSON content (as in HF Spaces secrets)
|
35 |
+
if bigquery_key.strip().startswith("{"):
|
36 |
+
print("DEBUG: BIGQUERY_KEY_PATH appears to contain JSON content")
|
37 |
+
try:
|
38 |
+
# Validate JSON
|
39 |
+
json_obj = json.loads(bigquery_key)
|
40 |
+
print("DEBUG: Successfully parsed JSON content")
|
41 |
+
|
42 |
+
# Create a temporary credentials file from the environment variable
|
43 |
+
credentials_path = "/tmp/bigquery_credentials.json"
|
44 |
+
|
45 |
+
with open(credentials_path, "w") as f:
|
46 |
+
f.write(bigquery_key)
|
47 |
+
|
48 |
+
print(f"DEBUG: Wrote credentials to {credentials_path}")
|
49 |
+
|
50 |
+
# Set the credentials path to the temporary file
|
51 |
+
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = credentials_path
|
52 |
+
print(f"DEBUG: Set GOOGLE_APPLICATION_CREDENTIALS to {credentials_path}")
|
53 |
+
|
54 |
+
# Verify the file exists
|
55 |
+
if os.path.exists(credentials_path):
|
56 |
+
print(f"DEBUG: Confirmed credentials file exists at {credentials_path}")
|
57 |
+
with open(credentials_path, "r") as f:
|
58 |
+
first_line = f.readline()
|
59 |
+
print(f"DEBUG: First line of credentials file: {first_line[:20]}...")
|
60 |
+
else:
|
61 |
+
print(f"ERROR: Failed to create credentials file at {credentials_path}")
|
62 |
+
except json.JSONDecodeError as e:
|
63 |
+
print(f"ERROR: BIGQUERY_KEY_PATH contains invalid JSON: {str(e)}")
|
64 |
+
print("Please ensure your secret contains valid JSON content")
|
65 |
+
# If BIGQUERY_KEY_PATH is a file path
|
66 |
+
elif os.path.exists(bigquery_key):
|
67 |
+
print(f"DEBUG: BIGQUERY_KEY_PATH points to existing file: {bigquery_key}")
|
68 |
+
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = bigquery_key
|
69 |
+
else:
|
70 |
+
print(f"WARNING: BIGQUERY_KEY_PATH is set but does not contain JSON and is not a valid file path: {bigquery_key}")
|
71 |
else:
|
72 |
+
print("WARNING: BIGQUERY_KEY_PATH environment variable is not set")
|
73 |
|
74 |
# BigQuery configuration
|
75 |
+
BIGQUERY_KEY_PATH = os.environ.get("BIGQUERY_KEY_PATH")
|
76 |
+
PROJECT_ID = os.environ.get("PROJECT_ID")
|
77 |
+
DATASET_ID = os.environ.get("DATASET_ID")
|
78 |
+
|
79 |
+
# Final check for GOOGLE_APPLICATION_CREDENTIALS
|
80 |
+
if os.environ.get("GOOGLE_APPLICATION_CREDENTIALS"):
|
81 |
+
print(f"DEBUG: GOOGLE_APPLICATION_CREDENTIALS is set to: {os.environ.get('GOOGLE_APPLICATION_CREDENTIALS')}")
|
82 |
+
if os.path.exists(os.environ.get("GOOGLE_APPLICATION_CREDENTIALS")):
|
83 |
+
print("DEBUG: GOOGLE_APPLICATION_CREDENTIALS file exists")
|
84 |
+
else:
|
85 |
+
print("ERROR: GOOGLE_APPLICATION_CREDENTIALS file does not exist")
|
86 |
+
else:
|
87 |
+
print("WARNING: GOOGLE_APPLICATION_CREDENTIALS is not set")
|