From 19aefc85e6c3801ac6c77246c1534fc9758739df Mon Sep 17 00:00:00 2001 From: Nick Sweeting Date: Thu, 8 Feb 2024 18:58:12 -0800 Subject: [PATCH] fix get_system_user failing on uid 999 in k3s --- archivebox/config.py | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/archivebox/config.py b/archivebox/config.py index 3186a6b0..1edd2eeb 100644 --- a/archivebox/config.py +++ b/archivebox/config.py @@ -366,24 +366,32 @@ ALLOWDENYLIST_REGEX_FLAGS: int = re.IGNORECASE | re.UNICODE | re.MULTILINE ############################## Version Config ################################## -def get_system_user(): - SYSTEM_USER = getpass.getuser() or os.getlogin() +def get_system_user() -> str: + # some host OS's are unable to provide a username (k3s, Windows), making this complicated + # uid 999 is especially problematic and breaks many attempts + SYSTEM_USER = None + FALLBACK_USER_PLACHOLDER = f'user_{os.getuid()}' + + # Option 1 try: import pwd - return pwd.getpwuid(os.geteuid()).pw_name or SYSTEM_USER - except KeyError: - # Process' UID might not map to a user in cases such as running the Docker image - # (where `archivebox` is 999) as a different UID. - pass - except ModuleNotFoundError: - # pwd doesn't exist on windows - pass - except Exception: - # this should never happen, uncomment to debug - # raise + SYSTEM_USER = SYSTEM_USER or pwd.getpwuid(os.geteuid()).pw_name + except (ModuleNotFoundError, Exception): pass - return SYSTEM_USER + # Option 2 + try: + SYSTEM_USER = SYSTEM_USER or getpass.getuser() + except Exception: + pass + + # Option 3 + try: + SYSTEM_USER = SYSTEM_USER or os.getlogin() + except Exception: + pass + + return SYSTEM_USER or FALLBACK_USER_PLACHOLDER def get_version(config): try: