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:
Adrian Likins
2017-02-24 12:35:39 -05:00
committed by GitHub
parent 8830cde28d
commit 6c6b647182
2 changed files with 348 additions and 14 deletions

View File

@@ -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: