2018-01-26 08:08:29 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-02-15 10:46:44 +00:00
|
|
|
# Copyright: (c) 2017, Thom Wiggers <ansible@thomwiggers.nl>
|
2018-01-26 08:08:29 +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
|
|
|
|
|
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
|
|
|
'status': ['preview'],
|
|
|
|
'supported_by': 'community'}
|
|
|
|
|
2019-02-15 10:46:44 +00:00
|
|
|
DOCUMENTATION = r'''
|
2018-01-26 08:08:29 +00:00
|
|
|
---
|
|
|
|
module: openssl_dhparam
|
|
|
|
version_added: "2.5"
|
|
|
|
short_description: Generate OpenSSL Diffie-Hellman Parameters
|
|
|
|
description:
|
2019-02-15 10:46:44 +00:00
|
|
|
- This module allows one to (re)generate OpenSSL DH-params.
|
|
|
|
- This module uses file common arguments to specify generated file permissions.
|
2018-01-26 08:08:29 +00:00
|
|
|
requirements:
|
|
|
|
- OpenSSL
|
2019-02-15 10:46:44 +00:00
|
|
|
author:
|
|
|
|
- Thom Wiggers (@thomwiggers)
|
2018-01-26 08:08:29 +00:00
|
|
|
options:
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Whether the parameters should exist or not,
|
|
|
|
taking action if the state is different from what is stated.
|
2019-02-15 10:46:44 +00:00
|
|
|
type: str
|
|
|
|
choices: [ absent, present ]
|
|
|
|
default: present
|
2018-01-26 08:08:29 +00:00
|
|
|
size:
|
|
|
|
description:
|
2019-02-15 10:46:44 +00:00
|
|
|
- Size (in bits) of the generated DH-params.
|
|
|
|
type: int
|
|
|
|
default: 4096
|
2018-01-26 08:08:29 +00:00
|
|
|
force:
|
|
|
|
description:
|
2019-02-15 10:46:44 +00:00
|
|
|
- Should the parameters be regenerated even it it already exists.
|
|
|
|
type: bool
|
|
|
|
default: no
|
2018-01-26 08:08:29 +00:00
|
|
|
path:
|
|
|
|
description:
|
|
|
|
- Name of the file in which the generated parameters will be saved.
|
2019-02-15 10:46:44 +00:00
|
|
|
type: path
|
|
|
|
required: true
|
|
|
|
extends_documentation_fragment:
|
|
|
|
- files
|
|
|
|
seealso:
|
|
|
|
- module: openssl_certificate
|
|
|
|
- module: openssl_csr
|
|
|
|
- module: openssl_pkcs12
|
|
|
|
- module: openssl_privatekey
|
|
|
|
- module: openssl_publickey
|
2018-01-26 08:08:29 +00:00
|
|
|
'''
|
|
|
|
|
2019-02-15 10:46:44 +00:00
|
|
|
EXAMPLES = r'''
|
|
|
|
- name: Generate Diffie-Hellman parameters with the default size (4096 bits)
|
|
|
|
openssl_dhparam:
|
2018-01-26 08:08:29 +00:00
|
|
|
path: /etc/ssl/dhparams.pem
|
|
|
|
|
2019-02-15 10:46:44 +00:00
|
|
|
- name: Generate DH Parameters with a different size (2048 bits)
|
|
|
|
openssl_dhparam:
|
2018-01-26 08:08:29 +00:00
|
|
|
path: /etc/ssl/dhparams.pem
|
|
|
|
size: 2048
|
|
|
|
|
2019-02-15 10:46:44 +00:00
|
|
|
- name: Force regenerate an DH parameters if they already exist
|
|
|
|
openssl_dhparam:
|
2018-01-26 08:08:29 +00:00
|
|
|
path: /etc/ssl/dhparams.pem
|
2019-02-15 10:46:44 +00:00
|
|
|
force: yes
|
2018-01-26 08:08:29 +00:00
|
|
|
'''
|
|
|
|
|
2019-02-15 10:46:44 +00:00
|
|
|
RETURN = r'''
|
2018-01-26 08:08:29 +00:00
|
|
|
size:
|
2019-02-15 10:46:44 +00:00
|
|
|
description: Size (in bits) of the Diffie-Hellman parameters.
|
2018-01-26 08:08:29 +00:00
|
|
|
returned: changed or success
|
|
|
|
type: int
|
|
|
|
sample: 4096
|
|
|
|
filename:
|
2019-02-15 10:46:44 +00:00
|
|
|
description: Path to the generated Diffie-Hellman parameters.
|
2018-01-26 08:08:29 +00:00
|
|
|
returned: changed or success
|
2018-12-18 21:25:30 +00:00
|
|
|
type: str
|
2018-01-26 08:08:29 +00:00
|
|
|
sample: /etc/ssl/dhparams.pem
|
|
|
|
'''
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible.module_utils._text import to_native
|
|
|
|
|
|
|
|
|
|
|
|
class DHParameterError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class DHParameter(object):
|
|
|
|
|
|
|
|
def __init__(self, module):
|
|
|
|
self.state = module.params['state']
|
|
|
|
self.path = module.params['path']
|
2019-02-15 10:46:44 +00:00
|
|
|
self.size = module.params['size']
|
2018-01-26 08:08:29 +00:00
|
|
|
self.force = module.params['force']
|
|
|
|
self.changed = False
|
|
|
|
self.openssl_bin = module.get_bin_path('openssl', True)
|
|
|
|
|
|
|
|
def generate(self, module):
|
|
|
|
"""Generate a keypair."""
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
# ony generate when necessary
|
|
|
|
if self.force or not self._check_params_valid(module):
|
|
|
|
# create a tempfile
|
|
|
|
fd, tmpsrc = tempfile.mkstemp()
|
|
|
|
os.close(fd)
|
|
|
|
module.add_cleanup_file(tmpsrc) # Ansible will delete the file on exit
|
|
|
|
# openssl dhparam -out <path> <bits>
|
|
|
|
command = [self.openssl_bin, 'dhparam', '-out', tmpsrc, str(self.size)]
|
|
|
|
rc, dummy, err = module.run_command(command, check_rc=False)
|
|
|
|
if rc != 0:
|
|
|
|
raise DHParameterError(to_native(err))
|
|
|
|
try:
|
|
|
|
module.atomic_move(tmpsrc, self.path)
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg="Failed to write to file %s: %s" % (self.path, str(e)))
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
# fix permissions (checking force not necessary as done above)
|
|
|
|
if not self._check_fs_attributes(module):
|
|
|
|
# Fix done implicitly by
|
|
|
|
# AnsibleModule.set_fs_attributes_if_different
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
self.changed = changed
|
|
|
|
|
|
|
|
def check(self, module):
|
|
|
|
"""Ensure the resource is in its desired state."""
|
|
|
|
if self.force:
|
|
|
|
return False
|
|
|
|
return self._check_params_valid(module) and self._check_fs_attributes(module)
|
|
|
|
|
|
|
|
def _check_params_valid(self, module):
|
|
|
|
"""Check if the params are in the correct state"""
|
|
|
|
command = [self.openssl_bin, 'dhparam', '-check', '-text', '-noout', '-in', self.path]
|
|
|
|
rc, out, err = module.run_command(command, check_rc=False)
|
|
|
|
result = to_native(out)
|
|
|
|
if rc != 0:
|
|
|
|
# If the call failed the file probably doesn't exist or is
|
|
|
|
# unreadable
|
|
|
|
return False
|
|
|
|
# output contains "(xxxx bit)"
|
|
|
|
match = re.search(r"Parameters:\s+\((\d+) bit\).*", result)
|
|
|
|
if not match:
|
|
|
|
return False # No "xxxx bit" in output
|
2019-02-15 10:46:44 +00:00
|
|
|
|
|
|
|
bits = int(match.group(1))
|
2018-01-26 08:08:29 +00:00
|
|
|
|
|
|
|
# if output contains "WARNING" we've got a problem
|
|
|
|
if "WARNING" in result or "WARNING" in to_native(err):
|
|
|
|
return False
|
|
|
|
|
|
|
|
return bits == self.size
|
|
|
|
|
|
|
|
def _check_fs_attributes(self, module):
|
|
|
|
"""Checks (and changes if not in check mode!) fs attributes"""
|
|
|
|
file_args = module.load_file_common_arguments(module.params)
|
|
|
|
attrs_changed = module.set_fs_attributes_if_different(file_args, False)
|
|
|
|
|
|
|
|
return not attrs_changed
|
|
|
|
|
|
|
|
def dump(self):
|
|
|
|
"""Serialize the object into a dictionary."""
|
|
|
|
|
|
|
|
result = {
|
|
|
|
'size': self.size,
|
|
|
|
'filename': self.path,
|
|
|
|
'changed': self.changed,
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
"""Main function"""
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
2019-02-15 10:46:44 +00:00
|
|
|
state=dict(type='str', default='present', choices=['absent', 'present']),
|
|
|
|
size=dict(type='int', default=4096),
|
|
|
|
force=dict(type='bool', default=False),
|
|
|
|
path=dict(type='path', required=True),
|
2018-01-26 08:08:29 +00:00
|
|
|
),
|
|
|
|
supports_check_mode=True,
|
|
|
|
add_file_common_args=True,
|
|
|
|
)
|
|
|
|
|
2019-01-03 11:34:46 +00:00
|
|
|
base_dir = os.path.dirname(module.params['path']) or '.'
|
2018-01-26 08:08:29 +00:00
|
|
|
if not os.path.isdir(base_dir):
|
|
|
|
module.fail_json(
|
|
|
|
name=base_dir,
|
2019-02-15 10:46:44 +00:00
|
|
|
msg="The directory '%s' does not exist or the file is not a directory" % base_dir
|
2018-01-26 08:08:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
dhparam = DHParameter(module)
|
|
|
|
|
|
|
|
if dhparam.state == 'present':
|
|
|
|
|
|
|
|
if module.check_mode:
|
|
|
|
result = dhparam.dump()
|
|
|
|
result['changed'] = module.params['force'] or not dhparam.check(module)
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
try:
|
|
|
|
dhparam.generate(module)
|
|
|
|
except DHParameterError as exc:
|
|
|
|
module.fail_json(msg=to_native(exc))
|
|
|
|
else:
|
|
|
|
|
|
|
|
if module.check_mode:
|
|
|
|
result = dhparam.dump()
|
|
|
|
result['changed'] = os.path.exists(module.params['path'])
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.remove(module.params['path'])
|
|
|
|
except OSError as exc:
|
|
|
|
module.fail_json(msg=to_native(exc))
|
|
|
|
|
|
|
|
result = dhparam.dump()
|
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|