Skip to content

Commit 6fe687f

Browse files
author
Dean Troyer
committed
Delay authentication to handle commands that do not require it
* Move the auth to OpenStackShell.prepare_to_run_command() and skip it if the command's auth_required == False * Default auth_required = True for all commands * Do authentication up-front for interactive use as OpenStackShell.prepare_to_run_command() is not called Change-Id: Id330092f242af624f430d469882d15f4a22f4e37
1 parent 8898e02 commit 6fe687f

1 file changed

Lines changed: 17 additions & 11 deletions

File tree

‎openstackclient/shell.py‎

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import sys
2323

2424
from cliff import app
25+
from cliff import command
2526
from cliff import help
2627

2728
import openstackclient
@@ -64,6 +65,11 @@ class OpenStackShell(app.App):
6465
log = logging.getLogger(__name__)
6566

6667
def __init__(self):
68+
# Patch command.Command to add a default auth_required = True
69+
command.Command.auth_required = True
70+
# But not help
71+
help.HelpCommand.auth_required = False
72+
6773
super(OpenStackShell, self).__init__(
6874
description=__doc__.strip(),
6975
version=openstackclient.__version__,
@@ -383,27 +389,27 @@ def initialize_app(self, argv):
383389
# Set up common client session
384390
self.restapi = restapi.RESTApi()
385391

386-
# If the user is not asking for help, make sure they
387-
# have given us auth.
388-
cmd_name = None
389-
if argv:
390-
cmd_info = self.command_manager.find_command(argv)
391-
cmd_factory, cmd_name, sub_argv = cmd_info
392-
if self.interactive_mode or cmd_name != 'help':
393-
self.authenticate_user()
394-
self.restapi.set_auth(self.client_manager.identity.auth_token)
395-
396392
def prepare_to_run_command(self, cmd):
397393
"""Set up auth and API versions"""
398394
self.log.debug('prepare_to_run_command %s', cmd.__class__.__name__)
399-
self.log.debug("api: %s" % cmd.api if hasattr(cmd, 'api') else None)
395+
396+
if cmd.auth_required:
397+
self.authenticate_user()
398+
self.restapi.set_auth(self.client_manager.identity.auth_token)
400399
return
401400

402401
def clean_up(self, cmd, result, err):
403402
self.log.debug('clean_up %s', cmd.__class__.__name__)
404403
if err:
405404
self.log.debug('got an error: %s', err)
406405

406+
def interact(self):
407+
# NOTE(dtroyer): Maintain the old behaviour for interactive use as
408+
# this path does not call prepare_to_run_command()
409+
self.authenticate_user()
410+
self.restapi.set_auth(self.client_manager.identity.auth_token)
411+
super(OpenStackShell, self).interact()
412+
407413

408414
def main(argv=sys.argv[1:]):
409415
try:

0 commit comments

Comments
 (0)