Skip to content

Commit 6fb1a4e

Browse files
author
Dean Troyer
committed
More identity client config
* move auth option checking back to OpenStackShell() to keep the shell-level interaction at that level; add checking for token flow options * make identity.client.make_client() configure keystoneclient.v2_0.Client() properly for both password flow and token flow auth * eliminated ClientManager.init_token(), set _service_catalog in __init__() * compute client handles token flow Change-Id: I42481b5424489387798c4ec6d3e2a723ab1e6067
1 parent a7da2b8 commit 6fb1a4e

4 files changed

Lines changed: 68 additions & 47 deletions

File tree

‎openstackclient/common/clientmanager.py‎

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ def __init__(self, factory):
2121
def __get__(self, instance, owner):
2222
# Tell the ClientManager to login to keystone
2323
if self._handle is None:
24-
instance.init_token()
2524
self._handle = self.factory(instance)
2625
return self._handle
2726

@@ -30,9 +29,7 @@ class ClientManager(object):
3029
"""Manages access to API clients, including authentication.
3130
"""
3231

33-
# Identity client is instantiated in init_token()
34-
# otherwise we have a recursion problem
35-
identity = None
32+
identity = ClientCache(identity_client.make_client)
3633
compute = ClientCache(compute_client.make_client)
3734

3835
def __init__(self, token=None, url=None,
@@ -55,40 +52,16 @@ def __init__(self, token=None, url=None,
5552
self._identity_api_version = identity_api_version
5653
self._compute_api_version = compute_api_version
5754
self._image_api_version = image_api_version
55+
self._service_catalog = None
5856

59-
def init_token(self):
60-
"""Return the auth token and endpoint.
61-
"""
62-
if self._token:
63-
LOG.debug('using existing auth token')
64-
return
65-
66-
LOG.debug('validating authentication options')
67-
if not self._username:
68-
raise exc.CommandError(
69-
"You must provide a username via"
70-
" either --os-username or env[OS_USERNAME]")
71-
72-
if not self._password:
73-
raise exc.CommandError(
74-
"You must provide a password via"
75-
" either --os-password or env[OS_PASSWORD]")
76-
77-
if not (self._tenant_id or self._tenant_name):
78-
raise exc.CommandError(
79-
"You must provide a tenant_id via"
80-
" either --os-tenant-id or via env[OS_TENANT_ID]")
57+
# Create the identity client
58+
self.identity
8159

82-
if not self._auth_url:
83-
raise exc.CommandError(
84-
"You must provide an auth url via"
85-
" either --os-auth-url or via env[OS_AUTH_URL]")
60+
if not self._url:
61+
# Populate other password flow attributes
62+
self._token = self.identity.auth_token
63+
self._service_catalog = self.identity.service_catalog
8664

87-
# Get an Identity client and keep a token and catalog
88-
if not self.identity:
89-
self.identity = identity_client.make_client(self)
90-
self._token = self.identity.auth_token
91-
self._service_catalog = self.identity.service_catalog
9265
return
9366

9467
def get_endpoint_for_service_type(self, service_type):

‎openstackclient/compute/client.py‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@ def make_client(instance):
2828
)
2929

3030
# Populate the Nova client to skip another auth query to Identity
31-
client.client.management_url = instance.get_endpoint_for_service_type(
32-
'compute')
33-
client.client.service_catalog = instance._service_catalog
31+
if instance._url:
32+
# token flow
33+
client.client.management_url = instance._url
34+
else:
35+
# password flow
36+
client.client.management_url = instance.get_endpoint_for_service_type(
37+
'compute')
38+
client.client.service_catalog = instance._service_catalog
3439
client.client.auth_token = instance._token
3540
return client

‎openstackclient/identity/client.py‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,20 @@
88
def make_client(instance):
99
"""Returns an identity service client.
1010
"""
11-
LOG.debug('instantiating identity client')
12-
client = identity_client.Client(
13-
username=instance._username,
14-
password=instance._password,
15-
tenant_name=instance._tenant_name,
16-
tenant_id=instance._tenant_id,
17-
auth_url=instance._auth_url,
18-
region_name=instance._region_name,
19-
)
11+
if instance._url:
12+
LOG.debug('instantiating identity client: token flow')
13+
client = identity_client.Client(
14+
endpoint=instance._url,
15+
token=instance._token,
16+
)
17+
else:
18+
LOG.debug('instantiating identity client: password flow')
19+
client = identity_client.Client(
20+
username=instance._username,
21+
password=instance._password,
22+
tenant_name=instance._tenant_name,
23+
tenant_id=instance._tenant_id,
24+
auth_url=instance._auth_url,
25+
region_name=instance._region_name,
26+
)
2027
return client

‎openstackclient/shell.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from cliff.commandmanager import CommandManager
2828

2929
from openstackclient.common import clientmanager
30+
from openstackclient.common import exceptions as exc
3031
from openstackclient.common import utils
3132

3233

@@ -141,6 +142,41 @@ def initialize_app(self):
141142
'image': self.options.os_image_api_version,
142143
}
143144

145+
self.log.debug('validating authentication options')
146+
if self.options.os_token or self.options.os_url:
147+
# Token flow auth takes priority
148+
if not self.options.os_token:
149+
raise exc.CommandError(
150+
"You must provide a token via"
151+
" either --os-token or env[OS_TOKEN]")
152+
153+
if not self.options.os_url:
154+
raise exc.CommandError(
155+
"You must provide a service URL via"
156+
" either --os-url or env[OS_URL]")
157+
158+
else:
159+
# Validate password flow auth
160+
if not self.options.os_username:
161+
raise exc.CommandError(
162+
"You must provide a username via"
163+
" either --os-username or env[OS_USERNAME]")
164+
165+
if not self.options.os_password:
166+
raise exc.CommandError(
167+
"You must provide a password via"
168+
" either --os-password or env[OS_PASSWORD]")
169+
170+
if not (self.options.os_tenant_id or self.options.os_tenant_name):
171+
raise exc.CommandError(
172+
"You must provide a tenant_id via"
173+
" either --os-tenant-id or via env[OS_TENANT_ID]")
174+
175+
if not self.options.os_auth_url:
176+
raise exc.CommandError(
177+
"You must provide an auth url via"
178+
" either --os-auth-url or via env[OS_AUTH_URL]")
179+
144180
self.client_manager = clientmanager.ClientManager(
145181
token=self.options.os_token,
146182
url=self.options.os_url,

0 commit comments

Comments
 (0)