Skip to content
Permalink
Browse files

Pull up r2177: Error cleaning digest credentials

  • Loading branch information...
wsanchez committed Mar 4, 2008
1 parent ca01d7f commit 16517930622ed1cab727f92dcef24a6061c8de8f
Showing with 83 additions and 13 deletions.
  1. +83 −13 twistedcaldav/directory/digest.py
@@ -90,6 +90,15 @@ def delete(self, key):
"""
pass

def deleteMany(self, keys):
"""
Remove the records associated with the supplied keys.
@param key: the key to remove.
@type key: C{str}
"""
pass

def keys(self):
"""
Return all the keys currently available.
@@ -98,6 +107,14 @@ def keys(self):
"""
pass

def items(self):
"""
Return all the key/value pairs currently available.
@return: a C{list} of C{tuple} for each key/value currently in the database.
"""
pass

class DigestCredentialsMap(object):

implements(IDigestCredentialsDatabase)
@@ -133,12 +150,26 @@ def delete(self, key):
if self.db.has_key(key):
del self.db[key]

def deleteMany(self, keys):
"""
See IDigestCredentialsDatabase.
"""
for key in keys:
if self.db.has_key(key):
del self.db[key]

def keys(self):
"""
See IDigestCredentialsDatabase.
"""
return self.db.keys()

def items(self):
"""
See IDigestCredentialsDatabase.
"""
return self.db.items()

class DigestCredentialsDB(AbstractSQLDatabase):

implements(IDigestCredentialsDatabase)
@@ -162,7 +193,7 @@ class DigestCredentialsDB(AbstractSQLDatabase):

def __init__(self, path):
db_path = os.path.join(path, DigestCredentialsDB.dbFilename)
super(DigestCredentialsDB, self).__init__(db_path, autocommit=True)
super(DigestCredentialsDB, self).__init__(db_path, autocommit=False)
self.exceptions = 0

def has_key(self, key):
@@ -192,8 +223,10 @@ def set(self, key, value):
try:
pvalue = pickle.dumps(value)
self._set_in_db(key, pvalue)
self._db_commit()
self.exceptions = 0
except OperationalError, e:
self._db_rollback()
self.exceptions += 1
if self.exceptions >= self.exceptionLimit:
self._db_close()
@@ -227,8 +260,27 @@ def delete(self, key):
"""
try:
self._delete_from_db(key)
self._db_commit()
self.exceptions = 0
except OperationalError, e:
self._db_rollback()
self.exceptions += 1
if self.exceptions >= self.exceptionLimit:
self._db_close()
log.err("Reset digest credentials database connection: %s" % (e,))
raise

def deleteMany(self, keys):
"""
See IDigestCredentialsDatabase.
"""
try:
for key in keys:
self._delete_from_db(key)
self._db_commit()
self.exceptions = 0
except OperationalError, e:
self._db_rollback()
self.exceptions += 1
if self.exceptions >= self.exceptionLimit:
self._db_close()
@@ -253,6 +305,24 @@ def keys(self):
log.err("Reset digest credentials database connection: %s" % (e,))
raise

def items(self):
"""
See IDigestCredentialsDatabase.
"""
try:
result = []
for key in self._db_execute("select KEY, VALUE from DIGESTCREDENTIALS"):
result.append((str(key[0]), pickle.loads(str(key[1])),))

self.exceptions = 0
return result
except OperationalError, e:
self.exceptions += 1
if self.exceptions >= self.exceptionLimit:
self._db_close()
log.err("Reset digest credentials database connection: %s" % (e,))
raise

def _set_in_db(self, key, value):
"""
Insert the specified entry into the database, replacing any that might already exist.
@@ -503,16 +573,16 @@ def cleanup(self):
"""
This should be called at regular intervals to remove expired credentials from the cache.
"""
keys = self.db.keys()
items = self.db.items()
oldest_allowed = time.time() - DigestCredentialFactory.CHALLENGE_LIFETIME_SECS
for key in keys:
try:
value = self.db.get(key)
if value is not None:
ignore_clientip, ignore_cnonce, db_timestamp = value
if db_timestamp <= oldest_allowed:
self.invalidate(key)
except Exception, e:
# Clean-up errors can be logged but we should ignore them
log.err("Error cleaning digest credentials: %s" % (e,))
pass
delete_keys = []
for key, value in items:
ignore_clientip, ignore_cnonce, db_timestamp = value
if db_timestamp <= oldest_allowed:
delete_keys.append(key)

try:
self.db.deleteMany(delete_keys)
except Exception, e:
# Clean-up errors can be logged but we should ignore them
log.err("Failed to clean digest credentials: %s" % (e,))

0 comments on commit 1651793

Please sign in to comment.
You can’t perform that action at this time.