Files
riposte-marketplace/integrations/harfanglab/scripts/get_alerts.py
T
f3nris 45df291eeb fix(harfanglab): drop the 50 KB filters catalogue from list outputs (1.7.1)
HarfangLab's alert and threat list endpoints echo, on every call, the
catalogue of every filterable field — about 50 KB, unrelated to the
results. get_security_events, get_alerts and get_threats now drop it
before printing, so a fetch carries the alerts and little else.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CXFMuDDLohs4qnL12mBJoP
2026-10-07 12:42:32 +00:00

71 lines
2.6 KiB
Python

import json, os, sys, urllib.request, urllib.parse, urllib.error
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
if isinstance(v, bool):
v = "true" if v else "false"
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",
}
ids = csv(inputs.get("alert_ids"))
if ids:
results = [request("GET", base + "/api/data/alert/alert/Alert/" + i + "/", headers) for i in ids]
print(json.dumps({"count": len(results), "results": results}))
return
params = {
"ordering": inputs.get("ordering") or "-alert_time",
"limit": int(inputs.get("limit") or 100),
"offset": int(inputs.get("offset") or 0),
"search": inputs.get("search"),
"agent.hostname": inputs.get("hostname"),
"agent.agentid": inputs.get("agent_id"),
"threat_key": inputs.get("threat_id"),
"rule_name": inputs.get("rule_name"),
"level": ",".join(csv(inputs.get("level"))) or None,
"status": ",".join(csv(inputs.get("status"))) or None,
"alert_type": ",".join(csv(inputs.get("alert_type"))) or None,
"alert_time__gte": inputs.get("from_date"),
"alert_time__lte": inputs.get("to_date"),
}
out = request("GET", base + "/api/data/alert/alert/Alert/" + qs(params), headers)
# The list endpoints echo the catalogue of filterable fields (~50 KB) on
# every call; it says nothing about the results, so it is not passed on.
out.pop("filters", None)
print(json.dumps(out))
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)