Use metadata service username for sshpublicckeys

SetUserSSHPublicKeysPlugin needs to use
the metadata service provided admin username (if provided),
to be consistent with the other plugins.

Fixes https://github.com/cloudbase/cloudbase-init/issues/22

Co-Authored-By: Rui Lopes <rgl@ruilopes.com>
Change-Id: Ie0df53a8aaaa15e9c82c620159bc846bca649b1b
This commit is contained in:
Adrian Vladu 2019-12-13 18:21:38 +02:00
parent b55774cbd8
commit ddbdda0b58
2 changed files with 15 additions and 3 deletions

View File

@ -34,7 +34,7 @@ class SetUserSSHPublicKeysPlugin(base.BasePlugin):
LOG.debug('Public keys not found in metadata')
return base.PLUGIN_EXECUTION_DONE, False
username = CONF.username
username = service.get_admin_username() or CONF.username
osutils = osutils_factory.get_os_utils()
user_home = osutils.get_user_home(username)

View File

@ -39,11 +39,18 @@ class SetUserSSHPublicKeysPluginTests(unittest.TestCase):
@mock.patch('os.path')
@mock.patch('os.makedirs')
def _test_execute(self, mock_os_makedirs, mock_os_path,
mock_get_os_utils, user_home):
mock_get_os_utils, user_home,
metadata_provided_username=False):
mock_service = mock.MagicMock()
mock_osutils = mock.MagicMock()
fake_shared_data = 'fake data'
mock_service.get_public_keys.return_value = self.fake_data
fake_username = 'fake_username'
if metadata_provided_username:
fake_username = mock.MagicMock()
mock_service.get_admin_username.return_value = fake_username
else:
mock_service.get_admin_username.return_value = None
mock_get_os_utils.return_value = mock_osutils
mock_osutils.get_user_home.return_value = user_home
mock_os_path.exists.return_value = False
@ -60,7 +67,7 @@ class SetUserSSHPublicKeysPluginTests(unittest.TestCase):
fake_shared_data)
mock_service.get_public_keys.assert_called_with()
mock_osutils.get_user_home.assert_called_with(
'fake_username')
fake_username)
self.assertEqual(2, mock_os_path.join.call_count)
mock_os_makedirs.assert_called_once_with(mock_os_path.join())
@ -83,3 +90,8 @@ class SetUserSSHPublicKeysPluginTests(unittest.TestCase):
expected_logging = ['Public keys not found in metadata']
self.assertEqual(expected_logging, snatcher.output)
def test_execute_with_user_provided_by_metadata(self):
fake_user_home = os.path.join('fake', 'home')
self._test_execute(user_home=fake_user_home,
metadata_provided_username=True)