Fixed to work with newer Swift
This commit is contained in:
parent
ead78c7bfe
commit
c44b5b6448
@ -39,9 +39,13 @@ from swift.common.utils import cache_from_env, get_logger, get_remote_client, \
|
||||
split_path, TRUE_VALUES, urlparse
|
||||
import swift.common.wsgi
|
||||
|
||||
from swauth import swift_version
|
||||
import swauth.authtypes
|
||||
|
||||
|
||||
MEMCACHE_TIME = swift_version.newer_than('1.7.7-dev')
|
||||
|
||||
|
||||
class Swauth(object):
|
||||
"""
|
||||
Scalable authentication and authorization system that uses Swift as its
|
||||
@ -336,9 +340,14 @@ class Swauth(object):
|
||||
expires_from_now = float(resp.getheader('x-auth-ttl'))
|
||||
groups = resp.getheader('x-auth-groups')
|
||||
if memcache_client:
|
||||
memcache_client.set(memcache_key,
|
||||
(time() + expires_from_now, groups),
|
||||
timeout=expires_from_now)
|
||||
if MEMCACHE_TIME:
|
||||
memcache_client.set(
|
||||
memcache_key, (time() + expires_from_now, groups),
|
||||
time=expires_from_now)
|
||||
else:
|
||||
memcache_client.set(
|
||||
memcache_key, (time() + expires_from_now, groups),
|
||||
timeout=expires_from_now)
|
||||
else:
|
||||
path = quote('/v1/%s/.token_%s/%s' %
|
||||
(self.auth_account, token[-1], token))
|
||||
@ -357,9 +366,16 @@ class Swauth(object):
|
||||
groups.append(detail['account_id'])
|
||||
groups = ','.join(groups)
|
||||
if memcache_client:
|
||||
memcache_client.set(memcache_key,
|
||||
(detail['expires'], groups),
|
||||
timeout=float(detail['expires'] - time()))
|
||||
if MEMCACHE_TIME:
|
||||
memcache_client.set(
|
||||
memcache_key,
|
||||
(detail['expires'], groups),
|
||||
time=float(detail['expires'] - time()))
|
||||
else:
|
||||
memcache_client.set(
|
||||
memcache_key,
|
||||
(detail['expires'], groups),
|
||||
timeout=float(detail['expires'] - time()))
|
||||
return groups
|
||||
|
||||
def authorize(self, req):
|
||||
@ -1377,9 +1393,18 @@ class Swauth(object):
|
||||
if not memcache_client:
|
||||
raise Exception(
|
||||
'No memcache set up; required for Swauth middleware')
|
||||
memcache_client.set(memcache_key, (self.itoken_expires,
|
||||
'.auth,.reseller_admin,%s.auth' % self.reseller_prefix),
|
||||
timeout=self.token_life)
|
||||
if MEMCACHE_TIME:
|
||||
memcache_client.set(
|
||||
memcache_key,
|
||||
(self.itoken_expires,
|
||||
'.auth,.reseller_admin,%s.auth' % self.reseller_prefix),
|
||||
time=self.token_life)
|
||||
else:
|
||||
memcache_client.set(
|
||||
memcache_key,
|
||||
(self.itoken_expires,
|
||||
'.auth,.reseller_admin,%s.auth' % self.reseller_prefix),
|
||||
timeout=self.token_life)
|
||||
return self.itoken
|
||||
|
||||
def get_admin_detail(self, req):
|
||||
|
71
swauth/swift_version.py
Normal file
71
swauth/swift_version.py
Normal file
@ -0,0 +1,71 @@
|
||||
import swift
|
||||
|
||||
|
||||
MAJOR = None
|
||||
MINOR = None
|
||||
REVISION = None
|
||||
FINAL = None
|
||||
|
||||
|
||||
def parse(value):
|
||||
parts = value.split('.')
|
||||
if parts[-1].endswith('-dev'):
|
||||
final = False
|
||||
parts[-1] = parts[-1][:-4]
|
||||
else:
|
||||
final = True
|
||||
major = int(parts.pop(0))
|
||||
minor = int(parts.pop(0))
|
||||
if parts:
|
||||
revision = int(parts.pop(0))
|
||||
else:
|
||||
revision = 0
|
||||
return major, minor, revision, final
|
||||
|
||||
|
||||
def newer_than(value):
|
||||
global MAJOR, MINOR, REVISION, FINAL
|
||||
major, minor, revision, final = parse(value)
|
||||
if MAJOR is None:
|
||||
MAJOR, MINOR, REVISION, FINAL = parse(swift.__version__)
|
||||
if MAJOR < major:
|
||||
return False
|
||||
elif MAJOR == major:
|
||||
if MINOR < minor:
|
||||
return False
|
||||
elif MINOR == minor:
|
||||
if REVISION < revision:
|
||||
return False
|
||||
elif REVISION == revision:
|
||||
if not FINAL or final:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def run_tests():
|
||||
global MAJOR, MINOR, REVISION, FINAL
|
||||
MAJOR, MINOR, REVISION, FINAL = parse('1.3')
|
||||
assert(newer_than('1.2'))
|
||||
assert(newer_than('1.2.9'))
|
||||
assert(newer_than('1.3-dev'))
|
||||
assert(newer_than('1.3.0-dev'))
|
||||
assert(not newer_than('1.3'))
|
||||
assert(not newer_than('1.3.0'))
|
||||
assert(not newer_than('1.3.1-dev'))
|
||||
assert(not newer_than('1.3.1'))
|
||||
assert(not newer_than('1.4'))
|
||||
assert(not newer_than('2.0'))
|
||||
MAJOR, MINOR, REVISION, FINAL = parse('1.7.7-dev')
|
||||
assert(newer_than('1.6'))
|
||||
assert(newer_than('1.7'))
|
||||
assert(newer_than('1.7.6-dev'))
|
||||
assert(newer_than('1.7.6'))
|
||||
assert(not newer_than('1.7.7'))
|
||||
assert(not newer_than('1.7.8-dev'))
|
||||
assert(not newer_than('1.7.8'))
|
||||
assert(not newer_than('1.8.0'))
|
||||
assert(not newer_than('2.0'))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run_tests()
|
@ -39,16 +39,16 @@ class FakeMemcache(object):
|
||||
def get(self, key):
|
||||
return self.store.get(key)
|
||||
|
||||
def set(self, key, value, timeout=0):
|
||||
def set(self, key, value, timeout=0, time=0):
|
||||
self.store[key] = value
|
||||
return True
|
||||
|
||||
def incr(self, key, timeout=0):
|
||||
def incr(self, key, timeout=0, time=0):
|
||||
self.store[key] = self.store.setdefault(key, 0) + 1
|
||||
return self.store[key]
|
||||
|
||||
@contextmanager
|
||||
def soft_lock(self, key, timeout=0, retries=5):
|
||||
def soft_lock(self, key, timeout=0, retries=5, time=0):
|
||||
yield True
|
||||
|
||||
def delete(self, key):
|
||||
|
Loading…
x
Reference in New Issue
Block a user