Skip to content

Commit fff4a1c

Browse files
author
Steve Martinelli
committed
Add helpful messages when authN'ing with password
Setting up auth options can be complicated, and we currently don't do any checking before we build all our auth parameters to send off to keystoneclient. We should do some basic checking to guide new users. Change-Id: I9c88f1c9637b3870c151952ecc797aaf65be271a Closes-Bug: #1400531
1 parent 77097c6 commit fff4a1c

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
@@ -126,7 +126,8 @@ def test_client_manager_password(self):
126126
client_manager = clientmanager.ClientManager(
127127
auth_options=FakeOptions(os_auth_url=fakes.AUTH_URL,
128128
os_username=fakes.USERNAME,
129-
os_password=fakes.PASSWORD),
129+
os_password=fakes.PASSWORD,
130+
os_project_name=fakes.PROJECT_NAME),
130131
api_version=API_VERSION,
131132
verify=False,
132133
)
@@ -183,6 +184,7 @@ def test_client_manager_password_verify_ca(self):
183184
auth_options=FakeOptions(os_auth_url=fakes.AUTH_URL,
184185
os_username=fakes.USERNAME,
185186
os_password=fakes.PASSWORD,
187+
os_project_name=fakes.PROJECT_NAME,
186188
os_auth_type='v2password'),
187189
api_version=API_VERSION,
188190
verify='cafile',
@@ -218,7 +220,8 @@ def test_client_manager_select_auth_plugin(self):
218220
# test password auth
219221
params = dict(os_auth_url=fakes.AUTH_URL,
220222
os_username=fakes.USERNAME,
221-
os_password=fakes.PASSWORD)
223+
os_password=fakes.PASSWORD,
224+
os_project_name=fakes.PROJECT_NAME)
222225
self._select_auth_plugin(params, '2.0', 'v2password')
223226
self._select_auth_plugin(params, '3', 'v3password')
224227
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)