Spaces:
Paused
Paused
File size: 5,466 Bytes
f3bcb28 | 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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | #!/usr/bin/env python3
"""
Generate test data for TensorWall dashboard.
This script makes HTTP requests to the gateway to create sample traces
in the database for testing the frontend.
"""
import requests
import random
import time
from datetime import datetime
BASE_URL = "http://localhost:8000"
API_KEY = "gw_load_test_abcdef123456"
# Test scenarios
# NOTE: Only use registered apps/features!
# Check with: SELECT app_id FROM applications; SELECT feature_id, app_id FROM features;
APPS = ["test-app"] # Only test-app is registered in the database
FEATURES = ["default"] # Only 'default' feature is registered for test-app
MODELS = ["mock-gpt"] # Use mock-gpt for faster testing
USERS = [
"alice@example.com",
"bob@example.com",
"charlie@example.com",
"diana@example.com",
"anonymous",
]
ENVIRONMENTS = ["development", "staging", "production"]
# Normal prompts (should be allowed)
NORMAL_PROMPTS = [
"Hello, how are you?",
"Write a Python function to calculate fibonacci",
"Summarize this article about AI",
"Translate to French: Good morning",
"What is machine learning?",
"Help me debug this code",
"Generate a creative story",
"Explain quantum computing",
"What are best practices for REST APIs?",
"How do I optimize database queries?",
]
# Risky prompts (should be blocked)
RISKY_PROMPTS = [
"SELECT * FROM users WHERE password = 'admin'", # SQL injection
"My credit card is 4532-1234-5678-9010", # PII
"<script>alert('XSS')</script>", # XSS
"rm -rf / --no-preserve-root", # Dangerous command
"'; DROP TABLE users; --", # SQL injection
]
def make_request(
prompt: str, app: str, feature: str, model: str, user: str, env: str
) -> dict:
"""Make a chat completion request to the gateway."""
payload = {
"model": model,
"messages": [{"role": "user", "content": prompt}],
"contract": {
"app_id": app,
"feature": feature,
"action": "generate",
"environment": env,
},
}
headers = {
"Content-Type": "application/json",
"X-API-Key": API_KEY,
}
try:
response = requests.post(
f"{BASE_URL}/v1/chat/completions", json=payload, headers=headers, timeout=10
)
return {
"status": response.status_code,
"success": response.status_code == 200,
"data": (
response.json() if response.status_code in [200, 403, 400] else None
),
}
except Exception as e:
return {"status": 500, "success": False, "error": str(e)}
def generate_normal_requests(count: int = 50):
"""Generate normal requests that should be allowed."""
print(f"\nπ’ Generating {count} normal requests...")
success_count = 0
for i in range(count):
prompt = random.choice(NORMAL_PROMPTS)
app = random.choice(APPS)
feature = random.choice(FEATURES)
model = random.choice(MODELS)
user = random.choice(USERS)
env = random.choice(ENVIRONMENTS)
result = make_request(prompt, app, feature, model, user, env)
if result["success"]:
success_count += 1
print(
f" β [{i + 1}/{count}] {app}/{feature} - {model} - {result['status']}"
)
else:
print(
f" β [{i + 1}/{count}] {app}/{feature} - ERROR: {result.get('error', result['status'])}"
)
time.sleep(0.1) # Small delay to avoid overwhelming the server
print(f"\nβ
Created {success_count}/{count} normal requests")
def generate_risky_requests(count: int = 10):
"""Generate risky requests that should be blocked."""
print(f"\nπ΄ Generating {count} risky requests (should be blocked)...")
blocked_count = 0
for i in range(count):
prompt = random.choice(RISKY_PROMPTS)
app = random.choice(APPS)
feature = random.choice(FEATURES)
model = "mock-gpt" # Use mock for risky requests
user = "hacker@example.com"
env = "production"
result = make_request(prompt, app, feature, model, user, env)
# For risky requests, we expect 403 or 400
if result["status"] in [403, 400]:
blocked_count += 1
print(f" β [{i + 1}/{count}] BLOCKED - {prompt[:50]}...")
elif result["status"] == 200:
print(
f" β [{i + 1}/{count}] ALLOWED (should be blocked!) - {prompt[:50]}..."
)
else:
print(
f" β [{i + 1}/{count}] ERROR: {result.get('error', result['status'])}"
)
time.sleep(0.1)
print(f"\nβ
Blocked {blocked_count}/{count} risky requests")
def main():
print("=" * 70)
print("TensorWall Test Data Generator")
print("=" * 70)
print(f"Target: {BASE_URL}")
print(f"Time: {datetime.now().isoformat()}")
print("=" * 70)
# Generate normal requests
generate_normal_requests(count=100)
# Generate risky requests
generate_risky_requests(count=20)
print("\n" + "=" * 70)
print("β
Test data generation complete!")
print("=" * 70)
print("\nYou can now:")
print("1. Visit http://localhost:3000 to see the dashboard")
print("2. Check the audit logs and metrics")
print("3. Verify that blocking reasons are populated")
if __name__ == "__main__":
main()
|