50 行
1.7 KiB
Python
50 行
1.7 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any, Dict, List
|
|
|
|
from lab.config import ADVISORIES_DIR, QUEUE_PATH
|
|
from lab.utils import load_json_dir, read_json, write_json
|
|
|
|
|
|
def load_queue() -> Dict[str, Any]:
|
|
return read_json(QUEUE_PATH, default={"items": []}) or {"items": []}
|
|
|
|
|
|
def save_queue(queue: Dict[str, Any]) -> None:
|
|
write_json(QUEUE_PATH, queue)
|
|
|
|
|
|
def enqueue_items(items: List[Dict[str, Any]]) -> Dict[str, Any]:
|
|
queue = load_queue()
|
|
existing = {item["advisory_id"] for item in queue.get("items", [])}
|
|
added = 0
|
|
for item in items:
|
|
if item["advisory_id"] in existing:
|
|
continue
|
|
queue.setdefault("items", []).append(item)
|
|
existing.add(item["advisory_id"])
|
|
added += 1
|
|
save_queue(queue)
|
|
return {"queued": len(queue["items"]), "added": added}
|
|
|
|
|
|
def enqueue_from_registry(only_hotlane: bool = False, limit: int = 50) -> Dict[str, Any]:
|
|
advisories = load_json_dir(ADVISORIES_DIR)
|
|
items = []
|
|
for advisory in advisories:
|
|
if only_hotlane:
|
|
hot = advisory.get("exploit_status") in {"known_exploited", "active_exploitation", "in_the_wild"}
|
|
if not hot and not (advisory.get("cvss_score") or 0) >= 8.8 and advisory.get("severity") != "critical":
|
|
continue
|
|
items.append({"advisory_id": advisory["canonical_id"], "system_id": advisory["system_id"], "priority": "hotlane" if only_hotlane else "default"})
|
|
return enqueue_items(items[:limit])
|
|
|
|
|
|
def dequeue(limit: int = 10) -> List[Dict[str, Any]]:
|
|
queue = load_queue()
|
|
items = queue.get("items", [])
|
|
selected = items[:limit]
|
|
queue["items"] = items[limit:]
|
|
save_queue(queue)
|
|
return selected
|