← Back to team overview

canonical-ubuntu-qa team mailing list archive

[Merge] ~andersson123/autopkgtest-cloud:api-key-dont-load-in-global-namespace into autopkgtest-cloud:master

 

Tim Andersson has proposed merging ~andersson123/autopkgtest-cloud:api-key-dont-load-in-global-namespace into autopkgtest-cloud:master.

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

For more details, see:
https://code.launchpad.net/~andersson123/autopkgtest-cloud/+git/autopkgtest-cloud/+merge/462404
-- 
Your team Canonical's Ubuntu QA is requested to review the proposed merge of ~andersson123/autopkgtest-cloud:api-key-dont-load-in-global-namespace into autopkgtest-cloud:master.
diff --git a/charms/focal/autopkgtest-web/webcontrol/request/app.py b/charms/focal/autopkgtest-web/webcontrol/request/app.py
index c58cf7f..7707996 100644
--- a/charms/focal/autopkgtest-web/webcontrol/request/app.py
+++ b/charms/focal/autopkgtest-web/webcontrol/request/app.py
@@ -55,21 +55,6 @@ SUCCESS = """
 </dl>
 """
 
-# API keys is a json file like this:
-# {
-#     "user1": "user1s-apikey",
-#     "user2": "user2s-apikey",
-# }
-try:
-    API_KEYS = json.loads(
-        pathlib.Path(
-            "/home/ubuntu/external-web-requests-api-keys.json"
-        ).read_text()
-    )
-except Exception as e:
-    logging.warning("Failed to read API keys: %s", e)
-    API_KEYS = {}
-
 
 def check_github_sig(request):
     """Validate github signature of request.
@@ -126,6 +111,25 @@ def maybe_escape(value):
     return _escape(value) if value else value
 
 
+def get_api_keys():
+    """
+    API keys is a json file like this:
+    {
+        "user1": "user1s-apikey",
+        "user2": "user2s-apikey",
+    }
+    """
+    try:
+        api_keys = json.loads(
+            pathlib.Path(
+                "/home/ubuntu/external-web-requests-api-keys.json"
+            ).read_text()
+        )
+    except Exception as _:
+        api_keys = {}
+    return api_keys
+
+
 # Initialize app
 PATH = os.path.join(
     os.path.sep, os.getenv("XDG_RUNTIME_DIR", "/run"), "autopkgtest_webcontrol"
@@ -155,7 +159,8 @@ def index_root():
         request_creds_sha1 = hmac.new(
             key_user.encode(), api_key.encode(), "sha1"
         ).hexdigest()
-        for user, user_key in API_KEYS.items():
+        api_keys = get_api_keys()
+        for user, user_key in api_keys.items():
             iter_creds_sha1 = hmac.new(
                 user.encode(), user_key.encode(), "sha1"
             ).hexdigest()