From 1de5f989936701571904829288d43f5fe1480e38 Mon Sep 17 00:00:00 2001 From: Ricardo Dias Date: Thu, 21 Nov 2019 15:01:31 +0000 Subject: [PATCH] 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 --- src/ceph-daemon/ceph-daemon | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/ceph-daemon/ceph-daemon b/src/ceph-daemon/ceph-daemon index 63c46567963..2e62a17980b 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...') -- 2.39.5