Skip to content

Commit b9e50a7

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Add helpful messages when authN'ing with password"
2 parents ee62718 + fff4a1c commit b9e50a7

4 files changed

Lines changed: 31 additions & 4 deletions

File tree

‎openstackclient/api/auth.py‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
from openstackclient.common import exceptions as exc
2626
from openstackclient.common import utils
27+
from openstackclient.i18n import _
2728

2829

2930
LOG = logging.getLogger(__name__)
@@ -122,6 +123,25 @@ def build_auth_params(auth_plugin_name, cmd_options):
122123
return (auth_plugin_class, auth_params)
123124

124125

126+
def check_valid_auth_options(options, auth_plugin_name):
127+
"""Perform basic option checking, provide helpful error messages"""
128+
129+
msg = ''
130+
if auth_plugin_name.endswith('password'):
131+
if not options.os_username:
132+
msg += _('Set a username with --os-username or OS_USERNAME\n')
133+
if not options.os_auth_url:
134+
msg += _('Set an authentication URL, with --os-auth-url or'
135+
' OS_AUTH_URL\n')
136+
if (not options.os_project_id and not options.os_domain_id and not
137+
options.os_domain_name and not options.os_project_name):
138+
msg += _('Set a scope, such as a project or domain, with '
139+
'--os-project-name or OS_PROJECT_NAME')
140+
141+
if msg:
142+
raise exc.CommandError('Missing parameter(s): \n%s' % msg)
143+
144+
125145
def build_auth_plugins_option_parser(parser):
126146
"""Auth plugins options builder
127147
@@ -140,7 +160,7 @@ def build_auth_plugins_option_parser(parser):
140160
' (Env: OS_AUTH_TYPE)',
141161
choices=available_plugins
142162
)
143-
# make sur we catch old v2.0 env values
163+
# make sure we catch old v2.0 env values
144164
envs = {
145165
'OS_PROJECT_NAME': utils.env(
146166
'OS_PROJECT_NAME',

‎openstackclient/common/clientmanager.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,16 @@ def __init__(
7474
:param pw_func:
7575
Callback function for asking the user for a password. The function
7676
takes an optional string for the prompt ('Password: ' on None) and
77-
returns a string containig the password
77+
returns a string containing the password
7878
"""
7979

8080
# If no auth type is named by the user, select one based on
8181
# the supplied options
8282
self.auth_plugin_name = auth.select_auth_plugin(auth_options)
8383

84+
# Basic option checking to avoid unhelpful error messages
85+
auth.check_valid_auth_options(auth_options, self.auth_plugin_name)
86+
8487
# Horrible hack alert...must handle prompt for null password if
8588
# password auth is requested.
8689
if (self.auth_plugin_name.endswith('password') and

‎openstackclient/tests/common/test_clientmanager.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ def test_client_manager_password(self):
127127
client_manager = clientmanager.ClientManager(
128128
auth_options=FakeOptions(os_auth_url=fakes.AUTH_URL,
129129
os_username=fakes.USERNAME,
130-
os_password=fakes.PASSWORD),
130+
os_password=fakes.PASSWORD,
131+
os_project_name=fakes.PROJECT_NAME),
131132
api_version=API_VERSION,
132133
verify=False,
133134
)
@@ -184,6 +185,7 @@ def test_client_manager_password_verify_ca(self):
184185
auth_options=FakeOptions(os_auth_url=fakes.AUTH_URL,
185186
os_username=fakes.USERNAME,
186187
os_password=fakes.PASSWORD,
188+
os_project_name=fakes.PROJECT_NAME,
187189
os_auth_type='v2password'),
188190
api_version=API_VERSION,
189191
verify='cafile',
@@ -219,7 +221,8 @@ def test_client_manager_select_auth_plugin(self):
219221
# test password auth
220222
params = dict(os_auth_url=fakes.AUTH_URL,
221223
os_username=fakes.USERNAME,
222-
os_password=fakes.PASSWORD)
224+
os_password=fakes.PASSWORD,
225+
os_project_name=fakes.PROJECT_NAME)
223226
self._select_auth_plugin(params, '2.0', 'v2password')
224227
self._select_auth_plugin(params, '3', 'v3password')
225228
self._select_auth_plugin(params, 'XXX', 'password')

‎openstackclient/tests/fakes.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
AUTH_URL = "http://0.0.0.0"
2626
USERNAME = "itchy"
2727
PASSWORD = "scratchy"
28+
PROJECT_NAME = "poochie"
2829

2930
TEST_RESPONSE_DICT = fixture.V2Token(token_id=AUTH_TOKEN,
3031
user_name=USERNAME)

0 commit comments

Comments
 (0)