| 1 |
# Copyright 2026 Red Hat Inc. |
| 2 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
|
| 4 |
""" |
| 5 |
The action plugin file for splunk_notes_info |
| 6 |
""" |
| 7 |
|
| 8 |
from typing import Any |
| 9 |
|
| 10 |
from ansible.errors import AnsibleActionFail |
| 11 |
from ansible.module_utils.connection import Connection |
| 12 |
from ansible.module_utils.six.moves.urllib.parse import quote |
| 13 |
from ansible.plugins.action import ActionBase |
| 14 |
from ansible.utils.display import Display |
| 15 |
|
| 16 |
from ansible_collections.splunk.es.plugins.module_utils.finding import extract_notable_time |
| 17 |
from ansible_collections.splunk.es.plugins.module_utils.notes import ( |
| 18 |
TARGET_FINDING, |
| 19 |
TARGET_RESPONSE_PLAN_TASK, |
| 20 |
build_notes_api_path, |
| 21 |
build_task_note_api_path, |
| 22 |
build_task_notes_api_path, |
| 23 |
map_note_from_api, |
| 24 |
validate_target_params, |
| 25 |
) |
| 26 |
from ansible_collections.splunk.es.plugins.module_utils.splunk import ( |
| 27 |
SplunkRequest, |
| 28 |
check_argspec, |
| 29 |
) |
| 30 |
from ansible_collections.splunk.es.plugins.module_utils.splunk_utils import ( |
| 31 |
DEFAULT_API_APP, |
| 32 |
DEFAULT_API_NAMESPACE, |
| 33 |
DEFAULT_API_USER, |
| 34 |
get_api_config_from_args, |
| 35 |
) |
| 36 |
from ansible_collections.splunk.es.plugins.modules.splunk_notes_info import DOCUMENTATION |
| 37 |
|
| 38 |
|
| 39 |
# Initialize display for debug output |
| 40 |
display = Display() |
| 41 |
|
| 42 |
# Default limit for notes query |
| 43 |
DEFAULT_NOTES_LIMIT = 100 |
| 44 |
|
| 45 |
|
| 46 |
class ActionModule(ActionBase): |
| 47 |
"""Action module for querying Splunk ES notes.""" |
| 48 |
|
| 49 |
def __init__(self, *args: Any, **kwargs: Any) -> None: |
| 50 |
super().__init__(*args, **kwargs) |
| 51 |
self._result: dict[str, Any] = {} |
| 52 |
self.api_namespace = DEFAULT_API_NAMESPACE |
| 53 |
self.api_user = DEFAULT_API_USER |
| 54 |
self.api_app = DEFAULT_API_APP |
| 55 |
|
| 56 |
def fail_json(self, msg: str) -> None: |
| 57 |
"""Raise an AnsibleActionFail with a cleaned up message. |
| 58 |
|
| 59 |
Args: |
| 60 |
msg: The message for the failure. |
| 61 |
|
| 62 |
Raises: |
| 63 |
AnsibleActionFail: Always raised with the provided message. |
| 64 |
""" |
| 65 |
msg = msg.replace("(basic.py)", self._task.action) |
| 66 |
raise AnsibleActionFail(msg) |
| 67 |
|
| 68 |
def _configure_api(self) -> None: |
| 69 |
"""Configure API path components from task arguments.""" |
| 70 |
self.api_namespace, self.api_user, self.api_app = get_api_config_from_args( |
| 71 |
self._task.args, |
| 72 |
) |
| 73 |
|
| 74 |
def _build_notes_path(self, target_type: str) -> str: |
| 75 |
"""Build the notes API path based on target type. |
| 76 |
|
| 77 |
Args: |
| 78 |
target_type: The target type. |
| 79 |
|
| 80 |
Returns: |
| 81 |
The API path for notes. |
| 82 |
""" |
| 83 |
if target_type == TARGET_RESPONSE_PLAN_TASK: |
| 84 |
return build_task_notes_api_path( |
| 85 |
investigation_id=self._task.args.get("investigation_ref_id"), |
| 86 |
response_plan_id=self._task.args.get("response_plan_id"), |
| 87 |
phase_id=self._task.args.get("phase_id"), |
| 88 |
task_id=self._task.args.get("task_id"), |
| 89 |
namespace=self.api_namespace, |
| 90 |
user=self.api_user, |
| 91 |
app=self.api_app, |
| 92 |
) |
| 93 |
|
| 94 |
# For finding or investigation |
| 95 |
if target_type == TARGET_FINDING: |
| 96 |
investigation_id = quote(self._task.args.get("finding_ref_id"), safe="") |
| 97 |
else: |
| 98 |
investigation_id = self._task.args.get("investigation_ref_id") |
| 99 |
|
| 100 |
return build_notes_api_path( |
| 101 |
investigation_id=investigation_id, |
| 102 |
namespace=self.api_namespace, |
| 103 |
user=self.api_user, |
| 104 |
app=self.api_app, |
| 105 |
) |
| 106 |
|
| 107 |
def _build_task_note_path(self, note_id: str) -> str: |
| 108 |
"""Build the API path for a specific task note. |
| 109 |
|
| 110 |
Only used for response_plan_task target type which supports direct note lookup. |
| 111 |
|
| 112 |
Args: |
| 113 |
note_id: The note ID. |
| 114 |
|
| 115 |
Returns: |
| 116 |
The API path for the specific task note. |
| 117 |
""" |
| 118 |
return build_task_note_api_path( |
| 119 |
investigation_id=self._task.args.get("investigation_ref_id"), |
| 120 |
response_plan_id=self._task.args.get("response_plan_id"), |
| 121 |
phase_id=self._task.args.get("phase_id"), |
| 122 |
task_id=self._task.args.get("task_id"), |
| 123 |
note_id=note_id, |
| 124 |
namespace=self.api_namespace, |
| 125 |
user=self.api_user, |
| 126 |
app=self.api_app, |
| 127 |
) |
| 128 |
|
| 129 |
def _get_query_params(self, target_type: str) -> dict[str, Any]: |
| 130 |
"""Get query parameters for API requests. |
| 131 |
|
| 132 |
For findings, extracts notable_time from finding_ref_id. |
| 133 |
Also includes limit parameter. |
| 134 |
|
| 135 |
Args: |
| 136 |
target_type: The target type. |
| 137 |
|
| 138 |
Returns: |
| 139 |
Query parameters dict. |
| 140 |
""" |
| 141 |
query_params: dict[str, Any] = {} |
| 142 |
|
| 143 |
# Add notable_time for findings |
| 144 |
if target_type == TARGET_FINDING: |
| 145 |
finding_ref_id = self._task.args.get("finding_ref_id") |
| 146 |
notable_time = extract_notable_time(finding_ref_id) |
| 147 |
if notable_time: |
| 148 |
display.vvv( |
| 149 |
f"splunk_notes_info: using notable_time={notable_time} from finding_ref_id", |
| 150 |
) |
| 151 |
query_params["notable_time"] = notable_time |
| 152 |
|
| 153 |
# Add limit parameter |
| 154 |
limit = self._task.args.get("limit", DEFAULT_NOTES_LIMIT) |
| 155 |
query_params["limit"] = limit |
| 156 |
|
| 157 |
# Sort by newest first |
| 158 |
query_params["sort"] = "create_time:-1" |
| 159 |
|
| 160 |
return query_params |
| 161 |
|
| 162 |
def _get_all_notes( |
| 163 |
self, |
| 164 |
conn_request: SplunkRequest, |
| 165 |
target_type: str, |
| 166 |
) -> list[dict[str, Any]]: |
| 167 |
"""Get all notes for a target. |
| 168 |
|
| 169 |
Args: |
| 170 |
conn_request: The SplunkRequest instance. |
| 171 |
target_type: The target type. |
| 172 |
|
| 173 |
Returns: |
| 174 |
List of notes mapped to module format, or empty list if none found. |
| 175 |
""" |
| 176 |
api_path = self._build_notes_path(target_type) |
| 177 |
query_params = self._get_query_params(target_type) |
| 178 |
|
| 179 |
display.vv(f"splunk_notes_info: GET {api_path}") |
| 180 |
display.vv(f"splunk_notes_info: query_params={query_params}") |
| 181 |
|
| 182 |
response = conn_request.get_by_path(api_path, query_params=query_params) |
| 183 |
|
| 184 |
display.vvv(f"splunk_notes_info: raw response: {response}") |
| 185 |
|
| 186 |
if not response: |
| 187 |
display.vv("splunk_notes_info: no notes found (empty response)") |
| 188 |
return [] |
| 189 |
|
| 190 |
# Extract notes from response - API returns {"items": [...]} |
| 191 |
raw_notes = response.get("items", []) |
| 192 |
|
| 193 |
# Map notes to module format |
| 194 |
notes = [map_note_from_api(note) for note in raw_notes if note] |
| 195 |
|
| 196 |
display.vv(f"splunk_notes_info: found {len(notes)} notes") |
| 197 |
return notes |
| 198 |
|
| 199 |
def _get_note_by_id_filtered( |
| 200 |
self, |
| 201 |
conn_request: SplunkRequest, |
| 202 |
target_type: str, |
| 203 |
note_id: str, |
| 204 |
) -> dict[str, Any]: |
| 205 |
"""Get a note by ID by fetching all notes and filtering. |
| 206 |
|
| 207 |
Used for finding and investigation target types where the API |
| 208 |
doesn't support direct note lookup. |
| 209 |
|
| 210 |
Args: |
| 211 |
conn_request: The SplunkRequest instance. |
| 212 |
target_type: The target type. |
| 213 |
note_id: The note ID. |
| 214 |
|
| 215 |
Returns: |
| 216 |
The note if found, empty dict otherwise. |
| 217 |
""" |
| 218 |
display.vv(f"splunk_notes_info: getting note by id (filtered): {note_id}") |
| 219 |
|
| 220 |
all_notes = self._get_all_notes(conn_request, target_type) |
| 221 |
|
| 222 |
for note in all_notes: |
| 223 |
if note.get("note_id") == note_id: |
| 224 |
display.vv(f"splunk_notes_info: found note: {note}") |
| 225 |
return note |
| 226 |
|
| 227 |
display.vv(f"splunk_notes_info: no note found with id: {note_id}") |
| 228 |
return {} |
| 229 |
|
| 230 |
def _get_task_note_direct( |
| 231 |
self, |
| 232 |
conn_request: SplunkRequest, |
| 233 |
note_id: str, |
| 234 |
) -> dict[str, Any]: |
| 235 |
"""Get a task note directly by API path. |
| 236 |
|
| 237 |
Used for response_plan_task target type which supports direct note lookup. |
| 238 |
|
| 239 |
Args: |
| 240 |
conn_request: The SplunkRequest instance. |
| 241 |
note_id: The note ID. |
| 242 |
|
| 243 |
Returns: |
| 244 |
The note if found, empty dict otherwise. |
| 245 |
""" |
| 246 |
api_path = self._build_task_note_path(note_id) |
| 247 |
|
| 248 |
display.vv(f"splunk_notes_info: GET {api_path}") |
| 249 |
|
| 250 |
response = conn_request.get_by_path(api_path) |
| 251 |
|
| 252 |
display.vvv(f"splunk_notes_info: raw response: {response}") |
| 253 |
|
| 254 |
if response: |
| 255 |
return map_note_from_api(response) |
| 256 |
|
| 257 |
display.vv(f"splunk_notes_info: no note found with id: {note_id}") |
| 258 |
return {} |
| 259 |
|
| 260 |
def run(self, tmp=None, task_vars=None): |
| 261 |
"""Execute the action module.""" |
| 262 |
self._supports_check_mode = True |
| 263 |
self._result = super().run(tmp, task_vars) |
| 264 |
|
| 265 |
display.v("splunk_notes_info: starting module execution") |
| 266 |
|
| 267 |
# Validate arguments |
| 268 |
if not check_argspec(self, self._result, DOCUMENTATION): |
| 269 |
display.v( |
| 270 |
f"splunk_notes_info: argument validation failed: {self._result.get('msg')}", |
| 271 |
) |
| 272 |
return self._result |
| 273 |
|
| 274 |
self._configure_api() |
| 275 |
|
| 276 |
# Extract parameters |
| 277 |
target_type = self._task.args.get("target_type") |
| 278 |
note_id = self._task.args.get("note_id") |
| 279 |
|
| 280 |
display.vv(f"splunk_notes_info: target_type: {target_type}") |
| 281 |
display.vv(f"splunk_notes_info: note_id: {note_id}") |
| 282 |
|
| 283 |
# Validate target-specific parameters |
| 284 |
error = validate_target_params(target_type, self._task.args) |
| 285 |
if error: |
| 286 |
self._result["failed"] = True |
| 287 |
self._result["msg"] = error |
| 288 |
display.v(f"splunk_notes_info: {error}") |
| 289 |
return self._result |
| 290 |
|
| 291 |
# Setup connection |
| 292 |
conn = Connection(self._connection.socket_path) |
| 293 |
conn_request = SplunkRequest( |
| 294 |
action_module=self, |
| 295 |
connection=conn, |
| 296 |
not_rest_data_keys=[ |
| 297 |
"target_type", |
| 298 |
"note_id", |
| 299 |
"finding_ref_id", |
| 300 |
"investigation_ref_id", |
| 301 |
"response_plan_id", |
| 302 |
"phase_id", |
| 303 |
"task_id", |
| 304 |
"limit", |
| 305 |
"api_namespace", |
| 306 |
"api_user", |
| 307 |
"api_app", |
| 308 |
], |
| 309 |
) |
| 310 |
|
| 311 |
try: |
| 312 |
if note_id: |
| 313 |
# Query specific note by ID |
| 314 |
display.v(f"splunk_notes_info: querying note by id: {note_id}") |
| 315 |
|
| 316 |
if target_type == TARGET_RESPONSE_PLAN_TASK: |
| 317 |
# Response plan tasks support direct note lookup |
| 318 |
note = self._get_task_note_direct(conn_request, note_id) |
| 319 |
else: |
| 320 |
# Finding/investigation require fetching all and filtering |
| 321 |
note = self._get_note_by_id_filtered(conn_request, target_type, note_id) |
| 322 |
|
| 323 |
# Return as list for consistency |
| 324 |
self._result["notes"] = [note] if note else [] |
| 325 |
|
| 326 |
else: |
| 327 |
# Return all notes |
| 328 |
display.v("splunk_notes_info: querying all notes") |
| 329 |
self._result["notes"] = self._get_all_notes(conn_request, target_type) |
| 330 |
|
| 331 |
self._result["changed"] = False |
| 332 |
display.v(f"splunk_notes_info: returning {len(self._result['notes'])} note(s)") |
| 333 |
|
| 334 |
except Exception as e: |
| 335 |
error_msg = str(e) |
| 336 |
# Handle resource not found gracefully - return empty list |
| 337 |
# Splunk may return 404, or 500 with MC_0050 for non-existent resources |
| 338 |
is_not_found = ( |
| 339 |
"404" in error_msg or "not found" in error_msg.lower() or "MC_0050" in error_msg |
| 340 |
) |
| 341 |
if is_not_found: |
| 342 |
self._result["changed"] = False |
| 343 |
self._result["notes"] = [] |
| 344 |
display.v("splunk_notes_info: no notes found (resource not found)") |
| 345 |
else: |
| 346 |
self.fail_json(msg=f"Failed to query note(s): {error_msg}") |
| 347 |
|
| 348 |
return self._result |