-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutm_storage_backends_oss.py
More file actions
594 lines (511 loc) · 21.7 KB
/
utm_storage_backends_oss.py
File metadata and controls
594 lines (511 loc) · 21.7 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
#!/usr/bin/env python3
"""
UTM Protocol Storage Backends - Open Source Edition
Multi-storage backend system for UTM Protocol (open-source version).
No Cloudflare dependencies - uses local storage only.
"""
import time
import json
import os
import pickle
import logging
import sqlite3
from typing import Dict, List, Any, Optional, Union
from abc import ABC, abstractmethod
from dataclasses import dataclass, asdict
import hashlib
import base64
import asyncio
import threading
import queue
# Quantum-safe cryptography imports (optional for OSS)
try:
import oqs
QUANTUM_SAFE_AVAILABLE = True
except ImportError:
QUANTUM_SAFE_AVAILABLE = False
@dataclass
class UTMStorageConfig:
"""Configuration for UTM storage backends (OSS version)"""
backend_type: str # local, file, direct
primary_backend: str
fallback_backends: List[str]
local_config: Optional[Dict[str, Any]] = None
file_config: Optional[Dict[str, Any]] = None
direct_config: Optional[Dict[str, Any]] = None
encryption_enabled: bool = True
compression_enabled: bool = True
quantum_safe_enabled: bool = False # Disabled by default in OSS
version: str = "1.0.0-oss"
@dataclass
class UTMStorageResult:
"""Result of UTM storage operation"""
success: bool
data: Optional[Any] = None
error: Optional[str] = None
backend_used: Optional[str] = None
timestamp: Optional[float] = None
metadata: Optional[Dict[str, Any]] = None
class UTMStorageBackend(ABC):
"""Abstract base class for UTM storage backends"""
def __init__(self, config: Dict[str, Any]):
self.config = config
self.logger = logging.getLogger(__name__)
@abstractmethod
def store(self, key: str, data: Any, metadata: Optional[Dict[str, Any]] = None) -> UTMStorageResult:
"""Store data with the given key"""
pass
@abstractmethod
def retrieve(self, key: str) -> UTMStorageResult:
"""Retrieve data by key"""
pass
@abstractmethod
def delete(self, key: str) -> UTMStorageResult:
"""Delete data by key"""
pass
@abstractmethod
def list_keys(self, prefix: str = "") -> UTMStorageResult:
"""List all keys with optional prefix"""
pass
def _serialize_data(self, data: Any) -> str:
"""Serialize data for storage"""
if self.config.get('compression_enabled', True):
return base64.b64encode(pickle.dumps(data)).decode('utf-8')
else:
return json.dumps(data, default=str)
def _deserialize_data(self, serialized_data: str) -> Any:
"""Deserialize data from storage"""
if self.config.get('compression_enabled', True):
return pickle.loads(base64.b64decode(serialized_data))
else:
return json.loads(serialized_data)
class LocalStorageBackend(UTMStorageBackend):
"""SQLite-based local storage backend for UTM tokens"""
def __init__(self, config: Dict[str, Any]):
super().__init__(config)
self.db_path = config.get('db_path', 'utm_tokens.db')
self._init_database()
def _init_database(self):
"""Initialize SQLite database with UTM token tables"""
try:
with sqlite3.connect(self.db_path) as conn:
conn.execute('''
CREATE TABLE IF NOT EXISTS utm_tokens (
token_id TEXT PRIMARY KEY,
token_data TEXT NOT NULL,
metadata TEXT,
created_at REAL NOT NULL,
expires_at REAL,
last_accessed REAL,
access_count INTEGER DEFAULT 0
)
''')
conn.execute('''
CREATE INDEX IF NOT EXISTS idx_expires_at
ON utm_tokens(expires_at)
''')
conn.execute('''
CREATE INDEX IF NOT EXISTS idx_created_at
ON utm_tokens(created_at)
''')
conn.commit()
self.logger.info(f"Initialized UTM token database: {self.db_path}")
except Exception as e:
self.logger.error(f"Failed to initialize database: {e}")
raise
def store(self, key: str, data: Any, metadata: Optional[Dict[str, Any]] = None) -> UTMStorageResult:
"""Store UTM token in SQLite database"""
try:
serialized_data = self._serialize_data(data)
serialized_metadata = json.dumps(metadata) if metadata else None
# Calculate expiration time
expires_at = time.time() + data.get('ttl', 86400) if isinstance(data, dict) else time.time() + 86400
with sqlite3.connect(self.db_path) as conn:
conn.execute('''
INSERT OR REPLACE INTO utm_tokens
(token_id, token_data, metadata, created_at, expires_at, last_accessed, access_count)
VALUES (?, ?, ?, ?, ?, ?, ?)
''', (key, serialized_data, serialized_metadata, time.time(), expires_at, time.time(), 0))
conn.commit()
return UTMStorageResult(
success=True,
data=data,
backend_used="sqlite_local",
timestamp=time.time(),
metadata=metadata
)
except Exception as e:
self.logger.error(f"Failed to store token {key}: {e}")
return UTMStorageResult(
success=False,
error=str(e),
backend_used="sqlite_local"
)
def retrieve(self, key: str) -> UTMStorageResult:
"""Retrieve UTM token from SQLite database"""
try:
with sqlite3.connect(self.db_path) as conn:
cursor = conn.execute('''
SELECT token_data, metadata, expires_at
FROM utm_tokens
WHERE token_id = ?
''', (key,))
row = cursor.fetchone()
if not row:
return UTMStorageResult(
success=False,
error="Token not found",
backend_used="sqlite_local"
)
token_data, metadata_str, expires_at = row
# Check expiration
if expires_at and time.time() > expires_at:
return UTMStorageResult(
success=False,
error="Token expired",
backend_used="sqlite_local"
)
# Update access statistics
conn.execute('''
UPDATE utm_tokens
SET last_accessed = ?, access_count = access_count + 1
WHERE token_id = ?
''', (time.time(), key))
conn.commit()
data = self._deserialize_data(token_data)
metadata = json.loads(metadata_str) if metadata_str else None
return UTMStorageResult(
success=True,
data=data,
backend_used="sqlite_local",
timestamp=time.time(),
metadata=metadata
)
except Exception as e:
self.logger.error(f"Failed to retrieve token {key}: {e}")
return UTMStorageResult(
success=False,
error=str(e),
backend_used="sqlite_local"
)
def delete(self, key: str) -> UTMStorageResult:
"""Delete UTM token from SQLite database"""
try:
with sqlite3.connect(self.db_path) as conn:
cursor = conn.execute('DELETE FROM utm_tokens WHERE token_id = ?', (key,))
conn.commit()
if cursor.rowcount == 0:
return UTMStorageResult(
success=False,
error="Token not found",
backend_used="sqlite_local"
)
return UTMStorageResult(
success=True,
backend_used="sqlite_local",
timestamp=time.time()
)
except Exception as e:
self.logger.error(f"Failed to delete token {key}: {e}")
return UTMStorageResult(
success=False,
error=str(e),
backend_used="sqlite_local"
)
def list_keys(self, prefix: str = "") -> UTMStorageResult:
"""List UTM token keys with optional prefix"""
try:
with sqlite3.connect(self.db_path) as conn:
if prefix:
cursor = conn.execute('''
SELECT token_id, created_at, expires_at
FROM utm_tokens
WHERE token_id LIKE ?
ORDER BY created_at DESC
''', (f"{prefix}%",))
else:
cursor = conn.execute('''
SELECT token_id, created_at, expires_at
FROM utm_tokens
ORDER BY created_at DESC
''')
rows = cursor.fetchall()
keys = [{"key": row[0], "created_at": row[1], "expires_at": row[2]} for row in rows]
return UTMStorageResult(
success=True,
data=keys,
backend_used="sqlite_local",
timestamp=time.time()
)
except Exception as e:
self.logger.error(f"Failed to list keys: {e}")
return UTMStorageResult(
success=False,
error=str(e),
backend_used="sqlite_local"
)
def cleanup_expired(self) -> UTMStorageResult:
"""Remove expired tokens from database"""
try:
with sqlite3.connect(self.db_path) as conn:
cursor = conn.execute('DELETE FROM utm_tokens WHERE expires_at < ?', (time.time(),))
deleted_count = cursor.rowcount
conn.commit()
return UTMStorageResult(
success=True,
data={"deleted_count": deleted_count},
backend_used="sqlite_local",
timestamp=time.time()
)
except Exception as e:
self.logger.error(f"Failed to cleanup expired tokens: {e}")
return UTMStorageResult(
success=False,
error=str(e),
backend_used="sqlite_local"
)
class FileStorageBackend(UTMStorageBackend):
"""File-based storage backend for UTM tokens (JSON files)"""
def __init__(self, config: Dict[str, Any]):
super().__init__(config)
self.storage_dir = config.get('storage_dir', './utm_tokens')
self._ensure_storage_dir()
def _ensure_storage_dir(self):
"""Ensure storage directory exists"""
try:
os.makedirs(self.storage_dir, exist_ok=True)
self.logger.info(f"Using file storage directory: {self.storage_dir}")
except Exception as e:
self.logger.error(f"Failed to create storage directory: {e}")
raise
def _get_file_path(self, key: str) -> str:
"""Get file path for a given key"""
# Use hash of key for filename to avoid filesystem issues
key_hash = hashlib.sha256(key.encode()).hexdigest()
return os.path.join(self.storage_dir, f"{key_hash}.json")
def store(self, key: str, data: Any, metadata: Optional[Dict[str, Any]] = None) -> UTMStorageResult:
"""Store UTM token as JSON file"""
try:
file_path = self._get_file_path(key)
token_record = {
"key": key,
"data": data,
"metadata": metadata,
"created_at": time.time(),
"expires_at": time.time() + (data.get('ttl', 86400) if isinstance(data, dict) else 86400)
}
with open(file_path, 'w') as f:
json.dump(token_record, f, indent=2, default=str)
return UTMStorageResult(
success=True,
data=data,
backend_used="file_json",
timestamp=time.time(),
metadata=metadata
)
except Exception as e:
self.logger.error(f"Failed to store token {key}: {e}")
return UTMStorageResult(
success=False,
error=str(e),
backend_used="file_json"
)
def retrieve(self, key: str) -> UTMStorageResult:
"""Retrieve UTM token from JSON file"""
try:
file_path = self._get_file_path(key)
if not os.path.exists(file_path):
return UTMStorageResult(
success=False,
error="Token not found",
backend_used="file_json"
)
with open(file_path, 'r') as f:
token_record = json.load(f)
# Check expiration
if time.time() > token_record.get('expires_at', 0):
return UTMStorageResult(
success=False,
error="Token expired",
backend_used="file_json"
)
return UTMStorageResult(
success=True,
data=token_record['data'],
backend_used="file_json",
timestamp=time.time(),
metadata=token_record.get('metadata')
)
except Exception as e:
self.logger.error(f"Failed to retrieve token {key}: {e}")
return UTMStorageResult(
success=False,
error=str(e),
backend_used="file_json"
)
def delete(self, key: str) -> UTMStorageResult:
"""Delete UTM token JSON file"""
try:
file_path = self._get_file_path(key)
if not os.path.exists(file_path):
return UTMStorageResult(
success=False,
error="Token not found",
backend_used="file_json"
)
os.remove(file_path)
return UTMStorageResult(
success=True,
backend_used="file_json",
timestamp=time.time()
)
except Exception as e:
self.logger.error(f"Failed to delete token {key}: {e}")
return UTMStorageResult(
success=False,
error=str(e),
backend_used="file_json"
)
def list_keys(self, prefix: str = "") -> UTMStorageResult:
"""List UTM token keys from JSON files"""
try:
keys = []
for filename in os.listdir(self.storage_dir):
if filename.endswith('.json'):
file_path = os.path.join(self.storage_dir, filename)
try:
with open(file_path, 'r') as f:
token_record = json.load(f)
key = token_record.get('key', '')
if not prefix or key.startswith(prefix):
keys.append({
"key": key,
"created_at": token_record.get('created_at'),
"expires_at": token_record.get('expires_at')
})
except Exception as e:
self.logger.warning(f"Failed to read token file {filename}: {e}")
continue
# Sort by creation time
keys.sort(key=lambda x: x.get('created_at', 0), reverse=True)
return UTMStorageResult(
success=True,
data=keys,
backend_used="file_json",
timestamp=time.time()
)
except Exception as e:
self.logger.error(f"Failed to list keys: {e}")
return UTMStorageResult(
success=False,
error=str(e),
backend_used="file_json"
)
class UTMStorageManager:
"""UTM Storage Manager for open-source edition"""
def __init__(self, config: UTMStorageConfig):
self.config = config
self.logger = logging.getLogger(__name__)
self.backend = self._create_backend()
self.fallback_backends = [self._create_fallback_backend(backend_type) for backend_type in config.fallback_backends]
def _create_backend(self) -> UTMStorageBackend:
"""Create primary storage backend"""
if self.config.primary_backend == "local":
return LocalStorageBackend(self.config.local_config or {})
elif self.config.primary_backend == "file":
return FileStorageBackend(self.config.file_config or {})
else:
raise ValueError(f"Unsupported primary backend: {self.config.primary_backend}")
def _create_fallback_backend(self, backend_type: str) -> UTMStorageBackend:
"""Create fallback storage backend"""
if backend_type == "local":
return LocalStorageBackend(self.config.local_config or {})
elif backend_type == "file":
return FileStorageBackend(self.config.file_config or {})
else:
raise ValueError(f"Unsupported fallback backend: {backend_type}")
def store_utm_token(self, token_id: str, token_data: Dict[str, Any]) -> UTMStorageResult:
"""Store UTM token with fallback support"""
result = self.backend.store(token_id, token_data)
if not result.success and self.fallback_backends:
self.logger.warning(f"Primary backend failed, trying fallback: {result.error}")
for fallback in self.fallback_backends:
result = fallback.store(token_id, token_data)
if result.success:
break
return result
def retrieve_utm_token(self, token_id: str) -> UTMStorageResult:
"""Retrieve UTM token with fallback support"""
result = self.backend.retrieve(token_id)
if not result.success and self.fallback_backends:
self.logger.warning(f"Primary backend failed, trying fallback: {result.error}")
for fallback in self.fallback_backends:
result = fallback.retrieve(token_id)
if result.success:
break
return result
def delete_utm_token(self, token_id: str) -> UTMStorageResult:
"""Delete UTM token from all backends"""
results = []
# Try primary backend
result = self.backend.delete(token_id)
results.append(result)
# Try fallback backends
for fallback in self.fallback_backends:
result = fallback.delete(token_id)
results.append(result)
# Return success if any backend succeeded
for result in results:
if result.success:
return result
return UTMStorageResult(
success=False,
error="Failed to delete from all backends",
backend_used="all"
)
def list_utm_tokens(self, prefix: str = "") -> UTMStorageResult:
"""List UTM tokens from primary backend"""
return self.backend.list_keys(prefix)
def cleanup_expired_tokens(self) -> UTMStorageResult:
"""Cleanup expired tokens from all backends"""
results = []
# Try primary backend
if hasattr(self.backend, 'cleanup_expired'):
result = self.backend.cleanup_expired()
results.append(result)
# Try fallback backends
for fallback in self.fallback_backends:
if hasattr(fallback, 'cleanup_expired'):
result = fallback.cleanup_expired()
results.append(result)
# Aggregate results
total_deleted = sum(r.data.get('deleted_count', 0) for r in results if r.success and r.data)
return UTMStorageResult(
success=True,
data={"total_deleted": total_deleted, "backend_results": results},
backend_used="all",
timestamp=time.time()
)
def create_utm_storage_manager(config: UTMStorageConfig) -> UTMStorageManager:
"""Factory function to create UTM Storage Manager"""
return UTMStorageManager(config)
def create_oss_config(
primary_backend: str = "local",
fallback_backends: List[str] = None,
db_path: str = "utm_tokens.db",
storage_dir: str = "./utm_tokens"
) -> UTMStorageConfig:
"""Create configuration for open-source UTM storage"""
if fallback_backends is None:
fallback_backends = ["file"]
return UTMStorageConfig(
backend_type="oss",
primary_backend=primary_backend,
fallback_backends=fallback_backends,
local_config={"db_path": db_path},
file_config={"storage_dir": storage_dir},
encryption_enabled=True,
compression_enabled=True,
quantum_safe_enabled=False, # Disabled in OSS
version="1.0.0-oss"
)