From: Ricardo Dias Date: Thu, 21 Nov 2019 15:01:31 +0000 (+0000) Subject: ceph-daemon: append newline before public key string X-Git-Tag: v15.1.0~795^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=refs%2Fpull%2F31788%2Fhead;p=ceph.git ceph-daemon: append newline before public key string Sometimes, `authorized_keys` file does not end with a newline and therefore the public key generated by ceph-daemon gets appended to the same line of the previous key. Therefore we need to append a newline before our key to guarantee that the above case does not happen. Signed-off-by: Ricardo Dias --- diff --git a/src/ceph-daemon/ceph-daemon b/src/ceph-daemon/ceph-daemon index 63c46567963d..2e62a17980b0 100755 --- a/src/ceph-daemon/ceph-daemon +++ b/src/ceph-daemon/ceph-daemon @@ -1171,8 +1171,19 @@ def command_bootstrap(): logger.info('Adding key to root@localhost\'s authorized_keys...') if not os.path.exists('/root/.ssh'): os.mkdir('/root/.ssh', 0o700) - with open('/root/.ssh/authorized_keys', 'a') as f: + auth_keys_file = '/root/.ssh/authorized_keys' + add_newline = False + if os.path.exists(auth_keys_file): + with open(auth_keys_file, 'r') as f: + f.seek(0, 2) + if f.tell() > 0: + f.seek(-1, 2) # go to last character + if f.read() != '\n': + add_newline = True + with open(auth_keys_file, 'a') as f: os.fchmod(f.fileno(), 0o600) # just in case we created it + if add_newline: + f.write('\n') f.write(ssh_pub.strip() + '\n') logger.info('Enabling ssh module...')