-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagnose_redis.py
More file actions
115 lines (98 loc) · 3.41 KB
/
diagnose_redis.py
File metadata and controls
115 lines (98 loc) · 3.41 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import os
import sys
import time
import redis
from dotenv import load_dotenv
# Load env vars
load_dotenv('backend/.env')
print("="*60)
print("REDIS DIAGNOSTIC TOOL")
print("="*60)
redis_url = os.getenv('REDIS_URL')
print(f"REDIS_URL provided: {'Yes' if redis_url else 'No'}")
if redis_url:
# Obfuscate password for display
safe_url = redis_url
if 'password=' in safe_url:
parts = safe_url.split(',')
safe_parts = []
for p in parts:
if 'password=' in p.lower():
safe_parts.append('password=****')
else:
safe_parts.append(p)
safe_url = ','.join(safe_parts)
print(f"Connection string: {safe_url}")
print(f"SESSION_TYPE: {os.getenv('SESSION_TYPE')}")
print("-" * 60)
def parse_azure_redis(value):
if not value or '://' in value:
return None
parts = [p.strip() for p in value.split(',') if p.strip()]
host_port = parts[0]
password = None
ssl = True
for p in parts[1:]:
if p.lower().startswith('password='):
password = p.split('=', 1)[1]
elif p.lower().startswith('ssl='):
ssl = p.split('=', 1)[1].strip().lower() in ('true', '1', 'yes')
if ':' in host_port:
host, port = host_port.rsplit(':', 1)
port = int(port)
else:
host = host_port
port = 6380 if ssl else 6379
return host, port, password, ssl
client = None
try:
if redis_url and '://' not in redis_url:
print("Parsing Azure-style connection string...")
host, port, password, ssl = parse_azure_redis(redis_url)
print(f"Host: {host}, Port: {port}, SSL: {ssl}")
client = redis.Redis(
host=host,
port=port,
password=password,
ssl=ssl,
socket_connect_timeout=5,
socket_timeout=5
)
elif redis_url:
print("Using URL invocation...")
client = redis.from_url(redis_url, socket_connect_timeout=5, socket_timeout=5)
else:
print("ERROR: No REDIS_URL found.")
sys.exit(1)
print("Attempting PING...")
start = time.time()
if client.ping():
duration = (time.time() - start) * 1000
print(f"SUCCESS: PING response received in {duration:.2f}ms")
print("Attempting SET/GET...")
client.set('diag_test', 'working', ex=60)
val = client.get('diag_test')
print(f"GET result: {val}")
if val == b'working':
print("SUCCESS: Read/Write verified.")
else:
print("ERROR: Read/Write failed.")
except Exception as e:
print(f"\nCRITICAL ERROR: {str(e)}")
import traceback
traceback.print_exc()
print("="*60)
print("Checking Config (Simulated)")
# Simulate config.py logic
is_azure = os.getenv('WEBSITE_INSTANCE_ID') is not None
print(f"IS_AZURE detected: {is_azure}")
redis_configured = is_azure or (os.getenv('SESSION_TYPE') == 'redis' and redis_url)
print(f"Should use Redis: {redis_configured}")
session_secure = is_azure or (os.getenv('ENVIRONMENT') == 'production')
print(f"SESSION_COOKIE_SECURE: {session_secure}")
print(f"SESSION_COOKIE_SAMESITE: {'None' if session_secure else 'Lax'}")
if session_secure and not is_azure:
print("\nWARNING: Secure cookies are enabled but you might be on HTTP (Local).")
print("This can cause the browser to reject cookies, preventing login.")
print("Ensure ENVIRONMENT is not 'production' locally if not using HTTPS.")
print("="*60)