diff options
author | Rob Crittenden <rcritten@redhat.com> | 2013-05-22 17:00:53 -0400 |
---|---|---|
committer | Rob Crittenden <rcritten@redhat.com> | 2013-06-10 11:40:12 -0400 |
commit | 68eed64a02bea4a3adb4c8761f8d8bc4f55e5ecd (patch) | |
tree | a417a9494a01142b004f112a29399d5633c7819f /keystoneclient/client.py | |
parent | 2ec2e7ee31a9a7305d524bceaf2c10f89ef27f02 (diff) | |
download | python-keystoneclient-68eed64a02bea4a3adb4c8761f8d8bc4f55e5ecd.tar.gz |
Fix optional keyring support, add basic keyring tests
Commit 06d9437e8388b369546d760607f17cb5022750e9 made using a keyring
optional by adding a function to handle the imports. The problem is that
import needs to be available at a global level for it to be usable in
other parts of the code.
The function is replaced by a top-level try/except.
Fixing the imports isn't enough to get keyring support working because
in get_auth_ref_from_keyring() the wrong token was being used when
evaluating token expiration. It was using the token already in the
HTTPClient object and not the one returned from the keyring.
Some simple tests were added to prevent future regressions. These
tests will be skipped if the keyring or pickle packages are not
installed.
Change-Id: I1fe2c9e5cdf275df2047018368da2e4b3d2d6de2
Fixes: bug #1183072
Diffstat (limited to 'keystoneclient/client.py')
-rw-r--r-- | keystoneclient/client.py | 28 |
1 files changed, 12 insertions, 16 deletions
diff --git a/keystoneclient/client.py b/keystoneclient/client.py index 5b2f125..9c00855 100644 --- a/keystoneclient/client.py +++ b/keystoneclient/client.py @@ -10,12 +10,18 @@ OpenStack Client interface. Handles the REST calls and responses. import copy import logging -import sys import urlparse import requests try: + import keyring + import pickle +except ImportError: + keyring = None + pickle = None + +try: import json except ImportError: import simplejson as json @@ -33,19 +39,6 @@ from keystoneclient import exceptions _logger = logging.getLogger(__name__) -def try_import_keyring(): - try: - import keyring # noqa - import pickle # noqa - return True - except ImportError: - if (hasattr(sys.stderr, 'isatty') and sys.stderr.isatty()): - print >> sys.stderr, 'Failed to load keyring modules.' - else: - _logger.warning('Failed to load keyring modules.') - return False - - class HTTPClient(object): USER_AGENT = 'python-keystoneclient' @@ -121,7 +114,9 @@ class HTTPClient(object): requests.logging.getLogger(requests.__name__).addHandler(ch) # keyring setup - self.use_keyring = use_keyring and try_import_keyring() + if use_keyring and keyring is None: + _logger.warning('Failed to load keyring modules.') + self.use_keyring = use_keyring and keyring is not None self.force_new_token = force_new_token self.stale_duration = stale_duration or access.STALE_TOKEN_DURATION self.stale_duration = int(self.stale_duration) @@ -233,6 +228,7 @@ class HTTPClient(object): Otherwise, (keyring_key, None) is returned. :returns: (keyring_key, auth_ref) or (keyring_key, None) + :returns: or (None, None) if use_keyring is not set in the object """ keyring_key = None @@ -246,7 +242,7 @@ class HTTPClient(object): keyring_key) if auth_ref: auth_ref = pickle.loads(auth_ref) - if self.auth_ref.will_expire_soon(self.stale_duration): + if auth_ref.will_expire_soon(self.stale_duration): # token has expired, don't use it auth_ref = None except Exception as e: |