From 19591a51e4f9fe5b10f3cf4146277f169786450f Mon Sep 17 00:00:00 2001 From: Chmouel Boudjnah Date: Thu, 20 Jun 2013 18:24:35 +0200 Subject: [PATCH] Fix the cache interface to use time= by default. Historically the swift cache conection used the argument timeout= for the cache timeout, but this has been unified with the official python memcache client with time= since grizzly, we still need to handle folsom for a while until this could get removed. Fixes bug 1193032 Change-Id: Ia9d544a0277beb197ed9824d7c1266d12968393d --- keystoneclient/middleware/auth_token.py | 14 +++++++------- tests/test_auth_token_middleware.py | 22 ++++++++++++++-------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/keystoneclient/middleware/auth_token.py b/keystoneclient/middleware/auth_token.py index e50f723c8..8148d8ada 100644 --- a/keystoneclient/middleware/auth_token.py +++ b/keystoneclient/middleware/auth_token.py @@ -331,7 +331,6 @@ class AuthProtocol(object): # Token caching via memcache self._cache = None - self._use_keystone_cache = False self._cache_initialized = False # cache already initialzied? # memcache value treatment, ENCRYPT or MAC self._memcache_security_strategy = \ @@ -372,7 +371,6 @@ class AuthProtocol(object): else: # use Keystone memcache self._cache = memorycache.get_client(memcache_servers) - self._use_keystone_cache = True self._cache_initialized = True def _conf_get(self, name): @@ -914,14 +912,16 @@ class AuthProtocol(object): cache_key = CACHE_KEY_TEMPLATE % memcache_crypt.get_cache_key(keys) data_to_store = memcache_crypt.protect_data(keys, serialized_data) - # we need to special-case set() because of the incompatibility between - # Swift MemcacheRing and python-memcached. See - # https://bugs.launchpad.net/swift/+bug/1095730 - if self._use_keystone_cache: + # Historically the swift cache conection used the argument + # timeout= for the cache timeout, but this has been unified + # with the official python memcache client with time= since + # grizzly, we still need to handle folsom for a while until + # this could get removed. + try: self._cache.set(cache_key, data_to_store, time=self.token_cache_time) - else: + except(TypeError): self._cache.set(cache_key, data_to_store, timeout=self.token_cache_time) diff --git a/tests/test_auth_token_middleware.py b/tests/test_auth_token_middleware.py index a4285043c..5492656b7 100644 --- a/tests/test_auth_token_middleware.py +++ b/tests/test_auth_token_middleware.py @@ -414,10 +414,10 @@ class DisableModuleFixture(fixtures.Fixture): sys.meta_path.insert(0, finder) -class FakeSwiftMemcacheRing(memorycache.Client): - # NOTE(vish): swift memcache uses param timeout instead of time +class FakeSwiftOldMemcacheClient(memorycache.Client): + # NOTE(vish,chmou): old swift memcache uses param timeout instead of time def set(self, key, value, timeout=0, min_compress_len=0): - sup = super(FakeSwiftMemcacheRing, self) + sup = super(FakeSwiftOldMemcacheClient, self) sup.set(key, value, timeout, min_compress_len) @@ -1036,16 +1036,18 @@ class AuthTokenMiddlewareTest(BaseAuthTokenMiddlewareTest): self.assertRaises(auth_token.InvalidUserToken, self._get_cached_token, token) - def test_memcache_set_expired(self): + def test_memcache_set_expired(self, extra_conf={}, extra_environ={}): token_cache_time = 10 conf = { 'token_cache_time': token_cache_time, 'signing_dir': CERTDIR, } + conf.update(extra_conf) self.set_middleware(conf=conf) req = webob.Request.blank('/') token = self.token_dict['signed_token_scoped'] req.headers['X-Auth-Token'] = token + req.environ.update(extra_environ) try: now = datetime.datetime.utcnow() timeutils.set_time_override(now) @@ -1057,11 +1059,15 @@ class AuthTokenMiddlewareTest(BaseAuthTokenMiddlewareTest): finally: timeutils.clear_time_override() + def test_old_swift_memcache_set_expired(self): + extra_conf = {'cache': 'swift.cache'} + extra_environ = {'swift.cache': FakeSwiftOldMemcacheClient()} + self.test_memcache_set_expired(extra_conf, extra_environ) + def test_swift_memcache_set_expired(self): - self.middleware._cache = FakeSwiftMemcacheRing() - self.middleware._use_keystone_cache = False - self.middleware._cache_initialized = True - self.test_memcache_set_expired() + extra_conf = {'cache': 'swift.cache'} + extra_environ = {'swift.cache': memorycache.Client()} + self.test_memcache_set_expired(extra_conf, extra_environ) def test_use_cache_from_env(self): env = {'swift.cache': 'CACHE_TEST'}