|
|
|
""" |
|
Google Cloud Authentication Helper |
|
This script handles Google Cloud authentication using environment variables. |
|
""" |
|
|
|
import os |
|
import json |
|
import tempfile |
|
from google.auth import default |
|
from google.cloud import storage |
|
|
|
|
|
def setup_gcp_credentials(): |
|
"""Setup Google Cloud credentials from environment variables.""" |
|
|
|
|
|
creds_content = os.getenv('GOOGLE_APPLICATION_CREDENTIALS_CONTENT') |
|
|
|
if creds_content: |
|
|
|
try: |
|
creds_dict = json.loads(creds_content) |
|
|
|
|
|
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f: |
|
json.dump(creds_dict, f) |
|
temp_creds_path = f.name |
|
|
|
|
|
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = temp_creds_path |
|
|
|
print(f"β
Google Cloud credentials configured successfully") |
|
print(f"π Project ID: {creds_dict.get('project_id', 'N/A')}") |
|
print(f"π§ Client Email: {creds_dict.get('client_email', 'N/A')}") |
|
|
|
return temp_creds_path |
|
|
|
except json.JSONDecodeError as e: |
|
print(f"β Error parsing Google Cloud credentials: {e}") |
|
return None |
|
else: |
|
print("β οΈ No Google Cloud credentials found in environment variables") |
|
return None |
|
|
|
|
|
def test_gcp_connection(): |
|
"""Test Google Cloud connection.""" |
|
try: |
|
|
|
credentials, project = default() |
|
print(f"β
Google Cloud authentication successful") |
|
print(f"π Project: {project}") |
|
|
|
|
|
client = storage.Client() |
|
buckets = list(client.list_buckets()) |
|
print(f"π¦ Found {len(buckets)} storage buckets") |
|
|
|
return True |
|
except Exception as e: |
|
print(f"β Google Cloud connection test failed: {e}") |
|
return False |
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
creds_path = setup_gcp_credentials() |
|
|
|
if creds_path: |
|
|
|
test_gcp_connection() |
|
else: |
|
print("β Failed to setup Google Cloud credentials") |
|
|