]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph-daemon: append newline before public key string 31788/head
authorRicardo Dias <rdias@suse.com>
Thu, 21 Nov 2019 15:01:31 +0000 (15:01 +0000)
committerRicardo Dias <rdias@suse.com>
Thu, 21 Nov 2019 16:31:13 +0000 (16:31 +0000)
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 <rdias@suse.com>
src/ceph-daemon/ceph-daemon

index 63c46567963d10258d5457a013458dd784ab8dda..2e62a17980b07985c191f453460470c10d819b15 100755 (executable)
@@ -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...')