2020-03-09 10:11:07 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright (c) 2018, Ansible Project
|
|
|
|
# Copyright (c) 2018, Abhijeet Kasurde <akasurde@redhat.com>
|
|
|
|
#
|
2022-08-05 12:28:29 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2020-06-24 21:50:36 +02:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
import os
|
|
|
|
|
2020-03-31 10:42:38 +02:00
|
|
|
from ansible_collections.community.general.tests.unit.plugins.modules.utils import ModuleTestCase, set_module_args
|
2020-03-09 10:11:07 +01:00
|
|
|
from ansible_collections.community.general.tests.unit.compat.mock import patch
|
|
|
|
from ansible_collections.community.general.tests.unit.compat.mock import Mock
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2022-11-02 21:42:29 +01:00
|
|
|
from ansible_collections.community.general.plugins.modules.java_keystore import JavaKeystore
|
2021-06-27 14:56:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
module_argument_spec = dict(
|
|
|
|
name=dict(type='str', required=True),
|
|
|
|
dest=dict(type='path', required=True),
|
|
|
|
certificate=dict(type='str', no_log=True),
|
|
|
|
certificate_path=dict(type='path'),
|
|
|
|
private_key=dict(type='str', no_log=True),
|
|
|
|
private_key_path=dict(type='path', no_log=False),
|
|
|
|
private_key_passphrase=dict(type='str', no_log=True),
|
|
|
|
password=dict(type='str', required=True, no_log=True),
|
|
|
|
ssl_backend=dict(type='str', default='openssl', choices=['openssl', 'cryptography']),
|
|
|
|
keystore_type=dict(type='str', choices=['jks', 'pkcs12']),
|
|
|
|
force=dict(type='bool', default=False),
|
|
|
|
)
|
|
|
|
module_supports_check_mode = True
|
|
|
|
module_choose_between = (['certificate', 'certificate_path'],
|
|
|
|
['private_key', 'private_key_path'])
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestCreateJavaKeystore(ModuleTestCase):
|
|
|
|
"""Test the creation of a Java keystore."""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""Setup."""
|
|
|
|
super(TestCreateJavaKeystore, self).setUp()
|
|
|
|
|
|
|
|
orig_exists = os.path.exists
|
2022-11-02 21:42:29 +01:00
|
|
|
self.mock_create_file = patch('ansible_collections.community.general.plugins.modules.java_keystore.create_file')
|
|
|
|
self.mock_create_path = patch('ansible_collections.community.general.plugins.modules.java_keystore.create_path')
|
|
|
|
self.mock_current_type = patch('ansible_collections.community.general.plugins.modules.java_keystore.JavaKeystore.current_type')
|
2021-05-14 22:31:44 +02:00
|
|
|
self.mock_run_command = patch('ansible.module_utils.basic.AnsibleModule.run_command')
|
|
|
|
self.mock_get_bin_path = patch('ansible.module_utils.basic.AnsibleModule.get_bin_path')
|
2021-06-27 14:56:43 +02:00
|
|
|
self.mock_preserved_copy = patch('ansible.module_utils.basic.AnsibleModule.preserved_copy')
|
|
|
|
self.mock_atomic_move = patch('ansible.module_utils.basic.AnsibleModule.atomic_move')
|
2020-03-09 10:11:07 +01:00
|
|
|
self.mock_os_path_exists = patch('os.path.exists',
|
|
|
|
side_effect=lambda path: True if path == '/path/to/keystore.jks' else orig_exists(path))
|
|
|
|
self.mock_selinux_context = patch('ansible.module_utils.basic.AnsibleModule.selinux_context',
|
|
|
|
side_effect=lambda path: ['unconfined_u', 'object_r', 'user_home_t', 's0'])
|
|
|
|
self.mock_is_special_selinux_path = patch('ansible.module_utils.basic.AnsibleModule.is_special_selinux_path',
|
|
|
|
side_effect=lambda path: (False, None))
|
2021-05-14 22:31:44 +02:00
|
|
|
self.run_command = self.mock_run_command.start()
|
|
|
|
self.get_bin_path = self.mock_get_bin_path.start()
|
2021-06-27 14:56:43 +02:00
|
|
|
self.preserved_copy = self.mock_preserved_copy.start()
|
|
|
|
self.atomic_move = self.mock_atomic_move.start()
|
2020-03-09 10:11:07 +01:00
|
|
|
self.create_file = self.mock_create_file.start()
|
2021-04-05 14:40:36 +02:00
|
|
|
self.create_path = self.mock_create_path.start()
|
2021-06-27 14:56:43 +02:00
|
|
|
self.current_type = self.mock_current_type.start()
|
2020-03-09 10:11:07 +01:00
|
|
|
self.selinux_context = self.mock_selinux_context.start()
|
|
|
|
self.is_special_selinux_path = self.mock_is_special_selinux_path.start()
|
|
|
|
self.os_path_exists = self.mock_os_path_exists.start()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
"""Teardown."""
|
|
|
|
super(TestCreateJavaKeystore, self).tearDown()
|
|
|
|
self.mock_create_file.stop()
|
2021-04-05 14:40:36 +02:00
|
|
|
self.mock_create_path.stop()
|
2021-06-27 14:56:43 +02:00
|
|
|
self.mock_current_type.stop()
|
2021-05-14 22:31:44 +02:00
|
|
|
self.mock_run_command.stop()
|
|
|
|
self.mock_get_bin_path.stop()
|
2021-06-27 14:56:43 +02:00
|
|
|
self.mock_preserved_copy.stop()
|
|
|
|
self.mock_atomic_move.stop()
|
2020-03-09 10:11:07 +01:00
|
|
|
self.mock_selinux_context.stop()
|
|
|
|
self.mock_is_special_selinux_path.stop()
|
|
|
|
self.mock_os_path_exists.stop()
|
|
|
|
|
|
|
|
def test_create_jks_success(self):
|
|
|
|
set_module_args(dict(
|
|
|
|
certificate='cert-foo',
|
|
|
|
private_key='private-foo',
|
|
|
|
dest='/path/to/keystore.jks',
|
2021-05-14 22:31:44 +02:00
|
|
|
name='test',
|
2020-03-09 10:11:07 +01:00
|
|
|
password='changeit'
|
|
|
|
))
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
2021-06-27 14:56:43 +02:00
|
|
|
argument_spec=module_argument_spec,
|
|
|
|
supports_check_mode=module_supports_check_mode,
|
|
|
|
mutually_exclusive=module_choose_between,
|
|
|
|
required_one_of=module_choose_between
|
2020-03-09 10:11:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
with patch('os.remove', return_value=True):
|
2021-04-05 14:40:36 +02:00
|
|
|
self.create_path.side_effect = ['/tmp/tmpgrzm2ah7']
|
2021-05-14 22:31:44 +02:00
|
|
|
self.create_file.side_effect = ['/tmp/etacifitrec', '/tmp/yek_etavirp', '']
|
|
|
|
self.run_command.side_effect = [(0, '', ''), (0, '', '')]
|
|
|
|
self.get_bin_path.side_effect = ['keytool', 'openssl', '']
|
|
|
|
jks = JavaKeystore(module)
|
|
|
|
assert jks.create() == {
|
|
|
|
'changed': True,
|
|
|
|
'cmd': ["keytool", "-importkeystore",
|
|
|
|
"-destkeystore", "/path/to/keystore.jks",
|
|
|
|
"-srckeystore", "/tmp/tmpgrzm2ah7", "-srcstoretype", "pkcs12", "-alias", "test",
|
2021-05-17 20:05:24 +02:00
|
|
|
"-noprompt"],
|
2021-05-14 22:31:44 +02:00
|
|
|
'msg': '',
|
|
|
|
'rc': 0
|
|
|
|
}
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2020-05-25 10:09:18 +02:00
|
|
|
def test_create_jks_keypass_fail_export_pkcs12(self):
|
|
|
|
set_module_args(dict(
|
|
|
|
certificate='cert-foo',
|
|
|
|
private_key='private-foo',
|
|
|
|
private_key_passphrase='passphrase-foo',
|
|
|
|
dest='/path/to/keystore.jks',
|
2021-05-14 22:31:44 +02:00
|
|
|
name='test',
|
2020-05-25 10:09:18 +02:00
|
|
|
password='changeit'
|
|
|
|
))
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
2021-06-27 14:56:43 +02:00
|
|
|
argument_spec=module_argument_spec,
|
|
|
|
supports_check_mode=module_supports_check_mode,
|
|
|
|
mutually_exclusive=module_choose_between,
|
|
|
|
required_one_of=module_choose_between
|
2020-05-25 10:09:18 +02:00
|
|
|
)
|
|
|
|
|
2021-05-14 22:31:44 +02:00
|
|
|
module.exit_json = Mock()
|
2020-05-25 10:09:18 +02:00
|
|
|
module.fail_json = Mock()
|
|
|
|
|
|
|
|
with patch('os.remove', return_value=True):
|
2021-04-05 14:40:36 +02:00
|
|
|
self.create_path.side_effect = ['/tmp/tmp1cyp12xa']
|
2021-05-14 22:31:44 +02:00
|
|
|
self.create_file.side_effect = ['/tmp/tmpvalcrt32', '/tmp/tmpwh4key0c', '']
|
2021-06-27 14:56:43 +02:00
|
|
|
self.run_command.side_effect = [(1, '', 'Oops'), (0, '', '')]
|
2021-05-14 22:31:44 +02:00
|
|
|
self.get_bin_path.side_effect = ['keytool', 'openssl', '']
|
|
|
|
jks = JavaKeystore(module)
|
|
|
|
jks.create()
|
2020-05-25 10:09:18 +02:00
|
|
|
module.fail_json.assert_called_once_with(
|
|
|
|
cmd=["openssl", "pkcs12", "-export", "-name", "test",
|
2021-04-05 14:40:36 +02:00
|
|
|
"-in", "/tmp/tmpvalcrt32",
|
|
|
|
"-inkey", "/tmp/tmpwh4key0c",
|
|
|
|
"-out", "/tmp/tmp1cyp12xa",
|
2020-05-25 10:09:18 +02:00
|
|
|
"-passout", "stdin",
|
|
|
|
"-passin", "stdin"],
|
|
|
|
msg='',
|
2021-06-27 14:56:43 +02:00
|
|
|
err='Oops',
|
2020-05-25 10:09:18 +02:00
|
|
|
rc=1
|
|
|
|
)
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
def test_create_jks_fail_export_pkcs12(self):
|
|
|
|
set_module_args(dict(
|
|
|
|
certificate='cert-foo',
|
|
|
|
private_key='private-foo',
|
|
|
|
dest='/path/to/keystore.jks',
|
2021-05-14 22:31:44 +02:00
|
|
|
name='test',
|
2020-03-09 10:11:07 +01:00
|
|
|
password='changeit'
|
|
|
|
))
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
2021-06-27 14:56:43 +02:00
|
|
|
argument_spec=module_argument_spec,
|
|
|
|
supports_check_mode=module_supports_check_mode,
|
|
|
|
mutually_exclusive=module_choose_between,
|
|
|
|
required_one_of=module_choose_between
|
2020-03-09 10:11:07 +01:00
|
|
|
)
|
|
|
|
|
2021-05-14 22:31:44 +02:00
|
|
|
module.exit_json = Mock()
|
2020-03-09 10:11:07 +01:00
|
|
|
module.fail_json = Mock()
|
|
|
|
|
|
|
|
with patch('os.remove', return_value=True):
|
2021-04-05 14:40:36 +02:00
|
|
|
self.create_path.side_effect = ['/tmp/tmp1cyp12xa']
|
2021-05-14 22:31:44 +02:00
|
|
|
self.create_file.side_effect = ['/tmp/tmpvalcrt32', '/tmp/tmpwh4key0c', '']
|
2021-06-27 14:56:43 +02:00
|
|
|
self.run_command.side_effect = [(1, '', 'Oops'), (0, '', '')]
|
2021-05-14 22:31:44 +02:00
|
|
|
self.get_bin_path.side_effect = ['keytool', 'openssl', '']
|
|
|
|
jks = JavaKeystore(module)
|
|
|
|
jks.create()
|
2020-03-09 10:11:07 +01:00
|
|
|
module.fail_json.assert_called_once_with(
|
2020-05-25 10:09:18 +02:00
|
|
|
cmd=["openssl", "pkcs12", "-export", "-name", "test",
|
2021-04-05 14:40:36 +02:00
|
|
|
"-in", "/tmp/tmpvalcrt32",
|
|
|
|
"-inkey", "/tmp/tmpwh4key0c",
|
|
|
|
"-out", "/tmp/tmp1cyp12xa",
|
2020-05-25 10:09:18 +02:00
|
|
|
"-passout", "stdin"],
|
2020-03-09 10:11:07 +01:00
|
|
|
msg='',
|
2021-06-27 14:56:43 +02:00
|
|
|
err='Oops',
|
2020-03-09 10:11:07 +01:00
|
|
|
rc=1
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_create_jks_fail_import_key(self):
|
|
|
|
set_module_args(dict(
|
|
|
|
certificate='cert-foo',
|
|
|
|
private_key='private-foo',
|
|
|
|
dest='/path/to/keystore.jks',
|
2021-05-14 22:31:44 +02:00
|
|
|
name='test',
|
2020-03-09 10:11:07 +01:00
|
|
|
password='changeit'
|
|
|
|
))
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
2021-06-27 14:56:43 +02:00
|
|
|
argument_spec=module_argument_spec,
|
|
|
|
supports_check_mode=module_supports_check_mode,
|
|
|
|
mutually_exclusive=module_choose_between,
|
|
|
|
required_one_of=module_choose_between
|
2020-03-09 10:11:07 +01:00
|
|
|
)
|
|
|
|
|
2021-05-14 22:31:44 +02:00
|
|
|
module.exit_json = Mock()
|
2020-03-09 10:11:07 +01:00
|
|
|
module.fail_json = Mock()
|
|
|
|
|
|
|
|
with patch('os.remove', return_value=True):
|
2021-04-05 14:40:36 +02:00
|
|
|
self.create_path.side_effect = ['/tmp/tmpgrzm2ah7']
|
2021-05-14 22:31:44 +02:00
|
|
|
self.create_file.side_effect = ['/tmp/etacifitrec', '/tmp/yek_etavirp', '']
|
2021-06-27 14:56:43 +02:00
|
|
|
self.run_command.side_effect = [(0, '', ''), (1, '', 'Oops')]
|
2021-05-14 22:31:44 +02:00
|
|
|
self.get_bin_path.side_effect = ['keytool', 'openssl', '']
|
|
|
|
jks = JavaKeystore(module)
|
|
|
|
jks.create()
|
2020-03-09 10:11:07 +01:00
|
|
|
module.fail_json.assert_called_once_with(
|
2020-05-25 10:09:18 +02:00
|
|
|
cmd=["keytool", "-importkeystore",
|
|
|
|
"-destkeystore", "/path/to/keystore.jks",
|
2021-04-05 14:40:36 +02:00
|
|
|
"-srckeystore", "/tmp/tmpgrzm2ah7", "-srcstoretype", "pkcs12", "-alias", "test",
|
2021-05-17 20:05:24 +02:00
|
|
|
"-noprompt"],
|
2020-03-09 10:11:07 +01:00
|
|
|
msg='',
|
2021-06-27 14:56:43 +02:00
|
|
|
err='Oops',
|
2020-03-09 10:11:07 +01:00
|
|
|
rc=1
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class TestCertChanged(ModuleTestCase):
|
|
|
|
"""Test if the cert has changed."""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""Setup."""
|
|
|
|
super(TestCertChanged, self).setUp()
|
2022-11-02 21:42:29 +01:00
|
|
|
self.mock_create_file = patch('ansible_collections.community.general.plugins.modules.java_keystore.create_file')
|
|
|
|
self.mock_current_type = patch('ansible_collections.community.general.plugins.modules.java_keystore.JavaKeystore.current_type')
|
2021-05-14 22:31:44 +02:00
|
|
|
self.mock_run_command = patch('ansible.module_utils.basic.AnsibleModule.run_command')
|
|
|
|
self.mock_get_bin_path = patch('ansible.module_utils.basic.AnsibleModule.get_bin_path')
|
2021-06-27 14:56:43 +02:00
|
|
|
self.mock_preserved_copy = patch('ansible.module_utils.basic.AnsibleModule.preserved_copy')
|
|
|
|
self.mock_atomic_move = patch('ansible.module_utils.basic.AnsibleModule.atomic_move')
|
2021-05-14 22:31:44 +02:00
|
|
|
self.run_command = self.mock_run_command.start()
|
2020-03-09 10:11:07 +01:00
|
|
|
self.create_file = self.mock_create_file.start()
|
2021-05-14 22:31:44 +02:00
|
|
|
self.get_bin_path = self.mock_get_bin_path.start()
|
2021-06-27 14:56:43 +02:00
|
|
|
self.current_type = self.mock_current_type.start()
|
|
|
|
self.preserved_copy = self.mock_preserved_copy.start()
|
|
|
|
self.atomic_move = self.mock_atomic_move.start()
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
"""Teardown."""
|
|
|
|
super(TestCertChanged, self).tearDown()
|
|
|
|
self.mock_create_file.stop()
|
2021-06-27 14:56:43 +02:00
|
|
|
self.mock_current_type.stop()
|
2021-05-14 22:31:44 +02:00
|
|
|
self.mock_run_command.stop()
|
|
|
|
self.mock_get_bin_path.stop()
|
2021-06-27 14:56:43 +02:00
|
|
|
self.mock_preserved_copy.stop()
|
|
|
|
self.mock_atomic_move.stop()
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
def test_cert_unchanged_same_fingerprint(self):
|
|
|
|
set_module_args(dict(
|
|
|
|
certificate='cert-foo',
|
|
|
|
private_key='private-foo',
|
|
|
|
dest='/path/to/keystore.jks',
|
|
|
|
name='foo',
|
|
|
|
password='changeit'
|
|
|
|
))
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
2021-06-27 14:56:43 +02:00
|
|
|
argument_spec=module_argument_spec,
|
|
|
|
supports_check_mode=module_supports_check_mode,
|
|
|
|
mutually_exclusive=module_choose_between,
|
|
|
|
required_one_of=module_choose_between
|
2020-03-09 10:11:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
with patch('os.remove', return_value=True):
|
2021-05-14 22:31:44 +02:00
|
|
|
self.create_file.side_effect = ['/tmp/placeholder', '']
|
|
|
|
self.run_command.side_effect = [(0, 'foo=abcd:1234:efgh', ''), (0, 'SHA256: abcd:1234:efgh', '')]
|
|
|
|
self.get_bin_path.side_effect = ['keytool', 'openssl', '']
|
2021-06-27 14:56:43 +02:00
|
|
|
self.current_type.side_effect = ['jks']
|
2021-05-14 22:31:44 +02:00
|
|
|
jks = JavaKeystore(module)
|
|
|
|
result = jks.cert_changed()
|
2020-03-09 10:11:07 +01:00
|
|
|
self.assertFalse(result, 'Fingerprint is identical')
|
|
|
|
|
|
|
|
def test_cert_changed_fingerprint_mismatch(self):
|
|
|
|
set_module_args(dict(
|
|
|
|
certificate='cert-foo',
|
|
|
|
private_key='private-foo',
|
|
|
|
dest='/path/to/keystore.jks',
|
|
|
|
name='foo',
|
|
|
|
password='changeit'
|
|
|
|
))
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
2021-06-27 14:56:43 +02:00
|
|
|
argument_spec=module_argument_spec,
|
|
|
|
supports_check_mode=module_supports_check_mode,
|
|
|
|
mutually_exclusive=module_choose_between,
|
|
|
|
required_one_of=module_choose_between
|
2020-03-09 10:11:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
with patch('os.remove', return_value=True):
|
2021-05-14 22:31:44 +02:00
|
|
|
self.create_file.side_effect = ['/tmp/placeholder', '']
|
|
|
|
self.run_command.side_effect = [(0, 'foo=abcd:1234:efgh', ''), (0, 'SHA256: wxyz:9876:stuv', '')]
|
|
|
|
self.get_bin_path.side_effect = ['keytool', 'openssl', '']
|
2021-06-27 14:56:43 +02:00
|
|
|
self.current_type.side_effect = ['jks']
|
2021-05-14 22:31:44 +02:00
|
|
|
jks = JavaKeystore(module)
|
|
|
|
result = jks.cert_changed()
|
2020-03-09 10:11:07 +01:00
|
|
|
self.assertTrue(result, 'Fingerprint mismatch')
|
|
|
|
|
2021-06-27 14:56:43 +02:00
|
|
|
def test_cert_changed_alias_does_not_exist(self):
|
2020-03-09 10:11:07 +01:00
|
|
|
set_module_args(dict(
|
|
|
|
certificate='cert-foo',
|
|
|
|
private_key='private-foo',
|
|
|
|
dest='/path/to/keystore.jks',
|
|
|
|
name='foo',
|
|
|
|
password='changeit'
|
|
|
|
))
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
2021-06-27 14:56:43 +02:00
|
|
|
argument_spec=module_argument_spec,
|
|
|
|
supports_check_mode=module_supports_check_mode,
|
|
|
|
mutually_exclusive=module_choose_between,
|
|
|
|
required_one_of=module_choose_between
|
2020-03-09 10:11:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
with patch('os.remove', return_value=True):
|
2021-05-14 22:31:44 +02:00
|
|
|
self.create_file.side_effect = ['/tmp/placeholder', '']
|
|
|
|
self.run_command.side_effect = [(0, 'foo=abcd:1234:efgh', ''),
|
|
|
|
(1, 'keytool error: java.lang.Exception: Alias <foo> does not exist', '')]
|
|
|
|
self.get_bin_path.side_effect = ['keytool', 'openssl', '']
|
|
|
|
jks = JavaKeystore(module)
|
|
|
|
result = jks.cert_changed()
|
2021-04-19 06:59:52 +02:00
|
|
|
self.assertTrue(result, 'Alias mismatch detected')
|
|
|
|
|
|
|
|
def test_cert_changed_password_mismatch(self):
|
|
|
|
set_module_args(dict(
|
|
|
|
certificate='cert-foo',
|
|
|
|
private_key='private-foo',
|
|
|
|
dest='/path/to/keystore.jks',
|
|
|
|
name='foo',
|
|
|
|
password='changeit'
|
|
|
|
))
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
2021-06-27 14:56:43 +02:00
|
|
|
argument_spec=module_argument_spec,
|
|
|
|
supports_check_mode=module_supports_check_mode,
|
|
|
|
mutually_exclusive=module_choose_between,
|
|
|
|
required_one_of=module_choose_between
|
2021-04-19 06:59:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
with patch('os.remove', return_value=True):
|
2021-05-14 22:31:44 +02:00
|
|
|
self.create_file.side_effect = ['/tmp/placeholder', '']
|
|
|
|
self.run_command.side_effect = [(0, 'foo=abcd:1234:efgh', ''),
|
|
|
|
(1, 'keytool error: java.io.IOException: Keystore password was incorrect', '')]
|
|
|
|
self.get_bin_path.side_effect = ['keytool', 'openssl', '']
|
|
|
|
jks = JavaKeystore(module)
|
|
|
|
result = jks.cert_changed()
|
2021-04-19 06:59:52 +02:00
|
|
|
self.assertTrue(result, 'Password mismatch detected')
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
def test_cert_changed_fail_read_cert(self):
|
|
|
|
set_module_args(dict(
|
|
|
|
certificate='cert-foo',
|
|
|
|
private_key='private-foo',
|
|
|
|
dest='/path/to/keystore.jks',
|
|
|
|
name='foo',
|
|
|
|
password='changeit'
|
|
|
|
))
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
2021-06-27 14:56:43 +02:00
|
|
|
argument_spec=module_argument_spec,
|
|
|
|
supports_check_mode=module_supports_check_mode,
|
|
|
|
mutually_exclusive=module_choose_between,
|
|
|
|
required_one_of=module_choose_between
|
2020-03-09 10:11:07 +01:00
|
|
|
)
|
|
|
|
|
2021-05-14 22:31:44 +02:00
|
|
|
module.exit_json = Mock()
|
2020-03-09 10:11:07 +01:00
|
|
|
module.fail_json = Mock()
|
|
|
|
|
|
|
|
with patch('os.remove', return_value=True):
|
2021-05-14 22:31:44 +02:00
|
|
|
self.create_file.side_effect = ['/tmp/tmpdj6bvvme', '']
|
|
|
|
self.run_command.side_effect = [(1, '', 'Oops'), (0, 'SHA256: wxyz:9876:stuv', '')]
|
|
|
|
self.get_bin_path.side_effect = ['keytool', 'openssl', '']
|
2021-06-27 14:56:43 +02:00
|
|
|
self.current_type.side_effect = ['jks']
|
2021-05-14 22:31:44 +02:00
|
|
|
jks = JavaKeystore(module)
|
|
|
|
jks.cert_changed()
|
2020-03-09 10:11:07 +01:00
|
|
|
module.fail_json.assert_called_once_with(
|
2021-04-05 14:40:36 +02:00
|
|
|
cmd=["openssl", "x509", "-noout", "-in", "/tmp/tmpdj6bvvme", "-fingerprint", "-sha256"],
|
2020-03-09 10:11:07 +01:00
|
|
|
msg='',
|
|
|
|
err='Oops',
|
|
|
|
rc=1
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_cert_changed_fail_read_keystore(self):
|
|
|
|
set_module_args(dict(
|
|
|
|
certificate='cert-foo',
|
|
|
|
private_key='private-foo',
|
|
|
|
dest='/path/to/keystore.jks',
|
|
|
|
name='foo',
|
|
|
|
password='changeit'
|
|
|
|
))
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
2021-06-27 14:56:43 +02:00
|
|
|
argument_spec=module_argument_spec,
|
|
|
|
supports_check_mode=module_supports_check_mode,
|
|
|
|
mutually_exclusive=module_choose_between,
|
|
|
|
required_one_of=module_choose_between
|
2020-03-09 10:11:07 +01:00
|
|
|
)
|
|
|
|
|
2021-05-14 22:31:44 +02:00
|
|
|
module.exit_json = Mock()
|
2020-03-09 10:11:07 +01:00
|
|
|
module.fail_json = Mock(return_value=True)
|
|
|
|
|
|
|
|
with patch('os.remove', return_value=True):
|
2021-05-14 22:31:44 +02:00
|
|
|
self.create_file.side_effect = ['/tmp/placeholder', '']
|
|
|
|
self.run_command.side_effect = [(0, 'foo: wxyz:9876:stuv', ''), (1, '', 'Oops')]
|
|
|
|
self.get_bin_path.side_effect = ['keytool', 'openssl', '']
|
|
|
|
jks = JavaKeystore(module)
|
|
|
|
jks.cert_changed()
|
2020-03-09 10:11:07 +01:00
|
|
|
module.fail_json.assert_called_with(
|
2021-05-17 20:05:24 +02:00
|
|
|
cmd=["keytool", "-list", "-alias", "foo", "-keystore", "/path/to/keystore.jks", "-v"],
|
2020-03-09 10:11:07 +01:00
|
|
|
msg='',
|
|
|
|
err='Oops',
|
|
|
|
rc=1
|
|
|
|
)
|