| 1 |
# (c) 2014, James Tanner <[email protected]> |
| 2 |
# (c) 2014, James Cammarata, <[email protected]> |
| 3 |
# |
| 4 |
# This file is part of Ansible |
| 5 |
# |
| 6 |
# Ansible is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# Ansible is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
from __future__ import annotations |
| 20 |
|
| 21 |
import os |
| 22 |
import tempfile |
| 23 |
from io import BytesIO, StringIO |
| 24 |
|
| 25 |
import pytest |
| 26 |
|
| 27 |
import unittest |
| 28 |
from unittest.mock import patch |
| 29 |
|
| 30 |
from ansible import errors |
| 31 |
from ansible.parsing import vault |
| 32 |
from ansible.parsing.vault import VaultLib, VaultEditor, match_encrypt_secret |
| 33 |
|
| 34 |
from ansible.module_utils.common.text.converters import to_bytes, to_text |
| 35 |
|
| 36 |
from units.mock.vault_helper import TextVaultSecret |
| 37 |
|
| 38 |
v11_data = """$ANSIBLE_VAULT;1.1;AES256 |
| 39 |
62303130653266653331306264616235333735323636616539316433666463323964623162386137 |
| 40 |
3961616263373033353631316333623566303532663065310a393036623466376263393961326530 |
| 41 |
64336561613965383835646464623865663966323464653236343638373165343863623638316664 |
| 42 |
3631633031323837340a396530313963373030343933616133393566366137363761373930663833 |
| 43 |
3739""" |
| 44 |
|
| 45 |
|
| 46 |
@pytest.mark.skipif(not vault.HAS_CRYPTOGRAPHY, |
| 47 |
reason="Skipping cryptography tests because cryptography is not installed") |
| 48 |
class TestVaultEditor(unittest.TestCase): |
| 49 |
|
| 50 |
def setUp(self): |
| 51 |
self._test_dir = None |
| 52 |
self.vault_password = "test-vault-password" |
| 53 |
vault_secret = TextVaultSecret(self.vault_password) |
| 54 |
self.vault_secrets = [('vault_secret', vault_secret), |
| 55 |
('default', vault_secret)] |
| 56 |
|
| 57 |
@property |
| 58 |
def vault_secret(self): |
| 59 |
return match_encrypt_secret(self.vault_secrets)[1] |
| 60 |
|
| 61 |
def tearDown(self): |
| 62 |
if self._test_dir: |
| 63 |
pass |
| 64 |
# shutil.rmtree(self._test_dir) |
| 65 |
self._test_dir = None |
| 66 |
|
| 67 |
def _secrets(self, password): |
| 68 |
vault_secret = TextVaultSecret(password) |
| 69 |
vault_secrets = [('default', vault_secret)] |
| 70 |
return vault_secrets |
| 71 |
|
| 72 |
def test_methods_exist(self): |
| 73 |
v = vault.VaultEditor(None) |
| 74 |
slots = ['create_file', |
| 75 |
'decrypt_file', |
| 76 |
'edit_file', |
| 77 |
'encrypt_file', |
| 78 |
'rekey_file', |
| 79 |
'read_data', |
| 80 |
'write_data'] |
| 81 |
for slot in slots: |
| 82 |
assert hasattr(v, slot), "VaultLib is missing the %s method" % slot |
| 83 |
|
| 84 |
def _create_test_dir(self): |
| 85 |
suffix = '_ansible_unit_test_%s_' % (self.__class__.__name__) |
| 86 |
return tempfile.mkdtemp(suffix=suffix) |
| 87 |
|
| 88 |
def _create_file(self, test_dir, name, content, symlink=False): |
| 89 |
file_path = os.path.join(test_dir, name) |
| 90 |
with open(file_path, 'wb') as opened_file: |
| 91 |
opened_file.write(content) |
| 92 |
return file_path |
| 93 |
|
| 94 |
def _vault_editor(self, vault_secrets=None): |
| 95 |
if vault_secrets is None: |
| 96 |
vault_secrets = self._secrets(self.vault_password) |
| 97 |
return VaultEditor(VaultLib(vault_secrets)) |
| 98 |
|
| 99 |
@patch('ansible.parsing.vault.subprocess.call') |
| 100 |
def test_edit_file_helper_empty_target(self, mock_sp_call): |
| 101 |
self._test_dir = self._create_test_dir() |
| 102 |
|
| 103 |
src_contents = to_bytes("some info in a file\nyup.") |
| 104 |
src_file_path = self._create_file(self._test_dir, 'src_file', content=src_contents) |
| 105 |
|
| 106 |
mock_sp_call.side_effect = self._faux_command |
| 107 |
ve = self._vault_editor() |
| 108 |
|
| 109 |
b_ciphertext = ve._edit_file_helper(src_file_path, self.vault_secret) |
| 110 |
|
| 111 |
self.assertNotEqual(src_contents, b_ciphertext) |
| 112 |
|
| 113 |
def test_stdin_binary(self): |
| 114 |
stdin_data = '\0' |
| 115 |
|
| 116 |
fake_stream = StringIO(stdin_data) |
| 117 |
fake_stream.buffer = BytesIO(to_bytes(stdin_data)) |
| 118 |
|
| 119 |
with patch('sys.stdin', fake_stream): |
| 120 |
ve = self._vault_editor() |
| 121 |
data = ve.read_data('-') |
| 122 |
|
| 123 |
self.assertEqual(data, b'\0') |
| 124 |
|
| 125 |
@patch('ansible.parsing.vault.subprocess.call') |
| 126 |
def test_edit_file_helper_call_exception(self, mock_sp_call): |
| 127 |
self._test_dir = self._create_test_dir() |
| 128 |
|
| 129 |
src_contents = to_bytes("some info in a file\nyup.") |
| 130 |
src_file_path = self._create_file(self._test_dir, 'src_file', content=src_contents) |
| 131 |
|
| 132 |
error_txt = 'calling editor raised an exception' |
| 133 |
mock_sp_call.side_effect = errors.AnsibleError(error_txt) |
| 134 |
|
| 135 |
ve = self._vault_editor() |
| 136 |
|
| 137 |
self.assertRaisesRegex(errors.AnsibleError, |
| 138 |
error_txt, |
| 139 |
ve._edit_file_helper, |
| 140 |
src_file_path, |
| 141 |
self.vault_secret) |
| 142 |
|
| 143 |
@patch('ansible.parsing.vault.subprocess.call') |
| 144 |
def test_edit_file_helper_symlink_target(self, mock_sp_call): |
| 145 |
self._test_dir = self._create_test_dir() |
| 146 |
|
| 147 |
src_file_contents = to_bytes("some info in a file\nyup.") |
| 148 |
src_file_path = self._create_file(self._test_dir, 'src_file', content=src_file_contents) |
| 149 |
|
| 150 |
src_file_link_path = os.path.join(self._test_dir, 'a_link_to_dest_file') |
| 151 |
|
| 152 |
os.symlink(src_file_path, src_file_link_path) |
| 153 |
|
| 154 |
mock_sp_call.side_effect = self._faux_command |
| 155 |
ve = self._vault_editor() |
| 156 |
|
| 157 |
b_ciphertext = ve._edit_file_helper(src_file_link_path, self.vault_secret) |
| 158 |
|
| 159 |
self.assertNotEqual(src_file_contents, b_ciphertext, |
| 160 |
'b_ciphertext should be encrypted and not equal to src_contents') |
| 161 |
|
| 162 |
def _faux_editor(self, editor_args, new_src_contents): |
| 163 |
if editor_args[0] == 'shred': |
| 164 |
return |
| 165 |
|
| 166 |
tmp_path = editor_args[-1] |
| 167 |
|
| 168 |
# simulate the tmp file being edited |
| 169 |
with open(tmp_path, 'wb') as tmp_file: |
| 170 |
tmp_file.write(new_src_contents) |
| 171 |
|
| 172 |
def _faux_command(self, tmp_path): |
| 173 |
pass |
| 174 |
|
| 175 |
@patch('ansible.parsing.vault.subprocess.call') |
| 176 |
def test_edit_file_helper_no_change(self, mock_sp_call): |
| 177 |
self._test_dir = self._create_test_dir() |
| 178 |
|
| 179 |
src_file_contents = to_bytes("some info in a file\nyup.") |
| 180 |
src_file_path = self._create_file(self._test_dir, 'src_file', content=src_file_contents) |
| 181 |
|
| 182 |
# editor invocation doesn't change anything |
| 183 |
def faux_editor(editor_args): |
| 184 |
self._faux_editor(editor_args, src_file_contents) |
| 185 |
|
| 186 |
mock_sp_call.side_effect = faux_editor |
| 187 |
ve = self._vault_editor() |
| 188 |
|
| 189 |
ve._edit_file_helper(src_file_path, self.vault_secret, existing_data=src_file_contents) |
| 190 |
|
| 191 |
with open(src_file_path, 'rb') as new_target_file: |
| 192 |
new_target_file_contents = new_target_file.read() |
| 193 |
self.assertEqual(src_file_contents, new_target_file_contents) |
| 194 |
|
| 195 |
def _assert_file_is_encrypted(self, vault_editor, src_file_path, src_contents): |
| 196 |
with open(src_file_path, 'rb') as new_src_file: |
| 197 |
new_src_file_contents = new_src_file.read() |
| 198 |
|
| 199 |
# TODO: assert that it is encrypted |
| 200 |
self.assertTrue(vault.is_encrypted(new_src_file_contents)) |
| 201 |
|
| 202 |
src_file_plaintext = vault_editor.vault.decrypt(new_src_file_contents) |
| 203 |
|
| 204 |
# the plaintext should not be encrypted |
| 205 |
self.assertFalse(vault.is_encrypted(src_file_plaintext)) |
| 206 |
|
| 207 |
# and the new plaintext should match the original |
| 208 |
self.assertEqual(src_file_plaintext, src_contents) |
| 209 |
|
| 210 |
def _assert_file_is_link(self, src_file_link_path, src_file_path): |
| 211 |
self.assertTrue(os.path.islink(src_file_link_path), |
| 212 |
'The dest path (%s) should be a symlink to (%s) but is not' % (src_file_link_path, src_file_path)) |
| 213 |
|
| 214 |
def test_rekey_file(self): |
| 215 |
self._test_dir = self._create_test_dir() |
| 216 |
|
| 217 |
src_file_contents = to_bytes("some info in a file\nyup.") |
| 218 |
src_file_path = self._create_file(self._test_dir, 'src_file', content=src_file_contents) |
| 219 |
|
| 220 |
ve = self._vault_editor() |
| 221 |
ve.encrypt_file(src_file_path, self.vault_secret) |
| 222 |
|
| 223 |
# FIXME: update to just set self._secrets or just a new vault secret id |
| 224 |
new_password = 'password2:electricbugaloo' |
| 225 |
new_vault_secret = TextVaultSecret(new_password) |
| 226 |
new_vault_secrets = [('default', new_vault_secret)] |
| 227 |
ve.rekey_file(src_file_path, vault.match_encrypt_secret(new_vault_secrets)[1]) |
| 228 |
|
| 229 |
# FIXME: can just update self._secrets here |
| 230 |
new_ve = vault.VaultEditor(VaultLib(new_vault_secrets)) |
| 231 |
self._assert_file_is_encrypted(new_ve, src_file_path, src_file_contents) |
| 232 |
|
| 233 |
def test_rekey_file_no_new_password(self): |
| 234 |
self._test_dir = self._create_test_dir() |
| 235 |
|
| 236 |
src_file_contents = to_bytes("some info in a file\nyup.") |
| 237 |
src_file_path = self._create_file(self._test_dir, 'src_file', content=src_file_contents) |
| 238 |
|
| 239 |
ve = self._vault_editor() |
| 240 |
ve.encrypt_file(src_file_path, self.vault_secret) |
| 241 |
|
| 242 |
self.assertRaisesRegex(errors.AnsibleError, |
| 243 |
'The value for the new_password to rekey', |
| 244 |
ve.rekey_file, |
| 245 |
src_file_path, |
| 246 |
None) |
| 247 |
|
| 248 |
def test_rekey_file_not_encrypted(self): |
| 249 |
self._test_dir = self._create_test_dir() |
| 250 |
|
| 251 |
src_file_contents = to_bytes("some info in a file\nyup.") |
| 252 |
src_file_path = self._create_file(self._test_dir, 'src_file', content=src_file_contents) |
| 253 |
|
| 254 |
ve = self._vault_editor() |
| 255 |
|
| 256 |
new_password = 'password2:electricbugaloo' |
| 257 |
self.assertRaisesRegex(errors.AnsibleError, |
| 258 |
'Input is not vault encrypted data', |
| 259 |
ve.rekey_file, |
| 260 |
src_file_path, new_password) |
| 261 |
|
| 262 |
def test_plaintext(self): |
| 263 |
self._test_dir = self._create_test_dir() |
| 264 |
|
| 265 |
src_file_contents = to_bytes("some info in a file\nyup.") |
| 266 |
src_file_path = self._create_file(self._test_dir, 'src_file', content=src_file_contents) |
| 267 |
|
| 268 |
ve = self._vault_editor() |
| 269 |
ve.encrypt_file(src_file_path, self.vault_secret) |
| 270 |
|
| 271 |
res = ve.plaintext(src_file_path) |
| 272 |
self.assertEqual(src_file_contents, res) |
| 273 |
|
| 274 |
def test_plaintext_not_encrypted(self): |
| 275 |
self._test_dir = self._create_test_dir() |
| 276 |
|
| 277 |
src_file_contents = to_bytes("some info in a file\nyup.") |
| 278 |
src_file_path = self._create_file(self._test_dir, 'src_file', content=src_file_contents) |
| 279 |
|
| 280 |
ve = self._vault_editor() |
| 281 |
self.assertRaisesRegex(errors.AnsibleError, |
| 282 |
'Input is not vault encrypted data', |
| 283 |
ve.plaintext, |
| 284 |
src_file_path) |
| 285 |
|
| 286 |
def test_encrypt_file(self): |
| 287 |
self._test_dir = self._create_test_dir() |
| 288 |
src_file_contents = to_bytes("some info in a file\nyup.") |
| 289 |
src_file_path = self._create_file(self._test_dir, 'src_file', content=src_file_contents) |
| 290 |
|
| 291 |
ve = self._vault_editor() |
| 292 |
ve.encrypt_file(src_file_path, self.vault_secret) |
| 293 |
|
| 294 |
self._assert_file_is_encrypted(ve, src_file_path, src_file_contents) |
| 295 |
|
| 296 |
def test_encrypt_file_symlink(self): |
| 297 |
self._test_dir = self._create_test_dir() |
| 298 |
|
| 299 |
src_file_contents = to_bytes("some info in a file\nyup.") |
| 300 |
src_file_path = self._create_file(self._test_dir, 'src_file', content=src_file_contents) |
| 301 |
|
| 302 |
src_file_link_path = os.path.join(self._test_dir, 'a_link_to_dest_file') |
| 303 |
os.symlink(src_file_path, src_file_link_path) |
| 304 |
|
| 305 |
ve = self._vault_editor() |
| 306 |
ve.encrypt_file(src_file_link_path, self.vault_secret) |
| 307 |
|
| 308 |
self._assert_file_is_encrypted(ve, src_file_path, src_file_contents) |
| 309 |
self._assert_file_is_encrypted(ve, src_file_link_path, src_file_contents) |
| 310 |
|
| 311 |
self._assert_file_is_link(src_file_link_path, src_file_path) |
| 312 |
|
| 313 |
@patch('ansible.parsing.vault.subprocess.call') |
| 314 |
def test_edit_file_no_vault_id(self, mock_sp_call): |
| 315 |
self._test_dir = self._create_test_dir() |
| 316 |
src_contents = to_bytes("some info in a file\nyup.") |
| 317 |
|
| 318 |
src_file_path = self._create_file(self._test_dir, 'src_file', content=src_contents) |
| 319 |
|
| 320 |
new_src_contents = to_bytes("The info is different now.") |
| 321 |
|
| 322 |
def faux_editor(editor_args): |
| 323 |
self._faux_editor(editor_args, new_src_contents) |
| 324 |
|
| 325 |
mock_sp_call.side_effect = faux_editor |
| 326 |
|
| 327 |
ve = self._vault_editor() |
| 328 |
|
| 329 |
ve.encrypt_file(src_file_path, self.vault_secret) |
| 330 |
ve.edit_file(src_file_path) |
| 331 |
|
| 332 |
with open(src_file_path, 'rb') as new_src_file: |
| 333 |
new_src_file_contents = new_src_file.read() |
| 334 |
|
| 335 |
self.assertTrue(b'$ANSIBLE_VAULT;1.1;AES256' in new_src_file_contents) |
| 336 |
|
| 337 |
src_file_plaintext = ve.vault.decrypt(new_src_file_contents) |
| 338 |
self.assertEqual(src_file_plaintext, new_src_contents) |
| 339 |
|
| 340 |
@patch('ansible.parsing.vault.subprocess.call') |
| 341 |
def test_edit_file_with_vault_id(self, mock_sp_call): |
| 342 |
self._test_dir = self._create_test_dir() |
| 343 |
src_contents = to_bytes("some info in a file\nyup.") |
| 344 |
|
| 345 |
src_file_path = self._create_file(self._test_dir, 'src_file', content=src_contents) |
| 346 |
|
| 347 |
new_src_contents = to_bytes("The info is different now.") |
| 348 |
|
| 349 |
def faux_editor(editor_args): |
| 350 |
self._faux_editor(editor_args, new_src_contents) |
| 351 |
|
| 352 |
mock_sp_call.side_effect = faux_editor |
| 353 |
|
| 354 |
ve = self._vault_editor() |
| 355 |
|
| 356 |
ve.encrypt_file(src_file_path, self.vault_secret, |
| 357 |
vault_id='vault_secrets') |
| 358 |
ve.edit_file(src_file_path) |
| 359 |
|
| 360 |
with open(src_file_path, 'rb') as new_src_file: |
| 361 |
new_src_file_contents = new_src_file.read() |
| 362 |
|
| 363 |
self.assertTrue(b'$ANSIBLE_VAULT;1.2;AES256;vault_secrets' in new_src_file_contents) |
| 364 |
|
| 365 |
src_file_plaintext = ve.vault.decrypt(new_src_file_contents) |
| 366 |
self.assertEqual(src_file_plaintext, new_src_contents) |
| 367 |
|
| 368 |
@patch('ansible.parsing.vault.subprocess.call') |
| 369 |
def test_edit_file_symlink(self, mock_sp_call): |
| 370 |
self._test_dir = self._create_test_dir() |
| 371 |
src_contents = to_bytes("some info in a file\nyup.") |
| 372 |
|
| 373 |
src_file_path = self._create_file(self._test_dir, 'src_file', content=src_contents) |
| 374 |
|
| 375 |
new_src_contents = to_bytes("The info is different now.") |
| 376 |
|
| 377 |
def faux_editor(editor_args): |
| 378 |
self._faux_editor(editor_args, new_src_contents) |
| 379 |
|
| 380 |
mock_sp_call.side_effect = faux_editor |
| 381 |
|
| 382 |
ve = self._vault_editor() |
| 383 |
|
| 384 |
ve.encrypt_file(src_file_path, self.vault_secret) |
| 385 |
|
| 386 |
src_file_link_path = os.path.join(self._test_dir, 'a_link_to_dest_file') |
| 387 |
|
| 388 |
os.symlink(src_file_path, src_file_link_path) |
| 389 |
|
| 390 |
ve.edit_file(src_file_link_path) |
| 391 |
|
| 392 |
with open(src_file_path, 'rb') as new_src_file: |
| 393 |
new_src_file_contents = new_src_file.read() |
| 394 |
|
| 395 |
src_file_plaintext = ve.vault.decrypt(new_src_file_contents) |
| 396 |
|
| 397 |
self._assert_file_is_link(src_file_link_path, src_file_path) |
| 398 |
|
| 399 |
self.assertEqual(src_file_plaintext, new_src_contents) |
| 400 |
|
| 401 |
# self.assertEqual(src_file_plaintext, new_src_contents, |
| 402 |
# 'The decrypted plaintext of the edited file is not the expected contents.') |
| 403 |
|
| 404 |
@patch('ansible.parsing.vault.subprocess.call') |
| 405 |
def test_edit_file_not_encrypted(self, mock_sp_call): |
| 406 |
self._test_dir = self._create_test_dir() |
| 407 |
src_contents = to_bytes("some info in a file\nyup.") |
| 408 |
|
| 409 |
src_file_path = self._create_file(self._test_dir, 'src_file', content=src_contents) |
| 410 |
|
| 411 |
ve = self._vault_editor() |
| 412 |
self.assertRaisesRegex(errors.AnsibleError, |
| 413 |
'Input is not vault encrypted data', |
| 414 |
ve.edit_file, |
| 415 |
src_file_path) |
| 416 |
|
| 417 |
def test_create_file_exists(self): |
| 418 |
self._test_dir = self._create_test_dir() |
| 419 |
src_contents = to_bytes("some info in a file\nyup.") |
| 420 |
src_file_path = self._create_file(self._test_dir, 'src_file', content=src_contents) |
| 421 |
|
| 422 |
ve = self._vault_editor() |
| 423 |
self.assertRaisesRegex(errors.AnsibleError, |
| 424 |
'please use .edit. instead', |
| 425 |
ve.create_file, |
| 426 |
src_file_path, |
| 427 |
self.vault_secret) |
| 428 |
|
| 429 |
def test_decrypt_file_exception(self): |
| 430 |
self._test_dir = self._create_test_dir() |
| 431 |
src_contents = to_bytes("some info in a file\nyup.") |
| 432 |
src_file_path = self._create_file(self._test_dir, 'src_file', content=src_contents) |
| 433 |
|
| 434 |
ve = self._vault_editor() |
| 435 |
self.assertRaisesRegex(errors.AnsibleError, |
| 436 |
'Input is not vault encrypted data', |
| 437 |
ve.decrypt_file, |
| 438 |
src_file_path) |
| 439 |
|
| 440 |
@patch.object(vault.VaultEditor, '_editor_shell_command') |
| 441 |
def test_create_file(self, mock_editor_shell_command): |
| 442 |
|
| 443 |
def sc_side_effect(filename): |
| 444 |
return ['touch', filename] |
| 445 |
mock_editor_shell_command.side_effect = sc_side_effect |
| 446 |
|
| 447 |
tmp_file = tempfile.NamedTemporaryFile() |
| 448 |
os.unlink(tmp_file.name) |
| 449 |
|
| 450 |
_secrets = self._secrets('ansible') |
| 451 |
ve = self._vault_editor(_secrets) |
| 452 |
ve.create_file(tmp_file.name, vault.match_encrypt_secret(_secrets)[1]) |
| 453 |
|
| 454 |
self.assertTrue(os.path.exists(tmp_file.name)) |
| 455 |
|
| 456 |
def test_decrypt_1_1(self): |
| 457 |
v11_file = tempfile.NamedTemporaryFile(delete=False) |
| 458 |
with v11_file as f: |
| 459 |
f.write(to_bytes(v11_data)) |
| 460 |
|
| 461 |
ve = self._vault_editor(self._secrets("ansible")) |
| 462 |
|
| 463 |
# make sure the password functions for the cipher |
| 464 |
ve.decrypt_file(v11_file.name) |
| 465 |
|
| 466 |
# verify decrypted content |
| 467 |
with open(v11_file.name, "rb") as f: |
| 468 |
fdata = to_text(f.read()) |
| 469 |
|
| 470 |
os.unlink(v11_file.name) |
| 471 |
|
| 472 |
assert fdata.strip() == "foo", "incorrect decryption of 1.1 file: %s" % fdata.strip() |
| 473 |
|
| 474 |
def test_real_path_dash(self): |
| 475 |
filename = '-' |
| 476 |
ve = self._vault_editor() |
| 477 |
|
| 478 |
res = ve._real_path(filename) |
| 479 |
self.assertEqual(res, '-') |
| 480 |
|
| 481 |
def test_real_path_not_dash(self): |
| 482 |
filename = '/dev/null' |
| 483 |
ve = self._vault_editor() |
| 484 |
|
| 485 |
res = ve._real_path(filename) |
| 486 |
self.assertNotEqual(res, '-') |