| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
ANSIBLE_METADATA = {'metadata_version': '1.1', |
| 10 |
'status': ['preview'], |
| 11 |
'supported_by': 'community'} |
| 12 |
|
| 13 |
DOCUMENTATION = r''' |
| 14 |
--- |
| 15 |
module: intersight_macsec_policy |
| 16 |
short_description: MACsec Policy configuration for Cisco Intersight |
| 17 |
description: |
| 18 |
- Manages MACsec Policy configuration on Cisco Intersight. |
| 19 |
- A policy to configure MACsec encryption settings for fabric interconnect ports. |
| 20 |
- Supports primary keychain and optional fallback keychain configuration. |
| 21 |
- For more information see L(Cisco Intersight,https://intersight.com/apidocs). |
| 22 |
extends_documentation_fragment: intersight |
| 23 |
options: |
| 24 |
state: |
| 25 |
description: |
| 26 |
- If C(present), will verify the resource is present and will create if needed. |
| 27 |
- If C(absent), will verify the resource is absent and will delete if needed. |
| 28 |
type: str |
| 29 |
choices: [present, absent] |
| 30 |
default: present |
| 31 |
organization: |
| 32 |
description: |
| 33 |
- The name of the Organization this resource is assigned to. |
| 34 |
- Profiles, Policies, and Pools that are created within a Custom Organization are applicable only to devices in the same Organization. |
| 35 |
type: str |
| 36 |
default: default |
| 37 |
name: |
| 38 |
description: |
| 39 |
- The name assigned to the MACsec Policy. |
| 40 |
- The name must be between 1 and 62 alphanumeric characters, allowing special characters :-_. |
| 41 |
type: str |
| 42 |
required: true |
| 43 |
description: |
| 44 |
description: |
| 45 |
- The user-defined description for the MACsec Policy. |
| 46 |
- Description can contain letters(a-z, A-Z), numbers(0-9), hyphen(-), period(.), colon(:), or an underscore(_). |
| 47 |
type: str |
| 48 |
aliases: [descr] |
| 49 |
tags: |
| 50 |
description: |
| 51 |
- List of tags in Key:<user-defined key> Value:<user-defined value> format. |
| 52 |
type: list |
| 53 |
elements: dict |
| 54 |
cipher_suite: |
| 55 |
description: |
| 56 |
- Cipher suite to be used for MACsec encryption. |
| 57 |
type: str |
| 58 |
choices: ['gcm-aes-xpn-256', 'gcm-aes-128', 'gcm-aes-256', 'gcm-aes-xpn-128'] |
| 59 |
default: 'gcm-aes-xpn-256' |
| 60 |
confidentiality_offset: |
| 61 |
description: |
| 62 |
- The MACsec confidentiality offset specifies the number of bytes starting from the frame header. |
| 63 |
- MACsec encrypts only the bytes after the offset in a frame. |
| 64 |
type: str |
| 65 |
choices: ['conf-offset-0', 'conf-offset-30', 'conf-offset-50'] |
| 66 |
default: 'conf-offset-0' |
| 67 |
security_policy: |
| 68 |
description: |
| 69 |
- The security policy specifies the level of MACsec enforcement on network traffic. |
| 70 |
- C(should-secure) allows unencrypted traffic until MKA session is secured. |
| 71 |
- C(must-secure) only allows MACsec encrypted traffic. |
| 72 |
type: str |
| 73 |
choices: ['should-secure', 'must-secure'] |
| 74 |
default: 'should-secure' |
| 75 |
key_server_priority: |
| 76 |
description: |
| 77 |
- Key server is selected by comparing priority values during MKA message exchange. |
| 78 |
- Valid values range from 0 to 255. |
| 79 |
- Lower value means higher chance of being selected as key server. |
| 80 |
type: int |
| 81 |
default: 16 |
| 82 |
sak_expiry_time: |
| 83 |
description: |
| 84 |
- Time in seconds to force secure association key (SAK) rekey. |
| 85 |
- Valid range is from 60 to 2592000 seconds. |
| 86 |
- When set to 0 or not configured, SAK rekey interval is determined based on interface speed. |
| 87 |
type: int |
| 88 |
replay_window_size: |
| 89 |
description: |
| 90 |
- Defines the size of the replay protection window. |
| 91 |
- Determines the number of packets that can be received out of order without being considered replay attacks. |
| 92 |
- Valid range is from 0 to 596000000. |
| 93 |
type: int |
| 94 |
default: 148809600 |
| 95 |
include_icv_indicator: |
| 96 |
description: |
| 97 |
- Configures inclusion of the optional integrity check value (ICV) indicator. |
| 98 |
- Part of the transmitted MACsec key agreement protocol data unit (PDU). |
| 99 |
type: bool |
| 100 |
default: false |
| 101 |
eapol_mac_address: |
| 102 |
description: |
| 103 |
- MAC address to use in extensible authentication protocol over LAN (EAPoL) for MKA PDUs. |
| 104 |
- EAPol MAC address should not be equal to all-zero (0000.0000.0000). |
| 105 |
type: str |
| 106 |
default: '0180.C200.0003' |
| 107 |
eapol_ethertype: |
| 108 |
description: |
| 109 |
- Ethertype to use in EAPoL frames for MKA PDUs. |
| 110 |
- The range is between 0x600 - 0xffff. |
| 111 |
type: str |
| 112 |
default: '0x888e' |
| 113 |
primary_keychain_name: |
| 114 |
description: |
| 115 |
- Primary keychain name for managing the default set of security keys. |
| 116 |
type: str |
| 117 |
primary_keys: |
| 118 |
description: |
| 119 |
- List of security keys for the primary keychain. |
| 120 |
type: list |
| 121 |
elements: dict |
| 122 |
default: [] |
| 123 |
suboptions: |
| 124 |
id: |
| 125 |
description: |
| 126 |
- Key ID must have an even number of hexadecimal characters (0-9, A-F). |
| 127 |
- Length must be between 2 and 64 characters. |
| 128 |
type: str |
| 129 |
required: true |
| 130 |
cryptographic_algorithm: |
| 131 |
description: |
| 132 |
- Cryptographic algorithm for the key. |
| 133 |
type: str |
| 134 |
choices: ['aes-256-cmac', 'aes-128-cmac'] |
| 135 |
default: 'aes-256-cmac' |
| 136 |
secret: |
| 137 |
description: |
| 138 |
- Key secret is a shared secret used in cryptographic operations. |
| 139 |
- Must start with the character 'J'. |
| 140 |
type: str |
| 141 |
required: true |
| 142 |
key_lifetime_always_active: |
| 143 |
description: |
| 144 |
- Indicates that the key remains active indefinitely. |
| 145 |
- When C(true), the key is always active. |
| 146 |
- When C(false), C(start_time) and C(lifetime_type) must be specified. |
| 147 |
type: bool |
| 148 |
default: true |
| 149 |
timezone: |
| 150 |
description: |
| 151 |
- The time zone used for key lifetime configurations. |
| 152 |
- Only used when C(key_lifetime_always_active) is C(false). |
| 153 |
type: str |
| 154 |
choices: ['utc', 'local'] |
| 155 |
default: 'utc' |
| 156 |
start_time: |
| 157 |
description: |
| 158 |
- The time of day and date when the key becomes active. |
| 159 |
- Format should be ISO 8601 format (e.g., 2025-11-20T09:14:00.000Z or 2025-11-20T09:14:00.000). |
| 160 |
- The 'Z' suffix will be automatically added if not provided. |
| 161 |
- Required when C(key_lifetime_always_active) is C(false). |
| 162 |
type: str |
| 163 |
lifetime_type: |
| 164 |
description: |
| 165 |
- Indicates key lifetime behavior after start time. |
| 166 |
- C(never) means the key remains active indefinitely after start time. |
| 167 |
- C(on-this-day) means the key becomes inactive at C(end_time). |
| 168 |
type: str |
| 169 |
choices: ['never', 'on-this-day'] |
| 170 |
default: 'never' |
| 171 |
end_time: |
| 172 |
description: |
| 173 |
- The time of day and date when the key becomes inactive. |
| 174 |
- Format should be ISO 8601 format (e.g., 2025-11-21T09:20:00.000Z or 2025-11-21T09:20:00.000). |
| 175 |
- The 'Z' suffix will be automatically added if not provided. |
| 176 |
- Required when C(lifetime_type) is C(on-this-day). |
| 177 |
type: str |
| 178 |
configure_fallback_keychain: |
| 179 |
description: |
| 180 |
- Enable configuration of fallback keychain. |
| 181 |
type: bool |
| 182 |
default: false |
| 183 |
fallback_keychain_name: |
| 184 |
description: |
| 185 |
- Fallback keychain name. |
| 186 |
- Required when C(configure_fallback_keychain) is C(true). |
| 187 |
type: str |
| 188 |
fallback_keys: |
| 189 |
description: |
| 190 |
- List of security keys for the fallback keychain. |
| 191 |
type: list |
| 192 |
elements: dict |
| 193 |
default: [] |
| 194 |
suboptions: |
| 195 |
id: |
| 196 |
description: |
| 197 |
- Key ID must have an even number of hexadecimal characters (0-9, A-F). |
| 198 |
- Length must be between 2 and 64 characters. |
| 199 |
type: str |
| 200 |
required: true |
| 201 |
cryptographic_algorithm: |
| 202 |
description: |
| 203 |
- Cryptographic algorithm for the key. |
| 204 |
type: str |
| 205 |
choices: ['aes-256-cmac', 'aes-128-cmac'] |
| 206 |
default: 'aes-256-cmac' |
| 207 |
secret: |
| 208 |
description: |
| 209 |
- Key secret is a shared secret used in cryptographic operations. |
| 210 |
- Must start with the character 'J'. |
| 211 |
type: str |
| 212 |
required: true |
| 213 |
key_lifetime_always_active: |
| 214 |
description: |
| 215 |
- Indicates that the key remains active indefinitely. |
| 216 |
- When C(true), the key is always active. |
| 217 |
- When C(false), C(start_time) and C(lifetime_type) must be specified. |
| 218 |
type: bool |
| 219 |
default: true |
| 220 |
timezone: |
| 221 |
description: |
| 222 |
- The time zone used for key lifetime configurations. |
| 223 |
- Only used when C(key_lifetime_always_active) is C(false). |
| 224 |
type: str |
| 225 |
choices: ['utc', 'local'] |
| 226 |
default: 'utc' |
| 227 |
start_time: |
| 228 |
description: |
| 229 |
- The time of day and date when the key becomes active. |
| 230 |
- Format should be ISO 8601 format (e.g., 2025-11-20T09:14:00.000Z or 2025-11-20T09:14:00.000). |
| 231 |
- The 'Z' suffix will be automatically added if not provided. |
| 232 |
- Required when C(key_lifetime_always_active) is C(false). |
| 233 |
type: str |
| 234 |
lifetime_type: |
| 235 |
description: |
| 236 |
- Indicates key lifetime behavior after start time. |
| 237 |
- C(never) means the key remains active indefinitely after start time. |
| 238 |
- C(on-this-day) means the key becomes inactive at C(end_time). |
| 239 |
type: str |
| 240 |
choices: ['never', 'on-this-day'] |
| 241 |
default: 'never' |
| 242 |
end_time: |
| 243 |
description: |
| 244 |
- The time of day and date when the key becomes inactive. |
| 245 |
- Format should be ISO 8601 format (e.g., 2025-11-21T09:20:00.000Z or 2025-11-21T09:20:00.000). |
| 246 |
- The 'Z' suffix will be automatically added if not provided. |
| 247 |
- Required when C(lifetime_type) is C(on-this-day). |
| 248 |
type: str |
| 249 |
author: |
| 250 |
- Ron Gershburg (@rgershbu) |
| 251 |
''' |
| 252 |
|
| 253 |
EXAMPLES = r''' |
| 254 |
- name: Create MACsec Policy with Primary Keychain Only |
| 255 |
cisco.intersight.intersight_macsec_policy: |
| 256 |
api_private_key: "{{ api_private_key }}" |
| 257 |
api_key_id: "{{ api_key_id }}" |
| 258 |
organization: "default" |
| 259 |
name: "macsec-policy-01" |
| 260 |
description: "MACsec policy with primary keychain" |
| 261 |
cipher_suite: "gcm-aes-xpn-256" |
| 262 |
security_policy: "should-secure" |
| 263 |
primary_keychain_name: "primary-keychain" |
| 264 |
primary_keys: |
| 265 |
- id: "1234" |
| 266 |
cryptographic_algorithm: "aes-256-cmac" |
| 267 |
secret: >- |
| 268 |
Ja1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9bd2e3f4a5b6c7d8e9f0a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c122222222222222222222222222222222222222222222 |
| 269 |
key_lifetime_always_active: true |
| 270 |
tags: |
| 271 |
- Key: Environment |
| 272 |
Value: Production |
| 273 |
state: present |
| 274 |
|
| 275 |
- name: Create MACsec Policy with Fallback Keychain |
| 276 |
cisco.intersight.intersight_macsec_policy: |
| 277 |
api_private_key: "{{ api_private_key }}" |
| 278 |
api_key_id: "{{ api_key_id }}" |
| 279 |
organization: "default" |
| 280 |
name: "macsec-policy-02" |
| 281 |
description: "MACsec policy with fallback keychain" |
| 282 |
cipher_suite: "gcm-aes-256" |
| 283 |
security_policy: "must-secure" |
| 284 |
key_server_priority: 32 |
| 285 |
sak_expiry_time: 3600 |
| 286 |
primary_keychain_name: "primary-keychain" |
| 287 |
primary_keys: |
| 288 |
- id: "ABCD" |
| 289 |
cryptographic_algorithm: "aes-256-cmac" |
| 290 |
secret: >- |
| 291 |
Ja1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9bd2e3f4a5b6c7d8e9f0a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c122222222222222222222222222222222222222222222 |
| 292 |
configure_fallback_keychain: true |
| 293 |
fallback_keychain_name: "fallback-keychain" |
| 294 |
fallback_keys: |
| 295 |
- id: "EF12" |
| 296 |
cryptographic_algorithm: "aes-128-cmac" |
| 297 |
secret: "Jf1e2d3c4b5a6f7e8d9c0b1a2f3e4d5c6b7a8f9e0d1c2b3a4f5e6d7c8b9a0f1e2d3c4b5a6f7e8d9c0b1a2f3e4d5c6b7a8f9e0d1c2b3a4f5e6d7c8b9a0f1e2d3c4b5a6f7e8d9c0" |
| 298 |
state: present |
| 299 |
|
| 300 |
- name: Create MACsec Policy with Custom Settings |
| 301 |
cisco.intersight.intersight_macsec_policy: |
| 302 |
api_private_key: "{{ api_private_key }}" |
| 303 |
api_key_id: "{{ api_key_id }}" |
| 304 |
organization: "default" |
| 305 |
name: "macsec-policy-custom" |
| 306 |
description: "MACsec policy with custom settings" |
| 307 |
cipher_suite: "gcm-aes-xpn-128" |
| 308 |
confidentiality_offset: "conf-offset-30" |
| 309 |
security_policy: "must-secure" |
| 310 |
key_server_priority: 64 |
| 311 |
sak_expiry_time: 7200 |
| 312 |
replay_window_size: 200000000 |
| 313 |
include_icv_indicator: true |
| 314 |
eapol_mac_address: "0180.C200.0004" |
| 315 |
eapol_ethertype: "0x88e5" |
| 316 |
primary_keychain_name: "custom-keychain" |
| 317 |
primary_keys: |
| 318 |
- id: "2468" |
| 319 |
cryptographic_algorithm: "aes-256-cmac" |
| 320 |
secret: "Jabcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz" |
| 321 |
state: present |
| 322 |
|
| 323 |
- name: Create MACsec Policy with Scheduled Key Lifetime (Never Expires) |
| 324 |
cisco.intersight.intersight_macsec_policy: |
| 325 |
api_private_key: "{{ api_private_key }}" |
| 326 |
api_key_id: "{{ api_key_id }}" |
| 327 |
organization: "default" |
| 328 |
name: "macsec-policy-scheduled-never" |
| 329 |
description: "MACsec policy with scheduled key that never expires" |
| 330 |
primary_keychain_name: "scheduled-keychain" |
| 331 |
primary_keys: |
| 332 |
- id: "3456" |
| 333 |
cryptographic_algorithm: "aes-256-cmac" |
| 334 |
secret: "Jabcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz" |
| 335 |
key_lifetime_always_active: false |
| 336 |
timezone: "local" |
| 337 |
start_time: "2025-11-20T10:00:00.000" |
| 338 |
lifetime_type: "never" |
| 339 |
state: present |
| 340 |
|
| 341 |
- name: Create MACsec Policy with Scheduled Key Lifetime (With End Time) |
| 342 |
cisco.intersight.intersight_macsec_policy: |
| 343 |
api_private_key: "{{ api_private_key }}" |
| 344 |
api_key_id: "{{ api_key_id }}" |
| 345 |
organization: "default" |
| 346 |
name: "macsec-policy-scheduled-endtime" |
| 347 |
description: "MACsec policy with scheduled key with end time" |
| 348 |
primary_keychain_name: "scheduled-keychain-endtime" |
| 349 |
primary_keys: |
| 350 |
- id: "4567" |
| 351 |
cryptographic_algorithm: "aes-256-cmac" |
| 352 |
secret: "Jabcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz" |
| 353 |
key_lifetime_always_active: false |
| 354 |
timezone: "utc" |
| 355 |
start_time: "2025-11-20T09:20:00.000Z" |
| 356 |
lifetime_type: "on-this-day" |
| 357 |
end_time: "2025-11-21T09:20:00.000Z" |
| 358 |
state: present |
| 359 |
|
| 360 |
- name: Delete MACsec Policy |
| 361 |
cisco.intersight.intersight_macsec_policy: |
| 362 |
api_private_key: "{{ api_private_key }}" |
| 363 |
api_key_id: "{{ api_key_id }}" |
| 364 |
organization: "default" |
| 365 |
name: "macsec-policy-01" |
| 366 |
state: absent |
| 367 |
''' |
| 368 |
|
| 369 |
RETURN = r''' |
| 370 |
api_response: |
| 371 |
description: The API response output returned by the specified resource. |
| 372 |
returned: always |
| 373 |
type: dict |
| 374 |
sample: |
| 375 |
"api_response": { |
| 376 |
"Name": "macsec-policy-01", |
| 377 |
"ObjectType": "fabric.MacsecPolicy", |
| 378 |
"Description": "MACsec policy with primary keychain", |
| 379 |
"CipherSuite": "GCM-AES-XPN-256", |
| 380 |
"ConfidentialityOffset": "CONF-OFFSET-0", |
| 381 |
"SecurityPolicy": "Should-secure", |
| 382 |
"KeyServerPriority": 16, |
| 383 |
"SakExpiryTime": 0, |
| 384 |
"ReplayWindowSize": 148809600, |
| 385 |
"IncludeIcvIndicator": false, |
| 386 |
"MacSecEaPol": { |
| 387 |
"EaPolMacAddress": "0180.C200.0003", |
| 388 |
"EaPolEthertype": "0x888e" |
| 389 |
}, |
| 390 |
"PrimaryKeyChain": { |
| 391 |
"Name": "primary-keychain", |
| 392 |
"SecKeys": [ |
| 393 |
{ |
| 394 |
"Id": "1234", |
| 395 |
"CryptographicAlgorithm": "AES_256_CMAC", |
| 396 |
"KeyType": "Type-6", |
| 397 |
"SendLifetimeUnlimited": true, |
| 398 |
"SendLifetimeInfinite": false, |
| 399 |
"IsOctetStringSet": true |
| 400 |
} |
| 401 |
] |
| 402 |
}, |
| 403 |
"FallbackKeyChain": { |
| 404 |
"Name": "", |
| 405 |
"SecKeys": null |
| 406 |
}, |
| 407 |
"Tags": [ |
| 408 |
{ |
| 409 |
"Key": "Environment", |
| 410 |
"Value": "Production" |
| 411 |
} |
| 412 |
] |
| 413 |
} |
| 414 |
''' |
| 415 |
|
| 416 |
from ansible.module_utils.basic import AnsibleModule |
| 417 |
from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec |
| 418 |
|
| 419 |
|
| 420 |
def validate_key_id(key_id, field_name): |
| 421 |
""" |
| 422 |
Validate key ID format. |
| 423 |
""" |
| 424 |
if not key_id: |
| 425 |
return False, f"{field_name} is required" |
| 426 |
if len(key_id) < 2 or len(key_id) > 64: |
| 427 |
return False, f"{field_name} must be between 2 and 64 characters" |
| 428 |
if len(key_id) % 2 != 0: |
| 429 |
return False, f"{field_name} must have an even number of characters" |
| 430 |
if not all(c in '0123456789ABCDEFabcdef' for c in key_id): |
| 431 |
return False, f"{field_name} must contain only hexadecimal characters (0-9, A-F)" |
| 432 |
return True, None |
| 433 |
|
| 434 |
|
| 435 |
def validate_secret(secret, field_name): |
| 436 |
""" |
| 437 |
Validate secret format. |
| 438 |
""" |
| 439 |
if not secret: |
| 440 |
return False, f"{field_name} is required" |
| 441 |
if not secret.startswith('J'): |
| 442 |
return False, f"{field_name} must start with the character 'J'" |
| 443 |
return True, None |
| 444 |
|
| 445 |
|
| 446 |
def validate_macsec_configuration(module): |
| 447 |
""" |
| 448 |
Validate MACsec Policy configuration parameters. |
| 449 |
""" |
| 450 |
params = module.params |
| 451 |
if params['state'] == 'present': |
| 452 |
if not params.get('primary_keychain_name'): |
| 453 |
module.fail_json(msg="primary_keychain_name is required when state is present") |
| 454 |
|
| 455 |
if params.get('key_server_priority') is not None: |
| 456 |
if params['key_server_priority'] < 0 or params['key_server_priority'] > 255: |
| 457 |
module.fail_json(msg="key_server_priority must be between 0 and 255") |
| 458 |
|
| 459 |
if params.get('sak_expiry_time') is not None and params['sak_expiry_time'] != 0: |
| 460 |
if params['sak_expiry_time'] < 60 or params['sak_expiry_time'] > 2592000: |
| 461 |
module.fail_json(msg="sak_expiry_time must be between 60 and 2592000 seconds, or 0 for auto-determination") |
| 462 |
|
| 463 |
if params.get('replay_window_size') is not None: |
| 464 |
if params['replay_window_size'] < 0 or params['replay_window_size'] > 596000000: |
| 465 |
module.fail_json(msg="replay_window_size must be between 0 and 596000000") |
| 466 |
|
| 467 |
if params.get('eapol_mac_address'): |
| 468 |
if params['eapol_mac_address'].replace('.', '').lower() == '000000000000': |
| 469 |
module.fail_json(msg="eapol_mac_address cannot be all zeros (0000.0000.0000)") |
| 470 |
|
| 471 |
for key in params.get('primary_keys', []): |
| 472 |
is_valid, error_msg = validate_key_id(key.get('id'), 'primary_keys.id') |
| 473 |
if not is_valid: |
| 474 |
module.fail_json(msg=error_msg) |
| 475 |
is_valid, error_msg = validate_secret(key.get('secret'), 'primary_keys.secret') |
| 476 |
if not is_valid: |
| 477 |
module.fail_json(msg=error_msg) |
| 478 |
if not key.get('key_lifetime_always_active', True): |
| 479 |
if not key.get('start_time'): |
| 480 |
module.fail_json(msg="primary_keys.start_time is required when key_lifetime_always_active is false") |
| 481 |
if key.get('lifetime_type') == 'on-this-day' and not key.get('end_time'): |
| 482 |
module.fail_json(msg="primary_keys.end_time is required when lifetime_type is 'on-this-day'") |
| 483 |
|
| 484 |
if params.get('configure_fallback_keychain'): |
| 485 |
if not params.get('fallback_keychain_name'): |
| 486 |
module.fail_json(msg="fallback_keychain_name is required when configure_fallback_keychain is true") |
| 487 |
for key in params.get('fallback_keys', []): |
| 488 |
is_valid, error_msg = validate_key_id(key.get('id'), 'fallback_keys.id') |
| 489 |
if not is_valid: |
| 490 |
module.fail_json(msg=error_msg) |
| 491 |
is_valid, error_msg = validate_secret(key.get('secret'), 'fallback_keys.secret') |
| 492 |
if not is_valid: |
| 493 |
module.fail_json(msg=error_msg) |
| 494 |
if not key.get('key_lifetime_always_active', True): |
| 495 |
if not key.get('start_time'): |
| 496 |
module.fail_json(msg="fallback_keys.start_time is required when key_lifetime_always_active is false") |
| 497 |
if key.get('lifetime_type') == 'on-this-day' and not key.get('end_time'): |
| 498 |
module.fail_json(msg="fallback_keys.end_time is required when lifetime_type is 'on-this-day'") |
| 499 |
|
| 500 |
|
| 501 |
def build_keychain_config(keychain_name, keys): |
| 502 |
""" |
| 503 |
Build keychain configuration for API body. |
| 504 |
""" |
| 505 |
keychain_config = { |
| 506 |
'Name': keychain_name |
| 507 |
} |
| 508 |
if keys: |
| 509 |
sec_keys = [] |
| 510 |
for key in keys: |
| 511 |
sec_key = { |
| 512 |
'Id': key['id'], |
| 513 |
'CryptographicAlgorithm': convert_to_api_format(key.get('cryptographic_algorithm', 'aes-256-cmac'), 'cryptographic_algorithm'), |
| 514 |
'KeyType': 'Type-6', |
| 515 |
'OctetString': key['secret'], |
| 516 |
'SendLifetimeUnlimited': key.get('key_lifetime_always_active', True), |
| 517 |
'IsOctetStringSet': True |
| 518 |
} |
| 519 |
if not key.get('key_lifetime_always_active', True): |
| 520 |
timezone = key.get('timezone', 'utc') |
| 521 |
sec_key['SendLifetimeTimeZone'] = 'UTC' if timezone == 'utc' else 'Local' |
| 522 |
start_time = key['start_time'] |
| 523 |
if not start_time.endswith('Z'): |
| 524 |
start_time = start_time + 'Z' |
| 525 |
sec_key['SendLifetimeStartTime'] = start_time |
| 526 |
if key.get('lifetime_type', 'never') == 'never': |
| 527 |
sec_key['SendLifetimeInfinite'] = True |
| 528 |
else: |
| 529 |
sec_key['SendLifetimeInfinite'] = False |
| 530 |
end_time = key['end_time'] |
| 531 |
if not end_time.endswith('Z'): |
| 532 |
end_time = end_time + 'Z' |
| 533 |
sec_key['SendLifetimeEndTime'] = end_time |
| 534 |
else: |
| 535 |
sec_key['SendLifetimeInfinite'] = False |
| 536 |
sec_keys.append(sec_key) |
| 537 |
keychain_config['SecKeys'] = sec_keys |
| 538 |
return keychain_config |
| 539 |
|
| 540 |
|
| 541 |
def convert_to_api_format(value, value_type): |
| 542 |
""" |
| 543 |
Convert lowercase user-facing values to API format. |
| 544 |
""" |
| 545 |
if value_type == 'cipher_suite': |
| 546 |
return value.upper() |
| 547 |
elif value_type == 'confidentiality_offset': |
| 548 |
return value.upper() |
| 549 |
elif value_type == 'security_policy': |
| 550 |
parts = value.split('-') |
| 551 |
return f"{parts[0].capitalize()}-{parts[1]}" |
| 552 |
elif value_type == 'cryptographic_algorithm': |
| 553 |
return value.upper().replace('-', '_') |
| 554 |
return value |
| 555 |
|
| 556 |
|
| 557 |
def build_api_body(intersight): |
| 558 |
""" |
| 559 |
Build the API body for MACsec Policy configuration. |
| 560 |
""" |
| 561 |
params = intersight.module.params |
| 562 |
intersight.api_body = { |
| 563 |
'Organization': { |
| 564 |
'Name': params['organization'], |
| 565 |
}, |
| 566 |
'Name': params['name'] |
| 567 |
} |
| 568 |
if params['state'] == 'present': |
| 569 |
intersight.set_tags_and_description() |
| 570 |
intersight.api_body['CipherSuite'] = convert_to_api_format(params['cipher_suite'], 'cipher_suite') |
| 571 |
intersight.api_body['ConfidentialityOffset'] = convert_to_api_format(params['confidentiality_offset'], 'confidentiality_offset') |
| 572 |
intersight.api_body['SecurityPolicy'] = convert_to_api_format(params['security_policy'], 'security_policy') |
| 573 |
intersight.api_body['KeyServerPriority'] = params['key_server_priority'] |
| 574 |
intersight.api_body['ReplayWindowSize'] = params['replay_window_size'] |
| 575 |
intersight.api_body['IncludeIcvIndicator'] = params['include_icv_indicator'] |
| 576 |
if params.get('sak_expiry_time') is not None: |
| 577 |
intersight.api_body['SakExpiryTime'] = params['sak_expiry_time'] |
| 578 |
|
| 579 |
intersight.api_body['MacSecEaPol'] = { |
| 580 |
'EaPolMacAddress': params['eapol_mac_address'], |
| 581 |
'EaPolEthertype': params['eapol_ethertype'] |
| 582 |
} |
| 583 |
|
| 584 |
intersight.api_body['PrimaryKeyChain'] = build_keychain_config( |
| 585 |
params['primary_keychain_name'], |
| 586 |
params.get('primary_keys', []) |
| 587 |
) |
| 588 |
if params.get('configure_fallback_keychain') and params.get('fallback_keychain_name'): |
| 589 |
intersight.api_body['FallbackKeyChain'] = build_keychain_config( |
| 590 |
params['fallback_keychain_name'], |
| 591 |
params.get('fallback_keys', []) |
| 592 |
) |
| 593 |
else: |
| 594 |
intersight.api_body['FallbackKeyChain'] = { |
| 595 |
'Name': '', |
| 596 |
'SecKeys': None |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
def main(): |
| 601 |
argument_spec = intersight_argument_spec.copy() |
| 602 |
argument_spec.update( |
| 603 |
state=dict(type='str', choices=['present', 'absent'], default='present'), |
| 604 |
organization=dict(type='str', default='default'), |
| 605 |
name=dict(type='str', required=True), |
| 606 |
description=dict(type='str', aliases=['descr']), |
| 607 |
tags=dict(type='list', elements='dict'), |
| 608 |
cipher_suite=dict(type='str', choices=['gcm-aes-xpn-256', 'gcm-aes-128', 'gcm-aes-256', 'gcm-aes-xpn-128'], default='gcm-aes-xpn-256'), |
| 609 |
confidentiality_offset=dict(type='str', choices=['conf-offset-0', 'conf-offset-30', 'conf-offset-50'], default='conf-offset-0'), |
| 610 |
security_policy=dict(type='str', choices=['should-secure', 'must-secure'], default='should-secure'), |
| 611 |
key_server_priority=dict(type='int', default=16), |
| 612 |
sak_expiry_time=dict(type='int'), |
| 613 |
replay_window_size=dict(type='int', default=148809600), |
| 614 |
include_icv_indicator=dict(type='bool', default=False), |
| 615 |
eapol_mac_address=dict(type='str', default='0180.C200.0003'), |
| 616 |
eapol_ethertype=dict(type='str', default='0x888e'), |
| 617 |
primary_keychain_name=dict(type='str'), |
| 618 |
primary_keys=dict( |
| 619 |
type='list', |
| 620 |
elements='dict', |
| 621 |
default=[], |
| 622 |
no_log=True, |
| 623 |
options=dict( |
| 624 |
id=dict(type='str', required=True), |
| 625 |
cryptographic_algorithm=dict(type='str', choices=['aes-256-cmac', 'aes-128-cmac'], default='aes-256-cmac'), |
| 626 |
secret=dict(type='str', required=True, no_log=True), |
| 627 |
key_lifetime_always_active=dict(type='bool', default=True), |
| 628 |
timezone=dict(type='str', choices=['utc', 'local'], default='utc'), |
| 629 |
start_time=dict(type='str'), |
| 630 |
lifetime_type=dict(type='str', choices=['never', 'on-this-day'], default='never'), |
| 631 |
end_time=dict(type='str') |
| 632 |
) |
| 633 |
), |
| 634 |
configure_fallback_keychain=dict(type='bool', default=False), |
| 635 |
fallback_keychain_name=dict(type='str'), |
| 636 |
fallback_keys=dict( |
| 637 |
type='list', |
| 638 |
elements='dict', |
| 639 |
default=[], |
| 640 |
no_log=True, |
| 641 |
options=dict( |
| 642 |
id=dict(type='str', required=True), |
| 643 |
cryptographic_algorithm=dict(type='str', choices=['aes-256-cmac', 'aes-128-cmac'], default='aes-256-cmac'), |
| 644 |
secret=dict(type='str', required=True, no_log=True), |
| 645 |
key_lifetime_always_active=dict(type='bool', default=True), |
| 646 |
timezone=dict(type='str', choices=['utc', 'local'], default='utc'), |
| 647 |
start_time=dict(type='str'), |
| 648 |
lifetime_type=dict(type='str', choices=['never', 'on-this-day'], default='never'), |
| 649 |
end_time=dict(type='str') |
| 650 |
) |
| 651 |
) |
| 652 |
) |
| 653 |
module = AnsibleModule( |
| 654 |
argument_spec, |
| 655 |
supports_check_mode=True, |
| 656 |
) |
| 657 |
if module.params['state'] == 'present': |
| 658 |
validate_macsec_configuration(module) |
| 659 |
|
| 660 |
intersight = IntersightModule(module) |
| 661 |
intersight.result['api_response'] = {} |
| 662 |
intersight.result['trace_id'] = '' |
| 663 |
|
| 664 |
build_api_body(intersight) |
| 665 |
|
| 666 |
resource_path = '/fabric/MacSecPolicies' |
| 667 |
intersight.configure_policy_or_profile(resource_path=resource_path) |
| 668 |
|
| 669 |
module.exit_json(**intersight.result) |
| 670 |
|
| 671 |
|
| 672 |
if __name__ == '__main__': |
| 673 |
main() |