| 1 |
# |
| 2 |
# Copyright 2026 Red Hat Inc. |
| 3 |
# |
| 4 |
# This file is part of Ansible |
| 5 |
# |
| 6 |
# Ansible is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# Ansible is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
# |
| 19 |
|
| 20 |
""" |
| 21 |
The action module for splunk_investigation |
| 22 |
""" |
| 23 |
|
| 24 |
from typing import Any, Optional |
| 25 |
|
| 26 |
from ansible.errors import AnsibleActionFail |
| 27 |
from ansible.module_utils.connection import Connection |
| 28 |
from ansible.plugins.action import ActionBase |
| 29 |
from ansible.utils.display import Display |
| 30 |
|
| 31 |
from ansible_collections.splunk.es.plugins.module_utils import dict_utils as utils |
| 32 |
from ansible_collections.splunk.es.plugins.module_utils.investigation import ( |
| 33 |
build_investigation_api_path, |
| 34 |
map_investigation_from_api, |
| 35 |
) |
| 36 |
from ansible_collections.splunk.es.plugins.module_utils.splunk import ( |
| 37 |
SplunkRequest, |
| 38 |
check_argspec, |
| 39 |
) |
| 40 |
from ansible_collections.splunk.es.plugins.module_utils.splunk_utils import ( |
| 41 |
DEFAULT_API_APP, |
| 42 |
DEFAULT_API_NAMESPACE, |
| 43 |
DEFAULT_API_USER, |
| 44 |
DISPOSITION_TO_API, |
| 45 |
STATUS_TO_API, |
| 46 |
) |
| 47 |
from ansible_collections.splunk.es.plugins.modules.splunk_investigation import DOCUMENTATION |
| 48 |
|
| 49 |
|
| 50 |
# Initialize display for debug output |
| 51 |
display = Display() |
| 52 |
|
| 53 |
|
| 54 |
class ActionModule(ActionBase): |
| 55 |
"""Action module for managing Splunk ES investigations.""" |
| 56 |
|
| 57 |
# Fields that can be updated via the main update endpoint (name cannot be updated) |
| 58 |
UPDATABLE_FIELDS = [ |
| 59 |
"description", |
| 60 |
"status", |
| 61 |
"disposition", |
| 62 |
"owner", |
| 63 |
"urgency", |
| 64 |
"sensitivity", |
| 65 |
"investigation_type", |
| 66 |
] |
| 67 |
|
| 68 |
# finding_ids requires a separate API endpoint |
| 69 |
FINDING_IDS_FIELD = "finding_ids" |
| 70 |
|
| 71 |
# Sensitivity mapping: module value (lowercase) -> API value (capitalized) |
| 72 |
SENSITIVITY_TO_API = { |
| 73 |
"white": "White", |
| 74 |
"green": "Green", |
| 75 |
"amber": "Amber", |
| 76 |
"red": "Red", |
| 77 |
"unassigned": "Unassigned", |
| 78 |
} |
| 79 |
|
| 80 |
@staticmethod |
| 81 |
def build_update_path( |
| 82 |
ref_id: str, |
| 83 |
namespace: str = DEFAULT_API_NAMESPACE, |
| 84 |
user: str = DEFAULT_API_USER, |
| 85 |
app: str = DEFAULT_API_APP, |
| 86 |
) -> str: |
| 87 |
"""Build the investigations update API path. |
| 88 |
|
| 89 |
Args: |
| 90 |
ref_id: The investigation reference ID. |
| 91 |
namespace: The namespace portion of the path. Defaults to 'servicesNS'. |
| 92 |
user: The user portion of the path. Defaults to 'nobody'. |
| 93 |
app: The app portion of the path. Defaults to 'missioncontrol'. |
| 94 |
|
| 95 |
Returns: |
| 96 |
The investigations update API path with ref_id. |
| 97 |
""" |
| 98 |
return f"{build_investigation_api_path(namespace, user, app)}/{ref_id}" |
| 99 |
|
| 100 |
@classmethod |
| 101 |
def build_findings_path( |
| 102 |
cls, |
| 103 |
ref_id: str, |
| 104 |
namespace: str = DEFAULT_API_NAMESPACE, |
| 105 |
user: str = DEFAULT_API_USER, |
| 106 |
app: str = DEFAULT_API_APP, |
| 107 |
) -> str: |
| 108 |
"""Build the API path for adding findings to an investigation. |
| 109 |
|
| 110 |
Args: |
| 111 |
ref_id: The investigation reference ID. |
| 112 |
namespace: The namespace portion of the path. Defaults to 'servicesNS'. |
| 113 |
user: The user portion of the path. Defaults to 'nobody'. |
| 114 |
app: The app portion of the path. Defaults to 'missioncontrol'. |
| 115 |
|
| 116 |
Returns: |
| 117 |
The API path for adding findings to the investigation. |
| 118 |
""" |
| 119 |
return f"{cls.build_update_path(ref_id, namespace, user, app)}/findings" |
| 120 |
|
| 121 |
@classmethod |
| 122 |
def map_to_api(cls, investigation: dict[str, Any]) -> dict[str, Any]: |
| 123 |
"""Convert module params to API payload format. |
| 124 |
|
| 125 |
Args: |
| 126 |
investigation: The investigation parameters dictionary. |
| 127 |
|
| 128 |
Returns: |
| 129 |
Dictionary formatted for the Splunk investigations API. |
| 130 |
""" |
| 131 |
res = investigation.copy() |
| 132 |
|
| 133 |
# Handle status conversion to API numeric value |
| 134 |
if "status" in res and res["status"]: |
| 135 |
res["status"] = STATUS_TO_API.get(res["status"], res["status"]) |
| 136 |
|
| 137 |
# Handle disposition conversion to API format |
| 138 |
if "disposition" in res and res["disposition"]: |
| 139 |
res["disposition"] = DISPOSITION_TO_API.get( |
| 140 |
res["disposition"].lower(), |
| 141 |
res["disposition"], |
| 142 |
) |
| 143 |
|
| 144 |
# Handle sensitivity conversion to API format (capitalized) |
| 145 |
if "sensitivity" in res and res["sensitivity"]: |
| 146 |
res["sensitivity"] = cls.SENSITIVITY_TO_API.get( |
| 147 |
res["sensitivity"].lower(), |
| 148 |
res["sensitivity"], |
| 149 |
) |
| 150 |
|
| 151 |
return res |
| 152 |
|
| 153 |
@classmethod |
| 154 |
def map_update_to_api(cls, investigation: dict[str, Any]) -> dict[str, Any]: |
| 155 |
"""Convert module params to API payload format for updating investigations. |
| 156 |
|
| 157 |
Only includes fields that are allowed to be updated. |
| 158 |
|
| 159 |
Args: |
| 160 |
investigation: The investigation parameters dictionary. |
| 161 |
|
| 162 |
Returns: |
| 163 |
Dictionary formatted for the Splunk investigations update API. |
| 164 |
""" |
| 165 |
res = {} |
| 166 |
|
| 167 |
for field in cls.UPDATABLE_FIELDS: |
| 168 |
if field in investigation and investigation[field] is not None: |
| 169 |
value = investigation[field] |
| 170 |
|
| 171 |
# Handle status conversion |
| 172 |
if field == "status": |
| 173 |
value = STATUS_TO_API.get(value, value) |
| 174 |
|
| 175 |
# Handle disposition conversion |
| 176 |
if field == "disposition": |
| 177 |
value = DISPOSITION_TO_API.get(value.lower(), value) |
| 178 |
|
| 179 |
# Handle sensitivity conversion |
| 180 |
if field == "sensitivity": |
| 181 |
value = cls.SENSITIVITY_TO_API.get(value.lower(), value) |
| 182 |
|
| 183 |
res[field] = value |
| 184 |
|
| 185 |
return res |
| 186 |
|
| 187 |
def __init__(self, *args: Any, **kwargs: Any) -> None: |
| 188 |
super().__init__(*args, **kwargs) |
| 189 |
self._result = None |
| 190 |
self.module_name = "investigation" |
| 191 |
self.api_namespace = DEFAULT_API_NAMESPACE |
| 192 |
self.api_user = DEFAULT_API_USER |
| 193 |
self.api_app = DEFAULT_API_APP |
| 194 |
self.api_object = None # Will be built dynamically |
| 195 |
|
| 196 |
def fail_json(self, msg: str) -> None: |
| 197 |
"""Raise an AnsibleActionFail with a cleaned up message. |
| 198 |
|
| 199 |
Args: |
| 200 |
msg: The message for the failure. |
| 201 |
|
| 202 |
Raises: |
| 203 |
AnsibleActionFail: Always raised with the provided message. |
| 204 |
""" |
| 205 |
msg = msg.replace("(basic.py)", self._task.action) |
| 206 |
raise AnsibleActionFail(msg) |
| 207 |
|
| 208 |
def _build_api_path(self) -> str: |
| 209 |
"""Build the investigations API path from configured components. |
| 210 |
|
| 211 |
Returns: |
| 212 |
The complete investigations API path. |
| 213 |
""" |
| 214 |
return build_investigation_api_path(self.api_namespace, self.api_user, self.api_app) |
| 215 |
|
| 216 |
def _configure_api(self) -> None: |
| 217 |
"""Configure API path components from task arguments.""" |
| 218 |
self.api_namespace = self._task.args.get("api_namespace", DEFAULT_API_NAMESPACE) |
| 219 |
self.api_user = self._task.args.get("api_user", DEFAULT_API_USER) |
| 220 |
self.api_app = self._task.args.get("api_app", DEFAULT_API_APP) |
| 221 |
self.api_object = self._build_api_path() |
| 222 |
display.vv(f"splunk_investigation: using API path: {self.api_object}") |
| 223 |
|
| 224 |
def _build_investigation_params(self) -> dict[str, Any]: |
| 225 |
"""Build investigation dictionary from task arguments. |
| 226 |
|
| 227 |
Returns: |
| 228 |
Dictionary containing investigation parameters from task args. |
| 229 |
""" |
| 230 |
investigation = {} |
| 231 |
name = self._task.args.get("name") |
| 232 |
if name: |
| 233 |
investigation["name"] = name |
| 234 |
|
| 235 |
param_keys = [ |
| 236 |
"description", |
| 237 |
"status", |
| 238 |
"disposition", |
| 239 |
"owner", |
| 240 |
"urgency", |
| 241 |
"sensitivity", |
| 242 |
"finding_ids", |
| 243 |
"investigation_type", |
| 244 |
] |
| 245 |
for key in param_keys: |
| 246 |
value = self._task.args.get(key) |
| 247 |
if value is not None: |
| 248 |
investigation[key] = value |
| 249 |
|
| 250 |
return investigation |
| 251 |
|
| 252 |
def _validate_create_params(self, investigation: dict[str, Any]) -> str: |
| 253 |
"""Validate required parameters for creating a new investigation. |
| 254 |
|
| 255 |
Args: |
| 256 |
investigation: The investigation parameters dictionary. |
| 257 |
|
| 258 |
Returns: |
| 259 |
Error message if validation fails, empty string if valid. |
| 260 |
""" |
| 261 |
if "name" not in investigation: |
| 262 |
return "Missing required parameter: name" |
| 263 |
|
| 264 |
return "" |
| 265 |
|
| 266 |
def _set_result_message(self, changed: bool) -> None: |
| 267 |
"""Set the appropriate result message based on check mode and changed status. |
| 268 |
|
| 269 |
Args: |
| 270 |
changed: Whether the operation resulted in changes. |
| 271 |
""" |
| 272 |
if self._task.check_mode: |
| 273 |
self._result["msg"] = ( |
| 274 |
"Check mode: would create/update investigation" |
| 275 |
if changed |
| 276 |
else "Check mode: no changes required" |
| 277 |
) |
| 278 |
else: |
| 279 |
self._result["msg"] = ( |
| 280 |
"Investigation created/updated successfully" if changed else "No changes required" |
| 281 |
) |
| 282 |
|
| 283 |
def get_investigation_by_id( |
| 284 |
self, |
| 285 |
conn_request: SplunkRequest, |
| 286 |
ref_id: str, |
| 287 |
) -> dict[str, Any]: |
| 288 |
"""Get an existing investigation by its reference ID. |
| 289 |
|
| 290 |
Args: |
| 291 |
conn_request: The SplunkRequest instance. |
| 292 |
ref_id: The reference ID (investigation ID) to search for. |
| 293 |
|
| 294 |
Returns: |
| 295 |
The existing investigation if found, empty dict otherwise. |
| 296 |
""" |
| 297 |
display.vv(f"splunk_investigation: getting investigation by ref_id: {ref_id}") |
| 298 |
|
| 299 |
# Use the ids query parameter to filter by investigation ID |
| 300 |
query_params = {"ids": ref_id} |
| 301 |
response = conn_request.get_by_path(self.api_object, query_params=query_params) |
| 302 |
|
| 303 |
search_result = {} |
| 304 |
|
| 305 |
# API returns a list, get the first matching investigation |
| 306 |
if response and isinstance(response, list) and len(response) > 0: |
| 307 |
display.vvv(f"splunk_investigation: raw API response: {response}") |
| 308 |
search_result = map_investigation_from_api(response[0]) |
| 309 |
search_result["investigation_ref_id"] = ref_id |
| 310 |
display.vv(f"splunk_investigation: found existing investigation: {search_result}") |
| 311 |
else: |
| 312 |
display.vv(f"splunk_investigation: no investigation found with ref_id: {ref_id}") |
| 313 |
|
| 314 |
return search_result |
| 315 |
|
| 316 |
def _post_investigation( |
| 317 |
self, |
| 318 |
conn_request: SplunkRequest, |
| 319 |
investigation: dict[str, Any], |
| 320 |
) -> dict[str, Any]: |
| 321 |
"""Send investigation payload to API for creating a new investigation. |
| 322 |
|
| 323 |
Args: |
| 324 |
conn_request: The SplunkRequest instance. |
| 325 |
investigation: The investigation parameters. |
| 326 |
|
| 327 |
Returns: |
| 328 |
Parsed investigation from API response. |
| 329 |
""" |
| 330 |
payload = self.map_to_api(investigation) |
| 331 |
|
| 332 |
display.vvv(f"splunk_investigation: posting to {self.api_object}") |
| 333 |
display.vvv(f"splunk_investigation: payload: {payload}") |
| 334 |
api_response = conn_request.create_update(self.api_object, data=payload, json_payload=True) |
| 335 |
|
| 336 |
after = {} |
| 337 |
if api_response: |
| 338 |
display.vvv(f"splunk_investigation: API response: {api_response}") |
| 339 |
after = map_investigation_from_api(api_response) |
| 340 |
|
| 341 |
return after |
| 342 |
|
| 343 |
def _post_update( |
| 344 |
self, |
| 345 |
conn_request: SplunkRequest, |
| 346 |
ref_id: str, |
| 347 |
investigation: dict[str, Any], |
| 348 |
) -> dict[str, Any]: |
| 349 |
"""Send investigation payload to API and return parsed response. |
| 350 |
|
| 351 |
Args: |
| 352 |
conn_request: The SplunkRequest instance. |
| 353 |
ref_id: The reference ID of the investigation to update. |
| 354 |
investigation: The investigation parameters to update. |
| 355 |
|
| 356 |
Returns: |
| 357 |
The updated investigation parameters. |
| 358 |
""" |
| 359 |
# Build the update API path |
| 360 |
update_url = self.build_update_path( |
| 361 |
ref_id, |
| 362 |
self.api_namespace, |
| 363 |
self.api_user, |
| 364 |
self.api_app, |
| 365 |
) |
| 366 |
|
| 367 |
# Map to update API payload format |
| 368 |
payload = self.map_update_to_api(investigation) |
| 369 |
|
| 370 |
display.vvv(f"splunk_investigation: posting update to {update_url}") |
| 371 |
display.vvv(f"splunk_investigation: update payload: {payload}") |
| 372 |
|
| 373 |
api_response = conn_request.create_update( |
| 374 |
update_url, |
| 375 |
data=payload, |
| 376 |
json_payload=True, |
| 377 |
) |
| 378 |
|
| 379 |
display.vvv(f"splunk_investigation: update API response: {api_response}") |
| 380 |
|
| 381 |
# Return the expected state after update |
| 382 |
return investigation |
| 383 |
|
| 384 |
def _add_findings_to_investigation( |
| 385 |
self, |
| 386 |
conn_request: SplunkRequest, |
| 387 |
ref_id: str, |
| 388 |
finding_ids: list[str], |
| 389 |
) -> None: |
| 390 |
"""Add findings to an existing investigation. |
| 391 |
|
| 392 |
Args: |
| 393 |
conn_request: The SplunkRequest instance. |
| 394 |
ref_id: The reference ID of the investigation. |
| 395 |
finding_ids: List of finding IDs to add to the investigation. |
| 396 |
""" |
| 397 |
findings_url = self.build_findings_path( |
| 398 |
ref_id, |
| 399 |
self.api_namespace, |
| 400 |
self.api_user, |
| 401 |
self.api_app, |
| 402 |
) |
| 403 |
|
| 404 |
# Send finding_ids as JSON body payload |
| 405 |
payload = { |
| 406 |
"finding_ids": finding_ids, |
| 407 |
} |
| 408 |
|
| 409 |
display.vvv(f"splunk_investigation: adding findings to {findings_url}") |
| 410 |
display.vvv(f"splunk_investigation: payload: {payload}") |
| 411 |
|
| 412 |
api_response = conn_request.create_update( |
| 413 |
findings_url, |
| 414 |
data=payload, |
| 415 |
json_payload=True, |
| 416 |
) |
| 417 |
|
| 418 |
display.vvv(f"splunk_investigation: add findings API response: {api_response}") |
| 419 |
|
| 420 |
def create_investigation( |
| 421 |
self, |
| 422 |
conn_request: SplunkRequest, |
| 423 |
investigation: dict[str, Any], |
| 424 |
) -> tuple[dict[str, Any], bool]: |
| 425 |
"""Create a new investigation. |
| 426 |
|
| 427 |
Args: |
| 428 |
conn_request: The SplunkRequest instance. |
| 429 |
investigation: The investigation parameters. |
| 430 |
|
| 431 |
Returns: |
| 432 |
Tuple of (result_dict, changed). |
| 433 |
""" |
| 434 |
name = investigation.get("name", "") |
| 435 |
display.v(f"splunk_investigation: creating new investigation with name: {name}") |
| 436 |
|
| 437 |
if self._task.check_mode: |
| 438 |
display.v("splunk_investigation: check mode - would create investigation") |
| 439 |
return {"before": None, "after": investigation}, True |
| 440 |
|
| 441 |
want_conf = utils.remove_empties(investigation) |
| 442 |
api_response = self._post_investigation(conn_request, want_conf) |
| 443 |
|
| 444 |
# API only returns the GUID on create, so merge input params with response |
| 445 |
# Input params provide the expected state, API response provides the ref_id |
| 446 |
after = want_conf.copy() |
| 447 |
after.update(api_response) |
| 448 |
|
| 449 |
display.v("splunk_investigation: created investigation successfully") |
| 450 |
return {"before": None, "after": after}, True |
| 451 |
|
| 452 |
def _filter_updatable_fields( |
| 453 |
self, |
| 454 |
investigation: dict[str, Any], |
| 455 |
) -> tuple[dict[str, Any], Optional[list[str]]]: |
| 456 |
"""Filter investigation params to only updatable fields. |
| 457 |
|
| 458 |
Args: |
| 459 |
investigation: The investigation parameters dictionary. |
| 460 |
|
| 461 |
Returns: |
| 462 |
Tuple of (filtered_fields, finding_ids). |
| 463 |
""" |
| 464 |
# Extract finding_ids (handled separately via different API) |
| 465 |
finding_ids = investigation.pop(self.FINDING_IDS_FIELD, None) |
| 466 |
if finding_ids: |
| 467 |
display.vv(f"splunk_investigation: will add findings: {finding_ids}") |
| 468 |
|
| 469 |
# Remove name field (cannot be updated) |
| 470 |
if "name" in investigation: |
| 471 |
display.vv("splunk_investigation: ignoring 'name' field - cannot be updated") |
| 472 |
investigation = {k: v for k, v in investigation.items() if k != "name"} |
| 473 |
|
| 474 |
# Filter to only updatable fields |
| 475 |
non_updatable = [k for k in investigation if k not in self.UPDATABLE_FIELDS] |
| 476 |
if non_updatable: |
| 477 |
display.vv(f"splunk_investigation: ignoring non-updatable fields: {non_updatable}") |
| 478 |
investigation = {k: v for k, v in investigation.items() if k in self.UPDATABLE_FIELDS} |
| 479 |
|
| 480 |
return investigation, finding_ids |
| 481 |
|
| 482 |
def _process_field_updates( |
| 483 |
self, |
| 484 |
conn_request: SplunkRequest, |
| 485 |
ref_id: str, |
| 486 |
investigation: dict[str, Any], |
| 487 |
have_conf: dict[str, Any], |
| 488 |
) -> tuple[bool, dict[str, Any]]: |
| 489 |
"""Process regular field updates. |
| 490 |
|
| 491 |
Args: |
| 492 |
conn_request: The SplunkRequest instance. |
| 493 |
ref_id: The investigation reference ID. |
| 494 |
investigation: The filtered investigation parameters. |
| 495 |
have_conf: The existing investigation configuration. |
| 496 |
|
| 497 |
Returns: |
| 498 |
Tuple of (changed, updated_fields). |
| 499 |
""" |
| 500 |
want_conf = utils.remove_empties(investigation) |
| 501 |
have_updatable = {k: have_conf.get(k) for k in self.UPDATABLE_FIELDS if k in have_conf} |
| 502 |
diff = utils.dict_diff(have_updatable, want_conf) |
| 503 |
|
| 504 |
if not diff: |
| 505 |
return False, {} |
| 506 |
|
| 507 |
display.vv(f"splunk_investigation: field changes detected: {diff}") |
| 508 |
|
| 509 |
if not self._task.check_mode: |
| 510 |
self._post_update(conn_request, ref_id, want_conf) |
| 511 |
|
| 512 |
return True, want_conf |
| 513 |
|
| 514 |
def _process_findings_updates( |
| 515 |
self, |
| 516 |
conn_request: SplunkRequest, |
| 517 |
ref_id: str, |
| 518 |
finding_ids: list[str], |
| 519 |
existing_findings: list[str], |
| 520 |
) -> tuple[bool, list[str]]: |
| 521 |
"""Process finding_ids updates. |
| 522 |
|
| 523 |
Args: |
| 524 |
conn_request: The SplunkRequest instance. |
| 525 |
ref_id: The investigation reference ID. |
| 526 |
finding_ids: The desired finding IDs to add. |
| 527 |
existing_findings: The existing finding IDs. |
| 528 |
|
| 529 |
Returns: |
| 530 |
Tuple of (changed, final_findings_list). |
| 531 |
""" |
| 532 |
existing_set = set(existing_findings) |
| 533 |
new_findings = [fid for fid in finding_ids if fid not in existing_set] |
| 534 |
|
| 535 |
if not new_findings: |
| 536 |
display.vv("splunk_investigation: all findings already exist, skipping") |
| 537 |
return False, existing_findings |
| 538 |
|
| 539 |
skipped = [fid for fid in finding_ids if fid in existing_set] |
| 540 |
display.vv(f"splunk_investigation: adding new findings: {new_findings}") |
| 541 |
if skipped: |
| 542 |
display.vv(f"splunk_investigation: skipping existing findings: {skipped}") |
| 543 |
|
| 544 |
if not self._task.check_mode: |
| 545 |
self._add_findings_to_investigation(conn_request, ref_id, new_findings) |
| 546 |
|
| 547 |
return True, existing_findings + new_findings |
| 548 |
|
| 549 |
def update_investigation( |
| 550 |
self, |
| 551 |
conn_request: SplunkRequest, |
| 552 |
ref_id: str, |
| 553 |
investigation: dict[str, Any], |
| 554 |
) -> tuple[dict[str, Any], bool]: |
| 555 |
"""Update an existing investigation by reference ID. |
| 556 |
|
| 557 |
Args: |
| 558 |
conn_request: The SplunkRequest instance. |
| 559 |
ref_id: The reference ID of the investigation to update. |
| 560 |
investigation: The investigation parameters. |
| 561 |
|
| 562 |
Returns: |
| 563 |
Tuple of (result_dict, changed). |
| 564 |
""" |
| 565 |
display.v(f"splunk_investigation: updating investigation with ref_id: {ref_id}") |
| 566 |
|
| 567 |
# Filter to updatable fields and extract finding_ids |
| 568 |
fields, finding_ids = self._filter_updatable_fields(investigation) |
| 569 |
|
| 570 |
if not fields and not finding_ids: |
| 571 |
display.v("splunk_investigation: no updatable fields provided") |
| 572 |
return { |
| 573 |
"before": None, |
| 574 |
"after": None, |
| 575 |
"error": "No updatable fields provided. Name cannot be updated.", |
| 576 |
}, False |
| 577 |
|
| 578 |
# Get existing investigation |
| 579 |
have_conf = self.get_investigation_by_id(conn_request, ref_id) |
| 580 |
if not have_conf: |
| 581 |
display.v(f"splunk_investigation: investigation with ref_id {ref_id} not found") |
| 582 |
return { |
| 583 |
"before": None, |
| 584 |
"after": None, |
| 585 |
"error": f"Investigation with ref_id '{ref_id}' not found", |
| 586 |
}, False |
| 587 |
|
| 588 |
display.vv(f"splunk_investigation: existing investigation found: {have_conf}") |
| 589 |
|
| 590 |
changed = False |
| 591 |
after_conf = have_conf.copy() |
| 592 |
|
| 593 |
# Process field updates |
| 594 |
if fields: |
| 595 |
field_changed, updated = self._process_field_updates( |
| 596 |
conn_request, |
| 597 |
ref_id, |
| 598 |
fields, |
| 599 |
have_conf, |
| 600 |
) |
| 601 |
if field_changed: |
| 602 |
changed = True |
| 603 |
after_conf.update(updated) |
| 604 |
|
| 605 |
# Process findings updates |
| 606 |
if finding_ids: |
| 607 |
existing = have_conf.get(self.FINDING_IDS_FIELD, []) or [] |
| 608 |
findings_changed, final_findings = self._process_findings_updates( |
| 609 |
conn_request, |
| 610 |
ref_id, |
| 611 |
finding_ids, |
| 612 |
existing, |
| 613 |
) |
| 614 |
if findings_changed: |
| 615 |
changed = True |
| 616 |
after_conf[self.FINDING_IDS_FIELD] = final_findings |
| 617 |
|
| 618 |
# Return result |
| 619 |
if changed: |
| 620 |
action = "would update" if self._task.check_mode else "updated" |
| 621 |
display.v(f"splunk_investigation: {action} investigation successfully") |
| 622 |
return {"before": have_conf, "after": after_conf}, True |
| 623 |
|
| 624 |
display.v("splunk_investigation: no changes needed") |
| 625 |
return {"before": have_conf, "after": have_conf}, False |
| 626 |
|
| 627 |
def _handle_update( |
| 628 |
self, |
| 629 |
conn_request: SplunkRequest, |
| 630 |
ref_id: str, |
| 631 |
investigation: dict[str, Any], |
| 632 |
) -> bool: |
| 633 |
"""Handle update operation for an existing investigation. |
| 634 |
|
| 635 |
Args: |
| 636 |
conn_request: The SplunkRequest instance. |
| 637 |
ref_id: The reference ID of the investigation to update. |
| 638 |
investigation: The investigation parameters. |
| 639 |
|
| 640 |
Returns: |
| 641 |
True if operation completed successfully, False if error occurred. |
| 642 |
""" |
| 643 |
display.v("splunk_investigation: ref_id provided, will update existing investigation") |
| 644 |
investigation_result, changed = self.update_investigation( |
| 645 |
conn_request, |
| 646 |
ref_id, |
| 647 |
investigation, |
| 648 |
) |
| 649 |
|
| 650 |
if "error" in investigation_result: |
| 651 |
self._result["failed"] = True |
| 652 |
self._result["msg"] = investigation_result["error"] |
| 653 |
return False |
| 654 |
|
| 655 |
self._result[self.module_name] = investigation_result |
| 656 |
self._result["changed"] = changed |
| 657 |
return True |
| 658 |
|
| 659 |
def _handle_create(self, conn_request: SplunkRequest, investigation: dict[str, Any]) -> bool: |
| 660 |
"""Handle create operation for a new investigation. |
| 661 |
|
| 662 |
Args: |
| 663 |
conn_request: The SplunkRequest instance. |
| 664 |
investigation: The investigation parameters. |
| 665 |
|
| 666 |
Returns: |
| 667 |
True if operation completed successfully, False if validation failed. |
| 668 |
""" |
| 669 |
display.v("splunk_investigation: no ref_id provided, will create new investigation") |
| 670 |
|
| 671 |
error_msg = self._validate_create_params(investigation) |
| 672 |
if error_msg: |
| 673 |
self._result["failed"] = True |
| 674 |
self._result["msg"] = error_msg |
| 675 |
display.v(f"splunk_investigation: {error_msg}") |
| 676 |
return False |
| 677 |
|
| 678 |
investigation_result, changed = self.create_investigation(conn_request, investigation) |
| 679 |
self._result[self.module_name] = investigation_result |
| 680 |
self._result["changed"] = changed |
| 681 |
return True |
| 682 |
|
| 683 |
def run(self, tmp=None, task_vars=None): |
| 684 |
"""Execute the action module.""" |
| 685 |
self._supports_check_mode = True |
| 686 |
self._result = super().run(tmp, task_vars) |
| 687 |
|
| 688 |
display.v("splunk_investigation: starting module execution") |
| 689 |
|
| 690 |
# Validate arguments |
| 691 |
if not check_argspec(self, self._result, DOCUMENTATION): |
| 692 |
display.v( |
| 693 |
f"splunk_investigation: argument validation failed: {self._result.get('msg')}", |
| 694 |
) |
| 695 |
return self._result |
| 696 |
|
| 697 |
# Initialize result structure |
| 698 |
self._result[self.module_name] = {} |
| 699 |
self._result["changed"] = False |
| 700 |
|
| 701 |
self._configure_api() |
| 702 |
|
| 703 |
ref_id = self._task.args.get("investigation_ref_id") |
| 704 |
investigation = self._build_investigation_params() |
| 705 |
|
| 706 |
display.vv(f"splunk_investigation: investigation parameters: {investigation}") |
| 707 |
display.vv(f"splunk_investigation: investigation_ref_id: {ref_id}") |
| 708 |
|
| 709 |
# Setup connection |
| 710 |
conn = Connection(self._connection.socket_path) |
| 711 |
conn_request = SplunkRequest( |
| 712 |
action_module=self, |
| 713 |
connection=conn, |
| 714 |
not_rest_data_keys=["investigation_ref_id", "api_namespace", "api_user", "api_app"], |
| 715 |
) |
| 716 |
|
| 717 |
if ref_id: |
| 718 |
if not self._handle_update(conn_request, ref_id, investigation): |
| 719 |
return self._result |
| 720 |
else: |
| 721 |
if not self._handle_create(conn_request, investigation): |
| 722 |
return self._result |
| 723 |
|
| 724 |
self._set_result_message(self._result["changed"]) |
| 725 |
display.v(f"splunk_investigation: module completed with changed={self._result['changed']}") |
| 726 |
return self._result |