-
Notifications
You must be signed in to change notification settings - Fork 0
fix(api): add security sentinel ingest compatibility endpoint #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| import os | ||||||||||||||||||||||
| import platform | ||||||||||||||||||||||
| import datetime as dt | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if platform.system() == 'Darwin': | ||||||||||||||||||||||
| os.environ['TOKENIZERS_PARALLELISM'] = 'false' | ||||||||||||||||||||||
|
|
@@ -82,6 +83,13 @@ class StatusResponse(BaseModel): | |||||||||||||||||||||
| loaded_repositories: List[Dict[str, Any]] = Field(default_factory=list) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| class SecurityEventIngestRequest(BaseModel): | ||||||||||||||||||||||
| """Compatibility payload for OmniLore security-sentinel ingest calls.""" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| event: Dict[str, Any] = Field(default_factory=dict) | ||||||||||||||||||||||
| tenant_context: Optional[Dict[str, Any]] = Field(default_factory=dict) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| # Initialize FastAPI app | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| @asynccontextmanager | ||||||||||||||||||||||
|
|
@@ -112,6 +120,11 @@ async def lifespan(app: FastAPI): | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| # Global FastCode instance | ||||||||||||||||||||||
| fastcode_instance: Optional[FastCode] = None | ||||||||||||||||||||||
| security_event_buffer: list[Dict[str, Any]] = [] | ||||||||||||||||||||||
| SECURITY_EVENT_BUFFER_LIMIT = max( | ||||||||||||||||||||||
| 10, | ||||||||||||||||||||||
| int(os.getenv("FASTCODE_SECURITY_EVENT_BUFFER_LIMIT", "500")), | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| # Setup logging | ||||||||||||||||||||||
| log_dir = Path("./logs") | ||||||||||||||||||||||
|
|
@@ -165,6 +178,53 @@ async def health_check(): | |||||||||||||||||||||
| "repo_loaded": fastcode_instance.repo_loaded, | ||||||||||||||||||||||
| "repo_indexed": fastcode_instance.repo_indexed, | ||||||||||||||||||||||
| "multi_repo_mode": fastcode_instance.multi_repo_mode, | ||||||||||||||||||||||
| "security_ingest_enabled": True, | ||||||||||||||||||||||
| "security_event_buffer_size": len(security_event_buffer), | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| @app.post("/ingest") | ||||||||||||||||||||||
| async def ingest_security_event(request: SecurityEventIngestRequest): | ||||||||||||||||||||||
| """ | ||||||||||||||||||||||
| Security Sentinel compatibility endpoint. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| OmniLore white-label tooling posts security events here when configured with | ||||||||||||||||||||||
| OMNILORE_SECURITY_SENTINEL_URL=http://127.0.0.1:8001. | ||||||||||||||||||||||
| """ | ||||||||||||||||||||||
| fastcode = _ensure_fastcode_initialized() | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| event = request.event or {} | ||||||||||||||||||||||
| tenant_context = request.tenant_context or {} | ||||||||||||||||||||||
| record = { | ||||||||||||||||||||||
| "received_at": dt.datetime.now(dt.timezone.utc).isoformat(), | ||||||||||||||||||||||
| "event": _safe_jsonable(event), | ||||||||||||||||||||||
| "tenant_context": _safe_jsonable(tenant_context), | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| security_event_buffer.append(record) | ||||||||||||||||||||||
| if len(security_event_buffer) > SECURITY_EVENT_BUFFER_LIMIT: | ||||||||||||||||||||||
| security_event_buffer.pop(0) | ||||||||||||||||||||||
|
Comment on lines
+203
to
+205
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| event_type = ( | ||||||||||||||||||||||
| event.get("type") | ||||||||||||||||||||||
| or event.get("event") | ||||||||||||||||||||||
| or event.get("name") | ||||||||||||||||||||||
| or "unknown" | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
| tenant_id = tenant_context.get("tenant_id", "unknown") | ||||||||||||||||||||||
| logger.warning( | ||||||||||||||||||||||
| "Security ingest accepted (compat): event_type=%s tenant_id=%s", | ||||||||||||||||||||||
| event_type, | ||||||||||||||||||||||
| tenant_id, | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
|
Comment on lines
+214
to
+218
|
||||||||||||||||||||||
| logger.warning( | |
| "Security ingest accepted (compat): event_type=%s tenant_id=%s", | |
| event_type, | |
| tenant_id, | |
| ) | |
| log_message = "Security ingest accepted (compat): event_type=%s tenant_id=%s" | |
| if event_type == "unknown" or tenant_id == "unknown": | |
| logger.warning(log_message, event_type, tenant_id) | |
| else: | |
| logger.info(log_message, event_type, tenant_id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SECURITY_EVENT_BUFFER_LIMIT is computed with int(os.getenv(...)). If FASTCODE_SECURITY_EVENT_BUFFER_LIMIT is set to a non-integer value, this will raise ValueError at import time and prevent the API from starting. Consider parsing defensively (try/except with a fallback) and optionally clamping to a reasonable maximum to avoid accidental huge memory usage.