Debian Codegraph @

ansible_collections/purestorage/flasharray/plugins/modules/purefa_ds.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2018, 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: purefa_ds
20 version_added: '1.0.0'
21 short_description: Configure FlashArray Directory Service
22 description:
23 - Set or erase configuration for the directory service. There is no facility
24 to SSL certificates at this time. Use the FlashArray GUI for this
25 additional configuration work.
26 - To modify an existing directory service configuration you must first delete
27 an existing configuration and then recreate with new settings.
28 author:
29 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
30 options:
31 state:
32 type: str
33 description:
34 - Create, delete or test directory service configuration
35 default: present
36 choices: [ absent, present, test ]
37 enable:
38 description:
39 - Whether to enable or disable directory service support.
40 default: false
41 type: bool
42 dstype:
43 description:
44 - The type of directory service to work on
45 choices: [ management, data ]
46 type: str
47 default: management
48 uri:
49 type: list
50 elements: str
51 description:
52 - A list of up to 30 URIs of the directory servers. Each URI must include
53 the scheme ldap:// or ldaps:// (for LDAP over SSL), a hostname, and a
54 domain name or IP address. For example, ldap://ad.company.com configures
55 the directory service with the hostname "ad" in the domain "company.com"
56 while specifying the unencrypted LDAP protocol.
57 base_dn:
58 type: str
59 description:
60 - Sets the base of the Distinguished Name (DN) of the directory service
61 groups. The base should consist of only Domain Components (DCs). The
62 base_dn will populate with a default value when a URI is entered by
63 parsing domain components from the URI. The base DN should specify DC=
64 for each domain component and multiple DCs should be separated by commas.
65 bind_password:
66 type: str
67 description:
68 - Sets the password of the bind_user user name account.
69 force_bind_password:
70 type: bool
71 default: true
72 description:
73 - Will force the bind password to be reset even if the bind user password
74 is unchanged.
75 - If set to I(false) and I(bind_user) is unchanged the password will not
76 be reset.
77 version_added: 1.14.0
78 bind_user:
79 type: str
80 description:
81 - Sets the user name that can be used to bind to and query the directory.
82 - For Active Directory, enter the username - often referred to as
83 sAMAccountName or User Logon Name - of the account that is used to
84 perform directory lookups.
85 - For OpenLDAP, enter the full DN of the user.
86 user_login:
87 type: str
88 description:
89 - User login attribute in the structure of the configured LDAP servers.
90 Typically the attribute field that holds the users unique login name.
91 Default value is I(sAMAccountName) for Active Directory or I(uid)
92 for all other directory services
93 user_object:
94 type: str
95 description:
96 - Value of the object class for a management LDAP user.
97 Defaults to I(User) for Active Directory servers, I(posixAccount) or
98 I(shadowAccount) for OpenLDAP servers dependent on the group type
99 of the server, or person for all other directory servers.
100 check_peer:
101 type: bool
102 description:
103 - Whether or not server authenticity is enforced when a certificate
104 is provided
105 default: false
106 version_added: 1.24.0
107 certificate:
108 type: str
109 description:
110 - The certificate of the Certificate Authority (CA) that signed the
111 certificates of the directory servers, which is used to validate the
112 authenticity of the configured servers
113 - A valid signed certificate in PEM format (Base64 encoded)
114 - Includes the "-----BEGIN CERTIFICATE-----" and "-----END CERTIFICATE-----" lines
115 version_added: 1.24.0
116 context:
117 description:
118 - Name of fleet member on which to perform the operation.
119 - This requires the array receiving the request is a member of a fleet
120 and the context name to be a member of the same fleet.
121 type: str
122 default: ""
123 version_added: '1.39.0'
124 extends_documentation_fragment:
125 - purestorage.flasharray.purestorage.fa
126 """
127
128 EXAMPLES = r"""
129 - name: Delete existing directory service
130 purestorage.flasharray.purefa_ds:
131 state: absent
132 fa_url: 10.10.10.2
133 api_token: e31060a7-21fc-e277-6240-25983c6c4592
134
135 - name: Update empty directory service (disabled)
136 purestorage.flasharray.purefa_ds:
137 dstype: management
138 uri: "ldap://lab.purestorage.com"
139 base_dn: "DC=lab,DC=purestorage,DC=com"
140 bind_user: Administrator
141 bind_password: password
142 fa_url: 10.10.10.2
143 api_token: e31060a7-21fc-e277-6240-25983c6c4592
144
145 - name: Enable existing directory service
146 purestorage.flasharray.purefa_ds:
147 enable: true
148 fa_url: 10.10.10.2
149 api_token: e31060a7-21fc-e277-6240-25983c6c4592
150
151 - name: Disable existing directory service
152 purestorage.flasharray.purefa_ds:
153 enable: false
154 fa_url: 10.10.10.2
155 api_token: e31060a7-21fc-e277-6240-25983c6c4592
156
157 - name: Update empty directory service (enabled)
158 purestorage.flasharray.purefa_ds:
159 enable: true
160 dstype: management
161 uri: "ldap://lab.purestorage.com"
162 base_dn: "DC=lab,DC=purestorage,DC=com"
163 bind_user: Administrator
164 bind_password: password
165 fa_url: 10.10.10.2
166 api_token: e31060a7-21fc-e277-6240-25983c6c4592
167
168 - name: Upload CA certificate for management DNS and check peer
169 purestorage.flasharray.purefa_ds:
170 enable: true
171 dstype: management
172 certificate: "{{lookup('file', 'ca_cert.pem') }}"
173 check_peer: true
174 fa_url: 10.10.10.2
175 api_token: e31060a7-21fc-e277-6240-25983c6c4592
176 """
177
178 RETURN = r"""
179 """
180
181 HAS_PURESTORAGE = True
182 try:
183 from pypureclient.flasharray import (
184 DirectoryService,
185 DirectoryServiceManagement,
186 )
187 except ImportError:
188 HAS_PURESTORAGE = False
189
190 from ansible.module_utils.basic import AnsibleModule
191 from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
192 get_array,
193 purefa_argument_spec,
194 )
195 from ansible_collections.purestorage.flasharray.plugins.module_utils.version import (
196 LooseVersion,
197 )
198
199 CONTEXT_VERSION = "2.42"
200
201
202 def delete_ds(module, array):
203 """Delete Directory Service"""
204 changed = True
205 api_version = array.get_rest_version()
206 if module.params["dstype"] == "management":
207 management = DirectoryServiceManagement(
208 user_login_attribute="", user_object_class=""
209 )
210 directory_service = DirectoryService(
211 uris=[""],
212 base_dn="",
213 bind_user="",
214 bind_password="",
215 enabled=False,
216 services=[module.params["dstype"]],
217 management=management,
218 )
219 else:
220 directory_service = DirectoryService(
221 uris=[""],
222 base_dn="",
223 bind_user="",
224 bind_password="",
225 enabled=False,
226 services=[module.params["dstype"]],
227 )
228 if not module.check_mode:
229 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
230 res = array.patch_directory_services(
231 names=[module.params["dstype"]],
232 directory_service=directory_service,
233 context_names=[module.params["context"]],
234 )
235 else:
236 res = array.patch_directory_services(
237 names=[module.params["dstype"]], directory_service=directory_service
238 )
239 if res.status_code != 200:
240 module.fail_json(
241 msg="Delete {0} Directory Service failed. Error message: {1}".format(
242 module.params["dstype"], res.errors[0].message
243 )
244 )
245 module.exit_json(changed=changed)
246
247
248 def update_ds(module, array):
249 """Update Directory Service"""
250 changed = False
251 api_version = array.get_rest_version()
252 ds_change = False
253 password_required = False
254 current_ds = []
255 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
256 dirservlist = list(
257 array.get_directory_services(context_names=[module.params["context"]]).items
258 )
259 else:
260 dirservlist = list(array.get_directory_services().items)
261 for dirs in dirservlist:
262 if dirs.name == module.params["dstype"]:
263 current_ds = dirs
264 if module.params["uri"] and current_ds.uris is None:
265 password_required = True
266 if module.params["uri"] and current_ds.uris != module.params["uri"]:
267 uris = module.params["uri"]
268 ds_change = True
269 else:
270 uris = current_ds.uris
271
272 base_dn = getattr(current_ds, "base_dn", "")
273 bind_user = getattr(current_ds, "bind_user", "")
274 cert = getattr(current_ds, "ca_certificate", None)
275 if module.params["base_dn"] and module.params["base_dn"] != base_dn:
276 base_dn = module.params["base_dn"]
277 ds_change = True
278 if module.params["enable"] != current_ds.enabled:
279 ds_change = True
280 if getattr(current_ds, "bind_password", None) is None:
281 password_required = True
282 if module.params["bind_user"] is not None:
283 if module.params["bind_user"] != bind_user:
284 bind_user = module.params["bind_user"]
285 password_required = True
286 ds_change = True
287 elif module.params["force_bind_password"]:
288 password_required = True
289 ds_change = True
290 if module.params["bind_password"] is not None and password_required:
291 bind_password = module.params["bind_password"]
292 ds_change = True
293 if password_required and not module.params["bind_password"]:
294 module.fail_json(msg="'bind_password' must be provided for this task")
295 if module.params["dstype"] == "management":
296 if module.params["certificate"] is not None:
297 if cert is None and module.params["certificate"] != "":
298 cert = module.params["certificate"]
299 ds_change = True
300 elif cert is None and module.params["certificate"] == "":
301 pass
302 elif module.params["certificate"] != cert:
303 cert = module.params["certificate"]
304 ds_change = True
305 if module.params["check_peer"] and not cert:
306 module.warn(
307 "Cannot check_peer without a CA certificate. Disabling check_peer"
308 )
309 module.params["check_peer"] = False
310 if module.params["check_peer"] != current_ds.check_peer:
311 ds_change = True
312 user_login = getattr(current_ds.management, "user_login_attribute", "")
313 user_object = getattr(current_ds.management, "user_object_class", "")
314 if (
315 module.params["user_object"] is not None
316 and user_object != module.params["user_object"]
317 ):
318 user_object = module.params["user_object"]
319 ds_change = True
320 if (
321 module.params["user_login"] is not None
322 and user_login != module.params["user_login"]
323 ):
324 user_login = module.params["user_login"]
325 ds_change = True
326 management = DirectoryServiceManagement(
327 user_login_attribute=user_login, user_object_class=user_object
328 )
329 if password_required:
330 directory_service = DirectoryService(
331 uris=uris,
332 base_dn=base_dn,
333 bind_user=bind_user,
334 bind_password=bind_password,
335 enabled=module.params["enable"],
336 services=[module.params["dstype"]],
337 management=management,
338 check_peer=module.params["check_peer"],
339 ca_certificate=cert,
340 )
341 else:
342 directory_service = DirectoryService(
343 uris=uris,
344 base_dn=base_dn,
345 bind_user=bind_user,
346 enabled=module.params["enable"],
347 services=[module.params["dstype"]],
348 management=management,
349 check_peer=module.params["check_peer"],
350 ca_certificate=cert,
351 )
352 else:
353 if password_required:
354 directory_service = DirectoryService(
355 uris=uris,
356 base_dn=base_dn,
357 bind_user=bind_user,
358 bind_password=bind_password,
359 enabled=module.params["enable"],
360 services=[module.params["dstype"]],
361 )
362 else:
363 directory_service = DirectoryService(
364 uris=uris,
365 base_dn=base_dn,
366 bind_user=bind_user,
367 enabled=module.params["enable"],
368 services=[module.params["dstype"]],
369 )
370 if ds_change:
371 changed = True
372 if not module.check_mode:
373 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
374 res = array.patch_directory_services(
375 names=[module.params["dstype"]],
376 directory_service=directory_service,
377 context_names=[module.params["context"]],
378 )
379 else:
380 res = array.patch_directory_services(
381 names=[module.params["dstype"]], directory_service=directory_service
382 )
383 if res.status_code != 200:
384 module.fail_json(
385 msg="{0} Directory Service failed. Error message: {1}".format(
386 module.params["dstype"].capitalize(), res.errors[0].message
387 )
388 )
389 module.exit_json(changed=changed)
390
391
392 def test_ds(module, array):
393 """Test directory services configuration"""
394 test_response = []
395 api_version = array.get_rest_version()
396 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
397 response = list(
398 array.get_directory_services_test(
399 names=[module.params["dstype"]],
400 context_names=[module.params["context"]],
401 ).items
402 )
403 else:
404 response = list(
405 array.get_directory_services_test(names=[module.params["dstype"]]).items
406 )
407 for component in response:
408 if component.enabled:
409 enabled = "true"
410 else:
411 enabled = "false"
412 if component.success:
413 success = "true"
414 else:
415 success = "false"
416 test_response.append(
417 {
418 "component_address": component.component_address,
419 "component_name": component.component_name,
420 "description": component.description,
421 "destination": component.destination,
422 "enabled": enabled,
423 "result_details": getattr(component, "result_details", ""),
424 "success": success,
425 "test_type": component.test_type,
426 "resource_name": component.resource.name,
427 }
428 )
429 module.exit_json(changed=False, test_response=test_response)
430
431
432 def main():
433 argument_spec = purefa_argument_spec()
434 argument_spec.update(
435 dict(
436 uri=dict(type="list", elements="str"),
437 state=dict(
438 type="str", default="present", choices=["absent", "present", "test"]
439 ),
440 enable=dict(type="bool", default=False),
441 force_bind_password=dict(type="bool", default=True, no_log=True),
442 bind_password=dict(type="str", no_log=True),
443 bind_user=dict(type="str"),
444 base_dn=dict(type="str"),
445 user_login=dict(type="str"),
446 user_object=dict(type="str"),
447 dstype=dict(
448 type="str", default="management", choices=["management", "data"]
449 ),
450 check_peer=dict(type="bool", default=False),
451 certificate=dict(type="str"),
452 context=dict(type="str", default=""),
453 )
454 )
455
456 module = AnsibleModule(argument_spec, supports_check_mode=True)
457
458 array = get_array(module)
459
460 if not HAS_PURESTORAGE:
461 module.fail_json(msg="py-pure-client sdk is required to for this module")
462
463 state = module.params["state"]
464 dirserv = []
465 dirservlist = list(array.get_directory_services().items)
466 for dirs in dirservlist:
467 if dirs.name == module.params["dstype"]:
468 dirserv = dirs
469 if dirserv:
470 if state == "absent":
471 if dirserv.uris != []:
472 delete_ds(module, array)
473 elif state == "test":
474 test_ds(module, array)
475 else:
476 update_ds(module, array)
477 else:
478 module.warn(
479 "Directory Service of type {0} does not exist. Check FlashArray configuration".format(
480 module.params["dstype"]
481 )
482 )
483
484 module.exit_json(changed=False)
485
486
487 if __name__ == "__main__":
488 main()