2017-04-06 16:09:07 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# (c) 2017, Yanis Guenane <yanis+ansible@guenane.org>
|
2017-07-29 07:20:36 +00:00
|
|
|
# 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
|
|
|
|
|
2017-04-06 16:09:07 +00:00
|
|
|
|
2017-08-16 03:16:38 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
2017-04-06 17:16:50 +00:00
|
|
|
'status': ['preview'],
|
|
|
|
'supported_by': 'community'}
|
|
|
|
|
2017-04-06 16:09:07 +00:00
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: openssl_csr
|
|
|
|
author: "Yanis Guenane (@Spredzy)"
|
2017-04-17 19:46:12 +00:00
|
|
|
version_added: "2.4"
|
2017-04-06 16:09:07 +00:00
|
|
|
short_description: Generate OpenSSL Certificate Signing Request (CSR)
|
|
|
|
description:
|
|
|
|
- "This module allows one to (re)generates OpenSSL certificate signing requests.
|
2017-08-03 11:27:17 +00:00
|
|
|
It uses the pyOpenSSL python library to interact with openssl. This module supports
|
|
|
|
the subjectAltName as well as the keyUsage and extendedKeyUsage extensions.
|
|
|
|
Note: At least one of commonName or subjectAltName must be specified.
|
|
|
|
This module uses file common arguments to specify generated file permissions."
|
2017-04-06 16:09:07 +00:00
|
|
|
requirements:
|
|
|
|
- "python-pyOpenSSL"
|
|
|
|
options:
|
|
|
|
state:
|
|
|
|
required: false
|
|
|
|
default: "present"
|
|
|
|
choices: [ present, absent ]
|
|
|
|
description:
|
|
|
|
- Whether the certificate signing request should exist or not, taking action if the state is different from what is stated.
|
|
|
|
digest:
|
|
|
|
required: false
|
|
|
|
default: "sha256"
|
|
|
|
description:
|
|
|
|
- Digest used when signing the certificate signing request with the private key
|
|
|
|
privatekey_path:
|
|
|
|
required: true
|
|
|
|
description:
|
|
|
|
- Path to the privatekey to use when signing the certificate signing request
|
2017-07-19 10:02:29 +00:00
|
|
|
privatekey_passphrase:
|
|
|
|
required: false
|
|
|
|
description:
|
|
|
|
- The passphrase for the privatekey.
|
|
|
|
version_added: "2.4"
|
2017-04-06 16:09:07 +00:00
|
|
|
version:
|
|
|
|
required: false
|
|
|
|
default: 3
|
|
|
|
description:
|
|
|
|
- Version of the certificate signing request
|
|
|
|
force:
|
|
|
|
required: false
|
|
|
|
default: False
|
|
|
|
choices: [ True, False ]
|
|
|
|
description:
|
|
|
|
- Should the certificate signing request be forced regenerated by this ansible module
|
|
|
|
path:
|
|
|
|
required: true
|
|
|
|
description:
|
|
|
|
- Name of the folder in which the generated OpenSSL certificate signing request will be written
|
|
|
|
countryName:
|
|
|
|
required: false
|
|
|
|
aliases: [ 'C' ]
|
|
|
|
description:
|
|
|
|
- countryName field of the certificate signing request subject
|
|
|
|
stateOrProvinceName:
|
|
|
|
required: false
|
|
|
|
aliases: [ 'ST' ]
|
|
|
|
description:
|
|
|
|
- stateOrProvinceName field of the certificate signing request subject
|
|
|
|
localityName:
|
|
|
|
required: false
|
|
|
|
aliases: [ 'L' ]
|
|
|
|
description:
|
|
|
|
- localityName field of the certificate signing request subject
|
|
|
|
organizationName:
|
|
|
|
required: false
|
|
|
|
aliases: [ 'O' ]
|
|
|
|
description:
|
|
|
|
- organizationName field of the certificate signing request subject
|
2017-07-19 09:17:45 +00:00
|
|
|
organizationalUnitName:
|
2017-04-06 16:09:07 +00:00
|
|
|
required: false
|
|
|
|
aliases: [ 'OU' ]
|
|
|
|
description:
|
2017-07-19 09:17:45 +00:00
|
|
|
- organizationalUnitName field of the certificate signing request subject
|
2017-04-06 16:09:07 +00:00
|
|
|
commonName:
|
|
|
|
required: false
|
|
|
|
aliases: [ 'CN' ]
|
|
|
|
description:
|
|
|
|
- commonName field of the certificate signing request subject
|
|
|
|
emailAddress:
|
|
|
|
required: false
|
|
|
|
aliases: [ 'E' ]
|
|
|
|
description:
|
|
|
|
- emailAddress field of the certificate signing request subject
|
2017-08-03 11:27:17 +00:00
|
|
|
subjectAltName:
|
|
|
|
required: false
|
|
|
|
description:
|
|
|
|
- SAN extension to attach to the certificate signing request
|
|
|
|
- This can either be a 'comma separated string' or a YAML list.
|
2017-08-15 08:29:29 +00:00
|
|
|
subjectAltName_critical:
|
|
|
|
required: false
|
|
|
|
description:
|
|
|
|
- Should the subjectAltName extension be considered as critical
|
2017-08-03 11:27:17 +00:00
|
|
|
keyUsage:
|
|
|
|
required: false
|
|
|
|
description:
|
|
|
|
- This defines the purpose (e.g. encipherment, signature, certificate signing)
|
|
|
|
of the key contained in the certificate.
|
|
|
|
- This can either be a 'comma separated string' or a YAML list.
|
2017-08-15 08:29:29 +00:00
|
|
|
keyUsage_critical:
|
|
|
|
required: false
|
|
|
|
description:
|
|
|
|
- Should the keyUsage extension be considered as critical
|
2017-08-03 11:27:17 +00:00
|
|
|
extendedKeyUsage:
|
|
|
|
required: false
|
|
|
|
aliases: [ 'extKeyUsage' ]
|
|
|
|
description:
|
|
|
|
- Additional restrictions (e.g. client authentication, server authentication)
|
|
|
|
on the allowed purposes for which the public key may be used.
|
|
|
|
- This can either be a 'comma separated string' or a YAML list.
|
2017-08-15 08:29:29 +00:00
|
|
|
extendedKeyUsage_critical:
|
|
|
|
required: false
|
|
|
|
aliases: [ 'extKeyUsage_critical' ]
|
|
|
|
description:
|
|
|
|
- Should the extkeyUsage extension be considered as critical
|
2017-08-03 11:27:17 +00:00
|
|
|
|
|
|
|
notes:
|
|
|
|
- "If the certificate signing request already exists it will be checked whether subjectAltName,
|
|
|
|
keyUsage and extendedKeyUsage only contain the requested values and if the request was signed
|
|
|
|
by the given private key"
|
2017-04-06 16:09:07 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Generate an OpenSSL Certificate Signing Request
|
|
|
|
- openssl_csr:
|
|
|
|
path: /etc/ssl/csr/www.ansible.com.csr
|
|
|
|
privatekey_path: /etc/ssl/private/ansible.com.pem
|
|
|
|
commonName: www.ansible.com
|
|
|
|
|
2017-07-19 10:02:29 +00:00
|
|
|
# Generate an OpenSSL Certificate Signing Request with a
|
|
|
|
# passphrase protected private key
|
|
|
|
- openssl_csr:
|
|
|
|
path: /etc/ssl/csr/www.ansible.com.csr
|
|
|
|
privatekey_path: /etc/ssl/private/ansible.com.pem
|
|
|
|
privatekey_passphrase: ansible
|
|
|
|
commonName: www.ansible.com
|
|
|
|
|
2017-06-12 06:55:19 +00:00
|
|
|
# Generate an OpenSSL Certificate Signing Request with Subject information
|
2017-04-06 16:09:07 +00:00
|
|
|
- openssl_csr:
|
|
|
|
path: /etc/ssl/csr/www.ansible.com.csr
|
|
|
|
privatekey_path: /etc/ssl/private/ansible.com.pem
|
|
|
|
countryName: FR
|
|
|
|
organizationName: Ansible
|
|
|
|
emailAddress: jdoe@ansible.com
|
|
|
|
commonName: www.ansible.com
|
|
|
|
|
|
|
|
# Generate an OpenSSL Certificate Signing Request with subjectAltName extension
|
|
|
|
- openssl_csr:
|
|
|
|
path: /etc/ssl/csr/www.ansible.com.csr
|
|
|
|
privatekey_path: /etc/ssl/private/ansible.com.pem
|
|
|
|
subjectAltName: 'DNS:www.ansible.com,DNS:m.ansible.com'
|
|
|
|
|
|
|
|
# Force re-generate an OpenSSL Certificate Signing Request
|
|
|
|
- openssl_csr:
|
|
|
|
path: /etc/ssl/csr/www.ansible.com.csr
|
|
|
|
privatekey_path: /etc/ssl/private/ansible.com.pem
|
|
|
|
force: True
|
|
|
|
commonName: www.ansible.com
|
2017-08-03 11:27:17 +00:00
|
|
|
|
|
|
|
# Generate an OpenSSL Certificate Signing Request with special key usages
|
|
|
|
- openssl_csr:
|
|
|
|
path: /etc/ssl/csr/www.ansible.com.csr
|
|
|
|
privatekey_path: /etc/ssl/private/ansible.com.pem
|
|
|
|
commonName: www.ansible.com
|
|
|
|
keyUsage:
|
|
|
|
- digitlaSignature
|
|
|
|
- keyAgreement
|
|
|
|
extKeyUsage:
|
|
|
|
- clientAuth
|
2017-04-06 16:09:07 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
RETURN = '''
|
2017-08-03 11:27:17 +00:00
|
|
|
privatekey:
|
|
|
|
description: Path to the TLS/SSL private key the CSR was generated for
|
|
|
|
returned: changed or success
|
|
|
|
type: string
|
|
|
|
sample: /etc/ssl/private/ansible.com.pem
|
|
|
|
filename:
|
2017-04-06 16:09:07 +00:00
|
|
|
description: Path to the generated Certificate Signing Request
|
2017-04-26 14:56:13 +00:00
|
|
|
returned: changed or success
|
2017-04-06 16:09:07 +00:00
|
|
|
type: string
|
|
|
|
sample: /etc/ssl/csr/www.ansible.com.csr
|
|
|
|
subject:
|
|
|
|
description: A dictionnary of the subject attached to the CSR
|
2017-04-26 14:56:13 +00:00
|
|
|
returned: changed or success
|
2017-04-06 16:09:07 +00:00
|
|
|
type: list
|
|
|
|
sample: {'CN': 'www.ansible.com', 'O': 'Ansible'}
|
|
|
|
subjectAltName:
|
|
|
|
description: The alternative names this CSR is valid for
|
2017-04-26 14:56:13 +00:00
|
|
|
returned: changed or success
|
2017-08-03 11:27:17 +00:00
|
|
|
type: list
|
|
|
|
sample: [ 'DNS:www.ansible.com', 'DNS:m.ansible.com' ]
|
|
|
|
keyUsage:
|
|
|
|
description: Purpose for which the public key may be used
|
|
|
|
returned: changed or success
|
|
|
|
type: list
|
|
|
|
sample: [ 'digitalSignature', 'keyAgreement' ]
|
|
|
|
extendedKeyUsage:
|
|
|
|
description: Additional restriction on the public key purposes
|
|
|
|
returned: changed or success
|
|
|
|
type: list
|
|
|
|
sample: [ 'clientAuth' ]
|
2017-04-06 16:09:07 +00:00
|
|
|
'''
|
|
|
|
|
2017-04-06 17:41:18 +00:00
|
|
|
import os
|
2017-04-06 16:09:07 +00:00
|
|
|
|
2017-08-03 11:27:17 +00:00
|
|
|
from ansible.module_utils import crypto as crypto_utils
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible.module_utils._text import to_native
|
|
|
|
|
2017-04-06 16:09:07 +00:00
|
|
|
try:
|
|
|
|
from OpenSSL import crypto
|
|
|
|
except ImportError:
|
|
|
|
pyopenssl_found = False
|
|
|
|
else:
|
|
|
|
pyopenssl_found = True
|
|
|
|
|
|
|
|
|
2017-08-03 11:27:17 +00:00
|
|
|
class CertificateSigningRequestError(crypto_utils.OpenSSLObjectError):
|
2017-04-06 16:09:07 +00:00
|
|
|
pass
|
|
|
|
|
2017-04-06 17:41:18 +00:00
|
|
|
|
2017-08-03 11:27:17 +00:00
|
|
|
class CertificateSigningRequest(crypto_utils.OpenSSLObject):
|
2017-04-06 16:09:07 +00:00
|
|
|
|
|
|
|
def __init__(self, module):
|
2017-08-03 11:27:17 +00:00
|
|
|
super(CertificateSigningRequest, self).__init__(
|
|
|
|
module.params['path'],
|
|
|
|
module.params['state'],
|
|
|
|
module.params['force'],
|
|
|
|
module.check_mode
|
|
|
|
)
|
2017-04-06 16:09:07 +00:00
|
|
|
self.digest = module.params['digest']
|
|
|
|
self.privatekey_path = module.params['privatekey_path']
|
2017-07-19 10:02:29 +00:00
|
|
|
self.privatekey_passphrase = module.params['privatekey_passphrase']
|
2017-04-06 16:09:07 +00:00
|
|
|
self.version = module.params['version']
|
2017-08-03 11:27:17 +00:00
|
|
|
self.subjectAltName = module.params['subjectAltName']
|
2017-08-15 08:29:29 +00:00
|
|
|
self.subjectAltName_critical = module.params['subjectAltName_critical']
|
2017-08-03 11:27:17 +00:00
|
|
|
self.keyUsage = module.params['keyUsage']
|
2017-08-15 08:29:29 +00:00
|
|
|
self.keyUsage_critical = module.params['keyUsage_critical']
|
2017-08-03 11:27:17 +00:00
|
|
|
self.extendedKeyUsage = module.params['extendedKeyUsage']
|
2017-08-15 08:29:29 +00:00
|
|
|
self.extendedKeyUsage_critical = module.params['extendedKeyUsage_critical']
|
2017-04-06 16:09:07 +00:00
|
|
|
self.request = None
|
|
|
|
self.privatekey = None
|
|
|
|
|
|
|
|
self.subject = {
|
|
|
|
'C': module.params['countryName'],
|
|
|
|
'ST': module.params['stateOrProvinceName'],
|
|
|
|
'L': module.params['localityName'],
|
|
|
|
'O': module.params['organizationName'],
|
|
|
|
'OU': module.params['organizationalUnitName'],
|
|
|
|
'CN': module.params['commonName'],
|
|
|
|
'emailAddress': module.params['emailAddress'],
|
|
|
|
}
|
|
|
|
|
2017-08-03 11:27:17 +00:00
|
|
|
if not self.subjectAltName:
|
|
|
|
self.subjectAltName = ['DNS:%s' % self.subject['CN']]
|
2017-04-06 16:09:07 +00:00
|
|
|
|
2017-07-13 13:42:48 +00:00
|
|
|
self.subject = dict((k, v) for k, v in self.subject.items() if v)
|
2017-04-06 16:09:07 +00:00
|
|
|
|
|
|
|
def generate(self, module):
|
|
|
|
'''Generate the certificate signing request.'''
|
|
|
|
|
2017-08-03 11:27:17 +00:00
|
|
|
if not self.check(module, perms_required=False) or self.force:
|
2017-04-06 16:09:07 +00:00
|
|
|
req = crypto.X509Req()
|
|
|
|
req.set_version(self.version)
|
|
|
|
subject = req.get_subject()
|
2017-04-06 17:41:18 +00:00
|
|
|
for (key, value) in self.subject.items():
|
2017-04-06 16:09:07 +00:00
|
|
|
if value is not None:
|
|
|
|
setattr(subject, key, value)
|
|
|
|
|
2017-08-03 11:27:17 +00:00
|
|
|
altnames = ', '.join(self.subjectAltName)
|
2017-08-15 08:29:29 +00:00
|
|
|
extensions = [crypto.X509Extension(b"subjectAltName", self.subjectAltName_critical, altnames.encode('ascii'))]
|
2017-04-06 16:09:07 +00:00
|
|
|
|
2017-08-03 11:27:17 +00:00
|
|
|
if self.keyUsage:
|
|
|
|
usages = ', '.join(self.keyUsage)
|
2017-08-15 08:29:29 +00:00
|
|
|
extensions.append(crypto.X509Extension(b"keyUsage", self.keyUsage_critical, usages.encode('ascii')))
|
2017-08-03 11:27:17 +00:00
|
|
|
|
|
|
|
if self.extendedKeyUsage:
|
|
|
|
usages = ', '.join(self.extendedKeyUsage)
|
2017-08-15 08:29:29 +00:00
|
|
|
extensions.append(crypto.X509Extension(b"extendedKeyUsage", self.extendedKeyUsage_critical, usages.encode('ascii')))
|
2017-08-03 11:27:17 +00:00
|
|
|
|
|
|
|
req.add_extensions(extensions)
|
2017-07-25 11:18:18 +00:00
|
|
|
|
2017-04-06 16:09:07 +00:00
|
|
|
req.set_pubkey(self.privatekey)
|
|
|
|
req.sign(self.privatekey, self.digest)
|
|
|
|
self.request = req
|
|
|
|
|
|
|
|
try:
|
2017-07-13 13:42:48 +00:00
|
|
|
csr_file = open(self.path, 'wb')
|
2017-04-06 16:09:07 +00:00
|
|
|
csr_file.write(crypto.dump_certificate_request(crypto.FILETYPE_PEM, self.request))
|
|
|
|
csr_file.close()
|
2017-07-03 10:07:42 +00:00
|
|
|
except (IOError, OSError) as exc:
|
|
|
|
raise CertificateSigningRequestError(exc)
|
2017-08-03 11:27:17 +00:00
|
|
|
|
|
|
|
self.changed = True
|
2017-04-06 16:09:07 +00:00
|
|
|
|
|
|
|
file_args = module.load_file_common_arguments(module.params)
|
|
|
|
if module.set_fs_attributes_if_different(file_args, False):
|
|
|
|
self.changed = True
|
|
|
|
|
2017-08-03 11:27:17 +00:00
|
|
|
def check(self, module, perms_required=True):
|
|
|
|
"""Ensure the resource is in its desired state."""
|
|
|
|
state_and_perms = super(CertificateSigningRequest, self).check(module, perms_required)
|
2017-04-06 16:09:07 +00:00
|
|
|
|
2017-08-03 11:27:17 +00:00
|
|
|
self.privatekey = crypto_utils.load_privatekey(self.privatekey_path, self.privatekey_passphrase)
|
|
|
|
|
|
|
|
def _check_subject(csr):
|
|
|
|
subject = csr.get_subject()
|
|
|
|
for (key, value) in self.subject.items():
|
|
|
|
if getattr(subject, key, None) != value:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def _check_subjectAltName(extensions):
|
2017-08-15 08:29:29 +00:00
|
|
|
altnames_ext = next((ext for ext in extensions if ext.get_short_name() == b'subjectAltName'), '')
|
|
|
|
altnames = [altname.strip() for altname in str(altnames_ext).split(',')]
|
2017-08-03 11:27:17 +00:00
|
|
|
# apperently openssl returns 'IP address' not 'IP' as specifier when converting the subjectAltName to string
|
|
|
|
# although it won't accept this specifier when generating the CSR. (https://github.com/openssl/openssl/issues/4004)
|
|
|
|
altnames = [name if not name.startswith('IP Address:') else "IP:" + name.split(':', 1)[1] for name in altnames]
|
|
|
|
if self.subjectAltName:
|
2017-08-15 08:29:29 +00:00
|
|
|
if set(altnames) != set(self.subjectAltName) or altnames_ext.get_critical() != self.subjectAltName_critical:
|
2017-08-03 11:27:17 +00:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
if altnames:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2017-08-15 08:29:29 +00:00
|
|
|
def _check_keyUsage_(extensions, extName, expected, critical, long):
|
|
|
|
usages_ext = [ext for ext in extensions if ext.get_short_name() == extName]
|
2017-08-03 11:27:17 +00:00
|
|
|
if (not usages_ext and expected) or (usages_ext and not expected):
|
|
|
|
return False
|
|
|
|
elif not usages_ext and not expected:
|
|
|
|
return True
|
2017-04-06 16:09:07 +00:00
|
|
|
else:
|
2017-08-15 08:29:29 +00:00
|
|
|
current = [usage.strip() for usage in str(usages_ext[0]).split(',')]
|
2017-08-03 11:27:17 +00:00
|
|
|
expected = [long[usage] if usage in long else usage for usage in expected]
|
2017-08-16 12:35:25 +00:00
|
|
|
return set(current) == set(expected) and usages_ext[0].get_critical() == critical
|
2017-08-03 11:27:17 +00:00
|
|
|
|
|
|
|
def _check_keyUsage(extensions):
|
2017-08-15 08:29:29 +00:00
|
|
|
return _check_keyUsage_(extensions, b'keyUsage', self.keyUsage, self.keyUsage_critical, crypto_utils.keyUsageLong)
|
2017-08-03 11:27:17 +00:00
|
|
|
|
|
|
|
def _check_extenededKeyUsage(extensions):
|
2017-08-15 08:29:29 +00:00
|
|
|
return _check_keyUsage_(extensions, b'extendedKeyUsage', self.extendedKeyUsage, self.extendedKeyUsage_critical, crypto_utils.extendedKeyUsageLong)
|
2017-08-03 11:27:17 +00:00
|
|
|
|
|
|
|
def _check_extensions(csr):
|
|
|
|
extensions = csr.get_extensions()
|
|
|
|
return _check_subjectAltName(extensions) and _check_keyUsage(extensions) and _check_extenededKeyUsage(extensions)
|
|
|
|
|
|
|
|
def _check_signature(csr):
|
|
|
|
try:
|
|
|
|
return csr.verify(self.privatekey)
|
|
|
|
except crypto.Error:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if not state_and_perms:
|
|
|
|
return False
|
|
|
|
|
|
|
|
csr = crypto_utils.load_certificate_request(self.path)
|
|
|
|
|
|
|
|
return _check_subject(csr) and _check_extensions(csr) and _check_signature(csr)
|
2017-04-06 16:09:07 +00:00
|
|
|
|
|
|
|
def dump(self):
|
2017-06-12 06:55:19 +00:00
|
|
|
'''Serialize the object into a dictionary.'''
|
2017-04-06 16:09:07 +00:00
|
|
|
|
|
|
|
result = {
|
2017-08-03 11:27:17 +00:00
|
|
|
'privatekey': self.privatekey_path,
|
|
|
|
'filename': self.path,
|
2017-04-06 16:09:07 +00:00
|
|
|
'subject': self.subject,
|
|
|
|
'subjectAltName': self.subjectAltName,
|
2017-08-03 11:27:17 +00:00
|
|
|
'keyUsage': self.keyUsage,
|
|
|
|
'extendedKeyUsage': self.extendedKeyUsage,
|
2017-04-06 16:09:07 +00:00
|
|
|
'changed': self.changed
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
2017-04-06 17:41:18 +00:00
|
|
|
argument_spec=dict(
|
2017-04-06 16:09:07 +00:00
|
|
|
state=dict(default='present', choices=['present', 'absent'], type='str'),
|
|
|
|
digest=dict(default='sha256', type='str'),
|
|
|
|
privatekey_path=dict(require=True, type='path'),
|
2017-07-19 10:02:29 +00:00
|
|
|
privatekey_passphrase=dict(type='str', no_log=True),
|
2017-04-06 16:09:07 +00:00
|
|
|
version=dict(default='3', type='int'),
|
|
|
|
force=dict(default=False, type='bool'),
|
|
|
|
path=dict(required=True, type='path'),
|
|
|
|
countryName=dict(aliases=['C'], type='str'),
|
|
|
|
stateOrProvinceName=dict(aliases=['ST'], type='str'),
|
|
|
|
localityName=dict(aliases=['L'], type='str'),
|
|
|
|
organizationName=dict(aliases=['O'], type='str'),
|
|
|
|
organizationalUnitName=dict(aliases=['OU'], type='str'),
|
|
|
|
commonName=dict(aliases=['CN'], type='str'),
|
|
|
|
emailAddress=dict(aliases=['E'], type='str'),
|
2017-08-03 11:27:17 +00:00
|
|
|
subjectAltName=dict(type='list'),
|
2017-08-15 08:29:29 +00:00
|
|
|
subjectAltName_critical=dict(default=False, type='bool'),
|
2017-08-03 11:27:17 +00:00
|
|
|
keyUsage=dict(type='list'),
|
2017-08-15 08:29:29 +00:00
|
|
|
keyUsage_critical=dict(default=False, type='bool'),
|
2017-08-03 11:27:17 +00:00
|
|
|
extendedKeyUsage=dict(aliases=['extKeyUsage'], type='list'),
|
2017-08-15 08:29:29 +00:00
|
|
|
extendedKeyUsage_critical=dict(default=False, aliases=['extKeyUsage_critical'], type='bool'),
|
2017-04-06 16:09:07 +00:00
|
|
|
),
|
2017-04-06 17:41:18 +00:00
|
|
|
add_file_common_args=True,
|
|
|
|
supports_check_mode=True,
|
2017-04-06 16:09:07 +00:00
|
|
|
required_one_of=[['commonName', 'subjectAltName']],
|
|
|
|
)
|
|
|
|
|
2017-07-13 13:42:48 +00:00
|
|
|
if not pyopenssl_found:
|
|
|
|
module.fail_json(msg='the python pyOpenSSL module is required')
|
|
|
|
|
2017-04-06 16:09:07 +00:00
|
|
|
base_dir = os.path.dirname(module.params['path'])
|
|
|
|
if not os.path.isdir(base_dir):
|
2017-08-03 11:27:17 +00:00
|
|
|
module.fail_json(name=base_dir, msg='The directory %s does not exist or the file is not a directory' % base_dir)
|
2017-04-06 16:09:07 +00:00
|
|
|
|
|
|
|
csr = CertificateSigningRequest(module)
|
|
|
|
|
|
|
|
if module.params['state'] == 'present':
|
|
|
|
|
|
|
|
if module.check_mode:
|
|
|
|
result = csr.dump()
|
2017-08-03 11:27:17 +00:00
|
|
|
result['changed'] = module.params['force'] or not csr.check(module)
|
2017-04-06 16:09:07 +00:00
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
try:
|
|
|
|
csr.generate(module)
|
2017-08-03 11:27:17 +00:00
|
|
|
except (CertificateSigningRequestError, crypto_utils.OpenSSLObjectError) as exc:
|
2017-07-03 10:07:42 +00:00
|
|
|
module.fail_json(msg=to_native(exc))
|
2017-04-06 16:09:07 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
if module.check_mode:
|
|
|
|
result = csr.dump()
|
2017-08-03 11:27:17 +00:00
|
|
|
result['changed'] = os.path.exists(module.params['path'])
|
2017-04-06 16:09:07 +00:00
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
try:
|
|
|
|
csr.remove()
|
2017-08-03 11:27:17 +00:00
|
|
|
except (CertificateSigningRequestError, crypto_utils.OpenSSLObjectError) as exc:
|
2017-07-03 10:07:42 +00:00
|
|
|
module.fail_json(msg=to_native(exc))
|
2017-04-06 16:09:07 +00:00
|
|
|
|
|
|
|
result = csr.dump()
|
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|