feat(harfanglab): fetch a threat's security events (1.7.0)
Since HarfangLab merged alerts and security events, a threat's events are the alerts whose threat_key is the threat id — the filter the vendor's XSOAR integration uses. get_alerts could filter on it but stops at one page of 100; the new threat_security_events command pages through up to max_results and returns the threat's total with a truncated flag. threat_status, threat_level and alert_tag offer their status and level as drop-downs, and threat_status now reads as the place to close a threat or mark it a false positive, HarfangLab's only verdict. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -0,0 +1,76 @@
|
||||
import json, os, sys, urllib.request, urllib.parse, urllib.error
|
||||
|
||||
PAGE = 500
|
||||
|
||||
|
||||
def request(method, url, headers, body=None):
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, headers=headers, method=method)
|
||||
with urllib.request.urlopen(req, timeout=60) as resp:
|
||||
raw = resp.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def qs(params):
|
||||
clean = {}
|
||||
for k, v in params.items():
|
||||
if v is None or v == "" or v == []:
|
||||
continue
|
||||
clean[k] = v
|
||||
return ("?" + urllib.parse.urlencode(clean)) if clean else ""
|
||||
|
||||
|
||||
def csv(v):
|
||||
return [x.strip() for x in str(v or "").split(",") if x.strip()]
|
||||
|
||||
|
||||
def main():
|
||||
secrets = json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
base = secrets.get("url", "").rstrip("/")
|
||||
headers = {
|
||||
"Authorization": "Token " + secrets.get("api_token", ""),
|
||||
"Accept": "application/json",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
threat_id = str(inputs.get("threat_id") or "").strip()
|
||||
if not threat_id:
|
||||
raise ValueError("threat_id is required")
|
||||
max_results = int(inputs.get("max_results") or 1000)
|
||||
# Since HarfangLab merged alerts and security events, a threat's events are
|
||||
# the alerts whose threat_key is the threat id (same filter as the
|
||||
# vendor's own XSOAR integration).
|
||||
params = {
|
||||
"threat_key": threat_id,
|
||||
"ordering": "-alert_time",
|
||||
"status": ",".join(csv(inputs.get("status"))) or None,
|
||||
"level": ",".join(csv(inputs.get("level"))) or None,
|
||||
"offset": 0,
|
||||
}
|
||||
results, total = [], 0
|
||||
while len(results) < max_results:
|
||||
params["limit"] = min(PAGE, max_results - len(results))
|
||||
page = request("GET", base + "/api/data/alert/alert/Alert/" + qs(params), headers)
|
||||
batch = page.get("results") or []
|
||||
total = page.get("count", total)
|
||||
results.extend(batch)
|
||||
if not batch or not page.get("next"):
|
||||
break
|
||||
params["offset"] += len(batch)
|
||||
print(json.dumps({
|
||||
"threat_id": threat_id,
|
||||
"count": len(results),
|
||||
"total": total,
|
||||
"truncated": len(results) < total,
|
||||
"results": results,
|
||||
}))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
Reference in New Issue
Block a user