| 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_vpn_qkd |
| 23 |
short_description: Configure Quantum Key Distribution server 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 vpn feature and qkd 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 |
state: |
| 80 |
description: |
| 81 |
- Indicates whether to create or remove the object. |
| 82 |
type: str |
| 83 |
required: true |
| 84 |
choices: |
| 85 |
- 'present' |
| 86 |
- 'absent' |
| 87 |
vpn_qkd: |
| 88 |
description: |
| 89 |
- Configure Quantum Key Distribution servers |
| 90 |
default: null |
| 91 |
type: dict |
| 92 |
suboptions: |
| 93 |
certificate: |
| 94 |
description: |
| 95 |
- Names of up to 4 certificates to offer to the KME. |
| 96 |
type: list |
| 97 |
elements: dict |
| 98 |
suboptions: |
| 99 |
name: |
| 100 |
description: |
| 101 |
- Certificate name. Source vpn.certificate.local.name. |
| 102 |
required: true |
| 103 |
type: str |
| 104 |
comment: |
| 105 |
description: |
| 106 |
- Comment. |
| 107 |
type: str |
| 108 |
id: |
| 109 |
description: |
| 110 |
- Quantum Key Distribution ID assigned by the KME. |
| 111 |
type: str |
| 112 |
name: |
| 113 |
description: |
| 114 |
- Quantum Key Distribution configuration name. |
| 115 |
required: true |
| 116 |
type: str |
| 117 |
peer: |
| 118 |
description: |
| 119 |
- Authenticate Quantum Key Device"s certificate with the peer/peergrp. Source user.peer.name user.peergrp.name. |
| 120 |
type: str |
| 121 |
port: |
| 122 |
description: |
| 123 |
- Port to connect to on the KME. |
| 124 |
type: int |
| 125 |
server: |
| 126 |
description: |
| 127 |
- IPv4, IPv6 or DNS address of the KME. |
| 128 |
type: str |
| 129 |
""" |
| 130 |
|
| 131 |
EXAMPLES = """ |
| 132 |
- name: Configure Quantum Key Distribution servers |
| 133 |
fortinet.fortios.fortios_vpn_qkd: |
| 134 |
vdom: "{{ vdom }}" |
| 135 |
state: "present" |
| 136 |
access_token: "<your_own_value>" |
| 137 |
vpn_qkd: |
| 138 |
certificate: |
| 139 |
- |
| 140 |
name: "default_name_4 (source vpn.certificate.local.name)" |
| 141 |
comment: "Comment." |
| 142 |
id: "6" |
| 143 |
name: "default_name_7" |
| 144 |
peer: "<your_own_value> (source user.peer.name user.peergrp.name)" |
| 145 |
port: "0" |
| 146 |
server: "192.168.100.40" |
| 147 |
""" |
| 148 |
|
| 149 |
RETURN = """ |
| 150 |
build: |
| 151 |
description: Build number of the fortigate image |
| 152 |
returned: always |
| 153 |
type: str |
| 154 |
sample: '1547' |
| 155 |
http_method: |
| 156 |
description: Last method used to provision the content into FortiGate |
| 157 |
returned: always |
| 158 |
type: str |
| 159 |
sample: 'PUT' |
| 160 |
http_status: |
| 161 |
description: Last result given by FortiGate on last operation applied |
| 162 |
returned: always |
| 163 |
type: str |
| 164 |
sample: "200" |
| 165 |
mkey: |
| 166 |
description: Master key (id) used in the last call to FortiGate |
| 167 |
returned: success |
| 168 |
type: str |
| 169 |
sample: "id" |
| 170 |
name: |
| 171 |
description: Name of the table used to fulfill the request |
| 172 |
returned: always |
| 173 |
type: str |
| 174 |
sample: "urlfilter" |
| 175 |
path: |
| 176 |
description: Path of the table used to fulfill the request |
| 177 |
returned: always |
| 178 |
type: str |
| 179 |
sample: "webfilter" |
| 180 |
revision: |
| 181 |
description: Internal revision number |
| 182 |
returned: always |
| 183 |
type: str |
| 184 |
sample: "17.0.2.10658" |
| 185 |
serial: |
| 186 |
description: Serial number of the unit |
| 187 |
returned: always |
| 188 |
type: str |
| 189 |
sample: "FGVMEVYYQT3AB5352" |
| 190 |
status: |
| 191 |
description: Indication of the operation's result |
| 192 |
returned: always |
| 193 |
type: str |
| 194 |
sample: "success" |
| 195 |
vdom: |
| 196 |
description: Virtual domain used |
| 197 |
returned: always |
| 198 |
type: str |
| 199 |
sample: "root" |
| 200 |
version: |
| 201 |
description: Version of the FortiGate |
| 202 |
returned: always |
| 203 |
type: str |
| 204 |
sample: "v5.6.3" |
| 205 |
""" |
| 206 |
from ansible.module_utils.basic import AnsibleModule |
| 207 |
from ansible.module_utils.connection import Connection |
| 208 |
from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import ( |
| 209 |
FortiOSHandler, |
| 210 |
) |
| 211 |
from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import ( |
| 212 |
check_legacy_fortiosapi, |
| 213 |
) |
| 214 |
from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import ( |
| 215 |
schema_to_module_spec, |
| 216 |
) |
| 217 |
from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import ( |
| 218 |
check_schema_versioning, |
| 219 |
) |
| 220 |
from ansible_collections.fortinet.fortios.plugins.module_utils.fortimanager.common import ( |
| 221 |
FAIL_SOCKET_MSG, |
| 222 |
) |
| 223 |
from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.data_post_processor import ( |
| 224 |
remove_invalid_fields, |
| 225 |
) |
| 226 |
from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import ( |
| 227 |
is_same_comparison, |
| 228 |
) |
| 229 |
from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import ( |
| 230 |
serialize, |
| 231 |
) |
| 232 |
from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import ( |
| 233 |
find_current_values, |
| 234 |
) |
| 235 |
from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import ( |
| 236 |
unify_data_format, |
| 237 |
) |
| 238 |
|
| 239 |
|
| 240 |
def filter_vpn_qkd_data(json): |
| 241 |
option_list = ["certificate", "comment", "id", "name", "peer", "port", "server"] |
| 242 |
|
| 243 |
json = remove_invalid_fields(json) |
| 244 |
dictionary = {} |
| 245 |
|
| 246 |
for attribute in option_list: |
| 247 |
if attribute in json and json[attribute] is not None: |
| 248 |
dictionary[attribute] = json[attribute] |
| 249 |
|
| 250 |
return dictionary |
| 251 |
|
| 252 |
|
| 253 |
def underscore_to_hyphen(data): |
| 254 |
new_data = None |
| 255 |
if isinstance(data, list): |
| 256 |
new_data = [] |
| 257 |
for i, elem in enumerate(data): |
| 258 |
new_data.append(underscore_to_hyphen(elem)) |
| 259 |
elif isinstance(data, dict): |
| 260 |
new_data = {} |
| 261 |
for k, v in data.items(): |
| 262 |
new_data[k.replace("_", "-")] = underscore_to_hyphen(v) |
| 263 |
else: |
| 264 |
return data |
| 265 |
return new_data |
| 266 |
|
| 267 |
|
| 268 |
def vpn_qkd(data, fos, check_mode=False): |
| 269 |
|
| 270 |
state = None |
| 271 |
vdom = data["vdom"] |
| 272 |
parameters = None |
| 273 |
state = data.get("state", None) |
| 274 |
vpn_qkd_data = data["vpn_qkd"] |
| 275 |
|
| 276 |
filtered_data = filter_vpn_qkd_data(vpn_qkd_data) |
| 277 |
converted_data = underscore_to_hyphen(filtered_data) |
| 278 |
|
| 279 |
|
| 280 |
if check_mode: |
| 281 |
diff = { |
| 282 |
"before": "", |
| 283 |
"after": filtered_data, |
| 284 |
} |
| 285 |
mkeyname = fos.get_mkeyname(None, None) |
| 286 |
mkey = fos.get_mkey("vpn", "qkd", filtered_data, vdom=vdom) |
| 287 |
current_data = fos.get( |
| 288 |
"vpn", "qkd", vdom=vdom, mkey=mkey, parameters=parameters |
| 289 |
) |
| 290 |
is_existed = ( |
| 291 |
current_data |
| 292 |
and current_data.get("http_status") == 200 |
| 293 |
and ( |
| 294 |
mkeyname |
| 295 |
and isinstance(current_data.get("results"), list) |
| 296 |
and len(current_data["results"]) > 0 |
| 297 |
or not mkeyname |
| 298 |
and current_data["results"] |
| 299 |
) |
| 300 |
) |
| 301 |
|
| 302 |
|
| 303 |
if state == "present" or state is True or state is None: |
| 304 |
|
| 305 |
if mkeyname is not None and mkey is None: |
| 306 |
return False, True, filtered_data, diff |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
copied_filtered_data = filtered_data.copy() |
| 311 |
copied_filtered_data.pop(mkeyname, None) |
| 312 |
unified_filtered_data = unify_data_format(copied_filtered_data) |
| 313 |
|
| 314 |
current_data_results = current_data.get("results", {}) |
| 315 |
current_config = ( |
| 316 |
current_data_results[0] |
| 317 |
if mkeyname |
| 318 |
and isinstance(current_data_results, list) |
| 319 |
and len(current_data_results) > 0 |
| 320 |
else current_data_results |
| 321 |
) |
| 322 |
if is_existed: |
| 323 |
unified_current_values = find_current_values( |
| 324 |
unified_filtered_data, |
| 325 |
unify_data_format(current_config), |
| 326 |
) |
| 327 |
|
| 328 |
is_same = is_same_comparison( |
| 329 |
serialize(unified_current_values), serialize(unified_filtered_data) |
| 330 |
) |
| 331 |
|
| 332 |
return ( |
| 333 |
False, |
| 334 |
not is_same, |
| 335 |
filtered_data, |
| 336 |
{"before": unified_current_values, "after": unified_filtered_data}, |
| 337 |
) |
| 338 |
|
| 339 |
|
| 340 |
return False, True, filtered_data, diff |
| 341 |
|
| 342 |
if state == "absent": |
| 343 |
if mkey is None: |
| 344 |
return ( |
| 345 |
False, |
| 346 |
False, |
| 347 |
filtered_data, |
| 348 |
{"before": current_data["results"][0], "after": ""}, |
| 349 |
) |
| 350 |
|
| 351 |
if is_existed: |
| 352 |
return ( |
| 353 |
False, |
| 354 |
True, |
| 355 |
filtered_data, |
| 356 |
{"before": current_data["results"][0], "after": ""}, |
| 357 |
) |
| 358 |
return False, False, filtered_data, {} |
| 359 |
|
| 360 |
return True, False, {"reason: ": "Must provide state parameter"}, {} |
| 361 |
|
| 362 |
|
| 363 |
data_copy = data.copy() |
| 364 |
data_copy["vpn_qkd"] = filtered_data |
| 365 |
fos.do_member_operation( |
| 366 |
"vpn", |
| 367 |
"qkd", |
| 368 |
data_copy, |
| 369 |
) |
| 370 |
|
| 371 |
if state == "present" or state is True: |
| 372 |
return fos.set( |
| 373 |
"vpn", "qkd", data=converted_data, vdom=vdom, parameters=parameters |
| 374 |
) |
| 375 |
|
| 376 |
elif state == "absent": |
| 377 |
return fos.delete( |
| 378 |
"vpn", "qkd", mkey=converted_data["name"], vdom=vdom, parameters=parameters |
| 379 |
) |
| 380 |
else: |
| 381 |
fos._module.fail_json(msg="state must be present or absent!") |
| 382 |
|
| 383 |
|
| 384 |
def is_successful_status(resp): |
| 385 |
return ( |
| 386 |
"status" in resp |
| 387 |
and resp["status"] == "success" |
| 388 |
or "http_status" in resp |
| 389 |
and resp["http_status"] == 200 |
| 390 |
or "http_method" in resp |
| 391 |
and resp["http_method"] == "DELETE" |
| 392 |
and resp["http_status"] == 404 |
| 393 |
) |
| 394 |
|
| 395 |
|
| 396 |
def fortios_vpn(data, fos, check_mode): |
| 397 |
|
| 398 |
if data["vpn_qkd"]: |
| 399 |
resp = vpn_qkd(data, fos, check_mode) |
| 400 |
else: |
| 401 |
fos._module.fail_json(msg="missing task body: %s" % ("vpn_qkd")) |
| 402 |
if isinstance(resp, tuple) and len(resp) == 4: |
| 403 |
return resp |
| 404 |
return ( |
| 405 |
not is_successful_status(resp), |
| 406 |
is_successful_status(resp) |
| 407 |
and (resp["revision_changed"] if "revision_changed" in resp else True), |
| 408 |
resp, |
| 409 |
{}, |
| 410 |
) |
| 411 |
|
| 412 |
|
| 413 |
versioned_schema = { |
| 414 |
"type": "list", |
| 415 |
"elements": "dict", |
| 416 |
"children": { |
| 417 |
"name": {"v_range": [["v7.4.2", ""]], "type": "string", "required": True}, |
| 418 |
"server": {"v_range": [["v7.4.2", ""]], "type": "string"}, |
| 419 |
"port": {"v_range": [["v7.4.2", ""]], "type": "integer"}, |
| 420 |
"id": {"v_range": [["v7.4.2", ""]], "type": "string"}, |
| 421 |
"peer": {"v_range": [["v7.4.2", ""]], "type": "string"}, |
| 422 |
"certificate": { |
| 423 |
"type": "list", |
| 424 |
"elements": "dict", |
| 425 |
"children": { |
| 426 |
"name": { |
| 427 |
"v_range": [["v7.4.2", ""]], |
| 428 |
"type": "string", |
| 429 |
"required": True, |
| 430 |
} |
| 431 |
}, |
| 432 |
"v_range": [["v7.4.2", ""]], |
| 433 |
}, |
| 434 |
"comment": {"v_range": [["v7.4.2", ""]], "type": "string"}, |
| 435 |
}, |
| 436 |
"v_range": [["v7.4.2", ""]], |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
def main(): |
| 441 |
module_spec = schema_to_module_spec(versioned_schema) |
| 442 |
mkeyname = "name" |
| 443 |
fields = { |
| 444 |
"access_token": {"required": False, "type": "str", "no_log": True}, |
| 445 |
"enable_log": {"required": False, "type": "bool", "default": False}, |
| 446 |
"vdom": {"required": False, "type": "str", "default": "root"}, |
| 447 |
"member_path": {"required": False, "type": "str"}, |
| 448 |
"member_state": { |
| 449 |
"type": "str", |
| 450 |
"required": False, |
| 451 |
"choices": ["present", "absent"], |
| 452 |
}, |
| 453 |
"state": {"required": True, "type": "str", "choices": ["present", "absent"]}, |
| 454 |
"vpn_qkd": {"required": False, "type": "dict", "default": None, "options": {}}, |
| 455 |
} |
| 456 |
for attribute_name in module_spec["options"]: |
| 457 |
fields["vpn_qkd"]["options"][attribute_name] = module_spec["options"][ |
| 458 |
attribute_name |
| 459 |
] |
| 460 |
if mkeyname and mkeyname == attribute_name: |
| 461 |
fields["vpn_qkd"]["options"][attribute_name]["required"] = True |
| 462 |
|
| 463 |
module = AnsibleModule(argument_spec=fields, supports_check_mode=True) |
| 464 |
check_legacy_fortiosapi(module) |
| 465 |
|
| 466 |
is_error = False |
| 467 |
has_changed = False |
| 468 |
result = None |
| 469 |
diff = None |
| 470 |
|
| 471 |
versions_check_result = None |
| 472 |
if module._socket_path: |
| 473 |
connection = Connection(module._socket_path) |
| 474 |
if "access_token" in module.params: |
| 475 |
connection.set_custom_option("access_token", module.params["access_token"]) |
| 476 |
|
| 477 |
if "enable_log" in module.params: |
| 478 |
connection.set_custom_option("enable_log", module.params["enable_log"]) |
| 479 |
else: |
| 480 |
connection.set_custom_option("enable_log", False) |
| 481 |
fos = FortiOSHandler(connection, module, mkeyname, admin_passwd_header=False) |
| 482 |
versions_check_result = check_schema_versioning( |
| 483 |
fos, versioned_schema, "vpn_qkd" |
| 484 |
) |
| 485 |
|
| 486 |
is_error, has_changed, result, diff = fortios_vpn( |
| 487 |
module.params, fos, module.check_mode |
| 488 |
) |
| 489 |
|
| 490 |
else: |
| 491 |
module.fail_json(**FAIL_SOCKET_MSG) |
| 492 |
|
| 493 |
if versions_check_result and versions_check_result["matched"] is False: |
| 494 |
module.warn( |
| 495 |
"Ansible has detected version mismatch between FortOS system and your playbook, see more details by specifying option -vvv" |
| 496 |
) |
| 497 |
|
| 498 |
if not is_error: |
| 499 |
if versions_check_result and versions_check_result["matched"] is False: |
| 500 |
module.exit_json( |
| 501 |
changed=has_changed, |
| 502 |
version_check_warning=versions_check_result, |
| 503 |
meta=result, |
| 504 |
diff=diff, |
| 505 |
) |
| 506 |
else: |
| 507 |
module.exit_json(changed=has_changed, meta=result, diff=diff) |
| 508 |
else: |
| 509 |
if versions_check_result and versions_check_result["matched"] is False: |
| 510 |
module.fail_json( |
| 511 |
msg="Error in repo", |
| 512 |
version_check_warning=versions_check_result, |
| 513 |
meta=result, |
| 514 |
) |
| 515 |
else: |
| 516 |
module.fail_json(msg="Error in repo", meta=result) |
| 517 |
|
| 518 |
|
| 519 |
if __name__ == "__main__": |
| 520 |
main() |