mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-06 13:22:48 +00:00
Fix 'ansible-vault edit /some/symlink' (#20417)
Since vault edit attempts to unlink edited files before creating a new file with the same name and writing to it, if the file was a symlink, the symlink would be replaced with a regular file. VaultEditor file ops now check if files it is changing are symlinks and instead works directly on the target, so that os.rename() and shutils do the right thing. Add unit tests cases for this case and assorted VaultEditor test cases. Fixes #20264
This commit is contained in:
@@ -427,6 +427,10 @@ class VaultEditor:
|
||||
|
||||
# A file to be encrypted into a vaultfile could be any encoding
|
||||
# so treat the contents as a byte string.
|
||||
|
||||
# follow the symlink
|
||||
filename = os.path.realpath(filename)
|
||||
|
||||
b_plaintext = self.read_data(filename)
|
||||
b_ciphertext = self.vault.encrypt(b_plaintext)
|
||||
self.write_data(b_ciphertext, output_file or filename)
|
||||
@@ -435,7 +439,11 @@ class VaultEditor:
|
||||
|
||||
check_prereqs()
|
||||
|
||||
# follow the symlink
|
||||
filename = os.path.realpath(filename)
|
||||
|
||||
ciphertext = self.read_data(filename)
|
||||
|
||||
try:
|
||||
plaintext = self.vault.decrypt(ciphertext)
|
||||
except AnsibleError as e:
|
||||
@@ -458,7 +466,11 @@ class VaultEditor:
|
||||
|
||||
check_prereqs()
|
||||
|
||||
# follow the symlink
|
||||
filename = os.path.realpath(filename)
|
||||
|
||||
ciphertext = self.read_data(filename)
|
||||
|
||||
try:
|
||||
plaintext = self.vault.decrypt(ciphertext)
|
||||
except AnsibleError as e:
|
||||
@@ -486,8 +498,12 @@ class VaultEditor:
|
||||
|
||||
check_prereqs()
|
||||
|
||||
# follow the symlink
|
||||
filename = os.path.realpath(filename)
|
||||
|
||||
prev = os.stat(filename)
|
||||
ciphertext = self.read_data(filename)
|
||||
|
||||
try:
|
||||
plaintext = self.vault.decrypt(ciphertext)
|
||||
except AnsibleError as e:
|
||||
|
||||
Reference in New Issue
Block a user