2011-10-25 16:50:08 -07:00
|
|
|
# Copyright 2010 Jacob Kaplan-Moss
|
2013-09-20 04:30:41 +08:00
|
|
|
# Copyright 2011 OpenStack Foundation
|
2011-10-25 16:50:08 -07:00
|
|
|
# Copyright 2011 Piston Cloud Computing, Inc.
|
|
|
|
# Copyright 2011 Nebula, Inc.
|
|
|
|
# All Rights Reserved.
|
2013-08-15 16:56:22 -07:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License. You may obtain
|
|
|
|
# a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
2011-10-25 16:50:08 -07:00
|
|
|
"""
|
|
|
|
OpenStack Client interface. Handles the REST calls and responses.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import logging
|
2014-01-17 20:13:24 +08:00
|
|
|
|
2014-10-14 17:49:17 -04:00
|
|
|
from oslo.serialization import jsonutils
|
2014-09-21 02:44:37 -04:00
|
|
|
import pkg_resources
|
2014-07-23 09:14:56 +10:00
|
|
|
import requests
|
2013-10-14 11:16:42 -07:00
|
|
|
from six.moves.urllib import parse as urlparse
|
2011-10-25 16:50:08 -07:00
|
|
|
|
2013-05-22 17:00:53 -04:00
|
|
|
try:
|
|
|
|
import pickle
|
2014-06-16 02:08:59 -04:00
|
|
|
|
2014-07-17 16:30:35 +02:00
|
|
|
# NOTE(sdague): The conditional keyring import needs to only
|
|
|
|
# trigger if it's a version of keyring that's supported in global
|
|
|
|
# requirements. Update _min and _bad when that changes.
|
|
|
|
keyring_v = pkg_resources.parse_version(
|
|
|
|
pkg_resources.get_distribution("keyring").version)
|
|
|
|
keyring_min = pkg_resources.parse_version('2.1')
|
|
|
|
keyring_bad = (pkg_resources.parse_version('3.3'),)
|
|
|
|
|
|
|
|
if keyring_v >= keyring_min and keyring_v not in keyring_bad:
|
|
|
|
import keyring
|
|
|
|
else:
|
|
|
|
keyring = None
|
|
|
|
except (ImportError, pkg_resources.DistributionNotFound):
|
2013-05-22 17:00:53 -04:00
|
|
|
keyring = None
|
|
|
|
pickle = None
|
|
|
|
|
2011-10-25 16:50:08 -07:00
|
|
|
# Python 2.5 compat fix
|
|
|
|
if not hasattr(urlparse, 'parse_qsl'):
|
|
|
|
import cgi
|
|
|
|
urlparse.parse_qsl = cgi.parse_qsl
|
|
|
|
|
|
|
|
|
2012-10-13 00:15:39 +00:00
|
|
|
from keystoneclient import access
|
2013-12-09 16:46:09 +10:00
|
|
|
from keystoneclient.auth import base
|
|
|
|
from keystoneclient import baseclient
|
2011-10-25 16:50:08 -07:00
|
|
|
from keystoneclient import exceptions
|
2013-09-30 10:35:55 +10:00
|
|
|
from keystoneclient import session as client_session
|
2014-02-28 13:31:00 +10:00
|
|
|
from keystoneclient import utils
|
2011-10-25 16:50:08 -07:00
|
|
|
|
|
|
|
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
2013-09-30 10:35:55 +10:00
|
|
|
# These variables are moved and using them via httpclient is deprecated.
|
|
|
|
# Maintain here for compatibility.
|
|
|
|
USER_AGENT = client_session.USER_AGENT
|
|
|
|
request = client_session.request
|
2013-07-31 16:00:58 +10:00
|
|
|
|
|
|
|
|
2014-07-23 09:14:56 +10:00
|
|
|
class _FakeRequestSession(object):
|
|
|
|
"""This object is a temporary hack that should be removed later.
|
|
|
|
|
|
|
|
Keystoneclient has a cyclical dependency with its managers which is
|
|
|
|
preventing it from being cleaned up correctly. This is always bad but when
|
|
|
|
we switched to doing connection pooling this object wasn't getting cleaned
|
|
|
|
either and so we had left over TCP connections hanging around.
|
|
|
|
|
|
|
|
Until we can fix the client cleanup we rollback the use of a requests
|
|
|
|
session and do individual connections like we used to.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def request(self, *args, **kwargs):
|
|
|
|
return requests.request(*args, **kwargs)
|
|
|
|
|
|
|
|
|
2013-12-09 16:46:09 +10:00
|
|
|
class HTTPClient(baseclient.Client, base.BaseAuthPlugin):
|
2011-10-25 16:50:08 -07:00
|
|
|
|
2014-02-19 15:35:46 +10:00
|
|
|
version = None
|
|
|
|
|
2014-02-28 13:31:00 +10:00
|
|
|
@utils.positional(enforcement=utils.positional.WARN)
|
2011-12-17 22:36:59 -08:00
|
|
|
def __init__(self, username=None, tenant_id=None, tenant_name=None,
|
2013-12-11 07:38:46 +10:00
|
|
|
password=None, auth_url=None, region_name=None, endpoint=None,
|
|
|
|
token=None, debug=False, auth_ref=None, use_keyring=False,
|
|
|
|
force_new_token=False, stale_duration=None, user_id=None,
|
|
|
|
user_domain_id=None, user_domain_name=None, domain_id=None,
|
|
|
|
domain_name=None, project_id=None, project_name=None,
|
|
|
|
project_domain_id=None, project_domain_name=None,
|
|
|
|
trust_id=None, session=None, **kwargs):
|
2013-01-11 21:56:24 -08:00
|
|
|
"""Construct a new http client
|
|
|
|
|
2013-02-13 22:52:05 -06:00
|
|
|
:param string user_id: User ID for authentication. (optional)
|
|
|
|
:param string username: Username for authentication. (optional)
|
|
|
|
:param string user_domain_id: User's domain ID for authentication.
|
|
|
|
(optional)
|
|
|
|
:param string user_domain_name: User's domain name for authentication.
|
|
|
|
(optional)
|
|
|
|
:param string password: Password for authentication. (optional)
|
|
|
|
:param string domain_id: Domain ID for domain scoping. (optional)
|
|
|
|
:param string domain_name: Domain name for domain scoping. (optional)
|
|
|
|
:param string project_id: Project ID for project scoping. (optional)
|
|
|
|
:param string project_name: Project name for project scoping.
|
|
|
|
(optional)
|
|
|
|
:param string project_domain_id: Project's domain ID for project
|
|
|
|
scoping. (optional)
|
|
|
|
:param string project_domain_name: Project's domain name for project
|
|
|
|
scoping. (optional)
|
|
|
|
:param string auth_url: Identity service endpoint for authorization.
|
|
|
|
:param string region_name: Name of a region to select when choosing an
|
|
|
|
endpoint from the service catalog.
|
2013-09-30 10:35:55 +10:00
|
|
|
:param integer timeout: DEPRECATED: use session. (optional)
|
2013-02-13 22:52:05 -06:00
|
|
|
:param string endpoint: A user-supplied endpoint URL for the identity
|
|
|
|
service. Lazy-authentication is possible for
|
|
|
|
API service calls if endpoint is set at
|
|
|
|
instantiation. (optional)
|
|
|
|
:param string token: Token for authentication. (optional)
|
2013-09-30 10:35:55 +10:00
|
|
|
:param string cacert: DEPRECATED: use session. (optional)
|
|
|
|
:param string key: DEPRECATED: use session. (optional)
|
|
|
|
:param string cert: DEPRECATED: use session. (optional)
|
|
|
|
:param boolean insecure: DEPRECATED: use session. (optional)
|
|
|
|
:param string original_ip: DEPRECATED: use session. (optional)
|
2013-12-17 15:30:51 +10:00
|
|
|
:param boolean debug: DEPRECATED: use logging configuration. (optional)
|
2013-02-13 22:52:05 -06:00
|
|
|
:param dict auth_ref: To allow for consumers of the client to manage
|
|
|
|
their own caching strategy, you may initialize a
|
|
|
|
client with a previously captured auth_reference
|
|
|
|
(token). If there are keyword arguments passed
|
|
|
|
that also exist in auth_ref, the value from the
|
|
|
|
argument will take precedence.
|
|
|
|
:param boolean use_keyring: Enables caching auth_ref into keyring.
|
|
|
|
default: False (optional)
|
|
|
|
:param boolean force_new_token: Keyring related parameter, forces
|
|
|
|
request for new token.
|
|
|
|
default: False (optional)
|
|
|
|
:param integer stale_duration: Gap in seconds to determine if token
|
|
|
|
from keyring is about to expire.
|
|
|
|
default: 30 (optional)
|
|
|
|
:param string tenant_name: Tenant name. (optional)
|
|
|
|
The tenant_name keyword argument is
|
|
|
|
deprecated, use project_name instead.
|
|
|
|
:param string tenant_id: Tenant id. (optional)
|
|
|
|
The tenant_id keyword argument is
|
|
|
|
deprecated, use project_id instead.
|
2013-08-02 11:45:38 +01:00
|
|
|
:param string trust_id: Trust ID for trust scoping. (optional)
|
2013-09-30 10:35:55 +10:00
|
|
|
:param object session: A Session object to be used for
|
|
|
|
communicating with the identity service.
|
2013-01-11 21:56:24 -08:00
|
|
|
|
|
|
|
"""
|
2012-11-14 05:38:16 +00:00
|
|
|
# set baseline defaults
|
2013-02-13 22:52:05 -06:00
|
|
|
self.user_id = None
|
2012-11-14 05:38:16 +00:00
|
|
|
self.username = None
|
2013-02-13 22:52:05 -06:00
|
|
|
self.user_domain_id = None
|
|
|
|
self.user_domain_name = None
|
|
|
|
|
|
|
|
self.domain_id = None
|
|
|
|
self.domain_name = None
|
|
|
|
|
|
|
|
self.project_id = None
|
|
|
|
self.project_name = None
|
|
|
|
self.project_domain_id = None
|
|
|
|
self.project_domain_name = None
|
|
|
|
|
2013-10-22 09:41:00 +10:00
|
|
|
self.region_name = None
|
2012-11-14 05:38:16 +00:00
|
|
|
self.auth_url = None
|
2013-11-20 12:40:13 +10:00
|
|
|
self._endpoint = None
|
|
|
|
self._management_url = None
|
2013-02-13 22:52:05 -06:00
|
|
|
|
2013-08-02 11:45:38 +01:00
|
|
|
self.trust_id = None
|
|
|
|
|
2012-11-14 05:38:16 +00:00
|
|
|
# if loading from a dictionary passed in via auth_ref,
|
|
|
|
# load values from AccessInfo parsing that dictionary
|
2013-02-13 22:52:05 -06:00
|
|
|
if auth_ref:
|
|
|
|
self.auth_ref = access.AccessInfo.factory(**auth_ref)
|
|
|
|
self.version = self.auth_ref.version
|
|
|
|
self.user_id = self.auth_ref.user_id
|
2012-10-13 00:15:39 +00:00
|
|
|
self.username = self.auth_ref.username
|
2013-02-13 22:52:05 -06:00
|
|
|
self.user_domain_id = self.auth_ref.user_domain_id
|
|
|
|
self.domain_id = self.auth_ref.domain_id
|
|
|
|
self.domain_name = self.auth_ref.domain_name
|
|
|
|
self.project_id = self.auth_ref.project_id
|
|
|
|
self.project_name = self.auth_ref.project_name
|
|
|
|
self.project_domain_id = self.auth_ref.project_domain_id
|
2012-11-14 05:38:16 +00:00
|
|
|
self.auth_url = self.auth_ref.auth_url[0]
|
2013-11-20 12:40:13 +10:00
|
|
|
self._management_url = self.auth_ref.management_url[0]
|
2013-11-22 11:14:49 +10:00
|
|
|
self.auth_token_from_user = self.auth_ref.auth_token
|
2013-08-02 11:45:38 +01:00
|
|
|
self.trust_id = self.auth_ref.trust_id
|
2013-10-22 09:41:00 +10:00
|
|
|
if self.auth_ref.has_service_catalog():
|
|
|
|
self.region_name = self.auth_ref.service_catalog.region_name
|
2013-02-13 22:52:05 -06:00
|
|
|
else:
|
|
|
|
self.auth_ref = None
|
|
|
|
|
2012-11-14 05:38:16 +00:00
|
|
|
# allow override of the auth_ref defaults from explicit
|
2012-10-13 00:15:39 +00:00
|
|
|
# values provided to the client
|
2013-02-13 22:52:05 -06:00
|
|
|
|
|
|
|
# apply deprecated variables first, so modern variables override them
|
2012-11-14 05:38:16 +00:00
|
|
|
if tenant_id:
|
2013-02-13 22:52:05 -06:00
|
|
|
self.project_id = tenant_id
|
2012-11-14 05:38:16 +00:00
|
|
|
if tenant_name:
|
2013-02-13 22:52:05 -06:00
|
|
|
self.project_name = tenant_name
|
|
|
|
|
|
|
|
# user-related attributes
|
|
|
|
self.password = password
|
|
|
|
if user_id:
|
|
|
|
self.user_id = user_id
|
|
|
|
if username:
|
|
|
|
self.username = username
|
|
|
|
if user_domain_id:
|
|
|
|
self.user_domain_id = user_domain_id
|
|
|
|
elif not (user_id or user_domain_name):
|
|
|
|
self.user_domain_id = 'default'
|
|
|
|
if user_domain_name:
|
|
|
|
self.user_domain_name = user_domain_name
|
|
|
|
|
|
|
|
# domain-related attributes
|
|
|
|
if domain_id:
|
|
|
|
self.domain_id = domain_id
|
|
|
|
if domain_name:
|
|
|
|
self.domain_name = domain_name
|
|
|
|
|
|
|
|
# project-related attributes
|
|
|
|
if project_id:
|
|
|
|
self.project_id = project_id
|
|
|
|
if project_name:
|
|
|
|
self.project_name = project_name
|
|
|
|
if project_domain_id:
|
|
|
|
self.project_domain_id = project_domain_id
|
|
|
|
elif not (project_id or project_domain_name):
|
|
|
|
self.project_domain_id = 'default'
|
|
|
|
if project_domain_name:
|
|
|
|
self.project_domain_name = project_domain_name
|
|
|
|
|
2013-08-02 11:45:38 +01:00
|
|
|
# trust-related attributes
|
|
|
|
if trust_id:
|
|
|
|
self.trust_id = trust_id
|
|
|
|
|
2013-02-13 22:52:05 -06:00
|
|
|
# endpoint selection
|
2012-11-14 05:38:16 +00:00
|
|
|
if auth_url:
|
|
|
|
self.auth_url = auth_url.rstrip('/')
|
|
|
|
if token:
|
2013-01-30 18:07:29 +01:00
|
|
|
self.auth_token_from_user = token
|
|
|
|
else:
|
|
|
|
self.auth_token_from_user = None
|
2012-11-14 05:38:16 +00:00
|
|
|
if endpoint:
|
2013-11-20 12:40:13 +10:00
|
|
|
self._endpoint = endpoint.rstrip('/')
|
2013-10-22 09:41:00 +10:00
|
|
|
if region_name:
|
|
|
|
self.region_name = region_name
|
2013-11-22 11:14:49 +10:00
|
|
|
self._auth_token = None
|
2013-02-13 22:52:05 -06:00
|
|
|
|
2013-09-30 10:35:55 +10:00
|
|
|
if not session:
|
2014-07-23 09:14:56 +10:00
|
|
|
kwargs['session'] = _FakeRequestSession()
|
2013-12-11 07:38:46 +10:00
|
|
|
session = client_session.Session.construct(kwargs)
|
2013-12-09 16:46:09 +10:00
|
|
|
session.auth = self
|
2013-09-30 10:35:55 +10:00
|
|
|
|
2013-12-09 16:46:09 +10:00
|
|
|
super(HTTPClient, self).__init__(session=session)
|
2012-11-16 17:43:05 -06:00
|
|
|
self.domain = ''
|
2012-10-13 00:15:39 +00:00
|
|
|
self.debug_log = debug
|
2012-08-16 18:18:22 -07:00
|
|
|
|
2012-11-08 16:32:17 -08:00
|
|
|
# keyring setup
|
2013-05-22 17:00:53 -04:00
|
|
|
if use_keyring and keyring is None:
|
|
|
|
_logger.warning('Failed to load keyring modules.')
|
|
|
|
self.use_keyring = use_keyring and keyring is not None
|
2012-11-08 16:32:17 -08:00
|
|
|
self.force_new_token = force_new_token
|
|
|
|
self.stale_duration = stale_duration or access.STALE_TOKEN_DURATION
|
|
|
|
self.stale_duration = int(self.stale_duration)
|
|
|
|
|
2013-12-09 16:46:09 +10:00
|
|
|
def get_token(self, session, **kwargs):
|
|
|
|
return self.auth_token
|
|
|
|
|
2013-01-30 18:07:29 +01:00
|
|
|
@property
|
|
|
|
def auth_token(self):
|
2013-11-22 11:14:49 +10:00
|
|
|
if self._auth_token:
|
|
|
|
return self._auth_token
|
2013-01-30 18:07:29 +01:00
|
|
|
if self.auth_ref:
|
|
|
|
if self.auth_ref.will_expire_soon(self.stale_duration):
|
|
|
|
self.authenticate()
|
|
|
|
return self.auth_ref.auth_token
|
2013-11-22 11:14:49 +10:00
|
|
|
if self.auth_token_from_user:
|
|
|
|
return self.auth_token_from_user
|
2013-01-30 18:07:29 +01:00
|
|
|
|
2013-12-05 16:32:56 +10:00
|
|
|
def get_endpoint(self, session, interface=None, **kwargs):
|
2014-07-04 10:41:35 +10:00
|
|
|
if interface == 'public' or interface is base.AUTH_INTERFACE:
|
2013-12-05 16:32:56 +10:00
|
|
|
return self.auth_url
|
|
|
|
else:
|
|
|
|
return self.management_url
|
|
|
|
|
2013-01-30 18:07:29 +01:00
|
|
|
@auth_token.setter
|
|
|
|
def auth_token(self, value):
|
2013-11-22 11:14:49 +10:00
|
|
|
"""Override the auth_token.
|
|
|
|
|
|
|
|
If an application sets auth_token explicitly then it will always be
|
|
|
|
used and override any past or future retrieved token.
|
|
|
|
"""
|
|
|
|
self._auth_token = value
|
2013-01-30 18:07:29 +01:00
|
|
|
|
|
|
|
@auth_token.deleter
|
2013-02-20 08:48:14 +01:00
|
|
|
def auth_token(self):
|
2013-11-22 11:14:49 +10:00
|
|
|
self._auth_token = None
|
|
|
|
self.auth_token_from_user = None
|
2013-01-30 18:07:29 +01:00
|
|
|
|
2013-02-13 22:52:05 -06:00
|
|
|
@property
|
|
|
|
def service_catalog(self):
|
|
|
|
"""Returns this client's service catalog."""
|
|
|
|
return self.auth_ref.service_catalog
|
|
|
|
|
|
|
|
def has_service_catalog(self):
|
|
|
|
"""Returns True if this client provides a service catalog."""
|
2014-06-17 19:17:10 -04:00
|
|
|
return self.auth_ref and self.auth_ref.has_service_catalog()
|
2013-02-13 22:52:05 -06:00
|
|
|
|
|
|
|
@property
|
|
|
|
def tenant_id(self):
|
|
|
|
"""Provide read-only backwards compatibility for tenant_id.
|
|
|
|
This is deprecated, use project_id instead.
|
|
|
|
"""
|
|
|
|
return self.project_id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def tenant_name(self):
|
|
|
|
"""Provide read-only backwards compatibility for tenant_name.
|
|
|
|
This is deprecated, use project_name instead.
|
|
|
|
"""
|
|
|
|
return self.project_name
|
|
|
|
|
2014-02-28 13:31:00 +10:00
|
|
|
@utils.positional(enforcement=utils.positional.WARN)
|
2012-11-08 16:32:17 -08:00
|
|
|
def authenticate(self, username=None, password=None, tenant_name=None,
|
2013-02-13 22:52:05 -06:00
|
|
|
tenant_id=None, auth_url=None, token=None,
|
|
|
|
user_id=None, domain_name=None, domain_id=None,
|
|
|
|
project_name=None, project_id=None, user_domain_id=None,
|
|
|
|
user_domain_name=None, project_domain_id=None,
|
2013-10-22 09:41:00 +10:00
|
|
|
project_domain_name=None, trust_id=None,
|
|
|
|
region_name=None):
|
2013-02-13 22:52:05 -06:00
|
|
|
"""Authenticate user.
|
2012-11-08 16:32:17 -08:00
|
|
|
|
|
|
|
Uses the data provided at instantiation to authenticate against
|
2013-02-13 22:52:05 -06:00
|
|
|
the Identity server. This may use either a username and password
|
2012-11-08 16:32:17 -08:00
|
|
|
or token for authentication. If a tenant name or id was provided
|
|
|
|
then the resulting authenticated client will be scoped to that
|
|
|
|
tenant and contain a service catalog of available endpoints.
|
|
|
|
|
|
|
|
With the v2.0 API, if a tenant name or ID is not provided, the
|
2013-02-13 22:52:05 -06:00
|
|
|
authentication token returned will be 'unscoped' and limited in
|
2012-11-08 16:32:17 -08:00
|
|
|
capabilities until a fully-scoped token is acquired.
|
|
|
|
|
2013-02-13 22:52:05 -06:00
|
|
|
With the v3 API, if a domain name or id was provided then the resulting
|
|
|
|
authenticated client will be scoped to that domain. If a project name
|
|
|
|
or ID is not provided, and the authenticating user has a default
|
|
|
|
project configured, the authentication token returned will be 'scoped'
|
|
|
|
to the default project. Otherwise, the authentication token returned
|
|
|
|
will be 'unscoped' and limited in capabilities until a fully-scoped
|
|
|
|
token is acquired.
|
|
|
|
|
2013-08-02 11:45:38 +01:00
|
|
|
With the v3 API, with the OS-TRUST extension enabled, the trust_id can
|
|
|
|
be provided to allow project-specific role delegation between users
|
|
|
|
|
2012-11-08 16:32:17 -08:00
|
|
|
If successful, sets the self.auth_ref and self.auth_token with
|
|
|
|
the returned token. If not already set, will also set
|
|
|
|
self.management_url from the details provided in the token.
|
|
|
|
|
|
|
|
:returns: ``True`` if authentication was successful.
|
|
|
|
:raises: AuthorizationFailure if unable to authenticate or validate
|
|
|
|
the existing authorization token
|
|
|
|
:raises: ValueError if insufficient parameters are used.
|
|
|
|
|
|
|
|
If keyring is used, token is retrieved from keyring instead.
|
|
|
|
Authentication will only be necessary if any of the following
|
|
|
|
conditions are met:
|
|
|
|
|
|
|
|
* keyring is not used
|
|
|
|
* if token is not found in keyring
|
|
|
|
* if token retrieved from keyring is expired or about to
|
|
|
|
expired (as determined by stale_duration)
|
|
|
|
* if force_new_token is true
|
|
|
|
|
|
|
|
"""
|
2013-04-30 17:34:14 +00:00
|
|
|
auth_url = auth_url or self.auth_url
|
2013-02-13 22:52:05 -06:00
|
|
|
user_id = user_id or self.user_id
|
2012-11-08 16:32:17 -08:00
|
|
|
username = username or self.username
|
|
|
|
password = password or self.password
|
2013-02-13 22:52:05 -06:00
|
|
|
|
|
|
|
user_domain_id = user_domain_id or self.user_domain_id
|
|
|
|
user_domain_name = user_domain_name or self.user_domain_name
|
|
|
|
domain_id = domain_id or self.domain_id
|
|
|
|
domain_name = domain_name or self.domain_name
|
|
|
|
project_id = project_id or tenant_id or self.project_id
|
|
|
|
project_name = project_name or tenant_name or self.project_name
|
|
|
|
project_domain_id = project_domain_id or self.project_domain_id
|
|
|
|
project_domain_name = project_domain_name or self.project_domain_name
|
2013-01-30 18:07:29 +01:00
|
|
|
|
2013-08-02 11:45:38 +01:00
|
|
|
trust_id = trust_id or self.trust_id
|
2013-10-22 09:41:00 +10:00
|
|
|
region_name = region_name or self.region_name
|
2013-08-02 11:45:38 +01:00
|
|
|
|
2013-01-30 18:07:29 +01:00
|
|
|
if not token:
|
|
|
|
token = self.auth_token_from_user
|
2013-05-28 08:48:44 -05:00
|
|
|
if (not token and self.auth_ref and not
|
|
|
|
self.auth_ref.will_expire_soon(self.stale_duration)):
|
2013-01-30 18:07:29 +01:00
|
|
|
token = self.auth_ref.auth_token
|
2012-11-08 16:32:17 -08:00
|
|
|
|
2013-02-13 22:52:05 -06:00
|
|
|
kwargs = {
|
|
|
|
'auth_url': auth_url,
|
|
|
|
'user_id': user_id,
|
|
|
|
'username': username,
|
|
|
|
'user_domain_id': user_domain_id,
|
|
|
|
'user_domain_name': user_domain_name,
|
|
|
|
'domain_id': domain_id,
|
|
|
|
'domain_name': domain_name,
|
|
|
|
'project_id': project_id,
|
|
|
|
'project_name': project_name,
|
|
|
|
'project_domain_id': project_domain_id,
|
|
|
|
'project_domain_name': project_domain_name,
|
2013-08-02 11:45:38 +01:00
|
|
|
'token': token,
|
|
|
|
'trust_id': trust_id,
|
2013-02-13 22:52:05 -06:00
|
|
|
}
|
|
|
|
(keyring_key, auth_ref) = self.get_auth_ref_from_keyring(**kwargs)
|
2012-11-08 16:32:17 -08:00
|
|
|
new_token_needed = False
|
|
|
|
if auth_ref is None or self.force_new_token:
|
|
|
|
new_token_needed = True
|
2013-02-13 22:52:05 -06:00
|
|
|
kwargs['password'] = password
|
2013-12-09 16:46:09 +10:00
|
|
|
resp = self.get_raw_token_from_identity_service(**kwargs)
|
|
|
|
|
|
|
|
if isinstance(resp, access.AccessInfo):
|
|
|
|
self.auth_ref = resp
|
|
|
|
else:
|
|
|
|
self.auth_ref = access.AccessInfo.factory(*resp)
|
|
|
|
|
|
|
|
# NOTE(jamielennox): The original client relies on being able to
|
|
|
|
# push the region name into the service catalog but new auth
|
|
|
|
# it in.
|
|
|
|
if region_name:
|
|
|
|
self.auth_ref.service_catalog._region_name = region_name
|
2012-11-08 16:32:17 -08:00
|
|
|
else:
|
|
|
|
self.auth_ref = auth_ref
|
2013-12-09 16:46:09 +10:00
|
|
|
|
2013-10-22 09:41:00 +10:00
|
|
|
self.process_token(region_name=region_name)
|
2012-11-08 16:32:17 -08:00
|
|
|
if new_token_needed:
|
|
|
|
self.store_auth_ref_into_keyring(keyring_key)
|
|
|
|
return True
|
|
|
|
|
2013-02-13 22:52:05 -06:00
|
|
|
def _build_keyring_key(self, **kwargs):
|
|
|
|
"""Create a unique key for keyring.
|
2012-11-08 16:32:17 -08:00
|
|
|
|
|
|
|
Used to store and retrieve auth_ref from keyring.
|
|
|
|
|
2013-02-13 22:52:05 -06:00
|
|
|
Returns a slash-separated string of values ordered by key name.
|
|
|
|
|
2012-11-08 16:32:17 -08:00
|
|
|
"""
|
2013-02-13 22:52:05 -06:00
|
|
|
return '/'.join([kwargs[k] or '?' for k in sorted(kwargs.keys())])
|
2012-11-08 16:32:17 -08:00
|
|
|
|
2013-02-13 22:52:05 -06:00
|
|
|
def get_auth_ref_from_keyring(self, **kwargs):
|
|
|
|
"""Retrieve auth_ref from keyring.
|
2012-11-08 16:32:17 -08:00
|
|
|
|
|
|
|
If auth_ref is found in keyring, (keyring_key, auth_ref) is returned.
|
|
|
|
Otherwise, (keyring_key, None) is returned.
|
|
|
|
|
|
|
|
:returns: (keyring_key, auth_ref) or (keyring_key, None)
|
2013-05-22 17:00:53 -04:00
|
|
|
:returns: or (None, None) if use_keyring is not set in the object
|
2012-11-08 16:32:17 -08:00
|
|
|
|
|
|
|
"""
|
|
|
|
keyring_key = None
|
|
|
|
auth_ref = None
|
|
|
|
if self.use_keyring:
|
2013-02-13 22:52:05 -06:00
|
|
|
keyring_key = self._build_keyring_key(**kwargs)
|
2012-11-08 16:32:17 -08:00
|
|
|
try:
|
|
|
|
auth_ref = keyring.get_password("keystoneclient_auth",
|
|
|
|
keyring_key)
|
|
|
|
if auth_ref:
|
|
|
|
auth_ref = pickle.loads(auth_ref)
|
2013-05-22 17:00:53 -04:00
|
|
|
if auth_ref.will_expire_soon(self.stale_duration):
|
2012-11-08 16:32:17 -08:00
|
|
|
# token has expired, don't use it
|
|
|
|
auth_ref = None
|
|
|
|
except Exception as e:
|
|
|
|
auth_ref = None
|
2014-05-19 17:13:01 +02:00
|
|
|
_logger.warning('Unable to retrieve token from keyring %s', e)
|
2012-11-08 16:32:17 -08:00
|
|
|
return (keyring_key, auth_ref)
|
|
|
|
|
|
|
|
def store_auth_ref_into_keyring(self, keyring_key):
|
2013-02-13 22:52:05 -06:00
|
|
|
"""Store auth_ref into keyring.
|
2012-11-08 16:32:17 -08:00
|
|
|
|
|
|
|
"""
|
|
|
|
if self.use_keyring:
|
|
|
|
try:
|
|
|
|
keyring.set_password("keystoneclient_auth",
|
|
|
|
keyring_key,
|
|
|
|
pickle.dumps(self.auth_ref))
|
|
|
|
except Exception as e:
|
2014-05-19 17:13:01 +02:00
|
|
|
_logger.warning("Failed to store token into keyring %s", e)
|
2012-11-08 16:32:17 -08:00
|
|
|
|
2014-01-10 16:38:55 +10:00
|
|
|
def _process_management_url(self, region_name):
|
|
|
|
try:
|
|
|
|
self._management_url = self.auth_ref.service_catalog.url_for(
|
|
|
|
service_type='identity',
|
|
|
|
endpoint_type='admin',
|
|
|
|
region_name=region_name)
|
|
|
|
except exceptions.EndpointNotFound:
|
2014-10-10 15:53:37 -07:00
|
|
|
pass
|
2014-01-10 16:38:55 +10:00
|
|
|
|
2013-10-22 09:41:00 +10:00
|
|
|
def process_token(self, region_name=None):
|
2013-02-13 22:52:05 -06:00
|
|
|
"""Extract and process information from the new auth_ref.
|
2012-11-08 16:32:17 -08:00
|
|
|
|
2013-07-18 15:54:41 +10:00
|
|
|
And set the relevant authentication information.
|
2012-11-08 16:32:17 -08:00
|
|
|
"""
|
2013-07-18 15:54:41 +10:00
|
|
|
# if we got a response without a service catalog, set the local
|
|
|
|
# list of tenants for introspection, and leave to client user
|
|
|
|
# to determine what to do. Otherwise, load up the service catalog
|
|
|
|
if self.auth_ref.project_scoped:
|
|
|
|
if not self.auth_ref.tenant_id:
|
|
|
|
raise exceptions.AuthorizationFailure(
|
|
|
|
"Token didn't provide tenant_id")
|
2014-01-10 16:38:55 +10:00
|
|
|
self._process_management_url(region_name)
|
2013-07-18 15:54:41 +10:00
|
|
|
self.project_name = self.auth_ref.tenant_name
|
|
|
|
self.project_id = self.auth_ref.tenant_id
|
|
|
|
|
|
|
|
if not self.auth_ref.user_id:
|
|
|
|
raise exceptions.AuthorizationFailure(
|
|
|
|
"Token didn't provide user_id")
|
|
|
|
|
|
|
|
self.user_id = self.auth_ref.user_id
|
|
|
|
|
|
|
|
self.auth_domain_id = self.auth_ref.domain_id
|
|
|
|
self.auth_tenant_id = self.auth_ref.tenant_id
|
|
|
|
self.auth_user_id = self.auth_ref.user_id
|
2012-11-08 16:32:17 -08:00
|
|
|
|
2013-11-20 12:40:13 +10:00
|
|
|
@property
|
|
|
|
def management_url(self):
|
|
|
|
return self._endpoint or self._management_url
|
|
|
|
|
|
|
|
@management_url.setter
|
|
|
|
def management_url(self, value):
|
|
|
|
# NOTE(jamielennox): it's debatable here whether we should set
|
|
|
|
# _endpoint or _management_url. As historically management_url was set
|
|
|
|
# permanently setting _endpoint would better match that behaviour.
|
|
|
|
self._endpoint = value
|
|
|
|
|
2014-02-28 13:31:00 +10:00
|
|
|
@utils.positional(enforcement=utils.positional.WARN)
|
2012-11-08 16:32:17 -08:00
|
|
|
def get_raw_token_from_identity_service(self, auth_url, username=None,
|
|
|
|
password=None, tenant_name=None,
|
2013-02-13 22:52:05 -06:00
|
|
|
tenant_id=None, token=None,
|
|
|
|
user_id=None, user_domain_id=None,
|
|
|
|
user_domain_name=None,
|
|
|
|
domain_id=None, domain_name=None,
|
|
|
|
project_id=None, project_name=None,
|
|
|
|
project_domain_id=None,
|
2013-08-02 11:45:38 +01:00
|
|
|
project_domain_name=None,
|
|
|
|
trust_id=None):
|
2013-02-13 22:52:05 -06:00
|
|
|
"""Authenticate against the Identity API and get a token.
|
2011-10-25 16:50:08 -07:00
|
|
|
|
|
|
|
Not implemented here because auth protocols should be API
|
|
|
|
version-specific.
|
2012-10-13 00:15:39 +00:00
|
|
|
|
|
|
|
Expected to authenticate or validate an existing authentication
|
|
|
|
reference already associated with the client. Invoking this call
|
2013-02-13 22:52:05 -06:00
|
|
|
*always* makes a call to the Identity service.
|
2012-11-08 16:32:17 -08:00
|
|
|
|
2013-02-13 22:52:05 -06:00
|
|
|
:returns: (``resp``, ``body``)
|
2012-11-08 16:32:17 -08:00
|
|
|
|
2011-10-25 16:50:08 -07:00
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2012-09-11 11:10:40 -05:00
|
|
|
def serialize(self, entity):
|
2013-08-16 11:45:44 +10:00
|
|
|
return jsonutils.dumps(entity)
|
2012-09-11 11:10:40 -05:00
|
|
|
|
2013-09-30 10:35:55 +10:00
|
|
|
@staticmethod
|
|
|
|
def _decode_body(resp):
|
2012-11-16 17:43:05 -06:00
|
|
|
if resp.text:
|
2011-10-25 16:50:08 -07:00
|
|
|
try:
|
2013-08-16 11:45:44 +10:00
|
|
|
body_resp = jsonutils.loads(resp.text)
|
2013-03-13 20:59:03 -07:00
|
|
|
except (ValueError, TypeError):
|
2013-07-31 16:00:58 +10:00
|
|
|
body_resp = None
|
2014-05-19 17:13:01 +02:00
|
|
|
_logger.debug("Could not decode JSON from body: %s",
|
|
|
|
resp.text)
|
2011-10-25 16:50:08 -07:00
|
|
|
else:
|
|
|
|
_logger.debug("No body was returned.")
|
2013-07-31 16:00:58 +10:00
|
|
|
body_resp = None
|
|
|
|
|
2013-09-30 10:35:55 +10:00
|
|
|
return body_resp
|
|
|
|
|
|
|
|
def request(self, url, method, **kwargs):
|
|
|
|
"""Send an http request with the specified characteristics.
|
|
|
|
|
|
|
|
Wrapper around requests.request to handle tasks such as
|
|
|
|
setting headers, JSON encoding/decoding, and error handling.
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
kwargs['json'] = kwargs.pop('body')
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
2013-12-09 16:46:09 +10:00
|
|
|
kwargs.setdefault('authenticated', False)
|
|
|
|
resp = super(HTTPClient, self).request(url, method, **kwargs)
|
2013-09-30 10:35:55 +10:00
|
|
|
return resp, self._decode_body(resp)
|
2011-10-25 16:50:08 -07:00
|
|
|
|
2013-12-05 16:32:56 +10:00
|
|
|
def _cs_request(self, url, method, management=True, **kwargs):
|
2013-02-13 22:52:05 -06:00
|
|
|
"""Makes an authenticated request to keystone endpoint by
|
2012-10-13 00:15:39 +00:00
|
|
|
concatenating self.management_url and url and passing in method and
|
2013-02-13 22:52:05 -06:00
|
|
|
any associated kwargs.
|
|
|
|
"""
|
2014-02-19 15:35:46 +10:00
|
|
|
# NOTE(jamielennox): remember that if you use the legacy client mode
|
|
|
|
# (you create a client without a session) then this HTTPClient object
|
|
|
|
# is the auth plugin you are using. Values in the endpoint_filter may
|
|
|
|
# be ignored and you should look at get_endpoint to figure out what.
|
2013-12-05 16:32:56 +10:00
|
|
|
interface = 'admin' if management else 'public'
|
|
|
|
endpoint_filter = kwargs.setdefault('endpoint_filter', {})
|
|
|
|
endpoint_filter.setdefault('service_type', 'identity')
|
|
|
|
endpoint_filter.setdefault('interface', interface)
|
2011-10-25 16:50:08 -07:00
|
|
|
|
2014-02-19 15:35:46 +10:00
|
|
|
if self.version:
|
|
|
|
endpoint_filter.setdefault('version', self.version)
|
|
|
|
|
2013-12-05 16:32:56 +10:00
|
|
|
if self.region_name:
|
|
|
|
endpoint_filter.setdefault('region_name', self.region_name)
|
2012-11-23 09:17:24 +00:00
|
|
|
|
2013-12-05 16:32:56 +10:00
|
|
|
kwargs.setdefault('authenticated', None)
|
|
|
|
try:
|
|
|
|
return self.request(url, method, **kwargs)
|
|
|
|
except exceptions.MissingAuthPlugin:
|
|
|
|
_logger.info('Cannot get authenticated endpoint without an '
|
|
|
|
'auth plugin')
|
2012-10-13 00:15:39 +00:00
|
|
|
raise exceptions.AuthorizationFailure(
|
|
|
|
'Current authorization does not have a known management url')
|
2012-11-23 09:17:24 +00:00
|
|
|
|
2011-10-25 16:50:08 -07:00
|
|
|
def get(self, url, **kwargs):
|
|
|
|
return self._cs_request(url, 'GET', **kwargs)
|
|
|
|
|
2012-09-11 11:06:54 -05:00
|
|
|
def head(self, url, **kwargs):
|
|
|
|
return self._cs_request(url, 'HEAD', **kwargs)
|
|
|
|
|
2011-10-25 16:50:08 -07:00
|
|
|
def post(self, url, **kwargs):
|
|
|
|
return self._cs_request(url, 'POST', **kwargs)
|
|
|
|
|
|
|
|
def put(self, url, **kwargs):
|
|
|
|
return self._cs_request(url, 'PUT', **kwargs)
|
|
|
|
|
2012-09-11 11:06:54 -05:00
|
|
|
def patch(self, url, **kwargs):
|
|
|
|
return self._cs_request(url, 'PATCH', **kwargs)
|
|
|
|
|
2011-10-25 16:50:08 -07:00
|
|
|
def delete(self, url, **kwargs):
|
|
|
|
return self._cs_request(url, 'DELETE', **kwargs)
|
2013-09-30 10:35:55 +10:00
|
|
|
|
|
|
|
# DEPRECATIONS: The following methods are no longer directly supported
|
|
|
|
# but maintained for compatibility purposes.
|
|
|
|
|
|
|
|
deprecated_session_variables = {'original_ip': None,
|
|
|
|
'cert': None,
|
|
|
|
'timeout': None,
|
|
|
|
'verify_cert': 'verify'}
|
|
|
|
|
|
|
|
def __getattr__(self, name):
|
|
|
|
# FIXME(jamielennox): provide a proper deprecated warning
|
|
|
|
try:
|
|
|
|
var_name = self.deprecated_session_variables[name]
|
|
|
|
except KeyError:
|
|
|
|
raise AttributeError("Unknown Attribute: %s" % name)
|
|
|
|
|
|
|
|
return getattr(self.session, var_name or name)
|
|
|
|
|
|
|
|
def __setattr__(self, name, val):
|
|
|
|
# FIXME(jamielennox): provide a proper deprecated warning
|
|
|
|
try:
|
|
|
|
var_name = self.deprecated_session_variables[name]
|
|
|
|
except KeyError:
|
|
|
|
super(HTTPClient, self).__setattr__(name, val)
|
|
|
|
else:
|
|
|
|
setattr(self.session, var_name or name)
|