ff461464fd
The alerts PATCH answers 200 to a refused update (errors, resources_affected 0) and the command reported it as a success. It now exits in error with Falcon's reasons and the ids sent; an empty update is refused up front. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VBnLYwQbUCaf5vpv77N14Q
93 lines
4.1 KiB
Python
93 lines
4.1 KiB
Python
import json, os, sys, urllib.request, urllib.parse, urllib.error
|
|
|
|
SECRETS = json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
|
INPUTS = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
|
BASE = SECRETS.get("base_url", "https://api.crowdstrike.com").rstrip("/")
|
|
|
|
|
|
def token():
|
|
data = urllib.parse.urlencode({"client_id": SECRETS.get("client_id", ""), "client_secret": SECRETS.get("client_secret", "")}).encode()
|
|
req = urllib.request.Request(BASE + "/oauth2/token", data=data, headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
|
with urllib.request.urlopen(req, timeout=30) as r:
|
|
return json.loads(r.read()).get("access_token", "")
|
|
|
|
|
|
def call(method, path, tok, params=None, body=None):
|
|
url = BASE + path
|
|
if params:
|
|
# params is a list of (key, value) tuples to allow repeated keys (e.g. ids)
|
|
url += "?" + urllib.parse.urlencode(params)
|
|
data = json.dumps(body).encode() if body is not None else None
|
|
headers = {"Authorization": "Bearer " + tok, "Accept": "application/json"}
|
|
if data is not None:
|
|
headers["Content-Type"] = "application/json"
|
|
req = urllib.request.Request(url, data=data, headers=headers, method=method)
|
|
with urllib.request.urlopen(req, timeout=90) as r:
|
|
raw = r.read()
|
|
return json.loads(raw) if raw else {}
|
|
|
|
|
|
def csv(v):
|
|
return [x.strip() for x in str(v or "").split(",") if x.strip()]
|
|
|
|
|
|
VERDICTS = ("true_positive", "false_positive", "ignored")
|
|
|
|
|
|
def main():
|
|
tok = token()
|
|
ids = csv(INPUTS.get("ids"))
|
|
verdict = str(INPUTS.get("verdict") or "").strip()
|
|
if verdict and verdict not in VERDICTS:
|
|
raise ValueError("verdict must be one of " + ", ".join(VERDICTS) + ", got " + verdict)
|
|
action_params = []
|
|
if verdict:
|
|
# The console's Resolution is read from these tags, and add_tag never
|
|
# replaces one: drop the other two first, or a false positive turned
|
|
# true positive would show both. Removing a tag the alert does not
|
|
# carry is not an error worth stopping for — the add below is the call
|
|
# that has to land.
|
|
for other in VERDICTS:
|
|
if other == verdict:
|
|
continue
|
|
try:
|
|
call("PATCH", "/alerts/entities/alerts/v3", tok,
|
|
body={"action_parameters": [{"name": "remove_tag", "value": other}], "composite_ids": ids})
|
|
except urllib.error.HTTPError:
|
|
pass
|
|
action_params.append({"name": "add_tag", "value": verdict})
|
|
if INPUTS.get("status"):
|
|
action_params.append({"name": "update_status", "value": str(INPUTS["status"])})
|
|
if INPUTS.get("comment"):
|
|
action_params.append({"name": "append_comment", "value": str(INPUTS["comment"])})
|
|
if INPUTS.get("assign_to_uuid"):
|
|
action_params.append({"name": "assign_to_uuid", "value": str(INPUTS["assign_to_uuid"])})
|
|
if not action_params:
|
|
raise ValueError("nothing to change: give a status, a verdict, a comment or an assignee")
|
|
body = {"action_parameters": action_params, "composite_ids": ids}
|
|
result = call("PATCH", "/alerts/entities/alerts/v3", tok, body=body)
|
|
# Falcon answers 200 to an update it refused: the reasons sit in errors and
|
|
# meta.writes.resources_affected stays at 0. Passing that on as a success
|
|
# left an incident closed in Riposte and still open in the console.
|
|
affected = ((result.get("meta") or {}).get("writes") or {}).get("resources_affected")
|
|
if result.get("errors") or affected == 0:
|
|
print(json.dumps({
|
|
"error": "Falcon updated no alert" if affected == 0 else "Falcon refused part of the update",
|
|
"resources_affected": affected,
|
|
"errors": result.get("errors") or [],
|
|
# The PATCH takes composite ids (<cid>:<id>), not the alert's bare id.
|
|
"composite_ids": ids,
|
|
}))
|
|
sys.exit(1)
|
|
print(json.dumps(result))
|
|
|
|
|
|
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)
|