Gonalb commited on
Commit
c1da4b1
·
1 Parent(s): 349f218

manage json secret

Browse files
Files changed (1) hide show
  1. config.py +34 -49
config.py CHANGED
@@ -29,59 +29,44 @@ print("DEBUG: Handling BigQuery credentials...")
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 configurationo
75
- BIGQUERY_KEY_PATH = bigquery_key
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")
 
 
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
 
33
+ # Create a temporary credentials file from the environment variable
34
+ credentials_path = "/tmp/bigquery_credentials.json"
35
+
36
+ try:
37
+ # Try to directly write the content to a file without JSON parsing
38
+ with open(credentials_path, "w") as f:
39
+ f.write(bigquery_key)
40
+
41
+ print(f"DEBUG: Wrote credentials to {credentials_path}")
42
+
43
+
44
+ os.environ["BIGQUERY_KEY_PATH"] = credentials_path
45
+ print(f"DEBUG: Set BIGQUERY_KEY_PATH to {credentials_path}")
46
+
47
+ # Verify the file exists
48
+ if os.path.exists(credentials_path):
49
+ print(f"DEBUG: Confirmed credentials file exists at {credentials_path}")
50
+ # Try to validate the JSON by reading it back
51
+ try:
 
 
 
 
52
  with open(credentials_path, "r") as f:
53
+ json.load(f)
54
+ print("DEBUG: Credentials file contains valid JSON")
55
+ except json.JSONDecodeError as e:
56
+ print(f"WARNING: Credentials file contains invalid JSON: {str(e)}")
57
+ print("This might cause issues with BigQuery authentication")
58
+ else:
59
+ print(f"ERROR: Failed to create credentials file at {credentials_path}")
60
+ except Exception as e:
61
+ print(f"ERROR: Failed to process BigQuery credentials: {str(e)}")
 
 
 
 
62
  else:
63
  print("WARNING: BIGQUERY_KEY_PATH environment variable is not set")
64
 
 
 
 
 
65
 
66
+
67
+ # Final check for credentials
68
+ if os.path.exists(os.environ.get("BIGQUERY_KEY_PATH", "")):
69
+ print(f"DEBUG: BIGQUERY_KEY_PATH file exists at: {os.environ.get('BIGQUERY_KEY_PATH')}")
 
 
 
70
  else:
71
+ print(f"WARNING: BIGQUERY_KEY_PATH file does not exist at: {os.environ.get('BIGQUERY_KEY_PATH')}")
72
+