| 1 |
# |
| 2 |
# Copyright 2022 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 module file for splunk_correlation_searches |
| 22 |
""" |
| 23 |
|
| 24 |
import json |
| 25 |
|
| 26 |
from ansible.errors import AnsibleActionFail |
| 27 |
from ansible.module_utils.connection import Connection |
| 28 |
from ansible.module_utils.six.moves.urllib.parse import quote |
| 29 |
from ansible.plugins.action import ActionBase |
| 30 |
|
| 31 |
from ansible_collections.splunk.es.plugins.module_utils import dict_utils as utils |
| 32 |
from ansible_collections.splunk.es.plugins.module_utils.splunk import ( |
| 33 |
SplunkRequest, |
| 34 |
check_argspec, |
| 35 |
) |
| 36 |
from ansible_collections.splunk.es.plugins.module_utils.splunk_utils import ( |
| 37 |
map_obj_to_params, |
| 38 |
map_params_to_obj, |
| 39 |
remove_get_keys_from_payload_dict, |
| 40 |
set_defaults, |
| 41 |
) |
| 42 |
from ansible_collections.splunk.es.plugins.modules.splunk_correlation_searches import DOCUMENTATION |
| 43 |
|
| 44 |
|
| 45 |
class ActionModule(ActionBase): |
| 46 |
"""action module""" |
| 47 |
|
| 48 |
def __init__(self, *args, **kwargs): |
| 49 |
super().__init__(*args, **kwargs) |
| 50 |
self._result = None |
| 51 |
self.api_object = "servicesNS/nobody/SplunkEnterpriseSecuritySuite/saved/searches" |
| 52 |
self.module_name = "correlation_searches" |
| 53 |
self.key_transform = { |
| 54 |
"disabled": "disabled", |
| 55 |
"name": "name", |
| 56 |
"description": "description", |
| 57 |
"search": "search", |
| 58 |
"action.correlationsearch.annotations": "annotations", |
| 59 |
"request.ui_dispatch_app": "ui_dispatch_context", |
| 60 |
"dispatch.earliest_time": "time_earliest", |
| 61 |
"dispatch.latest_time": "time_latest", |
| 62 |
"cron_schedule": "cron_schedule", |
| 63 |
"realtime_schedule": "scheduling", |
| 64 |
"schedule_window": "schedule_window", |
| 65 |
"schedule_priority": "schedule_priority", |
| 66 |
"alert.digest_mode": "trigger_alert", |
| 67 |
"alert_type": "trigger_alert_when", |
| 68 |
"alert_comparator": "trigger_alert_when_condition", |
| 69 |
"alert_threshold": "trigger_alert_when_value", |
| 70 |
"alert.suppress": "suppress_alerts", |
| 71 |
"alert.suppress.period": "throttle_window_duration", |
| 72 |
"alert.suppress.fields": "throttle_fields_to_group_by", |
| 73 |
} |
| 74 |
|
| 75 |
def fail_json(self, msg): |
| 76 |
"""Replace the AnsibleModule fail_json here |
| 77 |
:param msg: The message for the failure |
| 78 |
:type msg: str |
| 79 |
""" |
| 80 |
msg = msg.replace("(basic.py)", self._task.action) |
| 81 |
raise AnsibleActionFail(msg) |
| 82 |
|
| 83 |
# need to store 'annotations' and 'throttle_fields_to_group_by' |
| 84 |
# since merging in the parsed form will eliminate any differences |
| 85 |
# This is because these fields are getting converted from strings |
| 86 |
# to lists/dictionaries, and so these fields need to be compared |
| 87 |
# as such |
| 88 |
def save_params(self, want_conf): |
| 89 |
param_store = {} |
| 90 |
if "annotations" in want_conf: |
| 91 |
param_store["annotations"] = want_conf["annotations"] |
| 92 |
if "throttle_fields_to_group_by" in want_conf: |
| 93 |
param_store["throttle_fields_to_group_by"] = want_conf["throttle_fields_to_group_by"] |
| 94 |
|
| 95 |
return param_store |
| 96 |
|
| 97 |
def map_params_to_object(self, config): |
| 98 |
res = {} |
| 99 |
|
| 100 |
res["app"] = config["acl"]["app"] |
| 101 |
res.update(map_params_to_obj(config["content"], self.key_transform)) |
| 102 |
res.update(map_params_to_obj(config, self.key_transform)) |
| 103 |
|
| 104 |
if "scheduling" in res: |
| 105 |
if res["scheduling"]: |
| 106 |
res["scheduling"] = "realtime" |
| 107 |
else: |
| 108 |
res["scheduling"] = "continuous" |
| 109 |
|
| 110 |
if "trigger_alert" in res: |
| 111 |
if res["trigger_alert"]: |
| 112 |
res["trigger_alert"] = "once" |
| 113 |
else: |
| 114 |
res["trigger_alert"] = "for each result" |
| 115 |
|
| 116 |
if "throttle_fields_to_group_by" in res: |
| 117 |
res["throttle_fields_to_group_by"] = res["throttle_fields_to_group_by"].split(",") |
| 118 |
|
| 119 |
if "annotations" in res: |
| 120 |
res["annotations"] = json.loads(res["annotations"]) |
| 121 |
|
| 122 |
custom = [] |
| 123 |
|
| 124 |
# need to check for custom annotation frameworks |
| 125 |
for k, v in res["annotations"].items(): |
| 126 |
if k in {"cis20", "nist", "mitre_attack", "kill_chain_phases"}: |
| 127 |
continue |
| 128 |
entry = {} |
| 129 |
entry["framework"] = k |
| 130 |
entry["custom_annotations"] = v |
| 131 |
custom.append(entry) |
| 132 |
|
| 133 |
if custom: |
| 134 |
for entry in custom: |
| 135 |
res["annotations"].pop(entry["framework"]) |
| 136 |
res["annotations"]["custom"] = custom |
| 137 |
|
| 138 |
return res |
| 139 |
|
| 140 |
def map_objects_to_params(self, want_conf): |
| 141 |
res = {} |
| 142 |
|
| 143 |
# setting parameters that enable correlation search |
| 144 |
res["action.correlationsearch.enabled"] = "1" |
| 145 |
res["is_scheduled"] = True |
| 146 |
res["dispatch.rt_backfill"] = True |
| 147 |
res["action.correlationsearch.label"] = want_conf["name"] |
| 148 |
|
| 149 |
res.update(map_obj_to_params(want_conf, self.key_transform)) |
| 150 |
|
| 151 |
if "realtime_schedule" in res: |
| 152 |
if res["realtime_schedule"] == "realtime": |
| 153 |
res["realtime_schedule"] = True |
| 154 |
else: |
| 155 |
res["realtime_schedule"] = False |
| 156 |
|
| 157 |
if "alert.digest_mode" in res: |
| 158 |
if res["alert.digest_mode"] == "once": |
| 159 |
res["alert.digest_mode"] = True |
| 160 |
else: |
| 161 |
res["alert.digest_mode"] = False |
| 162 |
|
| 163 |
if "alert.suppress.fields" in res: |
| 164 |
res["alert.suppress.fields"] = ",".join( |
| 165 |
res["alert.suppress.fields"], |
| 166 |
) |
| 167 |
|
| 168 |
if ( |
| 169 |
"action.correlationsearch.annotations" in res |
| 170 |
and "custom" in res["action.correlationsearch.annotations"] |
| 171 |
): |
| 172 |
for ele in res["action.correlationsearch.annotations"]["custom"]: |
| 173 |
res["action.correlationsearch.annotations"][ele["framework"]] = ele[ |
| 174 |
"custom_annotations" |
| 175 |
] |
| 176 |
res["action.correlationsearch.annotations"].pop("custom") |
| 177 |
res["action.correlationsearch.annotations"] = json.dumps( |
| 178 |
res["action.correlationsearch.annotations"], |
| 179 |
) |
| 180 |
|
| 181 |
return res |
| 182 |
|
| 183 |
def search_for_resource_name(self, conn_request, correlation_search_name): |
| 184 |
query_dict = conn_request.get_by_path( |
| 185 |
f"{self.api_object}/{quote(correlation_search_name)}", |
| 186 |
) |
| 187 |
|
| 188 |
search_result = {} |
| 189 |
|
| 190 |
if query_dict: |
| 191 |
search_result = self.map_params_to_object(query_dict["entry"][0]) |
| 192 |
|
| 193 |
return search_result |
| 194 |
|
| 195 |
def delete_module_api_config(self, conn_request, config): |
| 196 |
before = [] |
| 197 |
after = None |
| 198 |
changed = False |
| 199 |
for want_conf in config: |
| 200 |
search_by_name = self.search_for_resource_name( |
| 201 |
conn_request, |
| 202 |
want_conf["name"], |
| 203 |
) |
| 204 |
|
| 205 |
if search_by_name: |
| 206 |
before.append(search_by_name) |
| 207 |
if not self._task.check_mode: |
| 208 |
url = f"{self.api_object}/{quote(want_conf['name'])}" |
| 209 |
conn_request.delete_by_path( |
| 210 |
url, |
| 211 |
) |
| 212 |
changed = True |
| 213 |
after = [] |
| 214 |
|
| 215 |
res_config = {} |
| 216 |
res_config["after"] = after |
| 217 |
res_config["before"] = before |
| 218 |
|
| 219 |
return res_config, changed |
| 220 |
|
| 221 |
def configure_module_api(self, conn_request, config): |
| 222 |
before = [] |
| 223 |
after = [] |
| 224 |
changed = False |
| 225 |
# Add to the THIS list for the value which needs to be excluded |
| 226 |
# from HAVE params when compared to WANT param like 'ID' can be |
| 227 |
# part of HAVE param but may not be part of your WANT param |
| 228 |
defaults = {} |
| 229 |
remove_from_diff_compare = [] |
| 230 |
for want_conf in config: |
| 231 |
have_conf = self.search_for_resource_name( |
| 232 |
conn_request, |
| 233 |
want_conf["name"], |
| 234 |
) |
| 235 |
|
| 236 |
if have_conf: |
| 237 |
want_conf = set_defaults(want_conf, defaults) |
| 238 |
want_conf = utils.remove_empties(want_conf) |
| 239 |
diff = utils.dict_diff(have_conf, want_conf) |
| 240 |
|
| 241 |
# Check if have_conf has extra parameters |
| 242 |
if self._task.args["state"] == "replaced": |
| 243 |
diff2 = utils.dict_diff(want_conf, have_conf) |
| 244 |
if len(diff) or len(diff2): |
| 245 |
diff.update(diff2) |
| 246 |
|
| 247 |
if diff: |
| 248 |
name = want_conf["name"] |
| 249 |
before.append(have_conf) |
| 250 |
if self._task.args["state"] == "merged": |
| 251 |
# need to store 'annotations' and 'throttle_group_by_field' |
| 252 |
# since merging in the parsed form will eliminate any differences |
| 253 |
param_store = self.save_params(want_conf) |
| 254 |
|
| 255 |
want_conf = utils.remove_empties( |
| 256 |
utils.dict_merge(have_conf, want_conf), |
| 257 |
) |
| 258 |
want_conf = remove_get_keys_from_payload_dict( |
| 259 |
want_conf, |
| 260 |
remove_from_diff_compare, |
| 261 |
) |
| 262 |
|
| 263 |
# restoring parameters |
| 264 |
want_conf.update(param_store) |
| 265 |
|
| 266 |
changed = True |
| 267 |
|
| 268 |
if self._task.check_mode: |
| 269 |
# In check mode, return the expected config |
| 270 |
after.append(want_conf) |
| 271 |
else: |
| 272 |
payload = self.map_objects_to_params(want_conf) |
| 273 |
|
| 274 |
url = f"{self.api_object}/{quote(name)}" |
| 275 |
api_response = conn_request.create_update( |
| 276 |
url, |
| 277 |
data=payload, |
| 278 |
) |
| 279 |
response_json = self.map_params_to_object( |
| 280 |
api_response["entry"][0], |
| 281 |
) |
| 282 |
|
| 283 |
after.append(response_json) |
| 284 |
elif self._task.args["state"] == "replaced": |
| 285 |
if not self._task.check_mode: |
| 286 |
self.delete_module_api_config( |
| 287 |
conn_request=conn_request, |
| 288 |
config=[want_conf], |
| 289 |
) |
| 290 |
changed = True |
| 291 |
|
| 292 |
if self._task.check_mode: |
| 293 |
# In check mode, return the expected configuration |
| 294 |
after.append(want_conf) |
| 295 |
else: |
| 296 |
payload = self.map_objects_to_params(want_conf) |
| 297 |
|
| 298 |
url = f"{self.api_object}/{quote(name)}" |
| 299 |
|
| 300 |
# while creating new correlation search, this is how to set the 'app' field |
| 301 |
if "app" in want_conf: |
| 302 |
url = url.replace( |
| 303 |
"SplunkEnterpriseSecuritySuite", |
| 304 |
want_conf["app"], |
| 305 |
) |
| 306 |
|
| 307 |
api_response = conn_request.create_update( |
| 308 |
url, |
| 309 |
data=payload, |
| 310 |
) |
| 311 |
response_json = self.map_params_to_object( |
| 312 |
api_response["entry"][0], |
| 313 |
) |
| 314 |
|
| 315 |
after.append(response_json) |
| 316 |
else: |
| 317 |
before.append(have_conf) |
| 318 |
after.append(have_conf) |
| 319 |
else: |
| 320 |
changed = True |
| 321 |
want_conf = utils.remove_empties(want_conf) |
| 322 |
name = want_conf["name"] |
| 323 |
|
| 324 |
if self._task.check_mode: |
| 325 |
# In check mode, return the expected configuration |
| 326 |
after.extend(before) |
| 327 |
after.append(want_conf) |
| 328 |
else: |
| 329 |
payload = self.map_objects_to_params(want_conf) |
| 330 |
|
| 331 |
url = f"{self.api_object}/{quote(name)}" |
| 332 |
|
| 333 |
# while creating new correlation search, this is how to set the 'app' field |
| 334 |
if "app" in want_conf: |
| 335 |
url = url.replace( |
| 336 |
"SplunkEnterpriseSecuritySuite", |
| 337 |
want_conf["app"], |
| 338 |
) |
| 339 |
|
| 340 |
api_response = conn_request.create_update( |
| 341 |
url, |
| 342 |
data=payload, |
| 343 |
) |
| 344 |
response_json = self.map_params_to_object( |
| 345 |
api_response["entry"][0], |
| 346 |
) |
| 347 |
|
| 348 |
after.extend(before) |
| 349 |
after.append(response_json) |
| 350 |
if not changed: |
| 351 |
after = None |
| 352 |
|
| 353 |
res_config = {} |
| 354 |
res_config["after"] = after |
| 355 |
res_config["before"] = before |
| 356 |
|
| 357 |
return res_config, changed |
| 358 |
|
| 359 |
def run(self, tmp=None, task_vars=None): |
| 360 |
self._supports_check_mode = True |
| 361 |
self._result = super().run(tmp, task_vars) |
| 362 |
|
| 363 |
if not check_argspec(self, self._result, DOCUMENTATION): |
| 364 |
return self._result |
| 365 |
|
| 366 |
self._result[self.module_name] = {} |
| 367 |
|
| 368 |
# config is retrieved as a string; need to deserialise |
| 369 |
config = self._task.args.get("config") |
| 370 |
|
| 371 |
conn = Connection(self._connection.socket_path) |
| 372 |
|
| 373 |
conn_request = SplunkRequest( |
| 374 |
action_module=self, |
| 375 |
connection=conn, |
| 376 |
not_rest_data_keys=["state"], |
| 377 |
) |
| 378 |
|
| 379 |
if self._task.args["state"] == "gathered": |
| 380 |
if config: |
| 381 |
self._result["changed"] = False |
| 382 |
self._result["gathered"] = [] |
| 383 |
for item in config: |
| 384 |
result = self.search_for_resource_name( |
| 385 |
conn_request, |
| 386 |
item["name"], |
| 387 |
) |
| 388 |
if result: |
| 389 |
self._result["gathered"].append(result) |
| 390 |
elif self._task.args["state"] == "merged" or self._task.args["state"] == "replaced": |
| 391 |
( |
| 392 |
self._result[self.module_name], |
| 393 |
self._result["changed"], |
| 394 |
) = self.configure_module_api(conn_request, config) |
| 395 |
if self._result[self.module_name]["after"] is None: |
| 396 |
self._result[self.module_name].pop("after") |
| 397 |
|
| 398 |
elif self._task.args["state"] == "deleted": |
| 399 |
( |
| 400 |
self._result[self.module_name], |
| 401 |
self._result["changed"], |
| 402 |
) = self.delete_module_api_config(conn_request, config) |
| 403 |
if self._result[self.module_name]["after"] is None: |
| 404 |
self._result[self.module_name].pop("after") |
| 405 |
|
| 406 |
return self._result |