Spaces:
Paused
Paused
Upload core/utils.py with huggingface_hub
Browse files- core/utils.py +22 -11
core/utils.py
CHANGED
|
@@ -2,7 +2,7 @@ import json
|
|
| 2 |
|
| 3 |
from fastapi import Request
|
| 4 |
|
| 5 |
-
from
|
| 6 |
from core.logging import logger
|
| 7 |
|
| 8 |
|
|
@@ -16,7 +16,9 @@ def safe_call(func, default=None, log_errors=True):
|
|
| 16 |
return func()
|
| 17 |
except Exception as e:
|
| 18 |
if log_errors:
|
| 19 |
-
logger.debug(
|
|
|
|
|
|
|
| 20 |
return default
|
| 21 |
|
| 22 |
|
|
@@ -46,16 +48,23 @@ def log_security_event(
|
|
| 46 |
}
|
| 47 |
)
|
| 48 |
|
| 49 |
-
# Log to audit service
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
# Also log to application log
|
| 58 |
-
logger.warning(
|
|
|
|
|
|
|
| 59 |
|
| 60 |
except Exception as e:
|
| 61 |
logger.error(f"Failed to log security event: {e}")
|
|
@@ -71,7 +80,9 @@ def log_auth_failure(user_id: str, reason: str, request: Request = None):
|
|
| 71 |
)
|
| 72 |
|
| 73 |
|
| 74 |
-
def log_suspicious_activity(
|
|
|
|
|
|
|
| 75 |
"""Log suspicious activities"""
|
| 76 |
log_security_event(
|
| 77 |
f"SUSPICIOUS_{activity_type.upper()}",
|
|
|
|
| 2 |
|
| 3 |
from fastapi import Request
|
| 4 |
|
| 5 |
+
from core.infrastructure.registry import kernel_registry
|
| 6 |
from core.logging import logger
|
| 7 |
|
| 8 |
|
|
|
|
| 16 |
return func()
|
| 17 |
except Exception as e:
|
| 18 |
if log_errors:
|
| 19 |
+
logger.debug(
|
| 20 |
+
f"Safe call failed for {func.__name__ if hasattr(func, '__name__') else 'function'}: {e}"
|
| 21 |
+
)
|
| 22 |
return default
|
| 23 |
|
| 24 |
|
|
|
|
| 48 |
}
|
| 49 |
)
|
| 50 |
|
| 51 |
+
# Log to audit service via kernel registry
|
| 52 |
+
if kernel_registry.audit_service:
|
| 53 |
+
kernel_registry.audit_service.log_security_event(
|
| 54 |
+
user_id=user_id,
|
| 55 |
+
action=event_type,
|
| 56 |
+
resource_type="security",
|
| 57 |
+
details=json.dumps(audit_details),
|
| 58 |
+
)
|
| 59 |
+
else:
|
| 60 |
+
logger.warning(
|
| 61 |
+
f"Audit service not registered in kernel. Skipping security log: {event_type}"
|
| 62 |
+
)
|
| 63 |
|
| 64 |
# Also log to application log
|
| 65 |
+
logger.warning(
|
| 66 |
+
f"Security Event: {event_type} - User: {user_id} - Details: {details}"
|
| 67 |
+
)
|
| 68 |
|
| 69 |
except Exception as e:
|
| 70 |
logger.error(f"Failed to log security event: {e}")
|
|
|
|
| 80 |
)
|
| 81 |
|
| 82 |
|
| 83 |
+
def log_suspicious_activity(
|
| 84 |
+
activity_type: str, user_id: str, details: dict, request: Request = None
|
| 85 |
+
):
|
| 86 |
"""Log suspicious activities"""
|
| 87 |
log_security_event(
|
| 88 |
f"SUSPICIOUS_{activity_type.upper()}",
|