summaryrefslogtreecommitdiffstats
path: root/pykolab
diff options
context:
space:
mode:
authorJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2020-10-02 15:44:14 +0200
committerJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2022-02-03 11:30:18 +0100
commitd2f1fed940ba610230b8e78a6748d43c90ba8002 (patch)
treeb02cd347fefc3e244bdbbffa4cd9551ede88e61a /pykolab
parent16bbda7fa28adbeb1da1f38d6126363db9eefaea (diff)
downloadpykolab-d2f1fed940ba610230b8e78a6748d43c90ba8002.tar.gz
Address stale cache entries
Summary: Avoid stale cache entries from making the cut, as well as allow an explicit auth_cache_uri=null Test Plan: None Reviewers: #pykolab_developers Subscribers: #pykolab_developers Differential Revision: https://git.kolab.org/D1690
Diffstat (limited to 'pykolab')
-rw-r--r--pykolab/auth/ldap/auth_cache.py36
1 files changed, 29 insertions, 7 deletions
diff --git a/pykolab/auth/ldap/auth_cache.py b/pykolab/auth/ldap/auth_cache.py
index f32779c..dcf8c23 100644
--- a/pykolab/auth/ldap/auth_cache.py
+++ b/pykolab/auth/ldap/auth_cache.py
@@ -17,6 +17,7 @@
#
import datetime
+import os
import sqlalchemy
@@ -84,6 +85,11 @@ def del_entry(key):
db = init_db()
+ if db is None:
+ return
+
+ purge_entries()
+
try:
db.query(Entry).filter_by(key=key).delete()
except sqlalchemy.exc.OperationalError:
@@ -102,6 +108,9 @@ def get_entry(key):
db = init_db()
+ if db is None:
+ return None
+
try:
_entries = db.query(Entry).filter_by(key=key).all()
except sqlalchemy.exc.OperationalError:
@@ -121,8 +130,14 @@ def get_entry(key):
def set_entry(key, value):
+ # pylint: disable=global-statement
+ global db
+
db = init_db()
+ if db is None:
+ return
+
try:
_entries = db.query(Entry).filter_by(key=key).all()
except sqlalchemy.exc.OperationalError:
@@ -147,12 +162,18 @@ def set_entry(key, value):
db.commit()
-def purge_entries(db):
- db.query(Entry).filter(
- Entry.last_change <= (datetime.datetime.now() - datetime.timedelta(1))
- ).delete()
+def purge_entries():
+ # pylint: disable=global-statement
+ global db
- db.commit()
+ try:
+ db.query(Entry).filter(
+ Entry.last_change <= (datetime.datetime.now() - datetime.timedelta(1))
+ ).delete()
+
+ db.commit()
+ except Exception:
+ return
def init_db(reinit=False):
@@ -167,11 +188,13 @@ def init_db(reinit=False):
db_uri = conf.get('ldap', 'auth_cache_uri')
+ if db_uri == "null":
+ return None
+
if db_uri is None:
db_uri = 'sqlite:///%s/auth_cache.db' % (KOLAB_LIB_PATH)
if reinit:
- import os
if os.path.isfile('%s/auth_cache.db' % (KOLAB_LIB_PATH)):
os.unlink('%s/auth_cache.db' % (KOLAB_LIB_PATH))
@@ -181,6 +204,5 @@ def init_db(reinit=False):
Session = sessionmaker(bind=engine)
db = Session()
- purge_entries(db)
return db