Debian Codegraph @

ansible_collections/dellemc/enterprise_sonic/plugins/modules/sonic_mfa.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # Copyright 2025 Dell Inc. or its subsidiaries. All Rights Reserved
4 # GNU General Public License v3.0+
5 # (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 """
8 The module file for sonic_mfa
9 """
10
11 from __future__ import absolute_import, division, print_function
12 __metaclass__ = type
13
14 DOCUMENTATION = """
15 ---
16 module: sonic_mfa
17 version_added: 4.0.0
18 short_description: Manage Multi-factor authentication (MFA) configurations on SONiC.
19 description:
20 - This module provides configuration management of MFA
21 parameters for devices running SONiC.
22 - Pre-configured host cert is required for MFA security profile, and
23 ca-cert for RSA/CAC-PIV security profiles.
24 author: 'Divya Narendran (@Divya-N3)'
25 options:
26 config:
27 description:
28 - Specifies MFA configurations.
29 type: dict
30 suboptions:
31 mfa_global:
32 description:
33 - MFA Global configuration.
34 type: dict
35 suboptions:
36 key_seed:
37 description:
38 - Seed for generating secure key in MFA service.
39 - Plain text seed i.e. I(key_seed_encrypted=false) will be stored in encrypted format in
40 running-config, so idempotency will not be maintained and hence the task output will
41 always be I(changed=true).
42 type: str
43 key_seed_encrypted:
44 description:
45 - Indicates whether I(key_seed) is plain text or encrypted.
46 type: bool
47 security_profile:
48 description:
49 - Security profile contains the certificate for MFA service.
50 type: str
51 client_secret:
52 description:
53 - Password used in basic authorization header for MFA REST API.
54 - Minimum 32 characters with atleast one symbol, digit, uppercase, and lowercase.
55 - Plain text password i.e. I(client_secret_encrypted=false) will be stored in encrypted
56 format in running-config, so idempotency will not be maintained and hence the task
57 output will always be I(changed=true).
58 type: str
59 client_secret_encrypted:
60 description:
61 - Indicates whether I(client_secret) is plain text or encrypted.
62 type: bool
63 rsa_global:
64 description:
65 - RSA Global configuration.
66 type: dict
67 suboptions:
68 security_profile:
69 description:
70 - Security profile with CA-cert for validating RSA SecurID server.
71 type: str
72 rsa_servers:
73 description:
74 - RSA Server configuration.
75 type: list
76 elements: dict
77 suboptions:
78 hostname:
79 description:
80 - RSA server's hostname or IP address.
81 type: str
82 required: True
83 server_port:
84 description:
85 - Port number of the RSA SecurID server.
86 - Range 1025-49151.
87 type: int
88 client_id:
89 description:
90 - Unique identifier of the system as a client of SecurID service, assigned by SecurID service.
91 type: str
92 client_key:
93 description:
94 - Key associated with the client-id, assigned by SecurID service.
95 - Plain text key i.e. I(client_key_encrypted=false) will be stored in encrypted format
96 in running-config, so idempotency will not be maintained and hence the task output
97 will always be I(changed=true).
98 type: str
99 client_key_encrypted:
100 description:
101 - Indicates whether I(client_key) is plain text or encrypted.
102 type: bool
103 connection_timeout:
104 description:
105 - Timeout in seconds for connection to the SecurID server.
106 - Range 1-30.
107 type: int
108 read_timeout:
109 description:
110 - Timeout in seconds to read from the SecurID server.
111 - Range 1-150.
112 type: int
113 cac_piv_global:
114 description:
115 - CAC-PIV Global configuration.
116 type: dict
117 suboptions:
118 security_profile:
119 description:
120 - Security profile for SSH access with CAC-PIV.
121 type: str
122 cert_username_field:
123 description:
124 - SSH user certificate field for matching with SSH login username.
125 type: str
126 choices:
127 - common-name
128 - common-name-or-user-principal-name
129 - user-principal-name
130 cert_username_match:
131 description:
132 - Match option to parse the username from respective certificate field.
133 type: str
134 choices:
135 - 10digit-username
136 - first-name
137 - username-as-is
138 - username-without-domain
139 state:
140 description:
141 - The state of the configuration after module completion.
142 - C(merged) - Merges provided MFA configuration with on-device configuration.
143 - C(replaced) - Replaces on-device MFA configuration with provided configuration.
144 - C(overridden) - Overrides all on-device MFA configurations with the provided configuration.
145 - C(deleted) - Deletes on-device MFA configuration.
146 type: str
147 choices:
148 - merged
149 - deleted
150 - replaced
151 - overridden
152 default: merged
153 """
154
155 EXAMPLES = """
156 # Using "deleted" state
157 #
158 # Before state:
159 # -------------
160 #
161 # sonic# show running-configuration mfa
162 # mfa key-seed U2FsdGVkX1/caD7u0ZGRnb981G2DKyML/Gvyfexsurg= encrypted
163 # mfa client-secret U2FsdGVkX1+WlquxtZRbsgQhfS1lQBFbJKflxGAp6S3u+Ox5Hi+O16NmprjMVb3HQn1pNSgaaa0Cz1MHeTfDWhFR0WqdENbLU2PqkiRDHv0iVfl72xNPzhnGeO01kAu0 encrypted
164 # mfa security-profile mSecurityProfile
165 # mfa rsa-server security-profile rSecProfile
166 # mfa rsa-server host rsaserver.che-lab.it port 1030 client-id sonicdevtest.che-lab.it client-key
167 # U2FsdGVkX18QFJoB9dp8GJN92eP79FGOZDLgQakBmAasGYX77p6PtiiAfS/nGoOb2uEocUkryc+BLLYsg+Wz0gO+c1QsIbIhXk5Pt+aECoVgoFQ9QpxO9od9cTik+3Ot encrypted
168 # connection-timeout 29 read-timeout 149
169 #
170 # sonic# show running-configuration | grep "cac-piv"
171 # aaa cac-piv cert-user common-name
172 # aaa cac-piv cert-user-match 10digit-username
173 # aaa cac-piv security-profile cSecurityProfile
174 # sonic#
175
176
177 - name: Delete specified mfa configuration
178 dellemc.enterprise_sonic.sonic_mfa:
179 config:
180 mfa_global:
181 key_seed: 'U2FsdGVkX1/caD7u0ZGRnb981G2DKyML/Gvyfexsurg='
182 key_seed_encrypted: true
183 client_secret: 'U2FsdGVkX1+WlquxtZRbsgQhfS1lQBFbJKflxGAp6S3u+Ox5Hi+O16NmprjMVb3HQn1pNSgaaa0Cz1MHeTfDWhFR0WqdENbLU2PqkiRDHv0iVfl72xNPzhnGeO01kAu0'
184 client_secret_encrypted: true
185 rsa_global:
186 security_profile: 'rSecProfile'
187 rsa_servers:
188 hostname: 'rsaserver.che-lab.it'
189 server_port: 1030
190 client_id: 'sonicdevtest.che-lab.it'
191 client_key: 'U2FsdGVkX18QFJoB9dp8GJN92eP79FGOZDLgQakBmAasGYX77p6PtiiAfS/nGoOb2uEocUkryc+BLLYsg+Wz0gO+c1QsIbIhXk5Pt+aECoVgoFQ9QpxO9od9cTik+3Ot'
192 client_key_encrypted: true
193 connection_timeout: 29
194 read_timeout: 149
195 cac_piv_global:
196 security_profile: 'cSecurityProfile'
197 cert_username_field: 'common-name'
198 state: deleted
199
200
201 # After state:
202 # ------------
203 #
204 # sonic# show running-configuration mfa
205 # mfa security-profile mSecurityProfile
206 #
207 # sonic# show running-configuration | grep "cac-piv"
208 # aaa cac-piv cert-user-match 10digit-username
209 # sonic#
210
211
212 # Using "deleted" state
213 #
214 # Before state:
215 # -------------
216 #
217 # sonic# show running-configuration mfa
218 # mfa key-seed U2FsdGVkX1/caD7u0ZGRnb981G2DKyML/Gvyfexsurg= encrypted
219 # mfa client-secret U2FsdGVkX1+WlquxtZRbsgQhfS1lQBFbJKflxGAp6S3u+Ox5Hi+O16NmprjMVb3HQn1pNSgaaa0Cz1MHeTfDWhFR0WqdENbLU2PqkiRDHv0iVfl72xNPzhnGeO01kAu0 encrypted
220 # mfa security-profile mSecurityProfile
221 # mfa rsa-server security-profile rSecProfile
222 # mfa rsa-server host rsaserver.che-lab.it port 1030 client-id sonicdevtest.che-lab.it client-key
223 # U2FsdGVkX18QFJoB9dp8GJN92eP79FGOZDLgQakBmAasGYX77p6PtiiAfS/nGoOb2uEocUkryc+BLLYsg+Wz0gO+c1QsIbIhXk5Pt+aECoVgoFQ9QpxO9od9cTik+3Ot encrypted
224 # connection-timeout 29 read-timeout 149
225 #
226 # sonic# show running-configuration | grep "cac-piv"
227 # aaa cac-piv cert-user common-name
228 # aaa cac-piv cert-user-match 10digit-username
229 # aaa cac-piv security-profile cSecurityProfile
230 # sonic#
231
232
233 - name: Delete all mfa configurations
234 dellemc.enterprise_sonic.sonic_mfa:
235 config:
236 state: deleted
237
238
239 # After state:
240 # ------------
241 #
242 # sonic# show running-configuration mfa
243 # sonic#
244 #
245 # sonic# show running-configuration | grep "cac-piv"
246 # sonic#
247
248
249 # Using "merged" state
250 #
251 # Before State:
252 # -------------
253 #
254 # sonic# show running-configuration mfa
255 # sonic#
256 #
257 # sonic# show running-configuration | grep "cac-piv"
258 # sonic#
259
260
261 - name: Merge provided MFA configurations
262 dellemc.enterprise_sonic.sonic_mfa:
263 config:
264 mfa_global:
265 security_profile: 'mSecurityProfile'
266 key_seed: 'sonic'
267 key_seed_encrypted: true
268 client_secret: 'U2FsdGVkX18mPdwkM1z24i7lxMtqNZR9p2q3aa6YXR16OfDxQXCR9z9I0lQZpVjE!'
269 client_secret_encrypted: true
270 rsa_global:
271 security_profile: 'rSecProfile'
272 rsa_servers:
273 hostname: 'rsaserver.che-lab.it'
274 server_port: 1030
275 client_id: 'sonicdevtest.che-lab.it'
276 client_key: 'aplr05825jshusp80699scuv62u5l3lu63wxf66b0y883w92677ac0c9m0lwv6o8'
277 client_key_encrypted: true
278 connection_timeout: 29
279 read_timeout: 149
280 cac_piv_global:
281 security_profile: 'cSecurityProfile'
282 cert_username_field: 'user-principal-name'
283 cert_username_match: '10digit-username'
284 state: merged
285
286
287 # After State:
288 # ------------
289 #
290 # sonic# show running-configuration mfa
291 # mfa key-seed U2FsdGVkX1/caD7u0ZGRnb981G2DKyML/Gvyfexsurg= encrypted
292 # mfa client-secret U2FsdGVkX1+WlquxtZRbsgQhfS1lQBFbJKflxGAp6S3u+Ox5Hi+O16NmprjMVb3HQn1pNSgaaa0Cz1MHeTfDWhFR0WqdENbLU2PqkiRDHv0iVfl72xNPzhnGeO01kAu0 encrypted
293 # mfa security-profile mSecurityProfile
294 # mfa rsa-server security-profile rSecProfile
295 # mfa rsa-server host rsaserver.che-lab.it port 1030 client-id sonicdevtest.che-lab.it client-key
296 # U2FsdGVkX18QFJoB9dp8GJN92eP79FGOZDLgQakBmAasGYX77p6PtiiAfS/nGoOb2uEocUkryc+BLLYsg+Wz0gO+c1QsIbIhXk5Pt+aECoVgoFQ9QpxO9od9cTik+3Ot encrypted
297 # connection-timeout 29 read-timeout 149
298 #
299 # sonic# show running-configuration | grep "cac-piv"
300 # aaa cac-piv cert-user user-principal-name
301 # aaa cac-piv cert-user-match 10digit-username
302 # aaa cac-piv security-profile cSecurityProfile
303
304
305 # Using "replaced" state
306 #
307 # Before state:
308 # -------------
309 #
310 # sonic# show running-configuration mfa
311 # mfa key-seed U2FsdGVkX1/caD7u0ZGRnb981G2DKyML/Gvyfexsurg= encrypted
312 # mfa rsa-server host rsaserver.che-lab.it port 1030 client-id sonicdevtest.che-lab.it client-key
313 # U2FsdGVkX1+xnsxfUrqCvBQg0KkPUm11R8Vpn2cXLHCWzL59k3Jm4/OrRiMOemPJccnEa8sMuynOAaySpHkaMOePtpedW0aApp+qicIF2Hz32LR4vB07b7OSx7OaEZBj encrypted
314 # connection-timeout 16 read-timeout 129
315
316
317 - name: Replace specified mfa rsa-server configuration
318 dellemc.enterprise_sonic.sonic_mfa:
319 config:
320 rsa_servers:
321 - hostname: 'rsaserver.che-lab.it'
322 server_port: 1050
323 client_id: 'sonicdevtest.che-lab.it'
324 client_key: 'aplr05825jshusp80699scuv62u5l3lu63wxf66b0y883w92677ac0c9m0lwv6o8'
325 client_key_encrypted: true
326 connection_timeout: 29
327 read_timeout: 149
328 state: replaced
329
330
331 # After state:
332 # ------------
333 #
334 # sonic# show running-configuration mfa
335 # mfa key-seed U2FsdGVkX1/caD7u0ZGRnb981G2DKyML/Gvyfexsurg= encrypted
336 # mfa rsa-server host rsaserver.che-lab.it port 1050 client-id sonicdevtest.che-lab.it client-key
337 # U2FsdGVkX1/b1Tjka6pWv1BjwGd1I8cfjXxBIIJ6ZK/JaZpGgPbNAnw6WmdstRWJz49A+bymj6gJfkGjbzlWQhGCGi4VofPStOdNktqDcIyk33AaDkO+awkzyi7HRxcB encrypted
338 # connection-timeout 29 read-timeout 149
339
340
341 # Using "overridden" state
342 #
343 # Before state:
344 # -------------
345 #
346 # sonic# show running-configuration mfa
347 # mfa key-seed U2FsdGVkX1/caD7u0ZGRnb981G2DKyML/Gvyfexsurg= encrypted
348 # mfa client-secret U2FsdGVkX1+WlquxtZRbsgQhfS1lQBFbJKflxGAp6S3u+Ox5Hi+O16NmprjMVb3HQn1pNSgaaa0Cz1MHeTfDWhFR0WqdENbLU2PqkiRDHv0iVfl72xNPzhnGeO01kAu0 encrypted
349 # mfa security-profile mSecurityProfile
350 # mfa rsa-server security-profile rSecProfile
351 # mfa rsa-server host sonicrsaserver.che-lab.it port 1030 client-id sonic.che-lab.it client-key
352 # U2FsdGVkX18QFJoB9dp8GJN92eP79FGOZDLgQakBmAasGYX77p6PtiiAfS/nGoOb2uEocUkryc+BLLYsg+Wz0gO+c1QsIbIhXk5Pt+aECoVgoFQ9QpxO9od9cTik+3Ot encrypted
353 # connection-timeout 29 read-timeout 149
354 #
355 # sonic# show running-configuration | grep "cac-piv"
356 # aaa cac-piv cert-user user-principal-name
357 # aaa cac-piv cert-user-match 10digit-username
358 # aaa cac-piv security-profile cSecurityProfile
359
360
361 - name: Override device configuration of mfa with provided configuration
362 dellemc.enterprise_sonic.sonic_mfa:
363 config:
364 cac_piv_global:
365 cert_username_match: 'first-name'
366 state: overridden
367
368
369 # After state:
370 # ------------
371 #
372 # sonic# show running-configuration | grep "cac-piv"
373 # aaa cac-piv cert-user-match first-name
374 """
375 RETURN = """
376 before:
377 description: The configuration prior to module invocation.
378 returned: always
379 type: dict
380 after:
381 description: The configuration resulting from module invocation.
382 returned: when changed
383 type: dict
384 generated_after:
385 description: The generated configuration from module invocation.
386 returned: when C(check_mode)
387 type: dict
388 commands:
389 description: The set of commands pushed to the remote device.
390 returned: always
391 type: list
392 sample: ['command 1', 'command 2', 'command 3']
393 """
394
395
396 from ansible.module_utils.basic import AnsibleModule
397 from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.argspec.mfa.mfa import MfaArgs
398 from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.config.mfa.mfa import Mfa
399
400
401 def main():
402 """
403 Main entry point for module execution
404
405 :returns: the result form module invocation
406 """
407 module = AnsibleModule(argument_spec=MfaArgs.argument_spec,
408 supports_check_mode=True)
409
410 result = Mfa(module).execute_module()
411 module.exit_json(**result)
412
413
414 if __name__ == '__main__':
415 main()