| 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_response_plan_execution |
| 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.response_plan_execution import ( |
| 32 |
map_applied_response_plan_from_api, |
| 33 |
) |
| 34 |
from ansible_collections.splunk.es.plugins.module_utils.splunk import ( |
| 35 |
SplunkRequest, |
| 36 |
check_argspec, |
| 37 |
) |
| 38 |
from ansible_collections.splunk.es.plugins.module_utils.splunk_utils import ( |
| 39 |
DEFAULT_API_APP, |
| 40 |
DEFAULT_API_NAMESPACE, |
| 41 |
DEFAULT_API_USER, |
| 42 |
get_api_config_from_args, |
| 43 |
is_uuid, |
| 44 |
) |
| 45 |
from ansible_collections.splunk.es.plugins.modules.splunk_response_plan_execution import ( |
| 46 |
DOCUMENTATION, |
| 47 |
) |
| 48 |
|
| 49 |
|
| 50 |
# Initialize display for debug output |
| 51 |
display = Display() |
| 52 |
|
| 53 |
# Task status mappings: module value -> API value (for sending to API) |
| 54 |
TASK_STATUS_TO_API = { |
| 55 |
"started": "Started", |
| 56 |
"ended": "Ended", |
| 57 |
"reopened": "Reopened", |
| 58 |
"pending": "Pending", |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
class ActionModule(ActionBase): |
| 63 |
"""Action module for managing Splunk ES response plan execution.""" |
| 64 |
|
| 65 |
def __init__(self, *args: Any, **kwargs: Any) -> None: |
| 66 |
super().__init__(*args, **kwargs) |
| 67 |
self._result: dict[str, Any] = {} |
| 68 |
self.module_name = "response_plan_execution" |
| 69 |
self.api_namespace = DEFAULT_API_NAMESPACE |
| 70 |
self.api_user = DEFAULT_API_USER |
| 71 |
self.api_app = DEFAULT_API_APP |
| 72 |
|
| 73 |
def fail_json(self, msg: str) -> None: |
| 74 |
"""Raise an AnsibleActionFail with a cleaned up message. |
| 75 |
|
| 76 |
Args: |
| 77 |
msg: The message for the failure. |
| 78 |
|
| 79 |
Raises: |
| 80 |
AnsibleActionFail: Always raised with the provided message. |
| 81 |
""" |
| 82 |
msg = msg.replace("(basic.py)", self._task.action) |
| 83 |
raise AnsibleActionFail(msg) |
| 84 |
|
| 85 |
def _configure_api(self) -> None: |
| 86 |
"""Configure API path components from task arguments.""" |
| 87 |
self.api_namespace, self.api_user, self.api_app = get_api_config_from_args( |
| 88 |
self._task.args, |
| 89 |
) |
| 90 |
|
| 91 |
def _build_response_plans_path(self, investigation_id: str) -> str: |
| 92 |
"""Build the API path for incident response plans. |
| 93 |
|
| 94 |
Args: |
| 95 |
investigation_id: The investigation/incident UUID. |
| 96 |
|
| 97 |
Returns: |
| 98 |
The complete API path for incident response plans. |
| 99 |
""" |
| 100 |
return ( |
| 101 |
f"{self.api_namespace}/{self.api_user}/{self.api_app}" |
| 102 |
f"/v1/incidents/{investigation_id}/responseplans" |
| 103 |
) |
| 104 |
|
| 105 |
def _build_response_plan_path(self, investigation_id: str, applied_plan_id: str) -> str: |
| 106 |
"""Build the API path for a specific applied response plan. |
| 107 |
|
| 108 |
Args: |
| 109 |
investigation_id: The investigation/incident UUID. |
| 110 |
applied_plan_id: The applied response plan instance ID. |
| 111 |
|
| 112 |
Returns: |
| 113 |
The API path for the specific applied response plan. |
| 114 |
""" |
| 115 |
return f"{self._build_response_plans_path(investigation_id)}/{applied_plan_id}" |
| 116 |
|
| 117 |
def _build_task_path( |
| 118 |
self, |
| 119 |
investigation_id: str, |
| 120 |
applied_plan_id: str, |
| 121 |
phase_id: str, |
| 122 |
task_id: str, |
| 123 |
) -> str: |
| 124 |
"""Build the API path for a specific task within an applied response plan. |
| 125 |
|
| 126 |
Args: |
| 127 |
investigation_id: The investigation/incident UUID. |
| 128 |
applied_plan_id: The applied response plan instance ID. |
| 129 |
phase_id: The phase ID containing the task. |
| 130 |
task_id: The task ID. |
| 131 |
|
| 132 |
Returns: |
| 133 |
The API path for the specific task. |
| 134 |
""" |
| 135 |
plan_path = self._build_response_plan_path(investigation_id, applied_plan_id) |
| 136 |
return f"{plan_path}/phase/{phase_id}/tasks/{task_id}" |
| 137 |
|
| 138 |
def _build_templates_path(self) -> str: |
| 139 |
"""Build the API path for response templates (for name-to-ID lookup). |
| 140 |
|
| 141 |
Returns: |
| 142 |
The complete API path for response templates. |
| 143 |
""" |
| 144 |
return f"{self.api_namespace}/{self.api_user}/{self.api_app}/v1/responsetemplates" |
| 145 |
|
| 146 |
def _find_phase_by_name( |
| 147 |
self, |
| 148 |
phases: list[dict[str, Any]], |
| 149 |
phase_name: str, |
| 150 |
) -> Optional[dict[str, Any]]: |
| 151 |
"""Find a phase by name within a list of phases. |
| 152 |
|
| 153 |
Args: |
| 154 |
phases: List of phase dictionaries. |
| 155 |
phase_name: The phase name to search for. |
| 156 |
|
| 157 |
Returns: |
| 158 |
The matching phase dictionary, or None if not found. |
| 159 |
""" |
| 160 |
for phase in phases: |
| 161 |
if phase.get("name") == phase_name: |
| 162 |
return phase |
| 163 |
return None |
| 164 |
|
| 165 |
def _find_task_by_name( |
| 166 |
self, |
| 167 |
tasks: list[dict[str, Any]], |
| 168 |
task_name: str, |
| 169 |
) -> Optional[dict[str, Any]]: |
| 170 |
"""Find a task by name within a list of tasks. |
| 171 |
|
| 172 |
Args: |
| 173 |
tasks: List of task dictionaries. |
| 174 |
task_name: The task name to search for. |
| 175 |
|
| 176 |
Returns: |
| 177 |
The matching task dictionary, or None if not found. |
| 178 |
""" |
| 179 |
for task in tasks: |
| 180 |
if task.get("name") == task_name: |
| 181 |
return task |
| 182 |
return None |
| 183 |
|
| 184 |
def _get_response_templates( |
| 185 |
self, |
| 186 |
conn_request: SplunkRequest, |
| 187 |
) -> list[dict[str, Any]]: |
| 188 |
"""Fetch all response plan templates from the API. |
| 189 |
|
| 190 |
Args: |
| 191 |
conn_request: The SplunkRequest instance. |
| 192 |
|
| 193 |
Returns: |
| 194 |
List of response plan templates. |
| 195 |
""" |
| 196 |
templates_path = self._build_templates_path() |
| 197 |
response = conn_request.get_by_path(templates_path) |
| 198 |
if not response or "items" not in response: |
| 199 |
return [] |
| 200 |
return response.get("items", []) |
| 201 |
|
| 202 |
def _get_template_name_by_id( |
| 203 |
self, |
| 204 |
templates: list[dict[str, Any]], |
| 205 |
template_id: str, |
| 206 |
) -> Optional[str]: |
| 207 |
"""Look up a response plan template name by its ID. |
| 208 |
|
| 209 |
Args: |
| 210 |
templates: List of response plan templates. |
| 211 |
template_id: The template UUID to look up. |
| 212 |
|
| 213 |
Returns: |
| 214 |
The template name, or None if not found. |
| 215 |
""" |
| 216 |
for template in templates: |
| 217 |
if template.get("id") == template_id: |
| 218 |
return template.get("name") |
| 219 |
return None |
| 220 |
|
| 221 |
def _get_template_id_by_name( |
| 222 |
self, |
| 223 |
templates: list[dict[str, Any]], |
| 224 |
template_name: str, |
| 225 |
) -> Optional[str]: |
| 226 |
"""Look up a response plan template ID by its name. |
| 227 |
|
| 228 |
Args: |
| 229 |
templates: List of response plan templates. |
| 230 |
template_name: The template name to look up. |
| 231 |
|
| 232 |
Returns: |
| 233 |
The template ID, or None if not found. |
| 234 |
""" |
| 235 |
for template in templates: |
| 236 |
if template.get("name") == template_name: |
| 237 |
return template.get("id") |
| 238 |
return None |
| 239 |
|
| 240 |
def _get_applied_response_plans( |
| 241 |
self, |
| 242 |
conn_request: SplunkRequest, |
| 243 |
investigation_id: str, |
| 244 |
) -> list[dict[str, Any]]: |
| 245 |
"""Get all response plans applied to an investigation. |
| 246 |
|
| 247 |
Args: |
| 248 |
conn_request: The SplunkRequest instance. |
| 249 |
investigation_id: The investigation UUID. |
| 250 |
|
| 251 |
Returns: |
| 252 |
List of applied response plans. |
| 253 |
""" |
| 254 |
api_path = ( |
| 255 |
f"{self.api_namespace}/{self.api_user}/{self.api_app}" |
| 256 |
f"/v1/incidents/{investigation_id}" |
| 257 |
) |
| 258 |
display.vvv(f"splunk_response_plan_execution: GET {api_path}") |
| 259 |
|
| 260 |
response = conn_request.get_by_path(api_path) |
| 261 |
if not response: |
| 262 |
return [] |
| 263 |
|
| 264 |
# Extract response_plans from the incident details |
| 265 |
response_plans = response.get("response_plans") |
| 266 |
if response_plans is None: |
| 267 |
return [] |
| 268 |
return response_plans |
| 269 |
|
| 270 |
def _find_applied_plan_by_name( |
| 271 |
self, |
| 272 |
applied_plans: list[dict[str, Any]], |
| 273 |
plan_name: str, |
| 274 |
) -> Optional[dict[str, Any]]: |
| 275 |
"""Find an applied response plan by its name. |
| 276 |
|
| 277 |
Note: The GET /v1/incidents/{id} response doesn't include source_template_id |
| 278 |
in the applied plans, so we match by name instead. |
| 279 |
|
| 280 |
Args: |
| 281 |
applied_plans: List of applied response plans. |
| 282 |
plan_name: The response plan name to match. |
| 283 |
|
| 284 |
Returns: |
| 285 |
The matching applied plan, or None if not found. |
| 286 |
""" |
| 287 |
for plan in applied_plans: |
| 288 |
if plan.get("name") == plan_name: |
| 289 |
return plan |
| 290 |
return None |
| 291 |
|
| 292 |
def _apply_response_plan( |
| 293 |
self, |
| 294 |
conn_request: SplunkRequest, |
| 295 |
investigation_id: str, |
| 296 |
template_id: str, |
| 297 |
) -> dict[str, Any]: |
| 298 |
"""Apply a response plan to an investigation. |
| 299 |
|
| 300 |
Args: |
| 301 |
conn_request: The SplunkRequest instance. |
| 302 |
investigation_id: The investigation UUID. |
| 303 |
template_id: The response plan template ID to apply. |
| 304 |
|
| 305 |
Returns: |
| 306 |
The applied response plan from API response. |
| 307 |
""" |
| 308 |
api_path = self._build_response_plans_path(investigation_id) |
| 309 |
|
| 310 |
payload = { |
| 311 |
"response_template_id": template_id, |
| 312 |
"incidentType": "default", |
| 313 |
} |
| 314 |
|
| 315 |
display.vvv(f"splunk_response_plan_execution: POST {api_path}") |
| 316 |
display.vvv(f"splunk_response_plan_execution: payload: {payload}") |
| 317 |
|
| 318 |
response = conn_request.create_update(api_path, data=payload, json_payload=True) |
| 319 |
display.vvv(f"splunk_response_plan_execution: apply response: {response}") |
| 320 |
|
| 321 |
return response or {} |
| 322 |
|
| 323 |
def _remove_response_plan( |
| 324 |
self, |
| 325 |
conn_request: SplunkRequest, |
| 326 |
investigation_id: str, |
| 327 |
applied_plan_id: str, |
| 328 |
) -> None: |
| 329 |
"""Remove an applied response plan from an investigation. |
| 330 |
|
| 331 |
Args: |
| 332 |
conn_request: The SplunkRequest instance. |
| 333 |
investigation_id: The investigation UUID. |
| 334 |
applied_plan_id: The applied response plan instance ID. |
| 335 |
""" |
| 336 |
api_path = self._build_response_plan_path(investigation_id, applied_plan_id) |
| 337 |
|
| 338 |
display.vvv(f"splunk_response_plan_execution: DELETE {api_path}") |
| 339 |
conn_request.delete_by_path(api_path) |
| 340 |
|
| 341 |
def _update_task( |
| 342 |
self, |
| 343 |
conn_request: SplunkRequest, |
| 344 |
investigation_id: str, |
| 345 |
applied_plan_id: str, |
| 346 |
phase_id: str, |
| 347 |
task_id: str, |
| 348 |
status: Optional[str], |
| 349 |
owner: Optional[str], |
| 350 |
) -> dict[str, Any]: |
| 351 |
"""Update a task's status and/or owner. |
| 352 |
|
| 353 |
Args: |
| 354 |
conn_request: The SplunkRequest instance. |
| 355 |
investigation_id: The investigation UUID. |
| 356 |
applied_plan_id: The applied response plan instance ID. |
| 357 |
phase_id: The phase ID. |
| 358 |
task_id: The task ID. |
| 359 |
status: The new task status (started/ended), or None to skip. |
| 360 |
owner: The new task owner, or None to skip. |
| 361 |
|
| 362 |
Returns: |
| 363 |
The API response. |
| 364 |
""" |
| 365 |
api_path = self._build_task_path(investigation_id, applied_plan_id, phase_id, task_id) |
| 366 |
|
| 367 |
payload: dict[str, Any] = {} |
| 368 |
if status: |
| 369 |
payload["status"] = TASK_STATUS_TO_API.get(status, status) |
| 370 |
if owner: |
| 371 |
payload["owner"] = owner |
| 372 |
|
| 373 |
display.vvv(f"splunk_response_plan_execution: POST {api_path}") |
| 374 |
display.vvv(f"splunk_response_plan_execution: task payload: {payload}") |
| 375 |
|
| 376 |
response = conn_request.create_update(api_path, data=payload, json_payload=True) |
| 377 |
display.vvv(f"splunk_response_plan_execution: task update response: {response}") |
| 378 |
|
| 379 |
return response or {} |
| 380 |
|
| 381 |
def _build_task_error_result( |
| 382 |
self, |
| 383 |
phase_name: str, |
| 384 |
task_name: str, |
| 385 |
error_msg: str, |
| 386 |
) -> dict[str, Any]: |
| 387 |
"""Build an error result for a task operation. |
| 388 |
|
| 389 |
Args: |
| 390 |
phase_name: The phase name. |
| 391 |
task_name: The task name. |
| 392 |
error_msg: The error message. |
| 393 |
|
| 394 |
Returns: |
| 395 |
A task result dictionary with error information. |
| 396 |
""" |
| 397 |
return { |
| 398 |
"phase_name": phase_name, |
| 399 |
"task_name": task_name, |
| 400 |
"error": error_msg, |
| 401 |
"changed": False, |
| 402 |
} |
| 403 |
|
| 404 |
def _build_task_result( |
| 405 |
self, |
| 406 |
phase_name: str, |
| 407 |
task_name: str, |
| 408 |
status: str, |
| 409 |
owner: str, |
| 410 |
changed: bool, |
| 411 |
) -> dict[str, Any]: |
| 412 |
"""Build a result dictionary for a task operation. |
| 413 |
|
| 414 |
Args: |
| 415 |
phase_name: The phase name. |
| 416 |
task_name: The task name. |
| 417 |
status: The task status. |
| 418 |
owner: The task owner. |
| 419 |
changed: Whether the task was changed. |
| 420 |
|
| 421 |
Returns: |
| 422 |
A task result dictionary. |
| 423 |
""" |
| 424 |
return { |
| 425 |
"phase_name": phase_name, |
| 426 |
"task_name": task_name, |
| 427 |
"status": status, |
| 428 |
"owner": owner, |
| 429 |
"changed": changed, |
| 430 |
} |
| 431 |
|
| 432 |
def _process_single_task( |
| 433 |
self, |
| 434 |
conn_request: SplunkRequest, |
| 435 |
investigation_id: str, |
| 436 |
applied_plan_id: str, |
| 437 |
phases: list[dict[str, Any]], |
| 438 |
task_config: dict[str, Any], |
| 439 |
) -> dict[str, Any]: |
| 440 |
"""Process a single task update. |
| 441 |
|
| 442 |
Args: |
| 443 |
conn_request: The SplunkRequest instance. |
| 444 |
investigation_id: The investigation UUID. |
| 445 |
applied_plan_id: The applied plan ID. |
| 446 |
phases: List of phases in the applied plan. |
| 447 |
task_config: The task configuration from module parameters. |
| 448 |
|
| 449 |
Returns: |
| 450 |
A task result dictionary. |
| 451 |
""" |
| 452 |
phase_name = task_config.get("phase_name", "") |
| 453 |
task_name = task_config.get("task_name", "") |
| 454 |
desired_status = task_config.get("status") |
| 455 |
desired_owner = task_config.get("owner") |
| 456 |
|
| 457 |
# Find the phase |
| 458 |
phase = self._find_phase_by_name(phases, phase_name) |
| 459 |
if not phase: |
| 460 |
display.warning( |
| 461 |
f"splunk_response_plan_execution: phase '{phase_name}' not found, skipping task", |
| 462 |
) |
| 463 |
return self._build_task_error_result( |
| 464 |
phase_name, |
| 465 |
task_name, |
| 466 |
f"Phase '{phase_name}' not found", |
| 467 |
) |
| 468 |
|
| 469 |
# Find the task |
| 470 |
task = self._find_task_by_name(phase.get("tasks", []), task_name) |
| 471 |
if not task: |
| 472 |
display.warning( |
| 473 |
f"splunk_response_plan_execution: task '{task_name}' not found in phase " |
| 474 |
f"'{phase_name}', skipping", |
| 475 |
) |
| 476 |
return self._build_task_error_result( |
| 477 |
phase_name, |
| 478 |
task_name, |
| 479 |
f"Task '{task_name}' not found in phase '{phase_name}'", |
| 480 |
) |
| 481 |
|
| 482 |
current_status = task.get("status", "") |
| 483 |
current_owner = task.get("owner", "") |
| 484 |
status_needs_update = desired_status and desired_status != current_status |
| 485 |
owner_needs_update = desired_owner and desired_owner != current_owner |
| 486 |
|
| 487 |
# No update needed - already in desired state |
| 488 |
if not status_needs_update and not owner_needs_update: |
| 489 |
display.vv( |
| 490 |
f"splunk_response_plan_execution: task '{task_name}' already in desired state", |
| 491 |
) |
| 492 |
return self._build_task_result( |
| 493 |
phase_name, |
| 494 |
task_name, |
| 495 |
current_status, |
| 496 |
current_owner, |
| 497 |
changed=False, |
| 498 |
) |
| 499 |
|
| 500 |
final_status = desired_status or current_status |
| 501 |
final_owner = desired_owner or current_owner |
| 502 |
|
| 503 |
# Check mode - don't make actual changes |
| 504 |
if self._task.check_mode: |
| 505 |
display.v( |
| 506 |
f"splunk_response_plan_execution: check mode - would update task '{task_name}'", |
| 507 |
) |
| 508 |
return self._build_task_result( |
| 509 |
phase_name, |
| 510 |
task_name, |
| 511 |
final_status, |
| 512 |
final_owner, |
| 513 |
changed=True, |
| 514 |
) |
| 515 |
|
| 516 |
# Perform the update |
| 517 |
self._update_task( |
| 518 |
conn_request, |
| 519 |
investigation_id, |
| 520 |
applied_plan_id, |
| 521 |
phase.get("id", ""), |
| 522 |
task.get("id", ""), |
| 523 |
desired_status if status_needs_update else None, |
| 524 |
desired_owner if owner_needs_update else None, |
| 525 |
) |
| 526 |
|
| 527 |
return self._build_task_result( |
| 528 |
phase_name, |
| 529 |
task_name, |
| 530 |
final_status, |
| 531 |
final_owner, |
| 532 |
changed=True, |
| 533 |
) |
| 534 |
|
| 535 |
def _process_tasks( |
| 536 |
self, |
| 537 |
conn_request: SplunkRequest, |
| 538 |
investigation_id: str, |
| 539 |
applied_plan: dict[str, Any], |
| 540 |
tasks_config: list[dict[str, Any]], |
| 541 |
) -> tuple[list[dict[str, Any]], bool]: |
| 542 |
"""Process task updates for an applied response plan. |
| 543 |
|
| 544 |
Args: |
| 545 |
conn_request: The SplunkRequest instance. |
| 546 |
investigation_id: The investigation UUID. |
| 547 |
applied_plan: The applied response plan (with phases/tasks). |
| 548 |
tasks_config: List of task configurations from module parameters. |
| 549 |
|
| 550 |
Returns: |
| 551 |
Tuple of (tasks_updated list, any_changed boolean). |
| 552 |
""" |
| 553 |
applied_plan_id = applied_plan.get("id", "") |
| 554 |
phases = applied_plan.get("phases", []) |
| 555 |
|
| 556 |
tasks_updated = [] |
| 557 |
for task_config in tasks_config: |
| 558 |
result = self._process_single_task( |
| 559 |
conn_request, |
| 560 |
investigation_id, |
| 561 |
applied_plan_id, |
| 562 |
phases, |
| 563 |
task_config, |
| 564 |
) |
| 565 |
tasks_updated.append(result) |
| 566 |
|
| 567 |
any_changed = any(task.get("changed", False) for task in tasks_updated) |
| 568 |
return tasks_updated, any_changed |
| 569 |
|
| 570 |
def _build_before_state( |
| 571 |
self, |
| 572 |
existing_plan: Optional[dict[str, Any]], |
| 573 |
template_id: str, |
| 574 |
) -> dict[str, Any]: |
| 575 |
"""Build the before state for result output. |
| 576 |
|
| 577 |
Args: |
| 578 |
existing_plan: The existing applied plan, if any. |
| 579 |
template_id: The response plan template ID. |
| 580 |
|
| 581 |
Returns: |
| 582 |
The before state dictionary. |
| 583 |
""" |
| 584 |
before_state: dict[str, Any] = {"applied": existing_plan is not None} |
| 585 |
if existing_plan: |
| 586 |
before_state["applied_plan_id"] = existing_plan.get("id") |
| 587 |
before_state["response_plan_id"] = template_id |
| 588 |
return before_state |
| 589 |
|
| 590 |
def _get_result_message(self, plan_changed: bool, tasks_changed: bool) -> str: |
| 591 |
"""Get the result message based on what changed. |
| 592 |
|
| 593 |
Args: |
| 594 |
plan_changed: Whether the plan was applied/changed. |
| 595 |
tasks_changed: Whether any tasks were updated. |
| 596 |
|
| 597 |
Returns: |
| 598 |
The appropriate result message. |
| 599 |
""" |
| 600 |
if plan_changed and tasks_changed: |
| 601 |
return "Response plan applied and tasks updated successfully" |
| 602 |
if plan_changed: |
| 603 |
return "Response plan applied successfully" |
| 604 |
if tasks_changed: |
| 605 |
return "Tasks updated successfully" |
| 606 |
return "No changes required" |
| 607 |
|
| 608 |
def _process_tasks_if_configured( |
| 609 |
self, |
| 610 |
conn_request: SplunkRequest, |
| 611 |
investigation_id: str, |
| 612 |
template_name: str, |
| 613 |
tasks_config: Optional[list[dict[str, Any]]], |
| 614 |
plan_changed: bool, |
| 615 |
) -> tuple[list[dict[str, Any]], bool]: |
| 616 |
"""Process task updates if task configuration is provided. |
| 617 |
|
| 618 |
Args: |
| 619 |
conn_request: The SplunkRequest instance. |
| 620 |
investigation_id: The investigation UUID. |
| 621 |
template_name: The response plan template name. |
| 622 |
tasks_config: Optional list of task configurations. |
| 623 |
plan_changed: Whether the plan was just applied. |
| 624 |
|
| 625 |
Returns: |
| 626 |
Tuple of (tasks_updated list, tasks_changed boolean). |
| 627 |
""" |
| 628 |
if not tasks_config: |
| 629 |
return [], False |
| 630 |
|
| 631 |
# Re-fetch to get the full structure with phases/tasks |
| 632 |
applied_plans = self._get_applied_response_plans(conn_request, investigation_id) |
| 633 |
applied_plan = self._find_applied_plan_by_name(applied_plans, template_name) |
| 634 |
|
| 635 |
if not applied_plan: |
| 636 |
return [], False |
| 637 |
|
| 638 |
mapped_plan = map_applied_response_plan_from_api(applied_plan) |
| 639 |
return self._process_tasks( |
| 640 |
conn_request, |
| 641 |
investigation_id, |
| 642 |
mapped_plan, |
| 643 |
tasks_config, |
| 644 |
) |
| 645 |
|
| 646 |
def _handle_present( |
| 647 |
self, |
| 648 |
conn_request: SplunkRequest, |
| 649 |
investigation_id: str, |
| 650 |
template_id: str, |
| 651 |
template_name: str, |
| 652 |
tasks_config: Optional[list[dict[str, Any]]], |
| 653 |
) -> None: |
| 654 |
"""Handle state=present operation. |
| 655 |
|
| 656 |
Args: |
| 657 |
conn_request: The SplunkRequest instance. |
| 658 |
investigation_id: The investigation UUID. |
| 659 |
template_id: The response plan template ID. |
| 660 |
template_name: The response plan template name. |
| 661 |
tasks_config: Optional list of task configurations. |
| 662 |
""" |
| 663 |
display.v(f"splunk_response_plan_execution: applying response plan to {investigation_id}") |
| 664 |
|
| 665 |
# Get current applied plans |
| 666 |
applied_plans = self._get_applied_response_plans(conn_request, investigation_id) |
| 667 |
display.vv(f"splunk_response_plan_execution: found {len(applied_plans)} applied plans") |
| 668 |
existing_plan = self._find_applied_plan_by_name(applied_plans, template_name) |
| 669 |
display.vv( |
| 670 |
f"splunk_response_plan_execution: existing plan found: {existing_plan is not None}", |
| 671 |
) |
| 672 |
|
| 673 |
before_state = self._build_before_state(existing_plan, template_id) |
| 674 |
|
| 675 |
# Handle check mode for applying a new plan |
| 676 |
if not existing_plan and self._task.check_mode: |
| 677 |
display.v("splunk_response_plan_execution: check mode - would apply response plan") |
| 678 |
self._result[self.module_name] = { |
| 679 |
"before": before_state, |
| 680 |
"after": {"applied": True, "response_plan_id": template_id}, |
| 681 |
} |
| 682 |
self._result["changed"] = True |
| 683 |
self._result["msg"] = "Check mode: would apply response plan" |
| 684 |
return |
| 685 |
|
| 686 |
# Apply or use existing plan |
| 687 |
if existing_plan: |
| 688 |
display.v("splunk_response_plan_execution: response plan already applied") |
| 689 |
applied_plan = existing_plan |
| 690 |
plan_changed = False |
| 691 |
else: |
| 692 |
applied_plan = self._apply_response_plan(conn_request, investigation_id, template_id) |
| 693 |
plan_changed = True |
| 694 |
display.v("splunk_response_plan_execution: response plan applied successfully") |
| 695 |
|
| 696 |
# Process tasks if configured |
| 697 |
tasks_updated, tasks_changed = self._process_tasks_if_configured( |
| 698 |
conn_request, |
| 699 |
investigation_id, |
| 700 |
template_name, |
| 701 |
tasks_config, |
| 702 |
plan_changed, |
| 703 |
) |
| 704 |
|
| 705 |
# Build result |
| 706 |
after_state = { |
| 707 |
"applied": True, |
| 708 |
"applied_plan_id": applied_plan.get("id", ""), |
| 709 |
"response_plan_id": template_id, |
| 710 |
} |
| 711 |
|
| 712 |
self._result[self.module_name] = {"before": before_state, "after": after_state} |
| 713 |
if tasks_updated: |
| 714 |
self._result[self.module_name]["tasks_updated"] = tasks_updated |
| 715 |
|
| 716 |
self._result["changed"] = plan_changed or tasks_changed |
| 717 |
self._result["msg"] = self._get_result_message(plan_changed, tasks_changed) |
| 718 |
|
| 719 |
def _handle_absent( |
| 720 |
self, |
| 721 |
conn_request: SplunkRequest, |
| 722 |
investigation_id: str, |
| 723 |
template_id: str, |
| 724 |
template_name: str, |
| 725 |
) -> None: |
| 726 |
"""Handle state=absent operation. |
| 727 |
|
| 728 |
Args: |
| 729 |
conn_request: The SplunkRequest instance. |
| 730 |
investigation_id: The investigation UUID. |
| 731 |
template_id: The response plan template ID. |
| 732 |
template_name: The response plan template name. |
| 733 |
""" |
| 734 |
display.v( |
| 735 |
f"splunk_response_plan_execution: removing response plan from {investigation_id}", |
| 736 |
) |
| 737 |
|
| 738 |
# Get current applied plans |
| 739 |
applied_plans = self._get_applied_response_plans(conn_request, investigation_id) |
| 740 |
existing_plan = self._find_applied_plan_by_name(applied_plans, template_name) |
| 741 |
|
| 742 |
before_state = { |
| 743 |
"applied": existing_plan is not None, |
| 744 |
} |
| 745 |
if existing_plan: |
| 746 |
before_state["applied_plan_id"] = existing_plan.get("id") |
| 747 |
before_state["response_plan_id"] = template_id |
| 748 |
|
| 749 |
after_state = { |
| 750 |
"applied": False, |
| 751 |
} |
| 752 |
|
| 753 |
if not existing_plan: |
| 754 |
display.v( |
| 755 |
"splunk_response_plan_execution: response plan not applied, nothing to remove", |
| 756 |
) |
| 757 |
self._result[self.module_name] = { |
| 758 |
"before": before_state, |
| 759 |
"after": after_state, |
| 760 |
} |
| 761 |
self._result["changed"] = False |
| 762 |
self._result["msg"] = "Response plan not applied, already absent" |
| 763 |
return |
| 764 |
|
| 765 |
if self._task.check_mode: |
| 766 |
display.v("splunk_response_plan_execution: check mode - would remove response plan") |
| 767 |
self._result[self.module_name] = { |
| 768 |
"before": before_state, |
| 769 |
"after": after_state, |
| 770 |
} |
| 771 |
self._result["changed"] = True |
| 772 |
self._result["msg"] = "Check mode: would remove response plan" |
| 773 |
return |
| 774 |
|
| 775 |
# Remove the response plan |
| 776 |
applied_plan_id = existing_plan.get("id", "") |
| 777 |
self._remove_response_plan(conn_request, investigation_id, applied_plan_id) |
| 778 |
|
| 779 |
self._result[self.module_name] = { |
| 780 |
"before": before_state, |
| 781 |
"after": after_state, |
| 782 |
} |
| 783 |
self._result["changed"] = True |
| 784 |
self._result["msg"] = "Response plan removed successfully" |
| 785 |
display.v("splunk_response_plan_execution: response plan removed successfully") |
| 786 |
|
| 787 |
def run(self, tmp=None, task_vars=None): |
| 788 |
"""Execute the action module.""" |
| 789 |
self._supports_check_mode = True |
| 790 |
self._result = super().run(tmp, task_vars) |
| 791 |
|
| 792 |
display.v("splunk_response_plan_execution: starting module execution") |
| 793 |
|
| 794 |
# Validate arguments |
| 795 |
if not check_argspec(self, self._result, DOCUMENTATION): |
| 796 |
display.v( |
| 797 |
f"splunk_response_plan_execution: argument validation failed: " |
| 798 |
f"{self._result.get('msg')}", |
| 799 |
) |
| 800 |
return self._result |
| 801 |
|
| 802 |
# Initialize result structure |
| 803 |
self._result[self.module_name] = {} |
| 804 |
self._result["changed"] = False |
| 805 |
|
| 806 |
self._configure_api() |
| 807 |
|
| 808 |
# Extract parameters |
| 809 |
investigation_id = self._task.args.get("investigation_ref_id") |
| 810 |
response_plan = self._task.args.get("response_plan") |
| 811 |
state = self._task.args.get("state", "present") |
| 812 |
tasks_config = self._task.args.get("tasks") |
| 813 |
|
| 814 |
display.vv(f"splunk_response_plan_execution: investigation_ref_id: {investigation_id}") |
| 815 |
display.vv(f"splunk_response_plan_execution: response_plan: {response_plan}") |
| 816 |
display.vv(f"splunk_response_plan_execution: state: {state}") |
| 817 |
display.vvv(f"splunk_response_plan_execution: tasks: {tasks_config}") |
| 818 |
|
| 819 |
# Validate required parameters |
| 820 |
if not investigation_id: |
| 821 |
self._result["failed"] = True |
| 822 |
self._result["msg"] = "Missing required parameter: investigation_ref_id" |
| 823 |
return self._result |
| 824 |
|
| 825 |
if not response_plan: |
| 826 |
self._result["failed"] = True |
| 827 |
self._result["msg"] = "Missing required parameter: response_plan" |
| 828 |
return self._result |
| 829 |
|
| 830 |
# Setup connection |
| 831 |
conn = Connection(self._connection.socket_path) |
| 832 |
conn_request = SplunkRequest( |
| 833 |
action_module=self, |
| 834 |
connection=conn, |
| 835 |
not_rest_data_keys=[ |
| 836 |
"investigation_ref_id", |
| 837 |
"response_plan", |
| 838 |
"state", |
| 839 |
"tasks", |
| 840 |
"api_namespace", |
| 841 |
"api_user", |
| 842 |
"api_app", |
| 843 |
], |
| 844 |
) |
| 845 |
|
| 846 |
# Resolve response plan to template ID and name |
| 847 |
templates = self._get_response_templates(conn_request) |
| 848 |
if not templates: |
| 849 |
self._result["failed"] = True |
| 850 |
self._result["msg"] = "No response plan templates found" |
| 851 |
return self._result |
| 852 |
|
| 853 |
if is_uuid(response_plan): |
| 854 |
template_id = response_plan |
| 855 |
template_name = self._get_template_name_by_id(templates, template_id) |
| 856 |
display.vv(f"splunk_response_plan_execution: looking up name for ID: {template_id}") |
| 857 |
else: |
| 858 |
template_name = response_plan |
| 859 |
template_id = self._get_template_id_by_name(templates, template_name) |
| 860 |
display.vv(f"splunk_response_plan_execution: looking up ID for name: {template_name}") |
| 861 |
|
| 862 |
if not template_id or not template_name: |
| 863 |
self._result["failed"] = True |
| 864 |
self._result["msg"] = f"Response plan not found: {response_plan}" |
| 865 |
return self._result |
| 866 |
|
| 867 |
display.vv(f"splunk_response_plan_execution: resolved template_id: {template_id}") |
| 868 |
display.vv(f"splunk_response_plan_execution: resolved template_name: {template_name}") |
| 869 |
|
| 870 |
# Route based on state |
| 871 |
if state == "absent": |
| 872 |
self._handle_absent(conn_request, investigation_id, template_id, template_name) |
| 873 |
else: |
| 874 |
self._handle_present( |
| 875 |
conn_request, |
| 876 |
investigation_id, |
| 877 |
template_id, |
| 878 |
template_name, |
| 879 |
tasks_config, |
| 880 |
) |
| 881 |
|
| 882 |
display.v( |
| 883 |
f"splunk_response_plan_execution: completed with changed={self._result['changed']}", |
| 884 |
) |
| 885 |
return self._result |