Skip to content

Commit

Permalink
ssh_util: handle non-default AuthorizedKeysFile config (#586)
Browse files Browse the repository at this point in the history
The following commit merged all ssh keys into a default user file
`~/.ssh/authorized_keys` in sshd_config had multiple files configured for
AuthorizedKeysFile:

commit f1094b1
Author: Eduardo Otubo <otubo@redhat.com>
Date:   Thu Dec 5 17:37:35 2019 +0100

    Multiple file fix for AuthorizedKeysFile config (#60)

This commit ignored the case when sshd_config would have a single file for
AuthorizedKeysFile, but a non default configuration, for example
`~/.ssh/authorized_keys_foobar`. In this case cloud-init would grab all keys
from this file and write a new one, the default `~/.ssh/authorized_keys`
causing the bug.

rhbz: #1862967

Signed-off-by: Eduardo Otubo <otubo@redhat.com>
  • Loading branch information
otubo committed Oct 20, 2020
1 parent 5a7f681 commit b0e7381
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions cloudinit/ssh_util.py
Expand Up @@ -262,13 +262,13 @@ def extract_authorized_keys(username, sshd_cfg_file=DEF_SSHD_CFG):

except (IOError, OSError):
# Give up and use a default key filename
auth_key_fns[0] = default_authorizedkeys_file
auth_key_fns.append(default_authorizedkeys_file)
util.logexc(LOG, "Failed extracting 'AuthorizedKeysFile' in SSH "
"config from %r, using 'AuthorizedKeysFile' file "
"%r instead", DEF_SSHD_CFG, auth_key_fns[0])

# always store all the keys in the user's private file
return (default_authorizedkeys_file, parse_authorized_keys(auth_key_fns))
# always store all the keys in the first file configured on sshd_config
return (auth_key_fns[0], parse_authorized_keys(auth_key_fns))


def setup_user_keys(keys, username, options=None):
Expand Down
6 changes: 3 additions & 3 deletions tests/unittests/test_sshutil.py
Expand Up @@ -593,7 +593,7 @@ def test_multiple_authorizedkeys_file_order1(self, m_getpwnam):
fpw.pw_name, sshd_config)
content = ssh_util.update_authorized_keys(auth_key_entries, [])

self.assertEqual("%s/.ssh/authorized_keys" % fpw.pw_dir, auth_key_fn)
self.assertEqual(authorized_keys, auth_key_fn)
self.assertTrue(VALID_CONTENT['rsa'] in content)
self.assertTrue(VALID_CONTENT['dsa'] in content)

Expand All @@ -610,15 +610,15 @@ def test_multiple_authorizedkeys_file_order2(self, m_getpwnam):
sshd_config = self.tmp_path('sshd_config')
util.write_file(
sshd_config,
"AuthorizedKeysFile %s %s" % (authorized_keys, user_keys)
"AuthorizedKeysFile %s %s" % (user_keys, authorized_keys)
)

(auth_key_fn, auth_key_entries) = ssh_util.extract_authorized_keys(
fpw.pw_name, sshd_config
)
content = ssh_util.update_authorized_keys(auth_key_entries, [])

self.assertEqual("%s/.ssh/authorized_keys" % fpw.pw_dir, auth_key_fn)
self.assertEqual(user_keys, auth_key_fn)
self.assertTrue(VALID_CONTENT['rsa'] in content)
self.assertTrue(VALID_CONTENT['dsa'] in content)

Expand Down

0 comments on commit b0e7381

Please sign in to comment.