| 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 adaptive_response_notable_events |
| 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_adaptive_response_notable_events import ( |
| 43 |
DOCUMENTATION, |
| 44 |
) |
| 45 |
|
| 46 |
|
| 47 |
class ActionModule(ActionBase): |
| 48 |
"""action module""" |
| 49 |
|
| 50 |
def __init__(self, *args, **kwargs): |
| 51 |
super().__init__(*args, **kwargs) |
| 52 |
self._result = None |
| 53 |
self.api_object = "servicesNS/nobody/SplunkEnterpriseSecuritySuite/saved/searches" |
| 54 |
self.module_name = "adaptive_response_notable_events" |
| 55 |
self.key_transform = { |
| 56 |
"action.notable.param.default_owner": "default_owner", |
| 57 |
"action.notable.param.default_status": "default_status", |
| 58 |
"action.notable.param.drilldown_name": "drilldown_name", |
| 59 |
"action.notable.param.drilldown_search": "drilldown_search", |
| 60 |
"action.notable.param.drilldown_earliest_offset": "drilldown_earliest_offset", |
| 61 |
"action.notable.param.drilldown_latest_offset": "drilldown_latest_offset", |
| 62 |
"action.notable.param.extract_artifacts": "extract_artifacts", |
| 63 |
"action.notable.param.investigation_profiles": "investigation_profiles", |
| 64 |
"action.notable.param.next_steps": "next_steps", |
| 65 |
"action.notable.param.recommended_actions": "recommended_actions", |
| 66 |
"action.notable.param.rule_description": "description", |
| 67 |
"action.notable.param.rule_title": "name", |
| 68 |
"action.notable.param.security_domain": "security_domain", |
| 69 |
"action.notable.param.severity": "severity", |
| 70 |
"name": "correlation_search_name", |
| 71 |
} |
| 72 |
|
| 73 |
def fail_json(self, msg): |
| 74 |
"""Replace the AnsibleModule fail_json here |
| 75 |
:param msg: The message for the failure |
| 76 |
:type msg: str |
| 77 |
""" |
| 78 |
msg = msg.replace("(basic.py)", self._task.action) |
| 79 |
raise AnsibleActionFail(msg) |
| 80 |
|
| 81 |
# need to store 'recommended_actions','extract_artifacts','next_steps' and 'investigation_profiles' |
| 82 |
# since merging in the parsed form will eliminate any differences |
| 83 |
def save_params(self, want_conf): |
| 84 |
param_store = {} |
| 85 |
if "recommended_actions" in want_conf: |
| 86 |
param_store["recommended_actions"] = want_conf["recommended_actions"] |
| 87 |
if "extract_artifacts" in want_conf: |
| 88 |
param_store["extract_artifacts"] = want_conf["extract_artifacts"] |
| 89 |
if "next_steps" in want_conf: |
| 90 |
param_store["next_steps"] = want_conf["next_steps"] |
| 91 |
if "investigation_profiles" in want_conf: |
| 92 |
param_store["investigation_profiles"] = want_conf["investigation_profiles"] |
| 93 |
|
| 94 |
return param_store |
| 95 |
|
| 96 |
# responsible for correctly setting certain parameters depending on the state being triggered. |
| 97 |
# These parameters are responsible for enabling and disabling notable response actions |
| 98 |
def create_metadata(self, metadata, mode="add"): |
| 99 |
if mode == "add": |
| 100 |
if "actions" in metadata: |
| 101 |
if metadata["actions"] == "notable": |
| 102 |
pass |
| 103 |
elif ( |
| 104 |
len(metadata["actions"].split(",")) > 0 and "notable" not in metadata["actions"] |
| 105 |
): |
| 106 |
metadata["actions"] = metadata["actions"] + ", notable" |
| 107 |
else: |
| 108 |
metadata["actions"] = "notable" |
| 109 |
metadata["action.notable"] = "1" |
| 110 |
elif mode == "delete": |
| 111 |
if "actions" in metadata: |
| 112 |
if metadata["actions"] == "notable": |
| 113 |
metadata["actions"] = "" |
| 114 |
elif len(metadata["actions"].split(",")) > 0 and "notable" in metadata["actions"]: |
| 115 |
tmp_list = metadata["actions"].split(",") |
| 116 |
tmp_list.remove(" notable") |
| 117 |
metadata["actions"] = ",".join(tmp_list) |
| 118 |
metadata["action.notable"] = "0" |
| 119 |
|
| 120 |
return metadata |
| 121 |
|
| 122 |
def map_params_to_object(self, config): |
| 123 |
res = {} |
| 124 |
res["correlation_search_name"] = config["name"] |
| 125 |
|
| 126 |
res.update(map_params_to_obj(config["content"], self.key_transform)) |
| 127 |
|
| 128 |
if "extract_artifacts" in res: |
| 129 |
res["extract_artifacts"] = json.loads(res["extract_artifacts"]) |
| 130 |
|
| 131 |
if "investigation_profiles" in res: |
| 132 |
if res["investigation_profiles"] == "{}": |
| 133 |
res.pop("investigation_profiles") |
| 134 |
else: |
| 135 |
res["investigation_profiles"] = json.loads( |
| 136 |
res["investigation_profiles"], |
| 137 |
) |
| 138 |
investigation_profiles = [] |
| 139 |
for keys in res["investigation_profiles"].keys(): |
| 140 |
investigation_profiles.append(keys.split("profile://")[1]) |
| 141 |
res["investigation_profiles"] = investigation_profiles |
| 142 |
|
| 143 |
if "recommended_actions" in res: |
| 144 |
res["recommended_actions"] = res["recommended_actions"].split(",") |
| 145 |
|
| 146 |
if "next_steps" in res: |
| 147 |
next_steps = json.loads(res["next_steps"])["data"] |
| 148 |
|
| 149 |
next_steps = next_steps.split("]][[") |
| 150 |
# trimming trailing characters |
| 151 |
next_steps[0] = next_steps[0].strip("[") |
| 152 |
next_steps[-1] = next_steps[-1].strip("]") |
| 153 |
|
| 154 |
res["next_steps"] = [] |
| 155 |
for element in next_steps: |
| 156 |
res["next_steps"].append(element.split("|")[1]) |
| 157 |
|
| 158 |
if "default_status" in res: |
| 159 |
mapping = { |
| 160 |
"0": "unassigned", |
| 161 |
"1": "new", |
| 162 |
"2": "in progress", |
| 163 |
"3": "pending", |
| 164 |
"4": "resolved", |
| 165 |
"5": "closed", |
| 166 |
} |
| 167 |
res["default_status"] = mapping[res["default_status"]] |
| 168 |
|
| 169 |
# need to store correlation search details for populating future request payloads |
| 170 |
metadata = {} |
| 171 |
metadata["search"] = config["content"]["search"] |
| 172 |
metadata["actions"] = config["content"]["actions"] |
| 173 |
|
| 174 |
return res, metadata |
| 175 |
|
| 176 |
def map_objects_to_params(self, metadata, want_conf): |
| 177 |
res = {} |
| 178 |
|
| 179 |
res.update(map_obj_to_params(want_conf, self.key_transform)) |
| 180 |
res.update(self.create_metadata(metadata)) |
| 181 |
|
| 182 |
if "action.notable.param.extract_artifacts" in res: |
| 183 |
res["action.notable.param.extract_artifacts"] = json.dumps( |
| 184 |
res["action.notable.param.extract_artifacts"], |
| 185 |
) |
| 186 |
|
| 187 |
if "action.notable.param.recommended_actions" in res: |
| 188 |
res["action.notable.param.recommended_actions"] = ",".join( |
| 189 |
res["action.notable.param.recommended_actions"], |
| 190 |
) |
| 191 |
|
| 192 |
if "action.notable.param.investigation_profiles" in res: |
| 193 |
investigation_profiles = {} |
| 194 |
for element in res["action.notable.param.investigation_profiles"]: |
| 195 |
investigation_profiles["profile://" + element] = {} |
| 196 |
res["action.notable.param.investigation_profiles"] = json.dumps( |
| 197 |
investigation_profiles, |
| 198 |
) |
| 199 |
|
| 200 |
if "action.notable.param.next_steps" in res: |
| 201 |
next_steps = "".join( |
| 202 |
f"[[action|{next_step}]]" for next_step in res["action.notable.param.next_steps"] |
| 203 |
) |
| 204 |
|
| 205 |
# NOTE: version:1 appears to be hard coded when you create this via the splunk web UI |
| 206 |
next_steps_dict = {"version": 1, "data": next_steps} |
| 207 |
res["action.notable.param.next_steps"] = json.dumps( |
| 208 |
next_steps_dict, |
| 209 |
) |
| 210 |
|
| 211 |
if "action.notable.param.default_status" in res: |
| 212 |
mapping = { |
| 213 |
"unassigned": "0", |
| 214 |
"new": "1", |
| 215 |
"in progress": "2", |
| 216 |
"pending": "3", |
| 217 |
"resolved": "4", |
| 218 |
"closed": "5", |
| 219 |
} |
| 220 |
res["action.notable.param.default_status"] = mapping[ |
| 221 |
res["action.notable.param.default_status"] |
| 222 |
] |
| 223 |
|
| 224 |
# need to remove 'name', otherwise the API call will try to modify the correlation search |
| 225 |
res.pop("name") |
| 226 |
|
| 227 |
return res |
| 228 |
|
| 229 |
def search_for_resource_name(self, conn_request, correlation_search_name): |
| 230 |
query_dict = conn_request.get_by_path( |
| 231 |
f"{self.api_object}/{quote(correlation_search_name)}", |
| 232 |
) |
| 233 |
|
| 234 |
search_result = {} |
| 235 |
|
| 236 |
if query_dict: |
| 237 |
search_result, metadata = self.map_params_to_object( |
| 238 |
query_dict["entry"][0], |
| 239 |
) |
| 240 |
else: |
| 241 |
raise AnsibleActionFail( |
| 242 |
f"Correlation Search '{correlation_search_name}' doesn't exist", |
| 243 |
) |
| 244 |
|
| 245 |
return search_result, metadata |
| 246 |
|
| 247 |
# Since there is no delete operation associated with an action, |
| 248 |
# The delete operation will unset the relevant fields |
| 249 |
def delete_module_api_config(self, conn_request, config): |
| 250 |
before = [] |
| 251 |
after = None |
| 252 |
changed = False |
| 253 |
for want_conf in config: |
| 254 |
search_by_name, metadata = self.search_for_resource_name( |
| 255 |
conn_request, |
| 256 |
want_conf["correlation_search_name"], |
| 257 |
) |
| 258 |
search_by_name = utils.remove_empties(search_by_name) |
| 259 |
|
| 260 |
# Compare obtained values with a dict representing values in a 'deleted' state |
| 261 |
diff_cmp = { |
| 262 |
"correlation_search_name": want_conf["correlation_search_name"], |
| 263 |
"drilldown_earliest_offset": "$info_min_time$", |
| 264 |
"drilldown_latest_offset": "$info_max_time$", |
| 265 |
} |
| 266 |
|
| 267 |
# if the obtained values are different from 'deleted' state values |
| 268 |
if search_by_name and search_by_name != diff_cmp: |
| 269 |
before.append(search_by_name) |
| 270 |
if not self._task.check_mode: |
| 271 |
payload = { |
| 272 |
"action.notable.param.default_owner": "", |
| 273 |
"action.notable.param.default_status": "", |
| 274 |
"action.notable.param.drilldown_name": "", |
| 275 |
"action.notable.param.drilldown_search": "", |
| 276 |
"action.notable.param.drilldown_earliest_offset": "$info_min_time$", |
| 277 |
"action.notable.param.drilldown_latest_offset": "$info_max_time$", |
| 278 |
"action.notable.param.extract_artifacts": "{}", |
| 279 |
"action.notable.param.investigation_profiles": "{}", |
| 280 |
"action.notable.param.next_steps": "", |
| 281 |
"action.notable.param.recommended_actions": "", |
| 282 |
"action.notable.param.rule_description": "", |
| 283 |
"action.notable.param.rule_title": "", |
| 284 |
"action.notable.param.security_domain": "", |
| 285 |
"action.notable.param.severity": "", |
| 286 |
} |
| 287 |
payload.update(self.create_metadata(metadata, mode="delete")) |
| 288 |
url = f"{self.api_object}/{quote(want_conf['correlation_search_name'])}" |
| 289 |
conn_request.create_update( |
| 290 |
url, |
| 291 |
data=payload, |
| 292 |
) |
| 293 |
changed = True |
| 294 |
after = [] |
| 295 |
|
| 296 |
res_config = {} |
| 297 |
res_config["after"] = after |
| 298 |
res_config["before"] = before |
| 299 |
|
| 300 |
return res_config, changed |
| 301 |
|
| 302 |
def configure_module_api(self, conn_request, config): |
| 303 |
before = [] |
| 304 |
after = [] |
| 305 |
changed = False |
| 306 |
# Add to the THIS list for the value which needs to be excluded |
| 307 |
# from HAVE params when compared to WANT param like 'ID' can be |
| 308 |
# part of HAVE param but may not be part of your WANT param |
| 309 |
defaults = { |
| 310 |
"drilldown_earliest_offset": "$info_min_time$", |
| 311 |
"drilldown_latest_offset": "$info_max_time$", |
| 312 |
"extract_artifacts": { |
| 313 |
"asset": [ |
| 314 |
"src", |
| 315 |
"dest", |
| 316 |
"dvc", |
| 317 |
"orig_host", |
| 318 |
], |
| 319 |
"identity": [ |
| 320 |
"src_user", |
| 321 |
"user", |
| 322 |
"src_user_id", |
| 323 |
"src_user_role", |
| 324 |
"user_id", |
| 325 |
"user_role", |
| 326 |
"vendor_account", |
| 327 |
], |
| 328 |
}, |
| 329 |
"investigation_profiles": "{}", |
| 330 |
} |
| 331 |
remove_from_diff_compare = [] |
| 332 |
for want_conf in config: |
| 333 |
have_conf, metadata = self.search_for_resource_name( |
| 334 |
conn_request, |
| 335 |
want_conf["correlation_search_name"], |
| 336 |
) |
| 337 |
correlation_search_name = want_conf["correlation_search_name"] |
| 338 |
|
| 339 |
if "notable" in metadata["actions"]: |
| 340 |
want_conf = set_defaults(want_conf, defaults) |
| 341 |
want_conf = utils.remove_empties(want_conf) |
| 342 |
diff = utils.dict_diff(have_conf, want_conf) |
| 343 |
|
| 344 |
# Check if have_conf has extra parameters |
| 345 |
if self._task.args["state"] == "replaced": |
| 346 |
diff2 = utils.dict_diff(want_conf, have_conf) |
| 347 |
if len(diff) or len(diff2): |
| 348 |
diff.update(diff2) |
| 349 |
|
| 350 |
if diff: |
| 351 |
before.append(have_conf) |
| 352 |
if self._task.args["state"] == "merged": |
| 353 |
# need to store 'recommended_actions','extract_artifacts' |
| 354 |
# 'next_steps' and 'investigation_profiles' |
| 355 |
# since merging in the parsed form will eliminate any differences |
| 356 |
param_store = self.save_params(want_conf) |
| 357 |
|
| 358 |
want_conf = utils.remove_empties( |
| 359 |
utils.dict_merge(have_conf, want_conf), |
| 360 |
) |
| 361 |
want_conf = remove_get_keys_from_payload_dict( |
| 362 |
want_conf, |
| 363 |
remove_from_diff_compare, |
| 364 |
) |
| 365 |
|
| 366 |
# restoring parameters |
| 367 |
want_conf.update(param_store) |
| 368 |
|
| 369 |
changed = True |
| 370 |
|
| 371 |
if self._task.check_mode: |
| 372 |
# In check mode, return the expected configuration |
| 373 |
after.append(want_conf) |
| 374 |
else: |
| 375 |
payload = self.map_objects_to_params( |
| 376 |
metadata, |
| 377 |
want_conf, |
| 378 |
) |
| 379 |
|
| 380 |
url = f"{self.api_object}/{quote(correlation_search_name)}" |
| 381 |
api_response = conn_request.create_update( |
| 382 |
url, |
| 383 |
data=payload, |
| 384 |
) |
| 385 |
response_json, metadata = self.map_params_to_object( |
| 386 |
api_response["entry"][0], |
| 387 |
) |
| 388 |
|
| 389 |
after.append(response_json) |
| 390 |
elif self._task.args["state"] == "replaced": |
| 391 |
if not self._task.check_mode: |
| 392 |
self.delete_module_api_config( |
| 393 |
conn_request=conn_request, |
| 394 |
config=[want_conf], |
| 395 |
) |
| 396 |
changed = True |
| 397 |
|
| 398 |
if self._task.check_mode: |
| 399 |
# In check mode, return the expected configuration |
| 400 |
after.append(want_conf) |
| 401 |
else: |
| 402 |
payload = self.map_objects_to_params( |
| 403 |
metadata, |
| 404 |
want_conf, |
| 405 |
) |
| 406 |
|
| 407 |
url = f"{self.api_object}/{quote(correlation_search_name)}" |
| 408 |
api_response = conn_request.create_update( |
| 409 |
url, |
| 410 |
data=payload, |
| 411 |
) |
| 412 |
response_json, metadata = self.map_params_to_object( |
| 413 |
api_response["entry"][0], |
| 414 |
) |
| 415 |
|
| 416 |
after.append(response_json) |
| 417 |
else: |
| 418 |
before.append(have_conf) |
| 419 |
after.append(have_conf) |
| 420 |
else: |
| 421 |
changed = True |
| 422 |
want_conf = utils.remove_empties(want_conf) |
| 423 |
|
| 424 |
if self._task.check_mode: |
| 425 |
# In check mode, return the expected configuration |
| 426 |
after.extend(before) |
| 427 |
after.append(want_conf) |
| 428 |
else: |
| 429 |
payload = self.map_objects_to_params(metadata, want_conf) |
| 430 |
|
| 431 |
url = f"{self.api_object}/{quote(correlation_search_name)}" |
| 432 |
api_response = conn_request.create_update( |
| 433 |
url, |
| 434 |
data=payload, |
| 435 |
) |
| 436 |
|
| 437 |
response_json, metadata = self.map_params_to_object( |
| 438 |
api_response["entry"][0], |
| 439 |
) |
| 440 |
|
| 441 |
after.extend(before) |
| 442 |
after.append(response_json) |
| 443 |
if not changed: |
| 444 |
after = None |
| 445 |
|
| 446 |
res_config = {} |
| 447 |
res_config["after"] = after |
| 448 |
res_config["before"] = before |
| 449 |
|
| 450 |
return res_config, changed |
| 451 |
|
| 452 |
def run(self, tmp=None, task_vars=None): |
| 453 |
self._supports_check_mode = True |
| 454 |
self._result = super().run(tmp, task_vars) |
| 455 |
|
| 456 |
if not check_argspec(self, self._result, DOCUMENTATION): |
| 457 |
return self._result |
| 458 |
|
| 459 |
self._result[self.module_name] = {} |
| 460 |
|
| 461 |
# config is retrieved as a string; need to deserialise |
| 462 |
config = self._task.args.get("config") |
| 463 |
|
| 464 |
conn = Connection(self._connection.socket_path) |
| 465 |
|
| 466 |
conn_request = SplunkRequest( |
| 467 |
action_module=self, |
| 468 |
connection=conn, |
| 469 |
not_rest_data_keys=["state"], |
| 470 |
) |
| 471 |
|
| 472 |
if self._task.args["state"] == "gathered": |
| 473 |
if config: |
| 474 |
self._result["changed"] = False |
| 475 |
self._result[self.module_name]["gathered"] = [] |
| 476 |
for item in config: |
| 477 |
self._result[self.module_name]["gathered"].append( |
| 478 |
self.search_for_resource_name( |
| 479 |
conn_request, |
| 480 |
item["correlation_search_name"], |
| 481 |
)[0], |
| 482 |
) |
| 483 |
|
| 484 |
elif self._task.args["state"] == "merged" or self._task.args["state"] == "replaced": |
| 485 |
( |
| 486 |
self._result[self.module_name], |
| 487 |
self._result["changed"], |
| 488 |
) = self.configure_module_api(conn_request, config) |
| 489 |
if self._result[self.module_name]["after"] is None: |
| 490 |
self._result[self.module_name].pop("after") |
| 491 |
|
| 492 |
elif self._task.args["state"] == "deleted": |
| 493 |
( |
| 494 |
self._result[self.module_name], |
| 495 |
self._result["changed"], |
| 496 |
) = self.delete_module_api_config(conn_request, config) |
| 497 |
if self._result[self.module_name]["after"] is None: |
| 498 |
self._result[self.module_name].pop("after") |
| 499 |
|
| 500 |
return self._result |