import json, os, sys, urllib.request, urllib.parse, urllib.error # Notes on a unified alert are their own mutation — write_threat_note covers the # REST threat object, which is a different record from a UAM alert. MUTATION = """mutation AddNote($alertId: ID!, $text: String!) { addAlertNote(alertId: $alertId, text: $text) { data { id text createdAt author { userId fullName } } } }""" 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=30) 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() text = str(inputs.get("note_text") or "") if not alert_id or not text.strip(): raise ValueError("alert_id and note_text are required") body = {"query": MUTATION, "variables": {"alertId": alert_id, "text": text}} resp = request("POST", base + "/unifiedalerts/graphql", headers, body) # Without this the playbook would read a successful write on a note the # console refused. if resp.get("errors"): print(json.dumps({"error": "GraphQL error", "detail": resp["errors"]})) sys.exit(1) print(json.dumps((resp.get("data") or {}).get("addAlertNote") 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)