import json, os, sys, urllib.request, urllib.parse, urllib.error # The single-alert view of the UAM model. get_unified_alerts reads the list # shape; this adds what the list cannot carry per alert: the indicators behind # the detection, their MITRE mapping, the observables and the sibling alerts. QUERY = """query GetAlertDetails($id: ID!) { alert(id: $id) { id externalId name description severity status classification confidenceLevel analystVerdict result detectedAt createdAt updatedAt firstSeenAt lastSeenAt storylineId attackSurfaces noteExists asset { id name osType osVersion agentVersion lastLoggedInUser } assignee { userId fullName email } detectionSource { vendor product engine } process { cmdLine parentName username file { name path md5 sha1 sha256 size } } indicators { uid type message severity eventTime attacks { tactic { name uid } technique { name uid } } observables { name value type typeName } } observables { name value type typeName } relatedAlerts { id name } } }""" 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 main(): secrets = json.loads(os.environ.get("INTEGRATION_SECRETS", "{}")) inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}")) base = secrets.get("url", "").rstrip("/") + "/web/api/v2.1" headers = { "Authorization": "ApiToken " + secrets.get("api_token", ""), "Accept": "application/json", "Content-Type": "application/json", } alert_id = str(inputs.get("alert_id") or "").strip() if not alert_id: raise ValueError("alert_id is required") body = {"query": QUERY, "variables": {"id": alert_id}} resp = request("POST", base + "/unifiedalerts/graphql", headers, body) # GraphQL answers 200 with an errors array; reported as an error rather than # printed as an empty alert, which would read as "this alert has no detail". if resp.get("errors"): print(json.dumps({"error": "GraphQL error", "detail": resp["errors"]})) sys.exit(1) print(json.dumps((resp.get("data") or {}).get("alert") or {})) 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)