| 1 |
|
| 2 |
from __future__ import absolute_import, division, print_function |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
__metaclass__ = type |
| 13 |
|
| 14 |
ANSIBLE_METADATA = { |
| 15 |
"status": ["preview"], |
| 16 |
"supported_by": "community", |
| 17 |
"metadata_version": "1.1", |
| 18 |
} |
| 19 |
|
| 20 |
DOCUMENTATION = """ |
| 21 |
--- |
| 22 |
module: fortios_user_quarantine |
| 23 |
short_description: Configure quarantine support in Fortinet's FortiOS and FortiGate. |
| 24 |
description: |
| 25 |
- This module is able to configure a FortiGate or FortiOS (FOS) device by allowing the |
| 26 |
user to set and modify user feature and quarantine category. |
| 27 |
Examples include all parameters and values need to be adjusted to datasources before usage. |
| 28 |
Tested with FOS v6.0.0 |
| 29 |
version_added: "2.0.0" |
| 30 |
author: |
| 31 |
- Link Zheng (@chillancezen) |
| 32 |
- Jie Xue (@JieX19) |
| 33 |
- Hongbin Lu (@fgtdev-hblu) |
| 34 |
- Frank Shen (@frankshen01) |
| 35 |
- Miguel Angel Munoz (@mamunozgonzalez) |
| 36 |
- Nicolas Thomas (@thomnico) |
| 37 |
notes: |
| 38 |
- Legacy fortiosapi has been deprecated, httpapi is the preferred way to run playbooks |
| 39 |
|
| 40 |
- The module supports check_mode. |
| 41 |
|
| 42 |
requirements: |
| 43 |
- ansible>=2.16 |
| 44 |
options: |
| 45 |
access_token: |
| 46 |
description: |
| 47 |
- Token-based authentication. |
| 48 |
Generated from GUI of Fortigate. |
| 49 |
type: str |
| 50 |
required: false |
| 51 |
enable_log: |
| 52 |
description: |
| 53 |
- Enable/Disable logging for task. |
| 54 |
type: bool |
| 55 |
required: false |
| 56 |
default: false |
| 57 |
vdom: |
| 58 |
description: |
| 59 |
- Virtual domain, among those defined previously. A vdom is a |
| 60 |
virtual instance of the FortiGate that can be configured and |
| 61 |
used as a different unit. |
| 62 |
type: str |
| 63 |
default: root |
| 64 |
member_path: |
| 65 |
type: str |
| 66 |
description: |
| 67 |
- Member attribute path to operate on. |
| 68 |
- Delimited by a slash character if there are more than one attribute. |
| 69 |
- Parameter marked with member_path is legitimate for doing member operation. |
| 70 |
member_state: |
| 71 |
type: str |
| 72 |
description: |
| 73 |
- Add or delete a member under specified attribute path. |
| 74 |
- When member_state is specified, the state option is ignored. |
| 75 |
choices: |
| 76 |
- 'present' |
| 77 |
- 'absent' |
| 78 |
|
| 79 |
user_quarantine: |
| 80 |
description: |
| 81 |
- Configure quarantine support. |
| 82 |
default: null |
| 83 |
type: dict |
| 84 |
suboptions: |
| 85 |
firewall_groups: |
| 86 |
description: |
| 87 |
- Firewall address group which includes all quarantine MAC address. Source firewall.addrgrp.name. |
| 88 |
type: str |
| 89 |
quarantine: |
| 90 |
description: |
| 91 |
- Enable/disable quarantine. |
| 92 |
type: str |
| 93 |
choices: |
| 94 |
- 'enable' |
| 95 |
- 'disable' |
| 96 |
targets: |
| 97 |
description: |
| 98 |
- Quarantine entry to hold multiple MACs. |
| 99 |
type: list |
| 100 |
elements: dict |
| 101 |
suboptions: |
| 102 |
description: |
| 103 |
description: |
| 104 |
- Description for the quarantine entry. |
| 105 |
type: str |
| 106 |
entry: |
| 107 |
description: |
| 108 |
- Quarantine entry name. |
| 109 |
required: true |
| 110 |
type: str |
| 111 |
macs: |
| 112 |
description: |
| 113 |
- Quarantine MACs. |
| 114 |
type: list |
| 115 |
elements: dict |
| 116 |
suboptions: |
| 117 |
description: |
| 118 |
description: |
| 119 |
- Description for the quarantine MAC. |
| 120 |
type: str |
| 121 |
drop: |
| 122 |
description: |
| 123 |
- Enable/disable dropping of quarantined device traffic. |
| 124 |
type: str |
| 125 |
choices: |
| 126 |
- 'disable' |
| 127 |
- 'enable' |
| 128 |
entry_id: |
| 129 |
description: |
| 130 |
- FSW entry id for the quarantine MAC. |
| 131 |
type: int |
| 132 |
mac: |
| 133 |
description: |
| 134 |
- Quarantine MAC. |
| 135 |
required: true |
| 136 |
type: str |
| 137 |
parent: |
| 138 |
description: |
| 139 |
- Parent entry name. |
| 140 |
type: str |
| 141 |
traffic_policy: |
| 142 |
description: |
| 143 |
- Traffic policy for quarantined MACs. Source switch-controller.traffic-policy.name. |
| 144 |
type: str |
| 145 |
""" |
| 146 |
|
| 147 |
EXAMPLES = """ |
| 148 |
- name: Configure quarantine support. |
| 149 |
fortinet.fortios.fortios_user_quarantine: |
| 150 |
vdom: "{{ vdom }}" |
| 151 |
user_quarantine: |
| 152 |
firewall_groups: "<your_own_value> (source firewall.addrgrp.name)" |
| 153 |
quarantine: "enable" |
| 154 |
targets: |
| 155 |
- |
| 156 |
description: "<your_own_value>" |
| 157 |
entry: "<your_own_value>" |
| 158 |
macs: |
| 159 |
- |
| 160 |
description: "<your_own_value>" |
| 161 |
drop: "disable" |
| 162 |
entry_id: "2147483647" |
| 163 |
mac: "<your_own_value>" |
| 164 |
parent: "<your_own_value>" |
| 165 |
traffic_policy: "<your_own_value> (source switch-controller.traffic-policy.name)" |
| 166 |
""" |
| 167 |
|
| 168 |
RETURN = """ |
| 169 |
build: |
| 170 |
description: Build number of the fortigate image |
| 171 |
returned: always |
| 172 |
type: str |
| 173 |
sample: '1547' |
| 174 |
http_method: |
| 175 |
description: Last method used to provision the content into FortiGate |
| 176 |
returned: always |
| 177 |
type: str |
| 178 |
sample: 'PUT' |
| 179 |
http_status: |
| 180 |
description: Last result given by FortiGate on last operation applied |
| 181 |
returned: always |
| 182 |
type: str |
| 183 |
sample: "200" |
| 184 |
mkey: |
| 185 |
description: Master key (id) used in the last call to FortiGate |
| 186 |
returned: success |
| 187 |
type: str |
| 188 |
sample: "id" |
| 189 |
name: |
| 190 |
description: Name of the table used to fulfill the request |
| 191 |
returned: always |
| 192 |
type: str |
| 193 |
sample: "urlfilter" |
| 194 |
path: |
| 195 |
description: Path of the table used to fulfill the request |
| 196 |
returned: always |
| 197 |
type: str |
| 198 |
sample: "webfilter" |
| 199 |
revision: |
| 200 |
description: Internal revision number |
| 201 |
returned: always |
| 202 |
type: str |
| 203 |
sample: "17.0.2.10658" |
| 204 |
serial: |
| 205 |
description: Serial number of the unit |
| 206 |
returned: always |
| 207 |
type: str |
| 208 |
sample: "FGVMEVYYQT3AB5352" |
| 209 |
status: |
| 210 |
description: Indication of the operation's result |
| 211 |
returned: always |
| 212 |
type: str |
| 213 |
sample: "success" |
| 214 |
vdom: |
| 215 |
description: Virtual domain used |
| 216 |
returned: always |
| 217 |
type: str |
| 218 |
sample: "root" |
| 219 |
version: |
| 220 |
description: Version of the FortiGate |
| 221 |
returned: always |
| 222 |
type: str |
| 223 |
sample: "v5.6.3" |
| 224 |
""" |
| 225 |
from ansible.module_utils.basic import AnsibleModule |
| 226 |
from ansible.module_utils.connection import Connection |
| 227 |
from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import ( |
| 228 |
FortiOSHandler, |
| 229 |
) |
| 230 |
from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import ( |
| 231 |
check_legacy_fortiosapi, |
| 232 |
) |
| 233 |
from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import ( |
| 234 |
schema_to_module_spec, |
| 235 |
) |
| 236 |
from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import ( |
| 237 |
check_schema_versioning, |
| 238 |
) |
| 239 |
from ansible_collections.fortinet.fortios.plugins.module_utils.fortimanager.common import ( |
| 240 |
FAIL_SOCKET_MSG, |
| 241 |
) |
| 242 |
from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.data_post_processor import ( |
| 243 |
remove_invalid_fields, |
| 244 |
) |
| 245 |
from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import ( |
| 246 |
is_same_comparison, |
| 247 |
) |
| 248 |
from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import ( |
| 249 |
serialize, |
| 250 |
) |
| 251 |
from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import ( |
| 252 |
find_current_values, |
| 253 |
) |
| 254 |
from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import ( |
| 255 |
unify_data_format, |
| 256 |
) |
| 257 |
|
| 258 |
|
| 259 |
def filter_user_quarantine_data(json): |
| 260 |
option_list = ["firewall_groups", "quarantine", "targets", "traffic_policy"] |
| 261 |
|
| 262 |
json = remove_invalid_fields(json) |
| 263 |
dictionary = {} |
| 264 |
|
| 265 |
for attribute in option_list: |
| 266 |
if attribute in json and json[attribute] is not None: |
| 267 |
dictionary[attribute] = json[attribute] |
| 268 |
|
| 269 |
return dictionary |
| 270 |
|
| 271 |
|
| 272 |
def underscore_to_hyphen(data): |
| 273 |
new_data = None |
| 274 |
if isinstance(data, list): |
| 275 |
new_data = [] |
| 276 |
for i, elem in enumerate(data): |
| 277 |
new_data.append(underscore_to_hyphen(elem)) |
| 278 |
elif isinstance(data, dict): |
| 279 |
new_data = {} |
| 280 |
for k, v in data.items(): |
| 281 |
new_data[k.replace("_", "-")] = underscore_to_hyphen(v) |
| 282 |
else: |
| 283 |
return data |
| 284 |
return new_data |
| 285 |
|
| 286 |
|
| 287 |
def user_quarantine(data, fos, check_mode=False): |
| 288 |
|
| 289 |
state = None |
| 290 |
vdom = data["vdom"] |
| 291 |
parameters = None |
| 292 |
state = data.get("state", None) |
| 293 |
user_quarantine_data = data["user_quarantine"] |
| 294 |
|
| 295 |
filtered_data = filter_user_quarantine_data(user_quarantine_data) |
| 296 |
converted_data = underscore_to_hyphen(filtered_data) |
| 297 |
|
| 298 |
|
| 299 |
if check_mode: |
| 300 |
diff = { |
| 301 |
"before": "", |
| 302 |
"after": filtered_data, |
| 303 |
} |
| 304 |
mkeyname = fos.get_mkeyname(None, None) |
| 305 |
mkey = fos.get_mkey("user", "quarantine", filtered_data, vdom=vdom) |
| 306 |
current_data = fos.get( |
| 307 |
"user", "quarantine", vdom=vdom, mkey=mkey, parameters=parameters |
| 308 |
) |
| 309 |
is_existed = ( |
| 310 |
current_data |
| 311 |
and current_data.get("http_status") == 200 |
| 312 |
and ( |
| 313 |
mkeyname |
| 314 |
and isinstance(current_data.get("results"), list) |
| 315 |
and len(current_data["results"]) > 0 |
| 316 |
or not mkeyname |
| 317 |
and current_data["results"] |
| 318 |
) |
| 319 |
) |
| 320 |
|
| 321 |
|
| 322 |
if state == "present" or state is True or state is None: |
| 323 |
|
| 324 |
if mkeyname is not None and mkey is None: |
| 325 |
return False, True, filtered_data, diff |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
copied_filtered_data = filtered_data.copy() |
| 330 |
copied_filtered_data.pop(mkeyname, None) |
| 331 |
unified_filtered_data = unify_data_format(copied_filtered_data) |
| 332 |
|
| 333 |
current_data_results = current_data.get("results", {}) |
| 334 |
current_config = ( |
| 335 |
current_data_results[0] |
| 336 |
if mkeyname |
| 337 |
and isinstance(current_data_results, list) |
| 338 |
and len(current_data_results) > 0 |
| 339 |
else current_data_results |
| 340 |
) |
| 341 |
if is_existed: |
| 342 |
unified_current_values = find_current_values( |
| 343 |
unified_filtered_data, |
| 344 |
unify_data_format(current_config), |
| 345 |
) |
| 346 |
|
| 347 |
is_same = is_same_comparison( |
| 348 |
serialize(unified_current_values), serialize(unified_filtered_data) |
| 349 |
) |
| 350 |
|
| 351 |
return ( |
| 352 |
False, |
| 353 |
not is_same, |
| 354 |
filtered_data, |
| 355 |
{"before": unified_current_values, "after": unified_filtered_data}, |
| 356 |
) |
| 357 |
|
| 358 |
|
| 359 |
return False, True, filtered_data, diff |
| 360 |
|
| 361 |
if state == "absent": |
| 362 |
if mkey is None: |
| 363 |
return ( |
| 364 |
False, |
| 365 |
False, |
| 366 |
filtered_data, |
| 367 |
{"before": current_data["results"][0], "after": ""}, |
| 368 |
) |
| 369 |
|
| 370 |
if is_existed: |
| 371 |
return ( |
| 372 |
False, |
| 373 |
True, |
| 374 |
filtered_data, |
| 375 |
{"before": current_data["results"][0], "after": ""}, |
| 376 |
) |
| 377 |
return False, False, filtered_data, {} |
| 378 |
|
| 379 |
return True, False, {"reason: ": "Must provide state parameter"}, {} |
| 380 |
|
| 381 |
|
| 382 |
data_copy = data.copy() |
| 383 |
data_copy["user_quarantine"] = filtered_data |
| 384 |
fos.do_member_operation( |
| 385 |
"user", |
| 386 |
"quarantine", |
| 387 |
data_copy, |
| 388 |
) |
| 389 |
|
| 390 |
return fos.set( |
| 391 |
"user", "quarantine", data=converted_data, vdom=vdom, parameters=parameters |
| 392 |
) |
| 393 |
|
| 394 |
|
| 395 |
def is_successful_status(resp): |
| 396 |
return ( |
| 397 |
"status" in resp |
| 398 |
and resp["status"] == "success" |
| 399 |
or "http_status" in resp |
| 400 |
and resp["http_status"] == 200 |
| 401 |
or "http_method" in resp |
| 402 |
and resp["http_method"] == "DELETE" |
| 403 |
and resp["http_status"] == 404 |
| 404 |
) |
| 405 |
|
| 406 |
|
| 407 |
def fortios_user(data, fos, check_mode): |
| 408 |
|
| 409 |
if data["user_quarantine"]: |
| 410 |
resp = user_quarantine(data, fos, check_mode) |
| 411 |
else: |
| 412 |
fos._module.fail_json(msg="missing task body: %s" % ("user_quarantine")) |
| 413 |
if isinstance(resp, tuple) and len(resp) == 4: |
| 414 |
return resp |
| 415 |
return ( |
| 416 |
not is_successful_status(resp), |
| 417 |
is_successful_status(resp) |
| 418 |
and (resp["revision_changed"] if "revision_changed" in resp else True), |
| 419 |
resp, |
| 420 |
{}, |
| 421 |
) |
| 422 |
|
| 423 |
|
| 424 |
versioned_schema = { |
| 425 |
"v_range": [["v6.0.0", ""]], |
| 426 |
"type": "dict", |
| 427 |
"children": { |
| 428 |
"quarantine": { |
| 429 |
"v_range": [["v6.0.0", ""]], |
| 430 |
"type": "string", |
| 431 |
"options": [{"value": "enable"}, {"value": "disable"}], |
| 432 |
}, |
| 433 |
"traffic_policy": {"v_range": [["v6.2.0", ""]], "type": "string"}, |
| 434 |
"firewall_groups": {"v_range": [["v6.4.0", ""]], "type": "string"}, |
| 435 |
"targets": { |
| 436 |
"type": "list", |
| 437 |
"elements": "dict", |
| 438 |
"children": { |
| 439 |
"entry": { |
| 440 |
"v_range": [["v6.0.0", ""]], |
| 441 |
"type": "string", |
| 442 |
"required": True, |
| 443 |
}, |
| 444 |
"description": {"v_range": [["v6.0.0", ""]], "type": "string"}, |
| 445 |
"macs": { |
| 446 |
"type": "list", |
| 447 |
"elements": "dict", |
| 448 |
"children": { |
| 449 |
"mac": { |
| 450 |
"v_range": [["v6.0.0", ""]], |
| 451 |
"type": "string", |
| 452 |
"required": True, |
| 453 |
}, |
| 454 |
"description": {"v_range": [["v6.0.0", ""]], "type": "string"}, |
| 455 |
"drop": { |
| 456 |
"v_range": [["v6.4.0", ""]], |
| 457 |
"type": "string", |
| 458 |
"options": [{"value": "disable"}, {"value": "enable"}], |
| 459 |
}, |
| 460 |
"parent": { |
| 461 |
"v_range": [["v6.0.0", "v7.0.5"], ["v7.2.0", "v7.2.0"]], |
| 462 |
"type": "string", |
| 463 |
}, |
| 464 |
"entry_id": { |
| 465 |
"v_range": [["v6.0.0", "v6.0.11"], ["v6.2.3", "v6.2.3"]], |
| 466 |
"type": "integer", |
| 467 |
}, |
| 468 |
}, |
| 469 |
"v_range": [["v6.0.0", ""]], |
| 470 |
}, |
| 471 |
}, |
| 472 |
"v_range": [["v6.0.0", ""]], |
| 473 |
}, |
| 474 |
}, |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
def main(): |
| 479 |
module_spec = schema_to_module_spec(versioned_schema) |
| 480 |
mkeyname = None |
| 481 |
fields = { |
| 482 |
"access_token": {"required": False, "type": "str", "no_log": True}, |
| 483 |
"enable_log": {"required": False, "type": "bool", "default": False}, |
| 484 |
"vdom": {"required": False, "type": "str", "default": "root"}, |
| 485 |
"member_path": {"required": False, "type": "str"}, |
| 486 |
"member_state": { |
| 487 |
"type": "str", |
| 488 |
"required": False, |
| 489 |
"choices": ["present", "absent"], |
| 490 |
}, |
| 491 |
"user_quarantine": { |
| 492 |
"required": False, |
| 493 |
"type": "dict", |
| 494 |
"default": None, |
| 495 |
"options": {}, |
| 496 |
}, |
| 497 |
} |
| 498 |
for attribute_name in module_spec["options"]: |
| 499 |
fields["user_quarantine"]["options"][attribute_name] = module_spec["options"][ |
| 500 |
attribute_name |
| 501 |
] |
| 502 |
if mkeyname and mkeyname == attribute_name: |
| 503 |
fields["user_quarantine"]["options"][attribute_name]["required"] = True |
| 504 |
|
| 505 |
module = AnsibleModule(argument_spec=fields, supports_check_mode=True) |
| 506 |
check_legacy_fortiosapi(module) |
| 507 |
|
| 508 |
is_error = False |
| 509 |
has_changed = False |
| 510 |
result = None |
| 511 |
diff = None |
| 512 |
|
| 513 |
versions_check_result = None |
| 514 |
if module._socket_path: |
| 515 |
connection = Connection(module._socket_path) |
| 516 |
if "access_token" in module.params: |
| 517 |
connection.set_custom_option("access_token", module.params["access_token"]) |
| 518 |
|
| 519 |
if "enable_log" in module.params: |
| 520 |
connection.set_custom_option("enable_log", module.params["enable_log"]) |
| 521 |
else: |
| 522 |
connection.set_custom_option("enable_log", False) |
| 523 |
fos = FortiOSHandler(connection, module, mkeyname, admin_passwd_header=False) |
| 524 |
versions_check_result = check_schema_versioning( |
| 525 |
fos, versioned_schema, "user_quarantine" |
| 526 |
) |
| 527 |
|
| 528 |
is_error, has_changed, result, diff = fortios_user( |
| 529 |
module.params, fos, module.check_mode |
| 530 |
) |
| 531 |
|
| 532 |
else: |
| 533 |
module.fail_json(**FAIL_SOCKET_MSG) |
| 534 |
|
| 535 |
if versions_check_result and versions_check_result["matched"] is False: |
| 536 |
module.warn( |
| 537 |
"Ansible has detected version mismatch between FortOS system and your playbook, see more details by specifying option -vvv" |
| 538 |
) |
| 539 |
|
| 540 |
if not is_error: |
| 541 |
if versions_check_result and versions_check_result["matched"] is False: |
| 542 |
module.exit_json( |
| 543 |
changed=has_changed, |
| 544 |
version_check_warning=versions_check_result, |
| 545 |
meta=result, |
| 546 |
diff=diff, |
| 547 |
) |
| 548 |
else: |
| 549 |
module.exit_json(changed=has_changed, meta=result, diff=diff) |
| 550 |
else: |
| 551 |
if versions_check_result and versions_check_result["matched"] is False: |
| 552 |
module.fail_json( |
| 553 |
msg="Error in repo", |
| 554 |
version_check_warning=versions_check_result, |
| 555 |
meta=result, |
| 556 |
) |
| 557 |
else: |
| 558 |
module.fail_json(msg="Error in repo", meta=result) |
| 559 |
|
| 560 |
|
| 561 |
if __name__ == "__main__": |
| 562 |
main() |