Merge "Adds tty password entry for glanceclient"
This commit is contained in:
@@ -21,6 +21,7 @@ from __future__ import print_function
|
|||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import copy
|
import copy
|
||||||
|
import getpass
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
@@ -448,10 +449,21 @@ class OpenStackImagesShell(object):
|
|||||||
"env[OS_USERNAME]"))
|
"env[OS_USERNAME]"))
|
||||||
|
|
||||||
if not args.os_password:
|
if not args.os_password:
|
||||||
raise exc.CommandError(
|
# No password, If we've got a tty, try prompting for it
|
||||||
_("You must provide a password via"
|
if hasattr(sys.stdin, 'isatty') and sys.stdin.isatty():
|
||||||
" either --os-password or "
|
# Check for Ctl-D
|
||||||
"env[OS_PASSWORD]"))
|
try:
|
||||||
|
args.os_password = getpass.getpass('OS Password: ')
|
||||||
|
except EOFError:
|
||||||
|
pass
|
||||||
|
# No password because we didn't have a tty or the
|
||||||
|
# user Ctl-D when prompted.
|
||||||
|
if not args.os_password:
|
||||||
|
raise exc.CommandError(
|
||||||
|
_("You must provide a password via "
|
||||||
|
"either --os-password, "
|
||||||
|
"env[OS_PASSWORD], "
|
||||||
|
"or prompted response"))
|
||||||
|
|
||||||
# Validate password flow auth
|
# Validate password flow auth
|
||||||
project_info = (args.os_tenant_name or
|
project_info = (args.os_tenant_name or
|
||||||
|
@@ -18,6 +18,7 @@ import argparse
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
import fixtures
|
||||||
import mock
|
import mock
|
||||||
import six
|
import six
|
||||||
|
|
||||||
@@ -67,6 +68,11 @@ class ShellTest(utils.TestCase):
|
|||||||
# expected auth plugin to invoke
|
# expected auth plugin to invoke
|
||||||
auth_plugin = 'keystoneclient.auth.identity.v2.Password'
|
auth_plugin = 'keystoneclient.auth.identity.v2.Password'
|
||||||
|
|
||||||
|
# Patch os.environ to avoid required auth info
|
||||||
|
def make_env(self, exclude=None, fake_env=FAKE_V2_ENV):
|
||||||
|
env = dict((k, v) for k, v in fake_env.items() if k != exclude)
|
||||||
|
self.useFixture(fixtures.MonkeyPatch('os.environ', env))
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(ShellTest, self).setUp()
|
super(ShellTest, self).setUp()
|
||||||
global _old_env
|
global _old_env
|
||||||
@@ -246,6 +252,27 @@ class ShellTest(utils.TestCase):
|
|||||||
glance_shell.main(args.split())
|
glance_shell.main(args.split())
|
||||||
self._assert_auth_plugin_args(mock_auth_plugin)
|
self._assert_auth_plugin_args(mock_auth_plugin)
|
||||||
|
|
||||||
|
@mock.patch('sys.stdin', side_effect=mock.MagicMock)
|
||||||
|
@mock.patch('getpass.getpass', return_value='password')
|
||||||
|
def test_password_prompted_with_v2(self, mock_getpass, mock_stdin):
|
||||||
|
glance_shell = openstack_shell.OpenStackImagesShell()
|
||||||
|
self.make_env(exclude='OS_PASSWORD')
|
||||||
|
# We will get a Connection Refused because there is no keystone.
|
||||||
|
self.assertRaises(ks_exc.ConnectionRefused,
|
||||||
|
glance_shell.main, ['image-list'])
|
||||||
|
# Make sure we are actually prompted.
|
||||||
|
mock_getpass.assert_called_with('OS Password: ')
|
||||||
|
|
||||||
|
@mock.patch('sys.stdin', side_effect=mock.MagicMock)
|
||||||
|
@mock.patch('getpass.getpass', side_effect=EOFError)
|
||||||
|
def test_password_prompted_ctrlD_with_v2(self, mock_getpass, mock_stdin):
|
||||||
|
glance_shell = openstack_shell.OpenStackImagesShell()
|
||||||
|
self.make_env(exclude='OS_PASSWORD')
|
||||||
|
# We should get Command Error because we mock Ctl-D.
|
||||||
|
self.assertRaises(exc.CommandError, glance_shell.main, ['image-list'])
|
||||||
|
# Make sure we are actually prompted.
|
||||||
|
mock_getpass.assert_called_with('OS Password: ')
|
||||||
|
|
||||||
|
|
||||||
class ShellTestWithKeystoneV3Auth(ShellTest):
|
class ShellTestWithKeystoneV3Auth(ShellTest):
|
||||||
# auth environment to use
|
# auth environment to use
|
||||||
|
Reference in New Issue
Block a user