← Back to team overview

canonical-ubuntu-qa team mailing list archive

[Merge] ~uralt/autopkgtest-cloud:allowed-user-cache into autopkgtest-cloud:master

 

Ural Tunaboyu has proposed merging ~uralt/autopkgtest-cloud:allowed-user-cache into autopkgtest-cloud:master.

Requested reviews:
  Skia (hyask)
  Canonical's Ubuntu QA (canonical-ubuntu-qa)

For more details, see:
https://code.launchpad.net/~uralt/autopkgtest-cloud/+git/autopkgtest-cloud/+merge/470495

Adds a simple in-memory cache for whether a user is in a team allowed to make a test request. Users who fail to authenticate are not cached, so users who are newly authorized should not have to wait to get access. As it stands, cache duration is 3 hours. 
-- 
Your team Canonical's Ubuntu QA is requested to review the proposed merge of ~uralt/autopkgtest-cloud:allowed-user-cache into autopkgtest-cloud:master.
diff --git a/charms/focal/autopkgtest-web/webcontrol/request/submit.py b/charms/focal/autopkgtest-web/webcontrol/request/submit.py
index 316d15e..4008f07 100644
--- a/charms/focal/autopkgtest-web/webcontrol/request/submit.py
+++ b/charms/focal/autopkgtest-web/webcontrol/request/submit.py
@@ -13,7 +13,7 @@ import sqlite3
 import urllib.parse
 import urllib.request
 import uuid
-from datetime import datetime
+from datetime import datetime, timedelta
 from urllib.error import HTTPError
 
 import amqplib.client_0_8 as amqp
@@ -57,6 +57,10 @@ QUEUE_FP = "/var/lib/cache-amqp/queued.json"
 # Path to json file detailing the running tests
 RUNNING_FP = "/run/amqp-status-collector/running.json"
 
+ALLOWED_USER_CACHE_TIME = timedelta(hours=3)
+
+allowed_users_cache = {}
+
 
 class Submit:
     def __init__(self):
@@ -526,6 +530,15 @@ class Submit:
     # pylint: disable=dangerous-default-value
     def in_allowed_team(self, person, teams=[]):
         """Check if person is in ALLOWED_REQUESTOR_TEAMS"""
+        global allowed_users_cache
+        if person in allowed_users_cache:
+            cache_entry = allowed_users_cache[person]
+            cache_age = datetime.now() - cache_entry
+            if cache_age <= ALLOWED_USER_CACHE_TIME:
+                return True
+            else:
+                del allowed_users_cache[person]
+
         # In the case someone is in more than 300 teams, and the first
         # 300 teams are alphabetically before "autopkgtest-requestors",
         # the following will fail.
@@ -536,6 +549,7 @@ class Submit:
         for e in entries:
             for team in teams or ALLOWED_REQUESTOR_TEAMS:
                 if team in e["team_link"]:
+                    allowed_users_cache[person] = datetime.now()
                     return True
         return False