Debian Codegraph @

ansible_collections/purestorage/flashblade/plugins/modules/purefb_ad.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2021, Simon Dodsley ([email protected])
5 # GNU General Public License v3.0+ (see COPYING 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 = {
12 "metadata_version": "1.1",
13 "status": ["preview"],
14 "supported_by": "community",
15 }
16
17 DOCUMENTATION = r"""
18 ---
19 module: purefb_ad
20 version_added: '1.6.0'
21 short_description: Manage FlashBlade Active Directory Account
22 description:
23 - Add or delete FlashBlade Active Directory Account
24 - FlashBlade allows the creation of one AD computer account, or joining of an
25 existing AD computer account.
26 author:
27 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
28 options:
29 name:
30 description:
31 - Name of the AD account
32 type: str
33 required: true
34 existing:
35 description:
36 - Does the account I(name) already exist in the AD environment
37 type: bool
38 default: false
39 state:
40 description:
41 - Define whether the AD sccount is deleted or not
42 - Test state will perform a test against the suppied account name
43 - Rotate will force a keytab rotation for the specified account
44 default: present
45 choices: [ absent, present, test, rotate ]
46 type: str
47 computer:
48 description:
49 - The common name of the computer account to be created in the Active Directory domain.
50 - If not specified, defaults to the name of the Active Directory configuration.
51 type: str
52 domain:
53 description:
54 - The Active Directory domain to join
55 type: str
56 username:
57 description:
58 - A user capable of creating a computer account within the domain
59 type: str
60 password:
61 description:
62 - Password string for I(username)
63 type: str
64 encryption:
65 description:
66 - The encryption types that will be supported for use by clients for Kerberos authentication
67 type: list
68 elements: str
69 choices: [ aes256-sha1, aes128-sha1, aes256-cts-hmac-sha1-96, aes128-cts-hmac-sha1-96, arcfour-hma ]
70 default: aes256-sha1
71 join_ou:
72 description:
73 - Location where the Computer account will be created. e.g. OU=Arrays,OU=Storage.
74 - If left empty, defaults to B(CN=Computers).
75 type: str
76 directory_servers:
77 description:
78 - A list of directory servers that will be used for lookups related to user authorization
79 - Accepted server formats are IP address and DNS name
80 - All specified servers must be registered to the domain appropriately in the array
81 configured DNS and will only be communicated with over the secure LDAP (LDAPS) protocol.
82 If not specified, servers are resolved for the domain in DNS
83 - The specified list can have a maximum length of 5. If more are provided only the first
84 5 are used.
85 type: list
86 elements: str
87 kerberos_servers:
88 description:
89 - A list of key distribution servers to use for Kerberos protocol
90 - Accepted server formats are IP address and DNS name
91 - All specified servers must be registered to the domain appropriately in the array
92 configured DNS. If not specified, servers are resolved for the domain in DNS.
93 - The specified list can have a maximum length of 5. If more are provided only the first
94 5 are used.
95 type: list
96 elements: str
97 service_principals:
98 description:
99 - A list of SPNs for registering services with the domain.
100 - The service must be provided with the FQDN. See examples below.
101 - If not specified B(Computer Name.Domain) is used
102 type: list
103 elements: str
104 global_catalog_servers:
105 description:
106 - A list of global catalog servers that will be used for lookups related to user authorization.
107 - Accepted server formats are IP address and DNS name with optional @domain suffix.
108 If the suffix is ommited, the joined domain is assumed.
109 - All specified servers must be registered to the domain appropriately in the arrays
110 configured DNS and will only be communicated with over the secure LDAP (LDAPS) protocol.
111 type: list
112 elements: str
113 version_added: 1.20.0
114 service:
115 description:
116 - Service protocol for Active Directory principals
117 - This parameter is now IGNORED.
118 - Refer to FlashBlade User Guide for more details
119 - Use the I(service_principals) parameter instead to correctly define the service type to be used
120 for each principal.
121 type: list
122 elements: str
123 choices: ['nfs', 'cifs', 'HOST', '']
124 default: ''
125 server:
126 description:
127 - Name of the local array server into which the AD account is added
128 - Do not provide if the AD account is to connect to the default server
129 version_added: "1.23.0"
130 type: str
131 local_only:
132 description:
133 - Do a local-only delete of an active directory account
134 type: bool
135 default: false
136 extends_documentation_fragment:
137 - purestorage.flashblade.purestorage.fb
138 """
139
140 EXAMPLES = r"""
141 - name: Create new AD account
142 purestorage.flashblade.purefb_ad:
143 name: ad_account
144 server: local_server
145 computer: FLASHBLADE
146 domain: acme.com
147 username: Administrator
148 password: Password
149 join_ou: "CN=FakeOU"
150 encryption:
151 - aes128-cts-hmac-sha1-96
152 - aes256-cts-hmac-sha1-96
153 kerberos_servers:
154 - kdc.acme.com
155 directory_servers:
156 - ldap.acme.com
157 service_principals:
158 - "nfs/vip1.flashblade.acme.com"
159 - "HOST/vip2.flashblade.acme.com"
160 global_catalog_servers:
161 - gc1.flashblade.acme.com
162 fb_url: 10.10.10.2
163 api_token: T-55a68eb5-c785-4720-a2ca-8b03903bf641
164
165 - name: Connect to existing AD account
166 purestorage.flashblade.purefb_ad:
167 name: ad_account
168 computer: FLASHBLADE
169 domain: acme.com
170 username: Administrator
171 password: Password
172 existing: true
173 kerberos_servers:
174 - kdc.acme.com
175 directory_servers:
176 - ldap.acme.com
177 fb_url: 10.10.10.2
178 api_token: T-55a68eb5-c785-4720-a2ca-8b03903bf641
179
180 - name: Update existing AD account
181 purestorage.flashblade.purefb_ad:
182 name: ad_account
183 server: local_server
184 encryption:
185 - aes256-cts-hmac-sha1-96
186 kerberos_servers:
187 - kdc.acme.com
188 directory_servers:
189 - ldap.acme.com
190 service_principals:
191 - "HOST/vip1.flashblade.acme.com"
192 fb_url: 10.10.10.2
193 api_token: T-55a68eb5-c785-4720-a2ca-8b03903bf641
194
195 - name: Delete local AD account
196 purestorage.flashblade.purefb_ad:
197 name: ad_account
198 local_only: true
199 fb_url: 10.10.10.2
200 api_token: T-55a68eb5-c785-4720-a2ca-8b03903bf641
201
202 - name: Fully delete AD account
203 purestorage.flashblade.purefb_ad:
204 name: ad_account
205 fb_url: 10.10.10.2
206 api_token: T-55a68eb5-c785-4720-a2ca-8b03903bf641
207
208 - name: Test AD account
209 purestorage.flashblade.purefb_ad:
210 name: ad_account
211 state: test
212 fb_url: 10.10.10.2
213 api_token: T-55a68eb5-c785-4720-a2ca-8b03903bf641
214
215 - name: Rotate AD account keytabs
216 purestorage.flashblade.purefb_ad:
217 name: ad_account
218 state: rotate
219 fb_url: 10.10.10.2
220 api_token: T-55a68eb5-c785-4720-a2ca-8b03903bf641
221 """
222
223 RETURN = r"""
224 """
225
226 HAS_PURESTORAGE = True
227 try:
228 from pypureclient.flashblade import (
229 ActiveDirectoryPost,
230 ActiveDirectoryPatch,
231 Reference,
232 KeytabPost,
233 )
234 except ImportError:
235 HAS_PURESTORAGE = False
236
237 from ansible.module_utils.basic import AnsibleModule
238 from ansible_collections.purestorage.flashblade.plugins.module_utils.purefb import (
239 get_system,
240 purefb_argument_spec,
241 )
242
243 GC_SERVERS_API_VERSION = "2.12"
244 SERVERS_API_VERSION = "2.16"
245
246
247 def delete_account(module, blade):
248 """Delete Active directory Account"""
249 changed = True
250 if not module.check_mode:
251 res = blade.delete_active_directory(
252 names=[module.params["name"]], local_only=module.params["local_only"]
253 )
254 if res.status_code != 200:
255 module.fail_json(
256 msg="Failed to delete AD Account {0}".format(module.params["name"])
257 )
258 module.exit_json(changed=changed)
259
260
261 def create_account(module, blade):
262 """Create Active Directory Account"""
263 changed = True
264 api_version = list(blade.get_versions().items)
265 if not module.params["existing"]:
266 if GC_SERVERS_API_VERSION in api_version:
267 ad_config = ActiveDirectoryPost(
268 computer_name=module.params["computer"],
269 directory_servers=module.params["directory_servers"],
270 kerberos_servers=module.params["kerberos_servers"],
271 domain=module.params["domain"],
272 encryption_types=module.params["encryption"],
273 service_principal_names=module.params["service_principals"],
274 join_ou=module.params["join_ou"],
275 user=module.params["username"],
276 password=module.params["password"],
277 global_catalog_servers=module.params["global_catalog_servers"],
278 )
279 else:
280 ad_config = ActiveDirectoryPost(
281 computer_name=module.params["computer"],
282 directory_servers=module.params["directory_servers"],
283 kerberos_servers=module.params["kerberos_servers"],
284 domain=module.params["domain"],
285 encryption_types=module.params["encryption"],
286 service_principal_names=module.params["service_principals"],
287 join_ou=module.params["join_ou"],
288 user=module.params["username"],
289 password=module.params["password"],
290 )
291 if not module.check_mode:
292 res = blade.post_active_directory(
293 names=[module.params["name"]], active_directory=ad_config
294 )
295 if res.status_code != 200:
296 module.fail_json(
297 msg="Failed to add Active Directory Account {0}. Error: {1}".format(
298 module.params["name"], res.errors[0].message
299 )
300 )
301 else:
302 if GC_SERVERS_API_VERSION in api_version:
303 ad_config = ActiveDirectoryPost(
304 computer_name=module.params["computer"],
305 directory_servers=module.params["directory_servers"],
306 kerberos_servers=module.params["kerberos_servers"],
307 domain=module.params["domain"],
308 encryption_types=module.params["encryption"],
309 user=module.params["username"],
310 password=module.params["password"],
311 global_catalog_servers=module.params["global_catalog_servers"],
312 )
313 else:
314 ad_config = ActiveDirectoryPost(
315 computer_name=module.params["computer"],
316 directory_servers=module.params["directory_servers"],
317 kerberos_servers=module.params["kerberos_servers"],
318 domain=module.params["domain"],
319 encryption_types=module.params["encryption"],
320 user=module.params["username"],
321 password=module.params["password"],
322 )
323 if not module.check_mode:
324 res = blade.post_active_directory(
325 names=[module.params["name"]],
326 active_directory=ad_config,
327 join_existing_account=True,
328 )
329 if res.status_code != 200:
330 module.fail_json(
331 msg="Failed to add Active Directory Account {0}. Error: {1}".format(
332 module.params["name"], res.errors[0].message
333 )
334 )
335 module.exit_json(changed=changed)
336
337
338 def update_account(module, blade):
339 """Update Active Directory Account"""
340 api_version = list(blade.get_versions().items)
341 changed = False
342 mod_ad = False
343 current_ad = list(blade.get_active_directory(names=[module.params["name"]]).items)[
344 0
345 ]
346 attr = {}
347 if (
348 module.params["join_ou"] != current_ad.join_ou
349 and module.params["encryption"].sort() != current_ad.encryption_types.sort()
350 ):
351 module.fail_json(msg="Cannot make changes to OU when changing encryption types")
352 if module.params["directory_servers"]:
353 if current_ad.directory_servers:
354 if sorted(module.params["directory_servers"]) != sorted(
355 current_ad.directory_servers
356 ):
357 attr["directory_servers"] = module.params["directory_servers"]
358 mod_ad = True
359 if module.params["kerberos_servers"]:
360 if current_ad.kerberos_servers:
361 if sorted(module.params["kerberos_servers"]) != sorted(
362 current_ad.kerberos_servers
363 ):
364 attr["kerberos_servers"] = module.params["kerberos_servers"]
365 mod_ad = True
366 if module.params["join_ou"] != current_ad.join_ou:
367 attr["join_ou"] = module.params["join_ou"]
368 mod_ad = True
369 if sorted(module.params["encryption"]) != sorted(current_ad.encryption_types):
370 attr["encryption_types"] = module.params["encryption"]
371 mod_ad = True
372 if module.params["service"] and module.params["service"] != [""]:
373 module.fail_json(
374 msg="Please incorporate the service parameter into the "
375 "service_principals parameter for better security control."
376 )
377 if module.params["service_principals"]:
378 if current_ad.service_principal_names:
379 if sorted(module.params["service_principals"]) != sorted(
380 current_ad.service_principal_names
381 ):
382 attr["service_principal_names"] = module.params["service_principals"]
383 mod_ad = True
384 if GC_SERVERS_API_VERSION in api_version:
385 if module.params["global_catalog_servers"]:
386 if current_ad.global_catalog_servers:
387 if sorted(current_ad.global_catalog_servers) != sorted(
388 module.params["global_catalog_servers"]
389 ):
390 attr["global_catalog_servers"] = module.params[
391 "global_catalog_servers"
392 ]
393 mod_ad = True
394 if mod_ad:
395 changed = True
396 if not module.check_mode:
397 ad_attr = ActiveDirectoryPatch(**attr)
398 res = blade.patch_active_directory(
399 names=[module.params["name"]], active_directory=ad_attr
400 )
401 if res.status_code != 200:
402 module.fail_json(
403 msg="Failed to update Active Directory Account {0}. Error: {1}".format(
404 module.params["name"], res.errors[0].message
405 )
406 )
407 module.exit_json(changed=changed)
408
409
410 def test_account(module, blade):
411 """Test AD account configuration"""
412 test_response = []
413 response = list(
414 blade.get_active_directory_test(names=[module.params["name"]]).items
415 )
416 for component in range(len(response)):
417 if response[component].enabled:
418 enabled = "true"
419 else:
420 enabled = "false"
421 if response[component].success:
422 success = "true"
423 else:
424 success = "false"
425 test_response.append(
426 {
427 "component_name": response[component].component_name,
428 "description": response[component].description,
429 "destination": response[component].destination,
430 "enabled": enabled,
431 "success": success,
432 "test_type": response[component].test_type,
433 "resource_name": response[component].resource.name,
434 }
435 )
436 module.exit_json(changed=False, test_response=test_response)
437
438
439 def rotate_account(module, blade):
440 """Rotate AD account keytabs"""
441 account = Reference(name=module.params["name"], resource_type="active-directory")
442 keytab = KeytabPost(source=account)
443 if not module.check_mode:
444 res = blade.post_keytabs(keytab=keytab)
445 if res.status_code != 200:
446 module.fail_json(
447 msg="Keytab rotation failed for account {0}. Error: {1}".format(
448 module.params["name"], res.errors[0].message
449 )
450 )
451 module.exit_json(changed=True)
452
453
454 def main():
455 argument_spec = purefb_argument_spec()
456 argument_spec.update(
457 dict(
458 state=dict(
459 type="str",
460 default="present",
461 choices=["absent", "present", "test", "rotate"],
462 ),
463 username=dict(type="str"),
464 password=dict(type="str", no_log=True),
465 name=dict(type="str", required=True),
466 computer=dict(type="str"),
467 service=dict(
468 type="list",
469 elements="str",
470 default="",
471 choices=["nfs", "cifs", "HOST", ""],
472 ),
473 existing=dict(type="bool", default=False),
474 local_only=dict(type="bool", default=False),
475 domain=dict(type="str"),
476 join_ou=dict(type="str"),
477 server=dict(type="str"),
478 directory_servers=dict(type="list", elements="str"),
479 kerberos_servers=dict(type="list", elements="str"),
480 service_principals=dict(type="list", elements="str"),
481 global_catalog_servers=dict(type="list", elements="str"),
482 encryption=dict(
483 type="list",
484 elements="str",
485 choices=[
486 "aes256-sha1",
487 "aes128-sha1",
488 "aes256-cts-hmac-sha1-96",
489 "aes128-cts-hmac-sha1-96",
490 "arcfour-hma",
491 ],
492 default=["aes256-sha1"],
493 ),
494 )
495 )
496
497 module = AnsibleModule(argument_spec, supports_check_mode=True)
498
499 if not HAS_PURESTORAGE:
500 module.fail_json(msg="py-pure-client sdk is required for this module")
501
502 blade = get_system(module)
503 module.params["encryption"] = [
504 crypt.replace("aes256-sha1", "aes256-cts-hmac-sha1-96").replace(
505 "aes128-sha1", "aes128-cts-hmac-sha1-96"
506 )
507 for crypt in module.params["encryption"]
508 ]
509 state = module.params["state"]
510 exists = bool(
511 blade.get_active_directory(names=[module.params["name"]]).status_code == 200
512 )
513
514 if not module.params["computer"]:
515 module.params["computer"] = module.params["name"].replace("_", "-")
516 api_version = list(blade.get_versions().items)
517 if SERVERS_API_VERSION in api_version and module.params["server"]:
518 if blade.get_servers(names=[module.params["server"]]).status_code != 200:
519 module.fail_json(
520 msg="Server {0} does not exist on this FlashBlade.".format(
521 module.params["server"]
522 )
523 )
524 module.params["name"] = module.params["server"] + module.params["name"]
525 if module.params["kerberos_servers"]:
526 module.params["kerberos_servers"] = module.params["kerberos_servers"][0:5]
527 if module.params["directory_servers"]:
528 module.params["directory_servers"] = module.params["directory_servers"][0:5]
529
530 if not exists and state == "present":
531 create_account(module, blade)
532 elif exists and state == "present":
533 update_account(module, blade)
534 elif exists and state == "test":
535 test_account(module, blade)
536 elif exists and state == "rotate":
537 rotate_account(module, blade)
538 elif exists and state == "absent":
539 delete_account(module, blade)
540
541 module.exit_json(changed=False)
542
543
544 if __name__ == "__main__":
545 main()