Gonalb commited on
Commit
0382412
·
1 Parent(s): 7a2828d

manage json secret

Browse files
Files changed (1) hide show
  1. config.py +58 -17
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
- # If BIGQUERY_KEY_PATH contains the actual JSON content (as in HF Spaces secrets)
27
- if os.environ.get("BIGQUERY_KEY_PATH") and os.environ.get("BIGQUERY_KEY_PATH").strip().startswith("{"):
28
- # Create a temporary credentials file from the environment variable
29
- credentials_content = os.environ.get("BIGQUERY_KEY_PATH")
30
- credentials_path = "/tmp/bigquery_credentials.json"
31
-
32
- with open(credentials_path, "w") as f:
33
- f.write(credentials_content)
34
 
35
- # Set the credentials path to the temporary file
36
- os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = credentials_path
37
- # If BIGQUERY_KEY_PATH is a file path
38
- elif os.environ.get("BIGQUERY_KEY_PATH") and os.path.exists(os.environ.get("BIGQUERY_KEY_PATH")):
39
- os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = os.environ.get("BIGQUERY_KEY_PATH")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  else:
41
- print("Warning: BigQuery credentials not found or invalid")
42
 
43
  # BigQuery configuration
44
- BIGQUERY_KEY_PATH = os.environ.get("BIGQUERY_KEY_PATH", "xepelin-411205-1ba8a63c9c96.json")
45
- PROJECT_ID = os.environ.get("PROJECT_ID", "xepelin-411205")
46
- DATASET_ID = os.environ.get("DATASET_ID", "ecommerceaie5")
 
 
 
 
 
 
 
 
 
 
 
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")