Debian Codegraph @

ansible_collections/splunk/es/plugins/module_utils/investigation.py

raw
1 # -*- coding: utf-8 -*-
2 """Splunk Investigation module utilities for Ansible."""
3
4 # Copyright 2026 Red Hat Inc.
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7
8 from typing import Any, Optional
9
10 from ansible_collections.splunk.es.plugins.module_utils.splunk_utils import (
11 DEFAULT_API_APP,
12 DEFAULT_API_NAMESPACE,
13 DEFAULT_API_USER,
14 DISPOSITION_FROM_API,
15 STATUS_FROM_API,
16 )
17
18
19 # API path for investigations API (uses missioncontrol app)
20 INVESTIGATION_API_PATH = (
21 f"{DEFAULT_API_NAMESPACE}/{DEFAULT_API_USER}/{DEFAULT_API_APP}/public/v2/investigations"
22 )
23
24 # Sensitivity mapping: API value (capitalized) -> module value (lowercase)
25 # Used by map_investigation_from_api to convert API responses
26 SENSITIVITY_FROM_API = {
27 "White": "white",
28 "Green": "green",
29 "Amber": "amber",
30 "Red": "red",
31 "Unassigned": "unassigned",
32 }
33
34
35 def build_investigation_api_path(
36 namespace: str = DEFAULT_API_NAMESPACE,
37 user: str = DEFAULT_API_USER,
38 app: str = DEFAULT_API_APP,
39 ) -> str:
40 """Build the investigations API path from components.
41
42 Args:
43 namespace: The namespace portion of the path. Defaults to 'servicesNS'.
44 user: The user portion of the path. Defaults to 'nobody'.
45 app: The app portion of the path. Defaults to 'missioncontrol'.
46
47 Returns:
48 The complete investigations API path.
49 """
50 return f"{namespace}/{user}/{app}/public/v2/investigations"
51
52
53 def _extract_finding_ids(config: dict[str, Any]) -> Optional[list[str]]:
54 """Extract finding_ids from consolidated_findings in API response.
55
56 Args:
57 config: The API response config dictionary.
58
59 Returns:
60 List of finding IDs, or None if not present.
61 """
62 consolidated = config.get("consolidated_findings", {})
63 if not consolidated:
64 return None
65
66 event_ids = consolidated.get("event_id")
67 if not event_ids:
68 return None
69
70 # event_id can be a string (single) or list (multiple)
71 return event_ids if isinstance(event_ids, list) else [event_ids]
72
73
74 def _convert_api_enum_value(
75 result: dict[str, Any],
76 field: str,
77 mapping: dict[str, str],
78 stringify_key: bool = False,
79 ) -> None:
80 """Convert an API enum value to module format in-place.
81
82 Args:
83 result: The result dictionary to modify in-place.
84 field: The field name to convert.
85 mapping: The mapping dictionary from API to module values.
86 stringify_key: Whether to convert the key to string before lookup.
87 """
88 if field not in result or not result[field]:
89 return
90
91 value = result[field]
92 lookup_key = str(value) if stringify_key else value
93 fallback = value.lower() if isinstance(value, str) else value
94 result[field] = mapping.get(lookup_key, fallback)
95
96
97 def map_investigation_from_api(config: dict[str, Any]) -> dict[str, Any]:
98 """Convert investigation API response to module params format.
99
100 Args:
101 config: The API response config dictionary.
102
103 Returns:
104 Dictionary with module parameter names and normalized values.
105 """
106 res = {}
107
108 # Extract ref_id from investigation_guid field
109 if "investigation_guid" in config:
110 res["investigation_ref_id"] = config["investigation_guid"]
111
112 # Copy fields directly (no key transformation needed)
113 field_names = [
114 "name",
115 "description",
116 "status",
117 "disposition",
118 "owner",
119 "urgency",
120 "sensitivity",
121 "investigation_type",
122 ]
123 for field in field_names:
124 if field in config and config[field] is not None:
125 res[field] = config[field]
126
127 # Extract finding_ids from consolidated_findings
128 finding_ids = _extract_finding_ids(config)
129 if finding_ids:
130 res["finding_ids"] = finding_ids
131
132 # Convert API enum values to human-readable module format
133 _convert_api_enum_value(res, "status", STATUS_FROM_API, stringify_key=True)
134 _convert_api_enum_value(res, "disposition", DISPOSITION_FROM_API, stringify_key=True)
135 _convert_api_enum_value(res, "sensitivity", SENSITIVITY_FROM_API)
136
137 return res