1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
# (c) 2021, Simon Dodsley (simon@purestorage.com)
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
ANSIBLE_METADATA = {
"metadata_version": "1.1",
"status": ["preview"],
"supported_by": "community",
}
DOCUMENTATION = r"""
---
module: purefa_certs
version_added: '1.8.0'
short_description: Manage FlashArray SSL Certificates
description:
- Create, delete, import and export FlashArray SSL Certificates
author:
- Pure Storage Ansible Team (@sdodsley) <pure-ansible-team@purestorage.com>
options:
name:
description:
- Name of the SSL Certificate
type: str
default: management
state:
description:
- Action for the module to perform
- I(present) will create or re-create an SSL certificate
- I(absent) will delete an existing SSL certificate
- I(sign) will construct a Certificate Signing request (CSR)
- I(export) will export the exisitng SSL certificate
- I(import) will import a CA provided certificate.
default: present
choices: [ absent, present, import, export, sign ]
type: str
country:
type: str
description:
- The two-letter ISO code for the country where your organization is located
province:
type: str
description:
- The full name of the state or province where your organization is located
locality:
type: str
description:
- The full name of the city where your organization is located
organization:
type: str
description:
- The full and exact legal name of your organization.
- The organization name should not be abbreviated and should
include suffixes such as Inc, Corp, or LLC.
org_unit:
type: str
description:
- The department within your organization that is managing the certificate
common_name:
type: str
description:
- The fully qualified domain name (FQDN) of the current array
- For example, the common name for https://purearray.example.com is
purearray.example.com, or *.example.com for a wildcard certificate
- This can also be the management IP address of the array or the
shortname of the current array.
- Maximum of 64 characters
- If not provided this will default to the shortname of the array
email:
type: str
description:
- The email address used to contact your organization
key_size:
type: int
description:
- The key size in bits if you generate a new private key
default: 2048
choices: [ 1024, 2048, 4096 ]
days:
default: 3650
type: int
description:
- The number of valid days for the self-signed certificate being generated
- If not specified, the self-signed certificate expires after 3650 days.
generate:
default: false
type: bool
description:
- Generate a new private key.
- If not selected, the certificate will use the existing key
certificate:
type: str
description:
- Required for I(import)
- A valid signed certicate in PEM format (Base64 encoded)
- Includes the "-----BEGIN CERTIFICATE-----" and "-----END CERTIFICATE-----" lines
- Does not exceed 3000 characters in length
intermeadiate_cert:
type: str
description:
- Intermeadiate certificate provided by the CA
key:
type: str
description:
- If the Certificate Signed Request (CSR) was not constructed on the array
or the private key has changed since construction the CSR, provide
a new private key here
passphrase:
type: str
description:
- Passphrase if the private key is encrypted
export_file:
type: str
description:
- Name of file to contain Certificate Signing Request when `status sign`
- Name of file to export the current SSL Certificate when `status export`
- File will be overwritten if it already exists
extends_documentation_fragment:
- purestorage.flasharray.purestorage.fa
"""
EXAMPLES = r"""
- name: Create SSL certifcate foo
purestorage.flasharray.purefa_certs:
name: foo
key_size: 4096
country: US
province: FL
locality: Miami
organization: "Acme Inc"
org_unit: "DevOps"
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Delete SSL certificate foo
purestorage.flasharray.purefa_certs:
name: foo
state: absent
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Request CSR
purestorage.flasharray.purefa_certs:
state: sign
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Request CSR with updated fields
purestorage.flasharray.purefa_certs:
state: sign
org_unit: Development
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Regenerate key for SSL foo
purestorage.flasharray.purefa_certs:
generate: true
name: foo
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Import SSL Cert foo and Private Key
purestorage.flasharray.purefa_certs:
state: import
name: foo
certificate: "{{lookup('file', 'example.crt') }}"
key: "{{lookup('file', 'example.key') }}"
passphrase: password
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
"""
RETURN = r"""
"""
HAS_PURESTORAGE = True
try:
from pypureclient import flasharray
except ImportError:
HAS_PURESTORAGE = False
HAS_PYCOUNTRY = True
try:
import pycountry
except ImportError:
HAS_PYCOUNTRY = False
import re
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
get_array,
purefa_argument_spec,
)
from ansible_collections.purestorage.flasharray.plugins.module_utils.version import (
LooseVersion,
)
MIN_REQUIRED_API_VERSION = "2.4"
def update_cert(module, array):
"""Update existing SSL Certificate"""
changed = False
current_cert = list(array.get_certificates(names=[module.params["name"]]).items)[0]
new_cert = current_cert
if module.params["common_name"] and module.params["common_name"] != getattr(
current_cert, "common_name", None
):
new_cert.common_name = module.params["common_name"]
else:
new_cert.common_name = getattr(current_cert, "common_name", None)
if module.params["country"] and module.params["country"] != getattr(
current_cert, "country", None
):
new_cert.country = module.params["country"]
else:
new_cert.country = getattr(current_cert, "country")
if module.params["email"] and module.params["email"] != getattr(
current_cert, "email", None
):
new_cert.email = module.params["email"]
else:
new_cert.email = getattr(current_cert, "email", None)
if module.params["key_size"] and module.params["key_size"] != getattr(
current_cert, "key_size", None
):
new_cert.key_size = module.params["key_size"]
else:
new_cert.key_size = getattr(current_cert, "key_size", None)
if module.params["locality"] and module.params["locality"] != getattr(
current_cert, "locality", None
):
new_cert.locality = module.params["locality"]
else:
new_cert.locality = getattr(current_cert, "locality", None)
if module.params["province"] and module.params["province"] != getattr(
current_cert, "state", None
):
new_cert.state = module.params["province"]
else:
new_cert.state = getattr(current_cert, "state", None)
if module.params["organization"] and module.params["organization"] != getattr(
current_cert, "organization", None
):
new_cert.organization = module.params["organization"]
else:
new_cert.organization = getattr(current_cert, "organization", None)
if module.params["org_unit"] and module.params["org_unit"] != getattr(
current_cert, "organizational_unit", None
):
new_cert.organizational_unit = module.params["org_unit"]
else:
new_cert.organizational_unit = getattr(
current_cert, "organizational_unit", None
)
if new_cert != current_cert:
changed = True
certificate = flasharray.CertificatePost(
common_name=new_cert.common_name,
country=getattr(new_cert, "country", None),
email=getattr(new_cert, "email", None),
key_size=getattr(new_cert, "key_size", None),
locality=getattr(new_cert, "locality", None),
organization=getattr(new_cert, "organization", None),
organizational_unit=getattr(new_cert, "organizational_unit", None),
state=getattr(new_cert, "state", None),
)
if not module.check_mode:
res = array.patch_certificates(
names=[module.params["name"]],
certificate=certificate,
generate_new_key=module.params["generate"],
)
if res.status_code != 200:
module.fail_json(
msg="Updating existing SSL certificate {0} failed. Error: {1}".format(
module.params["name"], res.errors[0].message
)
)
module.exit_json(changed=changed)
def create_cert(module, array):
changed = True
certificate = flasharray.CertificatePost(
common_name=module.params["common_name"],
country=module.params["country"],
email=module.params["email"],
key_size=module.params["key_size"],
locality=module.params["locality"],
organization=module.params["organization"],
organizational_unit=module.params["org_unit"],
state=module.params["province"],
status="self-signed",
days=module.params["days"],
)
if not module.check_mode:
res = array.post_certificates(
names=[module.params["name"]], certificate=certificate
)
if res.status_code != 200:
module.fail_json(
msg="Creating SSL certificate {0} failed. Error: {1}".format(
module.params["name"], res.errors[0].message
)
)
module.exit_json(changed=changed)
def delete_cert(module, array):
changed = True
if module.params["name"] == "management":
module.fail_json(msg="management SSL cannot be deleted")
if not module.check_mode:
res = array.delete_certificates(names=[module.params["name"]])
if res.status_code != 200:
module.fail_json(
msg="Failed to delete {0} SSL certifcate. Error: {1}".format(
module.params["name"], res.errors[0].message
)
)
module.exit_json(changed=changed)
def import_cert(module, array, reimport=False):
"""Import a CA provided SSL certificate"""
changed = True
certificate = flasharray.CertificatePost(
certificate=module.params["certificate"],
intermediate_certificate=module.params["intermeadiate_cert"],
key=module.params["key"],
key_size=module.params["key_size"],
passphrase=module.params["passphrase"],
status="imported",
)
if not module.check_mode:
if reimport:
res = array.patch_certificates(
names=[module.params["name"]], certificate=certificate
)
else:
res = array.post_certificates(
names=[module.params["name"]], certificate=certificate
)
if res.status_code != 200:
module.fail_json(
msg="Importing Certificate failed. Error: {0}".format(
res.errors[0].message
)
)
module.exit_json(changed=changed)
def export_cert(module, array):
"""Export current SSL certificate"""
changed = True
if not module.check_mode:
ssl = array.get_certificates(names=[module.params["name"]])
if ssl.status_code != 200:
module.fail_json(
msg="Exporting Certificate failed. Error: {0}".format(
ssl.errors[0].message
)
)
ssl_file = open(module.params["export_file"], "w")
ssl_file.write(list(ssl.items)[0].certificate)
ssl_file.close()
module.exit_json(changed=changed)
def create_csr(module, array):
"""Construct a Certificate Signing Request
Output the result to a specified file
"""
changed = True
current_attr = list(array.get_certificates(names=[module.params["name"]]).items)[0]
try:
if module.params["common_name"] and module.params["common_name"] != getattr(
current_attr, "common_name", None
):
current_attr.common_name = module.params["common_name"]
except AttributeError:
pass
try:
if module.params["country"] and module.params["country"] != getattr(
current_attr, "country", None
):
current_attr.country = module.params["country"]
except AttributeError:
pass
try:
if module.params["email"] and module.params["email"] != getattr(
current_attr, "email", None
):
current_attr.email = module.params["email"]
except AttributeError:
pass
try:
if module.params["locality"] and module.params["locality"] != getattr(
current_attr, "locality", None
):
current_attr.locality = module.params["locality"]
except AttributeError:
pass
try:
if module.params["province"] and module.params["province"] != getattr(
current_attr, "state", None
):
current_attr.state = module.params["province"]
except AttributeError:
pass
try:
if module.params["organization"] and module.params["organization"] != getattr(
current_attr, "organization", None
):
current_attr.organization = module.params["organization"]
except AttributeError:
pass
try:
if module.params["org_unit"] and module.params["org_unit"] != getattr(
current_attr, "organizational_unit", None
):
current_attr.organizational_unit = module.params["org_unit"]
except AttributeError:
pass
if not module.check_mode:
certificate = flasharray.CertificateSigningRequestPost(
certificate={"name": module.params["name"]},
common_name=getattr(current_attr, "common_name", None),
country=getattr(current_attr, "country", None),
email=getattr(current_attr, "email", None),
locality=getattr(current_attr, "locality", None),
state=getattr(current_attr, "state", None),
organization=getattr(current_attr, "organization", None),
organizational_unit=getattr(current_attr, "organizational_unit", None),
)
csr = list(
array.post_certificates_certificate_signing_requests(
certificate=certificate
).items
)[0].certificate_signing_request
csr_file = open(module.params["export_file"], "w")
csr_file.write(csr)
csr_file.close()
module.exit_json(changed=changed)
def main():
argument_spec = purefa_argument_spec()
argument_spec.update(
dict(
state=dict(
type="str",
default="present",
choices=["absent", "present", "import", "export", "sign"],
),
generate=dict(type="bool", default=False),
name=dict(type="str", default="management"),
country=dict(type="str"),
province=dict(type="str"),
locality=dict(type="str"),
organization=dict(type="str"),
org_unit=dict(type="str"),
common_name=dict(type="str"),
email=dict(type="str"),
key_size=dict(type="int", default=2048, choices=[1024, 2048, 4096]),
certificate=dict(type="str", no_log=True),
intermeadiate_cert=dict(type="str", no_log=True),
key=dict(type="str", no_log=True),
export_file=dict(type="str"),
passphrase=dict(type="str", no_log=True),
days=dict(type="int", default=3650),
)
)
mutually_exclusive = [["certificate", "key_size"]]
required_if = [
["state", "import", ["certificate"]],
["state", "export", ["export_file"]],
["state", "sign", ["export_file"]],
]
module = AnsibleModule(
argument_spec,
mutually_exclusive=mutually_exclusive,
required_if=required_if,
supports_check_mode=True,
)
if not HAS_PURESTORAGE:
module.fail_json(msg="py-pure-client sdk is required for this module")
if not HAS_PYCOUNTRY:
module.fail_json(msg="pycountry sdk is required for this module")
email_pattern = r"^(\w|\.|\_|\-)+[@](\w|\_|\-|\.)+[.]\w{2,3}$"
array = get_array(module)
api_version = array.get_rest_version()
if LooseVersion(MIN_REQUIRED_API_VERSION) > LooseVersion(api_version):
module.fail_json(
msg="FlashArray REST version not supported. "
"Minimum version required: {0}".format(MIN_REQUIRED_API_VERSION)
)
if module.params["email"]:
if not re.search(email_pattern, module.params["email"]):
module.fail_json(
msg="Email {0} is not valid".format(module.params["email"])
)
if module.params["country"]:
if len(module.params["country"]) != 2:
module.fail_json(msg="Country must be a two-letter country (ISO) code")
if not pycountry.countries.get(alpha_2=module.params["country"].upper()):
module.fail_json(
msg="Country code {0} is not an assigned ISO 3166-1 code".format(
module.params["country"].upper()
)
)
state = module.params["state"]
if state in ["present"]:
if not module.params["common_name"]:
module.params["common_name"] = list(array.get_arrays().items)[0].name
module.params["common_name"] = module.params["common_name"][:64]
exists = bool(
array.get_certificates(names=[module.params["name"]]).status_code == 200
)
if not exists and state == "present":
create_cert(module, array)
elif exists and state == "present":
update_cert(module, array)
elif state == "sign":
create_csr(module, array)
elif not exists and state == "import":
import_cert(module, array)
elif exists and state == "import":
import_cert(module, array, reimport=True)
elif state == "export":
export_cert(module, array)
elif exists and state == "absent":
delete_cert(module, array)
module.exit_json(changed=False)
if __name__ == "__main__":
main()
|