Files
riposte-marketplace/integrations/harfanglab/scripts/get_threats.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

57 lines
1.9 KiB
Python

import json, os, sys, urllib.request, urllib.parse, urllib.error
SEVERITIES = ["low", "medium", "high", "critical"]
def request(method, url, headers):
req = urllib.request.Request(url, headers=headers, method=method)
with urllib.request.urlopen(req, timeout=90) 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 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",
}
min_sev = str(inputs.get("min_severity") or "low").lower()
level = ",".join(SEVERITIES[SEVERITIES.index(min_sev):]) if min_sev in SEVERITIES else ",".join(SEVERITIES)
params = {
"ordering": "-last_seen",
"level": level,
"limit": int(inputs.get("limit") or 100),
"offset": 0,
"status": inputs.get("status"),
"creation_date__gte": inputs.get("created_after"),
}
url = base + "/api/data/alert/alert/Threat/" + qs(params)
out = request("GET", url, 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)