feat(microsoft-sentinel): new Microsoft Sentinel integration
19 commands (Azure Resource Manager API): incident ingestion + CRUD, comments, related alerts/entities/relations, watchlists, and threat indicators. Azure AD OAuth 2.0 client-credentials, stdlib-only. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
import json, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
API_VERSION = "2023-11-01"
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _token():
|
||||
cfg = _cfg()
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "client_credentials",
|
||||
"client_id": str(cfg.get("client_id") or ""),
|
||||
"client_secret": str(cfg.get("client_secret") or ""),
|
||||
"scope": "https://management.azure.com/.default",
|
||||
}).encode("utf-8")
|
||||
url = "https://login.microsoftonline.com/" + str(cfg.get("tenant_id") or "") + "/oauth2/v2.0/token"
|
||||
req = urllib.request.Request(url, data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"},
|
||||
method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("Token request failed: " + json.dumps(tok))
|
||||
return tok["access_token"]
|
||||
|
||||
|
||||
def _si_base():
|
||||
cfg = _cfg()
|
||||
for key in ("subscription_id", "resource_group", "workspace_name"):
|
||||
if not str(cfg.get(key) or ""):
|
||||
raise Exception(key + " is not set")
|
||||
return ("https://management.azure.com/subscriptions/" + str(cfg["subscription_id"])
|
||||
+ "/resourceGroups/" + str(cfg["resource_group"])
|
||||
+ "/providers/Microsoft.OperationalInsights/workspaces/" + str(cfg["workspace_name"])
|
||||
+ "/providers/Microsoft.SecurityInsights")
|
||||
|
||||
|
||||
def request(method, path, params=None, body=None, token=None):
|
||||
url = _si_base() + path
|
||||
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
||||
q["api-version"] = API_VERSION
|
||||
url += ("&" if "?" in url else "?") + urllib.parse.urlencode(q)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
headers = {"Accept": "application/json", "Authorization": "Bearer " + (token or _token())}
|
||||
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 {}
|
||||
|
||||
|
||||
q = lambda v: urllib.parse.quote(str(v), safe="")
|
||||
|
||||
|
||||
def main():
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
incident_id = inputs.get("incident_id")
|
||||
if not incident_id:
|
||||
raise Exception("incident_id is required")
|
||||
|
||||
existing = request("GET", "/incidents/" + q(incident_id))
|
||||
props = dict(existing.get("properties", {}))
|
||||
|
||||
if inputs.get("title"):
|
||||
props["title"] = inputs["title"]
|
||||
if inputs.get("status"):
|
||||
props["status"] = inputs["status"]
|
||||
if inputs.get("severity"):
|
||||
props["severity"] = inputs["severity"]
|
||||
if inputs.get("description"):
|
||||
props["description"] = inputs["description"]
|
||||
if inputs.get("classification"):
|
||||
props["classification"] = inputs["classification"]
|
||||
if inputs.get("classification_reason"):
|
||||
props["classificationReason"] = inputs["classification_reason"]
|
||||
if inputs.get("assigned_to"):
|
||||
props["owner"] = {"assignedTo": inputs["assigned_to"]}
|
||||
|
||||
body = {"properties": props, "etag": existing.get("etag")}
|
||||
res = request("PUT", "/incidents/" + q(incident_id), body=body)
|
||||
print(json.dumps(res))
|
||||
|
||||
|
||||
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)
|
||||
Reference in New Issue
Block a user