Skip to content

Commit

Permalink
Move generic I/O functions to plugins/module_utils/io.py.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein committed Apr 13, 2020
1 parent 6b3606e commit 1bdfbaa
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 88 deletions.
74 changes: 0 additions & 74 deletions plugins/module_utils/crypto/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@
__metaclass__ = type


import errno
import os
import tempfile

from distutils.version import LooseVersion

try:
Expand Down Expand Up @@ -114,73 +110,3 @@ class OpenSSLObjectError(Exception):

class OpenSSLBadPassphraseError(OpenSSLObjectError):
pass


def load_file_if_exists(path, module=None, ignore_errors=False):
try:
with open(path, 'rb') as f:
return f.read()
except EnvironmentError as exc:
if exc.errno == errno.ENOENT:
return None
if ignore_errors:
return None
if module is None:
raise
module.fail_json('Error while loading {0} - {1}'.format(path, str(exc)))
except Exception as exc:
if ignore_errors:
return None
if module is None:
raise
module.fail_json('Error while loading {0} - {1}'.format(path, str(exc)))


def write_file(module, content, default_mode=None, path=None):
'''
Writes content into destination file as securely as possible.
Uses file arguments from module.
'''
# Find out parameters for file
try:
file_args = module.load_file_common_arguments(module.params, path=path)
except TypeError:
# The path argument is only supported in Ansible 2.10+. Fall back to
# pre-2.10 behavior of module_utils/crypto.py for older Ansible versions.
file_args = module.load_file_common_arguments(module.params)
if path is not None:
file_args['path'] = path
if file_args['mode'] is None:
file_args['mode'] = default_mode
# Create tempfile name
tmp_fd, tmp_name = tempfile.mkstemp(prefix=b'.ansible_tmp')
try:
os.close(tmp_fd)
except Exception:
pass
module.add_cleanup_file(tmp_name) # if we fail, let Ansible try to remove the file
try:
try:
# Create tempfile
file = os.open(tmp_name, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
os.write(file, content)
os.close(file)
except Exception as e:
try:
os.remove(tmp_name)
except Exception:
pass
module.fail_json(msg='Error while writing result into temporary file: {0}'.format(e))
# Update destination to wanted permissions
if os.path.exists(file_args['path']):
module.set_fs_attributes_if_different(file_args, False)
# Move tempfile to final destination
module.atomic_move(tmp_name, file_args['path'])
# Try to update permissions again
module.set_fs_attributes_if_different(file_args, False)
except Exception as e:
try:
os.remove(tmp_name)
except Exception:
pass
module.fail_json(msg='Error while writing result: {0}'.format(e))
101 changes: 101 additions & 0 deletions plugins/module_utils/io.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# -*- coding: utf-8 -*-
#
# (c) 2016, Yanis Guenane <[email protected]>
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.

from __future__ import absolute_import, division, print_function
__metaclass__ = type


import errno
import os
import tempfile


def load_file_if_exists(path, module=None, ignore_errors=False):
'''
Load the file as a bytes string. If the file does not exist, ``None`` is returned.
If ``ignore_errors`` is ``True``, will ignore errors. Otherwise, errors are
raised as exceptions if ``module`` is not specified, and result in ``module.fail_json``
being called when ``module`` is specified.
'''
try:
with open(path, 'rb') as f:
return f.read()
except EnvironmentError as exc:
if exc.errno == errno.ENOENT:
return None
if ignore_errors:
return None
if module is None:
raise
module.fail_json('Error while loading {0} - {1}'.format(path, str(exc)))
except Exception as exc:
if ignore_errors:
return None
if module is None:
raise
module.fail_json('Error while loading {0} - {1}'.format(path, str(exc)))


def write_file(module, content, default_mode=None, path=None):
'''
Writes content into destination file as securely as possible.
Uses file arguments from module.
'''
# Find out parameters for file
try:
file_args = module.load_file_common_arguments(module.params, path=path)
except TypeError:
# The path argument is only supported in Ansible 2.10+. Fall back to
# pre-2.10 behavior of module_utils/crypto.py for older Ansible versions.
file_args = module.load_file_common_arguments(module.params)
if path is not None:
file_args['path'] = path
if file_args['mode'] is None:
file_args['mode'] = default_mode
# Create tempfile name
tmp_fd, tmp_name = tempfile.mkstemp(prefix=b'.ansible_tmp')
try:
os.close(tmp_fd)
except Exception:
pass
module.add_cleanup_file(tmp_name) # if we fail, let Ansible try to remove the file
try:
try:
# Create tempfile
file = os.open(tmp_name, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
os.write(file, content)
os.close(file)
except Exception as e:
try:
os.remove(tmp_name)
except Exception:
pass
module.fail_json(msg='Error while writing result into temporary file: {0}'.format(e))
# Update destination to wanted permissions
if os.path.exists(file_args['path']):
module.set_fs_attributes_if_different(file_args, False)
# Move tempfile to final destination
module.atomic_move(tmp_name, file_args['path'])
# Try to update permissions again
module.set_fs_attributes_if_different(file_args, False)
except Exception as e:
try:
os.remove(tmp_name)
except Exception:
pass
module.fail_json(msg='Error while writing result: {0}'.format(e))
4 changes: 2 additions & 2 deletions plugins/modules/ecs_certificate.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,8 @@
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils._text import to_native, to_bytes

from ansible_collections.community.crypto.plugins.module_utils.crypto.basic import (
write_file
from ansible_collections.community.crypto.plugins.module_utils.io import (
write_file,
)

from ansible_collections.community.crypto.plugins.module_utils.crypto.support import (
Expand Down
7 changes: 5 additions & 2 deletions plugins/modules/openssl_csr.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,11 +428,14 @@

from ansible_collections.community.crypto.plugins.module_utils.compat import ipaddress as compat_ipaddress

from ansible_collections.community.crypto.plugins.module_utils.io import (
load_file_if_exists,
write_file,
)

from ansible_collections.community.crypto.plugins.module_utils.crypto.basic import (
OpenSSLObjectError,
OpenSSLBadPassphraseError,
write_file,
load_file_if_exists,
)

from ansible_collections.community.crypto.plugins.module_utils.crypto.support import (
Expand Down
2 changes: 1 addition & 1 deletion plugins/modules/openssl_dhparam.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils._text import to_native

from ansible_collections.community.crypto.plugins.module_utils.crypto.basic import (
from ansible_collections.community.crypto.plugins.module_utils.io import (
load_file_if_exists,
write_file,
)
Expand Down
7 changes: 5 additions & 2 deletions plugins/modules/openssl_pkcs12.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,14 @@
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils._text import to_bytes, to_native

from ansible_collections.community.crypto.plugins.module_utils.io import (
load_file_if_exists,
write_file,
)

from ansible_collections.community.crypto.plugins.module_utils.crypto.basic import (
OpenSSLObjectError,
OpenSSLBadPassphraseError,
load_file_if_exists,
write_file,
)

from ansible_collections.community.crypto.plugins.module_utils.crypto.support import (
Expand Down
7 changes: 5 additions & 2 deletions plugins/modules/openssl_privatekey.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils._text import to_native, to_bytes

from ansible_collections.community.crypto.plugins.module_utils.io import (
load_file_if_exists,
write_file,
)

from ansible_collections.community.crypto.plugins.module_utils.crypto.basic import (
CRYPTOGRAPHY_HAS_X25519,
CRYPTOGRAPHY_HAS_X25519_FULL,
Expand All @@ -290,8 +295,6 @@
CRYPTOGRAPHY_HAS_ED448,
OpenSSLObjectError,
OpenSSLBadPassphraseError,
write_file,
load_file_if_exists,
)

from ansible_collections.community.crypto.plugins.module_utils.crypto.support import (
Expand Down
7 changes: 5 additions & 2 deletions plugins/modules/openssl_publickey.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,14 @@
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils._text import to_native

from ansible_collections.community.crypto.plugins.module_utils.io import (
load_file_if_exists,
write_file,
)

from ansible_collections.community.crypto.plugins.module_utils.crypto.basic import (
OpenSSLObjectError,
OpenSSLBadPassphraseError,
write_file,
load_file_if_exists,
)

from ansible_collections.community.crypto.plugins.module_utils.crypto.support import (
Expand Down
7 changes: 5 additions & 2 deletions plugins/modules/x509_certificate.py
Original file line number Diff line number Diff line change
Expand Up @@ -864,11 +864,14 @@
from ansible_collections.community.crypto.plugins.module_utils.compat import ipaddress as compat_ipaddress
from ansible_collections.community.crypto.plugins.module_utils.ecs.api import ECSClient, RestOperationException, SessionConfigurationException

from ansible_collections.community.crypto.plugins.module_utils.io import (
load_file_if_exists,
write_file,
)

from ansible_collections.community.crypto.plugins.module_utils.crypto.basic import (
OpenSSLObjectError,
OpenSSLBadPassphraseError,
write_file,
load_file_if_exists,
)

from ansible_collections.community.crypto.plugins.module_utils.crypto.support import (
Expand Down
5 changes: 4 additions & 1 deletion plugins/modules/x509_crl.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,13 @@
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils._text import to_native, to_text

from ansible_collections.community.crypto.plugins.module_utils.io import (
write_file,
)

from ansible_collections.community.crypto.plugins.module_utils.crypto.basic import (
OpenSSLObjectError,
OpenSSLBadPassphraseError,
write_file,
)

from ansible_collections.community.crypto.plugins.module_utils.crypto.support import (
Expand Down

0 comments on commit 1bdfbaa

Please sign in to comment.