| 1 |
# Copyright (c) 2018, Ansible Project |
| 2 |
# Copyright (c) 2018, Abhijeet Kasurde <[email protected]> |
| 3 |
# |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
import os |
| 10 |
from unittest.mock import Mock, patch |
| 11 |
|
| 12 |
from ansible.module_utils.basic import AnsibleModule # noqa: F401 # pylint: disable=unused-import |
| 13 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( |
| 14 |
ModuleTestCase, |
| 15 |
set_module_args, |
| 16 |
) |
| 17 |
|
| 18 |
from ansible_collections.community.general.plugins.modules.java_keystore import JavaKeystore, create_module |
| 19 |
|
| 20 |
|
| 21 |
class TestCreateJavaKeystore(ModuleTestCase): |
| 22 |
"""Test the creation of a Java keystore.""" |
| 23 |
|
| 24 |
def setUp(self): |
| 25 |
"""Setup.""" |
| 26 |
super().setUp() |
| 27 |
|
| 28 |
orig_exists = os.path.exists |
| 29 |
self.mock_create_file = patch("ansible_collections.community.general.plugins.modules.java_keystore.create_file") |
| 30 |
self.mock_create_path = patch("ansible_collections.community.general.plugins.modules.java_keystore.create_path") |
| 31 |
self.mock_current_type = patch( |
| 32 |
"ansible_collections.community.general.plugins.modules.java_keystore.JavaKeystore.current_type" |
| 33 |
) |
| 34 |
self.mock_run_command = patch("ansible.module_utils.basic.AnsibleModule.run_command") |
| 35 |
self.mock_get_bin_path = patch("ansible.module_utils.basic.AnsibleModule.get_bin_path") |
| 36 |
self.mock_preserved_copy = patch("ansible.module_utils.basic.AnsibleModule.preserved_copy") |
| 37 |
self.mock_atomic_move = patch("ansible.module_utils.basic.AnsibleModule.atomic_move") |
| 38 |
self.mock_os_path_exists = patch( |
| 39 |
"os.path.exists", side_effect=lambda path: True if path == "/path/to/keystore.jks" else orig_exists(path) |
| 40 |
) |
| 41 |
self.mock_selinux_context = patch( |
| 42 |
"ansible.module_utils.basic.AnsibleModule.selinux_context", |
| 43 |
side_effect=lambda path: ["unconfined_u", "object_r", "user_home_t", "s0"], |
| 44 |
) |
| 45 |
self.mock_is_special_selinux_path = patch( |
| 46 |
"ansible.module_utils.basic.AnsibleModule.is_special_selinux_path", side_effect=lambda path: (False, None) |
| 47 |
) |
| 48 |
self.run_command = self.mock_run_command.start() |
| 49 |
self.get_bin_path = self.mock_get_bin_path.start() |
| 50 |
self.preserved_copy = self.mock_preserved_copy.start() |
| 51 |
self.atomic_move = self.mock_atomic_move.start() |
| 52 |
self.create_file = self.mock_create_file.start() |
| 53 |
self.create_path = self.mock_create_path.start() |
| 54 |
self.current_type = self.mock_current_type.start() |
| 55 |
self.selinux_context = self.mock_selinux_context.start() |
| 56 |
self.is_special_selinux_path = self.mock_is_special_selinux_path.start() |
| 57 |
self.os_path_exists = self.mock_os_path_exists.start() |
| 58 |
|
| 59 |
def tearDown(self): |
| 60 |
"""Teardown.""" |
| 61 |
super().tearDown() |
| 62 |
self.mock_create_file.stop() |
| 63 |
self.mock_create_path.stop() |
| 64 |
self.mock_current_type.stop() |
| 65 |
self.mock_run_command.stop() |
| 66 |
self.mock_get_bin_path.stop() |
| 67 |
self.mock_preserved_copy.stop() |
| 68 |
self.mock_atomic_move.stop() |
| 69 |
self.mock_selinux_context.stop() |
| 70 |
self.mock_is_special_selinux_path.stop() |
| 71 |
self.mock_os_path_exists.stop() |
| 72 |
|
| 73 |
def test_create_jks_success(self): |
| 74 |
with set_module_args( |
| 75 |
dict( |
| 76 |
certificate="cert-foo", |
| 77 |
private_key="private-foo", |
| 78 |
dest="/path/to/keystore.jks", |
| 79 |
name="test", |
| 80 |
password="changeit", |
| 81 |
) |
| 82 |
): |
| 83 |
module = create_module() |
| 84 |
|
| 85 |
with patch("os.remove", return_value=True): |
| 86 |
self.create_path.side_effect = ["/tmp/tmpgrzm2ah7"] |
| 87 |
self.create_file.side_effect = ["/tmp/etacifitrec", "/tmp/yek_etavirp", ""] |
| 88 |
self.run_command.side_effect = [(0, "", ""), (0, "", "")] |
| 89 |
self.get_bin_path.side_effect = ["keytool", "openssl", ""] |
| 90 |
jks = JavaKeystore(module) |
| 91 |
assert jks.create() == { |
| 92 |
"changed": True, |
| 93 |
"cmd": [ |
| 94 |
"keytool", |
| 95 |
"-importkeystore", |
| 96 |
"-destkeystore", |
| 97 |
"/path/to/keystore.jks", |
| 98 |
"-srckeystore", |
| 99 |
"/tmp/tmpgrzm2ah7", |
| 100 |
"-srcstoretype", |
| 101 |
"pkcs12", |
| 102 |
"-alias", |
| 103 |
"test", |
| 104 |
"-noprompt", |
| 105 |
], |
| 106 |
"msg": "", |
| 107 |
"rc": 0, |
| 108 |
} |
| 109 |
|
| 110 |
def test_create_jks_keypass_fail_export_pkcs12(self): |
| 111 |
with set_module_args( |
| 112 |
dict( |
| 113 |
certificate="cert-foo", |
| 114 |
private_key="private-foo", |
| 115 |
private_key_passphrase="passphrase-foo", |
| 116 |
dest="/path/to/keystore.jks", |
| 117 |
name="test", |
| 118 |
password="changeit", |
| 119 |
) |
| 120 |
): |
| 121 |
module = create_module() |
| 122 |
|
| 123 |
module.exit_json = Mock() |
| 124 |
module.fail_json = Mock() |
| 125 |
|
| 126 |
with patch("os.remove", return_value=True): |
| 127 |
self.create_path.side_effect = ["/tmp/tmp1cyp12xa"] |
| 128 |
self.create_file.side_effect = ["/tmp/tmpvalcrt32", "/tmp/tmpwh4key0c", ""] |
| 129 |
self.run_command.side_effect = [(1, "", "Oops"), (0, "", "")] |
| 130 |
self.get_bin_path.side_effect = ["keytool", "openssl", ""] |
| 131 |
jks = JavaKeystore(module) |
| 132 |
jks.create() |
| 133 |
module.fail_json.assert_called_once_with( |
| 134 |
cmd=[ |
| 135 |
"openssl", |
| 136 |
"pkcs12", |
| 137 |
"-export", |
| 138 |
"-name", |
| 139 |
"test", |
| 140 |
"-in", |
| 141 |
"/tmp/tmpvalcrt32", |
| 142 |
"-inkey", |
| 143 |
"/tmp/tmpwh4key0c", |
| 144 |
"-out", |
| 145 |
"/tmp/tmp1cyp12xa", |
| 146 |
"-passout", |
| 147 |
"stdin", |
| 148 |
"-passin", |
| 149 |
"stdin", |
| 150 |
], |
| 151 |
msg="", |
| 152 |
err="Oops", |
| 153 |
rc=1, |
| 154 |
) |
| 155 |
|
| 156 |
def test_create_jks_fail_export_pkcs12(self): |
| 157 |
with set_module_args( |
| 158 |
dict( |
| 159 |
certificate="cert-foo", |
| 160 |
private_key="private-foo", |
| 161 |
dest="/path/to/keystore.jks", |
| 162 |
name="test", |
| 163 |
password="changeit", |
| 164 |
) |
| 165 |
): |
| 166 |
module = create_module() |
| 167 |
|
| 168 |
module.exit_json = Mock() |
| 169 |
module.fail_json = Mock() |
| 170 |
|
| 171 |
with patch("os.remove", return_value=True): |
| 172 |
self.create_path.side_effect = ["/tmp/tmp1cyp12xa"] |
| 173 |
self.create_file.side_effect = ["/tmp/tmpvalcrt32", "/tmp/tmpwh4key0c", ""] |
| 174 |
self.run_command.side_effect = [(1, "", "Oops"), (0, "", "")] |
| 175 |
self.get_bin_path.side_effect = ["keytool", "openssl", ""] |
| 176 |
jks = JavaKeystore(module) |
| 177 |
jks.create() |
| 178 |
module.fail_json.assert_called_once_with( |
| 179 |
cmd=[ |
| 180 |
"openssl", |
| 181 |
"pkcs12", |
| 182 |
"-export", |
| 183 |
"-name", |
| 184 |
"test", |
| 185 |
"-in", |
| 186 |
"/tmp/tmpvalcrt32", |
| 187 |
"-inkey", |
| 188 |
"/tmp/tmpwh4key0c", |
| 189 |
"-out", |
| 190 |
"/tmp/tmp1cyp12xa", |
| 191 |
"-passout", |
| 192 |
"stdin", |
| 193 |
], |
| 194 |
msg="", |
| 195 |
err="Oops", |
| 196 |
rc=1, |
| 197 |
) |
| 198 |
|
| 199 |
def test_create_jks_fail_import_key(self): |
| 200 |
with set_module_args( |
| 201 |
dict( |
| 202 |
certificate="cert-foo", |
| 203 |
private_key="private-foo", |
| 204 |
dest="/path/to/keystore.jks", |
| 205 |
name="test", |
| 206 |
password="changeit", |
| 207 |
) |
| 208 |
): |
| 209 |
module = create_module() |
| 210 |
|
| 211 |
module.exit_json = Mock() |
| 212 |
module.fail_json = Mock() |
| 213 |
|
| 214 |
with patch("os.remove", return_value=True): |
| 215 |
self.create_path.side_effect = ["/tmp/tmpgrzm2ah7"] |
| 216 |
self.create_file.side_effect = ["/tmp/etacifitrec", "/tmp/yek_etavirp", ""] |
| 217 |
self.run_command.side_effect = [(0, "", ""), (1, "", "Oops")] |
| 218 |
self.get_bin_path.side_effect = ["keytool", "openssl", ""] |
| 219 |
jks = JavaKeystore(module) |
| 220 |
jks.create() |
| 221 |
module.fail_json.assert_called_once_with( |
| 222 |
cmd=[ |
| 223 |
"keytool", |
| 224 |
"-importkeystore", |
| 225 |
"-destkeystore", |
| 226 |
"/path/to/keystore.jks", |
| 227 |
"-srckeystore", |
| 228 |
"/tmp/tmpgrzm2ah7", |
| 229 |
"-srcstoretype", |
| 230 |
"pkcs12", |
| 231 |
"-alias", |
| 232 |
"test", |
| 233 |
"-noprompt", |
| 234 |
], |
| 235 |
msg="", |
| 236 |
err="Oops", |
| 237 |
rc=1, |
| 238 |
) |
| 239 |
|
| 240 |
|
| 241 |
class TestCertChanged(ModuleTestCase): |
| 242 |
"""Test if the cert has changed.""" |
| 243 |
|
| 244 |
def setUp(self): |
| 245 |
"""Setup.""" |
| 246 |
super().setUp() |
| 247 |
self.mock_create_file = patch("ansible_collections.community.general.plugins.modules.java_keystore.create_file") |
| 248 |
self.mock_current_type = patch( |
| 249 |
"ansible_collections.community.general.plugins.modules.java_keystore.JavaKeystore.current_type" |
| 250 |
) |
| 251 |
self.mock_run_command = patch("ansible.module_utils.basic.AnsibleModule.run_command") |
| 252 |
self.mock_get_bin_path = patch("ansible.module_utils.basic.AnsibleModule.get_bin_path") |
| 253 |
self.mock_preserved_copy = patch("ansible.module_utils.basic.AnsibleModule.preserved_copy") |
| 254 |
self.mock_atomic_move = patch("ansible.module_utils.basic.AnsibleModule.atomic_move") |
| 255 |
self.run_command = self.mock_run_command.start() |
| 256 |
self.create_file = self.mock_create_file.start() |
| 257 |
self.get_bin_path = self.mock_get_bin_path.start() |
| 258 |
self.current_type = self.mock_current_type.start() |
| 259 |
self.preserved_copy = self.mock_preserved_copy.start() |
| 260 |
self.atomic_move = self.mock_atomic_move.start() |
| 261 |
|
| 262 |
def tearDown(self): |
| 263 |
"""Teardown.""" |
| 264 |
super().tearDown() |
| 265 |
self.mock_create_file.stop() |
| 266 |
self.mock_current_type.stop() |
| 267 |
self.mock_run_command.stop() |
| 268 |
self.mock_get_bin_path.stop() |
| 269 |
self.mock_preserved_copy.stop() |
| 270 |
self.mock_atomic_move.stop() |
| 271 |
|
| 272 |
def test_cert_unchanged_same_fingerprint(self): |
| 273 |
with set_module_args( |
| 274 |
dict( |
| 275 |
certificate="cert-foo", |
| 276 |
private_key="private-foo", |
| 277 |
dest="/path/to/keystore.jks", |
| 278 |
name="foo", |
| 279 |
password="changeit", |
| 280 |
) |
| 281 |
): |
| 282 |
module = create_module() |
| 283 |
|
| 284 |
with patch("os.remove", return_value=True): |
| 285 |
self.create_file.side_effect = ["/tmp/placeholder", ""] |
| 286 |
self.run_command.side_effect = [(0, "foo=abcd:1234:efgh", ""), (0, "SHA256: abcd:1234:efgh", "")] |
| 287 |
self.get_bin_path.side_effect = ["keytool", "openssl", ""] |
| 288 |
self.current_type.side_effect = ["jks"] |
| 289 |
jks = JavaKeystore(module) |
| 290 |
result = jks.cert_changed() |
| 291 |
self.assertFalse(result, "Fingerprint is identical") |
| 292 |
|
| 293 |
def test_cert_changed_fingerprint_mismatch(self): |
| 294 |
with set_module_args( |
| 295 |
dict( |
| 296 |
certificate="cert-foo", |
| 297 |
private_key="private-foo", |
| 298 |
dest="/path/to/keystore.jks", |
| 299 |
name="foo", |
| 300 |
password="changeit", |
| 301 |
) |
| 302 |
): |
| 303 |
module = create_module() |
| 304 |
|
| 305 |
with patch("os.remove", return_value=True): |
| 306 |
self.create_file.side_effect = ["/tmp/placeholder", ""] |
| 307 |
self.run_command.side_effect = [(0, "foo=abcd:1234:efgh", ""), (0, "SHA256: wxyz:9876:stuv", "")] |
| 308 |
self.get_bin_path.side_effect = ["keytool", "openssl", ""] |
| 309 |
self.current_type.side_effect = ["jks"] |
| 310 |
jks = JavaKeystore(module) |
| 311 |
result = jks.cert_changed() |
| 312 |
self.assertTrue(result, "Fingerprint mismatch") |
| 313 |
|
| 314 |
def test_cert_changed_alias_does_not_exist(self): |
| 315 |
with set_module_args( |
| 316 |
dict( |
| 317 |
certificate="cert-foo", |
| 318 |
private_key="private-foo", |
| 319 |
dest="/path/to/keystore.jks", |
| 320 |
name="foo", |
| 321 |
password="changeit", |
| 322 |
) |
| 323 |
): |
| 324 |
module = create_module() |
| 325 |
|
| 326 |
with patch("os.remove", return_value=True): |
| 327 |
self.create_file.side_effect = ["/tmp/placeholder", ""] |
| 328 |
self.run_command.side_effect = [ |
| 329 |
(0, "foo=abcd:1234:efgh", ""), |
| 330 |
(1, "keytool error: java.lang.Exception: Alias <foo> does not exist", ""), |
| 331 |
] |
| 332 |
self.get_bin_path.side_effect = ["keytool", "openssl", ""] |
| 333 |
jks = JavaKeystore(module) |
| 334 |
result = jks.cert_changed() |
| 335 |
self.assertTrue(result, "Alias mismatch detected") |
| 336 |
|
| 337 |
def test_cert_changed_password_mismatch(self): |
| 338 |
with set_module_args( |
| 339 |
dict( |
| 340 |
certificate="cert-foo", |
| 341 |
private_key="private-foo", |
| 342 |
dest="/path/to/keystore.jks", |
| 343 |
name="foo", |
| 344 |
password="changeit", |
| 345 |
) |
| 346 |
): |
| 347 |
module = create_module() |
| 348 |
|
| 349 |
with patch("os.remove", return_value=True): |
| 350 |
self.create_file.side_effect = ["/tmp/placeholder", ""] |
| 351 |
self.run_command.side_effect = [ |
| 352 |
(0, "foo=abcd:1234:efgh", ""), |
| 353 |
(1, "keytool error: java.io.IOException: Keystore password was incorrect", ""), |
| 354 |
] |
| 355 |
self.get_bin_path.side_effect = ["keytool", "openssl", ""] |
| 356 |
jks = JavaKeystore(module) |
| 357 |
result = jks.cert_changed() |
| 358 |
self.assertTrue(result, "Password mismatch detected") |
| 359 |
|
| 360 |
def test_cert_changed_fail_read_cert(self): |
| 361 |
with set_module_args( |
| 362 |
dict( |
| 363 |
certificate="cert-foo", |
| 364 |
private_key="private-foo", |
| 365 |
dest="/path/to/keystore.jks", |
| 366 |
name="foo", |
| 367 |
password="changeit", |
| 368 |
) |
| 369 |
): |
| 370 |
module = create_module() |
| 371 |
|
| 372 |
module.exit_json = Mock() |
| 373 |
module.fail_json = Mock() |
| 374 |
|
| 375 |
with patch("os.remove", return_value=True): |
| 376 |
self.create_file.side_effect = ["/tmp/tmpdj6bvvme", ""] |
| 377 |
self.run_command.side_effect = [(1, "", "Oops"), (0, "SHA256: wxyz:9876:stuv", "")] |
| 378 |
self.get_bin_path.side_effect = ["keytool", "openssl", ""] |
| 379 |
self.current_type.side_effect = ["jks"] |
| 380 |
jks = JavaKeystore(module) |
| 381 |
jks.cert_changed() |
| 382 |
module.fail_json.assert_called_once_with( |
| 383 |
cmd=["openssl", "x509", "-noout", "-in", "/tmp/tmpdj6bvvme", "-fingerprint", "-sha256"], |
| 384 |
msg="", |
| 385 |
err="Oops", |
| 386 |
rc=1, |
| 387 |
) |
| 388 |
|
| 389 |
def test_cert_changed_fail_read_keystore(self): |
| 390 |
with set_module_args( |
| 391 |
dict( |
| 392 |
certificate="cert-foo", |
| 393 |
private_key="private-foo", |
| 394 |
dest="/path/to/keystore.jks", |
| 395 |
name="foo", |
| 396 |
password="changeit", |
| 397 |
) |
| 398 |
): |
| 399 |
module = create_module() |
| 400 |
|
| 401 |
module.exit_json = Mock() |
| 402 |
module.fail_json = Mock(return_value=True) |
| 403 |
|
| 404 |
with patch("os.remove", return_value=True): |
| 405 |
self.create_file.side_effect = ["/tmp/placeholder", ""] |
| 406 |
self.run_command.side_effect = [(0, "foo: wxyz:9876:stuv", ""), (1, "", "Oops")] |
| 407 |
self.get_bin_path.side_effect = ["keytool", "openssl", ""] |
| 408 |
jks = JavaKeystore(module) |
| 409 |
jks.cert_changed() |
| 410 |
module.fail_json.assert_called_with( |
| 411 |
cmd=["keytool", "-list", "-alias", "foo", "-keystore", "/path/to/keystore.jks", "-v"], |
| 412 |
msg="", |
| 413 |
err="Oops", |
| 414 |
rc=1, |
| 415 |
) |