Debian Codegraph @

ansible_collections/cisco/aci/plugins/modules/aci_aaa_security_default_settings.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2024, Akini Ross (@akinross) <[email protected]>
5 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "certified"}
12
13 DOCUMENTATION = r"""
14 ---
15 module: aci_aaa_security_default_settings
16 short_description: Manage AAA Key Rings (pki:Ep)
17 description:
18 - Manage AAA Key Rings on Cisco ACI fabrics.
19 options:
20 password_strength_check:
21 description:
22 - Enable password strength check.
23 - Use C(true) to enable and C(false) to disable.
24 - The APIC defaults to C(true) when unset during creation.
25 type: bool
26 password_strength_profile:
27 description:
28 - The password strength profile (aaa:PwdStrengthProfile).
29 type: dict
30 suboptions:
31 enable:
32 description:
33 - Enable or disable password strength profile.
34 - Use C(true) to enable and C(false) to disable.
35 type: bool
36 required: true
37 type:
38 description:
39 - The type of the password strength profile.
40 - The APIC defaults to C(any_three) when unset during creation.
41 type: str
42 choices: [ custom, any_three ]
43 min_length:
44 description:
45 - The minimum length of the password.
46 - The APIC defaults to C(8) when unset during creation.
47 type: int
48 aliases: [ minimum_length, min ]
49 max_length:
50 description:
51 - The maximum length of the password.
52 - The APIC defaults to C(64) when unset during creation.
53 type: int
54 aliases: [ maximum_length, max ]
55 class_flags:
56 description:
57 - The class flags of the password strength profile.
58 - At least 3 class flags must be specified.
59 - This attribute is only applicable when type is set to O(password_strength_profile.type=custom).
60 - The APIC defaults to C(digits,lowercase,uppercase) when unset during creation.
61 type: list
62 elements: str
63 choices: [ digits, lowercase, specialchars, uppercase ]
64 aliases: [ flags ]
65 password_change:
66 description:
67 - The password change interval (aaa:PwdProfile).
68 type: dict
69 suboptions:
70 enable:
71 description:
72 - Enforce password change interval.
73 - Use C(true) to enable and C(false) to disable.
74 - The APIC defaults to C(true) when unset during creation.
75 type: bool
76 interval:
77 description:
78 - The password change interval in hours.
79 - The APIC defaults to C(48) when unset during creation.
80 type: int
81 allowed_changes:
82 description:
83 - The number of changes allowed within the change interval.
84 - The APIC defaults to C(2) when unset during creation.
85 type: int
86 minimum_period:
87 description:
88 - The minimum period between password changes in hours.
89 - The APIC defaults to C(24) when unset during creation.
90 type: int
91 aliases: [ minimum_period_between_password_changes, min_period ]
92 history_storage_amount:
93 description:
94 - The number of recent user passwords to store.
95 - The APIC defaults to C(5) when unset during creation.
96 type: int
97 aliases: [ history, amount ]
98 lockout:
99 description:
100 - Lockout behaviour after multiple failed login attempts (aaa:BlockLoginProfile).
101 type: dict
102 suboptions:
103 enable:
104 description:
105 - Enable lockout behaviour.
106 - Use C(true) to enable and C(false) to disable.
107 - The APIC defaults to C(false) when unset during creation.
108 type: bool
109 max_attempts:
110 description:
111 - The maximum number of failed attempts before user is locked out.
112 - The APIC defaults to C(5) when unset during creation.
113 type: int
114 aliases: [ max_failed_attempts, failed_attempts, attempts ]
115 window:
116 description:
117 - The time period in which consecutive attempts were failed in minutes.
118 - The APIC defaults to C(5) when unset during creation.
119 type: int
120 aliases: [ max_failed_attempts_window, failed_attempts_window ]
121 duration:
122 description:
123 - The duration of lockout in minutes.
124 - The APIC defaults to C(60) when unset during creation.
125 type: int
126 web_token:
127 description:
128 - The web token related configuration (pki:WebTokenData).
129 type: dict
130 suboptions:
131 timeout:
132 description:
133 - The web token timeout in seconds.
134 - The APIC defaults to C(600) when unset during creation.
135 type: int
136 idle_timeout:
137 description:
138 - The web/console (SSH/Telnet) session idle timeout in seconds.
139 - The APIC defaults to C(1200) when unset during creation.
140 type: int
141 aliases: [ session_idle_timeout ]
142 validity_period:
143 description:
144 - The maximum validity period in hours.
145 - The APIC defaults to C(24) when unset during creation.
146 type: int
147 aliases: [ maximum_validity_period ]
148 refresh:
149 description:
150 - Include refresh in session records.
151 - Use C(true) to include and C(false) to exclude.
152 - The APIC defaults to C(false) when unset during creation.
153 type: bool
154 state:
155 description:
156 - Use C(present) or C(absent) for adding or removing.
157 - Use C(query) for listing an object or multiple objects.
158 type: str
159 choices: [ present, query ]
160 default: present
161 name_alias:
162 description:
163 - The alias for the current object. This relates to the nameAlias field in ACI.
164 type: str
165 extends_documentation_fragment:
166 - cisco.aci.aci
167 - cisco.aci.annotation
168 - cisco.aci.owner
169
170 seealso:
171 - name: APIC Management Information Model reference
172 description: More information about the internal APIC class B(pki:Ep).
173 link: https://developer.cisco.com/docs/apic-mim-ref/
174 author:
175 - Akini Ross (@akinross)
176 """
177
178 EXAMPLES = r"""
179 - name: Set AAA Security Default Settings
180 cisco.aci.aci_aaa_security_default_settings:
181 host: apic
182 username: admin
183 password: SomeSecretPassword
184 password_strength_check: true
185 password_strength_profile:
186 enable: true
187 type: custom
188 min_length: 10
189 max_length: 60
190 class_flags:
191 - digits
192 - lowercase
193 - specialchars
194 - uppercase
195 password_change:
196 enable: true
197 interval: 49
198 allowed_changes: 6
199 minimum_period_between_password_changes: 25
200 history_storage_amount: 6
201 lockout:
202 enable: true
203 max_attempts: 6
204 window: 6
205 duration: 61
206 web_token:
207 timeout: 601
208 idle_timeout: 1201
209 validity_period: 23
210 refresh: true
211 state: present
212 delegate_to: localhost
213
214 - name: Set AAA Security Default Settings to Default Values
215 cisco.aci.aci_aaa_security_default_settings:
216 host: apic
217 username: admin
218 password: SomeSecretPassword
219 password_strength_check: true
220 password_strength_profile:
221 enable: false
222 password_change:
223 enable: true
224 interval: 48
225 allowed_changes: 2
226 minimum_period_between_password_changes: 24
227 history_storage_amount: 5
228 lockout:
229 enable: false
230 max_attempts: 5
231 window: 5
232 duration: 60
233 web_token:
234 timeout: 600
235 idle_timeout: 1200
236 validity_period: 24
237 refresh: false
238 state: present
239 delegate_to: localhost
240
241 - name: Query AAA Security Default Settings
242 cisco.aci.aci_aaa_security_default_settings:
243 host: apic
244 username: admin
245 password: SomeSecretPassword
246 state: query
247 delegate_to: localhost
248 register: query_result
249 """
250
251 RETURN = r"""
252 current:
253 description: The existing configuration from the APIC after the module has finished
254 returned: success
255 type: list
256 sample:
257 [
258 {
259 "fvTenant": {
260 "attributes": {
261 "descr": "Production environment",
262 "dn": "uni/tn-production",
263 "name": "production",
264 "nameAlias": "",
265 "ownerKey": "",
266 "ownerTag": ""
267 }
268 }
269 }
270 ]
271 error:
272 description: The error information as returned from the APIC
273 returned: failure
274 type: dict
275 sample:
276 {
277 "code": "122",
278 "text": "unknown managed object class foo"
279 }
280 raw:
281 description: The raw output returned by the APIC REST API (xml or json)
282 returned: parse error
283 type: str
284 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
285 sent:
286 description: The actual/minimal configuration pushed to the APIC
287 returned: info
288 type: list
289 sample:
290 {
291 "fvTenant": {
292 "attributes": {
293 "descr": "Production environment"
294 }
295 }
296 }
297 previous:
298 description: The original configuration from the APIC before the module has started
299 returned: info
300 type: list
301 sample:
302 [
303 {
304 "fvTenant": {
305 "attributes": {
306 "descr": "Production",
307 "dn": "uni/tn-production",
308 "name": "production",
309 "nameAlias": "",
310 "ownerKey": "",
311 "ownerTag": ""
312 }
313 }
314 }
315 ]
316 proposed:
317 description: The assembled configuration from the user-provided parameters
318 returned: info
319 type: dict
320 sample:
321 {
322 "fvTenant": {
323 "attributes": {
324 "descr": "Production environment",
325 "name": "production"
326 }
327 }
328 }
329 filter_string:
330 description: The filter string used for the request
331 returned: failure or debug
332 type: str
333 sample: ?rsp-prop-include=config-only
334 method:
335 description: The HTTP method used for the request to the APIC
336 returned: failure or debug
337 type: str
338 sample: POST
339 response:
340 description: The HTTP response from the APIC
341 returned: failure or debug
342 type: str
343 sample: OK (30 bytes)
344 status:
345 description: The HTTP status from the APIC
346 returned: failure or debug
347 type: int
348 sample: 200
349 url:
350 description: The HTTP url used for the request to the APIC
351 returned: failure or debug
352 type: str
353 sample: https://10.11.12.13/api/mo/uni/tn-production.json
354 """
355
356 from ansible.module_utils.basic import AnsibleModule
357 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
358
359
360 def main():
361 argument_spec = aci_argument_spec()
362 argument_spec.update(aci_annotation_spec())
363 argument_spec.update(aci_owner_spec())
364 argument_spec.update(
365 password_strength_check=dict(type="bool", no_log=False),
366 password_strength_profile=dict(
367 type="dict",
368 no_log=False,
369 options=dict(
370 enable=dict(type="bool", required=True),
371 type=dict(type="str", choices=["custom", "any_three"]),
372 min_length=dict(type="int", aliases=["minimum_length", "min"]),
373 max_length=dict(type="int", aliases=["maximum_length", "max"]),
374 class_flags=dict(type="list", elements="str", choices=["digits", "lowercase", "specialchars", "uppercase"], aliases=["flags"]),
375 ),
376 ),
377 password_change=dict(
378 type="dict",
379 no_log=False,
380 options=dict(
381 enable=dict(type="bool"),
382 interval=dict(type="int"),
383 allowed_changes=dict(type="int"),
384 minimum_period=dict(type="int", aliases=["minimum_period_between_password_changes", "min_period"]),
385 history_storage_amount=dict(type="int", aliases=["history", "amount"]),
386 ),
387 ),
388 lockout=dict(
389 type="dict",
390 options=dict(
391 enable=dict(type="bool"),
392 max_attempts=dict(type="int", aliases=["max_failed_attempts", "failed_attempts", "attempts"]),
393 window=dict(type="int", aliases=["max_failed_attempts_window", "failed_attempts_window"]),
394 duration=dict(type="int"),
395 ),
396 ),
397 web_token=dict(
398 type="dict",
399 no_log=False,
400 options=dict(
401 timeout=dict(type="int"),
402 idle_timeout=dict(type="int", aliases=["session_idle_timeout"]),
403 validity_period=dict(type="int", aliases=["maximum_validity_period"]),
404 refresh=dict(type="bool"),
405 ),
406 ),
407 state=dict(type="str", default="present", choices=["present", "query"]),
408 name_alias=dict(type="str"),
409 )
410
411 module = AnsibleModule(
412 argument_spec=argument_spec,
413 supports_check_mode=True,
414 )
415 aci = ACIModule(module)
416
417 password_strength_check = aci.boolean(module.params.get("password_strength_check"))
418 password_strength_profile = module.params.get("password_strength_profile")
419 password_change = module.params.get("password_change")
420 lockout = module.params.get("lockout")
421 web_token = module.params.get("web_token")
422 state = module.params.get("state")
423 name_alias = module.params.get("name_alias")
424
425 aci_class = "aaaUserEp"
426 child_classes = ["aaaPwdStrengthProfile", "aaaPwdProfile", "aaaBlockLoginProfile", "pkiWebTokenData"]
427
428 aci.construct_url(
429 root_class=dict(
430 aci_class=aci_class,
431 aci_rn="userext",
432 ),
433 child_classes=child_classes,
434 )
435 aci.get_existing()
436
437 if state == "present":
438 child_configs = []
439
440 class_config = dict(
441 pwdStrengthCheck=password_strength_check,
442 nameAlias=name_alias,
443 )
444
445 if password_strength_profile:
446 if password_strength_profile.get("enable"):
447 child_configs.append(
448 dict(
449 aaaPwdStrengthProfile=dict(
450 attributes=dict(
451 pwdStrengthTestType=password_strength_profile.get("type"),
452 pwdMinLength=password_strength_profile.get("min_length"),
453 pwdMaxLength=password_strength_profile.get("max_length"),
454 pwdClassFlags=",".join(sorted(password_strength_profile.get("class_flags"))),
455 ),
456 ),
457 ),
458 )
459 # Delete existing aaaPwdStrengthProfile if enable is set to false and it exists
460 # This is done for setting the correct output for changed state
461 elif len(aci.existing) > 0 and len(aci.existing[0].get("aaaUserEp", {}).get("children", {})) > 3:
462 for child in aci.existing[0].get("aaaUserEp", {}).get("children", {}):
463 if "aaaPwdStrengthProfile" in child.keys():
464 child_configs.append(dict(aaaPwdStrengthProfile=dict(attributes=dict(status="deleted"))))
465 break
466
467 if password_change:
468 child_configs.append(
469 dict(
470 aaaPwdProfile=dict(
471 attributes=dict(
472 changeDuringInterval=aci.boolean(password_change.get("enable"), "enable", "disable"),
473 changeInterval=password_change.get("interval"),
474 changeCount=password_change.get("allowed_changes"),
475 noChangeInterval=password_change.get("minimum_period"),
476 historyCount=password_change.get("history_storage_amount"),
477 ),
478 ),
479 ),
480 )
481
482 if lockout:
483 child_configs.append(
484 dict(
485 aaaBlockLoginProfile=dict(
486 attributes=dict(
487 enableLoginBlock=aci.boolean(lockout.get("enable"), "enable", "disable"),
488 maxFailedAttempts=lockout.get("max_attempts"),
489 maxFailedAttemptsWindow=lockout.get("window"),
490 blockDuration=lockout.get("duration"),
491 ),
492 ),
493 ),
494 )
495
496 if web_token:
497 child_configs.append(
498 dict(
499 pkiEp=dict(
500 attributes=dict(descr=""),
501 children=[
502 dict(
503 pkiWebTokenData=dict(
504 attributes=dict(
505 webtokenTimeoutSeconds=web_token.get("timeout"),
506 uiIdleTimeoutSeconds=web_token.get("idle_timeout"),
507 maximumValidityPeriod=web_token.get("validity_period"),
508 sessionRecordFlags="login,logout,refresh" if web_token.get("refresh") else "login,logout",
509 ),
510 ),
511 ),
512 ],
513 ),
514 ),
515 )
516
517 aci.payload(
518 aci_class=aci_class,
519 class_config=class_config,
520 child_configs=child_configs,
521 )
522
523 aci.get_diff(aci_class=aci_class)
524
525 aci.post_config()
526
527 aci.exit_json()
528
529
530 if __name__ == "__main__":
531 main()