Skip to content

Commit 067647b

Browse files
author
Dean Troyer
committed
Temp work around for missing select_auth_plugin()
These were removed prematurely from osc-lib (by me) but the real fix in https://review.openstack.org/329189 is having racy functional test issues that may be related to osc-lib, so let's clear this up while we fix that... Change-Id: I8f67466967751fdf6fd24ae1b16ccee2aec52323
1 parent 5a21eb2 commit 067647b

1 file changed

Lines changed: 74 additions & 2 deletions

File tree

openstackclient/common/clientmanager.py

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import pkg_resources
2121
import sys
2222

23+
from keystoneauth1.loading import base
2324
from osc_lib.api import auth
2425
from osc_lib import exceptions
2526
from oslo_utils import strutils
@@ -37,6 +38,77 @@
3738
USER_AGENT = 'python-openstackclient'
3839

3940

41+
# NOTE(dtroyer): Bringing back select_auth_plugin() and build_auth_params()
42+
# temporarily because osc-lib 0.3.0 removed it a wee bit early
43+
def select_auth_plugin(options):
44+
"""Pick an auth plugin based on --os-auth-type or other options"""
45+
46+
auth_plugin_name = None
47+
48+
# Do the token/url check first as this must override the default
49+
# 'password' set by os-client-config
50+
# Also, url and token are not copied into o-c-c's auth dict (yet?)
51+
if options.auth.get('url') and options.auth.get('token'):
52+
# service token authentication
53+
auth_plugin_name = 'token_endpoint'
54+
elif options.auth_type in auth.PLUGIN_LIST:
55+
# A direct plugin name was given, use it
56+
auth_plugin_name = options.auth_type
57+
elif options.auth.get('username'):
58+
if options.identity_api_version == '3':
59+
auth_plugin_name = 'v3password'
60+
elif options.identity_api_version.startswith('2'):
61+
auth_plugin_name = 'v2password'
62+
else:
63+
# let keystoneauth figure it out itself
64+
auth_plugin_name = 'password'
65+
elif options.auth.get('token'):
66+
if options.identity_api_version == '3':
67+
auth_plugin_name = 'v3token'
68+
elif options.identity_api_version.startswith('2'):
69+
auth_plugin_name = 'v2token'
70+
else:
71+
# let keystoneauth figure it out itself
72+
auth_plugin_name = 'token'
73+
else:
74+
# The ultimate default is similar to the original behaviour,
75+
# but this time with version discovery
76+
auth_plugin_name = 'password'
77+
LOG.debug("Auth plugin %s selected", auth_plugin_name)
78+
return auth_plugin_name
79+
80+
81+
def build_auth_params(auth_plugin_name, cmd_options):
82+
if auth_plugin_name:
83+
LOG.debug('auth_type: %s', auth_plugin_name)
84+
auth_plugin_loader = base.get_plugin_loader(auth_plugin_name)
85+
auth_params = {
86+
opt.dest: opt.default
87+
for opt in base.get_plugin_options(auth_plugin_name)
88+
}
89+
auth_params.update(dict(cmd_options.auth))
90+
# grab tenant from project for v2.0 API compatibility
91+
if auth_plugin_name.startswith("v2"):
92+
if 'project_id' in auth_params:
93+
auth_params['tenant_id'] = auth_params['project_id']
94+
del auth_params['project_id']
95+
if 'project_name' in auth_params:
96+
auth_params['tenant_name'] = auth_params['project_name']
97+
del auth_params['project_name']
98+
else:
99+
LOG.debug('no auth_type')
100+
# delay the plugin choice, grab every option
101+
auth_plugin_loader = None
102+
auth_params = dict(cmd_options.auth)
103+
plugin_options = set(
104+
[o.replace('-', '_') for o in auth.get_options_list()]
105+
)
106+
for option in plugin_options:
107+
LOG.debug('fetching option %s', option)
108+
auth_params[option] = getattr(cmd_options.auth, option, None)
109+
return (auth_plugin_loader, auth_params)
110+
111+
40112
class ClientCache(object):
41113
"""Descriptor class for caching created client handles."""
42114

@@ -193,7 +265,7 @@ def setup_auth(self):
193265

194266
# If no auth type is named by the user, select one based on
195267
# the supplied options
196-
self.auth_plugin_name = auth.select_auth_plugin(self._cli_options)
268+
self.auth_plugin_name = select_auth_plugin(self._cli_options)
197269

198270
# Basic option checking to avoid unhelpful error messages
199271
auth.check_valid_authentication_options(self._cli_options,
@@ -205,7 +277,7 @@ def setup_auth(self):
205277
not self._cli_options.auth.get('password')):
206278
self._cli_options.auth['password'] = self._pw_callback()
207279

208-
(auth_plugin, self._auth_params) = auth.build_auth_params(
280+
(auth_plugin, self._auth_params) = build_auth_params(
209281
self.auth_plugin_name,
210282
self._cli_options,
211283
)

0 commit comments

Comments
 (0)