mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-05-20 16:09:09 -07:00
Fix vault reading from stdin (avoid realpath() on non-links) (#23583)
* Fix vault reading from stdin (avoid realpath() on non-links) os.path.realpath() is used to find the target of file paths that are symlinks so vault operations happen directly on the target. However, in addition to resolving symlinks, realpath() also returns a full path. when reading from stdin, vault cli uses '-' as a special file path so VaultEditor() will replace with stdin. realpath() was expanding '-' with the CWD to something like '/home/user/playbooks/-' causing errors like: ERROR! [Errno 2] No such file or directory: u'/home/user/ansible/-' Fix is to specialcase '-' to not use realpath() Fixes #23567 * to_text decrypt output when writing to stdout
This commit is contained in:
parent
f82d95ae28
commit
ae3d7fb29e
3 changed files with 47 additions and 5 deletions
|
@ -9,6 +9,8 @@ trap 'rm -rf "${MYTMPDIR}"' EXIT
|
|||
TEST_FILE="${MYTMPDIR}/test_file"
|
||||
echo "This is a test file" > "${TEST_FILE}"
|
||||
|
||||
TEST_FILE_OUTPUT="${MYTMPDIR}/test_file_output"
|
||||
|
||||
# encrypt it
|
||||
ansible-vault encrypt "$@" --vault-password-file vault-password "${TEST_FILE}"
|
||||
|
||||
|
@ -28,6 +30,11 @@ ansible-vault view "$@" --vault-password-file "${NEW_VAULT_PASSWORD}" "${TEST_FI
|
|||
|
||||
ansible-vault decrypt "$@" --vault-password-file "${NEW_VAULT_PASSWORD}" "${TEST_FILE}"
|
||||
|
||||
# reading/writing to/from stdin/stdin (See https://github.com/ansible/ansible/issues/23567)
|
||||
ansible-vault encrypt "$@" --vault-password-file "${NEW_VAULT_PASSWORD}" --output="${TEST_FILE_OUTPUT}" < "${TEST_FILE}"
|
||||
ansible-vault view "$@" --vault-password-file "${NEW_VAULT_PASSWORD}" - < "${TEST_FILE_OUTPUT}"
|
||||
ansible-vault decrypt "$@" --vault-password-file "${NEW_VAULT_PASSWORD}" --output=- < "${TEST_FILE_OUTPUT}"
|
||||
|
||||
ansible-vault encrypt_string "$@" --vault-password-file "${NEW_VAULT_PASSWORD}" "a test string"
|
||||
|
||||
ansible-vault encrypt_string "$@" --vault-password-file "${NEW_VAULT_PASSWORD}" --name "blippy" "a test string names blippy"
|
||||
|
|
|
@ -516,3 +516,29 @@ class TestVaultEditor(unittest.TestCase):
|
|||
assert vl.cipher_name == "AES256", "wrong cipher name set after rekey: %s" % vl.cipher_name
|
||||
assert error_hit is False, "error decrypting migrated 1.0 file"
|
||||
assert dec_data.strip() == b"foo", "incorrect decryption of rekeyed/migrated file: %s" % dec_data
|
||||
|
||||
def test_real_path_dash(self):
|
||||
filename = '-'
|
||||
ve = vault.VaultEditor('password')
|
||||
|
||||
res = ve._real_path(filename)
|
||||
self.assertEqual(res, '-')
|
||||
|
||||
def test_real_path_dev_null(self):
|
||||
filename = '/dev/null'
|
||||
ve = vault.VaultEditor('password')
|
||||
|
||||
res = ve._real_path(filename)
|
||||
self.assertEqual(res, '/dev/null')
|
||||
|
||||
def test_real_path_symlink(self):
|
||||
self._test_dir = self._create_test_dir()
|
||||
file_path = self._create_file(self._test_dir, 'test_file', content=b'this is a test file')
|
||||
file_link_path = os.path.join(self._test_dir, 'a_link_to_test_file')
|
||||
|
||||
os.symlink(file_path, file_link_path)
|
||||
|
||||
ve = vault.VaultEditor('password')
|
||||
|
||||
res = ve._real_path(file_link_path)
|
||||
self.assertEqual(res, file_path)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue