import json, os, sys, urllib.request, urllib.parse, urllib.error # The generic form of the alertTriggerActions mutation the update_uam_alert_* # commands use with a fixed action: any action the console offers on an alert # (quarantine, kill, remediate, blocklist…) is reachable by passing its id. MUTATION = """mutation TriggerAction($actions: [TriggerActionInput!]!, $filter: OrFilterSelectionInput) { alertTriggerActions(actions: $actions, filter: $filter) { __typename ... on ActionsTriggered { actions { actionId alertCount success { id } failure { id errorType errorMessage } skip { id } } } } }""" 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() action_id = str(inputs.get("action_id") or "").strip() if not alert_id or not action_id: raise ValueError("alert_id and action_id are required") payload = {} action_type = str(inputs.get("action_type") or "").strip() if action_type: payload["type"] = action_type # Free-form payload for the actions that take more than a type; passed # through as-is so a new console action does not need a new command. extra = str(inputs.get("payload_json") or "").strip() if extra: parsed = json.loads(extra) if not isinstance(parsed, dict): raise ValueError("payload_json must be a JSON object") payload.update(parsed) variables = { "actions": [{"id": action_id, "payload": payload}], "filter": {"or": [{"and": [{"fieldId": "id", "stringEqual": {"value": alert_id}}]}]}, } resp = request("POST", base + "/unifiedalerts/graphql", headers, {"query": MUTATION, "variables": variables}) if resp.get("errors"): print(json.dumps({"error": "GraphQL error", "detail": resp["errors"]})) sys.exit(1) print(json.dumps({"alert_id": alert_id, "action_id": action_id, "result": resp.get("data") 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)