Merge "Enable password set in case of rescued instances"

This commit is contained in:
Jenkins 2015-02-12 15:45:07 +00:00 committed by Gerrit Code Review
commit 4ed717f7ff
2 changed files with 27 additions and 23 deletions

View File

@ -70,6 +70,11 @@ class SetUserPasswordPlugin(base.BasePlugin):
return password
def _set_metadata_password(self, password, service):
if service.is_password_set:
LOG.debug('User\'s password already set in the instance metadata '
'and it cannot be updated in the instance metadata')
return True
else:
ssh_pub_key = self._get_ssh_public_key(service)
if ssh_pub_key:
enc_password_b64 = self._encrypt_password(ssh_pub_key,
@ -91,20 +96,17 @@ class SetUserPasswordPlugin(base.BasePlugin):
user_name = shared_data.get(constants.SHARED_DATA_USERNAME,
CONF.username)
if service.can_post_password and service.is_password_set:
LOG.debug('User\'s password already set in the instance metadata')
else:
osutils = osutils_factory.get_os_utils()
if osutils.user_exists(user_name):
password = self._set_password(service, osutils, user_name)
LOG.info('Password succesfully updated for user %s' % user_name)
# TODO(alexpilotti): encrypt with DPAPI
shared_data[constants.SHARED_DATA_PASSWORD] = password
if not service.can_post_password:
LOG.info('Cannot set the password in the metadata as it '
'is not supported by this service')
return (base.PLUGIN_EXECUTION_DONE, False)
LOG.info('Cannot set the password in the metadata as it is '
'not supported by this service')
else:
self._set_metadata_password(password, service)
return (base.PLUGIN_EXECUTE_ON_NEXT_BOOT, False)
return (base.PLUGIN_EXECUTION_DONE, False)

View File

@ -101,6 +101,8 @@ class SetUserPasswordPluginTests(unittest.TestCase):
mock_get_key.return_value = ssh_pub_key
mock_encrypt_password.return_value = 'encrypted password'
mock_service.post_password.return_value = 'value'
mock_service.can_post_password = True
mock_service.is_password_set = False
response = self._setpassword_plugin._set_metadata_password(
fake_passw0rd, mock_service)
if ssh_pub_key is None:
@ -160,4 +162,4 @@ class SetUserPasswordPluginTests(unittest.TestCase):
'fake username')
mock_set_metadata_password.assert_called_once_with('fake password',
mock_service)
self.assertEqual((2, False), response)
self.assertEqual((1, False), response)