Merge pull request #43483 from 3add3287/modules-ssh-add-newline-fix

Handle bogus newline before newly added keys
This commit is contained in:
Nicole Thomas 2017-09-14 10:19:51 -04:00 committed by GitHub
commit ebbc3c11d0

View file

@ -769,10 +769,13 @@ def set_auth_key(
with salt.utils.files.fopen(fconfig, 'ab+') as _fh:
if new_file is False:
# Let's make sure we have a new line at the end of the file
_fh.seek(1024, 2)
if not _fh.read(1024).rstrip(six.b(' ')).endswith(six.b('\n')):
_fh.seek(0, 2)
_fh.write(six.b('\n'))
_fh.seek(0, 2)
if _fh.tell() > 0:
# File isn't empty, check if last byte is a newline
# If not, add one
_fh.seek(-1, 2)
if _fh.read(1) != six.b('\n'):
_fh.write(six.b('\n'))
if six.PY3:
auth_line = auth_line.encode(__salt_system_encoding__)
_fh.write(auth_line)