| 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 data_inputs_network |
| 22 |
""" |
| 23 |
|
| 24 |
from ansible.errors import AnsibleActionFail |
| 25 |
from ansible.module_utils.connection import Connection |
| 26 |
from ansible.module_utils.six.moves.urllib.parse import quote_plus |
| 27 |
from ansible.plugins.action import ActionBase |
| 28 |
|
| 29 |
from ansible_collections.splunk.es.plugins.module_utils import dict_utils as utils |
| 30 |
from ansible_collections.splunk.es.plugins.module_utils.splunk import ( |
| 31 |
SplunkRequest, |
| 32 |
check_argspec, |
| 33 |
) |
| 34 |
from ansible_collections.splunk.es.plugins.module_utils.splunk_utils import ( |
| 35 |
map_obj_to_params, |
| 36 |
map_params_to_obj, |
| 37 |
remove_get_keys_from_payload_dict, |
| 38 |
) |
| 39 |
from ansible_collections.splunk.es.plugins.modules.splunk_data_inputs_network import DOCUMENTATION |
| 40 |
|
| 41 |
|
| 42 |
class ActionModule(ActionBase): |
| 43 |
"""action module""" |
| 44 |
|
| 45 |
def __init__(self, *args, **kwargs): |
| 46 |
super().__init__(*args, **kwargs) |
| 47 |
self._result = None |
| 48 |
self.api_object = "servicesNS/nobody/search/data/inputs" |
| 49 |
self.module_return = "data_inputs_network" |
| 50 |
self.key_transform = { |
| 51 |
"name": "name", |
| 52 |
"connection_host": "connection_host", |
| 53 |
"disabled": "disabled", |
| 54 |
"index": "index", |
| 55 |
"host": "host", |
| 56 |
"no_appending_timestamp": "no_appending_timestamp", |
| 57 |
"no_priority_stripping": "no_priority_stripping", |
| 58 |
"rawTcpDoneTimeout": "raw_tcp_done_timeout", |
| 59 |
"restrictToHost": "restrict_to_host", |
| 60 |
"queue": "queue", |
| 61 |
"SSL": "ssl", |
| 62 |
"source": "source", |
| 63 |
"sourcetype": "sourcetype", |
| 64 |
"token": "token", |
| 65 |
"password": "password", |
| 66 |
"requireClientCert": "require_client_cert", |
| 67 |
"rootCA": "root_ca", |
| 68 |
"serverCert": "server_cert", |
| 69 |
"cipherSuite": "cipher_suite", |
| 70 |
} |
| 71 |
|
| 72 |
def fail_json(self, msg): |
| 73 |
"""Replace the AnsibleModule fail_json here |
| 74 |
:param msg: The message for the failure |
| 75 |
:type msg: str |
| 76 |
""" |
| 77 |
msg = msg.replace("(basic.py)", self._task.action) |
| 78 |
raise AnsibleActionFail(msg) |
| 79 |
|
| 80 |
def map_params_to_object(self, config, datatype=None): |
| 81 |
res = {} |
| 82 |
|
| 83 |
res["name"] = config["name"] |
| 84 |
res.update(map_params_to_obj(config["content"], self.key_transform)) |
| 85 |
|
| 86 |
# API returns back "index", even though it can't be set within /tcp/cooked |
| 87 |
if datatype: |
| 88 |
if datatype == "cooked" and "index" in res: |
| 89 |
res.pop("index") |
| 90 |
elif datatype == "splunktcptoken": |
| 91 |
if "index" in res: |
| 92 |
res.pop("index") |
| 93 |
if "host" in res: |
| 94 |
res.pop("host") |
| 95 |
if "disabled" in res: |
| 96 |
res.pop("disabled") |
| 97 |
|
| 98 |
return res |
| 99 |
|
| 100 |
# This function is meant to construct the URL and handle GET, POST and DELETE calls |
| 101 |
# depending on th context. The URLs constructed and handled are: |
| 102 |
# /tcp/raw[/{name}] |
| 103 |
# /tcp/cooked[/{name}] |
| 104 |
# /tcp/splunktcptoken[/{name}] |
| 105 |
# /tcp/ssl[/{name}] |
| 106 |
# /udp[/{name}] |
| 107 |
def request_by_path( |
| 108 |
self, |
| 109 |
conn_request, |
| 110 |
protocol, |
| 111 |
datatype=None, |
| 112 |
name=None, |
| 113 |
req_type="get", |
| 114 |
payload=None, |
| 115 |
): |
| 116 |
query_dict = None |
| 117 |
url = "" |
| 118 |
|
| 119 |
if protocol == "tcp": |
| 120 |
if not datatype: |
| 121 |
raise AnsibleActionFail("No datatype specified for TCP input") |
| 122 |
|
| 123 |
# In all cases except "ssl" datatype, creation of objects is handled |
| 124 |
# by a POST request to the parent directory. Therefore name shouldn't |
| 125 |
# be included in the URL. |
| 126 |
if not name or (req_type == "post_create" and datatype != "ssl"): |
| 127 |
name = "" |
| 128 |
|
| 129 |
url = f"{self.api_object}/{protocol}/{datatype}/{quote_plus(str(name))}" |
| 130 |
# if no "name" was provided |
| 131 |
if url[-1] == "/": |
| 132 |
url = url[:-1] |
| 133 |
|
| 134 |
elif protocol == "udp": |
| 135 |
if datatype: |
| 136 |
raise AnsibleActionFail("Datatype specified for UDP input") |
| 137 |
if not name or req_type == "post_create": |
| 138 |
name = "" |
| 139 |
|
| 140 |
url = f"{self.api_object}/{protocol}/{quote_plus(str(name))}" |
| 141 |
# if no "name" was provided |
| 142 |
if url[-1] == "/": |
| 143 |
url = url[:-1] |
| 144 |
else: |
| 145 |
raise AnsibleActionFail( |
| 146 |
"Incompatible protocol specified. Please specify 'tcp' or 'udp'", |
| 147 |
) |
| 148 |
|
| 149 |
if req_type == "get": |
| 150 |
query_dict = conn_request.get_by_path(url) |
| 151 |
elif req_type == "post_create": |
| 152 |
query_dict = conn_request.create_update(url, data=payload) |
| 153 |
elif req_type == "post_update": |
| 154 |
payload.pop("name") |
| 155 |
query_dict = conn_request.create_update(url, data=payload) |
| 156 |
elif req_type == "delete": |
| 157 |
query_dict = conn_request.delete_by_path(url) |
| 158 |
|
| 159 |
return query_dict |
| 160 |
|
| 161 |
def search_for_resource_name(self, conn_request, protocol, datatype, name): |
| 162 |
query_dict = self.request_by_path( |
| 163 |
conn_request, |
| 164 |
protocol, |
| 165 |
datatype, |
| 166 |
name, |
| 167 |
) |
| 168 |
|
| 169 |
search_result = {} |
| 170 |
|
| 171 |
if query_dict: |
| 172 |
search_result = self.map_params_to_object( |
| 173 |
query_dict["entry"][0], |
| 174 |
datatype, |
| 175 |
) |
| 176 |
|
| 177 |
# Adding back protocol and datatype fields for better clarity |
| 178 |
search_result["protocol"] = protocol |
| 179 |
if datatype: |
| 180 |
search_result["datatype"] = datatype |
| 181 |
if datatype == "ssl": |
| 182 |
search_result["name"] = name |
| 183 |
|
| 184 |
return search_result |
| 185 |
|
| 186 |
# If certain parameters are present, Splunk appends the value of those parameters |
| 187 |
# to the name. Therefore this causes idempotency to fail. This function looks for |
| 188 |
# said parameters and conducts checks to see if the configuration already exists. |
| 189 |
def parse_config(self, conn_request, want_conf): |
| 190 |
old_name = None |
| 191 |
protocol = want_conf["protocol"] |
| 192 |
datatype = want_conf.get("datatype") |
| 193 |
|
| 194 |
if not want_conf.get("name"): |
| 195 |
raise AnsibleActionFail("No name specified for merge action") |
| 196 |
else: |
| 197 |
# Int values confuse diff |
| 198 |
want_conf["name"] = str(want_conf["name"]) |
| 199 |
|
| 200 |
old_name = want_conf["name"] |
| 201 |
|
| 202 |
if ( |
| 203 |
want_conf.get("restrict_to_host") |
| 204 |
and old_name.split(":")[0] == want_conf["restrict_to_host"] |
| 205 |
): |
| 206 |
old_name = old_name.split(":")[1] |
| 207 |
|
| 208 |
# If "restrictToHost" parameter is set, the value of this parameter is appended |
| 209 |
# to the numerical name meant to represent port number |
| 210 |
if ( |
| 211 |
want_conf.get("restrict_to_host") |
| 212 |
and want_conf["restrict_to_host"] not in want_conf["name"] |
| 213 |
): |
| 214 |
want_conf["name"] = f"{want_conf['restrict_to_host']}:{want_conf['name']}" |
| 215 |
|
| 216 |
# If datatype is "splunktcptoken", the value "splunktcptoken://" is appended |
| 217 |
# to the name |
| 218 |
elif ( |
| 219 |
datatype |
| 220 |
and datatype == "splunktcptoken" |
| 221 |
and "splunktcptoken://" not in want_conf["name"] |
| 222 |
): |
| 223 |
want_conf["name"] = f"splunktcptoken://{want_conf['name']}" |
| 224 |
|
| 225 |
name = want_conf["name"] |
| 226 |
|
| 227 |
# If the above parameters are present, but the object doesn't exist |
| 228 |
# the value of the parameters shouldn't be prepended to the name. |
| 229 |
# Otherwise Splunk returns 400. This check is takes advantage of this |
| 230 |
# and sets the correct name. |
| 231 |
have_conf = None |
| 232 |
try: |
| 233 |
have_conf = self.search_for_resource_name( |
| 234 |
conn_request, |
| 235 |
protocol, |
| 236 |
datatype, |
| 237 |
name, |
| 238 |
) |
| 239 |
# while creating new conf, we need to only use numerical values |
| 240 |
# splunk will later append param value to it. |
| 241 |
if not have_conf: |
| 242 |
want_conf["name"] = old_name |
| 243 |
except AnsibleActionFail: |
| 244 |
want_conf["name"] = old_name |
| 245 |
have_conf = self.search_for_resource_name( |
| 246 |
conn_request, |
| 247 |
protocol, |
| 248 |
datatype, |
| 249 |
old_name, |
| 250 |
) |
| 251 |
|
| 252 |
# SSL response returns a blank "name" parameter, which causes problems |
| 253 |
if datatype == "ssl": |
| 254 |
have_conf["name"] = want_conf["name"] |
| 255 |
|
| 256 |
return have_conf, protocol, datatype, name, old_name |
| 257 |
|
| 258 |
def delete_module_api_config(self, conn_request, config): |
| 259 |
before = [] |
| 260 |
after = None |
| 261 |
changed = False |
| 262 |
for want_conf in config: |
| 263 |
if not want_conf.get("name"): |
| 264 |
raise AnsibleActionFail("No name specified") |
| 265 |
|
| 266 |
have_conf, protocol, datatype, name, _old_name = self.parse_config( |
| 267 |
conn_request, |
| 268 |
want_conf, |
| 269 |
) |
| 270 |
|
| 271 |
if protocol == "tcp" and datatype == "ssl": |
| 272 |
raise AnsibleActionFail("Deleted state not supported for SSL") |
| 273 |
|
| 274 |
if have_conf: |
| 275 |
before.append(have_conf) |
| 276 |
if not self._task.check_mode: |
| 277 |
self.request_by_path( |
| 278 |
conn_request, |
| 279 |
protocol, |
| 280 |
datatype, |
| 281 |
name, |
| 282 |
req_type="delete", |
| 283 |
) |
| 284 |
changed = True |
| 285 |
after = [] |
| 286 |
|
| 287 |
ret_config = {} |
| 288 |
ret_config["before"] = before |
| 289 |
ret_config["after"] = after |
| 290 |
|
| 291 |
return ret_config, changed |
| 292 |
|
| 293 |
def configure_module_api(self, conn_request, config): |
| 294 |
before = [] |
| 295 |
after = [] |
| 296 |
changed = False |
| 297 |
|
| 298 |
for want_conf in config: |
| 299 |
# Add to the THIS list for the value which needs to be excluded |
| 300 |
# from HAVE params when compared to WANT param like 'ID' can be |
| 301 |
# part of HAVE param but may not be part of your WANT param |
| 302 |
remove_from_diff_compare = [ |
| 303 |
"datatype", |
| 304 |
"protocol", |
| 305 |
"cipher_suite", |
| 306 |
] |
| 307 |
|
| 308 |
have_conf, protocol, datatype, name, old_name = self.parse_config( |
| 309 |
conn_request, |
| 310 |
want_conf, |
| 311 |
) |
| 312 |
|
| 313 |
if protocol == "tcp" and datatype == "ssl" and self._task.args["state"] == "replaced": |
| 314 |
raise AnsibleActionFail("Replaced state not supported for SSL") |
| 315 |
|
| 316 |
if have_conf: |
| 317 |
want_conf = utils.remove_empties(want_conf) |
| 318 |
diff = utils.dict_diff(have_conf, want_conf) |
| 319 |
|
| 320 |
# Check if have_conf has extra parameters |
| 321 |
if self._task.args["state"] == "replaced": |
| 322 |
diff2 = utils.dict_diff(want_conf, have_conf) |
| 323 |
if len(diff) or len(diff2): |
| 324 |
diff.update(diff2) |
| 325 |
|
| 326 |
if diff: |
| 327 |
diff = remove_get_keys_from_payload_dict( |
| 328 |
diff, |
| 329 |
remove_from_diff_compare, |
| 330 |
) |
| 331 |
if diff: |
| 332 |
before.append(have_conf) |
| 333 |
if self._task.args["state"] == "merged": |
| 334 |
want_conf = utils.remove_empties( |
| 335 |
utils.dict_merge(have_conf, want_conf), |
| 336 |
) |
| 337 |
want_conf = remove_get_keys_from_payload_dict( |
| 338 |
want_conf, |
| 339 |
remove_from_diff_compare, |
| 340 |
) |
| 341 |
changed = True |
| 342 |
|
| 343 |
if self._task.check_mode: |
| 344 |
# In check mode, return the expected configuration |
| 345 |
# Adding back protocol and datatype fields for better clarity |
| 346 |
want_conf["protocol"] = protocol |
| 347 |
if datatype: |
| 348 |
want_conf["datatype"] = datatype |
| 349 |
after.append(want_conf) |
| 350 |
else: |
| 351 |
payload = map_obj_to_params( |
| 352 |
want_conf, |
| 353 |
self.key_transform, |
| 354 |
) |
| 355 |
api_response = self.request_by_path( |
| 356 |
conn_request, |
| 357 |
protocol, |
| 358 |
datatype, |
| 359 |
name, |
| 360 |
req_type="post_update", |
| 361 |
payload=payload, |
| 362 |
) |
| 363 |
response_json = self.map_params_to_object( |
| 364 |
api_response["entry"][0], |
| 365 |
datatype, |
| 366 |
) |
| 367 |
|
| 368 |
# Adding back protocol and datatype fields for better clarity |
| 369 |
response_json["protocol"] = protocol |
| 370 |
if datatype: |
| 371 |
response_json["datatype"] = datatype |
| 372 |
|
| 373 |
after.append(response_json) |
| 374 |
elif self._task.args["state"] == "replaced": |
| 375 |
if not self._task.check_mode: |
| 376 |
self.request_by_path( |
| 377 |
conn_request, |
| 378 |
protocol, |
| 379 |
datatype, |
| 380 |
name, |
| 381 |
req_type="delete", |
| 382 |
) |
| 383 |
|
| 384 |
changed = True |
| 385 |
|
| 386 |
if self._task.check_mode: |
| 387 |
# In check mode, return the expected configuration |
| 388 |
# Adding back protocol and datatype fields for better clarity |
| 389 |
want_conf["protocol"] = protocol |
| 390 |
if datatype: |
| 391 |
want_conf["datatype"] = datatype |
| 392 |
after.append(want_conf) |
| 393 |
else: |
| 394 |
payload = map_obj_to_params( |
| 395 |
want_conf, |
| 396 |
self.key_transform, |
| 397 |
) |
| 398 |
# while creating new conf, we need to only use numerical values |
| 399 |
# splunk will later append param value to it. |
| 400 |
payload["name"] = old_name |
| 401 |
|
| 402 |
api_response = self.request_by_path( |
| 403 |
conn_request, |
| 404 |
protocol, |
| 405 |
datatype, |
| 406 |
name, |
| 407 |
req_type="post_create", |
| 408 |
payload=payload, |
| 409 |
) |
| 410 |
response_json = self.map_params_to_object( |
| 411 |
api_response["entry"][0], |
| 412 |
datatype, |
| 413 |
) |
| 414 |
|
| 415 |
# Adding back protocol and datatype fields for better clarity |
| 416 |
response_json["protocol"] = protocol |
| 417 |
if datatype: |
| 418 |
response_json["datatype"] = datatype |
| 419 |
|
| 420 |
after.append(response_json) |
| 421 |
else: |
| 422 |
before.append(have_conf) |
| 423 |
after.append(have_conf) |
| 424 |
else: |
| 425 |
before.append(have_conf) |
| 426 |
after.append(have_conf) |
| 427 |
else: |
| 428 |
changed = True |
| 429 |
want_conf = utils.remove_empties(want_conf) |
| 430 |
|
| 431 |
if self._task.check_mode: |
| 432 |
# In check mode, return the expected configuration |
| 433 |
# Adding back protocol and datatype fields for better clarity |
| 434 |
want_conf["protocol"] = protocol |
| 435 |
if datatype: |
| 436 |
want_conf["datatype"] = datatype |
| 437 |
after.extend(before) |
| 438 |
after.append(want_conf) |
| 439 |
else: |
| 440 |
payload = map_obj_to_params(want_conf, self.key_transform) |
| 441 |
|
| 442 |
api_response = self.request_by_path( |
| 443 |
conn_request, |
| 444 |
protocol, |
| 445 |
datatype, |
| 446 |
name, |
| 447 |
req_type="post_create", |
| 448 |
payload=payload, |
| 449 |
) |
| 450 |
response_json = self.map_params_to_object( |
| 451 |
api_response["entry"][0], |
| 452 |
datatype, |
| 453 |
) |
| 454 |
|
| 455 |
# Adding back protocol and datatype fields for better clarity |
| 456 |
response_json["protocol"] = protocol |
| 457 |
if datatype: |
| 458 |
response_json["datatype"] = datatype |
| 459 |
|
| 460 |
after.extend(before) |
| 461 |
after.append(response_json) |
| 462 |
if not changed: |
| 463 |
after = None |
| 464 |
|
| 465 |
ret_config = {} |
| 466 |
ret_config["before"] = before |
| 467 |
ret_config["after"] = after |
| 468 |
|
| 469 |
return ret_config, changed |
| 470 |
|
| 471 |
def run(self, tmp=None, task_vars=None): |
| 472 |
self._supports_check_mode = True |
| 473 |
self._result = super().run(tmp, task_vars) |
| 474 |
if not check_argspec(self, self._result, DOCUMENTATION): |
| 475 |
return self._result |
| 476 |
|
| 477 |
config = self._task.args.get("config") |
| 478 |
|
| 479 |
conn = Connection(self._connection.socket_path) |
| 480 |
|
| 481 |
conn_request = SplunkRequest( |
| 482 |
connection=conn, |
| 483 |
action_module=self, |
| 484 |
) |
| 485 |
|
| 486 |
if self._task.args["state"] == "gathered": |
| 487 |
if config: |
| 488 |
self._result["gathered"] = [] |
| 489 |
self._result["changed"] = False |
| 490 |
for item in config: |
| 491 |
if item.get("name"): |
| 492 |
result = self.search_for_resource_name( |
| 493 |
conn_request, |
| 494 |
item["protocol"], |
| 495 |
item.get("datatype"), |
| 496 |
item.get("name"), |
| 497 |
) |
| 498 |
if result: |
| 499 |
self._result["gathered"].append(result) |
| 500 |
else: |
| 501 |
response_list = self.request_by_path( |
| 502 |
conn_request, |
| 503 |
item["protocol"], |
| 504 |
item.get("datatype"), |
| 505 |
None, |
| 506 |
)["entry"] |
| 507 |
self._result["gathered"] = [] |
| 508 |
for response_dict in response_list: |
| 509 |
self._result["gathered"].append( |
| 510 |
self.map_params_to_object(response_dict), |
| 511 |
) |
| 512 |
else: |
| 513 |
raise AnsibleActionFail("No protocol specified") |
| 514 |
|
| 515 |
elif self._task.args["state"] == "merged" or self._task.args["state"] == "replaced": |
| 516 |
if config: |
| 517 |
( |
| 518 |
self._result[self.module_return], |
| 519 |
self._result["changed"], |
| 520 |
) = self.configure_module_api(conn_request, config) |
| 521 |
if not self._result[self.module_return]["after"]: |
| 522 |
self._result[self.module_return].pop("after") |
| 523 |
|
| 524 |
elif self._task.args["state"] == "deleted": |
| 525 |
if config: |
| 526 |
( |
| 527 |
self._result[self.module_return], |
| 528 |
self._result["changed"], |
| 529 |
) = self.delete_module_api_config(conn_request, config) |
| 530 |
if self._result[self.module_return]["after"] is None: |
| 531 |
self._result[self.module_return].pop("after") |
| 532 |
|
| 533 |
return self._result |