Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2022, 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_saml
20 version_added: '1.12.0'
21 short_description: Manage FlashArray SAML2 service and identity providers
22 description:
23 - Enable or disable FlashArray SAML2 providers
24 author:
25 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
26 options:
27 name:
28 description:
29 - Name of the SAML2 identity provider (IdP)
30 type: str
31 required: true
32 state:
33 description:
34 - Define whether the API client should exist or not, or test.
35 default: present
36 choices: [ absent, present, test ]
37 type: str
38 url:
39 description:
40 - The URL of the identity provider
41 type: str
42 array_url:
43 description:
44 - The URL of the FlashArray
45 type: str
46 metadata_url:
47 description:
48 - The URL of the identity provider metadata
49 type: str
50 enabled:
51 description:
52 - Defines the enabled state of the identity provider
53 default: false
54 type: bool
55 encrypt_asserts:
56 description:
57 - If set to true, SAML assertions will be encrypted by the identity provider
58 default: false
59 type: bool
60 sign_request:
61 description:
62 - If set to true, SAML requests will be signed by the service provider.
63 default: false
64 type: bool
65 x509_cert:
66 description:
67 - The X509 certificate that the service provider uses to verify the SAML
68 response signature from the identity provider
69 type: str
70 decryption_credential:
71 description:
72 - The credential used by the service provider to decrypt encrypted SAML assertions from the identity provider
73 type: str
74 signing_credential:
75 description:
76 - The credential used by the service provider to sign SAML requests
77 type: str
78 extends_documentation_fragment:
79 - purestorage.flasharray.purestorage.fa
80 """
81
82 EXAMPLES = r"""
83 - name: Create (disabled) SAML2 SSO with only metadata URL
84 purestorage.flasharray.purefa_saml:
85 name: myIDP
86 array_url: "https://10.10.10.2"
87 metadata_url: "https://myidp.acme.com/adfs/ls"
88 x509_cert: "{{lookup('file', 'x509_cert_file') }}"
89 fa_url: 10.10.10.2
90 api_token: e31060a7-21fc-e277-6240-25983c6c4592
91
92 - name: Enable SAML2 SSO
93 purestorage.flasharray.purefa_saml:
94 name: myISO
95 enabled: true
96 fa_url: 10.10.10.2
97 api_token: e31060a7-21fc-e277-6240-25983c6c4592
98
99 - name: Delete SAML2 SSO
100 purestorage.flasharray.purefa_saml:
101 state: absent
102 name: myIDP
103 fa_url: 10.10.10.2
104 api_token: e31060a7-21fc-e277-6240-25983c6c4592
105 """
106
107 RETURN = r"""
108 """
109
110 HAS_PURESTORAGE = True
111 try:
112 from pypureclient.flasharray import (
113 Saml2Sso,
114 Saml2SsoPost,
115 Saml2SsoSp,
116 Saml2SsoIdp,
117 ReferenceNoId,
118 )
119 except ImportError:
120 HAS_PURESTORAGE = False
121
122 from ansible.module_utils.basic import AnsibleModule
123 from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
124 get_array,
125 purefa_argument_spec,
126 )
127 from ansible_collections.purestorage.flasharray.plugins.module_utils.version import (
128 LooseVersion,
129 )
130
131 MIN_REQUIRED_API_VERSION = "2.11"
132
133
134 def test_saml(module, array):
135 """Test SAML2 IdP configuration"""
136 test_response = []
137 response = list(array.get_sso_saml2_idps_test(names=[module.params["name"]]).items)
138 for component in response:
139 if component.enabled:
140 enabled = "true"
141 else:
142 enabled = "false"
143 if component.success:
144 success = "true"
145 else:
146 success = "false"
147 test_response.append(
148 {
149 "component_address": component.component_address,
150 "component_name": component.component_name,
151 "description": component.description,
152 "destination": component.destination,
153 "enabled": enabled,
154 "result_details": getattr(component, "result_details", ""),
155 "success": success,
156 "test_type": component.test_type,
157 "resource_name": component.resource.name,
158 }
159 )
160 module.exit_json(changed=True, test_response=test_response)
161
162
163 def delete_saml(module, array):
164 """Delete SSO SAML2 IdP"""
165 changed = True
166 if not module.check_mode:
167 res = array.delete_sso_saml2_idps(names=[module.params["name"]])
168 if res.status_code != 200:
169 module.fail_json(
170 msg="Failed to delete SAML2 IdP {0}. Error: {1}".format(
171 module.params["name"], res.errors[0].message
172 )
173 )
174 module.exit_json(changed=changed)
175
176
177 def update_saml(module, array):
178 """Update SSO SAML2 IdP"""
179 changed = False
180 current_idp = list(array.get_sso_saml2_idps(names=[module.params["name"]]).items)[0]
181 old_idp = {
182 "array_url": current_idp.array_url,
183 "enabled": current_idp.enabled,
184 "sp_sign_cred": getattr(current_idp.sp.signing_credential, "name", None),
185 "sp_decrypt_cred": getattr(current_idp.sp.decryption_credential, "name", None),
186 "id_metadata": current_idp.idp.metadata_url,
187 "id_url": getattr(current_idp.idp, "url", None),
188 "id_sign_enabled": current_idp.idp.sign_request_enabled,
189 "id_encrypt_enabled": current_idp.idp.encrypt_assertion_enabled,
190 "id_cert": current_idp.idp.verification_certificate,
191 }
192 if module.params["url"]:
193 new_url = module.params["url"]
194 else:
195 new_url = old_idp["id_url"]
196 if module.params["array_url"]:
197 new_array_url = module.params["array_url"]
198 else:
199 new_array_url = old_idp["array_url"]
200 if module.params["enabled"] != old_idp["enabled"]:
201 new_enabled = module.params["enabled"]
202 else:
203 new_enabled = old_idp["enabled"]
204 if module.params["sign_request"] != old_idp["id_sign_enabled"]:
205 new_sign = module.params["sign_request"]
206 else:
207 new_sign = old_idp["id_sign_enabled"]
208 if module.params["encrypt_asserts"] != old_idp["id_encrypt_enabled"]:
209 new_encrypt = module.params["encrypt_asserts"]
210 else:
211 new_encrypt = old_idp["id_encrypt_enabled"]
212 if module.params["signing_credential"]:
213 new_sign_cred = module.params["signing_credential"]
214 else:
215 new_sign_cred = old_idp["sp_sign_cred"]
216 if module.params["decryption_credential"]:
217 new_decrypt_cred = module.params["decryption_credential"]
218 else:
219 new_decrypt_cred = old_idp["sp_decrypt_cred"]
220 if module.params["metadata_url"]:
221 new_meta_url = module.params["metadata_url"]
222 else:
223 new_meta_url = old_idp["id_metadata"]
224 if module.params["x509_cert"]:
225 new_cert = module.params["x509_cert"]
226 else:
227 new_cert = old_idp["id_cert"]
228 new_idp = {
229 "array_url": new_array_url,
230 "enabled": new_enabled,
231 "sp_sign_cred": new_sign_cred,
232 "sp_decrypt_cred": new_decrypt_cred,
233 "id_metadata": new_meta_url,
234 "id_sign_enabled": new_sign,
235 "id_encrypt_enabled": new_encrypt,
236 "id_url": new_url,
237 "id_cert": new_cert,
238 }
239 if old_idp != new_idp:
240 changed = True
241 if not module.check_mode:
242 sp = Saml2SsoSp(
243 decryption_credential=ReferenceNoId(name=new_idp["sp_decrypt_cred"]),
244 signing_credential=ReferenceNoId(name=new_idp["sp_sign_cred"]),
245 )
246 idp = Saml2SsoIdp(
247 url=new_idp["id_url"],
248 metadata_url=new_idp["id_metadata"],
249 sign_request_enabled=new_idp["id_sign_enabled"],
250 encrypt_assertion_enabled=new_idp["id_encrypt_enabled"],
251 verification_certificate=new_idp["id_cert"],
252 )
253 res = array.patch_sso_saml2_idps(
254 idp=Saml2Sso(
255 array_url=new_idp["array_url"],
256 idp=idp,
257 sp=sp,
258 enabled=new_idp["enabled"],
259 ),
260 names=[module.params["name"]],
261 )
262 if res.status_code != 200:
263 module.fail_json(
264 msg="Failed to update SAML2 IdP {0}. Error: {1}".format(
265 module.params["name"], res.errors[0].message
266 )
267 )
268 module.exit_json(changed=changed)
269
270
271 def create_saml(module, array):
272 """Create SAML2 IdP"""
273 changed = True
274 if not module.check_mode:
275 sp = Saml2SsoSp(
276 decryption_credential=ReferenceNoId(
277 name=module.params["decryption_credential"]
278 ),
279 signing_credential=ReferenceNoId(name=module.params["signing_credential"]),
280 )
281 idp = Saml2SsoIdp(
282 url=module.params["url"],
283 metadata_url=module.params["metadata_url"],
284 sign_request_enabled=module.params["sign_request"],
285 encrypt_assertion_enabled=module.params["encrypt_asserts"],
286 verification_certificate=module.params["x509_cert"],
287 )
288 if not module.check_mode:
289 res = array.post_sso_saml2_idps(
290 idp=Saml2SsoPost(array_url=module.params["array_url"], idp=idp, sp=sp),
291 names=[module.params["name"]],
292 )
293 if res.status_code != 200:
294 module.fail_json(
295 msg="Failed to create SAML2 Identity Provider {0}. Error message: {1}".format(
296 module.params["name"], res.errors[0].message
297 )
298 )
299 if module.params["enabled"]:
300 res = array.patch_sso_saml2_idps(
301 idp=Saml2Sso(enabled=module.params["enabled"]),
302 names=[module.params["name"]],
303 )
304 if res.status_code != 200:
305 array.delete_sso_saml2_idps(names=[module.params["name"]])
306 module.fail_json(
307 msg="Failed to create SAML2 Identity Provider {0}. Error message: {1}".format(
308 module.params["name"], res.errors[0].message
309 )
310 )
311
312 module.exit_json(changed=changed)
313
314
315 def main():
316 argument_spec = purefa_argument_spec()
317 argument_spec.update(
318 dict(
319 state=dict(
320 type="str", default="present", choices=["absent", "present", "test"]
321 ),
322 name=dict(type="str", required=True),
323 url=dict(type="str"),
324 array_url=dict(type="str"),
325 metadata_url=dict(type="str"),
326 x509_cert=dict(type="str", no_log=True),
327 signing_credential=dict(type="str"),
328 decryption_credential=dict(type="str"),
329 enabled=dict(type="bool", default=False),
330 encrypt_asserts=dict(type="bool", default=False),
331 sign_request=dict(type="bool", default=False),
332 )
333 )
334
335 required_if = [
336 ["encrypt_asserts", True, ["decryption_credential"]],
337 ["sign_request", True, ["signing_credential"]],
338 ]
339
340 module = AnsibleModule(
341 argument_spec, supports_check_mode=True, required_if=required_if
342 )
343
344 if not HAS_PURESTORAGE:
345 module.fail_json(msg="py-pure-client sdk is required for this module")
346
347 array = get_array(module)
348 api_version = array.get_rest_version()
349
350 if LooseVersion(MIN_REQUIRED_API_VERSION) > LooseVersion(api_version):
351 module.fail_json(
352 msg="FlashArray REST version not supported. "
353 "Minimum version required: {0}".format(MIN_REQUIRED_API_VERSION)
354 )
355 state = module.params["state"]
356 exists = True
357 res = array.get_sso_saml2_idps(names=[module.params["name"]])
358 if res.status_code != 200:
359 exists = False
360 if not exists and state == "present":
361 create_saml(module, array)
362 elif exists and state == "present":
363 update_saml(module, array)
364 elif exists and state == "absent":
365 delete_saml(module, array)
366 elif exists and state == "test":
367 test_saml(module, array)
368
369 module.exit_json(changed=False)
370
371
372 if __name__ == "__main__":
373 main()