about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2023-06-05 06:41:43 +0300
committerFrederick Muriuki Muriithi2023-06-05 06:41:43 +0300
commitbfb6fdee924cc60dfdba8ede609a206ca6982454 (patch)
tree5e62d8e5b9aa4b81f24f58bc271d8bf9ce416999
parent1a22f2b5dedd3a685bb2869f1b51f590eca36c5a (diff)
downloadgenenetwork3-bfb6fdee924cc60dfdba8ede609a206ca6982454.tar.gz
Logging: Don't allow getting user info to stop application starting
In certain scenarios (probably due to host contamination) the effective UID of
the running application is not the expected "genenetwork" user, rather, it is
a user on the host system, which leads to the error:

    KeyError: 'getpwuid(): uid not found: 1000'

This commit prevents the application from failing in such a case, but still
logs out such weirdness.
-rw-r--r--gn3/app.py11
-rw-r--r--gn3/auth/db.py2
2 files changed, 9 insertions, 4 deletions
diff --git a/gn3/app.py b/gn3/app.py
index ffde223..da87f2b 100644
--- a/gn3/app.py
+++ b/gn3/app.py
@@ -3,6 +3,7 @@ import os
 import sys
 import logging
 import getpass
+import traceback
 
 from typing import Dict
 from typing import Union
@@ -48,9 +49,13 @@ def create_app(config: Union[Dict, str, None] = None) -> Flask:
 
     setup_app_handlers(app)
     # DO NOT log anything before this point
-    logging.info("Guix Profile: '%s'." % (os.environ.get("GUIX_PROFILE"),))
-    logging.info("Python Executable: '%s'." % sys.executable)
-    logging.info("Effective User: '%s'." % getpass.getuser())
+    logging.info("Guix Profile: '%s'.", os.environ.get("GUIX_PROFILE"))
+    logging.info("Python Executable: '%s'.", sys.executable)
+    try:
+        logging.info("Effective User: '%s'.", getpass.getuser())
+    except KeyError as kerr:
+        logging.debug("User Error: %s", kerr.args[0])
+        logging.error(traceback.format_exc())
 
     CORS(
         app,
diff --git a/gn3/auth/db.py b/gn3/auth/db.py
index 7c20bf7..2ba6619 100644
--- a/gn3/auth/db.py
+++ b/gn3/auth/db.py
@@ -48,7 +48,7 @@ class DbCursor(Protocol):
 @contextlib.contextmanager
 def connection(db_path: str, row_factory: Callable = sqlite3.Row) -> Iterator[DbConnection]:
     """Create the connection to the auth database."""
-    logging.debug("SQLite3 DB Path: '%s'." % (db_path,))
+    logging.debug("SQLite3 DB Path: '%s'.", db_path)
     conn = sqlite3.connect(db_path)
     conn.row_factory = row_factory
     conn.set_trace_callback(logging.debug)