Do not prompt for scope options with default scoped tokens · Ara4Sh/python-openstackclient@fe0c8e9 · GitHub
Skip to content

Commit fe0c8e9

Browse files
dolphstevemar
authored andcommitted
Do not prompt for scope options with default scoped tokens
This changes the scope validation to occur after a token has already been created. Previous flow: 1. Validate authentication options. 2. Validate authorization options if the command requires a scope. 3. Create a token (using authentication + authorization options) 4. Run command. This means that scope was being checked, even if a default scope was applied in step 3 by Keystone. New flow: 1. Validate authentication options. 2. Create token (using authentication + authorization options) 3 Validate authorization options if the command requires a scope and the token is not scoped. 4. Run command. Change-Id: Idae368a11249f425b14b891fc68b4176e2b3e981 Closes-Bug: 1592062
1 parent 1464c8a commit fe0c8e9

5 files changed

Lines changed: 49 additions & 33 deletions

File tree

openstackclient/api/auth.py

Lines changed: 18 additions & 18 deletions

openstackclient/common/clientmanager.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,8 @@ def __init__(
140140
# prior to dereferrencing auth_ref.
141141
self._auth_setup_completed = False
142142

143-
def setup_auth(self, required_scope=True):
144-
"""Set up authentication
145-
146-
:param required_scope: indicate whether a scoped token is required
143+
def setup_auth(self):
144+
"""Set up authentication.
147145
148146
This is deferred until authentication is actually attempted because
149147
it gets in the way of things that do not require auth.
@@ -157,9 +155,8 @@ def setup_auth(self, required_scope=True):
157155
self.auth_plugin_name = auth.select_auth_plugin(self._cli_options)
158156

159157
# Basic option checking to avoid unhelpful error messages
160-
auth.check_valid_auth_options(self._cli_options,
161-
self.auth_plugin_name,
162-
required_scope=required_scope)
158+
auth.check_valid_authentication_options(self._cli_options,
159+
self.auth_plugin_name)
163160

164161
# Horrible hack alert...must handle prompt for null password if
165162
# password auth is requested.
@@ -229,6 +226,20 @@ def setup_auth(self, required_scope=True):
229226

230227
self._auth_setup_completed = True
231228

229+
def validate_scope(self):
230+
if self._auth_ref.project_id is not None:
231+
# We already have a project scope.
232+
return
233+
if self._auth_ref.domain_id is not None:
234+
# We already have a domain scope.
235+
return
236+
237+
# We do not have a scoped token (and the user's default project scope
238+
# was not implied), so the client needs to be explicitly configured
239+
# with a scope.
240+
auth.check_valid_authorization_options(self._cli_options,
241+
self.auth_plugin_name)
242+
232243
@property
233244
def auth_ref(self):
234245
"""Dereference will trigger an auth if it hasn't already"""

openstackclient/shell.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,12 +443,12 @@ def prepare_to_run_command(self, cmd):
443443
cmd.__class__.__name__,
444444
)
445445
if cmd.auth_required:
446-
if hasattr(cmd, 'required_scope'):
446+
self.client_manager.setup_auth()
447+
if hasattr(cmd, 'required_scope') and cmd.required_scope:
447448
# let the command decide whether we need a scoped token
448-
self.client_manager.setup_auth(cmd.required_scope)
449+
self.client_manager.validate_scope()
449450
# Trigger the Identity client to initialize
450451
self.client_manager.auth_ref
451-
return
452452

453453
def clean_up(self, cmd, result, err):
454454
self.log.debug('clean_up %s: %s', cmd.__class__.__name__, err or '')

openstackclient/tests/common/test_clientmanager.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,8 @@ def test_client_manager_select_auth_plugin_failure(self):
356356
client_manager.setup_auth,
357357
)
358358

359-
@mock.patch('openstackclient.api.auth.check_valid_auth_options')
360-
def test_client_manager_auth_setup_once(self, check_auth_options_func):
359+
@mock.patch('openstackclient.api.auth.check_valid_authentication_options')
360+
def test_client_manager_auth_setup_once(self, check_authn_options_func):
361361
client_manager = clientmanager.ClientManager(
362362
cli_options=FakeOptions(
363363
auth=dict(
@@ -372,11 +372,11 @@ def test_client_manager_auth_setup_once(self, check_auth_options_func):
372372
)
373373
self.assertFalse(client_manager._auth_setup_completed)
374374
client_manager.setup_auth()
375-
self.assertTrue(check_auth_options_func.called)
375+
self.assertTrue(check_authn_options_func.called)
376376
self.assertTrue(client_manager._auth_setup_completed)
377377

378378
# now make sure we don't do auth setup the second time around
379379
# by checking whether check_valid_auth_options() gets called again
380-
check_auth_options_func.reset_mock()
380+
check_authn_options_func.reset_mock()
381381
client_manager.auth_ref
382-
check_auth_options_func.assert_not_called()
382+
check_authn_options_func.assert_not_called()
Lines changed: 5 additions & 0 deletions

0 commit comments

Comments
 (0)