| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2024, Anvitha Jain (@anvjain) <[email protected]> |
| 5 |
|
| 6 |
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
|
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"} |
| 13 |
|
| 14 |
DOCUMENTATION = r""" |
| 15 |
--- |
| 16 |
module: ndo_macsec_policy |
| 17 |
short_description: Manage MACsec Policies on Cisco Nexus Dashboard Orchestrator (NDO). |
| 18 |
description: |
| 19 |
- Manage MACsec Policies on Cisco Nexus Dashboard Orchestrator (NDO). |
| 20 |
- This module is only supported on ND v3.1 (NDO v4.3) and later. |
| 21 |
author: |
| 22 |
- Anvitha Jain (@anvjain) |
| 23 |
options: |
| 24 |
template: |
| 25 |
description: |
| 26 |
- The name of the template. |
| 27 |
- The template must be a fabric policy template. |
| 28 |
type: str |
| 29 |
required: true |
| 30 |
macsec_policy: |
| 31 |
description: |
| 32 |
- The name of the MACsec Policy. |
| 33 |
type: str |
| 34 |
aliases: [ name ] |
| 35 |
macsec_policy_uuid: |
| 36 |
description: |
| 37 |
- The UUID of the MACsec Policy. |
| 38 |
- This parameter is required when the O(macsec_policy) needs to be updated. |
| 39 |
type: str |
| 40 |
aliases: [ uuid ] |
| 41 |
description: |
| 42 |
description: |
| 43 |
- The description of the MACsec Policy. |
| 44 |
type: str |
| 45 |
admin_state: |
| 46 |
description: |
| 47 |
- The administrative state of the MACsec Policy. (Enables or disables the policy) |
| 48 |
- The default value is C(enabled). |
| 49 |
type: str |
| 50 |
choices: [ enabled, disabled ] |
| 51 |
interface_type: |
| 52 |
description: |
| 53 |
- The type of the interfaces this policy will be applied to. |
| 54 |
type: str |
| 55 |
choices: [ fabric, access ] |
| 56 |
default: fabric |
| 57 |
cipher_suite: |
| 58 |
description: |
| 59 |
- The cipher suite to be used for encryption. |
| 60 |
- The default value is C(256_gcm_aes_xpn). |
| 61 |
type: str |
| 62 |
choices: [ 128_gcm_aes, 128_gcm_aes_xpn, 256_gcm_aes, 256_gcm_aes_xpn ] |
| 63 |
window_size: |
| 64 |
description: |
| 65 |
- The window size defines the maximum number of frames that can be received out of order |
| 66 |
- before a replay attack is detected. |
| 67 |
- The value must be between 0 and 4294967295. |
| 68 |
- The default value is 0 for type C(fabric) and 64 for type C(access). |
| 69 |
type: int |
| 70 |
security_policy: |
| 71 |
description: |
| 72 |
- The security policy to allow traffic on the link for the MACsec Policy. |
| 73 |
- The default value is C(should_secure). |
| 74 |
type: str |
| 75 |
choices: [ should_secure, must_secure ] |
| 76 |
sak_expiry_time: |
| 77 |
description: |
| 78 |
- The expiry time for the Security Association Key (SAK) for the MACsec Policy. |
| 79 |
- The value must be 0 or between 60 and 2592000. |
| 80 |
- The default value is 0. |
| 81 |
type: int |
| 82 |
confidentiality_offset: |
| 83 |
description: |
| 84 |
- The confidentiality offset for the MACsec Policy. |
| 85 |
- The default value is 0. |
| 86 |
- This parameter is only available for type C(access). |
| 87 |
type: int |
| 88 |
choices: [ 0, 30, 50 ] |
| 89 |
key_server_priority: |
| 90 |
description: |
| 91 |
- The key server priority for the MACsec Policy. |
| 92 |
- The value must be between 0 and 255. |
| 93 |
- The default value 16 for type C(access). |
| 94 |
- This parameter is only available for type C(access). |
| 95 |
type: int |
| 96 |
macsec_keys: |
| 97 |
description: |
| 98 |
- List of the MACsec Keys. |
| 99 |
- Providing an empty list will remove the O(macsec_keys) from the MACsec Policy. |
| 100 |
- The old O(macsec_keys) entries will be replaced with the new entries during update. |
| 101 |
type: list |
| 102 |
elements: dict |
| 103 |
suboptions: |
| 104 |
key_name: |
| 105 |
description: |
| 106 |
- The name of the MACsec Key. |
| 107 |
- Key Name has to be Hex chars [0-9a-fA-F] |
| 108 |
type: str |
| 109 |
required: true |
| 110 |
psk: |
| 111 |
description: |
| 112 |
- The Pre-Shared Key (PSK) for the MACsec Key. |
| 113 |
- PSK has to be 64 chars long if cipher suite is C(256_gcm_aes) or C(256_gcm_aes_xpn). |
| 114 |
- PSK has to be 32 chars long if cipher suite is C(128_gcm_aes) or C(128_gcm_aes_xpn). |
| 115 |
- PSK has to be Hex chars [0-9a-fA-F] |
| 116 |
type: str |
| 117 |
required: true |
| 118 |
start_time: |
| 119 |
description: |
| 120 |
- The start time for the MACsec Key. |
| 121 |
- The date time format - YYYY-MM-DD HH:MM:SS or 'now' |
| 122 |
- The start time for each key_name should be unique. |
| 123 |
- The default value is C(now). |
| 124 |
type: str |
| 125 |
end_time: |
| 126 |
description: |
| 127 |
- The end time for the MACsec Key. |
| 128 |
- The date time format - YYYY-MM-DD HH:MM:SS or 'infinite' |
| 129 |
- The default value is C(infinite). |
| 130 |
type: str |
| 131 |
state: |
| 132 |
description: |
| 133 |
- Use C(absent) for removing. |
| 134 |
- Use C(query) for listing an object or multiple objects. |
| 135 |
- Use C(present) for creating or updating. |
| 136 |
type: str |
| 137 |
choices: [ absent, query, present ] |
| 138 |
default: query |
| 139 |
extends_documentation_fragment: cisco.mso.modules |
| 140 |
""" |
| 141 |
|
| 142 |
EXAMPLES = r""" |
| 143 |
- name: Create a new MACsec Policy of interface_type fabric |
| 144 |
cisco.mso.ndo_macsec_policy: |
| 145 |
host: mso_host |
| 146 |
username: admin |
| 147 |
password: SomeSecretPassword |
| 148 |
template: ansible_test_template |
| 149 |
macsec_policy: ansible_test_macsec_policy |
| 150 |
description: "Ansible Test MACsec Policy" |
| 151 |
state: present |
| 152 |
|
| 153 |
- name: Create a new MACsec Policy of interface_type access |
| 154 |
cisco.mso.ndo_macsec_policy: |
| 155 |
host: mso_host |
| 156 |
username: admin |
| 157 |
password: SomeSecretPassword |
| 158 |
template: ansible_test_template |
| 159 |
macsec_policy: ansible_test_macsec_policy |
| 160 |
description: "Ansible Test MACsec Policy" |
| 161 |
macsec_keys: |
| 162 |
- key_name: ansible_test_key |
| 163 |
psk: 'AA111111111111111111111111111111111111111111111111111111111111aa' |
| 164 |
start_time: '2029-12-11 11:12:13' |
| 165 |
end_time: 'infinite' |
| 166 |
state: present |
| 167 |
|
| 168 |
- name: Query a MACsec Policy with macsec_policy name |
| 169 |
cisco.mso.ndo_macsec_policy: |
| 170 |
host: mso_host |
| 171 |
username: admin |
| 172 |
password: SomeSecretPassword |
| 173 |
template: ansible_test_template |
| 174 |
macsec_policy: ansible_test_macsec_policy |
| 175 |
state: query |
| 176 |
register: query_one |
| 177 |
|
| 178 |
- name: Query all MACsec Policies |
| 179 |
cisco.mso.ndo_macsec_policy: |
| 180 |
host: mso_host |
| 181 |
username: admin |
| 182 |
password: SomeSecretPassword |
| 183 |
template: ansible_test_template |
| 184 |
state: query |
| 185 |
register: query_all |
| 186 |
|
| 187 |
- name: Query a MACsec Policy with macsec_policy UUID |
| 188 |
cisco.mso.ndo_macsec_policy: |
| 189 |
host: mso_host |
| 190 |
username: admin |
| 191 |
password: SomeSecretPassword |
| 192 |
template: ansible_test_template |
| 193 |
macsec_policy_uuid: ansible_test_macsec_policy_uuid |
| 194 |
state: query |
| 195 |
register: query_uuid |
| 196 |
|
| 197 |
- name: Delete a MACsec Policy with name |
| 198 |
cisco.mso.ndo_macsec_policy: |
| 199 |
host: mso_host |
| 200 |
username: admin |
| 201 |
password: SomeSecretPassword |
| 202 |
template: ansible_test_template |
| 203 |
macsec_policy: ansible_test_macsec_policy |
| 204 |
state: absent |
| 205 |
|
| 206 |
- name: Delete a MACsec Policy with UUID |
| 207 |
cisco.mso.ndo_macsec_policy: |
| 208 |
host: mso_host |
| 209 |
username: admin |
| 210 |
password: SomeSecretPassword |
| 211 |
template: ansible_test_template |
| 212 |
macsec_policy_uuid: ansible_test_macsec_policy_uuid |
| 213 |
state: absent |
| 214 |
""" |
| 215 |
|
| 216 |
RETURN = r""" |
| 217 |
""" |
| 218 |
|
| 219 |
|
| 220 |
from ansible.module_utils.basic import AnsibleModule |
| 221 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 222 |
from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate, KVPair |
| 223 |
from ansible_collections.cisco.mso.plugins.module_utils.constants import NDO_CIPHER_SUITE_MAP, NDO_SECURITY_POLICY_MAP |
| 224 |
import copy |
| 225 |
|
| 226 |
|
| 227 |
def main(): |
| 228 |
argument_spec = mso_argument_spec() |
| 229 |
argument_spec.update( |
| 230 |
dict( |
| 231 |
template=dict(type="str", required=True), |
| 232 |
macsec_policy=dict(type="str", aliases=["name"]), |
| 233 |
macsec_policy_uuid=dict(type="str", aliases=["uuid"]), |
| 234 |
description=dict(type="str"), |
| 235 |
admin_state=dict(type="str", choices=["enabled", "disabled"]), |
| 236 |
interface_type=dict(type="str", choices=["fabric", "access"], default="fabric"), |
| 237 |
cipher_suite=dict(type="str", choices=list(NDO_CIPHER_SUITE_MAP)), |
| 238 |
window_size=dict(type="int"), |
| 239 |
security_policy=dict(type="str", choices=list(NDO_SECURITY_POLICY_MAP)), |
| 240 |
sak_expiry_time=dict(type="int"), |
| 241 |
confidentiality_offset=dict(type="int", choices=[0, 30, 50]), |
| 242 |
key_server_priority=dict(type="int"), |
| 243 |
macsec_keys=dict( |
| 244 |
type="list", |
| 245 |
elements="dict", |
| 246 |
options=dict( |
| 247 |
key_name=dict(type="str", required=True), |
| 248 |
psk=dict(type="str", required=True, no_log=True), |
| 249 |
start_time=dict(type="str"), |
| 250 |
end_time=dict(type="str"), |
| 251 |
), |
| 252 |
no_log=False, |
| 253 |
), |
| 254 |
state=dict(type="str", choices=["absent", "query", "present"], default="query"), |
| 255 |
) |
| 256 |
) |
| 257 |
|
| 258 |
module = AnsibleModule( |
| 259 |
argument_spec=argument_spec, |
| 260 |
supports_check_mode=True, |
| 261 |
required_if=[ |
| 262 |
["state", "present", ["macsec_policy", "macsec_policy_uuid"], True], |
| 263 |
["state", "absent", ["macsec_policy", "macsec_policy_uuid"], True], |
| 264 |
], |
| 265 |
) |
| 266 |
|
| 267 |
mso = MSOModule(module) |
| 268 |
|
| 269 |
template = module.params.get("template") |
| 270 |
macsec_policy = module.params.get("macsec_policy") |
| 271 |
macsec_policy_uuid = module.params.get("macsec_policy_uuid") |
| 272 |
description = module.params.get("description") |
| 273 |
admin_state = module.params.get("admin_state") |
| 274 |
interface_type = module.params.get("interface_type") |
| 275 |
cipher_suite = NDO_CIPHER_SUITE_MAP.get(module.params.get("cipher_suite")) |
| 276 |
window_size = module.params.get("window_size") |
| 277 |
security_policy = NDO_SECURITY_POLICY_MAP.get(module.params.get("security_policy")) |
| 278 |
sak_expiry_time = module.params.get("sak_expiry_time") |
| 279 |
confidentiality_offset = module.params.get("confidentiality_offset") |
| 280 |
key_server_priority = module.params.get("key_server_priority") |
| 281 |
macsec_keys = module.params.get("macsec_keys") |
| 282 |
state = module.params.get("state") |
| 283 |
|
| 284 |
ops = [] |
| 285 |
match = None |
| 286 |
|
| 287 |
mso_template = MSOTemplate(mso, "fabric_policy", template) |
| 288 |
mso_template.validate_template("fabricPolicy") |
| 289 |
|
| 290 |
path = "/fabricPolicyTemplate/template/macsecPolicies" |
| 291 |
object_description = "MACsec Policy" |
| 292 |
|
| 293 |
existing_macsec_policies = mso_template.template.get("fabricPolicyTemplate", {}).get("template", {}).get("macsecPolicies", []) |
| 294 |
if macsec_policy or macsec_policy_uuid: |
| 295 |
match = mso_template.get_object_by_key_value_pairs( |
| 296 |
object_description, |
| 297 |
existing_macsec_policies, |
| 298 |
[KVPair("uuid", macsec_policy_uuid) if macsec_policy_uuid else KVPair("name", macsec_policy)], |
| 299 |
) |
| 300 |
if match: |
| 301 |
mso.existing = mso.previous = copy.deepcopy(match.details) |
| 302 |
else: |
| 303 |
mso.existing = mso.previous = existing_macsec_policies |
| 304 |
|
| 305 |
if state == "present": |
| 306 |
|
| 307 |
if match: |
| 308 |
|
| 309 |
if macsec_policy and match.details.get("name") != macsec_policy: |
| 310 |
ops.append(dict(op="replace", path="{0}/{1}/name".format(path, match.index), value=macsec_policy)) |
| 311 |
match.details["name"] = macsec_policy |
| 312 |
|
| 313 |
if description is not None and match.details.get("description") != description: |
| 314 |
ops.append(dict(op="replace", path="{0}/{1}/description".format(path, match.index), value=description)) |
| 315 |
match.details["description"] = description |
| 316 |
|
| 317 |
if admin_state and match.details.get("adminState") != admin_state: |
| 318 |
ops.append(dict(op="replace", path="{0}/{1}/adminState".format(path, match.index), value=admin_state)) |
| 319 |
match.details["adminState"] = admin_state |
| 320 |
|
| 321 |
if interface_type and match.details.get("type") != interface_type: |
| 322 |
mso.fail_json(msg="Type cannot be changed for an existing MACsec Policy.") |
| 323 |
|
| 324 |
if cipher_suite and match.details.get("macsecParams")["cipherSuite"] != cipher_suite: |
| 325 |
ops.append(dict(op="replace", path="{0}/{1}/macsecParams/cipherSuite".format(path, match.index), value=cipher_suite)) |
| 326 |
match.details["macsecParams"]["cipherSuite"] = cipher_suite |
| 327 |
|
| 328 |
if window_size and match.details.get("macsecParams")["windowSize"] != window_size: |
| 329 |
ops.append(dict(op="replace", path="{0}/{1}/macsecParams/windowSize".format(path, match.index), value=window_size)) |
| 330 |
match.details["macsecParams"]["windowSize"] = window_size |
| 331 |
|
| 332 |
if security_policy and match.details.get("macsecParams")["securityPol"] != security_policy: |
| 333 |
ops.append(dict(op="replace", path="{0}/{1}/macsecParams/securityPol".format(path, match.index), value=security_policy)) |
| 334 |
match.details["macsecParams"]["securityPol"] = security_policy |
| 335 |
|
| 336 |
if sak_expiry_time and match.details.get("macsecParams")["sakExpiryTime"] != sak_expiry_time: |
| 337 |
ops.append(dict(op="replace", path="{0}/{1}/macsecParams/sakExpiryTime".format(path, match.index), value=sak_expiry_time)) |
| 338 |
match.details["macsecParams"]["sakExpiryTime"] = sak_expiry_time |
| 339 |
|
| 340 |
if interface_type == "access": |
| 341 |
if confidentiality_offset and match.details.get("macsecParams")["confOffSet"] != confidentiality_offset: |
| 342 |
ops.append( |
| 343 |
dict(op="replace", path="{0}/{1}/macsecParams/confOffSet".format(path, match.index), value="offset{0}".format(confidentiality_offset)) |
| 344 |
) |
| 345 |
match.details["macsecParams"]["confOffSet"] = "offset{0}".format(confidentiality_offset) |
| 346 |
|
| 347 |
if key_server_priority and match.details.get("macsecParams")["keyServerPrio"] != key_server_priority: |
| 348 |
ops.append(dict(op="replace", path="{0}/{1}/macsecParams/keyServerPrio".format(path, match.index), value=key_server_priority)) |
| 349 |
match.details["macsecParams"]["keyServerPrio"] = key_server_priority |
| 350 |
|
| 351 |
if macsec_keys: |
| 352 |
# updating macsec_keys modifies the existing list with the new list |
| 353 |
macsec_keys_list = [] |
| 354 |
for macsec_key in macsec_keys: |
| 355 |
macsec_keys_list.append( |
| 356 |
dict( |
| 357 |
keyname=macsec_key.get("key_name"), |
| 358 |
psk=macsec_key.get("psk"), |
| 359 |
start=mso.verify_time_format(macsec_key.get("start_time")) if macsec_key.get("start_time") else None, |
| 360 |
end=mso.verify_time_format(macsec_key.get("end_time")) if macsec_key.get("end_time") else None, |
| 361 |
) |
| 362 |
) |
| 363 |
|
| 364 |
if macsec_keys_list != match.details.get("macsecKeys", []): |
| 365 |
ops.append(dict(op="replace", path="{0}/{1}/macsecKeys".format(path, match.index), value=macsec_keys_list)) |
| 366 |
match.details["macsecKeys"] = macsec_keys |
| 367 |
elif macsec_keys == []: |
| 368 |
# remove macsec_keys if the list is empty |
| 369 |
ops.append(dict(op="remove", path="{0}/{1}/macsecKeys".format(path, match.index))) |
| 370 |
match.details.pop("macsecKeys", None) |
| 371 |
|
| 372 |
mso.sanitize(match.details) |
| 373 |
|
| 374 |
else: |
| 375 |
macsec_param_map = {} |
| 376 |
|
| 377 |
payload = {"name": macsec_policy, "templateId": mso_template.template.get("templateId"), "schemaId": mso_template.template.get("schemaId")} |
| 378 |
payload["type"] = interface_type |
| 379 |
|
| 380 |
if description: |
| 381 |
payload["description"] = description |
| 382 |
if admin_state: |
| 383 |
payload["adminState"] = admin_state |
| 384 |
if cipher_suite: |
| 385 |
macsec_param_map["cipherSuite"] = cipher_suite |
| 386 |
if window_size: |
| 387 |
macsec_param_map["windowSize"] = window_size |
| 388 |
if security_policy: |
| 389 |
macsec_param_map["securityPol"] = security_policy |
| 390 |
if sak_expiry_time: |
| 391 |
macsec_param_map["sakExpiryTime"] = sak_expiry_time |
| 392 |
|
| 393 |
if interface_type == "access": |
| 394 |
if confidentiality_offset: |
| 395 |
macsec_param_map["confOffSet"] = "offset{0}".format(confidentiality_offset) |
| 396 |
if key_server_priority: |
| 397 |
macsec_param_map["keyServerPrio"] = key_server_priority |
| 398 |
payload["macsecParams"] = macsec_param_map |
| 399 |
|
| 400 |
if macsec_keys: |
| 401 |
macsec_keys_list = [] |
| 402 |
for macsec_key in macsec_keys: |
| 403 |
macsec_key_dict = { |
| 404 |
"keyname": macsec_key.get("key_name"), |
| 405 |
"psk": macsec_key.get("psk"), |
| 406 |
} |
| 407 |
if macsec_key.get("start_time"): |
| 408 |
macsec_key_dict["start"] = macsec_key.get("start_time") |
| 409 |
if macsec_key.get("end_time"): |
| 410 |
macsec_key_dict["end"] = macsec_key.get("end_time") |
| 411 |
macsec_keys_list.append(macsec_key_dict) |
| 412 |
payload["macsecKeys"] = macsec_keys_list |
| 413 |
|
| 414 |
ops.append(dict(op="add", path="{0}/-".format(path), value=copy.deepcopy(payload))) |
| 415 |
|
| 416 |
mso.sanitize(payload) |
| 417 |
|
| 418 |
mso.existing = mso.proposed |
| 419 |
|
| 420 |
elif state == "absent": |
| 421 |
if match: |
| 422 |
ops.append(dict(op="remove", path="{0}/{1}".format(path, match.index))) |
| 423 |
|
| 424 |
if not module.check_mode and ops: |
| 425 |
response = mso.request(mso_template.template_path, method="PATCH", data=ops) |
| 426 |
macsec_policies = response.get("fabricPolicyTemplate", {}).get("template", {}).get("macsecPolicies", []) |
| 427 |
match = mso_template.get_object_by_key_value_pairs( |
| 428 |
object_description, |
| 429 |
macsec_policies, |
| 430 |
[KVPair("uuid", macsec_policy_uuid) if macsec_policy_uuid else KVPair("name", macsec_policy)], |
| 431 |
) |
| 432 |
if match: |
| 433 |
mso.existing = match.details |
| 434 |
else: |
| 435 |
mso.existing = {} |
| 436 |
elif module.check_mode and state != "query": |
| 437 |
mso.existing = mso.proposed if state == "present" else {} |
| 438 |
|
| 439 |
mso.exit_json() |
| 440 |
|
| 441 |
|
| 442 |
if __name__ == "__main__": |
| 443 |
main() |