File size: 4,490 Bytes
611e2c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python3
"""
Quick script to verify Supabase rules storage is working.
Run this to check if rules are being saved to Supabase.
"""

import os
import sys
from pathlib import Path

# Load environment variables from .env file
from dotenv import load_dotenv
load_dotenv()

# Add backend to path
backend_dir = Path(__file__).resolve().parent
sys.path.insert(0, str(backend_dir))

from backend.api.storage.rules_store import RulesStore


def main():
    print("=" * 60)
    print("Supabase Rules Storage Verification")
    print("=" * 60)
    
    # Check environment variables
    supabase_url = os.getenv("SUPABASE_URL")
    supabase_key = os.getenv("SUPABASE_SERVICE_KEY")
    
    print("\n1. Checking Environment Variables:")
    if supabase_url:
        print(f"   βœ… SUPABASE_URL is set: {supabase_url[:50]}...")
    else:
        print("   ❌ SUPABASE_URL is not set")
        print("      Add it to your .env file: SUPABASE_URL=https://your-project.supabase.co")
    
    if supabase_key:
        print(f"   βœ… SUPABASE_SERVICE_KEY is set: {supabase_key[:20]}...")
    else:
        print("   ❌ SUPABASE_SERVICE_KEY is not set")
        print("      Add it to your .env file: SUPABASE_SERVICE_KEY=your_service_role_key")
    
    if not supabase_url or not supabase_key:
        print("\n⚠️  Supabase credentials are missing!")
        print("   Rules will be saved to SQLite instead.")
        print("   See SUPABASE_SETUP.md for setup instructions.")
        print("\n   To use Supabase:")
        print("   1. Add SUPABASE_URL and SUPABASE_SERVICE_KEY to your .env file")
        print("   2. Create the admin_rules table in Supabase (see supabase_admin_rules_table.sql)")
        print("   3. Restart your application")
        return
    
    # Initialize RulesStore
    print("\n2. Initializing RulesStore:")
    try:
        store = RulesStore(auto_create_table=True)
        print(f"   βœ… RulesStore initialized")
        print(f"   πŸ“¦ Using Supabase: {store.use_supabase}")
        
        if not store.use_supabase:
            print("   ⚠️  RulesStore is using SQLite, not Supabase!")
            print("   Check that:")
            print("   - SUPABASE_URL and SUPABASE_SERVICE_KEY are correct")
            print("   - Supabase Python client is installed: pip install supabase")
            return
        
    except Exception as e:
        print(f"   ❌ Failed to initialize RulesStore: {e}")
        return
    
    # Test adding a rule
    print("\n3. Testing Rule Storage:")
    test_tenant = "test_verification"
    test_rule = "Test rule for Supabase verification"
    
    try:
        # Delete test rule if it exists
        store.delete_rule(test_tenant, test_rule)
        
        # Add test rule
        success = store.add_rule(
            test_tenant,
            test_rule,
            severity="medium",
            description="Verification test rule"
        )
        
        if success:
            print(f"   βœ… Successfully added test rule to Supabase")
        else:
            print(f"   ❌ Failed to add rule to Supabase")
            return
        
        # Retrieve rule
        rules = store.get_rules(test_tenant)
        if test_rule in rules:
            print(f"   βœ… Successfully retrieved rule from Supabase")
            print(f"   πŸ“‹ Found {len(rules)} rule(s) for tenant '{test_tenant}'")
        else:
            print(f"   ❌ Rule not found after adding")
            return
        
        # Get detailed rules
        detailed_rules = store.get_rules_detailed(test_tenant)
        if detailed_rules:
            print(f"   βœ… Successfully retrieved detailed rules")
            for rule in detailed_rules:
                if rule['rule'] == test_rule:
                    print(f"   πŸ“ Rule details:")
                    print(f"      - Pattern: {rule.get('pattern', 'N/A')}")
                    print(f"      - Severity: {rule.get('severity', 'N/A')}")
                    print(f"      - Enabled: {rule.get('enabled', 'N/A')}")
        
        # Cleanup test rule
        store.delete_rule(test_tenant, test_rule)
        print(f"   🧹 Cleaned up test rule")
        
    except Exception as e:
        print(f"   ❌ Error during test: {e}")
        import traceback
        traceback.print_exc()
        return
    
    print("\n" + "=" * 60)
    print("βœ… All checks passed! Rules are being saved to Supabase.")
    print("=" * 60)


if __name__ == "__main__":
    main()