Debian Codegraph @

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

raw
1 # -*- coding: utf-8 -*-
2 """Pure Python utility functions for Splunk modules.
3
4 This module contains utility functions that have NO Ansible dependencies,
5 making them safe to import in unit tests without triggering Ansible imports.
6
7 """
8
9 # (c) 2018, Adam Miller ([email protected])
10 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
11
12 import re
13
14
15 # UUID regex pattern for validation
16 UUID_PATTERN = re.compile(
17 r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$",
18 re.IGNORECASE,
19 )
20
21 # Default API path components (shared across all ES modules)
22 DEFAULT_API_NAMESPACE = "servicesNS"
23 DEFAULT_API_USER = "nobody"
24 DEFAULT_API_APP = "missioncontrol"
25 DEFAULT_API_APP_SECURITY_SUITE = "SplunkEnterpriseSecuritySuite"
26
27 # Disposition mapping: module value -> API value (shared by finding and investigation)
28 DISPOSITION_TO_API = {
29 "unassigned": "disposition:0",
30 "true_positive": "disposition:1",
31 "benign_positive": "disposition:2",
32 "false_positive": "disposition:3",
33 "false_positive_inaccurate_data": "disposition:4",
34 "other": "disposition:5",
35 "undetermined": "disposition:6",
36 }
37
38 # Disposition mapping: API value -> module value
39 DISPOSITION_FROM_API = {v: k for k, v in DISPOSITION_TO_API.items()}
40
41 # Status mapping: module value -> API value (shared by finding and investigation)
42 STATUS_TO_API = {
43 "unassigned": "0",
44 "new": "1",
45 "in_progress": "2",
46 "pending": "3",
47 "resolved": "4",
48 "closed": "5",
49 }
50
51 # Status mapping: API value -> module value
52 STATUS_FROM_API = {v: k for k, v in STATUS_TO_API.items()}
53
54
55 def remove_get_keys_from_payload_dict(payload_dict, remove_key_list):
56 """Remove specified keys from a payload dictionary.
57
58 Args:
59 payload_dict: Dictionary to remove keys from.
60 remove_key_list: List of keys to remove.
61
62 Returns:
63 The modified payload_dict with specified keys removed.
64 """
65 for each_key in remove_key_list:
66 if each_key in payload_dict:
67 payload_dict.pop(each_key)
68 return payload_dict
69
70
71 def map_params_to_obj(module_params, key_transform):
72 """Convert API returned params to module params using key transformation.
73
74 This function transforms dictionary keys from API format to module format.
75 Keys are popped from the input dictionary (mutates input).
76
77 Args:
78 module_params: Dictionary with API parameter names.
79 key_transform: Dict mapping API param names to module param names.
80 Format: {api_key: module_key}
81
82 Returns:
83 Dictionary with transformed keys (module param names).
84 """
85 obj = {}
86 for k, v in key_transform.items():
87 if k in module_params and (
88 module_params.get(k) or module_params.get(k) == 0 or module_params.get(k) is False
89 ):
90 obj[v] = module_params.pop(k)
91 return obj
92
93
94 def map_obj_to_params(module_return_params, key_transform):
95 """Convert module params to API params using key transformation.
96
97 This function transforms dictionary keys from module format to API format.
98 Keys are popped from the input dictionary (mutates input).
99
100 Args:
101 module_return_params: Dictionary with module parameter names.
102 key_transform: Dict mapping API param names to module param names.
103 Format: {api_key: module_key}
104
105 Returns:
106 Dictionary with transformed keys (API param names).
107 """
108 temp = {}
109 for k, v in key_transform.items():
110 if v in module_return_params and (
111 module_return_params.get(v)
112 or module_return_params.get(v) == 0
113 or module_return_params.get(v) is False
114 ):
115 temp[k] = module_return_params.pop(v)
116 return temp
117
118
119 def set_defaults(config, defaults):
120 """Set default values in config dictionary if keys are not present.
121
122 Args:
123 config: Configuration dictionary to update.
124 defaults: Dictionary of default key-value pairs.
125
126 Returns:
127 The modified config dictionary with defaults applied.
128 """
129 for k, v in defaults.items():
130 config.setdefault(k, v)
131 return config
132
133
134 def is_uuid(value: str) -> bool:
135 """Check if a string is a valid UUID format.
136
137 Args:
138 value: The string to check.
139
140 Returns:
141 True if the string matches UUID format, False otherwise.
142 """
143 return bool(UUID_PATTERN.match(value))
144
145
146 def get_api_config_from_args(args):
147 """Extract API configuration from task arguments.
148
149 Args:
150 args: Task arguments dictionary.
151
152 Returns:
153 Tuple of (namespace, user, app) with defaults applied.
154 """
155 return (
156 args.get("api_namespace", DEFAULT_API_NAMESPACE),
157 args.get("api_user", DEFAULT_API_USER),
158 args.get("api_app", DEFAULT_API_APP),
159 )